Flashing a SAM4S MCU

One of my final assignments in my final year of Computer Engineering is to build a ‘Wacky Racer’ car. It is a group assignment with six students in each team. The project requirements include:

  1. Be controllable from a bog-standard infrared TV remote control.
  2. Be controllable wirelessly from a mobile phone.
  3. Can capture still images and send them wirelessly to a mobile phone or laptop.
  4. Have a soft on/off switch that will power up your vehicle.
  5. Have a green LED that indicates the system is powered up.
  6. Have a red LED that indicates that the battery needs charging.
  7. Only use a single battery pack for everything.
  8. Have no trailing cables.
  9. Use almost exclusively surface mount components apart from the supplied connectors.
  10. Monitor the battery voltage and motor speed.
  11. Be dastardly!
  12. Comprise three PCBs, each with a microcontroller:
    1. Motor board: power supply/motor-control/steering/infrared comms
    2. Camera board: using the TCM8230MD image sensor.
    3. Communications board: wifi or bluetooth
  13. The boards are to communicate with an I2C bus only.
  14. Each board is to have a UART or USB debug interface.
  15. Each board needs to be able to run standalone for debugging.

We decided to use a Atmel ATSAM4S16B Microcontroller for all three boards. With our PCBs designed, manufactured and the components placed, and the voltage levels appearing to be correct we then needed to configure a toolchain to connect to our board using it’s JTAG header. The instructions below cover installing OpenOCD 0.6.1 and installing an ARM EABI toolchain. We are using a USB to JTAG converter supplied by our department (Electrical Engineering) at the University of Canterbury, however the process will be similar for commercially available adapters. Note that our adapter is based on a FTDI FT2232 chip.

Populated BoardUSB to JTAG

Ubuntu Linux (12.10) is used in this tutorial.

1). Installing OpenOCD

The FTDI drivers are required. Install these with the following command:

 sudo apt-get install libusb-dev libftdi-dev 

Now, download OpenOCD 0.6.1 from http://sourceforge.net/projects/openocd/files/openocd/0.6.1/openocd-0.6.1.tar.bz2/download

Unzip it (I use the gui folder manager because I can never remember the command) and cd into the openocd-0.6.1 directory. Now build and install OpenOCD with the following commands:

 ./configure --enable-ft2232_libftdi
 make
 sudo make install

Test that the install worked by running openocd and checking that something like the following appears:

Open On-Chip Debugger 0.6.1 (2013-05-17-12:52)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.sourceforge.net/doc/doxygen/bugs.html
Runtime Error: embedded:startup.tcl:47: Can’t find openocd.cfg
in procedure ‘script’
at file “embedded:startup.tcl”, line 47
Error: Debug Adapter has to be specified, see “interface” command
in procedure ‘init’

2). Running OpenOCD

The next thing to do is to get the OpenOCD config files sorted for the SAM4S target and the USB to JTAG interface. Do this by creating a file called myconfig.cfg in your home directory and put the following in it:

# This section configures OpenOCD for using the university's USB-JTAG adapter.
interface ft2232
ft2232_layout usbjtag
ft2232_vid_pid 0x0403 0x6010
adapter_khz 4
adapter_nsrst_delay 200
jtag_ntrst_delay 200

# This section configures OpenOCD for working with a SAM7 chip.
source [find target/at91sam4sXX.cfg]

# Halt the MCU when GDB connects otherwise the connection fails. ($_TARGETNAME is defined in at91sam7sx.cfg)
$_TARGETNAME configure -event gdb-attach {
echo "Halting target due to gdb attach"
halt
}
$_TARGETNAME configure -event gdb-detach {
echo "Resuming target due to gdb detach"
resume
}

At this point, connect your board via the USB to JTAG adapter, and run OpenOCD from the same directory as your myconfig.cfg file with the following command:

 sudo openocd -f myconfig.cfg 

3). Connecting to OpenOCD

There are two approaches to connecting to OpenOCD, telnet or GDB. Use can use one of the following commands in a new terminal to connect to OpenOCD:

 telnet localhost 4444 #telnet 

 

 telnet localhost 3333 #gdb 

For the remainder of this tutorial we shall focus on using the GDB connection method.

4). Configuring an ARM EABI toolchain (with GDB)

We need to install a version of GDB configured for an ARM target. We might as well install our cross-compiler at the same time. This can all be achieved in one swoop by using James Snyder’s build system from https://github.com/jsnyder/arm-eabi-toolchain.

Install this as follows (alternatively, if you are used to cloning Git repos you can just follow the README on the github page):

Install git:

 sudo apt-get install git 

Install the required dependencies for the toolchain:

sudo apt-get install curl flex bison libgmp3-dev libmpfr-dev texinfo \
      libelf-dev autoconf build-essential libncurses5-dev libmpc-dev \

Clone the repo:

 git clone https://github.com/jsnyder/arm-eabi-toolchain.git 

Install the toolchain:

cd arm-eabi-toolchain
sudo make install-cross

Now wait for ages while the makefile does it’s thing. When it’s done, add the stuff to your system path and clean up.

export PATH=$HOME/arm-cs-tools/bin:$PATH
sudo make clean

You can check this all went well by starting the ARM version of GDB at the command line.

arm-none-eabi-gdb

You should be presented with a (gdb) prompt.

5). Using arm-none-eabi-gdb alongside OpenOCD

The following script can be used to upload a program to the SAM4S target using the GDB method:

target remote tcp:localhost:3333
monitor reset
monitor sleep 500
monitor poll
monitor soft_reset_halt
load
monitor reset

