8000 RP2: Add support for Arduino Nano RP2040. · micropython/micropython@813628e · GitHub
[go: up one dir, main page]

Skip to content

Commit 813628e

Browse files
committed
RP2: Add support for Arduino Nano RP2040.
1 parent c70930f commit 813628e

File tree

6 files changed

+258
-0
lines changed

6 files changed

+258
-0
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,37 @@ set(PICO_SDK_COMPONENTS
161161
tinyusb_device
162162
)
163163

164+
if (MICROPY_PY_NETWORK_NINAW10)
165+
target_compile_definitions(${MICROPY_TARGET} PRIVATE
166+
MICROPY_PY_NETWORK_NINAW10=1
167+
)
168+
169+
target_include_directories(${MICROPY_TARGET} PRIVATE
170+
${MICROPY_DIR}/drivers/ninaw10/
171+
)
172+
173+
# Enable NINA-W10 WiFi driver and Python module.
174+
if (MICROPY_PY_NETWORK)
175+
list(APPEND MICROPY_SOURCE_DRIVERS
176+
${MICROPY_DIR}/drivers/ninaw10/nina_wifi_drv.c
177+
${MICROPY_DIR}/drivers/ninaw10/nina_wifi_bsp.c
178+
)
179+
list(APPEND MICROPY_SOURCE_EXTMOD
180+
${MICROPY_DIR}/extmod/network_nina.c
181+
)
182+
list(APPEND MICROPY_SOURCE_QSTR
183+
${MICROPY_DIR}/extmod/network_nina.c
184+
)
185+
endif()
186+
187+
# Enable NINA-W10 Bluetooth driver.
188+
if(MICROPY_PY_BLUETOOTH)
189+
list(APPEND MICROPY_SOURCE_DRIVERS
190+
${MICROPY_DIR}/drivers/ninaw10/nina_bt_hci.c
191+
)
192+
endif()
193+
endif()
194+
164195
# Define mpy-cross flags and frozen manifest
165196
set(MICROPY_CROSS_FLAGS -march=armv7m)
166197
if (NOT MICROPY_FROZEN_MANIFEST)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include("$(PORT_DIR)/boards/manifest.py")
2+
freeze ("$(BOARD_DIR)/modules")
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Helpers for generating BLE advertising payloads.
2+
3+
from micropython import const
4+
import struct
5+
import bluetooth
6+
7+
# Advertising payloads are repeated packets of the following form:
8+
# 1 byte data length (N + 1)
9+
# 1 byte type (see constants below)
10+
# N bytes type-specific data
11+
12+
_ADV_TYPE_FLAGS = const(0x01)
13+
_ADV_TYPE_NAME = const(0x09)
14+
_ADV_TYPE_UUID16_COMPLETE = const(0x3)
15+
_ADV_TYPE_UUID32_COMPLETE = const(0x5)
16+
_ADV_TYPE_UUID128_COMPLETE = const(0x7)
17+
_ADV_TYPE_UUID16_MORE = const(0x2)
18+
_ADV_TYPE_UUID32_MORE = const(0x4)
19+
_ADV_TYPE_UUID128_MORE = const(0x6)
20+
_ADV_TYPE_APPEARANCE = const(0x19)
21+
22+
23+
# Generate a payload to be passed to gap_advertise(adv_data=...).
24+
def advertising_payload(limited_disc=False, br_edr=False, name=None, services=None, appearance=0):
25+
payload = bytearray()
26+
27+
def _append(adv_type, value):
28+
nonlocal payload
29+
payload += struct.pack("BB", len(value) + 1, adv_type) + value
30+
31+
_append(
32+
_ADV_TYPE_FLAGS,
33+
struct.pack("B", (0x01 if limited_disc else 0x02) + (0x18 if br_edr else 0x04)),
34+
)
35+
36+
if name:
37+
_append(_ADV_TYPE_NAME, name)
38+
39+
if services:
40+
for uuid in services:
41+
b = bytes(uuid)
42+
if len(b) == 2:
43+
_append(_ADV_TYPE_UUID16_COMPLETE, b)
44+
elif len(b) == 4:
45+
_append(_ADV_TYPE_UUID32_COMPLETE, b)
46+
elif len(b) == 16:
47+
_append(_ADV_TYPE_UUID128_COMPLETE, b)
48+
49+
# See org.bluetooth.characteristic.gap.appearance.xml
50+
if appearance:
51+
_append(_ADV_TYPE_APPEARANCE, struct.pack("<h", appearance))
52+
53+
return payload
54+
55+
56+
def decode_field(payload, adv_type):
57+
i = 0
58+
result = []
59+
while i + 1 < len(payload):
60+
if payload[i + 1] == adv_type:
61+
result.append(payload[i + 2 : i + payload[i] + 1])
62+
i += 1 + payload[i]
63+
return result
64+
65+
66+
def decode_name(payload):
67+
n = decode_field(payload, _ADV_TYPE_NAME)
68+
return str(n[0], "utf-8") if n else ""
69+
70+
71+
def decode_services(payload):
72+
services = []
73+
for u in decode_field(payload, _ADV_TYPE_UUID16_COMPLETE):
74+
services.append(bluetooth.UUID(struct.unpack("<h", u)[0]))
75+
for u in decode_field(payload, _ADV_TYPE_UUID32_COMPLETE):
76+
services.append(bluetooth.UUID(struct.unpack("<d", u)[0]))
77+
for u in decode_field(payload, _ADV_TYPE_UUID128_COMPLETE):
78+
services.append(bluetooth.UUID(u))
79+
return services
80+
81+
82+
def demo():
83+
payload = advertising_payload(
84+
name="micropython",
85+
services=[bluetooth.UUID(0x181A), bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")],
86+
)
87+
print(payload)
88+
print(decode_name(payload))
89+
print(decode_services(payload))
90+
91+
92+
if __name__ == "__main__":
93+
demo()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""
2+
LSM6DSOX STMicro driver for MicroPython.
3+
Base on: LSM9DS1 driver and https://github.com/arduino-libraries/Arduino_LSM6DSOX
4+
5+
Example usage:
6+
import time
7+
from lsm6dsox import LSM6DSOX
8+
9+
from machine import Pin, I2C
10+
lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12)))
11+
12+
while (True):
13+
print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_accel()))
14+
print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_gyro()))
15+
print("")
16+
time.sleep_ms(100)
17+
"""
18+
import array
19+
20+
21+
class LSM6DSOX:
22+
DEFAULT_ADDR = const(0x6A)
23+
24+
WHO_AM_I_REG = const(0x0F)
25+
CTRL1_XL = const(0x10)
26+
CTRL2_G = const(0x11)
27+
28+
STATUS_REG = const(0x1E)
29+
30+
CTRL6_C = const(0x15)
31+
CTRL7_G = const(0x16)
32+
CTRL8_XL = const(0x17)
33+
34+
OUTX_L_G = const(0x22)
35+
OUTX_H_G = const(0x23)
36+
OUTY_L_G = const(0x24)
37+
OUTY_H_G = const(0x25)
38+
OUTZ_L_G = const(0x26)
39+
OUTZ_H_G = const(0x27)
40+
41+
OUTX_L_XL = const(0x28)
42+
OUTX_H_XL = const(0x29)
43+
OUTY_L_XL = const(0x2A)
44+
OUTY_H_XL = const(0x2B)
45+
OUTZ_L_XL = const(0x2C)
46+
OUTZ_H_XL = const(0x2D)
47+
48+
def __init__(self, i2c, address=DEFAULT_ADDR):
49+
self.i2c = i2c
50+
self.address = address
51+
52+
# check the id of the Accelerometer/Gyro
53+
if self.__read_reg(WHO_AM_I_REG, 1) != b"l":
54+
raise OSError("No LSM6DS device was found at address 0x%x" % (self.address))
55+
56+
# Set the gyroscope control register to work at 104 Hz, 2000 dps and in bypass mode
57+
self.__write_reg(CTRL2_G, b"\x4C")
58+
59+
# Set the Accelerometer control register to work at 104 Hz, 4g, and in bypass mode and enable ODR/4
60+
# low pass filter (check figure9 of LSM6DSOX's datasheet)
61+
self.__write_reg(CTRL1_XL, b"\x4A")
62+
63+
# set gyroscope power mode to high performance and bandwidth to 16 MHz
64+
self.__write_reg(CTRL7_G, b"\x00")
65+
66+
# Set the ODR config register to ODR/4
67+
self.__write_reg(CTRL8_XL, b"\x09")
68+
69+
self.scale_gyro = 32768 / 2000
70+
self.scale_accel = 32768 / 4
71+
self.scratch_int = array.array("h", [0, 0, 0])
72+
73+
def __read_reg(self, reg, size):
74+
return self.i2c.readfrom_mem(self.address, reg, size)
75+
76+
def __write_reg(self, reg, val):
77+
self.i2c.writeto_mem(self.address, reg, val)
78+
79+
def read_gyro(self):
80+
"""Returns gyroscope vector in degrees/sec."""
81+
mv = memoryview(self.scratch_int)
82+
f = self.scale_gyro
83+
self.i2c.readfrom_mem_into(self.address, OUTX_L_G, mv)
84+
return (mv[0] / f, mv[1] / f, mv[2] / f)
85+
86+
def read_accel(self):
87+
"""Returns acceleration vector in gravity units (9.81m/s^2)."""
88+
mv = memoryview(self.scratch_int)
89+
f = self.scale_accel
90+
self.i2c.readfrom_mem_into(self.address, OUTX_L_XL, mv)
91+
return (mv[0] / f, mv[1] / f, mv[2] / f)
Lines changed: 6 additions & 0 deletions
< 1241 button class="Button Button--iconOnly Button--invisible" aria-label="More options" id=":R14etlab:" aria-haspopup="true" aria-expanded="false" tabindex="0">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# cmake file for Arduino Nano RP2040 Connect.
2+
set(MICROPY_PY_NETWORK 1)
3+
set(MICROPY_PY_BLUETOOTH 1)
4+
set(MICROPY_BLUETOOTH_NIMBLE 1)
5+
set(MICROPY_PY_NETWORK_NINAW10 1)
6+
set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//Board config for Arduino Nano RP2040 Connect.
2+
3+
// Board and hardware specific configuration
4+
#define MICROPY_HW_BOARD_NAME "Arduino Nano RP2040 Connect"
5+
#define MICROPY_HW_FLASH_STORAGE_BYTES (8 * 1024 * 1024)
6+
7+
// Enable USB Mass Storage with FatFS filesystem.
8+
#define MICROPY_HW_USB_MSC (1)
9+
#define MICROPY_HW_USB_VID (0x2341)
10+
#define MICROPY_HW_USB_PID (0x015e)
11+
12+
// UART 1 config.
13+
#define MICROPY_HW_UART1_TX (8)
14+
#define MICROPY_HW_UART1_RX (9)
15+
#define MICROPY_HW_UART1_CTS (10)
16+
#define MICROPY_HW_UART1_RTS (11)
17+
18+
// SPI 1 config.
19+
#define MICROPY_HW_SPI1_SCK (14)
20+
#define MICROPY_HW_SPI1_MOSI (11)
21+
#define MICROPY_HW_SPI1_MISO (8)
22+
23+
// Bluetooth config.
24+
#define MICROPY_HW_BLE_UART_ID (1)
25+
#define MICROPY_HW_BLE_UART_BAUDRATE (119600)
26+
27+
// WiFi/NINA-W10 config.
28+
#define MICROPY_HW_WIFI_SPI_ID (1)
29+
#define MICROPY_HW_WIFI_SPI_BAUDRATE (8 * 1000 * 1000)
30+
31+
// ublox Nina-W10 module config.
32+
#define MICROPY_HW_NINA_RESET (3)
33+
#define MICROPY_HW_NINA_GPIO0 (2)
34+
#define MICROPY_HW_NINA_GPIO1 (9)
35+
#define MICROPY_HW_NINA_ACK (10)

0 commit comments

Comments
 (0)
0