Week 9:

Embedded Programming

Unit description

Task: Embedded Programming

  • Read a micro-controller data sheet. (#data-sheet)
  • Program your board to do something, with as many different programming languages and programming environments as possible. (#GreatCowBasic)
  • Optionally, experiment with other architectures (#Atmel-Ice)

Learning Outcomes

  • Identify relevant information in a micro-controller data sheet.
  • Implement programming protocols.
  • Checklist:

  • Documented what you learned from reading a micro-controller data-sheet. (#datasheet)
  • What questions do you have? What would you like to learn more about? (#more)
  • Programmed your board (#programming)
  • Described the programming process/es you used (#processes)
  • Included your code (#files) (#files-Atmel-Ice) (#files-GCBasic)
  • This week's assignment is to read a micro-controller datasheet, program my board to do something.

    I decided to use both the hello-echo board developed in week6 for embedded programming. This board was based on the ATTiny44 chip. I decided to abandon the ISP I fabbed earlier during Fabacademy because it damaged my laptop. I purchased a Sparkfun Pocket AVR Programmer to program the hello-world board from earlier. The Pocket AVR programmer can be purchased from DigiKey or directly from Sparkfun's website for $14.95. It looks like this:

    It has a slide switch on it, so you can use it to power, or not power, the target board. It also comes with a nice header cable. You plug this programmer into your laptop or desktop with a micro-USB. It is also buffered, so if anything goes awry, the pocket programmer will take the blow and not your laptop, supposedly.

    (id='datasheet')

    Datasheet for the ATTiny24/44/84 Family

    The ATTiny datasheet can be found from the manufacturer's website here: http://www.atmel.com/images/doc8006.pdf. The datasheet is very elaborate and is more like a reference manual. The beginning of the datasheet describes the features of this family of micro-controllers. I have highlighted what I feel is significant for my upcoming project. Items from the data sheet are in italic. My notes are in regular case.

    Features

    Pin Configuration

    The following diagram from the datasheet shows pin configuration for the PDIP/SOIC packages.

    PDIP designates dual in-line package. This means the chip will have pins that you can push into a breadboard. It looks something like this:

    SOIC designates a surface mount package. This means that the chip will have solder pads that you can solder to a PC board you mill. It looks something like this:

    If you have PCB milling capabilities, the SOIC is more reliable.

    Notice on each package, there are 14 pins, seven on the left side, and seven on the right. The datasheet has a diagram which tells you what each of these pins can do. The pins can be configured to a variety of uses, depending upon what you want to do.

    The above Figure 1-1 describes each of the 14 pins. When you use a design package such as Autocad Eagle, you will be looking at this diagram a lot. The datasheet is a little intimidating and not very clear to a novice. I recommend you visit a website called Hi-Low-Tech (http://highlowtech.org/?p=1695) which simplifies the ATTiny quite a bit, and provides you a "crossover" map for using it in the Arduino environment. I will try to give a very simplified overview description of the pins. There are two significant groups of pins on the microcontroller, namely PORTA and PORTB which can be used for input and/or output. In addition to input and output, they can have alternate functions. Table 10.3 describes alternate functions for PORTA pins. Table 10-7 describes alternate functions for PORTB pins.

     

    Pin 1: VCC. This is the power applied to the pin. Typically 5 volts DC. This pin has only one function. Red wire from power supply paths to here.

    Pin 2: PB0 - PORTB-0 can be connected to an external clock, crystal oscillator input, pin change interrupt, external clock input

    Pin 3: PB1 - PORTB-1 can be used for pin change interrupt, crystal oscillator output

    Pin 4: PB3 - PORTB-3 can be used for RESET

    Pin 5: PB2 - PORTB-2 can be used for an interrupt, clock out, pin change interrupt.

    Pin 6: PA2 - PORTA-2 can be used for analog digital converter input

    Pin 7: PA6 - PORTA-6 can be used for 3-wire or 2-wire communication input

    Pin 8: PA5 - PORTA-5 can be used for three-wire communication output

    Pin 9: PA4 - PORTA-4 can be used for analog digital converter input

    Pin 10: PA3 - PORTA-3 can be used for analog digital converter input, or timer/counter, or interrupt

    Pin 11: PA2 - PORTA-2 can be used for interrupt, analog digital converter input

    Pin 12: PA1 - PORTA-1 can be used for interrupt, analog digital converter input

    Pin 13: PA0 - PORTA-0 can be used for interrupt, analog digital converter input, and external analog reference

    Pin 14: GND - GROUND - 0 volts. Black wire from power supply goes here.

     

    The ATTiny Family of Microprocessors

    ATtiny24

    The ATtiny24 has 2 KB of flash, 128 B of SRAM, 128 B of EEPROM, and includes the following features:

    ATtiny44

    The ATtiny44 is identical to the ATtiny24 except that it has 4 KB of flash, 256 B of SRAM, 256 B of EEPROM, and includes 6 touch channels.

     

    ATtiny84

    The ATtiny84 is identical to the ATtiny24 except that it has 8 KB of flash, 512 B of SRAM, and 256 B of EEPROM.

     

     

    Hello-World Board Connections

    On the hello-world board, I made the following connections:

    (id='more')

    My instructor, Chris Rohal happened to be familiar with the Sparkfun programmer and directed me to a very useful website called "high-low tech". The link to this website is: http://highlowtech.org/?p=1695. This website explains how to set up the Arduino programming environment to work with a ATTTiny44 target. The Arduino environment comes with a standard list of targets. However, this list can be extended through the additional boards manager. Select "Preferences" and then find the "Additional Boards Manager URL's" data entry box.

    (id='processes')

    You can enter more than one programmer in here, if you separate the URL's with commas. In the above case, I would follow the existing entry with a comma, and then type: https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json

    Click OK and it is saved.

    You can press the small icon at the end of the URL entry box. It will display a list of all the URL's that have been entered into the Arduino environment. In this case, the ESP8266 AND the ATTiny are both listed.

    The next thing you have to do is select the ATTiny from the modified list of targets and install it.

    You do this by navigating Tools > Boards and scroll down until you see attiny

     

     

     

     

    Next, we have to select the correct settings for the board. We will be selecting Board = ATTINY44, 8MHZ internal clock, and Programmer = USBtinyISP.

    Drill the menu as follows to select the ATTiny family:

    Tools > Board > ATtiny24/44/84

    Drill the menu as follows to select the ATTiny44:

    Tools > Processor > ATTiny44

    Lots of windows in this environment. Drill again:

    Tools > Clock > Internal 8 MHz

    Drill again for the right programmer:

    Tools > Programmer > USBtinyISP

    Arduino programming environment is all set. You will have to assign the serial port detected by your computer.

    Switch the slide switch on the programmer to provide power to the hello-world board.

    The next thing that needs to be done is translating ATTiny pins to their corollary Arduino pins. The following is a map from the High-Low Tech website:

     

    So, I expand the mapping of the pins until I identify how I should program them in the Arduino environment.

    (id='programming')

    Blink Program, modified for hello-world board. Two screen shots of a long program. Splice them together at the "for" loop.

    -------------------- first screen shot ----------------------

    ------------------ second screen shot ---------------------

    (id='files')

    (File: sketch_apr03a.ino)

     

    (id='Atmel-Ice')

    Atmel-Ice Toolchain and C

    The Atmel-Ice is a more advanced programming tool. The libraries are more specific to the Atmel microcontroller family. There are extensions for other processors, but I don't think they are as extensive as the Arduino IDE. The learning curve is higher. However, the programmer allows you to run real-time debugging through the programmer, which is not possible with the Arduino IDE. Studio 7 is only available for Windows 10. There is no version for the Mac. I installed the application using a Lenovo ideapad 100, Intel Core i5 processor, and 4GB RAM. Performance is sluggish. I plan on using a desktop with a similar processor but more RAM.

    I purchased my Atmel-Ice from Digikey (ATATMEL-ICE-ND) $130 which is the EMU FOR SAM AND AVR MCU KIT. In retrospect, the ATMEL-Ice BASIC kit would have sufficed (Digikey Part number ATATMEL-ICE-BASIC-ND ~ $90).

    First thing I did was download the free app from the Atmel/Microchip website: http://www.microchip.com/avr-support/atmel-studio-7

     

    Scroll down, and you see a box which includes a menu of things you can download. Download the app using the recommended method

    Further down in the website, you will see additional tools, named Atmel START. The library is pretty extensive. There are debugging tools here you don't find in the Arduino IDE environment.

     

    The library includes Data Visualizer tools. You basically can use the Atmel-ICE as an oscilloscope, without probes. How nice is that.

     

     

     

    First thing is to download and install the application. I prefer the recommended web installer, but either one will work fine. After you download the installer, start it up and follow the prompts. Studio 7 allows you to install just the portion of the package you are interested in working with. In my case, I am interested in the ATTiny44 processor, so I check off AVR 8-bit. I may also want to use Atmel Studio 7 on the different Arduino boards and ESP modules I have, so I also check off AVR32 bit ND SMART ARM (SAM).

     

    The application takes quite a while to load and requires plenty of interaction from the user, so don't plan on going anywhere for about an hour.

     

     

     

    Once you have the application loaded, start it up. Atmel Studio 7 greets you with a lengthy splash screen. Be patient. It takes about 10 minutes on my machine. If you have a decent PC, probably less.

     

     

    Once you have the application running, you should check for updates. If you want to run the Arduino IDE within the Atmel Studio, then you should add the VisualMicro extension. There is a free teaser, or a full version for individuals for around $30. I suggest you go to visualmicro.com and buy the key to the full version because the crippled version is not going to help you much.

     

    I also went to gallery.atmel.com where you will find you can download the "Arduino IDE for Atmel Studio 7" as well as the Microchip Gallery, a LUFA library. There is also many other interesting tools which I plan on exploring later.

     

    Once you have installed the application, time to see if the laptop recognizes the device. It appears it does. My machine is also displaying a USB port which is having difficulty. I eventually will delete the "unknown USB device".

     

     

    Next, I select "GCC C Executable Project" from the list. Since I just installed the VisualMicro extension for Arduino IDE, I will explore what is there.

    Next, AtmelStudio asks for the name for a sketch, the target board, and device. You also need to select the Arduino IDE Path, target board, and microcontroller device.

    This error message keeps popping up. I don't know why. It seems Microsoft related.

    I return back to what is called the Start Page. I will need to get some information from my system, and the best way to do this is to run the Arduino IDE from the main Windows Start Menu, SEPERATE from AtmelStudio 7. I will be switching back and forth between apps, and it gets a little confusing, but I found it to be the best way to set up Atmel Studio 7.

    Once you have AtmelStudio 7 running and the Start Page displayed, start the Arduino IDE application in a separate window. Once you have the Arduino IDE running, open up an example program that you know runs. I suggest you open up the Blink example and use a board that you know to work. The Arduino Uno is a good reference board. Connect the Atmel-Ice to the Uno using the ISP connector. The bump on the connector should be facing in towards the center of the board. The Atmel-Ice does not provide power to the board. You must use a power supply or the USB connector on the board. With my Hello-Output board, I connect with the ISP cable and power with the FTDI connector. Make sure the settings in the Arduino IDE match the board you have connected. You can do the following procedure for whichever board you choose to connect, and develop a quick list of parameters you need to enter later for the board you want to develop.

     

    Next, drill the File menu down to Preferences.

     

    A "Preferences" dialog box pops up.

    Make sure the "Show verbose output during: upload" is checked off.

    I like testing with a readily available reference board. That way, if there is an issue, I can try a second board, and also try similar boards on other machines. I prefer this as a troubleshooting strategy. So in this instance, I continue on with an Arduino Uno. Setting up the Arduino Uno for the Arduino IDE is pretty well documented. I will do it here since we are going to glean some important information from the resulting screens. Select "Tools", "Board", "Arduino Uno"

    Next, select, "Tools", "Programmer", "Atmel-ICE"

    Upload the sketch to the Arduino Uno.

    After the upload completes without any errors, scroll to the top of the output box and copy the following lines into Notepad. Take the first highlighted section which ends with avrdude, and add the .exe extension to make it avrdude.exe, and then press RETURN.

    The balance of the line contains data about the parameters for the target board which will need to be used in AtmelStudio 7.

    The first highlighted text describes where your Arduino IDE environment is located:

    The next highlighted sections are parameters regarding your target board. In this example, the target board is an Arduino Leonardo (-patmega32u4) utilizing Comm port 5 with a baud rate of -b57600. When I tried an Arduino Uno, it resulted with (-patmega328p), Comm port -PCOM6 and a baud rate of -b115200.

    The last highlighted section has information about the example sketch we used to upload to the board. Soon, we are going to reformat all of this information.

     

    You will need to add a quote-mark at each of the highlighted areas of the text as shown in the following box.

    Next, select and cut the text highlighted in blue in the following box.

     

    Replace what you just cut with the following text: "$(ProjectDir)Debug\$(TargetName)". When you are done, it should look like this:

    After you have made these changes to your notepad file, you can repeat them for whichever processor you would like to use with the Atmel-Ice programmer. Keep the Notepad window open. We are not done with the Notepad file, but you can close the Arduino IDE at this point. Now go back to the Atmel Studio 7 application. Select "New Project" as highlighted below, and click it with your mouse.

     

    A "New Project" screen will pop up. Select "GCC C Executable Project" as shown below.

    Next, you select target device family. In this case, ATmega.

    Next select device within the family of devices. In this case, ATMega328P for the Arduino Uno.

    Next we drill down into Tools menu. This is where we are going to add some parameters.

    Select "External Tools" and press with a left-click of the mouse.

    The External Tools dialog box pops open.

     

    I enter "Arduino_ISP_bootloader" for the title. This will later show up in the Tools Menu with this name. You could name it whatever you like. After that is complete, we are going to copy and paste some of the formatted text in the notepad file into the line labeled "Command:"

    Click your mouse in the Command box. Next, switch to Notepad app and select and copy (CTRL-C) the highlighted text:

    Go back to AtmelStudio app. Click on the Command box and paste (CTRL-V) the text you just copied from the Notepad file. It should look like what is circled in the following screen. After that is complete, we are going to copy more text from the Notepad file to fill into the arguments section, which I have highlighted below.

     

    Switch to the Notepad file and select-copy highlighted text.

    Go back to AtmelStudio, and click in the Arguments field. Then paste (CTRL-V) the text you just copied from the Notepad file. It should look like the following highlighted section:

    Check off the "Use Output Window" box and then press "Apply" and then press "OK".

    Next, we look at the schematic for the target board. IO13 goes to pin 32 which is PC7

     

     

    We finish by writing a program to flash the LED:

     

    Use plenty of comments by using /* comment comment */ so you don't confuse people with a silly narrative. While you enter your program, AtmelStudio provides a lot of helpful pop-up boxes. I find them kind of annoying.

    Next you drill down into the Build menu to Build Solution.

    The Output Box expands and scrolls as it is building your project.

    When the project compiles properly, it reports "Build succeeded".

    At this point, you can switch back and forth between processors, making sure you change the COM port and baud rate and target processor. Notice that the External tool that I built shows up in the menu here. Earlier, I stated you could name it whatever you wanted. I believe you can enter multiple External tools, perhaps one for each processor you want to develop. So instead of "Arduino_ISP_bootloader", it could be "Hello-Output_bootloader", "hello_Solenoid_bootloader" and so forth.

    In this instance, I just went back to my Notepad file and cut and paste different sections to the External tool below to effect the changes I need.

     

    Whenever you unplug and replug a device, you can go back to the Arduino IDE to see what COM pert is assigned. AtmelStudio is weak in this area.

    Once you determine the current COM port, make the change to the External Tool.

     

     

    Project Folders and Files for Atmel-Ice/Studio 7:

    (id='files-Atmel-Ice')

    (File: GccApplication1.zip)

    (File: GccApplication3.zip)

    (File: GccApplication4.zip)

    (File: main-arduino-uno.c)

    (File: Atmel-Ice-Arduino-Leonardo-Parameters.txt )

     

    GreatCowBasic

    (id='GreatCowBasic')

    I decided that on Memorial Day, I would try to program my microcontrollers with BASIC. My roots in computing go way back to the beginning when BASIC was an experiment of John Kemeny and Tom Kurtz at Dartmouth College, and I got to play with a Digital PDP/8 which you loaded a version of BASIC with rocker switches and a paper tape teletype. I remember the development of BASIC into sBASIC, a structured, compiled language by Stephen Garland. BASIC was intended to be an easy to learn language, and intended to democratize computing in education. So, in memory of all the hard work my professors did, I would try to use BASIC as an additional language. I found GreatCow BASIC (GCBASIC) as one of the languages Neil has listed, so I decided to explore it.

    GreatCowBASIC is both a language and an IDE. There is a graphical version as well, but I did not have a chance to explore that. I tried it, and I succeeded. I found the installation to be simple. I found the features to be extensive. The documentation exists, but is not completely comprehensive. There are a lot of menus to drill through, similar to the Arduino IDE. The language and IDE is free, limited by a Creative Commons license. It has an extensive list of examples, which are nice starting points to learn from. It supports a large list of "chips" as well as a long list of programmers, including the Sparkfun AVR Pocket Programmer, all of the Atmel programmers, including the Atmel ICE and JTAG ICE. Supposedly, you can program through an Arduino COM port, but I could not get that to work. I relied on my Sparkfun AVR Pocket programmer to both power and program through the ISP port of a Arduino Uno, an Arduino Leonardo, my HelloEcho board, as well as my HelloSolenoid Board. Everything worked.

    I first started by using a reference board, the Arduino Uno. Not having anyone to show me anything, my only resource for learning was "the internet". There are some tutorials out there for GCBASIC, but they are for earlier versions. The latest version, 0.98.02, is considered a BETA version. There is a home page here (http://gcbasic.sourceforge.net/Typesetter/index.php/Home) which describes the "Great Cow BASIC Suite", a little bit about it's history, and a features list. GCBasic seems to support LED displays, which makes sense since one of BASIC's strengths is string handling, although I have not tried this yet. The IDE claims it can run on Windows, Mac and Linux. I could not get it to install on a Mac so I installed it on a Lenovo ideapad 100 laptop with an Intel Core i5 processor and 4GB of memory. It ran very well on a machine which struggles with just running a browser. It seemed to compile "Blink" program into native code less than 200 Bytes. That's impressive.

    First, go to the GreatCowBasic website http://gcbasic.sourceforge.net/Typesetter/index.php/Home and click the DOWNLOAD heading. You will be directed to a page which contains a table containing different levels of the GCBasic suite. The first is the BASIC IDE (Windows 10), The second is Graphical BASIC (Windows 10), and the third is a BASIC Linux version. Supposidly there is a Mac version. I could not find it.

     

    After you downoad and install the application, ot automatically opens the IDE right to an example program. The example program blinks a LED. Great, just what I was looking for. The example is well documented and tells you how to modify the code for different microcontrollers, or "chips".

     

     

    The default setup is for a board with the 16F877A chip. I am starting with an Arduino Uno as a reference board. I need to comment out the active line #26 with an apostrophe and remove the apostrophe from line 27 to make it active. An apostrophe is how you create a comment line in BASIC. The comments are shown in green. Lines that are not green are program statements.

     

     

    This is how the program looks after I change the #chip statement. Notice how the colors have changed. The rest of the program is an infinite loop (Do Forever .... Loop). The program is going to set Port PB5 high for 100 milliseconds and then turn the LED off. Next the program will wait for 900 milliseconds.

     

    You can specify the speed in MHz you want to run the chip at by appending the chip statement with the speed you desire. In this case, 16 MHz.

     

     

     

     

     

     

     

     

     

     

     

     

     

    Next, we try setting up the IDE to communicate with the board. At this point, I have the Arduino Uno plugged into a USB Port. The Windows control panel has the Arduino Uno registered as COM6.

    I select Arduino Uno and press EDIT. Another window pops up, where I can modify speed and COM port settings.

     

    I double check with device manager which COM port to use. Today I have two boards plugged in. COM6 is Arduino Uno. COM4 is Arduino Leonardo.

     

    I change the baud rate to 115200 for COM6 and press OK.

    I go back to the IDE and I make sure the communication settings match. Once they match, I press OK.

    Next, I press the green tile called "hex". This compiles the program.

    Yet another window pops up and shows the progress of the compilation of the program.

     

    The IDE keeps progressing, creating different files needed for compiling.

    I kept getting an error message about not being able to communicate with the board. I tried multiple times, with the Arduino Leonardo as well. My guess is that the IDE is expecting you to use an Arduino as a programmer. That is not what I intended. I unplugged the Arduionos from the COM port and plugged in my Sparkfun AVR Pocket Programmer. I then drill down the IDE Tools menu - AVRdude GUI menu so I can set up my toolchain.

     

     

    This pops up a different menu called AVRDUDESS 2.4 which allows you to select a Programmer, which in this case is USBtiny. I change the port to "usb" and then I press "Detect".

     

    The toolchain works. It detects the ATMega328P which is the standard chip in the Arduino Uno. I next close this window and press the green/yellow tile called "Hex/Flash". This compiles and loads the program to the board successfully. It is very quick, even on my sluggish laptop. The program is 124 bytes. Very efficient.

    I try with the Arduino Leonardo, and have similar success. After that, I try flashing my HelloEcho and my HelloSolenoid boards. I try to detect the new boards. That fails because I did not switch chips. One of the things you need to do when switching boards with different chips is run the external tool called AVRDUDESS again, which was demonstrated earlier. In order to switch chips, you need to pull down the right table and scroll through the very long list of compatible processors. Here I am going for ATtiny44 which is what I used on HelloEcho and HelloSolenoid.

    After selecting the correct chip, I press the "Detect" button. Success!

     

    I decide at this point I should save my work that I have done so far, before I start altering the chip statement in my program.

     

    Ok, now I will make changes in my BASIC program to match the new chip. The HelloWorld and all the variations of that original board I made use the ATTiny44 chip. I comment out line 27 rather than delete or change. I add a line 28 which includes the statrement for the chip on the Hello-series boards. In those cases "#chip tiny 44".

     

    I quickly refer back to the notes in this webpage for the data sheet for the Hello board. I change line 38 to match the port. Unlike the Arduino IDE where you have to translate everything to match up to a reference board, here you just put in the name of the port and pin. Eesy Peezy. In the case of the HelloWorld board, the HelloEcho, and the HelloSolenoid, the LED is on PA7.

    I then press the Hex/Flash tile and run the program. Success!

     

    GreatCowBASIC Blink program running on the Hello-Solenoid board.

    Click this link to run video: GreatCowBasic-Hello-Solenoid.mp4 or on Vimeo: https://vimeo.com/275387630

     

    (id='files-GCBasic')

    (File: arduino-uno.zip)

    (File: hello-echo.zip)

    (File: GreatCowBasic-helloEcho-compile-message.txt)