Week 4

☆☆☆ 04 — Embedded Programming ☆☆☆

Asignment

  • Group assignment:
    • Demonstrate and compare the toolchains and development workflows for available embedded architectures
    • Document your work to the group work page and reflect on your individual page what you learned

Outline

  • 00— Defining Terms
  • 01— MCU Families
  • 02— Toolchains
  • 03— Development Workflow
  • 04— Quick Overview of MCU Families and Asociated Dev. Workflows
  • 05— MCU Workflow

00—Defining Terms

At first, we were a bit startled with the definition of the group assignmnent. We were not sure what a toolchain was or if we had to talk about software, hardware or simply focus on how to program different MCUs

01—MCU Families

Section Summary: Now that we have defined the concept of a toolchain lets look at the different MCU and make some important distinctions. I our class with Josep Marti we went over several MCUs including the: ATtiny412, ESP32, SAMD21 and the RP2040.

Family vs Variant

The MCU family denotes the architecture of the MCU whereas the varient denotes its particular capabilities. These series can often include additional feature like better wifi antenaes or specific bluetooth capbilities. Beyond this there can be additional varients within the series like in our case the Barduino which uses an ESP32 S3 Wroom-1 as it MCU.

Dev Board

A dev board is your PCB with all peripherael components need to power the MCU along with any other aditional feature that enable you to flash on board memory or carry out whatever your specific development needs.

02—Toolchains

So to answer the question, we decided to define our terms. First, a toolchain is the set of software tools used to develop, compile, and deploy the code onto a microcontroller. We can think of the toolchain in a sense as tools needed to setup the chip so it can be used by the MCU, for example in different functions such as, flashing a light, ringing a buzzer, or outputting data reading based on a sensor.

The toolchain typically includes:

  1. Compiler – This translates the computer codes such as C, C++, and Python into instructions that the Microcontroller can execute.
  2. Linker – Combines different pieces such as data, libraries, and code files into a single program file.
  3. Debugger – Helps identify and fix issues in the code by inspecting what’s happening inside the Microcontroller.
  4. Flashing Tool – Once your program is compiled and linked, the flashing tool uploads the compiled program into the microcontroller’s memory. Some chips will need an intermediate flashing tool to acquire their programs while others can be connected directly to our laptop. In class, for instance, we used a SAMD to program an ATTiny while our ESP32 could be connected straight away to the Arduino IDE.

Common MCU Families and Variants

  • ESP32 ****(ESP32-S2, ESP32-S3, ESP32-C3, ESP32-C6 etc.)
  • SAMD21 (SAMD21G18, SAMD21E18, SAMD21J18 etc.)
  • tinyAVR 1- Series (ATtiny202, ATtiny402, ATtiny804, ATtiny1604,)
  • RP series (RP2040)

03—Development Workflow

A development workflow refers to the step-by-step process of writing, compiling, uploading, and debugging firmware on a microcontroller. It includes:

  1. IDE (Integrated Development Environment) this is your swiss army knife for software development on your board and in the case of envirments Arduino IDE, PlatformIO, Atmel Studio (atmel chip specific) you have all of the features listed below as one integrated package. It is important to not however that additional software might be needed outside of your IDE to do things like flashing more notes on this are listed below.
    1. Code Writing – Using a programming language (C, C++, Python, etc.).
    2. Compiling – Converting the code into a binary file the MCU can understand.
    3. Flashing – Uploading the compiled code onto the MCU using a USB, UPDI, or other flashing methods.
    4. Debugging – Running the program, monitoring its behavior, and fixing issues if needed.

Some key differences when developing with different MCUs When working with a bare MCU it will need to be flashed with a bootloader. This , it is important to mention that the ESP32 v3 and SAMD21 have a built in USB to serial interface and that makes it easier to flash code. ATtiny412 requires an external UPDI programmer which adds complication to the process of uploading code, and the XIAO RP2040 has a small step to flashing code as one needs to drag and drop a file to flash. Equally in terms of debugging tools, the ESP32 and SAMD21 have the most robust debugging. The ATtiny and XIAO RP2040 has more limited debugging. In the XIAO’s case, it is a bit of a blackbox, the main disadvantage of its great compactness.

