1. 程式人生 > 其它 >在 raspberry pi pico 上體驗 MicroPython

在 raspberry pi pico 上體驗 MicroPython

raspberry pi pico 官方提供 2 種開發方式,其中一種是使用 python3 開發,樹莓派提供了 MicroPython 韌體,把這個韌體燒錄進去,就可以 在 pico 上跑 python3 指令碼。

1、MicroPython 相關資源

用 python 開發,官網能找到的資源有:
1、MicroPython 韌體
2、文件
3、MicroPython 例程

raspberrypi 官方 github 上有相關例程:pico-micropython-examples

2、下載、燒錄 MicroPython 韌體

樹莓派官網提供了編譯好的 MicroPython 韌體,也可以自己編譯, Raspberry Pi Pico Python SDK 給出了編譯教程。

這裡直接使用官方編譯好的韌體,下載下來是這樣的:

先按住板子上的按鍵,然後連上電腦,等電腦出現名字為 RPI-RP2的磁碟時,把下載到的韌體拷貝到該磁碟,等拷貝完成就燒錄好了,燒錄完,裝置會自動重新啟動,然後我的電腦上出現了這個:

裝置管理器中多了個 COM 口:

PC上可以使用串列埠終端通過該 COM 口操作 pico。

3、使用 MobaXterm 操作 pico

我是用的串列埠終端是 MobaXterm,MobaXterm 什麼?MobaXterm 官網的描述是:

Enhanced terminal for Windows with X11 server, tabbed SSH client, network tools and much more

可以建立SSH、Telnet 、串列埠終端等等,功能釋放強大,有免費版、也有收費的,免費的夠用了。

建立一個串列埠終端連線跑 MicroPython 的 pico,選擇對應的 COM 口,波特率115200,如下:

點選 OK 後,介面烏漆嘛黑,什麼都沒有,需要按下回車,然後就可以進行操作了,如下:

4、點燈

點下燈,從原理圖看,是 GPIO25 控制 LED:

在MobaXterm 執行一下 python 程式碼,就可以點亮板子上的 LED 了:

from machine import Pin
led = Pin(25, Pin.OUT)
led.value(1)

5、附上 pico 跑 MicroPython 是 USB 相關資訊

[Port2]  :  USB Composite Device

          ---===>Device Information<===---
English product name: "Board in FS mode"

ConnectionStatus:                  
Current Config Value:              0x01  -> Device Bus Speed: Full
Device Address:                    0x15
Open Pipes:                           3

          ===>Endpoint Descriptor<===
bLength:                           0x07
bDescriptorType:                   0x05
bEndpointAddress:                  0x81  -> Direction: IN - EndpointID: 1
bmAttributes:                      0x03  -> Interrupt Transfer Type
wMaxPacketSize:                  0x0008 = 0x08 bytes
bInterval:                         0x10

          ===>Endpoint Descriptor<===
bLength:                           0x07
bDescriptorType:                   0x05
bEndpointAddress:                  0x02  -> Direction: OUT - EndpointID: 2
bmAttributes:                      0x02  -> Bulk Transfer Type
wMaxPacketSize:                  0x0040 = 0x40 bytes
bInterval:                         0x00

          ===>Endpoint Descriptor<===
bLength:                           0x07
bDescriptorType:                   0x05
bEndpointAddress:                  0x82  -> Direction: IN - EndpointID: 2
bmAttributes:                      0x02  -> Bulk Transfer Type
wMaxPacketSize:                  0x0040 = 0x40 bytes
bInterval:                         0x00

          ===>Device Descriptor<===
bLength:                           0x12
bDescriptorType:                   0x01
bcdUSB:                          0x0200
bDeviceClass:                      0xEF  -> This is a Multi-interface Function Code Device
bDeviceSubClass:                   0x02  -> This is the Common Class Sub Class
bDeviceProtocol:                   0x01  -> This is the Interface Association Descriptor protocol
bMaxPacketSize0:                   0x40 = (64) Bytes
idVendor:                        0x2E8A = Vendor ID not listed with USB.org as of 03-19-2008
idProduct:                       0x0005
bcdDevice:                       0x0100
iManufacturer:                     0x01
     English (United States)  "MicroPython"
