Embedded Programmig



Fot this week, we had to deal only with programming rather than hardware production. Using the Echo Hello board we created in the Electronics Design week and programming it using the ISPattiny we made on the Elictronics Production week. On a Linux based system.

As mentioned, I had all the objects I need for this week, except one tool, which is the Linux system. I have downloaded Ubuntu, which is a free product you can easily download.

I have installed it on a USB driver and ran it on my computer by selecting the system from the BIOS whcih could be accessed by clicking Alt+Ctrl+Delete bfore entering the Windows.

Note: the method of accessing the BIOS might differ depending on the computer manufacturer. Mine is Dell.

Just to make sure that my hardwares are working properly, I attached the echo board to my FabISP then to my computer and pressed the reset button to see if there the power reaches to it. The fisrt trial did not work, so I used my firends FabISP and the LED on the reset button indicated the the system works. So I figured that I need anew FabISP.



Programming

The first thing to do is prepare the makefile and C code. For the make file, we could download the one available on Fab Academy's archives then edit on ot.

The first line must contain the name of the C code file. And the second one its type. The third line goes for the chip and fourth one the frequency of the clock used.

Following this turorial to understand the basic concepts behind makefiles, we can learn that some of the lines ought to be excluded.

The Symbol 0x5E is a hexidecimal number refers to the state of the fuses we need to program. To calculate the right number we need to study the datasheet of the Attiny44 and relate it to our system.

Refer to this page to understand the idea of the fuses. An Attiny44 has three types of fuses: Low, High, and Extended. But we are only going to program the Low Fuses, which are four and has a value summation of 8 bits.

For the CKSEL fuse, we're using an external clock, so it will be set to 0000 according to page 25 on the datasheet.

For the SUT, to avoid any misbehaviour as advised in page 26, I programmed it with the highest delay option (10).

Last but not least, refering to page 30. The CKDIV8 fuse is best remain on defult, programmed (0). And CKOUT is only required when using the chip's internal clock. So in my case I will leave it unprogrammed (1).

On the fuse calculator (available on the link i mentioned for the attiny fuses) I checked the programmed low fuses to find out the hexidecimal number, which came to be 06.

Finally my makefile looked like this:




C Language

A C code might vary from 3 lines to millions depending on the application. And it could be written as any text file and saved with the extension (.c). To compile it, we can use Arduino IDE.