*For more detail we asked Chatgpt to give us some more detail on the toolchain of each of the MCU’s which can be seen in Appendix 1.

04—Quick Overview of MCU Families and Asociated Dev. Workflows


ATtiny412

Toolchain Options

  • Arduino IDE – Supports ATtiny through the use of specific cores and libraries.
  • AVR-GCC – For more advanced, low-level programming and control.
  • PlatformIO – A more flexible, alternative platform for development.

  • Highlights:

    • Tiny size and power efficiency: Perfect for compact and battery-powered projects.
    • Burning the Bootloader: The ATtiny412 requires a bootloader for easy flashing, which can be done with a UPDI programmer.
    • Ideal for simple applications: Great for projects that need low power consumption and minimal processing, such as small sensors or basic controllers.

ESP32


  • Toolchain Options:
    • ESP-IDF (the official SDK using Xtensa GCC) – Offers deep control over the chip.
    • Arduino IDE & PlatformIO – Simpler alternatives if you prefer an easier interface.
  • Highlights:
    • Built-in USB-to-serial converter makes flashing and debugging very easy.
    • Excellent for wireless projects (Wi-Fi and Bluetooth) with a strong community and lots of resources.

SAMD21

  • Toolchain Options:
    • Arduino IDE & PlatformIO – User-friendly choices.
    • Atmel Studio – For more advanced development if needed.
  • Highlights:
    • Uses the reliable ARM GCC compiler.
    • Features native USB support, making programming as simple as plugging it in.
    • Good for projects that rely on USB connectivity, with solid debugging tools.

XIAO RP2040

  • Toolchain Options:
    • Arduino IDE & PlatformIO – For traditional C/C++ coding.
    • MicroPython & CircuitPython – Great if you prefer coding in Python.
  • Highlights:
    • Extremely beginner-friendly with its UF2 drag-and-drop bootloader (just copy your firmware file to flash it!).
    • Supports both C++ and Python, making it versatile for various skill levels.
    • Best for quick prototyping, though its debugging options are simpler than those of the ESP32.

05—MCU Workflow: SAMD11

Flashing USD drive to

To start off the learning experience to program the SAMD11 micro-controllers, Adrian Torres from the 2020 fab academy has created an exhaustive guide for samdino and adraindino (darduino).

https://fabacademy.org/2020/labs/leon/students/adrian-torres/samdino.html

Connection using ARDUINO app

First step is to open Arduino IDE 2.3.4 app and install the board manager to arduino app by adding the following URL to the additional boards manager URLs, found in : files —>preferences: https://descartes.net/package_drazzy.com_index.json

Figure 1: add the additional board json files necessary for the ATTINY and SAMD microcontrollers

Figure 1: add the additional board json files necessary for the ATTINY and SAMD microcontrollers

UPDI programmer : programmer is required to convert and upload code from computer to the board. It works as the translator between the code/developer and the board, select the option :

Arduino App

Connect board to programmer to computer.

Select board to program

Select Program

Select bootloader to connect SAMD

Connecting and coding directly to board using EDGB.exe

Program board (SAMD) connected to computer. Need to connect programmer to SAMD11 to flash the USB bootloader so it can understand USB to be programmed.

PS C:\USER\..| .\edbg-windows-r65.exe --help

CMSIS-DAP SWD programmer. Built May  8 2024 19:58:14.

Usage: C:\Users\bedoi\OneDrive\Documents\fablab\03 - Computer Controlled Cutting\group assignment\edbg-windows-r65.exe [options]
Options:
  -h, --help                 print this help message and exit
  -b, --verbose              print verbose messages
  -d, --version <version>    use a specified CMSIS-DAP version (default is best available)
  -x, --reset <duration>     assert the reset pin before any other operation (duration in ms)
  -e, --erase                perform a chip erase before programming
  -p, --program              program the chip
  -v, --verify               verify memory
  -k, --lock                 lock the chip (set security bit)
  -u, --unlock               unlock the chip (forces chip erase in most cases)
  -r, --read                 read the whole content of the chip flash
  -f, --file <file>          binary file to be programmed or verified; also read output file name
  -t, --target <name>        specify a target type (use '-t list' for a list of supported target types)
  -l, --list                 list all available debuggers
  -s, --serial <number>      use a debugger with a specified serial number or index in the list
  -c, --clock <freq>         interface clock frequency in kHz (default 16000)
  -o, --offset <offset>      offset for the operation
  -z, --size <size>          size for the operation
  -F, --fuse <options>       operations on the fuses (use '-F help' for details)

