Tuesday, June 18, 2024

Radxa X2L Single Board Computer Part 2

I have been experimenting with the Radxa X2L One of the conclusions I have come to is the Picomite Basic firmware is easier to work with on an RP2040 than MircoPython or Circuit Python. I am aware that the Basic programing language is not exactly in vogue these days and Python is superior in almost every way, but really no programing language beats Basic for rapid prototyping. On top of that, I really do like this Basic implementation, it keeps a lot of what made the old 1980's Basic fun, while adding modern features.

  https://geoffg.net/picomite.html

 To keep everything in line, I also opted to use FreeBasic on the Intel side of this system. FreeBasic is a pretty good implementation of the Basic language and even has some switches to make it compatible with old Basic code.

 https://www.freebasic.net/

This project is going to show you how to use FreeBasic to retrieve the IP address of your system in Linux, then transmit your IP address to the RP2040, which will then use Picomite MMBasic to display the data on a SSD1309 OLED. This is useful for systems in a DHCP environment IP addresses change frequently. You could also use this method to display CPU temperatures or even a joke of the day.

 The first thing you will need to do is flash the firmware into the RP2040, this is covered in the Picomite documentation and a dozen or so Youtube video, so I will not go over it. The second thing you will need to do is wire your OLED to the RP2040 GPIO pins. Again, there are plenty of tutorials about how to do this.

Next you will need to connect to the RP2040 through the serial port. I use Minicom in Linux

minicom -D /dev/ttyACM0

Finally before we can start programming, we need to configure the Picomite software to use I2C and the SSD1306 OLED. You do this by typing the following two lines into your terminal program, you will only have to do this once.

OPTION SYSTEM I2C GP2, GP19

OPTION LCDPANEL SSD1306I2C, LANDSCAPE

The GP2 and GP19 switches tell the software what wires you are using for communication, if you are using different GPIO pins, you will need to adjust your command appropriately. The first one is the SDA pin, the second is the SCL pin.

 Now, in your terminal, type edit and press enter. This will open the built in text editor. Go a head and type this program in.

'RP2040.BAS

CLS
Box 0, 0, 128, 64, 2, RGB(WHITE), RGB(BLACK)
Text 10, 10, "Connected", "L", 1, 1, RGB(WHITE), RGB(BLACK)

Input "IP Address: ", ipaddr$
Text 10, 20, ipaddr$, "L", 1, 1, RGB(WHITE), RGB(BLACK)

 The first line is simply a comment stating the name of the program. I do this because when you save a program to the flash memory, it automatically names it whatever is in the first line. Doing this makes it easy to identify which is which.

The next five lines;

  1. Clear the OLED screen
  2. Draws a framing box around the edge of the display
  3. Writes "Connected" to the screen
  4. It then waits for input
  5. Once it receives input, it writes that input to the OLED.

Once you have typed in the program, or done a copy/paste, press the F1 key to save it. Next type "FLASH SAVE 1", This will save the program to the build in storage, so it can be retrieved later. Lastly, you will want to type "run", you should see is waiting for input, leave it be.

Now open a new terminal, if you have not installed FreeBasic, you should do so now. Type "nano intel.bas" and enter this program.

dim as string ipaddr
open pipe "ip -4 addr show wlo1 | grep -oP '(?<=inet\s)\d+(\.\d+){3}' " for input as #1
        line input #1, ipaddr
close #1

open "/dev/ttyACM0" for output as #1
print #1, ipaddr
close #1

Each line of this program

  1. Defines the variable ipaddr as a string
  2. Runs a shell command to retrieve the IP address of the system *
  3. Sets the ipaddr variable to the IP address of the system
  4. Closes the pipe used for running the shell command
  5. Opens the serial port so it can be written to
  6. Transmits the ipaddr variable to the RP2040
  7. Closes the serial port
    * In the shell command where it says wlo1, this is the network device whose IP address you want to retrieve, you will need to change this to match your system.

Once the program has been written, press Ctrl-x, press Y and then enter. This will save the program. To compile the program, type "fbc intel.bas", you should now have an intel executable, simply type "./intel" and press enter. If all went well, you should see the IP address appear in the minicom terminal and on the OLED.

If you want this to run each time the system boots up, type "OPTION AUTORUN 1" into the minicom terminal. This will tell the RP 2040 to run the program in slot 1 on boot up. Then copy the intel executable to /usr/local/bin and add it to you startup programs.

That is pretty much it. You will still have two slots on the RP2040 to save other programs. This is a pretty good start to making the RP2040 useful to you when using the Radxa X2L. I kind of feel like all computers should have an RP2040 built in.

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Mastodon