Embedded Programming

Embedded Programming

By Saheen Palayi | 18 March 2021 | FAB Academy 2021

week 8 assignment is to read a microcontroller data sheet and to program the board (which created on week6) to do something, with as many different programming languages and programming environments as possible

Embedded System

Multiple components interfaces jointed together and also embed in to a chip or chip based board for performing a dedicated task is called embedded system. Mostly in a Embedded system contains a programable chip like Microcontroller ,Microprocessors or FPGA chip etc..


Embedded_system

The Picture shows the applications of embedded system in Everyday life.many of them based on microcontrollers

Microcontrollers

we all know about computer and its CPU Unit.Microcontroller is like a CPU unit of a Computer it has RAM,ROM,BUS,I/O Ports,Processor everything embed into a single silicon chip.


Embedded_system

The below picture shows internal blocks of a Motorola microcontroller which is introduced in 1978.they called "computer on chip" ,source link


Embedded_system

Atmel AVR Microcontroller

The board that I made during electronics design is based on an Atmel AVR Chip called Attiny84.Atmel was a company who make AVR series 8 bit single chip using modified Harvard architecture called RISC.Recently another chip making company called Microchip Acquired atmel.So all the supports of AVR is now coming from Microchip


During my electronics engineering diploma course I explored 8051 and AVR architectures well and made many projects and programs using 8051,Atmega328,Atmega8,Attiny85,Attiny45 ..etc.. So this is the first time I'm using Attiny 84


Embedded_system

I found the Atmel Attiny24/44/84 datasheet online from microchip website. This chip created Based on Advanced RISC Architecture.24,44 and 84 are same design only the memory capacity is different 84 has more program memory than 44.


Embedded_system

After the Specifications and features of the particular microcontroller, In the data sheet shows the its pin diagrams of different packages and its pin details. This diagram must be refereed while we design the circuit with Attiny 84. from this diagram we can understand this microcontroller available many packages like SOIC and QFN,MLF.


Embedded_system

But during the designing of the echo board I was following this diagram and ofcourse this wasn't the actual also this is not wrong .This diagram was built by following the arduino support by the open hardware community to use the chip using the arduino and I did programed the chip in arduino during my Machine week


Embedded_system

Here is the whole block diagram of the Attiny 84 which helps to understand how thing are works inside this Attiny Microcontroller


Embedded_system

This image represent the block diagram of RISC-V architecture implemented in the Attiny84


Embedded-programing
From pediaa.com

Programing

Actually I did program the board during the Electronics design week using the arduino and the avrdude tool chains.So this week we actually learn that how its program and what are the codes for program and etc..

Our instructor Mr.Yadu guided us through the AVR tool chain and the basics of the Programing in Embedded C language.His Instructions classes help us to understand more about the tool chain and embedded programing for me it was a revision because all these basics I where learned during my diploma.

With avrdude

avrdude is a tool provided by Atmel to communicating with the Atmel MCUs for flashing,erasing,reading..etc.. . So using the avrdude toolchain and avr gcc compiler we can create programs and compile then program to the Atmel microcontrollers.


Embedded_system

Using draw.io I drew the above diagram for giving an understandable workflow of how the AVR tool chain works. first the given 'code.c' will compile using AVR-GCC and the 'code.hex' file will be created. Then we use the avrdude for writing to the microcontroller using the AVR ISP programer.


Embedded_system

Here is the AVR-GCC compiler and '.hex' file creator makefile which I got it from Mr.Yadu and he explains the process line by line. In this file the first above things mentioned has to be change as per the code file name and MCU we uses and also the crystal selection. There is two process having in the code before creating the hex file for the code.

Yadu shared a documentation which contain a code for button and led in embedded C programing for AVR microcontrollers from electronicsome.com tutorials. It uses a button to initiate blink a LED

