Microcontroller programming

2024-02-26

Nicolas De Coster & Henk Buursen

Choose microcontroller

Better "fit for purpose"

Consider:

  • processing power
  • memory
  • I/O capabilities
  • power consumption
  • existing libraries / support

"FabAcademy families"

AtTiny (412 / 1614 / 3216 / ...)

AtTiny microcontrollers are low-cost and low-power devices suitable for simple embedded applications with limited processing and I/O requirements.

Programming : UPDI (older : ISP)

AtTiny (412 / 1614 / 3216 / ...)

  • 8 bits - 1MHz-20Mhz (→ 32MHz overclock)
  • Versatile I/O (analog, pwm, i²c, UART, SPI, ...)
  • Single pin programming (UPDI)
  • Sandbox board : Adrianino

SAMD (11C, 11D, 21E, D51)

SAMD microcontrollers offer a balance of performance and power efficiency, making them suitable for a wide range of applications including IoT devices and wearables.

Programming : SWD / JTAG

SAMD (11C, 11D, 21E, D51)

  • 32 bits - 48MHz - Single ARM Cortex-M0+
  • Native USB support (+CDC +HID)
  • Versatile I/O (analog, pwm, i²c, UART, SPI, ...)
  • Sandbox board : Samdino

RP2040

The RP2040, developed by Raspberry Pi, is a dual-core ARM Cortex-M0+ microcontroller with versatile I/O capabilities (PIO!), making it ideal for projects requiring multitasking and connectivity.

Programming : UF2

RP2040

  • 32 bits - 133MHz (→ >250MHz overclocking)
  • Dual ARM Cortex-M0+
  • Hardcoded bootloader - XIAO module or RPi Zero
  • Sandbox board : FabXIAO

ESP32-C3/S3

ESP32-C3 and ESP32-S3 are developed by Espressif Systems, known for their built-in Wi-Fi and Bluetooth capabilities.
→ IoT projects requiring wireless connectivity

  • RISC-V - 160MHz
  • Wifi 4 - BlueTooth 5 (LE)
  • Versatile I/O
  • Sandbox board : FabXIAO

Others

STM32
PIC (Microchip)
MSP/TI
...

General rule : RTFM / RTFDatasheet

Always refer to the microcontroller's datasheet and reference manual for detailed specifications and usage instructions. These documents provide essential information for programming and configuring the microcontroller.

Choose language

Bootcamp instructors 2024 : different languages tests

Assembly / PIO / VHDL / Verilog

Assembly language provides direct control over the microcontroller's hardware, making it efficient but complex to write. Programmable I/O (PIO) offers a way to offload I/O operations from the CPU, enhancing performance in certain scenarios.

Learn binary/logic basics! (worth it!)

C and C++ are widely used for microcontroller programming due to their efficiency and low-level access to hardware. They are compiled languages, meaning the code is translated into machine language before execution.

Note : Arduino is C++ based

Rust is gaining popularity for microcontroller development due to its strong safety guarantees and modern features. Like C and C++, Rust code is compiled before execution.

E.g.Minecraft rendering on RP2040

MicroPython offers a higher level of abstraction compared to C and assembly, making it easier to write and understand code. It can be either interpreted directly or precompiled into bytecode for execution.

JavaScript can be used for microcontroller programming (Kaluma / Esrpuino). It offers a familiar syntax for web developers but typically requires more resources compared to lower-level languages like C.

TinyGo brings the Go language to microcontrollers.

  • Various microcontrollers (samd, rp2040, ...)
  • Includes optimized garbage collector
  • Comes with built-in libraries for GPIO, I2C, SPI, etc.
  • Supports cross-compilation for easy development
  • Drawback : not fully multi-core compatible (yet?)

Mixed languages

Mixing languages might give you the best of each world. E.g. : ASM → C → C++

Mixing microPython and PIO "assembly" on RP2040

Programming

Direct programming :
UPDI / ISP / SWD / JTAG

These are various methods for directly programming microcontrollers. They involve using specialized hardware interfaces to transfer compiled code into the microcontroller's memory.

Bootloader : small program stored memory that initialize the µC and load/rewrite code from a secondary storage device such as serial port (USB/UART), flash memory or EEPROM.

Hardcoded bootloader (UF2)

