Sander van Vliet

Fabacademy2016
@Fablab Amsterdam


Embedded Programming

Week 8 (16 - 23 march)


Lecture

I found the lecture really interesting. I learnt many things about microprocessors and programming that I have been wanting to know for a long time.

Assignment


Read a microcontroller data sheet

I chose the datasheet of the ATtiny24A/44A/84A microcontroller which I used on the hello world board that I recreated in week 6. As we will be programming this board and other boards with similar microcontrollers during the course, this is quite useful. The datasheet gives us information about: what kind of features this microcontroller has, what the pinout/pin configuration looks like, which different memories it has among many other things like:

Quite some parts make general sense but a lot of the datasheet's detailed information really doesn't. I found it interesting though and went in 'discovery mode' to get understanding of the above and more.


Program your board to do 'something', with as many different programming languages and programming environments as possible

For this assignment I wanted to program my hello world board:
  • using Arduino IDE
  • from the Linux command line with a program written in C
  • from the Windows7 command line with a program written in Assembly source code
  • and if I have time I also would like to try out another development environment like
  • Atmel studio.

  • Preparing my laptop and the hello world board
    To program my board I will use the ISP (In System Programmer: a hardware device to program microcontroller devices) I made in week 4 which I already had programmed from my laptop's Linux Ubuntu partition so my Linux os is already set up to program boards. I also want to be able to use the fabtinyISP to program boards from my laptop's Windows7 partition and for this I needed to install the following software first:
  • AVR development platform
  • To be able to make use of the AVR toolchain: avr-gcc and avr-libc to compile C code and avrdude to transfer/upload the .hex file/program to the board). I downloaded the AVR setup from this ladyada.net page.
  • usbtinyISP drivers
  • Then I installed the drivers for my fabtinyISP so my Windows7 operating system could communicate with it. I downloaded them from learn.adafruit.com.
    After this I checked it, but I made a little mistake, as you can see in the screenshot below.
    The first try I did not switch the external powering switch on, on the fabtinyISP, so the initialization failed. Okay, so I switched that on.
    At my second try, avrdude did not read the expected signature. This was because I had typed in -p t45, but the hello world board has the ATtiny44a on it, so the third time having changed it into -p t44 it responded like it should when all is fine and avrdude was now able to program the hello world board with my fabtinyISP.
  • FTDI Virtual COM Port drivers
  • These I need so I can use a FTDI cable. This is a USB to Serial converter cable for serial communication between the (in this case) Hello world board and my laptop. Downloaded from ftdichip.com.
    VCP drivers cause the USB device to appear as an additional COM port available to the PC. Application software can access the USB device in the same way as it would access a standard COM port. (which most computers do not have anymore nowadays.)
    Read more about TTL serial communication here.
  • installing an assembler
  • I installed Gavrasm. Gerds AVR ASseMbler is a program for the commandline. It takes a text file with assembly source code, and encodes it to hexadecimal code, which is the microcontrollers 'mother thongue'. The program is available for Linux, DOS and windows. source.
  • Python
  • This can come in handy for reading serial output from the board to my laptop. Downloaded from Python's official website.
  • adding the ATtiny44a to Arduino IDE
  • I had installed the Arduino IDE environment a while ago already and played around with an Arduino board, but now I needed to configure it to be able to communicate with the ATtiny44a as well. (This step is also required for the next step; putting on the bootloader) It goes via the Tools - Board Manager menu, I followed this highlowtech.org tutorial.
  • bootloader
  • To make the hello world board programmable, a bootloader needs to be burned on the board's ATtiny44 microcontroller. This only needs to be done once for each board after it has been produced. You can do this in the Arduino IDE environment, in the Tools menu the red circled items needed to be set correctly first, see below.

    Program my hello world board using Arduino IDE


    The first thing I did (on the train home) was opening the Blink sketch from the IDE basic examples. I looked on my schematic of the hello echo board to see which pin the led was connected to (pin6, PA7). Arduino defined its own pin numbering for microcontrollers which is different from Atmel's numbering, see below.
    The led is connected to Arduino pin nr 7; I changed the code accordingly so it became the following:
    hello world board blink sketch source file - Arduino IDE

    Then I uploaded it to the microcontroller and the led started blinking, so my board worked! Now I know I can go on and create code for it to learn more about programming and the usage of input and output etc.





    Issue compiling for AVR boards after updating in Boards Manager!

    After I updated the 'Standard Arduino AVR Boards' by clicking the update button in board manager, v1.6.5 (automatically to 1.6.10) I could no longer compile for my ATtiny44a/hello world board! I received the following error message while compiling:
    "avr-gcc: error: C:\Users\Snader\AppData\Local\Temp\build859542859958490258.tmp/core.a: No such file or directory. Error compiling."
    I found this issue and its resolution in the Arduino Forum:
    To fix this you have to downgrade the version of the Standard Arduino AVR Boards in Board Manager to 1.6.8:
    Tools > Board > Boards Manager...
    Wait for downloads to finish.
    Click on Arduino AVR Boards
    Select Version 1.6.8
    Click Install



    Program my hello world board with a program in C from the Linux command line

  • getting a program written in C to test with
  • To try the process in Linux I downloaded the hello echo program from this weeks lecture page
  • example/tutorial
  • I followed the workflow described on this page and succeeded to create the "hello.ftdi.44.echo.c.hex" file and the "hello.ftdi.44.echo.c.out" file with the makefile utilizing the make command.
  • resetting the microcontroller
  • Prepare the microcontroller for receiving the program with the command make program-usbtiny-fuses
  • programming the board
  • The command make program-usbtiny was the last step with which the board actually gets programmed
  • TEST
  • The function of this little program is to echo keys that you type on the keyboard right back at you. To be able to test this I needed to have communication between the hello echo board and my laptop (via a FTDI cable). I used Python and a script called pyserial which I downloaded and installed with the command python setup.py install, executed from the pyserial installation files directory.
    I started the script with the command python term.py /dev/ttyUSB0 115200

    Below you can see the commands and responses:
    snader@Duck:~/Documents/fabacademy/08 Embedded Programming/hello.ftdi.44.echo$ls -al total 48 drwxrwxr-x 2 snader snader 4096 mrt 21 14:07 . drwxrwxr-x 4 snader snader 4096 mrt 19 00:01 .. -rw-rw-r-- 1 snader snader 4867 mrt 18 21:57 hello.ftdi.44.echo.c -rw-rw-r-- 1 snader snader 997 mrt 18 21:57 hello.ftdi.44.echo.c.make -rw-rw-r-- 1 snader snader 2314 mrt 19 00:29 term.py


    snader@Duck:~/Documents/fabacademy/08 Embedded Programming/hello.ftdi.44.echo$make -f hello.ftdi.44.echo.c.make avr-gcc -mmcu=attiny44 -Wall -Os -DF_CPU=20000000 -I./ -o hello.ftdi.44.echo.out hello.ftdi.44.echo.c avr-objcopy -O ihex hello.ftdi.44.echo.out hello.ftdi.44.echo.c.hex;\ avr-size --mcu=attiny44 --format=avr hello.ftdi.44.echo.out AVR Memory Usage ---------------- Device: attiny44 Program: 758 bytes (18.5% Full) (.text + .data + .bootloader) Data: 64 bytes (25.0% Full) (.data + .bss + .noinit)


    snader@Duck:~/Documents/fabacademy/08 Embedded Programming/hello.ftdi.44.echo$ls -al total 76 drwxrwxr-x 2 snader snader 4096 mrt 21 14:23 . drwxrwxr-x 4 snader snader 4096 mrt 19 00:01 .. -rw-rw-r-- 1 snader snader 4867 mrt 18 21:57 hello.ftdi.44.echo.c -rw-rw-r-- 1 snader snader 2166 mrt 21 14:23 hello.ftdi.44.echo.c.hex -rw-rw-r-- 1 snader snader 997 mrt 18 21:57 hello.ftdi.44.echo.c.make -rwxrwxr-x 1 snader snader 5045 mrt 21 14:23 hello.ftdi.44.echo.out -rw-rw-r-- 1 snader snader 2314 mrt 19 00:29 term.py


    snader@Duck:~/Documents/fabacademy/08 Embedded Programming/hello.ftdi.44.echo$sudo make -f hello.ftdi.44.echo.c.make program-usbtiny-fuses avr-objcopy -O ihex hello.ftdi.44.echo.out hello.ftdi.44.echo.c.hex;\ avr-size --mcu=attiny44 --format=avr hello.ftdi.44.echo.out AVR Memory Usage ---------------- Device: attiny44 Program: 758 bytes (18.5% Full) (.text + .data + .bootloader) Data: 64 bytes (25.0% Full) (.data + .bss + .noinit) avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x5E:m avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.01s avrdude: Device signature = 0x1e9207 avrdude: reading input file "0x5E" avrdude: writing lfuse (1 bytes): Writing | ################################################## | 100% 0.00s avrdude: 1 bytes of lfuse written avrdude: verifying lfuse memory against 0x5E: avrdude: load data lfuse data from input file 0x5E: avrdude: input file 0x5E contains 1 bytes avrdude: reading on-chip lfuse data: Reading | ################################################## | 100% 0.00s avrdude: verifying ... avrdude: 1 bytes of lfuse verified avrdude: safemode: Fuses OK (H:FF, E:DF, L:5E) avrdude done. Thank you.


    snader@Duck:~/Documents/fabacademy/08 Embedded Programming/hello.ftdi.44.echo$sudo make -f hello.ftdi.44.echo.c.make program-usbtiny avr-objcopy -O ihex hello.ftdi.44.echo.out hello.ftdi.44.echo.c.hex;\ avr-size --mcu=attiny44 --format=avr hello.ftdi.44.echo.out AVR Memory Usage ---------------- Device: attiny44 Program: 758 bytes (18.5% Full) (.text + .data + .bootloader) Data: 64 bytes (25.0% Full) (.data + .bss + .noinit) avrdude -p t44 -P usb -c usbtiny -U flash:w:hello.ftdi.44.echo.c.hex avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.01s avrdude: Device signature = 0x1e9207 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.ftdi.44.echo.c.hex" avrdude: input file hello.ftdi.44.echo.c.hex auto detected as Intel Hex avrdude: writing flash (758 bytes): Writing | ################################################## | 100% 0.51s avrdude: 758 bytes of flash written avrdude: verifying flash memory against hello.ftdi.44.echo.c.hex: avrdude: load data flash data from input file hello.ftdi.44.echo.c.hex: avrdude: input file hello.ftdi.44.echo.c.hex auto detected as Intel Hex avrdude: input file hello.ftdi.44.echo.c.hex contains 758 bytes avrdude: reading on-chip flash data: Reading | ################################################## | 100% 0.80s avrdude: verifying ... avrdude: 758 bytes of flash verified avrdude: safemode: Fuses OK (H:FF, E:DF, L:5E) avrdude done. Thank you.


    snader@Duck:~/Documents/fabacademy/08 Embedded Programming/hello.ftdi.44.echo$python term.py /dev/tty tty tty2 tty31 tty43 tty55 ttyprintk ttyS2 ttyS31 tty0 tty20 tty32 tty44 tty56 ttyS0 ttyS20 ttyS4 tty1 tty21 tty33 tty45 tty57 ttyS1 ttyS21 ttyS5 tty10 tty22 tty34 tty46 tty58 ttyS10 ttyS22 ttyS6 tty11 tty23 tty35 tty47 tty59 ttyS11 ttyS23 ttyS7 tty12 tty24 tty36 tty48 tty6 ttyS12 ttyS24 ttyS8 tty13 tty25 tty37 tty49 tty60 ttyS13 ttyS25 ttyS9 tty14 tty26 tty38 tty5 tty61 ttyS14 ttyS26 ttyUSB0 tty15 tty27 tty39 tty50 tty62 ttyS15 ttyS27 tty16 tty28 tty4 tty51 tty63 ttyS16 ttyS28 tty17 tty29 tty40 tty52 tty7 ttyS17 ttyS29 tty18 tty3 tty41 tty53 tty8 ttyS18 ttyS3 tty19 tty30 tty42 tty54 tty9 ttyS19 ttyS30

    After typing "python term.py /dev/tty" I had to press TAB a few times for Ubuntu to show me the above info, including "ttyUSB0" which is the serial device which outputs to the USB port. Below you see the full command including "115200" which is the data speed of the communication in baud (bits per second).
    snader@Duck:~/Documents/fabacademy/08 Embedded Programming/hello.ftdi.44.echo$sudo python term.py /dev/ttyUSB0 115200 Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__ return self.func(*args) File "term.py", line 28, in key if (ord(key) == 13): TypeError: ord() expected a character, but string of length 0 found

    Although it gave me the error shown above, the Python script still did what it should, see the screenshot of the hello world board in effect below.

    Program my hello world board with a program written in Assembly source code from the Windows7 command line

  • getting a program in Assembler to start with
  • To try this process I found a program which was used by a 2015 Fabacademy student.
    Here is the Assembler code:
    .device attiny44
    .org 0
    cbi DDRA, 3
    sbi DDRB, 2
    loop:
    sbis PINA,3
    sbi PORTB, 2
    sbic PINA,3
    cbi PORTB, 2
    rjmp loop

    I looked on my schematic of the hello echo board to see which pin the led was connected to (pin6, PA7) and I changed the code accordingly: from sbi PORTB, 2 to sbi PORTA, 7.
    The button is connected to pin10, PA3 so that didn't need a furher change in the code.
    .device attiny44
    .org 0
    cbi DDRA, 3
    sbi DDRB, 2
    loop:
    sbis PINA,3
    sbi PORTA, 7
    sbic PINA,3
    cbi PORTA, 7
    rjmp loop

    Then I needed to save the assembly code in a text file with the extension .asm and then next step then was generating a .hex file from this .asm file using Gavrasm with the following command: gavrasm [filename].asm
    Below you can see a movie of the uploading of the program to the hello echo board using avrdude and the functioning of the program on my hello echo board. The commands are the same as in Linux:
    avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x5E:m
    avrdude -p t44 -P usb -c usbtiny -U flash:w:blinkassembler.hex