AVR C Button Blink CODE :-

                        #include < avr/io.h>
                        #include < util/delay.h>
                        
                        int main(void)
                        {
                          DDRA |= (3 << PC0); //Makes Third pin of PORTA as Output
                          // OR DDRA = 0x01;
                          DDRB &= ~(2 << PD0);//Makes Third pin of PORTB as Input
                          // OR DDRB = 0x00; //Makes all pins of PORTB input
                          while(1) //infinite loop
                          {
                            if(PINB & (1 << PB2) == 1) //If switch is pressed
                            {
                              PORTA |= (1 << PA3); //Turns ON LED
                              _delay_ms(3000); //3 second delay
                              PORTA &= ~(1 << PA3); //Turns OFF LED
                            }
                          }
                        }
                      
Enter fullscreen mode Exit fullscreen mode

I took the example code from website and made changes according to my board . The modified code show in the above snippet.

Atmel Studio


Embedded_system

Embedded_system

So I've opened a new project in AVR Studio by clicking File>>New>>Project


Embedded_system

Then I selected the GCC C Executable project (The image showing the wrong one so it's gonna support both C and C++) and gave the project name as "Blink_LED" then clicked 'OK' for next.


Embedded_system

From the next window I search for Attiny84 and selected .we can also find all the files and links related to the microcontroller we are gonna use.


Embedded_system

Then it opens a main.C (main.cpp in figure) file with a pre template to make our coding easy. I've start to write a blink code by adding the time delay library.You can see the keywords are showing up while I type in the document that's one of the good thing about the AVR Studio.


Embedded_system

After I completed the program writing I go to build>>Compile to see errors if any . I've done this before while I'm at college so I didn't made any error


Embedded_system

Compiling isn't enough in AVR Studio and needs to convert to hex file for program to my echo board so I clicked the build >> Build Solution for creating the Blink_LED.hex.


Embedded_system

In order to program fro the AVR studio we need a official programer suggested by Microchip Atmel so I've to add an external tool for using my FAB tinyISP. I was following Salman's Embedded Week documentation so I started to added a new tool by clicking Tools>>External Tools.


Embedded_system

Then I added a new tool and named FAB-ISP and for the tool avrdude for windows I downloaded from nongnu.org and unzipped to folder then I selected the folder in the external tool adding process as tool 'Command'.


Embedded_system
-c usbtiny -p attiny44 -U flash:w:$(ProjectDir)Debug\$(TargetName).hex:i

I entered the code above as argument and then checked the boxes like in the picture and Clicked the 'Apply' to complete the adding process.the tool Appeared in Tools menu as 'FAB-ISP'.


Embedded_system

Then I connected my board to computer with Fab ISP then clicked tools>>FAB-ISP for flashing the program to the echo board.


Embedded_system

By following that a confirmation check window appeared with the Argument and command like clicked 'OK' to continue.


Then the LEDs on the ISP and the RGB LED (which sharing pins with same ISP pin of Attiny84) on the indicate while the flashing then immediately the blue LED which connected to PA3 is starts blink with 1 second delay.


Embedded_system

The flashing is done successfully! we can see the terminal messages in the Output section of Atmel studio. The High bright Blue LED is awesome.

Programing in Assembly


Embedded_system

I opened a new project in Atmel studio and selected the assembler>>>AVR Assembler Project and gave a new name for the program and then followed everything I did before in Atmel studio to create the project.


Embedded_system

then I made code for blink that works. coding in assembly is something that we do at a low level programing so I already know the logics of the program and then I referred the Attiny84 Data sheet to find right Instructions for assembly programing.


Embedded_system

I used this AVR Delay Loop code generator to make the 1 second delay assembler program .please checkout the website here.


Here is the output of the program showing above video .And also changing the output LED to Red color by replacing 'PA3' to 'PA4'

week 8 Group assignment is to compare the performance and development workflows for other architectures

Other Architectures

For this week of group assignment at fabacademy we all together explore other Architectures and its development workflows.For exploring other architecture , we found out that we should try to use other development hardwares from our lab.So we got some Development hardware board from our Inventory and our instructors help us to get start with it.


STM32 Nucleo

I selected STM32 for the experimental explorations.This works on ARM Architecture.FAB LAB Kochi has a hardware development board called STM32 Nucleo this board is based on a STM32 Series Chip from ST Microcontrollers for learning I just plugged the Nucleo Development board to the PC


Embedded_system

After plugged it opens a directory like a Flash drive .the directory contain an html file I thought may be its a tutorial


Embedded_system

But it was like a login page into some kind of webapp,may be a online compiler .


Embedded_system

I sign up any way and by clicking sign up and filled all the information required then logged in to the web app after confirming the email the app is called Mbed OS


Embedded_system

it opens a webpage which has information about the board and pin diagrams etc.....On the top corner I clicked on te compiler button that's open a new web app in new tab


Embedded_system

Then opened new Program under the 'New' tab and selected a by default blink test program


Embedded_system

Then hit ok The program opened .cpp file its in C++ and I double clicked the .cpp file


Embedded_system

The cpp file is opened which contains the basic LED blinking The LD2 is on board led


Embedded_system

Then I just compiled the program by clicking te compile button on the menu tab


Embedded_system

The it started to compile the program and then downloaded a bin file as the result


Embedded_system

Then I saved the .bin file and copied the .bin file to the flash directory of the Nucleo development board


Embedded_system

then I checked the board ,but it's not blinking anything!! . it's like no response.so I search for tutorials on YouTube and find out one embed os web app tutorial.It seems I did everything same but didn't work anyway I decided to go with other tutorial to try other ways to programing the board .I found another good tutorial by digikey It was awesome and I followed the video and downloaded the programing software they suggested


Embedded_system

After installing the IDE I opened the STM32 cube IDE and created a new stm32 project by following file>>new>>STM32Project


Embedded_system

Then I selected the target as NUCLEO-F303RE board on the board selector tab


Embedded_system

That shows a picture of the board that I'm using and clicked on it and clicked on the next button


Embedded_system

Then i given a project name as 'First_step' and selected the targeted language as 'C' language then clicked finish


Embedded_system

Then the IDE starts to download some resource files from the internet for the NUCLEO-F303RE development board


Embedded_system

After the downloading the Project window opened and also shows the graphical pin diagram of the STM32 Microcontroller of the NUCLEO-F303RE board


Embedded_system

We can Also see the set-up diagram of the stm32 nucleo-f303re development board under the clock configuration tab


Embedded_system

I don't need to change that.but if I making a custom board then I have change this accordingly


Embedded_system

On the tool tab we can calculate current consumption of the Microcontroller,many other features are available too


Embedded_system

I opened the main.c file That I want work with by following First_step>>Core>>src>>main.c on the left


Embedded_system

Scrolled down to find while loop to add my code in loop we can write what I want do with the Microcontroller


Embedded_system

we can declare each IO pins as whatever we want with it on the graphical pin diagram by right clicking on the pins


Embedded_system

while scrolling down we can also see how pins are declared in the program.the n I started to write the code for blinking the on board led by following the digikey tutorial.


Embedded_system

for finding the syntax I found the doc for stm32L4 by typing 'stm32l4 hal' on google will get this PDF doc I clicked on the GPIO gernric drivers and searches for the led toggle command,there is lot of commands are there for other applications



Embedded_system

I found the Toggle pin command and entered the syntax in the code also a delay command.For toggle command it required two parameters 1)which PORT we are using then 2) which PIN we are using


Embedded_system

On the graphical PIN diagram we can see the LD2 LED connected to the GPIO_PIN_5 at POARTA (PA5)


Embedded_system

Then I build the program following Project>>Build but got an error and rebuilds again after fixing the error


Embedded_system

The while loop of the code that I created is given below

while (1)   //loop
      {
     HAL_GPIO_TogglePin (GPIOA,GPIO_PIN_5); //togling the LED LD2 Connected to the PIN 5
     HAL_Delay(1000);   // 1 second delay (1000ms = 1 sec)

     /*USER CODE END WHILE*/

     /*USER CODE BEGIN 3*/
      }
    

Embedded_system

After the successful building ,to upload the program I clicked run>>Debug As>>STM32 Cortex-M C/C++ Application


Embedded_system

I leaves everything default on the De-Bug menu and clicked OK


Embedded_system

It STM32 Cube IDE automatically dedicated the NUCLEO-F303RE development board and says it's firmware need to be update


Embedded_system

I clicked 'OK' then it started to update and after updating I decided to do the drag and drop programing that I tried before



It started to blink,So the firmware was the problem that's it didn't works earlier with embed Os programing method after the firmware updating program working fine.then I continue in the Cube IDE I run the program again


Embedded_system

After downloading the program to the Board it asked to switch the debugging mode or not .I clicked OK to see the debugging process