iProduct:                          0x02
     English (United States)  "Board in FS mode"
iSerialNumber:                     0x03
     English (United States)  "000000000000"
bNumConfigurations:                0x01

          ===>Configuration Descriptor<===
bLength:                           0x09
bDescriptorType:                   0x02
wTotalLength:                    0x004B  -> Validated
bNumInterfaces:                    0x02
bConfigurationValue:               0x01
iConfiguration:                    0x00
bmAttributes:                      0xA0  -> Bus Powered
MaxPower:                          0x7D = 250 mA

          ===>IAD Descriptor<===
bLength:                           0x08
bDescriptorType:                   0x0B
bFirstInterface:                   0x00
bInterfaceCount:                   0x02
bFunctionClass:                    0x02  -> This is Communications (CDC Control) USB Device Interface Class
bFunctionSubClass:                 0x02
bFunctionProtocol:                 0x00
iFunction:                         0x00

          ===>Interface Descriptor<===
bLength:                           0x09
bDescriptorType:                   0x04
bInterfaceNumber:                  0x00
bAlternateSetting:                 0x00
bNumEndpoints:                     0x01
bInterfaceClass:                   0x02  -> This is Communications (CDC Control) USB Device Interface Class
bInterfaceSubClass:                0x02
bInterfaceProtocol:                0x00
CAUTION:  This may be an invalid bInterfaceProtocol
iInterface:                        0x04
     English (United States)  "Board CDC"
  -> This is a Communications (CDC Control) USB Device Interface Class

          ===>Descriptor Hex Dump<===
bLength:                           0x05
bDescriptorType:                   0x24
05 24 00 20 01 
  -> This is a Communications (CDC Control) USB Device Interface Class

          ===>Descriptor Hex Dump<===
bLength:                           0x05
bDescriptorType:                   0x24
05 24 01 00 01 
  -> This is a Communications (CDC Control) USB Device Interface Class

          ===>Descriptor Hex Dump<===
bLength:                           0x04
bDescriptorType:                   0x24
04 24 02 02 
  -> This is a Communications (CDC Control) USB Device Interface Class

          ===>Descriptor Hex Dump<===
bLength:                           0x05
bDescriptorType:                   0x24
05 24 06 00 01 

          ===>Endpoint Descriptor<===
bLength:                           0x07
bDescriptorType:                   0x05
bEndpointAddress:                  0x81  -> Direction: IN - EndpointID: 1
bmAttributes:                      0x03  -> Interrupt Transfer Type
wMaxPacketSize:                  0x0008 = 0x08 bytes
bInterval:                         0x10

          ===>Interface Descriptor<===
bLength:                           0x09
bDescriptorType:                   0x04
bInterfaceNumber:                  0x01
bAlternateSetting:                 0x00
bNumEndpoints:                     0x02
bInterfaceClass:                   0x0A  -> This is a CDC Data USB Device Interface Class
bInterfaceSubClass:                0x00
bInterfaceProtocol:                0x00
CAUTION:  This may be an invalid bInterfaceProtocol
iInterface:                        0x00

          ===>Endpoint Descriptor<===
bLength:                           0x07
bDescriptorType:                   0x05
bEndpointAddress:                  0x02  -> Direction: OUT - EndpointID: 2
bmAttributes:                      0x02  -> Bulk Transfer Type
wMaxPacketSize:                  0x0040 = 0x40 bytes
bInterval:                         0x00

          ===>Endpoint Descriptor<===
bLength:                           0x07
bDescriptorType:                   0x05
bEndpointAddress:                  0x82  -> Direction: IN - EndpointID: 2
bmAttributes:                      0x02  -> Bulk Transfer Type
wMaxPacketSize:                  0x0040 = 0x40 bytes
bInterval:                         0x00