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

Skip to content

Commit 43375a8

Browse files
committed
nrf: Add support for Arduino Nano 33 BLE board.
1 parent e22b7fb commit 43375a8

File tree

11 files changed

+635
-0
lines changed

11 files changed

+635
-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 Mi 8000 croPython 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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2013-2022 Ibrahim Abdelkader <iabdalkader@openmv.io>
5+
Copyright (c) 2013-2022 Kwabena W. Agyeman <kwagyeman@openmv.io>
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
HTS221 driver driver for MicroPython.
26+
Original source: https://github.com/ControlEverythingCommunity/HTS221/blob/master/Python/HTS221.py
27+
"""
28+
29+
import struct
30+
31+
32+
class HTS221:
33+
def __init__(self, i2c, data_rate=1, dev_addr=0x5F):
34+
self.bus = i2c
35+
self.odr = data_rate
36+
self.slv_addr = dev_addr
37+
38+
# Set configuration register
39+
# Humidity and temperature average configuration
40+
self.bus.writeto_mem(self.slv_addr, 0x10, b"\x1B")
41+
42+
# Set control register
43+
# PD | BDU | ODR
44+
cfg = 0x80 | 0x04 | (self.odr & 0x3)
45+
self.bus.writeto_mem(self.slv_addr, 0x20, bytes([cfg]))
46+
47+
# Read Calibration values from non-volatile memory of the device
48+
# Humidity Calibration values
49+
self.H0 = self._read_reg(0x30, 1) / 2
50+
self.H1 = self._read_reg(0x31, 1) / 2
51+
self.H2 = self._read_reg(0x36, 2)
52+
self.H3 = self._read_reg(0x3A, 2)
53+
54+
# Temperature Calibration values
55+
raw = self._read_reg(0x35, 1)
56+
self.T0 = ((raw & 0x03) * 256) + self._read_reg(0x32, 1)
57+
self.T1 = ((raw & 0x0C) * 64) + self._read_reg(0x33, 1)
58+
self.T2 = self._read_reg(0x3C, 2)
59+
self.T3 = self._read_reg(0x3E, 2)
60+
61+
def _read_reg(self, reg_addr, size):
62+
fmt = "B" if size == 1 else "H"
63+
reg_addr = reg_addr if size == 1 else reg_addr | 0x80
64+
return struct.unpack(fmt, self.bus.readfrom_mem(self.slv_addr, reg_addr, size))[0]
65+
66+
def humidity(self):
67+
rH = self._read_reg(0x28, 2)
68+
return (self.H1 - self.H0) * (rH - self.H2) / (self.H3 - self.H2) + self.H0
69+
70+
def temperature(self):
71+
temp = self._read_reg(0x2A, 2)
72+
if temp > 32767:
73+
temp -= 65536
74+
return ((self.T1 - self.T0) / 8.0) * (temp - self.T2) / (self.T3 - self.T2) + (
75+
self.T0 / 8.0
76+
)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
"""
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2016-2019 shaoziyang <shaoziyang@micropython.org.cn>
5+
Copyright (c) 2022 Ibrahim Abdelkader <iabdalkader@openmv.io>
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
LPS22HB/HH pressure sensor driver for MicroPython.
26+
Basic example usage:
27+
28+
import time
29+
from lps22h import LPS22H
30+
from machine import Pin, I2C
31+
32+
bus = I2C(1, scl=Pin(15), sda=Pin(14))
33+
lps = LPS22H(bus, oneshot=False)
34+
35+
while (True):
36+
print("Pressure: %.2f hPa Temperature: %.2f C"%(lps.pressure(), lps.temperature()))
37+
time.sleep_ms(10)
38+
"""
39+
import machine
40+
41+
_LPS22_CTRL_REG1 = const(0x10)
42+
_LPS22_CTRL_REG2 = const(0x11)
43+
_LPS22_STATUS = const(0x27)
44+
_LPS22_TEMP_OUT_L = const(0x2B)
45+
_LPS22_PRESS_OUT_XL = const(0x28)
46+
_LPS22_PRESS_OUT_L = const(0x29)
47+
48+
49+
class LPS22H:
50+
def __init__(self, i2c, addr=0x5C, oneshot=False):
51+
self.i2c = i2c
52+
self.addr = addr
53+
self.oneshot = oneshot
54+
self.buf = bytearray(1)
55+
56+
if hasattr(machine, "idle"):
57+
self._go_idle = lambda: machine.idle()
58+
else:
59+
import time
60+
61+
self._go_idle = lambda: time.sleep_ms(1)
62+
63+
# ODR=1 EN_LPFP=1 BDU=1
64+
self._write_reg(_LPS22_CTRL_REG1, 0x1A)
65+
self.set_oneshot_mode(self.oneshot)
66+
67+
def _int16(self, d):
68+
return d if d < 0x8000 else d - 0x10000
69+
70+
def _write_reg(self, reg, dat):
71+
self.buf[0] = dat
72+
self.i2c.writeto_mem(self.addr, reg, self.buf)
73+
74+
def _read_reg(self, reg, width=8):
75+
self.i2c.readfrom_mem_into(self.addr, reg, self.buf)
76+
val = self.buf[0]
77+
if width == 16:
78+
val |= self._read_reg(reg + 1) << 8
79+
return val
80+
81+
def _tigger_oneshot(self, b):
82+
if self.oneshot:
83+
self._write_reg(_LPS22_CTRL_REG2, self._read_reg(_LPS22_CTRL_REG2) | 0x01)
84+
self._read_reg(0x28 + b * 2)
85+
while True:
86+
if self._read_reg(_LPS22_STATUS) & b:
87+
return
88+
self._go_idle()
89+
90+
def set_oneshot_mode(self, oneshot):
91+
self._read_reg(_LPS22_CTRL_REG1)
92+
self.oneshot = oneshot
93+
if oneshot:
94+
self.buf[0] &= 0x0F
95+
else:
96+
self.buf[0] |= 0x10
97+
self._write_reg(_LPS22_CTRL_REG1, self.buf[0])
< A09A /td>98+
99+
def pressure(self):
100+
if self.oneshot:
101+
self._tigger_oneshot(1)
102+
return (
103+
self._read_reg(_LPS22_PRESS_OUT_XL) + self._read_reg(_LPS22_PRESS_OUT_L, 16) * 256
104+
) / 4096
105+
106+
def temperature(self):
107+
if self.oneshot:
108+
self._tigger_oneshot(2)
109+
return self._int16(self._read_reg(_LPS22_TEMP_OUT_L, 16)) / 100
110+
111+
def altitude(self):
112+
return (
113+
(((1013.25 / self.pressure()) ** (1 / 5.257)) - 1.0)
114+
* (self.temperature() + 273.15)
115+
/ 0.0065
116+
)

0 commit comments

Comments
 (0)
0