Hardcoded bootloaders like UF2 provide a convenient way to load firmware onto a microcontroller without requiring a separate programming tool. They are often used in development boards and single-board computers. (e.g. : XIAO2040)

One programmer to rule them all

Quentorres

Basic board for using Alex Tardov's Free DAP

Arduino (C/C++)

Know the differences:

Understanding the distinction between Arduino as a hardware platform, the Arduino libraries, and the Arduino IDE is essential for effective microcontroller programming with Arduino-compatible boards.

Arduino Uno = board = hardware

The Arduino Uno refers to a specific hardware board featuring an Atmel ATmega328P microcontroller. It is (was?) one of the most commonly used Arduino boards for prototyping and educational purposes.

Arduino Uno R3 board (quite outdated)

Arduino is a set of libraries

Arduino provides a rich set of libraries that simplify common tasks such as reading analog inputs, controlling digital outputs, and communicating with peripherals like sensors and displays.

  • Let you write code without knowing all the registers details and in a portable (various chips) way.
  • Generated code usually (much) less efficient.

"Pure C" VS Arduino

→ add them to arduino preferences : additional boards manager


Arduino IDE is a development environment

The Arduino Integrated Development Environment (IDE) is a software tool used for writing, compiling, and uploading code to Arduino boards. It includes features like syntax highlighting, serial monitor, and library management.

Be aware :

  • IDE v2 has a better editor BUT has compatibility issues with some libraries (AtTiny port!)
  • IDE v1.9 and above works fine but less user friendly (use external editor... [preferences > ])

Arduino IDE 2

Good practice

Document your code

Every programming language contains special section of "commented" code.

Fit-for-purpose processor

Selecting a microcontroller with appropriate processing power for your application can help conserve energy and reduce cost. Underclocking, or running the microcontroller at a lower frequency, may be beneficial for power-sensitive projects.

Underclocking may be as usefull as overclocking!

Write modular and readable

Breaking down your code into modular components enhances readability and maintainability. Organize your code into separate functions or modules based on their functionality to facilitate debugging and future updates.

Flat files are difficult to modify / maintain.

Modular writing

Write "parametric"

const / #DEFINE
Avoid constants/values directly into your code
→ use symbolic names or macros defined with const variables or #define directives.

This improves code clarity and allows for easier modifications in the future.

Parametric : Example

Parametric : Example

Use Classes

Class:

  • Allows you to "think as objects"
  • Is called "object oriented" (C++ vs C)
  • Widely used by Arduino hardware specific libraries

Classes

Optimizations

Some tips :

  • lookup tables (vs complex maths)
  • inline
  • pointers
  • "simple" arithmetics : 2n divisions = >>
  • Proper type : uint8_t, int16_t, etc. (pecularly on 8bits proc)
  • ...

Credits

  • Comics : XKCD
  • Beautiful microprocessors B&W : pinouts.org
  • Fragments of codes : some AI like chatGPT and Neil

Time for debugging

Attitude

Write down what you do

The Pragmatic Programmer

Talk to me

Narrowing down the problem

  • Reproduce the problem
  • Reproduce the problem
  • Always first do visual check
  • Reproduce the problem
  • Always first do visual check
    1. traces
      soldering
      components
  • Reproduce the problem
  • Always first do visual check
    1. traces
      soldering
      components
  • Keep in mind that the problem
    might not be visible without a microscope

Multimeter

Continuity

  • Test power supply to power pins continuity
  • Check fuses, diodes orientations, ...
  • Check short-circuits
  • Check traces
  • Parallel measurement => check your cables!
  • ! absolute maximum ratings (may burn your component)
  • Power supply impedence / max power, badly selected
  • GND/VCC loop

Continuity

Voltage drop

Logic Analyzer

Logic Analayzer

Miniware LA104

LA104 and alternative firmware and apps

Oscilloscope

Radio communications

RTL-SDR RTL2832U DVB-T Tuner Dongles

Flashing your board

and what can go wrong

USB

SWD/JTag

UPDI

Unified Program and Debug Interface

Serial (or any) "Hello world!"


#define DEBUG_MODE 1
	...
#if DEBUG_MODE
	 Serial.print("DEBUG -- MyVal value = ");
	Serial.println(MyVal, DEC);
#endif

Read The F*cking Manual