Save this script as program.gdb. Once you have compiled your program, and with the OpenOCD dameon running, you can invoke this script using the following command:

./arm-none-eabi-gdb -batch -x 'program.gdb myProgram.elf 

Where myProgram.elf is your compiled application.

TODO: 6). Compiling a program for the SAM4S + an example blinking LED program.

Life in the cloud

For day to day computing, my web browser is the only program I use. Email, documents, spreadsheets, calendar, photos, music, even LaTex documents; it’s all in the cloud these days. The advantages of this move to decentralized computing have been well documented and I won’t go into them here. However, what hasn’t evolved at the same rate is a method of keeping all these services accessible and organized.

Since 1993, bookmarks have been a feature of all major web browsers and the canonical way of keeping sites organized. This worked okay at a time when sites were static and could be easily categorized into subjects and folders. However, these days, one of the main issues I have is remembering what service I stored something on – ‘What was that cool new js library again? Did I star it on Github, or save it to Evernote or Pocket…’. ‘That online banking form we filled in for the club last year, did I save that to Dropbox, or is it loitering in my Gmail account somewhere? or was it my Uni email?’. I’m sure this sort of thought process must be familiar to anyone who is as immersed in web applications as I am, and bookmarks are no help for this sort of problem.

Luckily some other troubled souls realised this was a issue before I did and have created services to try and aggregate various cloud services:

  • Jolidrive – Jolicloud is the new computing platform built around your life in the cloud. Jolicloud is the home for your most precious content.
  • Kippt – Build your online library of amazing things.
  • IFTT – Put the Internet to work for you.
  • CloudKafe – Organize your cloud.
  • Cloud Magic – A better life with every search.

These services all take a slightly different approach to addressing the problem, and each is a step in the right direction. Sadly, my experiences so far have been that they essentially try and do the jobs of the services they are aggregating, just in a less intuitive way or with fewer options. They’re also all relatively young and don’t come without the confidence of a well established company such as Google. For many users it can therefore be a tough sell to learn a whole new way of accessing their content, only for that service to potentially disappear the next day. And then there are the obligatory privacy concerns in giving one company the keys to all of your online information…

At the moment the best solution I’ve found has been to carefully select and limit the number of cloud services I subscribe to. One simple and worthwhile aggregation is to choose one email provider (in my case Gmail) and use it to pull (via POP3), email from all your other email accounts. Then at least all your emails are in one place!

Meanwhile, my search for the perfect tool continues.

iTunes U – beware

I subscribed to iTunes Match when it was first released but it was only recently that I actually called on its backup functionality – and the results were a little unexpected.

As a quick summary for those unfamiliar with the service, iTunes U is Apple’s cloud music offering enabling users to have their music stored on Apple’s servers – backed up and accessible from anywhere. The main point of difference the service has with some of its competitors is that it will attempt to ‘match’ songs and just put a mark next to them song on Apple’s servers, acknowledging the user’s ownership (optimistically). This provides joint benefits for apple and the user:

  • Apple, in the best case need only store a single copy of each song, rather than a duplicate for every user.
  • There is essentially no upload when a song is matched, so user’s libraries can be backed up much more rapidly.

As a means of accessing songs on my iPad, the service works well. However, it was obviously no help for getting music onto my Android phone.

Recently, I wiped my computer whilst ‘upgrading’ the OS (for those contemplating the move from Windows 7 to 8, don’t!). Upon transferring music from my external hard drive back onto my laptop, I realised that I hadn’t backed up my last couple of month’s worth of new songs. No problem I thought, they’ll just download into iTunes once I’ve signed back into iTunes Match – and indeed they did.

However, they downloaded as AAC files (Apple’s proprietary music format), rather than the MP3 format of the original songs. Thinking about this, it makes sense, as the songs were ‘matched’ rather than uploaded but it is a side-affect I hadn’t considered. I had to convert all the songs back into MP3 format for compatibility with my Android phone (and consistency with the rest of my music library).

Whilst the service works as designed, and the matching performs well, the enforced ties to Apple’s devices and formats is a deal-breaker for me, and I’ve cancelled my iTunes Match subscription. Back to the manual external hard-drive backup for now!

Tech Environment

I’m sometimes asked which tools I use for a particular job, so here goes:

  • At home I work on a Dell XPS Laptop with a dual boot setup of Ubuntu 12.04 and Windows 8.1. When developing I usually hook-up a small external screen.
  • I use Eclipse, with the Aptana extension installed for Java and PHP programming. I like the fact that I can use the same editor on both Linux and Windows, and for C, Java, Python and Web development. I also use Microsoft Visual Studio for C#, and PyCharm for Python development. For smaller editing tasks I use Notepad++ on Windows and Vim on Ubuntu.
  • Chrome is my default browser (although I did use Firefox for a while). I especially like the developer tools.
  • I use Mercurial and Git, combined with Bitbucket and Github respectively, for version control. Currently I am most comfortable with Mercurial.
  • Just about everything that isn’t code related goes in my Dropbox folder, of which I’m a big fan.
  • I use cloud based tools, especially Google’s, for as much as possible. I like the portability they provide and in general trust ‘them’ to look after my data more than I trust myself!

Top 10 consumer software tools:

  1. Gmail
  2. Google Drive
  3. Dropbox
  4. LastPass
  5. Pocket (saving articles I come across on the web for reading on my iPad later)
  6. Evernote (longer notes, recipes etc.)
  7. Google Maps
  8. Feedly (RSS feeds)
  9. Goodreads
  10. Rotten Tomatoes

Other gear:

  • Google Nexus 4
  • Kindle Paperwhite
  • Panasonic Lumix FT4