htmaa '22, by jakin

week 7: embedded programming

Prior Experience: 0/5

I have never done CNC machining, and I was not previously familiar with feeds and speeds and CAM and so on. I am a little afraid of the CAD/design portion, and quite afraid of having to figure out CAM, and not that afraid of actually sanding/assembling the pieces.

The assignment for this week is

  • individual assignment:
    • browse through the data sheet for your microcontroller
    • use your programmer to program your board to do something
    • extra credit: try other programming languages and development environments
  • group assignment:
    • compare the performance and development workflows for other architectures

For this week, it seems that the most efficient way to program the board will be to use Arduino. I am following this guide: https://mtm.cba.mit.edu/2021/2021-10_microcontroller-primer/fab-arduino/

Bootloading the PCB

Here is my board from the electronics design week:

For bootloading previously, I used the computer in the archshops, but for this week I wanted to set it up on my own computer so I could do it from home/without help if necessary.

I decided to use pyOCD since it seems like edbg isn't good for use with Mac. Here is the guide I am following: https://mtm.cba.mit.edu/2021/2021-10_microcontroller-primer/pyOCD/. First, I installed pyOCD so I could bootload the board from my computer.

python3 -m pip install -U pyocd

Then, I need to download the pack for the specific microcontroller I am using, which is the SAMD11C. I went to https://packs.download.microchip.com/, ctrl-f ed for atsamd11C and downloaded the relevant pack.

In terminal, I navigated to where I downloaded the folder to, renamed it to .pack instead of .atpack, and added a pyocd.yaml file containing just this. This configures the settings for pyOCD so that it knows to open this pack.

pack:
    - Microchip.SAMD11_DFP.2.6.76.pack

I checked that it downloaded properly by checking the version:

I also downloaded the programmer binary from https://github.com/qbolsee/ArduinoCore-fab-sam/blob/master/bootloaders/zero/binaries/sam_ba_SAMD11C14A.bin and moved it to the working directory. Then I ran pyocd list --targets to check the list of potential targets. (My target is atsamd11c14a since I am using the SAMD11C microcontroller.)

Then I plugged in the ATMEL programmer to the USB port of my computer and I plugged in the other end to the 10 pin connector on my board.

This is the command to bootload the board:

pyocd flash -t atsamd11c14a sam_ba_SAMD11C14A.bin

I kept getting this error:

First, I realized that I had to actually plug in the USB connection of the board, so that it has power, as well as using the 10 pin connector. Demircan helped me to peel off the top layer of copper with tweezers so that the USB connection doesn't short.

It still didn't work, so I will have to take a look at this later.

I ended up using Demircan's programmer, and he used edbg on his computer, and the bootloading worked!

The programming/verification message:

Setting up Arduino

Next, still following the CBA tutorial, I have to set up Arduino so it can actually recognize my board/load the code into my microcontroller.

I went to file -> preferences -> additional board manager URLS and added this link: https://raw.githubusercontent.com/qbolsee/ArduinoCore-fab-sam/master/json/package_Fab_SAM_index.json.

Then, I went to tools -> board -> board manager, searched for Fab SAM core for Arduino, and hit install.

This means that Arduino now knows how to load the Arduino code into my microcontroller (SAMD11C).

Unfortunately, I still cannot find the board in the port options when I plug it in. (It turns out that I was just plugging it in not all the way because of the dongle I use for my computer.)

I tried uploading an empty sketch to all three available ports, but none of them worked. I tried resoldering some of the joints but it didn't help. I also added a shim under the USB connection.

I was afraid that the programmer actually didn't work. Chi-li, who was in the archshops, let me use his computer/programmer to re-program my board and then upload a simple Arduino sketch, which worked! I realized later that my issue was literally just that I didn't plug in the board the entire way (I thought it was in the whole way because when I listed USB connections in Terminal, it changed when my board was/wasn't plugged in, but I think something was just wrong and I will not worry about it.)

Programming the Board

Referencing this: https://github.com/qbolsee/ArduinoCore-fab-sam/tree/master/variants/Generic_D11C14A, I learned that the pin number in Arduino can just be the same as the "PA" pin number, except without the PA.

"Most pins can be used for more than one function. When using PIN_MAP_STANDARD, the port pin number printed on the chip above is also used in Arduino (but without the 'A') for all of the supported functions (ie: digitalRead(), analogRead(), analogWrite(), etc.)."

Here's the Eagle schematic of the microcontroller:

From looking at this, I see that the LED is connected to PA05, so I should use pin number 5, and the pullup resistor for the switch is connected to PA14, so I should use 14 for the pin number. I was a little confused because I wasn't sure if I was supposed to use 1 and 4 instead, but I turned out to be right.

First, I loaded in a simple Blink sketch from Arduino (https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink), changing the led pin to 5 (still on Chi-li's computer):

int LED_PIN = 5;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_PIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_PIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_PIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

It works! The LED blinks on and off every 1000 milliseconds (or whatever units the time is in).

Then I loaded in code to make the LED turn on and off when the button is clicked. It also works! (Although the button is a little finicky.)

This is when I realized that my dongle was making it so that I couldn't stick in the USB the entire way, which is why I was having issues with Arduino (I am dumb). Next time, I will remember to make the USB stick out a little more in the outline.

I ended up doing a pretty simple program for this week, but it's easy to see how I would do much more complicated things, either with just the LED and the button, or by adding more components into the other pins of the microcontroller. I feel powerful >:) mwahahahaha (even though I basically didn't do anything I feel like I could do lots of things, assuming I can figure it out)