-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
RP2: Add support for Arduino Nano RP2040 #7669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
""" | ||
LSM6DSOX STMicro driver for MicroPython based on LSM9DS1: | ||
Source repo: https://github.com/hoihu/projects/tree/master/raspi-hat | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2021 Damien P. George | ||
Copyright (c) 2021 Ibrahim Abdelkader <iabdalkader@openmv.io> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
|
||
Basic example usage: | ||
|
||
import time | ||
from lsm6dsox import LSM6DSOX | ||
|
||
from machine import Pin, I2C | ||
lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12))) | ||
|
||
while (True): | ||
print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_accel())) | ||
print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_gyro())) | ||
print("") | ||
time.sleep_ms(100) | ||
""" | ||
import array | ||
from micropython import const | ||
|
||
|
||
class LSM6DSOX: | ||
_CTRL3_C = const(0x12) | ||
_CTRL1_XL = const(0x10) | ||
_CTRL8_XL = const(0x17) | ||
_CTRL9_XL = const(0x18) | ||
|
||
_CTRL2_G = const(0x11) | ||
_CTRL7_G = const(0x16) | ||
|
||
_OUTX_L_G = const(0x22) | ||
_OUTX_L_XL = const(0x28) | ||
_MLC_STATUS = const(0x38) | ||
|
||
_DEFAULT_ADDR = const(0x6A) | ||
_WHO_AM_I_REG = const(0x0F) | ||
|
||
_FUNC_CFG_ACCESS = const(0x01) | ||
_FUNC_CFG_BANK_USER = const(0) | ||
_FUNC_CFG_BANK_HUB = const(1) | ||
_FUNC_CFG_BANK_EMBED = const(2) | ||
|
||
_MLC0_SRC = const(0x70) | ||
_MLC_INT1 = const(0x0D) | ||
_TAP_CFG0 = const(0x56) | ||
|
||
_EMB_FUNC_EN_A = const(0x04) | ||
_EMB_FUNC_EN_B = const(0x05) | ||
|
||
def __init__( | ||
self, | ||
i2c, | ||
address=_DEFAULT_ADDR, | ||
gyro_odr=104, | ||
accel_odr=104, | ||
gyro_scale=2000, | ||
accel_scale=4, | ||
ucf=None, | ||
): | ||
"""Initalizes Gyro and Accelerator. | ||
accel_odr: (0, 1.6Hz, 3.33Hz, 6.66Hz, 12.5Hz, 26Hz, 52Hz, 104Hz, 208Hz, 416Hz, 888Hz) | ||
gyro_odr: (0, 1.6Hz, 3.33Hz, 6.66Hz, 12.5Hz, 26Hz, 52Hz, 104Hz, 208Hz, 416Hz, 888Hz) | ||
gyro_scale: (245dps, 500dps, 1000dps, 2000dps) | ||
accel_scale: (+/-2g, +/-4g, +/-8g, +-16g) | ||
ucf: MLC program to load. | ||
""" | ||
self.i2c = i2c | ||
self.address = address | ||
|
||
# check the id of the Accelerometer/Gyro | ||
if self.__read_reg(_WHO_AM_I_REG) != 108: | ||
raise OSError("No LSM6DS device was found at address 0x%x" % (self.address)) | ||
|
||
# allocate scratch buffer for efficient conversions and memread op's | ||
self.scratch_int = array.array("h", [0, 0, 0]) | ||
|
||
SCALE_GYRO = {250: 0, 500: 1, 1000: 2, 2000: 3} | ||
SCALE_ACCEL = {2: 0, 4: 2, 8: 3, 16: 1} | ||
# XL_HM_MODE = 0 by default. G_HM_MODE = 0 by default. | ||
ODR = { | ||
0: 0x00, | ||
1.6: 0x08, | ||
3.33: 0x09, | ||
6.66: 0x0A, | ||
12.5: 0x01, | ||
26: 0x02, | ||
52: 0x03, | ||
104: 0x04, | ||
208: 0x05, | ||
416: 0x06, | ||
888: 0x07, | ||
} | ||
|
||
gyro_odr = round(gyro_odr, 2) | ||
accel_odr = round(accel_odr, 2) | ||
|
||
# Sanity checks | ||
if not gyro_odr in ODR: | ||
raise ValueError("Invalid sampling rate: %d" % accel_odr) | ||
if not gyro_scale in SCALE_GYRO: | ||
raise ValueError("invalid gyro scaling: %d" % gyro_scale) | ||
if not accel_odr in ODR: | ||
raise ValueError("Invalid sampling rate: %d" % accel_odr) | ||
if not accel_scale in SCALE_ACCEL: | ||
raise ValueError("invalid accelerometer scaling: %d" % accel_scale) | ||
|
||
# Soft-reset the device. | ||
self.reset() | ||
|
||
# Load and configure MLC if UCF file is provided | ||
if ucf != None: | ||
self.load_mlc(ucf) | ||
|
||
# Set Gyroscope datarate and scale. | ||
# Note output from LPF2 second filtering stage is selected. See Figure 18. | ||
self.__write_reg(_CTRL1_XL, (ODR[accel_odr] << 4) | (SCALE_ACCEL[accel_scale] << 2) | 2) | ||
|
||
# Enable LPF2 and HPF fast-settling mode, ODR/4 | ||
self.__write_reg(_CTRL8_XL, 0x09) | ||
|
||
# Set Gyroscope datarate and scale. | ||
self.__write_reg(_CTRL2_G, (ODR[gyro_odr] << 4) | (SCALE_GYRO[gyro_scale] << 2) | 0) | ||
|
||
self.gyro_scale = 32768 / gyro_scale | ||
self.accel_scale = 32768 / accel_scale | ||
|
||
def __read_reg(self, reg, size=1): | ||
buf = self.i2c.readfrom_mem(self.address, reg, size) | ||
if size == 1: | ||
return int(buf[0]) | ||
return [int(x) for x in buf] | ||
|
||
def __write_reg(self, reg, val): | ||
self.i2c.writeto_mem(self.address, reg, bytes([val])) | ||
|
||
def reset(self): | ||
self.__write_reg(_CTRL3_C, self.__read_reg(_CTRL3_C) | 0x1) | ||
for i in range(0, 10): | ||
if (self.__read_reg(_CTRL3_C) & 0x01) == 0: | ||
return | ||
time.sleep_ms(10) | ||
raise OSError("Failed to reset LSM6DS device.") | ||
|
||
def set_mem_bank(self, bank): | ||
cfg = self.__read_reg(_FUNC_CFG_ACCESS) & 0x3F | ||
self.__write_reg(_FUNC_CFG_ACCESS, cfg | (bank << 6)) | ||
|
||
def set_embedded_functions(self, enable, emb_ab=None): | ||
self.set_mem_bank(_FUNC_CFG_BANK_EMBED) | ||
if enable: | ||
self.__write_reg(_EMB_FUNC_EN_A, emb_ab[0]) | ||
self.__write_reg(_EMB_FUNC_EN_B, emb_ab[1]) | ||
else: | ||
emb_a = self.__read_reg(_EMB_FUNC_EN_A) | ||
emb_b = self.__read_reg(_EMB_FUNC_EN_B) | ||
self.__write_reg(_EMB_FUNC_EN_A, (emb_a & 0xC7)) | ||
self.__write_reg(_EMB_FUNC_EN_B, (emb_b & 0xE6)) | ||
emb_ab = (emb_a, emb_b) | ||
|
||
self.set_mem_bank(_FUNC_CFG_BANK_USER) | ||
return emb_ab | ||
|
||
def load_mlc(self, ucf): | ||
# Load MLC config from file | ||
with open(ucf, "r") as ucf_file: | ||
for l in ucf_file: | ||
if l.startswith("Ac"): | ||
v = [int(v, 16) for v in l.strip().split(" ")[1:3]] | ||
self.__write_reg(v[0], v[1]) | ||
|
||
emb_ab = self.set_embedded_functions(False) | ||
|
||
# Disable I3C interface | ||
self.__write_reg(_CTRL9_XL, self.__read_reg(_CTRL9_XL) | 0x01) | ||
|
||
# Enable Block Data Update | ||
self.__write_reg(_CTRL3_C, self.__read_reg(_CTRL3_C) | 0x40) | ||
|
||
# Route signals on interrupt pin 1 | ||
self.set_mem_bank(_FUNC_CFG_BANK_EMBED) | ||
self.__write_reg(_MLC_INT1, self.__read_reg(_MLC_INT1) & 0x01) | ||
self.set_mem_bank(_FUNC_CFG_BANK_USER) | ||
|
||
# Configure interrupt pin mode | ||
self.__write_reg(_TAP_CFG0, self.__read_reg(_TAP_CFG0) | 0x41) | ||
|
||
self.set_embedded_functions(True, emb_ab) | ||
|
||
def read_mlc_output(self): | ||
buf = None | ||
if self.__read_reg(_MLC_STATUS) & 0x1: | ||
self.__read_reg(0x1A, size=12) | ||
self.set_mem_bank(_FUNC_CFG_BANK_EMBED) | ||
buf = self.__read_reg(_MLC0_SRC, 8) | ||
self.set_mem_bank(_FUNC_CFG_BANK_USER) | ||
return buf | ||
|
||
def read_gyro(self): | ||
"""Returns gyroscope vector in degrees/sec.""" | ||
mv = memoryview(self.scratch_int) | ||
f = self.gyro_scale | ||
self.i2c.readfrom_mem_into(self.address, _OUTX_L_G, mv) | ||
return (mv[0] / f, mv[1] / f, mv[2] / f) | ||
|
||
def read_accel(self): | ||
"""Returns acceleration vector in gravity units (9.81m/s^2).""" | ||
mv = memoryview(self.scratch_int) | ||
f = self.accel_scale | ||
self.i2c.readfrom_mem_into(self.address, _OUTX_L_XL, mv) | ||
return (mv[0] / f, mv[1] / f, mv[2] / f) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# LSM6DSOX Basic Example. | ||
import time | ||
from lsm6dsox import LSM6DSOX | ||
|
||
from machine import Pin, I2C | ||
|
||
lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12))) | ||
|
||
while True: | ||
print("Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}".format(*lsm.read_accel())) | ||
print("Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}".format(*lsm.read_gyro())) | ||
print("") | ||
time.sleep_ms(100) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# LSM6DSOX IMU MLC (Machine Learning Core) Example. | ||
# Download the raw UCF file, copy to storage and reset. | ||
|
||
# NOTE: The pre-trained models (UCF files) for the examples can be found here: | ||
# https://github.com/STMicroelectronics/STMems_Machine_Learning_Core/tree/master/application_examples/lsm6dsox | ||
|
||
import time | ||
from lsm6dsox import LSM6DSOX | ||
from machine import Pin, I2C | ||
|
||
INT_MODE = True # Run in interrupt mode. | ||
INT_FLAG = False # Set True on interrupt. | ||
|
||
|
||
def imu_int_handler(pin): | ||
global INT_FLAG | ||
INT_FLAG = True | ||
|
||
|
||
if INT_MODE == True: | ||
int_pin = Pin(24) | ||
int_pin.irq(handler=imu_int_handler, trigger=Pin.IRQ_RISING) | ||
|
||
i2c = I2C(0, scl=Pin(13), sda=Pin(12)) | ||
|
||
# Vibration detection example | ||
UCF_FILE = "lsm6dsox_vibration_monitoring.ucf" | ||
UCF_LABELS = {0: "no vibration", 1: "low vibration", 2: "high vibration"} | ||
# NOTE: Selected data rate and scale must match the MLC data rate and scale. | ||
lsm = LSM6DSOX(i2c, gyro_odr=26, accel_odr=26, gyro_scale=2000, accel_scale=4, ucf=UCF_FILE) | ||
|
||
# Head gestures example | ||
# UCF_FILE = "lsm6dsox_head_gestures.ucf" | ||
# UCF_LABELS = {0:"Nod", 1:"Shake", 2:"Stationary", 3:"Swing", 4:"Walk"} | ||
# NOTE: Selected data rate and scale must match the MLC data rate and scale. | ||
# lsm = LSM6DSOX(i2c, gyro_odr=26, accel_odr=26, gyro_scale=250, accel_scale=2, ucf=UCF_FILE) | ||
|
||
print("MLC configured...") | ||
|
||
while True: | ||
if INT_MODE: | ||
if INT_FLAG: | ||
INT_FLAG = False | ||
print(UCF_LABELS[lsm.read_mlc_output()[0]]) | ||
else: | ||
buf = lsm.read_mlc_output() | ||
if buf != None: | ||
print(UCF_LABELS[buf[0]]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"deploy": [ | ||
"../deploy.md" | ||
], | ||
"docs": "", | ||
"features": [ | ||
"Breadboard Friendly", | ||
"Castellated Pads", | ||
"WiFi Nina-W102", | ||
"Bluetooth Nina-W102", | ||
"IMU LSM6DSOXTR", | ||
"Crypto IC ATECC608A-MAHDA-T", | ||
"Microphone MP34DT05", | ||
"SPI Flash 16MB", | ||
"USB-MICRO" | ||
], | ||
"images": [ | ||
"ABX00052_01.iso_999x750.jpg" | ||
], | ||
"mcu": "RP2040", | ||
"product": "Arduino Nano RP2040 Connect", | ||
"thumbnail": "", | ||
"url": "https://store-usa.arduino.cc/products/arduino-nano-rp2040-connect", | ||
"vendor": "Arduino" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
include("$(PORT_DIR)/boards/manifest.py") | ||
freeze("$(MPY_DIR)/drivers/lsm6dsox/", "lsm6dsox.py") | ||
include( | ||
"$(MPY_LIB_DIR)/micropython/bluetooth/aioble/manifest.py", | ||
client=True, | ||
central=True, | ||
l2cap=True, | ||
security=True, | ||
) |
6 changes: 6 additions & 0 deletions
6
ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.cmake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# cmake file for Arduino Nano RP2040 Connect. | ||
set(MICROPY_PY_BLUETOOTH 1) | ||
set(MICROPY_BLUETOOTH_NIMBLE 1) | ||
set(MICROPY_PY_NETWORK_NINAW10 1) | ||
set(MICROPY_HW_ENABLE_DOUBLE_TAP 1) | ||
set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py) |
39 changes: 39 additions & 0 deletions
39
ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//Board config for Arduino Nano RP2040 Connect. | ||
|
||
// Board and hardware specific configuration | ||
#define MICROPY_HW_BOARD_NAME "Arduino Nano RP2040 Connect" | ||
#define MICROPY_HW_FLASH_STORAGE_BYTES (8 * 1024 * 1024) | ||
|
||
// Enable networking and sockets. | ||
#define MICROPY_PY_NETWORK (1) | ||
#define MICROPY_PY_USOCKET (1) | ||
|
||
// Enable USB Mass Storage with FatFS filesystem. | ||
#define MICROPY_HW_USB_MSC (1) | ||
#define MICROPY_HW_USB_VID (0x2341) | ||
#define MICROPY_HW_USB_PID (0x015e) | ||
iabdalkader marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// UART 1 config. | ||
#define MICROPY_HW_UART1_TX (8) | ||
#define MICROPY_HW_UART1_RX (9) | ||
#define MICROPY_HW_UART1_CTS (10) | ||
#define MICROPY_HW_UART1_RTS (11) | ||
|
||
// SPI 1 config. | ||
#define MICROPY_HW_SPI1_SCK (14) | ||
#define MICROPY_HW_SPI1_MOSI (11) | ||
#define MICROPY_HW_SPI1_MISO (8) | ||
|
||
// Bluetooth config. | ||
#define MICROPY_HW_BLE_UART_ID (1) | ||
#define MICROPY_HW_BLE_UART_BAUDRATE (119600) | ||
iabdalkader marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// WiFi/NINA-W10 config. | ||
#define MICROPY_HW_WIFI_SPI_ID (1) | ||
#define MICROPY_HW_WIFI_SPI_BAUDRATE (8 * 1000 * 1000) | ||
|
||
// ublox Nina-W10 module config. | ||
#define MICROPY_HW_NINA_RESET (3) | ||
#define MICROPY_HW_NINA_GPIO0 (2) | ||
#define MICROPY_HW_NINA_GPIO1 (9) | ||
#define MICROPY_HW_NINA_ACK (10) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.