User:Brendlefly62/Radxa x4 N100 sbc with RP2040/Use the RP2040 Microcontroller/Write a new blink program

From Gentoo Wiki
Jump to:navigation Jump to:search

Write the program

Replace the contents of pico-examples/blink/blink.c with your own program

user $nano pico-examples/blink/blink.c
FILE pico-examples/blink/blink.c
#include "pico/stdlib.h"
#define GREEN_LED 28  // GPIO28 (pin 3)
#define YELLOW_LED 29 // GPIO29 (pin 5)
#define RED_LED 4     // GPIO04 (pin 7)
                      // use pin 9 as GND

int main() {

    gpio_init(GREEN_LED);
    gpio_init(YELLOW_LED);
    gpio_init(RED_LED);
    gpio_set_dir(GREEN_LED, GPIO_OUT);
    gpio_set_dir(YELLOW_LED, GPIO_OUT);
    gpio_set_dir(RED_LED, GPIO_OUT);

    while (true) {
        gpio_put(GREEN_LED, 1);
        sleep_ms(250);
        gpio_put(YELLOW_LED, 1);
        sleep_ms(250);
        gpio_put(RED_LED, 1);
        sleep_ms(250);
        gpio_put(GREEN_LED, 0);
        sleep_ms(250);
        gpio_put(YELLOW_LED, 0);
        sleep_ms(250);
        gpio_put(RED_LED, 0);
        sleep_ms(250);
    }
}

Now recompile it

user $export PICO_SDK_PATH=/home/joe/pico-sdk/
user $cmake .. && make -j$(nproc)

Flash the program to the RP2040

The new program will now be transferred to the RP2040, by putting temporarily making the RP2040's storage accessible as USB storage in the X4/N100's filesystem, and copying the new program to that device. After the copy operation completes, the USB storage device will "disappear" (no longer available to the N100 host system), and the program will begin to run on the RP2040

"reboot" the rp2040 to make it accessible as USB storage

This can be done two ways -- by pressing the "boot-sel" button, or by a executing a script to toggle a couple of internal GPIO pins on the N100 (do not confuse this wtih the RP2040 toggling GPIO pins pysically present at the 40-pin header).

Here's now to use the software method

(if pressing the boot-sel button, skip this section) ensure the host X4/N100 system has libgpiod installed (optionally with USE=python)

root #USE="cxx tools" emerge -av dev-libs/libgpiod

Now create a script to toggle internal gpio lines to put the rp2040 in usb-storage mode --

root #nano rp2040_usb.sh
FILE rp2040_usb.sh
#! /bin/bash
sudo gpioset gpiochip0 17=1
sudo gpioset gpiochip0 7=1
sleep 1
sudo gpioset gpiochip0 17=0
sudo gpioset gpiochip0 7=0

Make this file executable and save it to a location in this environment's PATH --

root #chmod a+x rp2040_usb.sh
root #mv rp2040_usb.sh /usr/local/sbin/

put the rp2040 in usb-storage mode

Press the boot-select button or execute the script above --

user $rp2040_usb.sh

Verify the rp2040 is now available as a storage device

user $lsblk
$ lsblk
NAME               MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINTS
sda                  8:0    1  14.6G  0 disk  
`-sda1               8:1    1  14.6G  0 part  
sdb                  8:16   1   128M  0 disk  
`-sdb1               8:17   1   128M  0 part         ### <=== here it is !
nvme0n1            259:0    0 119.2G  0 disk  
|-nvme0n1p1        259:1    0     4M  0 part  
|-nvme0n1p2        259:2    0   250M  0 part  /efi
|-nvme0n1p3        259:3    0   500M  0 part  /boot
`-nvme0n1p4        259:4    0 118.5G  0 part  
  `-ev014          253:0    0 118.5G  0 crypt 
    |-vg_x401-root 253:1    0  13.5G  0 lvm   /
    |-vg_x401-usr  253:2    0    40G  0 lvm   /usr
    |-vg_x401-var  253:3    0    40G  0 lvm   /var
    |-vg_x401-swap 253:4    0     6G  0 lvm   [SWAP]
    |-vg_x401-home 253:5    0     5G  0 lvm   /home
    |-vg_x401-opt  253:6    0     4G  0 lvm   /opt
    `-vg_x401-tmp  253:7    0    10G  0 lvm   /tmp

Copy the program to the rp2040

user $sudo mount /dev/sdb1 /mnt/rp2040/
user $sudo cp -v blink/blink.uf2 /mnt/rp2040/
'blink/blink.uf2' -> '/mnt/rp2040/blink.uf2'
Important
The rp2040 will now "disappear" from availability on the host system, but don't forget to un-do the above mount action, else the system will track the /dev/sdb1 device as used and the next time this procedure is followed, the rp2040 will be assigned a new device node (e.g. sdc1)..
user $sudo umount /mnt/rp2040

Now observe the new program running

"RP 2040 on Radxa X4 SBC controlling LED on GPIO pin header"

See video here

https://youtube.com/shorts/Lbe5EFXIKNs?feature=share