advanced embedded programming

this week i learned how to setup openocd and gdb in my mac, make my own eagle footprint, burn an arduino compatible bootloader with openocd into an atsamd11, setup the arduino ide, and debug the code inside the samd11 with openocd and gdb.

openocd & gdb setup

i followed erik strands instructions on how to install openocd and gdb. since i was only planning on using the atsamd11 and not the atsamd51, i just needed version 0.10.0, so 'brew install opencd' worked perfectly. if i needed to program an atsamd51 i would've had to build from source to get a later version.

to install gdb, i used homebrew as well 'brew install gdb'.

designing, milling board and creating my own footprint on eagle

i used neil's atsamd11 hello.d11.usb board as a reference. i avoided using the zero µF by using vias instead. i won't claim smart design but instead lazy design with my prefered usaged of auto routing in eagle. however, this task wasn't trivial becasue i needed a 2x5 header that didn't exist in the fablab parts library. all of them had the pins that were 1mm apart and they needed to be .5mm apart. infact i didn’t even notice this until my second attempt at milling the part. i then spent too much time looking online for the part, and i even milled a third board before realizing that the internet had lied to me. it wasn't until the fourth mill, after i had designed my own footprint. that it worked.

making a footprint in eagle

  1. open eagle
  2. click the libraries drop down arrow
  3. pick a library, i chose parts_fablab.lbr
  4. right click and click open
  5. in the top menu, click device and under "new device name" type thew new device name
  6. in the bottom right of the new window that pops up, click new and "create new with package generator"
  7. there were only two types of male pin headers, straight and right angle, and none fit the type of pin header we need to make. instead i click on “SOIC, SOP”
  8. for this footprint, we don't care about the 3d model this will generate, so click options in the top right and unselect "show 3d model"
  9. this link takes you to the part in digikey and there you can the header pin's schematic drawing
  10. fill in the measurements of L, D, E, b, e and E1. ignored A and A1 because you only cared about the footprint and it doesn’t ask about D2 or E2. use the exact measurements given in the drawing’s “recommended pub layout” for the min and max values, so if the drawings said 2.4mm for L, then that’s what you put for the min and max. then put 0mm for both the fabrication and placement tolerances.
  11. click finish button in the top left
  12. once generated, you’ll see the footprint and a 3d rendering of a funny looking chip. don’t worry about the 3D model, you’ll only need the schematic and the footprint.
  13. click the devices button in the left panel to add an existing device and choose CONN_05X2
  14. you then label the pads by right clicking on the package/variant window and clicking edit footprint
  15. then click the connect button in the bottom right to connect the footprint and schematic pins and your window should end up looking something like this
  16. click save and you can now use this in your board and schematic layouts!

here's my final board layout:

here's my first milled board:

burning arduino bootloader using openocd

to get the right files, i created a folder that contained the openocd.cfg file and the binary file to bootload.

with the board milled and soldered, i plugged in the my board into my copmuter, plugged in an amtel-ice debugger into my computer and then connected the two through the 10 pin header. check to see that both the red and green led lights are lit up in the amtel-ice debugger and then cd to the folder that contains the .cfg and binary file and type 'openocd -f openocd.cfg'. If successfull, you'll get this output:

setting up arduino ide

once the atsamd11 has been bootloaded, you should be able to program it through the arduino ide. there are a couple of steps you need to first take:

  1. go to arduino > preferences, and click on the button next to 'Additional Boards Manager URLs'
  2. add 'https://www.mattairtech.com/software/arduino/package_MattairTech_index.json'
  3. go to tools > board > boards manager
  4. search for "Mattair", and install the 'MattairTech SAM D|L|C Core for Arduino' package
  5. go to tools > board and select 'GenericD11C14A' under the recently installed package
  6. go to tools > port, you should see your board and select it

write a simple program and test that it works!

debugging... the circuit board?

so it turned out that i was having problems with my first board where i was able to bootload and program once, but wasn't able to replicate it. i checked every connection with the voltmeter and even soldered usb wires onto the the board to ensure that it wasn't a problem with the usb connection itself.

alas, that didn't work and after going back and forth with erik (he checked my connections, cleaned the board with isopropenol) i decided to just re-mill the board from scratch. instead of adding solder to the usb connection or even soldering the usb wires onto the board itself, i decided to add 3 layers of electrical tape.

debugging the code

one of the main reasons that i wanted to do advanced embedded programming was so that i could learn how to do live debugging of the code in a chip, once i got a board that i was able to bootload and program consistently, i finally had my chance.

i thought i could use the first openocd config file but it turned out that i needed a more minial config file that didn't have any resets or halts, so i made a new one openocd_debug.cfg. i also needed to link gdb with the program's corresponding .elf file. to get it from arduino i had to go to file > preferences and select 'Show verbose output during compilation'. then when you build or deploy the code, the arduino ide will display the location of the .elf file. you'll only need the file name as gdb is smart enough to know where it is.

once you have the correct config file and the elf

  1. make sure your board is connected to the amtel-ice debugger
  2. open up a terminal window and enter 'openocd -f openocd_debug.cfg' to launch the openocd server
  3. open up another terminal window and enter 'gdb <filename>.elf' and the gdb cli should appear
  4. in the gdb cli, type ‘tar ext :3333’ to connect with the openocd server
  5. type continue to let openocd know the program is running (it gives a warning about it already running but ignore it) normally we can use ‘monitor reset’ but it wasn’t working for us at the moment, so the continue is a workaround
  6. then press ctrl-c to pass the execution of the program and you should be ready to debug!

here are some handy commands and what they do: