4. Embedded Programming¶
This week, we studied embedded programming for microcontrollers. It was challenging for me because I wasn’t sure where to begin, and it was my first time learning about programming in general.
Group assignment¶
For the group assignment, we compared different programming languages for a microcontroller. We needed to write one program for RP2040, in two different programming languages and compare the execution speed of each.
First, I downloaded and installed the Arduino IDE. Since the RP2040 is not included in the Arduino IDE’s default list of supported boards, we must manually add it. To do this, I clicked “File” and then “Settings”. In the “Additional boards manager URLs” field, I inserted the following link.
https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
After that, I need to find the XIAO RP2040 board in the boards manager and install it.
Speed Test¶
We used an oscilloscope to measure the speed of each code’s execution.
Mkhitar wrote this code using micropython.
led = machine.Pin(26, machine.Pin.OUT)
while True:
led.high()
led.low()
Following Maxim’s instructions, I created a simple code for c++ with Arduino Framework
void setup() {
pinMode(1, OUTPUT);
}
void loop() {
digitalWrite(1, HIGH);
digitalWrite(1, LOW);
}
And bare metal version, for register-level interaction.
int main() {
/*
0x400140d4: GPIO26_CTRL: GPIO 26 control including function select and overrides.
0x05: from "2.19.2. Function Select": selects function SIO (Single-Cycle IO)
*/
// same for GPIO26_CTRL
*((volatile unsigned int*) 0x400140d4) = 0x05;
/*
Output enable registers, GPIO_OE and GPIO_HI_OE, are used to enable the output driver. 0 for high-impedance, 1
for drive high/low based on GPIO_OUT and GPIO_HI_OUT.
sets the 27th bit of this register to 1 in order to enable output
*/
*((volatile unsigned int*) 0xd0000020) = (1 << 26); // GPIO_OE
while( 1 )
{
//toggles the pin 26 high and low
*((volatile unsigned int*) 0xd000001c) ^= ( 1 << 26);
}
return 0;
}
Test Results¶
The closer the code is to the hardware, the faster the microcontroller will work.
- MicroPython is the slowest, as the code is executed by an interpreter, rather than directly.
- C++ with Arduino is faster, but it still has intermediate layers (libraries) that add some delay.
- Pure C++ on registers is as fast as it can be, as commands go directly to the processor with no unnecessary delays.
In conclusion, MicroPython is convenient, Arduino is a good middle ground, and registers provide maximum speed.
microcontroller | language | oscillation frequency |
---|---|---|
XIAO RP2040 | MicroPython | 82.8 kHz |
XIAO RP2040 | c++ with Arduino Framework | 618.2 kHz |
XIAO RP2040 | register level c++ | 12.5 MHz |
Individual assignment¶
#define PUMP_PIN 1
#define LED_PIN 13
void setup() {
pinMode(PUMP_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
digitalWrite(PUMP_PIN, LOW);
digitalWrite(LED_PIN, LOW);
}
void loop() {
Serial.println("Turning on the pump...");
digitalWrite(PUMP_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
Serial.println("The pump is on!");
delay(3000);
Serial.println("Turning off the pump...");
digitalWrite(PUMP_PIN, LOW);
digitalWrite(LED_PIN, LOW);
Serial.println("The pump is off!");
delay(3000);
}