6. Embedded Programming¶
Group assignment: compare the performance and development workflows for other architectures
ATMega328P vs STM32 comparison¶
In order to compare these two microcontrollers, we refered to their datasheets: ATtiny1614 and XIAO RP2040
Feature | XIAO RP2040 | ATtiny1614 |
---|---|---|
Register size | 32-bits | 8-bits |
Operating conditions | 3.3 - 5. V | 1.8 - 5.5 V |
Flash memory | 2 mB | 16 kB |
SRAM | 264 kB | 2 kB |
Clock speed | 133 MHz | 20 MHz |
We noticed that the XIAO RP2040 is significantly more powerful than the ATtiny1614. We then compared their pins configurations:
For the XIAO RP2040:
And for the Attiny1614:
Comparison of RP2040 for C++ and Micropython¶
This performance comparison involves calculating the equation “5 + 10 / n” 100,000 times on the same microcontroller using both C++ and MicroPython languages. Since MicroPython is a higher-level language, it is expected that the machine will require more time to process the code compared to C++. This difference in processing time was indeed observed and acknowledged in the results.
MicroPython
import time
while True:
start = time.ticks_us()
result = 0
for n in range(1, 100000):
result += (5 + 10) / n
print(result)
print(time.ticks_diff(time.ticks_us(), start))
С++
void setup() {
Serial.begin(9600);
}
void loop() {
long startTime = micros();
double result = 0;
for (int n = 1; n <= 100000; n++) {
result += (5.0 + 10.0) / n;
}
long endTime = micros() - startTime;
Serial.println(result);
Serial.println(endTime);
delay(1000); // Adjust delay as needed
}
unsigned long startTime = micros();
// Your code to be timed
unsigned long endTime = micros();
unsigned long elapsedTime = endTime - startTime;
microcontroller | language | calculation time |
---|---|---|
XIAO RP2040 | C++ | 0.028416 second |
XIAO RP2040 | C++ | 2.250147 seconds |
Result: C++ is faster for processing about 100 times.
Comparison of RP2040 and ATtiny1614¶
This comparison is on performance for equation calculation 5 + 10 / n for 1000 times.
ATtiny1614
XIAO RP2040
The following code was used for the test.
And here we got the following results:
microcontroller | language | calculation time |
---|---|---|
XIAO RP2040 | C++ | 0.002856 second |
ATtiny1614 | C++ | 0.033362 seconds |
Result: RP2040 is more than 11 times powerfull than ATtiny1614.
Comparison of high level language and low level language for programming¶
This time we tested different level of the same language both on RP2040. The test aimed at understanding the speed difference between a program using the Arduino environment (pinMode(), digitalWrite(), etc) and a program written “bare metal”, “talking” directly to the registers.
Bare metal version of the code¶
Here is the code for the “bare metal” version:
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;
}
Then we connected it to an oscilloscope and saw the result at 7.81258 MHz:
Arduino IDE code¶
For the Arduino IDE development environment, we wrote the following code:
void setup() {
// put your setup code here, to run once:
pinMode(26,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(26, HIGH);
digitalWrite(26,LOW);
}
After connecting to the oscilloscope, we saw the result at a frequency of 590.590 KHz:
Results:
microcontroller | language | calculation time |
---|---|---|
XIAO RP2040 | “bare metal” | 7.81258 MHz |
XIAO RP2040 | arduino | 590.590 kHz |
The result: code written on bare metal is more than 13 times faster than the Arduino IDE.