When I wrote my initial blog about the Arduino-Pico library, fmilburn mentioned that Arduino had just released an Mbed library that had support for the RPi Pico https://github.com/arduino/ArduinoCore-mbed/releases/tag/2.0.0 . The github installation instructions require cloning the ArduinoCore-mbed repo and the ArduinoCore-API repo and then adding a symlink to that API. I had been wanting to use the Mbed RTOS functions with the Nano 33 BLE Sense board, but I guess I'll try it with the RPi Pico first.
Before I tried using the installation instructions, I decided to check the Boards Manager for Mbed supported boards.
And there is support the RPi Pico. I also noticed that the previous entry that was used for the Nano 33 BLESense (Arduino Mbed OS Boards) has been deprecated. So, I decided to do the install from the Boards Manager for the RPi Pico and the Nano 33 BLE Sense.
And it looks like it is the current version (2.0.0). I don't have a Portenta yet so I didn't install that.
Then I took a look at available examples and there were only a few.
I found it interesting because the first two examples use an onboard PDM microphone and onboard RGB led - neither of which exist on an RPi Pico .
It runs out that none of the examples will compile. The first two for obvious reasons and the third I think because there is a missing architecture definition (this was a red herring).
Then I noticed that all the Mbed supported boards have the same examples.
All 3 examples will compile for the Nano 33 BLE Sense. The first two examples were written for the Nano 33 BLE Sense and the third one was written for the Portenta. They need to provide new examples for the RPi Pico...
So, not being an Mbed programmer, I go looking for Arduino IDE Mbed examples. I find a few, but I run into problems with deprecated functions that no longer are available - simple things like wait() and wait_ms() - because the examples are too old. And I get tripped up trying to replace the functions because of namespace issues, i.e. knowing where functions exist. Just learning curve issues, but I realize I should verify I can do something simple and then build from there.
I go back to the basic Blinky example and blink the onboard green LED. Two seconds on, one second off. Even that confused me for a second because I thought that I needed to specify the arduino namespace in order to use the delay() function, but I didn't. LED1 is defined to be the onboard LED pin, but I haven't figured out where (not in pins_arduino.h).
RPi_Pico_Mbed_Blink.ino
#include <mbed.h> using namespace mbed; DigitalOut led(LED1); void setup(){ } void loop(){ led.write(1); // set LED1 HIGH delay(2000); led.write(0); // set LED1 LOW delay(1000); }
Then I modified a multi-threaded example to fade the onboard LED and an external Red LED on pin 22 using separate threads.
RPi_Pico_Mbed_2thread_LED_Fade.ino
/** * A basic example to show multithreading in mbed OS. * Two threads are created to fade the external red and onboard green LEDs. */ #include "rtos.h" #define RED 22 #define GREEN 25 #define MS(x) chrono::milliseconds(x) using namespace std; // we will be using std::chrono using namespace rtos; // we will be using rtos::ThisThread // Declaring the Thread objects Thread led_red_thread; Thread led_green_thread; void setup_leds() { pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); } // function to be attached to led_red_thread void led_red_function() { for (;;) { // delay while blue fades out and green fades in ThisThread::sleep_for(MS(512)); // Fade red out for(int i = 255; i >= 0; i--) { analogWrite(RED, 255 - i); ThisThread::sleep_for(MS(1)); } // delay while green fades out and blue fades in ThisThread::sleep_for(MS(512)); // Fade red back in for(int i = 05; i < 255; i++) { analogWrite(RED, 255 - i); ThisThread::sleep_for(MS(1)); } } } // function to be attached to led_green_thread void led_green_function() { for (;;) { // delay while blue fades out ThisThread::sleep_for(MS(256)); // fade green in for(int i = 0; i < 255; i++) { analogWrite(GREEN, 255 - i); ThisThread::sleep_for(MS(1)); } // delay while red fades out and blue fades in ThisThread::sleep_for(MS(512)); // fade green out for(int i = 255; i>= 0; i--) { analogWrite(GREEN, 255 - i); ThisThread::sleep_for(MS(1)); } // delay while red fades in ThisThread::sleep_for(MS(256)); } } int main() { setup_leds(); led_red_thread.start(led_red_function); // start red thread led_green_thread.start(led_green_function); // start green thread }
It seems the RPi Pico Mbed installation is okay, I just need to learn how to use it.
I think I'm finally getting my head wrapped around this. It seemed that I should be able to take an unmodified Arduino sketch that I had working with the Arduino-Pico library and compile it with the Mbed library and everything should still work. This is almost true. To test it out, I tried compiling the GFX demo for the OLED - and it compiled but there was no display.
I then tried the NeoPixel test and that worked - the same code actually seems to run faster with the Mbed library.
I pondered why the OLED test didn't work. I checked the pins_arduino.h file in the Mbed library and saw that the default Wire library I2C pins for SDA and SCL were GPIO 6 and 7. For the Arduino-Pico library they are GPIO 0 and 1. I moved the jumper wires over - and now it works!
So, I guess I just need to learn the Mbed functions, namespaces and pin assignments and I'll be able to use the RTOS and multi-threading with the RP2040 . It would be nice if there was a simple way to use both cores. I'm sure someone has figured that out.
I should also mention that using the Mbed library requires that that you do Reset-Boot sequence every time you upload. The Arduino-Pico library just required that you do it the first time and it handled it after that. It's nice to just hit the Upload arrow...
Top Comments