Week 04. embedded programming

Assignments for this week

Group assignment

  • compare the performance and development workflows for other architectures

Individual assignment

  • browse through the data sheet for your microcontroller
  • program a microcontroller development board to interact and communicate
    • extra credit: use different languages &/or development environments

What I've done this week

  1. Browse through Datasheet
    • RP2040
  2. Program a microcontroller board
  3. Compare several arcchitectures

1. Browse through Datasheet

RP2040

I skimmed through the RP2040 datasheet and seeed studio. Here are some information I picked up:

  • Dual ARM Cortex-M0+ @133MHz
  • 264kB on-chip SRAM in six independent banks
  • Support for up to 16MB of off-chip Flash memory via QSPI bus
  • DMA controller
  • Fully-connected AHB crossbar
  • Interpolator and integer divider peripherals
  • On-chip PLLs to generate USB and core clocks
  • 30 GPIO pins, 4 of which can be used as analogue inputs
  • Peripherals
    • 2 UARTs
    • 2 SPI controllers
    • 2 I2C controllers
    • 16 PWM channels
    • USB 1.1 controller and PHY, with host and device support
    • 8 PIO state machines

Hardware overview

RP2040 pins

There are several LEDs embeded:

  • POWER LED
  • USER LED (red, blue and green)
  • RGB LED

Layout of PINs are as followed:

RP2040 pins

RP2040 has a default program

2. Program a microcontroller board

Check initial state

Plugging RP2040 to a power source, the default program starts like this:

Testing an example

Step 1. Open Arduino program

Step 2. In Settings, add the URL of the additional board you can find here. I did that by writing the following line in Additional Boards Manager URLs:

https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

additional board manager

Step 3. From Board Maneger, download Raspberry Pi Pico/RP2040.

board manager

Step 4. Configure the Arduino IDE for Seeed XIAO RP2040.

Step 5. Open a example. "File/Examples/01.Basics/Blink"

example blink

Step 6. Click the upload button.

The blink example was successfully uploaded. The RED user LED is blinking while the BLUE and GREEN is always ON.

Editing a program

I followed "blink-echo.ino" in Neil's documentation.

//
// hello.RP204-XIAO.blink-echo.ino
//
// Seeed XIAO RP2040 blink and echo hello-world
//
// Neil Gershenfeld 2/12/23
//
// This work may be reproduced, modified, distributed,
// performed, and displayed for any purpose, but must
// acknowledge this project. Copyright is retained and
// must be preserved. The work is provided as is; no
// warranty is provided, and users accept all liability.
//
#include <Adafruit_NeoPixel.h>
//
// globals
//
#define numpixels 1
#define pixelpower 11
#define pixelpin 12
#define bufsize 25
char buf[bufsize];
int count=0;
//
// setup
//
Adafruit_NeoPixel pixel(numpixels,pixelpin,NEO_GRB+NEO_KHZ800);
void setup() {
   Serial.begin();
   pixel.begin();
   pinMode(pixelpower,OUTPUT);
   digitalWrite(pixelpower,HIGH);
   }
//
// main loop
//
void loop() {
   char chr;
   //
   // check for a char
   //
   if (Serial.available()) {
      //
      // read, save, and send char
      //
      chr = Serial.read();
      buf[count] = chr;
      count += 1;
      buf[count] = 0;
      if (count == (bufsize-1))
         count = 0;
      Serial.print("hello.RP2040-XIAO.blink-echo.ino: you typed ");
      Serial.println(buf);
      //
      // blink LED red green blue white black
      //
      pixel.setPixelColor(0,pixel.Color(255,0,0));
      pixel.show();
      delay(100);
      //
      pixel.setPixelColor(0,pixel.Color(0,255,0));
      pixel.show();
      delay(100);
      //
      pixel.setPixelColor(0,pixel.Color(0,0,255));
      pixel.show();
      delay(100);
      //
      pixel.setPixelColor(0,pixel.Color(255,255,255));
      pixel.show();
      delay(100);
      //
      pixel.setPixelColor(0,pixel.Color(0,0,0));
      pixel.show();
      pixel.show();
      }
   }