When loading up a new board to be programmed the first thing to do is erase the contents, program and verify:

edbg -ebpv -t samd11 -f sam_ba_SAMD11C14A.bin

Programming the microcontroller

Once the microcontroller is setup and connected to the computer it is time to program it to perform a desired task. The example below is just a embedding a simple code to the SAMD11 in order to make the LED blink.

First the board is compared to the datasheet linked below, to determine which pin is connected to the LED:

https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/Atmel-42363-SAM-D11_Datasheet.pdf

Comparing the datasheet and the board using figures 2 and 3 below, it is determined that the LED is connected to PA04.

Figure 2: extract from page 8 of the SAMD11 data sheet

Figure 2: extract from page 8 of the SAMD11 data sheet

Figure 3:  SAMD11 (copper colour) being flashed (right) with the programmer (black).

Figure 3: SAMD11 (copper colour) being flashed (right) with the programmer (black).

Using the simple example code for “Blink” in the Arduino app, the following code is embedded on to the code; first verified then uploaded.

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

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

The result is a blinking light on the board.


ATtiny412

Flashing USB Drive to ATtiny412

To get started with programming the ATtiny412, the first step is to burn the bootloader to the microcontroller. This allows you to upload code directly to the board. The bootloader can be burned using a UPDI (Unified Program and Debug Interface) programmer. The guide below outlines the steps required to get your ATtiny412 up and running.

Connection using Arduino IDE

  1. Open the Arduino IDE and install the ATtiny core by adding the following URL in Preferences > Additional Boards Manager URLs:

    http://drazzy.com/package_drazzy.com_index.json

    This will allow the Arduino IDE to recognize ATtiny microcontrollers.

  2. Connect your ATtiny412 to the UPDI programmer and plug the programmer into your computer.

  3. In the Arduino IDE, go to Tools > Board, and select the appropriate ATtiny model (e.g., ATtiny412). Then, go to Tools > Programmer and select the UPDI programmer.
  4. To burn the bootloader, go to Tools > Burn Bootloader. This will set up your ATtiny412 so you can upload code.

Flashing the Bootloader via UPDI

  • Connect the UPDI programmer to the ATtiny412 and your computer.
  • Go to Tools > Burn Bootloader in the Arduino IDE to burn the bootloader onto the ATtiny412.

Once the bootloader is burned, your ATtiny412 is ready to be programmed.

Programming the ATtiny412

Once the bootloader is burned, it’s time to upload a program to the ATtiny412. You can use the Arduino IDE to write and upload simple code. Here's an example to blink an LED:

  1. Set up the LED: According to the ATtiny412 datasheet, we can use PB2 as the output pin for the LED.
  2. Code for Blinking LED: The following simple code will blink an LED on pin PB2.

void setup() {
  pinMode(2, OUTPUT);  // Set PB2 as an output pin
}

void loop() {
  digitalWrite(2, HIGH);   // Turn the LED on
  delay(1000);              // Wait for 1 second
  digitalWrite(2, LOW);    // Turn the LED off
  delay(1000);              // Wait for 1 second
}

Uploading the Code

  1. Verify the code in the Arduino IDE.
  2. Click on the Upload button to transfer the code to the ATtiny412 via the UPDI programmer.

Once the code is uploaded, the ATtiny412 should blink the LED connected to pin PB2, indicating successful programming.

ATtiny412 Datasheet

For reference, check the ATtiny412 datasheet to ensure correct pinout and electrical specifications:

ATtiny412 Datasheet

Citations

ACTIONS FOR WEDNESDAY

  • TEST TWO BOARDS ESP32 & another one (found in the protolab) & with images/video document the process.
  • Explain and document the dev workflows for each (definition is alreday fdone).