Programming an ATmega328

Ever heard about Barebone Arduino? It consists of trimming down an Arduino to it’s very basic parts and can be really effective in cost reduction.

3197918087_c578d8c12f

Tipical Barebone Arduino

Even it might seem the simplest version of an Arduino (actually I wouldn’t be calling it an Arduino any more) we can go even further and drop the external crystal in favor of the internal (yes, there is one) oscillator: that is now just a plain ATmega328:

328pb_rev1_pinout

Thanks to pighixxx for his beautiful pinout diagrams!

To program an ATmega328 or, for what matters, many other MCUs, you can use an USBasp, an hardware device providing direct access to the MCU via ICSP, aka a programmer. Installing the USBasp drivers under Windows 10 has been time consuming, but luckily a very simple solution does exist.

I’m not going into the details of how you connect the USBasp to the ATmega328 as this is information widely available on the web and the first link is quite comprehensive in this regards. So the remaining part is, obviously, software!

There is only one option in my mind when it comes down to programming microcontrollers: Sloeber (formerly known as Arduino Eclipse Plugin). The latest release is just great and not only because I did personally contribute a few bits here and there, but because the whole thing is just a leap step forward in MCU development compared to Arduino IDE.

Programming a bare ATmega328 though has provided some challenges I’m here to describe for those who might encounter the same issues.

Installing the tools

With Sloeber it’s quite easy to install support for new boards, even those which are not built-in or, if you prefer, third-party.

Just open Window > Preferences > Arduino > Locations, you’ll find a link in the bottom part of the settings page, click on it and search the web page for the board or board set you’d like to install. I was looking for “barebone” and that is what I added into my list of locations: https://raw.githubusercontent.com/carlosefr/atmega/master/package_carlosefr_atmega_index.json (if you want to shortcut and avoid the issues I’ve faced, an alternative URL is available at the very bottom of this article).

locations

Then switch to the Platforms and Boards section to select and obtain the definitions to build for a bare ATmega328: obviously I’m going for the latest available version.

platforms

After a few seconds I got my Sloeber ready for compiling what would be a normal Arduino project but targeted at a barebone Arduino!

build config

My choice was to create a custom build configuration, but that’s totally optional.

Filling the gaps

I encountered a few compilation problems though, which I was able to overcome with some support.

Making the long story short I had to modify the JSON file associate to the Barebones ATmega Chips in order to provide some missing information:

  • the tools section, originally consisting of an empty array, should contain the references to the two tools used by the package, avr-gcc and avrdude
  • the toolDependencies section needs to be updated in order to match the tool versions now being linked

I’ve prepared a fixed JSON package file available from https://raw.githubusercontent.com/rlogiacco/arduino-eclipse-plugin/packages/package_atmega_index.json.

Also I had to copy the programmers.txt from arduinoPlugin\packages\arduino\hardware\avr\1.6.13 to arduinoPlugin\packages\atmega\hardware\avr\1.3.0 in order to make the USBasp protocol available with the new boards file.

Setting the fuses

When you buy a plain ATmega328 micro controller you are more than probably going to get the chip configured to the factory settings, meaning it will run at a 1MHz with it’s internal oscillator. That’s obviously a very conservative setting on which you have control, along with some other settings, via the chip fuses.

If you never had to deal with chip fuses before and the AVR ones in particular, you might want to read at least an introductory explanation  before going to change any of them.
WARNING: an erroneous setting of the chip fuses can turn your micro controller into a desk decoration, don’t blame me if that occurs to you!

I want the chip to run at 8MHz which requires just one bit to be changed, but I’m taking the opportunity to also tweak other parts of the settings and I’m going to use a nice AVR Fuses Calculator web application to do so:

  • low fuse 0xE2 means “use the internal 8MHz clock source and run at full speed
  • high fuse 0xD6 means “don’t clear the EEPROM each time and don’t reserve any space for a bootloader
  • extended fuse 0x07 means “don’t auto reset on min voltage

To do so I’m using the avrdude tool, which I already got installed automatically by Sloeber.

To quickly get to avrdude look at the last command printed by Sloeber into the upload console when you try to upload any firmware/sketch to the micro: it should look like the following

