OpenWSN with the AVR Toolchain, JTAGICE3 debugger/programmer and Zigduino platform on Windows and Linux

The Atmel AVR Toolchain is a collection of tools/libraries used to create applications for AVR microcontrollers. This collection includes compiler, assembler, linker and Standard C & math libraries.

As-of-yet this toolchain has not been integrated into the main OpenWSN Scons build environment.

Currently, the OpenWSN Zigduino fork (available at https://github.com/SvenAkk/openwsn-fw ) contains adjusted Sconstruct and Sconscript files that allow working with this toolchain for AVRDUDE based programming of a Zigduino (an Arduino board) using a JTAGICE3 on a configured Windows Machine. These files are also included on this page but the most recent versions are always on the fork.

Using this guide allows a Windows 8.1 or Linux  (tested with Ubuntu 14.04) machine, using a JTAGICE3 programmer, to flash the Zigduino platform with the OpenWSN Zigduino port firmware. No guarantees are made for other software or hardware although this will likely be similar.

 

AVR Toolchain for Windows 

There are no official easy-to-use recent avr-gcc versions available. The easiest way to work with AVR for Windows is using Atmel Studio, which forces using a big software solution, or using the outdated WinAVR tools, which is incompatible with more recent JTAG devices such as the JTAGICE3.  In addition, most online resources are (partly) outdated, though still useful (See also: http://m8051.blogspot.be/2015/01/avrdude-on-windows-long-time-after.html ). This guide describes how to easily get a more recent version of AVRDUDE working with Windows. Alternatively, you can always compile the most recent version of AVRDUDE yourself (from http://www.nongnu.org/avrdude/ ) but this is a fairly painful process on an unconfigured Windows computer (see also).

There is a recent avr-gcc version compiled for Windows which can be found here: http://andybrown.me.uk/downloads/ .

  1. Extract the zip file to a location on your hard disk. These command-line tools are not happy to see pathnames with spaces in them so I recommend that you extract to the root directory of your C: drive. That will result in everything being installed into c:\avr-gcc.
  2. It’ll be most convenient to add c:\avr-gcc\bin to your PATH environment variable.
  3. You may need to logout and login again to detect the changes to PATH.

The AVR toolchain itself should now be working on your computer. Test it out by opening the command line terminal and doing and seeing the following:

> avrdude -v
avrdude: Version 6.1-svn-20131205, compiled on Dec 5 2013 at 17:34:22
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2009 Joerg Wunsch
System wide configuration file is "C:\avr-gcc\bin\avrdude.conf"

JTAGICE3 with Windows

Unfortunately, AVRDUDE relies on the JTAGICE3 programmer to be connected to a Linux USB port such as LibUSB (source). In addition, depending on the firmware of the JTAGICE3 the USB descriptors might be totally different (source and source).

First, if needed, downgrade the firmware of your JTAGICE3 device to a v2. To do so follow this guide.

Second, connect your JTAGICE3 device to your computer. It should be recognized and automatically install some USB drivers. At this point, avrdude will most likely not work.

To fix this, download Zadig and run it. Select your JTAGICE3 from the list of devices and install the libusbK driver. 

You should now be able to use avrdude to flash your firmware using a JTAGICE3.

AVR Toolchain on Linux

Using the AVR-toolchain is much simpler on Linux.

Simply install the avr toolchain using:

sudo apt-get install avr-libc binutils-avr gcc-avr avrdude avarice gdb-avr

This can possibly install an outdated version of avrdude.

To get a more recent one (that can work with the JTAGICE3) do the following:

sudo add-apt-repository ppa:pmjdebruijn/avrdude-release
sudo apt-get update
sudo apt-get install avrdude

and now everything should work as is, with the modified files.

Note that Linux might require adding "sudo" in front of commands to get access to the relevant USB ports to be able to flash.

Note also that this does not install the most recent versions of this software. Therefore, it is sufficient to program but, for example, lacks debugging support with the JTAGICE3.
See the "Debugging" section for more info on how to make this work. 

Tying it together – integrating it all and flashing the Zigduino firmware

As a reference on how you can use this toolchain for your own port, we describe how to do it for the Zigduino platform.

Download the openwsn-fw code as found on  https://github.com/SvenAkk/openwsn-fw.

Connect your JTAG to your Zigduino and your PC and have the Zigduino be powered. If this is the first time you flash the Zigduino, you will need to adjust the fuses, see "Adjusting fuses"

Using a terminal, navigate to the openwsn-fw directory and do and see the following to flash the openwsn project over a JTAG interface:

Sven@S-WORK-LAPTOP C:\Users\Sven\Desktop\OpenWSN_Sven\openwsn-fw
> scons board=zigduino toolchain=avr jtag=x oos_openwsn
scons: Reading SConscript files ..
 ___                 _ _ _  ___  _ _
| . | ___  ___ ._ _ | | | |/ __>| \ |
| | || . \/ ._>| ' || | | |\__ \|   |
`___'|  _/\___.|_|_||__/_/ <___/|_\_|
     |_|                  openwsn.org

scons: done reading SConscript files.
scons: Building targets ...
Dynifying build\zigduino_avr\openapps\openapps_dyn.c
avr-size build\zigduino_avr\projects\common\03oos_openwsn_prog.exe
   text    data     bss     dec     hex filename
  94658    4918    4511  104087   19697 build\zigduino_avr\projects\common\03oos_openwsn_prog.exe
avrdude -c jtag3isp -p m128rfa1 -B 1 -U flash:w:build\zigduino_avr\projects\common\03oos_openwsn_prog.ihex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.25s

avrdude: Device signature = 0x1ea701
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 "build\zigduino_avr\projects\common\03oos_openwsn_prog.ihex"
avrdude: input file build\zigduino_avr\projects\common\03oos_openwsn_prog.ihex auto detected as Intel Hex
avrdude: writing flash (99576 bytes):

Writing | ################################################## | 100% 11.00s

avrdude: 99576 bytes of flash written
avrdude: verifying flash memory against build\zigduino_avr\projects\common\03oos_openwsn_prog.ihex:
avrdude: load data flash data from input file build\zigduino_avr\projects\common\03oos_openwsn_prog.ihex:
avrdude: input file build\zigduino_avr\projects\common\03oos_openwsn_prog.ihex auto detected as Intel Hex
avrdude: input file build\zigduino_avr\projects\common\03oos_openwsn_prog.ihex contains 99576 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 9.25s

avrdude: verifying ...
avrdude: 99576 bytes of flash verified

avrdude: safemode: Fuses OK (E:F5, H:D7, L:F7)

avrdude done.  Thank you.

scons: done building targets.

 

If you see this, your Zigduino has been successfully flashed with the OpenWSN firmware.

Note that the adjusted Sconscript can also flash using  an ISP interface. To use JTAG, simply do the above with a a 'jtag=' command. To use ISP, simply replace that command with 'isp='.

The variable included does not actually matter; the port itself is automatically detected and will simply flash the board it is connected to. 

Adjusting fuses

If you need to adjust the fuses, simply go to the following code in Sconscript:

 

#============================ upload over JTAG ================================
def jtagUploadFunc(location):
if env['toolchain']=='armgcc':
if env['board'] in ['iot-lab_M3','iot-lab_A8-M3']:
return Builder(
action = os.path.join('bsp','boards',env['board'],'tools','flash.sh') + " $SOURCE",
suffix = '.phonyupload',
src_suffix = '.ihex',
)
# LOW: Transceiver osc as CLK (16Mhz when prescaler 0x0), maximum start-up delay
# HIGH: JTAG/OCD off, SPI on, WatchDog override off (can be enabled at runtime), Bootsize 512b,
# start bootsector at 0xfe00 (word address!), save EEPROM on reflash, start at addr 0000
# EXT: BrownOut at 1.9V
# Last fuse fd -> f5 due to immutable bits, otherwise avrdude gives a verification error
elif env['toolchain']=='avr':
return Builder(
action = 'avrdude -c jtag3 -p m128rfa1 -B 1 -U flash:w:$SOURCE',
#+	' -U lfuse:w:0xf7:m -U hfuse:w:0xd7:m -U efuse:w:0xf5:m', #if you need to do non-debug fuses
#+	' -U lfuse:w:0xf7:m -U hfuse:w:0x17:m -U efuse:w:0xf5:m', #if you need to do debug fuses
suffix = '.phonyupload',
src_suffix = '.ihex',

and uncomment the following line:

#+ ' -U lfuse:w:0xf7:m -U hfuse:w:0xd7:m -U efuse:w:0xf5:m', #if you need to do non-debug fuses

Remember that you only need to set the fuses once per change. Once changed, you can recomment the line again.

Note that if you flash over ISP, you need to adjust the same lines but in the "==== upload over ISP ====" section.

For other ports with the same toolchain and programmer

If you have another board and want to use the same toolchain and programmer, you only need the Sconstruct and Sconscript files with some small additions.

For the Sconstruct file look for the mention of 'zigduino' and simply add the name of your board similarly.

For the Sconscript file look for all mentions of 'zigduino' and follow that example to add the relevant code for your (type of) board.

Debugging with the JTAGICE3 on Linux

This section explains how to debug an atmel board with a JTAGICE3 on Linux.

Note, that I have not been able to get debugging working on Windows due to the lack of support for a recent standalone AVR-toolchain version on this platform. E.g., the JTAGICE3 has no support.

Therefore, only Linux is described. Thanks goes out to http://www.mikrocontroller.net/topic/301030#postform.

Also note that, if you want to debug a Zigduino, you'll need to adjust the fuses accordingly.

 

If you want to debug with the JTAGICE3, you aren't finished yet. 

First off, you will need to update Avarice to the most recent version from their repo (2.13):

svn checkout svn://svn.code.sf.net/p/avarice/code/trunk avarice-code
sudo ./Bootstrap
sudo ./configure
sudo make
sudo make install
:/tmp/avarice-code/avarice$ avarice -h
AVaRICE version 2.13svn20130104, Jun 30 2013 21:26:37
Usage: avarice [OPTION]... [[HOST_NAME]:PORT]
Options:
-h, --help Print this message.
-1, --mkI Connect to JTAG ICE mkI (default)
-2, --mkII Connect to JTAG ICE mkII
-3, --jtag3 Connect to JTAGICE3

Then you need to connect your JTAGICE3 to your board. You will need to connect to 5 pins (+ 2 for ground) (reference: link ).
Note that your platform might not have a compliant 10-pin JTAG header. In that case, you will need to use the provided squid cable to connect.

For instance, this is necessary for the Zigduino platform (see also pinout on which wires to connect where).

You should have established a successful JTAG interface connection now. 

To start debugging, run avarice through the following command:

sven@ubuntu:~$ sudo avarice -P atmega128rfa1 -3 -j usb :4242

AVaRICE version 2.13svn20141210, Dec 13 2015 23:45:03
Defaulting JTAG bitrate to 250 kHz.
JTAG config starting.
Found a device, serial number: J30200026194
Reported device ID: 0xA701
Configured for device ID: 0xA701 atmega128rfa1 -- Matched with atmega128rfa1
JTAG config complete.
Preparing the target device for On Chip Debugging.
Waiting for connection on port 4242.

Finally, you can pick your desired gdb front-end. This has been tested with both regular GDB through the command line interface and with Eclipse.

In both cases, you need to have started avarice as above.

GDB in the command line interface

Simple, point GDB to the project file you want to debug, for example, oos_openwsn, and then attach it to avarice. Then you can start debugging away.

sven@ubuntu:~/Desktop/OpenWSN_Sven/openwsn-fw$ gdb /home/sven/Desktop/OpenWSN_Sven/openwsn-fw/build/zigduino_avr/projects/common/03oos_openwsn_prog
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/sven/Desktop/OpenWSN_Sven/openwsn-fw/build/zigduino_avr/projects/common/03oos_openwsn_prog...done.
(gdb) target remote localhost:4242
Remote debugging using localhost:4242
0x0041ff00 in ?? ()
(gdb) list
30	extern uint8_t radio_trx_end_isr();
31	extern uint8_t radiotimer_compare_isr();
32	extern uint8_t radiotimer_overflow_isr();
33	
34	//=========================== main ============================================
35	uint8_t mcusr_backup;
36	
37	extern int mote_main(void);
38	
39	int main(void) {

 

Debugging through Eclipse

Debugging through a graphical front-end can be easier. This has been tried out with Eclipse. This section has been copied mostly from http://avr-eclipse.sourceforge.net/wiki/index.php/Debugging with some relevant adjustments.

Once avarice is started we can start the Eclipse debugger. But first we need to configure GDB Hardware Debugging.

Open the Debug Configuration dialog: select the project to debug and then click on the little triangle icon next to the debug icon and select Debug Configurations...

The dialog should look something like this:

Debugging debug config dialog.png


If the GDB Hardware Debugging configuration type is not shown you do not have this optional feature installed. Start Software Updates... from the Help Menu and add the CDT update site (at the time of writing http://download.eclipse.org/tools/cdt/releases/8.8). Then browse this update site and install the Eclipse C/C++ GDB Hardware Debugging optional feature.

Now create a new configuration.

The first Tab (Main) should be configured like this, with the C/C++ application pointing towards your file.

For the Debugger tab. Configure your options as shown.


GDB Command: change this to "avr-gdb". The preselected "gdb" does not work, as it would try to debug the target as an i386.

Next continue on the Startup tab and also configure it as shown.


Now the configuration is finished. Click on the Debug button to save everything and if all is well the debugging session should start and your application should stop at the beginning of your mote_main() function in the loaded program.