The previous article: Playing with RP2040: Unboxing, Evaluation and Power-on Operation introduced the hardware and power-on usage of RP2040. This article will build the software development environment.
RP2040 supports Python or C language development. This article uses Python for development.
1. Software environment construction
The processors of this RP2040 board and Pico, another product of Raspberry Pi, are both RP2040, so you can refer to the tutorial of RaspberryPi Pico:
https://pico.wiki/index.php/2021/04/27/getting-started-with-raspberry-pi-pico-basic-intro.html
1.1 Download Python firmware
Go to the Raspberry Pi official website: https://www.raspberrypi.com/documentation/microcontrollers/micropython.html and download the corresponding Python firmware. The firmware I downloaded is: rp2-pico-20221116-unstable-v1.19.1-682-gd1ed0f161.uf2
After downloading the firmware, you need to burn it to the board. In fact, you only need to drag and drop the firmware to the board.
The steps are:
-
Press BOOT and RESET to reset the board.
-
Release RESET (BOOT is still pressed), and then the board enters BOOT
-
After the RPI-RP2 drive letter (folder) pops up on the computer, release the BOOT
-
Then drag the downloaded python firmware to this disk
-
After the drag is successful, the drive letter will automatically disappear, and the firmware burning work is completed.
Note that after the firmware is burned, the automatic program of the board will also disappear, so if you restart the board at this time, the screen will be black.
1.2 Download Thonny
Thonny is a Python IDE for beginners
Go to Thonny’s official website: https://thonny.org/, download the Windows version installation package, and install it.
2 Program burning test
Connect the board to the computer, open the Python example with Thonny, switch to Pi Pico in the lower right corner, and then click the download button in the upper left corner.
After the download is complete, you can see the program's running results:
Please note that with this download method, the program is not yet solidified in the board. After power failure and restart, the program will no longer exist. You need to download the program again to see it running.
3. The program is solidified into the board
After studying the software Thonny, I found that I can put Python programs on the board. The operation method is to click File->Save As in the menu. A window will pop up, prompting you to save to the computer or Pico. After selecting Pico, you can save the Python file to the board.
When saving, you can also rename the file, such as renaming it to main.py. Note that for files saved to the board, the file name in Thonny will be displayed in brackets, and the top information of the Thonny window will also show that this file is in Pico.
At this time, click Run to run the Python program in the board. After the board is powered on, it can also start and run the Python program in the board, which means that the program has been solidified into the board.
4 Multi-file project structure
In the test program above, the entire code is in one .py file, which is not convenient for code classification and management. In fact, we can split the code by function and run it in the board. I separate the LCD and IMU codes and organize them into 3 files:
-
main.py
-
mylcd.py
-
myimu.py
Note that during runtime, you need to put all three files into the board and then click Run. If you don't put them into the board and run main.py, you will get an error message saying that the other two files cannot be found.
main.py
from machine import Pin,I2C,SPI,PWM,ADC
import mylcd
import myimu
import time
Vbat_Pin = 29
#Main function
if __name__=='__main__':
#LCD
LCD = mylcd.LCD_GC9A01A()
LCD.set_bl_pwm(65535)
#NOSE
qmi8658=myimu.IMU_QMI8658C()
#ADC
Vbat= ADC(Pin(Vbat_Pin))
while(True):
#read QMI8658
xyz=qmi8658.Read_XYZ()
LCD.fill(LCD.white)
LCD.fill_rect(0,0,240,40,LCD.red)
LCD.text("RP2040-LCD-1.28",60,25,LCD.white)
LCD.fill_rect(0,40,240,40,LCD.blue)
LCD.text("Waveshare",80,57,LCD.white)
LCD.fill_rect(0,80,120,120,0x1805)
LCD.text("ACC_X={:+.2f}".format(xyz[0]),20,100-3,LCD.white)
LCD.text("ACC_Y={:+.2f}".format(xyz[1]),20,140-3,LCD.white)
LCD.text("ACC_Z={:+.2f}".format(xyz[2]),20,180-3,LCD.white)
LCD.fill_rect(120,80,120,120,0xF073)
LCD.text("GYR_X={:+3.2f}".format(xyz[3]),125,100-3,LCD.white)
LCD.text("GYR_Y={:+3.2f}".format(xyz[4]),125,140-3,LCD.white)
LCD.text("GYR_Z={:+3.2f}".format(xyz[5]),125,180-3,LCD.white)
LCD.fill_rect(0,200,240,40,0x180f)
reading = Vbat.read_u16()*3.3/65535*2
LCD.text("Vbat={:.2f}".format(reading),80,215,LCD.white)
LCD.show()
time.sleep(0.1)
myimu.py
from machine import Pin,I2C,SPI,PWM,ADC
I2C_SDA = 6
I2C_SDL = 7
#IMU sensor--IIC interface
class IMU_QMI8658C(object):
def __init__(self,address=0X6B):
self._address = address
self._bus = I2C(id=1,scl=Pin(I2C_SDL),sda=Pin(I2C_SDA),freq=100_000)
bRet=self.WhoAmI()
if bRet :
self.Read_Revision()
else :
return NULL
self.Config_apply()
def _read_byte(self,cmd):
rec=self._bus.readfrom_mem(int(self._address),int(cmd),1)
return rec[0]
def _read_block(self, reg, length=1):
rec=self._bus.readfrom_mem(int(self._address),int(reg),length)
return rec
def _read_u16(self,cmd):
LSB = self._bus.readfrom_mem(int(self._address),int(cmd),1)
MSB = self._bus.readfrom_mem(int(self._address),int(cmd)+1,1)
return (MSB[0] << 8) + LSB[0]
def _write_byte(self,cmd,val):
self._bus.writeto_mem(int(self._address),int(cmd),bytes([int(val)]))
def WhoAmI(self):
bRight=False
if (0x05) == self._read_byte(0x00):
bRet = True
return bRet
def Read_Revision(self):
return self._read_byte(0x01)
def Config_apply(self):
# REG CTRL1
self._write_byte(0x02,0x60)
# REG CTRL2 : QMI8658AccRange_8g and QMI8658AccOdr_1000Hz
self._write_byte(0x03,0x23)
# REG CTRL3 : QMI8658GyrRange_512dps and QMI8658GyrOdr_1000Hz
self._write_byte(0x04,0x53)
# REG CTRL4 : No
self._write_byte(0x05,0x00)
# REG CTRL5 : Enable Gyroscope And Accelerometer Low-Pass Filter
self._write_byte(0x06,0x11)
# REG CTRL6 : Disables Motion on Demand.
self._write_byte(0x07,0x00)
# REG CTRL7 : Enable Gyroscope And Accelerometer
self._write_byte(0x08,0x03)
def Read_Raw_XYZ(self):
xyz=[0,0,0,0,0,0]
raw_timestamp = self._read_block(0x30,3)
raw_acc_xyz=self._read_block(0x35,6)
raw_gyro_xyz=self._read_block(0x3b,6)
raw_xyz=self._read_block(0x35,12)
timestamp = (raw_timestamp[2]<<16)|(raw_timestamp[1]<<8)|(raw_timestamp[0])
for i in range(6):
# xyz=(raw_acc_xyz[(i*2)+1]<<8)|(raw_acc_xyz[i*2])
# xyz[i+3]=(raw_gyro_xyz[((i+3)*2)+1]<<8)|(raw_gyro_xyz[(i+3)*2])
xyz = (raw_xyz[(i*2)+1]<<8)|(raw_xyz[i*2])
if xyz >= 32767:
xyz = xyz-65535
return xyz
def Read_XYZ(self):
xyz=[0,0,0,0,0,0]
raw_xyz=self.Read_Raw_XYZ()
#QMI8658AccRange_8g
acc_lsb_div=(1<<12)
#QMI8658GyrRange_512dps
gyro_lsb_div = 64
for i in range(3):
xyz=raw_xyz/acc_lsb_div#(acc_lsb_div/1000.0)
xyz[i+3]=raw_xyz[i+3]*1.0/gyro_lsb_div
return xyz
mylcd.py
from machine import Pin,I2C,SPI,PWM,ADC
import framebuf
import time
DC = 8
CS = 9
SCK = 10
MOSI = 11
RST = 12
BL = 25
#Screen--SPI interface
class LCD_GC9A01A(framebuf.FrameBuffer):
def __init__(self):
self.width = 240 #screen width
self.height = 240 #screen height
self.cs = Pin(CS,Pin.OUT)
self.rst = Pin(RST,Pin.OUT)
self.cs(1)
self.spi = SPI(1,100_000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
self.init_display()
self.red = 0x07E0
self.green = 0x001f
self.blue = 0xf800
self.white = 0xffff
self.fill(self.white) #Fill with white first
self.show()
self.pwm = PWM(Pin(BL))
self.pwm.freq(5000)
#omission......
The following is a demonstration of saving these three files to the board. You can see that the three file names all have square brackets:
Run it on main.py and it will run successfully, which means the program runs normally on the board.
5 Conclusion
This article introduces the Python software development environment for RP2040, and conducts program burning test, code solidification test in the board, and Python project split into multiple files for running test. This is the first time to use Thonny software, and there may be some functions that have not been explored. If you have any experience, please discuss and share.