Launching X:\Sloeber\arduinoPlugin\tools\arduino\avrdude\6.0.1-arduino5/bin/avrdude -CX:\Sloeber\arduinoPlugin\tools\arduino\avrdude\6.0.1-arduino5/etc/avrdude.conf -patmega328p -cusbasp -Pusb -Uflash:w:D:\Projects\.cpp\Serial/Release/Serial.hex:i

The bold part is what you want to copy past into a command prompt and then add the missing bits:

X:\Sloeber\arduinoPlugin\tools\arduino\avrdude\6.0.1-arduino5/bin/avrdude -CX:\Sloeber\arduinoPlugin\tools\arduino\avrdude\6.0.1-arduino5/etc/avrdude.conf -patmega328p -c usbasp -U lfuse:w:0xE2:m -U hfuse:w:0xD6:m -U efuse:w:0x07:m

If all goes well you should get a nice and reassuring confirmation message like the following:

avrdude: safemode: Fuses OK (H:07, E:D6, L:E2)
avrdude done. Thank you.

Upload a firmware

Once you have all of the above in place, uploading a firmware to your micro controller is not going to be different from doing the same on any of your Arduino boards: hit the upload button, let Sloeber compile your code and enjoy your cheap and very professional setup!

Advertisements

The SparkCore: makers meet the cloud!

Ok, I’ve recently got caught into the makers fever, I admit it.

Now, I’ve recently got two Spark Cores (those little devices are amazing!) I intend to use for a project of mine, but the nRF24L01 library I need has not been ported yet for this little beast.

Hey, this is the open source and open hardware ground: let’s contribute!

Sadly setting up the non cloud based environment on my Windows 7 64bit laptop was not that easy and it took me about an entire saturday morning: what follows is the summary of my experience in a step-by-step guide.

Step 1: get the base software

Start by downloading the following pieces of software and install/uncompress them all but Zadig (it doesn’t need to) and the Spark Core Serial Driver (you can’t until we reach step 4) using an installation folder of your choice, but avoid anything like the Program Files (x86), those brackets and spaces will drive you crazy:

  • GCC ARM ver. 4.8
  • Make 3.81
  • DFU-util ver. 0.7 binary
  • GIT ver. 1.8.5
  • Atlassian SourceTree (Optional)
  • Eclipse CPP Kepler x86_64 (I’m an Eclipse fan)
  • Zadig ver. 2.0.1
  • Spark Core Serial Driver

Please note it would be much better if you install these softwares into a path that doesn’t contain special characters and/or spaces: my advice is to use something like C:\Spark or something like that.

Step 2: set environment variables

To ease development it’s going to be best if some binaries end up into the system path, like make, gcc and dfu-util. My user PATH variable looks like the following (adjust it with your own installation paths):

C:\Spark\GNU Tools ARM Embedded\bin;C:\Spark\GnuWin32\bin;C:\Spark\DFU Util\win32-mingw32;C:\Spark\Git\bin

Step 3: build the firmware

In order to build your application for the Spark Core you need to rebuild the entire firmware, but don’t get scared by that: you are already doing it when you use the cloud based IDE, even if you are not really aware of it.

To re-build the firmware you need to get its sources which, I know it might sounds weird, are openly and freely available on Github: what you need to do is use the GIT command line tools (or a GUI wrapper like SourceTree) to clone three repositories:

At this point all you need is to build your own firmware by issuing the make command from within the core-firmware/build folder, so get on the command line and do it!

Step 4: install the drivers

Next step was tricky to get right for me, but only because I did it in the wrong order, which means this guide should make it a piece of cake for you.

First let’s install the serial driver to allow debugging of our code. Plug your Spark Core into a USB port and start holding the MODE button (the one on the analog pins side) for a ìbout 3 seconds, until the led starts blinking blue: congrats, you just put your device into Listening Mode.

At this stage you should hear the usual Windows sound telling you a new device has been plugged in: in a few seconds the classic notification windows will tell you a driver is going to be searched and, after a while, the “No driver could be found” warning. It’s time for the Spark Core Serial Driver we downloaded earlier to get into the scene: click on the warning window and point the Windows driver installation procedure to it to get it working!

You should be able to find the Spark Core among the computer serial ports with a nice label saying Spark Core with Wifi and Arduino Compatibility (mine is recognized as COM12, but yours might be different).

This is the driver you are going to use to get access to the device Serial port on USB, but we need to use the USB line for another important task: flash the firmware.