In debug-mode we can see the RGB LED on the Debugging section blinking to indicate communication in between the Microcontroller and the cube-IDE


Embedded_system

After Clicking the resume button the program will starts to run.We can pause the program and Can be execute step by step it was awesome

I did some adding and changes in the program for fast blinking in the cube ide.as per my experience the coding in STM32 Cube IDE is more simpler than the AVR Embedded C. I like it so Much

while (1)   //loop
                {
               HAL_GPIO_TogglePin (GPIOA,GPIO_PIN_5); //togling the LED LD2 Connected to the PIN 5
               HAL_Delay(1000);   // 100 milli second delay
               HAL_GPIO_TogglePin (GPIOA,GPIO_PIN_5);
               HAL_Delay(100);
               HAL_GPIO_TogglePin (GPIOA,GPIO_PIN_5);
               HAL_Delay(100);
               /*USER CODE END WHILE*/
          
               /*USER CODE BEGIN 3*/
                }
              


ESP8266

Esp8266 is a WIFI chip built for Iot applications by the chinese manufacturer Espressif Systems. And then they made it's own community by making it open source and cheap so people can buy and use them. So in this chip used a Tensilica L106 32-bit RISC processor based on Harvard architecture.


Embedded-programing
From electronicscuriosities.com

Here showing an open source development board that used the ESP8266 12E Module which is based ot rhe ESP8266 chip with a 4MB Flash ROM.The spec are given below

For more about the ESP32 Node-mcu exploration check Abhinav's Embedded week page.He did a lot of exploring on ESP8266 and also did use the board by trying some experiments.

ESP32

ESP 32 is a Powerful chip also from Espressif Systems and It has more clock speed and pins and other awesome feature than the other wifi chips.so It's suitable for various applications ranging from low-power sensor networks to the most demanding tasks, such as voice encoding, music streaming and MP3 decoding


Embedded-programing
From docs.espressif.com

For more about the ESP32 Node-mcu exploration check Hanani's Embedded week page

Raspberry PI

Here Raspberry pi is not a Microcontroller. Instead, its a single board credit card sized mini computer that can do amazing things. We can attach keyboard ,mouse, Displays and etc.. just like our ordinary computers. And It's a good thing that the Raspberry Pi is open source and support linux based free Operating systems.

It runs on Broadcom ARM cortex-A72 processor from ARM architecture family.Apart from the processor it has RAM Memory card slots I/O ports(GPIO) that we can use just like a microcontroller but it's more powerful than that .


Embedded-programing
From microcontrollerslab.com

The above diagram shows the picture of a Raspberry PI 4 and its port details and The Specifications are shown below

For more about the Raspberry pi exploration check Abel Tomy's Embedded week page

Lattice Ice-stick

Unlike other boards and microcontroller the Lattice Ice-stick works in different way , this development board comes with a Lattice ICE40HX1K FPGA chip.which means Field Programable array Chip. This means we can chose the inputs and outputs and other communication protocol for the chip our own by developing a HDL (Hardware Description Language) program to the chip as per our need.


Embedded-programing
From latticesemi.com

Pallab Shrestha was exploring the development board and he doesn't know anything about it actually because he is from a mechanical background.he failed to program the board but he explore well and Because of the time limitation we decided to move aside to explore in another time.

features

For more about the ESP32 Node-mcu exploration check Pallab Shrestha's Embedded week page

Updates: Latter I've done my wild card week on the FPGA using the Lattice-Ice-stick Evaluation board check out my Week 15 to see more

References


Downloads

Back to Top Back to top