I checked I had the library "Adafruit NeoPixel" installed.

check neopixel

Customizing the program

I changed the program so that it will blink in different patterns if a vowel was typed.

If a vowel is typed, RGB LED will turn "cyan magenta yellow white black". If something else (consonant, number or synbol) is typed, the RGB LED will turn "red green blue white black".

#include <Adafruit_NeoPixel.h>
//
// globals
//
#define numpixels 1
#define pixelpower 11
#define pixelpin 12
#define bufsize 25
char buf[bufsize];
int count=0;
//
// setup
//
Adafruit_NeoPixel pixel(numpixels,pixelpin,NEO_GRB+NEO_KHZ800);
void setup() {
   Serial.begin();
   pixel.begin();
   pinMode(pixelpower,OUTPUT);
   digitalWrite(pixelpower,HIGH);
   }
//
// main loop
//
void loop() {
   char chr;
   //
   // check for a char
   //
   if (Serial.available()) {
      //
      // read, save, and send char
      //
      chr = Serial.read();
      buf[count] = chr;
      count += 1;
      buf[count] = 0;
      if (count == (bufsize-1))
         count = 0;      
      Serial.print("hello.RP2040-XIAO.blink-echo.ino: you typed ");
      Serial.println(buf);
      if (chr == 'a' || chr == 'e' || chr == 'i' || chr == 'o' || chr == 'u'){
        //
        // blink LED cyan magenta yellow white black
        //
        pixel.setPixelColor(0,pixel.Color(0,255,255));
        pixel.show();
        delay(100);
        //
        pixel.setPixelColor(0,pixel.Color(255,0,255));
        pixel.show();
        delay(100);
        //
        pixel.setPixelColor(0,pixel.Color(255,255,0));
        pixel.show();
        delay(100);
        //
        pixel.setPixelColor(0,pixel.Color(255,255,255));
        pixel.show();
        delay(100);
        //
        pixel.setPixelColor(0,pixel.Color(0,0,0));
        pixel.show();
      }else{
        //
        // blink LED red green blue white black
        //
        pixel.setPixelColor(0,pixel.Color(255,0,0));
        pixel.show();
        delay(100);
        //
        pixel.setPixelColor(0,pixel.Color(0,255,0));
        pixel.show();
        delay(100);
        //
        pixel.setPixelColor(0,pixel.Color(0,0,255));
        pixel.show();
        delay(100);
        //
        pixel.setPixelColor(0,pixel.Color(255,255,255));
        pixel.show();
        delay(100);
        //
        pixel.setPixelColor(0,pixel.Color(0,0,0));
        pixel.show();
      }
      pixel.show();
      }
   }

The program suceeded like this:

3. Compare several arcchitectures

Our lab compared three microcontrollers and three IDE.

microcontroller:

  • Seeed Studio XIAO RP2040
  • M5STACK ATOM Matrix
  • micro:bit

IDE:

  • Arduino IDE
  • PlatformIO
  • MakeCode editor for micro:bit

Comparing Workflows

Set up

Set up for Arduino IDE and Platform IO is mostly the same; install required libraries and install apropriate board manager. The difference is, in Platform IO, you have to create a project before you select the board. On the other hand, in Arduino IDE, the project directory is built when you save the file.

For micro:bit, we made codes in Microsoft MakeCode. Therefore, the set up we did was to access the website.

Workflow

Workflow in Arduino IDE and Platform IO were mostly the same; Coding, compiling and uploading.

Coding was much easier in VScode owing to the high quality auto fill. Compiling is much faster in Platform IDE. However, I felt uploading was more comfortable in Arduino, since there is a GUI button which is easy to use.

If you install the Arduino extention in VScode, you will be able to access all the libraries and documents in Arduino IDE via VScode. This makes VScode and Platform IO easy to use as Arduino IDE.