What you need to do is to switch the device into DFU Mode: while connected to USB, press and hold the MODE button, after one second tap the RESET button and realease it while keeping the MODE button pressed. After three seconds or so the RGB led should start fast blinking yellow and which means it gots into DFU Mode and another Windows sounds is played: congrats again!

Get to the command line again and this time execute:

dfu-util -l

You should get something like the following as output, which means your Spark Core is almost ready for flashing:

Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2012 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to dfu-util@lists.gnumonks.org

Found DFU: [1d50:607f] devnum=0, cfg=1, intf=0, alt=0, name=”UNDEFINED”
Found DFU: [1d50:607f] devnum=0, cfg=1, intf=0, alt=1, name=”UNDEFINED”

It’s time to run the last tool in our tool chain: Zalig. If you still rememeber where you downloaded it take the hassle to double click on it and install the WinUSB driver for the CORE DFU device to allow the flashing procedure to work as expected.

Step 5: flash it!

Well, this is the easiest step, all you have to do is to execute the following on the command line:

dfu-util.exe -d 1d50:607f -a 0 -s 0x08005000:leave -D core-firmware\build\core-firmware.bin

Just be sure you actually are pointing at existing files 😉

Step 6: make it sweeter!

Now that you are capable of flashing the Spark Core without the need of the Cloud IDE I believe you want to be able to use something better than the Windows Notepad to edit and build your projects: my choice is Eclipse so let’s make the build process a little more visual.

For each of the three cloned projects, from within Eclipse CPP

  1. Import > Existing Code as Makefile Project
    import
  2. Select GNU Autotools as Toolchain
    import-2

For the core-firmware project only:

  1. From the project contextual menu Make Target > Build…
    1. Add target “all”
    2. Add target “clean”
  2. From the project contextual menu Properties > C/C++ Build
    1. Within the Builder Settings tab set the Build-Directory to ${workspace_loc:/core-firmware}/build
    2. Within the Environment subsection add a PATH variable pointing at your system environment path
      environment
  3. Project > Build All should make the same binary as from command line
  4. Select Run > External Tools > External Tools Configurations… and create a new configuration to execute the dfu-util command to upload your firmware with a mouse click!
    external-tool
  5. If, like me, you are able to build everything but you get validation errors within Eclipse, add the GNU ARM Toolchain includes to the core-firmware project libraries by adding the arm-none-eabi\include subfolder of the GNU ARM Toolchain to Project > Properties > C/C++ General > Paths and Symbols  > Includes > GNU C++
    paths-symbols

And that’s all folks!

Stepping up in Arduino development

Ok, you have done some experience with your arduino, wired up some examples and you feel proficient with the Arduino IDE, but you feel you are missing something on the software side: a real integrated environment with auto completion, source code formatting and all that stuff.

Why don’t you switch to Eclipse? Well, that’s an easy task nowdays, thankfully to Jantje and the rest of the Eclipse Arduino team.

The learning curve, which was the reason why I waited so long to try it, is actually not steep at all: I actually managed to compile and upload a very basic sketch to my Arduino Micro in just 5 mins.

The installation wasn’t issue-free though, but if read carefully the instructions on the official web site and Google a little bit about the issues you might encounter, you will get up and running in no time.

My environment is set up on a Windows 7 64bit laptop with a Java 7 64bit runtime. I decided to go for the most advanced (and most complicated) configuration by setting up Arduino Eclipse on top of Arduino IDE version 1.5.5-r2 (aka version 1.5.5-beta).

I’ve tried to follow the almost step-by-step instructions available on the Eclipse Arduino Plugin website, but I found a couple of misleading/missing bits of information:

  • when using the 1.5.5-beta Arduino IDE, you need to update a quite high number of source files, the best way to do that is by running the script reported on the plugin website using CygWin, but you have to run the script from the top Arduino IDE folder, rather than from the hardware subfolder as advised
  • when you install the Arduino IDE, do not install it in a folder containing strange characters, including brackets like the Program Files (x86) directory as I am used to: that will cause a weird make error to occur
make: Interrupt/Exception caught (code = 0xc00000fd, addr = 0x4217b3)

I actually haven’t had any other issues and I’m now an happy user of the Arduino Eclipse Plugin, looking forward to contribute to those guys effort as much as I can: come join me!