8000 nrf: Add support for Arduino Nano 33 BLE board. · micropython/micropython@2767333 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2767333

Browse files
committed
nrf: Add support for Arduino Nano 33 BLE board.
1 parent 49c23ac commit 2767333

File tree

11 files changed

+602
-0
lines changed

11 files changed

+602
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Arduino SA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "nrf.h"
28+
#include "nrf_gpio.h"
29+
#include "nrf_rtc.h"
30+
31+
#define PIN_ENABLE_SENSORS_3V3 (22u)
32+
#define PIN_ENABLE_I2C_PULLUP (32u)
33+
#define DFU_MAGIC_SERIAL_ONLY_RESET (0xb0)
34+
35+
void NANO33_board_early_init(void) {
36+
// Errata Nano33BLE - I2C pullup is on SWO line, need to disable TRACE
37+
// was being enabled by nrfx_clock_anomaly_132
38+
CoreDebug->DEMCR = 0;
39+
NRF_CLOCK->TRACECONFIG = 0;
40+
41+
// Bootloader enables interrupt on COMPARE[0], which we don't handle
42+
// Disable it here to avoid getting stuck when OVERFLOW irq is triggered
43+
nrf_rtc_event_disable(NRF_RTC1, NRF_RTC_INT_COMPARE0_MASK);
44+
nrf_rtc_int_disable(NRF_RTC1, NRF_RTC_INT_COMPARE0_MASK);
45+
46+
// Always enable I2C pullup and power on startup
47+
// Change for maximum powersave
48+
nrf_gpio_cfg_output(PIN_ENABLE_SENSORS_3V3);
49+
nrf_gpio_cfg_output(PIN_ENABLE_I2C_PULLUP);
50+
51+
nrf_gpio_pin_set(PIN_ENABLE_SENSORS_3V3);
52+
nrf_gpio_pin_set(PIN_ENABLE_I2C_PULLUP);
53+
}
54+
55+
void NANO33_board_deinit(void) {
56+
nrf_gpio_cfg_output(PIN_ENABLE_SENSORS_3V3);
57+
nrf_gpio_cfg_output(PIN_ENABLE_I2C_PULLUP);
58+
59+
nrf_gpio_pin_clear(PIN_ENABLE_SENSORS_3V3);
60+
nrf_gpio_pin_clear(PIN_ENABLE_I2C_PULLUP);
61+
}
62+
63+
void NANO33_board_enter_bootloader(void) {
64+
__disable_irq();
65+
NRF_POWER->GPREGRET = DFU_MAGIC_SERIAL_ONLY_RESET;
66+
NVIC_SystemReset();
67+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"deploy": [
3+
"./deploy.md"
4+
],
5+
"docs": "",
6+
"features": [
7+
"Bluetooth 5.0",
8+
"IMU LSM9DS1",
9+
"Humidity sensor HTS221",
10+
"Pressure sensor LPS22H",
11+
"Proximity, Light, RGB sensor APDS-9960",
12+
"Microphone MPM3610",
13+
"Crypto IC ARM CC310",
14+
"USB-MICRO",
15+
"Breadboard Friendly"
16+
],
17+
"images": [
18+
"ABX00030_01.iso_1000x750.jpg"
19+
],
20+
"mcu": "nRF52840",
21+
"product": "Arduino Nano 33 BLE",
22+
"thumbnail": "",
23+
"url": "https://store.arduino.cc/products/arduino-nano-33-ble",
24+
"vendor": "Arduino"
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
### Update the bootloader
2+
3+
Before deploying any firmware, make sure you have the updated Arduino Nano 33 BLE bootloader, which relocates the bootloader so the softdevice doesn't overwrite it. Please see:
4+
5+
https://docs.arduino.cc/tutorials/nano-33-ble/getting-started-omv
6+
7+
### Via Arduino bootloader and BOSSA
8+
9+
Download BOSSA from https://github.com/shumatech/BOSSA/ and double tap reset button to enter the Arduino bootloader
10+
11+
```bash
12+
bossac -e -w --offset=0x16000 --port=ttyACM0 -i -d -U -R build-arduino_nano_33_ble-s140/firmware.bin
13+
```
14+
15+
Alternatively, a Linux binary can be found here: https://github.com/openmv/openmv/blob/master/tools/bossac
16+
17+
### Via nrfprog
18+
19+
This board can also be programmed via nrfjprog (with Jlink for example), from MicroPython source repository:
20+
21+
```bash
22+
make -j8 BOARD=arduino_nano_33_ble SD=s140 deploy
23+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include("$(PORT_DIR)/modules/manifest.py")
2+
freeze("$(BOARD_DIR)/modules")
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# This file is part of the OpenMV project.
2+
#
3+
# Copyright (c) 2013-2021 Ibrahim Abdelkader <iabdalkader@openmv.io>
4+
# Copyright (c) 2013-2021 Kwabena W. Agyeman <kwagyeman@openmv.io>
5+
#
6+
# This work is licensed under the MIT license, see the file LICENSE for details.
7+
#
8+
# HTS221 driver based on public domain driver.
9+
# Original source: https://github.com/ControlEverythingCommunity/HTS221/blob/master/Python/HTS221.py
10+
11+
import time
12+
import struct
13+
14+
15+
class HTS221:
16+
def __init__(self, i2c, data_rate=1, dev_addr=0x5F):
17+
self.bus = i2c
18+
self.odr = data_rate
19+
self.slv_addr = dev_addr
20+
21+
# Set configuration register
22+
# Humidity and temperature average configuration
23+
self.bus.writeto_mem(self.slv_addr, 0x10, b"\x1B")
24+
25+
# Set control register
26+
# PD | BDU | ODR
27+
cfg = 0x80 | 0x04 | (self.odr & 0x3)
28+
self.bus.writeto_mem(self.slv_addr, 0x20, bytes([cfg]))
29+
30+
# TODO needed ?
31+
time.sleep_ms(100)
32+
33+
# Read Calibration values from non-volatile memory of the device
34+
# Humidity Calibration values
35+
self.H0 = self.read_reg(0x30, 1) / 2
36+
self.H1 = self.read_reg(0x31, 1) / 2
37+
self.H2 = self.read_reg(0x36, 2)
38+
self.H3 = self.read_reg(0x3A, 2)
39+
40+
# Temperature Calibration values
41+
raw = self.read_reg(0x35, 1)
42+
self.T0 = ((raw & 0x03) * 256) + self.read_reg(0x32, 1)
43+
self.T1 = ((raw & 0x0C) * 64) + self.read_reg(0x33, 1)
44+
self.T2 = self.read_reg(0x3C, 2)
45+
self.T3 = self.read_reg(0x3E, 2)
46+
47+
def read_reg(self, reg_addr, size):
48+
fmt = "B" if size == 1 else "H"
49+
reg_addr = reg_addr if size == 1 else reg_addr | 0x80
50+
return struct.unpack(fmt, self.bus.readfrom_mem(self.slv_addr, reg_addr, size))[0]
51+
52+
def humidity(self):
53+
rH = self.read_reg(0x28, 2)
54+
return (self.H1 - self.H0) * (rH - self.H2) / (self.H3 - self.H2) + self.H0
55+
56+
def temperature(self):
57+
temp = self.read_reg(0x2A, 2)
58+
if temp > 32767:
59+
temp -= 65536
60+
return ((self.T1 - self.T0) / 8.0) * (temp - self.T2) / (self.T3 - self.T2) + (
61+
self.T0 / 8.0
62+
)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# LPS22HB/HH pressure seneor micropython drive
2+
# ver: 2.0
3+
# License: MIT
4+
# Author: shaoziyang (shaoziyang@micropython.org.cn)
5+
# v1.0 2016.4
6+
# v2.0 2019.7
7+
8+
9+
class LPS22H:
10+
LPS22_CTRL_REG1 = const(0x10)
11+
LPS22_CTRL_REG2 = const(0x11)
12+
LPS22_STATUS = const(0x27)
13+
LPS22_TEMP_OUT_L = const(0x2B)
14+
LPS22_PRESS_OUT_XL = const(0x28)
15+
LPS22_PRESS_OUT_L = const(0x29)
16+
17+
def __init__(self, i2c, addr=0x5C):
18+
self.i2c = i2c
19+
self.addr = addr
20+
self.tb = bytearray(1)
21+
self.rb = bytearray(1)
22+
self.oneshot = False
23+
self.irq_v = [0, 0]
24+
# ODR=1 EN_LPFP=1 BDU=1
25+
self.setreg(LPS22_CTRL_REG1, 0x1A)
26+
self.oneshot_mode(False)
27+
28+
def oneshot_mode(self, oneshot=None):
29+
if oneshot is None:
30+
return self.oneshot
31+
else:
32+
self.getreg(LPS22_CTRL_REG1)
33+
self.oneshot = oneshot
34+
if oneshot:
35+
self.rb[0] &= 0x0F
36+
else:
37+
self.rb[0] |= 0x10
38+
self.setreg(LPS22_CTRL_REG1, self.rb[0])
39+
40+
def int16(self, d):
41+
return d if d < 0x8000 else d - 0x10000
42+
43+
def setreg(self, reg, dat):
44+
self.tb[0] = dat
45+
self.i2c.writeto_mem(self.addr, reg, self.tb)
46+
47+
def getreg(self, reg):
48+
self.i2c.readfrom_mem_into(self.addr, reg, self.rb)
49+
return self.rb[0]
50+
51+
def get2reg(self, reg):
52+
return self.getreg(reg) + self.getreg(reg + 1) * 256
53+
54+
def ONE_SHOT(self, b):
55+
if self.oneshot:
56+
self.setreg(LPS22_CTRL_REG2, self.getreg(LPS22_CTRL_REG2) | 0x01)
57+
self.getreg(0x28 + b * 2)
58+
while 1:
59+
if self.getreg(LPS22_STATUS) & b:
60+
return
61+
62+
def temperature(self):
63+
self.ONE_SHOT(2)
64+
try:
65+
return self.int16(self.get2reg(LPS22_TEMP_OUT_L)) / 100
66+
except MemoryError:
67+
return self.temperature_irq()
68+
69+
def pressure(self):
70+
self.ONE_SHOT(1)
71+
try:
72+
return (self.getreg(LPS22_PRESS_OUT_XL) + self.get2reg(LPS22_PRESS_OUT_L) * 256) / 4096
73+
except MemoryError:
74+
return self.pressure_irq()
75+
76+
def altitude(self):
77+
return (
78+
(((1013.25 / self.pressure()) ** (1 / 5.257)) - 1.0)
79+
* (self.temperature() + 273.15)
80+
/ 0.0065
81+
)
82+
83+
def temperature_irq(self):
84+
self.ONE_SHOT(2)
85+
return self.int16(self.get2reg(LPS22_TEMP_OUT_L)) // 100
86+
87+
def pressure_irq(self):
88+
self.ONE_SHOT(1)
89+
return self.get2reg(LPS22_PRESS_OUT_L) >> 4
90+
91+
def get_irq(self):
92+
self.irq_v[0] = self.temperature_irq()
93+
self.irq_v[1] = self.pressure_irq()
94+
return self.irq_v

0 commit comments

Comments
 (0)
0