Skip to content

Commit 3440ab2

Browse files
authored
Merge pull request #915 from pimoroni/feature/usb
MicroPython v1.23.0 + USB-compatible Pico firmware.
2 parents 10fed7e + 7d0bb04 commit 3440ab2

File tree

84 files changed

+903
-431
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+903
-431
lines changed

.github/workflows/micropython.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
types: [created]
88

99
env:
10-
MICROPYTHON_VERSION: v1.22.2
10+
MICROPYTHON_VERSION: 932f76c6ba64c5a3e68de3324556d9979f09303b
1111

1212
jobs:
1313
build:
@@ -19,6 +19,8 @@ jobs:
1919
include:
2020
- name: pico
2121
board: RPI_PICO
22+
- name: pico_usb
23+
board: RPI_PICO_USB
2224
- name: picow
2325
board: RPI_PICO_W
2426
- name: tiny2040_8mb

ci/micropython.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ function log_warning {
1414

1515
function micropython_clone {
1616
log_inform "Using MicroPython $MICROPYTHON_VERSION"
17-
git clone https://github.com/micropython/micropython --depth=1 --branch=$MICROPYTHON_VERSION
17+
git clone https://github.com/micropython/micropython
1818
cd micropython
19+
git checkout $MICROPYTHON_VERSION
1920
git submodule update --init lib/pico-sdk
2021
git submodule update --init lib/cyw43-driver
2122
git submodule update --init lib/lwip
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"deploy": [
3+
"../deploy.md"
4+
],
5+
"docs": "",
6+
"features": [
7+
"Breadboard friendly",
8+
"Castellated Pads",
9+
"Micro USB"
10+
],
11+
"id": "rp2-pico",
12+
"images": [
13+
"rp2-pico.jpg"
14+
],
15+
"mcu": "rp2040",
16+
"product": "Pico",
17+
"thumbnail": "",
18+
"url": "https://www.raspberrypi.com/products/raspberry-pi-pico/",
19+
"vendor": "Raspberry Pi"
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include("$(PORT_DIR)/boards/manifest.py")
2+
3+
include("../manifest_pico.py")
4+
5+
require("usb-device")
6+
require("usb-device-hid")
7+
require("usb-device-cdc")
8+
require("usb-device-keyboard")
9+
require("usb-device-mouse")
10+
require("usb-device-midi")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# cmake file for Raspberry Pi Pico
2+
set(PICO_BOARD "pico")
3+
4+
# Board specific version of the frozen manifest
5+
set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py)
6+
7+
set(MICROPY_C_HEAP_SIZE 4096)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Board and hardware specific configuration
2+
#define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico"
3+
#define MICROPY_HW_FLASH_STORAGE_BYTES (1024 * 1024)
4+
#define MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE 1
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
GP0,GPIO0
2+
GP1,GPIO1
3+
GP2,GPIO2
4+
GP3,GPIO3
5+
GP4,GPIO4
6+
GP5,GPIO5
7+
GP6,GPIO6
8+
GP7,GPIO7
9+
GP8,GPIO8
10+
GP9,GPIO9
11+
GP10,GPIO10
12+
GP11,GPIO11
13+
GP12,GPIO12
14+
GP13,GPIO13
15+
GP14,GPIO14
16+
GP15,GPIO15
17+
GP16,GPIO16
18+
GP17,GPIO17
19+
GP18,GPIO18
20+
GP19,GPIO19
21+
GP20,GPIO20
22+
GP21,GPIO21
23+
GP22,GPIO22
24+
GP25,GPIO25
25+
GP26,GPIO26
26+
GP27,GPIO27
27+
GP28,GPIO28
28+
LED,GPIO25

micropython/board/manifest_pico.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
freeze("../modules_py", "interstate75.py")
33

44
freeze("../modules_py", "pimoroni.py")
5-
freeze("../modules_py", "boot.py")
5+
freeze("../modules_py", "boot.py")
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
# Borrows heavily from : https://github.com/adafruit/Adafruit_CircuitPython_IS31FL3731/blob/main/adafruit_is31fl3731/keybow2040.py
2+
# and : https://github.com/adafruit/micropython-adafruit-is31fl3731/blob/master/is31fl3731.py
3+
import math
4+
import utime
5+
6+
7+
_MODE_REGISTER = const(0x00) # noqa: F821
8+
_FRAME_REGISTER = const(0x01) # noqa: F821
9+
_AUTOPLAY1_REGISTER = const(0x02) # noqa: F821
10+
_AUTOPLAY2_REGISTER = const(0x03) # noqa: F821
11+
_BLINK_REGISTER = const(0x05) # noqa: F821
12+
_AUDIOSYNC_REGISTER = const(0x06) # noqa: F821
13+
_BREATH1_REGISTER = const(0x08) # noqa: F821
14+
_BREATH2_REGISTER = const(0x09) # noqa: F821
15+
_SHUTDOWN_REGISTER = const(0x0a) # noqa: F821
16+
_GAIN_REGISTER = const(0x0b) # noqa: F821
17+
_ADC_REGISTER = const(0x0c) # noqa: F821
18+
19+
_CONFIG_BANK = const(0x0b) # noqa: F821
20+
_BANK_ADDRESS = const(0xfd) # noqa: F821
21+
22+
_PICTURE_MODE = const(0x00) # noqa: F821
23+
_AUTOPLAY_MODE = const(0x08) # noqa: F821
24+
_AUDIOPLAY_MODE = const(0x18) # noqa: F821
25+
26+
_ENABLE_OFFSET = const(0x00) # noqa: F821
27+
_BLINK_OFFSET = const(0x12) # noqa: F821
28+
_COLOR_OFFSET = const(0x24) # noqa: F821
29+
30+
31+
class Matrix:
32+
width = 16
33+
height = 9
34+
35+
def __init__(self, i2c, address=0x74):
36+
self.i2c = i2c
37+
self.address = address
38+
self.reset()
39+
self.init()
40+
41+
def _bank(self, bank=None):
42+
if bank is None:
43+
return self.i2c.readfrom_mem(self.address, _BANK_ADDRESS, 1)[0]
44+
self.i2c.writeto_mem(self.address, _BANK_ADDRESS, bytearray([bank]))
45+
46+
def _register(self, bank, register, value=None):
47+
self._bank(bank)
48+
if value is None:
49+
return self.i2c.readfrom_mem(self.address, register, 1)[0]
50+
self.i2c.writeto_mem(self.address, register, bytearray([value]))
51+
52+
def _mode(self, mode=None):
53+
return self._register(_CONFIG_BANK, _MODE_REGISTER, mode)
54+
55+
def init(self):
56+
self._mode(_PICTURE_MODE)
57+
self.frame(0)
58+
for frame in range(8):
59+
self.fill(0, False, frame=frame)
60+
for col in range(18):
61+
self._register(frame, _ENABLE_OFFSET + col, 0xff)
62+
self.audio_sync(False)
63+
64+
def reset(self):
65+
self.sleep(True)
66+
utime.sleep_us(10)
67+
self.sleep(False)
68+
69+
def sleep(self, value):
70+
return self._register(_CONFIG_BANK, _SHUTDOWN_REGISTER, not value)
71+
72+
def autoplay(self, delay=0, loops=0, frames=0):
73+
if delay == 0:
74+
self._mode(_PICTURE_MODE)
75+
return
76+
delay //= 11
77+
if not 0 <= loops <= 7:
78+
raise ValueError("Loops out of range")
79+
if not 0 <= frames <= 7:
80+
raise ValueError("Frames out of range")
81+
if not 1 <= delay <= 64:
82+
raise ValueError("Delay out of range")
83+
self._register(_CONFIG_BANK, _AUTOPLAY1_REGISTER, loops << 4 | frames)
84+
self._register(_CONFIG_BANK, _AUTOPLAY2_REGISTER, delay % 64)
85+
self._mode(_AUTOPLAY_MODE | self._frame)
86+
87+
def fade(self, fade_in=None, fade_out=None, pause=0):
88+
if fade_in is None and fade_out is None:
89+
self._register(_CONFIG_BANK, _BREATH2_REGISTER, 0)
90+
elif fade_in is None:
91+
fade_in = fade_out
92+
elif fade_out is None:
93+
fade_out = fade_in
94+
fade_in = int(math.log(fade_in / 26, 2))
95+
fade_out = int(math.log(fade_out / 26, 2))
96+
pause = int(math.log(pause / 26, 2))
97+
if not 0 <= fade_in <= 7:
98+
raise ValueError("Fade in out of range")
99+
if not 0 <= fade_out <= 7:
100+
raise ValueError("Fade out out of range")
101+
if not 0 <= pause <= 7:
102+
raise ValueError("Pause out of range")
103+
self._register(_CONFIG_BANK, _BREATH1_REGISTER, fade_out << 4 | fade_in)
104+
self._register(_CONFIG_BANK, _BREATH2_REGISTER, 1 << 4 | pause)
105+
106+
def frame(self, frame=None, show=True):
107+
if frame is None:
108+
return self._frame
109+
if not 0 <= frame <= 8:
110+
raise ValueError("Frame out of range")
111+
self._frame = frame
112+
if show:
113+
self._register(_CONFIG_BANK, _FRAME_REGISTER, frame)
114+
115+
def audio_sync(self, value=None):
116+
return self._register(_CONFIG_BANK, _AUDIOSYNC_REGISTER, value)
117+
118+
def audio_play(self, sample_rate, audio_gain=0,
119+
agc_enable=False, agc_fast=False):
120+
if sample_rate == 0:
121+
self._mode(_PICTURE_MODE)
122+
return
123+
sample_rate //= 46
124+
if not 1 <= sample_rate <= 256:
125+
raise ValueError("Sample rate out of range")
126+
self._register(_CONFIG_BANK, _ADC_REGISTER, sample_rate % 256)
127+
audio_gain //= 3
128+
if not 0 <= audio_gain <= 7:
129+
raise ValueError("Audio gain out of range")
130+
self._register(_CONFIG_BANK, _GAIN_REGISTER,
131+
bool(agc_enable) << 3 | bool(agc_fast) << 4 | audio_gain)
132+
self._mode(_AUDIOPLAY_MODE)
133+
134+
def blink(self, rate=None):
135+
if rate is None:
136+
return (self._register(_CONFIG_BANK, _BLINK_REGISTER) & 0x07) * 270
137+
elif rate == 0:
138+
self._register(_CONFIG_BANK, _BLINK_REGISTER, 0x00)
139+
return
140+
rate //= 270
141+
self._register(_CONFIG_BANK, _BLINK_REGISTER, rate & 0x07 | 0x08)
142+
143+
def fill(self, color=None, blink=None, frame=None):
144+
if frame is None:
145+
frame = self._frame
146+
self._bank(frame)
147+
if color is not None:
148+
if not 0 <= color <= 255:
149+
raise ValueError("Color out of range")
150+
data = bytearray([color] * 24)
151+
for row in range(6):
152+
self.i2c.writeto_mem(self.address,
153+
_COLOR_OFFSET + row * 24, data)
154+
if blink is not None:
155+
data = bool(blink) * 0xff
156+
for col in range(18):
157+
self._register(frame, _BLINK_OFFSET + col, data)
158+
159+
def write_frame(self, data, frame=None):
160+
if len(data) > 144:
161+
raise ValueError("Bytearray too large for frame")
162+
if frame is None:
163+
frame = self._frame
164+
self._bank(frame)
165+
self.i2c.writeto_mem(self.address, _COLOR_OFFSET, data)
166+
167+
def _pixel_addr(self, x, y):
168+
return x + y * 16
169+
170+
def pixel(self, x, y, color=None, blink=None, frame=None):
171+
if not 0 <= x <= self.width:
172+
return
173+
if not 0 <= y <= self.height:
174+
return
175+
pixel = self._pixel_addr(x, y)
176+
if color is None and blink is None:
177+
return self._register(self._frame, pixel)
178+
if frame is None:
179+
frame = self._frame
180+
if color is not None:
181+
if not 0 <= color <= 255:
182+
raise ValueError("Color out of range")
183+
self._register(frame, _COLOR_OFFSET + pixel, color)
184+
if blink is not None:
185+
addr, bit = divmod(pixel, 8)
186+
bits = self._register(frame, _BLINK_OFFSET + addr)
187+
if blink:
188+
bits |= 1 << bit
189+
else:
190+
bits &= ~(1 << bit)
191+
self._register(frame, _BLINK_OFFSET + addr, bits)
192+
193+
194+
class Matrix_Keybow2040(Matrix):
195+
width = 16
196+
height = 3
197+
198+
def pixelrgb(self, x, y, r, g, b, blink=None, frame=None):
199+
"""
200+
Blink or brightness for x, y-pixel
201+
202+
:param x: horizontal pixel position
203+
:param y: vertical pixel position
204+
:param r: red brightness value 0->255
205+
:param g: green brightness value 0->255
206+
:param b: blue brightness value 0->255
207+
:param blink: True to blink
208+
:param frame: the frame to set the pixel
209+
"""
210+
x = (4 * (3 - x)) + y
211+
212+
super().pixel(x, 0, r, blink, frame)
213+
super().pixel(x, 1, g, blink, frame)
214+
super().pixel(x, 2, b, blink, frame)
215+
216+
def _pixel_addr(self, x, y):
217+
lookup = [
218+
(120, 88, 104), # 0, 0
219+
(136, 40, 72), # 1, 0
220+
(112, 80, 96), # 2, 0
221+
(128, 32, 64), # 3, 0
222+
(121, 89, 105), # 0, 1
223+
(137, 41, 73), # 1, 1
224+
(113, 81, 97), # 2, 1
225+
(129, 33, 65), # 3, 1
226+
(122, 90, 106), # 0, 2
227+
(138, 25, 74), # 1, 2
228+
(114, 82, 98), # 2, 2
229+
(130, 17, 66), # 3, 2
230+
(123, 91, 107), # 0, 3
231+
(139, 26, 75), # 1, 3
232+
(115, 83, 99), # 2, 3
233+
(131, 18, 67), # 3, 3
234+
]
235+
236+
return lookup[x][y]

0 commit comments

Comments
 (0)