A C code consists mainly of these parts:

  • Preprocessor Commands : Including libraries and defining variables.
  • Statements: preserved words combined to create a function to carry out a command.
  • Variables: entity to reserve vlaue.
  • To write the code after including the needed libraries and making the nessisary definitions, we do the following:

    This function is the beggining of the processing code. The finctions and statements of the code go onside it.

    Functions and statements here are similar the ones we wrote in programming on Arduino. The major difference in this week's programming is the numeric system. We will be using the hexadecimal numbers.





    Attiny44 has 8 input/output pins in the port A, which we'll be using...


    I want to set pin 2 as an output and pin 7 as an input. (the other pins are not important so i will set them all as inputs.) As a binary system, 1 is output and 0 is input.

    We take every four pins as a segment;


    To change this configuration (0000 0100) from binary to hexadecimal we get 04.


    Now for my C code, it was simply to light up the LED when the button is pushed. And to be turned off again when the button is released.

    ---

    Now as all the files are prepared. We will open the terminal on linux. And there, I have to install the AVR files for programming. This can be done by using the folloing comands:

    - sudo apt-get install flex byacc bison gcc libusb-dev avrdude

    - sudo apt-get install gcc-avr

    - sudo apt-get install avr-libc

    - sudo apt-get install libc6-dev


    Then placed the makefile and C code in a folder, and navigated to it on the terminal using the ls and cd commands (just like in Git Bash).

    After that, I used the command make to create .hex and .out files. Those I will find now in the same make folder.

    The last command is writing make and the name of the program like shown in the this screenshot...

    And by this, the board comes to life.




    My Terminal Log:


        
    ubuntu@ubuntu:~$ sudo apt-get install flex byacc bison gcc libusb-dev avrdude Reading package lists... Done Building dependency tree Reading state information... Done gcc is already the newest version (4:5.3.1-1ubuntu1). The following additional packages will be installed: libbison-dev libfl-dev libftdi1 libsigsegv2 m4 Suggested packages: avrdude-doc bison-doc The following NEW packages will be installed: avrdude bison byacc flex libbison-dev libfl-dev libftdi1 libsigsegv2 libusb-dev m4 0 upgraded, 10 newly installed, 0 to remove and 426 not upgraded. Need to get 1,501 kB of archives. After this operation, 4,607 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libsigsegv2 amd64 2.10-4 [14.1 kB] Get:2 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 m4 amd64 1.4.17-5 [195 kB] Get:3 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libfl-dev amd64 2.6.0-11 [12.5 kB] Get:4 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 flex amd64 2.6.0-11 [290 kB] Get:5 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libbison-dev amd64 2:3.0.4.dfsg-1 [338 kB] Get:6 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 bison amd64 2:3.0.4.dfsg-1 [259 kB] Get:7 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 libftdi1 amd64 0.20-4build1 [15.2 kB] Get:8 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libusb-dev amd64 2:0.1.12-28 [29.9 kB] Get:9 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 avrdude amd64 6.2-5 [291 kB] Get:10 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 byacc amd64 20140715-1 [56.9 kB] Fetched 1,501 kB in 6s (221 kB/s) Selecting previously unselected package libsigsegv2:amd64. (Reading database ... 194399 files and directories currently installed.) Preparing to unpack .../libsigsegv2_2.10-4_amd64.deb ... Unpacking libsigsegv2:amd64 (2.10-4) ... Selecting previously unselected package m4. Preparing to unpack .../archives/m4_1.4.17-5_amd64.deb ... Unpacking m4 (1.4.17-5) ... Selecting previously unselected package libfl-dev:amd64. Preparing to unpack .../libfl-dev_2.6.0-11_amd64.deb ... Unpacking libfl-dev:amd64 (2.6.0-11) ... Selecting previously unselected package flex. Preparing to unpack .../flex_2.6.0-11_amd64.deb ... Unpacking flex (2.6.0-11) ... Selecting previously unselected package libbison-dev:amd64. Preparing to unpack .../libbison-dev_2%3a3.0.4.dfsg-1_amd64.deb ... Unpacking libbison-dev:amd64 (2:3.0.4.dfsg-1) ... Selecting previously unselected package bison. Preparing to unpack .../bison_2%3a3.0.4.dfsg-1_amd64.deb ... Unpacking bison (2:3.0.4.dfsg-1) ... Selecting previously unselected package libftdi1:amd64. Preparing to unpack .../libftdi1_0.20-4build1_amd64.deb ... Unpacking libftdi1:amd64 (0.20-4build1) ... Selecting previously unselected package libusb-dev. Preparing to unpack .../libusb-dev_2%3a0.1.12-28_amd64.deb ... Unpacking libusb-dev (2:0.1.12-28) ... Selecting previously unselected package avrdude. Preparing to unpack .../avrdude_6.2-5_amd64.deb ... Unpacking avrdude (6.2-5) ... Selecting previously unselected package byacc. Preparing to unpack .../byacc_20140715-1_amd64.deb ... Unpacking byacc (20140715-1) ... Processing triggers for install-info (6.1.0.dfsg.1-5) ... Processing triggers for man-db (2.7.5-1) ... Processing triggers for libc-bin (2.23-0ubuntu9) ... Processing triggers for doc-base (0.10.7) ... Processing 33 changed doc-base files, 2 added doc-base files... Setting up libsigsegv2:amd64 (2.10-4) ... Setting up m4 (1.4.17-5) ... Setting up libfl-dev:amd64 (2.6.0-11) ... Setting up flex (2.6.0-11) ... Setting up libbison-dev:amd64 (2:3.0.4.dfsg-1) ... Setting up bison (2:3.0.4.dfsg-1) ... update-alternatives: using /usr/bin/bison.yacc to provide /usr/bin/yacc (yacc) in auto mode Setting up libftdi1:amd64 (0.20-4build1) ... Setting up libusb-dev (2:0.1.12-28) ... Setting up avrdude (6.2-5) ... Setting up byacc (20140715-1) ... Processing triggers for libc-bin (2.23-0ubuntu9) ... ubuntu@ubuntu:~$ sudo apt-get install gcc-avr Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: binutils-avr Suggested packages: task-c-devel gcc-doc avr-libc The following NEW packages will be installed: binutils-avr gcc-avr 0 upgraded, 2 newly installed, 0 to remove and 426 not upgraded. Need to get 14.1 MB of archives. After this operation, 74.1 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 binutils-avr amd64 2.25+Atmel3.5.0-2 [1,425 kB] Get:2 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 gcc-avr amd64 1:4.9.2+Atmel3.5.0-1 [12.7 MB] Fetched 14.1 MB in 12s (1,141 kB/s) Selecting previously unselected package binutils-avr. (Reading database ... 194706 files and directories currently installed.) Preparing to unpack .../binutils-avr_2.25+Atmel3.5.0-2_amd64.deb ... Unpacking binutils-avr (2.25+Atmel3.5.0-2) ... Selecting previously unselected package gcc-avr. Preparing to unpack .../gcc-avr_1%3a4.9.2+Atmel3.5.0-1_amd64.deb ... Unpacking gcc-avr (1:4.9.2+Atmel3.5.0-1) ... Processing triggers for man-db (2.7.5-1) ... Setting up binutils-avr (2.25+Atmel3.5.0-2) ... Setting up gcc-avr (1:4.9.2+Atmel3.5.0-1) ... ubuntu@ubuntu:~$ sudo apt-get install avr-libc Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: avr-libc 0 upgraded, 1 newly installed, 0 to remove and 426 not upgraded. Need to get 4,378 kB of archives. After this operation, 37.3 MB of additional disk space will be used. Get:1 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 avr-libc all 1:1.8.0+Atmel3.5.0-1 [4,378 kB] Fetched 4,378 kB in 7s (563 kB/s) Selecting previously unselected package avr-libc. (Reading database ... 195450 files and directories currently installed.) Preparing to unpack .../avr-libc_1%3a1.8.0+Atmel3.5.0-1_all.deb ... Unpacking avr-libc (1:1.8.0+Atmel3.5.0-1) ... Processing triggers for man-db (2.7.5-1) ... Setting up avr-libc (1:1.8.0+Atmel3.5.0-1) ... ubuntu@ubuntu:~$ sudo apt-get install libc6-dev Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libc-dev-bin libc6 libc6-dbg Suggested packages: glibc-doc The following packages will be upgraded: libc-dev-bin libc6 libc6-dbg libc6-dev 4 upgraded, 0 newly installed, 0 to remove and 422 not upgraded. Need to get 8,411 kB of archives. After this operation, 2,048 B of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://security.ubuntu.com/ubuntu xenial-security/main amd64 libc6-dev amd64 2.23-0ubuntu10 [2,079 kB] Get:2 http://security.ubuntu.com/ubuntu xenial-security/main amd64 libc-dev-bin amd64 2.23-0ubuntu10 [68.7 kB] Get:3 http://security.ubuntu.com/ubuntu xenial-security/main amd64 libc6-dbg amd64 2.23-0ubuntu10 [3,683 kB] Get:4 http://security.ubuntu.com/ubuntu xenial-security/main amd64 libc6 amd64 2.23-0ubuntu10 [2,580 kB] Fetched 8,411 kB in 9s (852 kB/s) Preconfiguring packages ... (Reading database ... 197925 files and directories currently installed.) Preparing to unpack .../libc6-dev_2.23-0ubuntu10_amd64.deb ... Unpacking libc6-dev:amd64 (2.23-0ubuntu10) over (2.23-0ubuntu9) ... Preparing to unpack .../libc-dev-bin_2.23-0ubuntu10_amd64.deb ... Unpacking libc-dev-bin (2.23-0ubuntu10) over (2.23-0ubuntu9) ... Preparing to unpack .../libc6-dbg_2.23-0ubuntu10_amd64.deb ... Unpacking libc6-dbg:amd64 (2.23-0ubuntu10) over (2.23-0ubuntu9) ... Preparing to unpack .../libc6_2.23-0ubuntu10_amd64.deb ... Unpacking libc6:amd64 (2.23-0ubuntu10) over (2.23-0ubuntu9) ... Setting up libc6:amd64 (2.23-0ubuntu10) ... Setting up libc-dev-bin (2.23-0ubuntu10) ... Setting up libc6-dev:amd64 (2.23-0ubuntu10) ... Setting up libc6-dbg:amd64 (2.23-0ubuntu10) ... Processing triggers for libc-bin (2.23-0ubuntu9) ... Processing triggers for man-db (2.7.5-1) ... ubuntu@ubuntu:~$ lsusb Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 006: ID 138a:0011 Validity Sensors, Inc. VFS5011 Fingerprint Reader Bus 001 Device 005: ID 04f2:b5c0 Chicony Electronics Co., Ltd Bus 001 Device 004: ID 0cf3:e500 Atheros Communications, Inc. Bus 001 Device 008: ID 045e:0797 Microsoft Corp. Optical Mouse 200 Bus 001 Device 007: ID 1781:0c9f Multiple Vendors USBtiny Bus 001 Device 002: ID 058f:6387 Alcor Micro Corp. Flash Drive Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub ubuntu@ubuntu:~$ lsusb Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 006: ID 138a:0011 Validity Sensors, Inc. VFS5011 Fingerprint Reader Bus 001 Device 005: ID 04f2:b5c0 Chicony Electronics Co., Ltd Bus 001 Device 004: ID 0cf3:e500 Atheros Communications, Inc. Bus 001 Device 008: ID 045e:0797 Microsoft Corp. Optical Mouse 200 Bus 001 Device 009: ID 1781:0c9f Multiple Vendors USBtiny Bus 001 Device 002: ID 058f:6387 Alcor Micro Corp. Flash Drive Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub ubuntu@ubuntu:~$ ls Desktop Documents Downloads Music Pictures Public Templates Videos ubuntu@ubuntu:~$ cd Desktop/make ubuntu@ubuntu:~/Desktop/make ls makefile Hello.Worldc ubuntu@ubuntu:~/Desktop/make$ lsusb Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 006: ID 138a:0011 Validity Sensors, Inc. VFS5011 Fingerprint Reader Bus 001 Device 005: ID 04f2:b5c0 Chicony Electronics Co., Ltd Bus 001 Device 004: ID 0cf3:e500 Atheros Communications, Inc. Bus 001 Device 008: ID 045e:0797 Microsoft Corp. Optical Mouse 200 Bus 001 Device 040: ID 1781:0c9f Multiple Vendors USBtiny Bus 001 Device 002: ID 058f:6387 Alcor Micro Corp. Flash Drive Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub ubuntu@ubuntu:~/Desktop/make$ ls makefile Hello.World.c ubuntu@ubuntu:~/Desktop/make$ make avr-gcc -mmcu=attiny44 -Wall -Os -DF_CPU=20000000 -I./ -Hello.world.out Hello.World.c avr-objcopy -O ihex Hello.world.out Hello.World.c.hex;\ avr-size --mcu=attiny44 --format=avr Hello.world.out AVR Memory Usage ---------------- Device: attiny44 Program: 64 bytes (1.6% Full) (.text + .data + .bootloader) Data: 0 bytes (0.0% Full) (.data + .bss + .noinit) ubuntu@ubuntu:~/Desktop/make$ make program-fabISP avr-objcopy -O ihex Hello.world.out Hello.World.c.hex;\ avr-size --mcu=attiny44 --format=avr Hello.world.out AVR Memory Usage ---------------- Device: attiny44 Program: 64 bytes (1.6% Full) (.text + .data + .bootloader) Data: 0 bytes (0.0% Full) (.data + .bss + .noinit) avrdude -p t44 -P usb -c usbtiny -U flash:w:Hello.World.c.hex avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.00s avrdude: Device signature = 0x1e9207 (probably t44) avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed To disable this feature, specify the -D option. avrdude: erasing chip avrdude: reading input file "Hello.World.c.hex" avrdude: input file Hello.World.c.hex auto detected as Intel Hex avrdude: writing flash (64 bytes): Writing | ################################################## | 100% 0.06s avrdude: 64 bytes of flash written avrdude: verifying flash memory against Hello.World.c.hex: avrdude: load data flash data from input file Hello.World.c.hex: avrdude: input file Hello.World.c.hex auto detected as Intel Hex avrdude: input file Hello.World.c.hex contains 64 bytes avrdude: reading on-chip flash data: Reading | ################################################## | 100% 0.07s avrdude: verifying ... avrdude: 64 bytes of flash verified avrdude: safemode: Fuses OK (E:FF, H:DF, L:FE) avrdude done. Thank you. ubuntu@ubuntu:~/Desktop/make$



    ---


    Files:

    - Makefile

    - C code





    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.