Skip to content

Commit 484b7b8

Browse files
authored
Merge pull request #476 from WattleFoxxo/xiao-rp2040
Adds support for the XIAO-RP2040 board
2 parents cb423bc + d23378c commit 484b7b8

File tree

5 files changed

+301
-0
lines changed

5 files changed

+301
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "XiaoRP2040Board.h"
2+
3+
#include <Arduino.h>
4+
#include <Wire.h>
5+
6+
void XiaoRP2040Board::begin() {
7+
// for future use, sub-classes SHOULD call this from their begin()
8+
startup_reason = BD_STARTUP_NORMAL;
9+
10+
#ifdef P_LORA_TX_LED
11+
pinMode(P_LORA_TX_LED, OUTPUT);
12+
#endif
13+
14+
#ifdef PIN_VBAT_READ
15+
pinMode(PIN_VBAT_READ, INPUT);
16+
#endif
17+
18+
#if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL)
19+
Wire.setSDA(PIN_BOARD_SDA);
20+
Wire.setSCL(PIN_BOARD_SCL);
21+
#endif
22+
23+
Wire.begin();
24+
25+
delay(10); // give sx1262 some time to power up
26+
}
27+
28+
bool XiaoRP2040Board::startOTAUpdate(const char *id, char reply[]) {
29+
return false;
30+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#pragma once
2+
3+
#include <Arduino.h>
4+
#include <MeshCore.h>
5+
6+
// LoRa radio module pins for the Xiao RP2040
7+
// https://wiki.seeedstudio.com/XIAO-RP2040/
8+
9+
#define P_LORA_DIO_1 27 // D1
10+
#define P_LORA_NSS 6 // D4
11+
#define P_LORA_RESET 28 // D2
12+
#define P_LORA_BUSY 29 // D3
13+
#define P_LORA_TX_LED 17
14+
15+
#define SX126X_RXEN 7 // D5
16+
#define SX126X_TXEN -1
17+
18+
#define SX126X_DIO2_AS_RF_SWITCH true
19+
#define SX126X_DIO3_TCXO_VOLTAGE 1.8
20+
21+
/*
22+
* This board has no built-in way to read battery voltage.
23+
* Nevertheless it's very easy to make it work, you only require two 1% resistors.
24+
* If your using the WIO SX1262 Addon for xaio, make sure you dont connect D0!
25+
*
26+
* BAT+ -----+
27+
* |
28+
* VSYS --+ -/\/\/\/\- --+
29+
* 200k |
30+
* +-- D0
31+
* |
32+
* GND --+ -/\/\/\/\- --+
33+
* | 100k
34+
* BAT- -----+
35+
*/
36+
#define PIN_VBAT_READ 26 // D0
37+
#define BATTERY_SAMPLES 8
38+
#define ADC_MULTIPLIER (3.0f * 3.3f * 1000)
39+
40+
class XiaoRP2040Board : public mesh::MainBoard {
41+
protected:
42+
uint8_t startup_reason;
43+
44+
public:
45+
void begin();
46+
uint8_t getStartupReason() const override { return startup_reason; }
47+
48+
#ifdef P_LORA_TX_LED
49+
void onBeforeTransmit() override { digitalWrite(P_LORA_TX_LED, HIGH); }
50+
void onAfterTransmit() override { digitalWrite(P_LORA_TX_LED, LOW); }
51+
#endif
52+
53+
54+
uint16_t getBattMilliVolts() override {
55+
#if defined(PIN_VBAT_READ) && defined(ADC_MULTIPLIER)
56+
analogReadResolution(12);
57+
58+
uint32_t raw = 0;
59+
for (int i = 0; i < BATTERY_SAMPLES; i++) {
60+
raw += analogRead(PIN_VBAT_READ);
61+
}
62+
raw = raw / BATTERY_SAMPLES;
63+
64+
return (ADC_MULTIPLIER * raw) / 4096;
65+
#else
66+
return 0;
67+
#endif
68+
}
69+
70+
const char *getManufacturerName() const override { return "Xiao RP2040"; }
71+
72+
void reboot() override { rp2040.reboot(); }
73+
74+
bool startOTAUpdate(const char *id, char reply[]) override;
75+
};
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
[Xiao_rp2040]
2+
extends = rp2040_base
3+
4+
board = seeed_xiao_rp2040
5+
board_build.filesystem_size = 0.5m
6+
7+
build_flags = ${rp2040_base.build_flags}
8+
-I variants/xiao_rp2040
9+
-D SX126X_CURRENT_LIMIT=140
10+
-D RADIO_CLASS=CustomSX1262
11+
-D WRAPPER_CLASS=CustomSX1262Wrapper
12+
-D LORA_TX_POWER=22
13+
-D SX126X_RX_BOOSTED_GAIN=1
14+
; Debug options
15+
; -D DEBUG_RP2040_WIRE=1
16+
; -D DEBUG_RP2040_SPI=1
17+
; -D DEBUG_RP2040_CORE=1
18+
; -D RADIOLIB_DEBUG_SPI=1
19+
; -D DEBUG_RP2040_PORT=Serial
20+
21+
build_src_filter = ${rp2040_base.build_src_filter}
22+
+<helpers/rp2040/XiaoRP2040Board.cpp>
23+
+<../variants/xiao_rp2040>
24+
25+
lib_deps = ${rp2040_base.lib_deps}
26+
27+
[env:Xiao_rp2040_Repeater]
28+
extends = Xiao_rp2040
29+
build_flags = ${Xiao_rp2040.build_flags}
30+
-D ADVERT_NAME='"Xiao Repeater"'
31+
-D ADVERT_LAT=0.0
32+
-D ADVERT_LON=0.0
33+
-D ADMIN_PASSWORD='"password"'
34+
-D MAX_NEIGHBOURS=8
35+
-D MESH_PACKET_LOGGING=1
36+
-D MESH_DEBUG=1
37+
build_src_filter = ${Xiao_rp2040.build_src_filter}
38+
+<../examples/simple_repeater>
39+
40+
[env:Xiao_rp2040_room_server]
41+
extends = Xiao_rp2040
42+
build_flags = ${Xiao_rp2040.build_flags}
43+
-D ADVERT_NAME='"Xiao Room"'
44+
-D ADVERT_LAT=0.0
45+
-D ADVERT_LON=0.0
46+
-D ADMIN_PASSWORD='"password"'
47+
-D ROOM_PASSWORD='"hello"'
48+
; -D MESH_PACKET_LOGGING=1
49+
; -D MESH_DEBUG=1
50+
build_src_filter = ${Xiao_rp2040.build_src_filter}
51+
+<../examples/simple_room_server>
52+
53+
[env:Xiao_rp2040_companion_radio_usb]
54+
extends = Xiao_rp2040
55+
build_flags = ${Xiao_rp2040.build_flags}
56+
-D MAX_CONTACTS=100
57+
-D MAX_GROUP_CHANNELS=8
58+
; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1
59+
; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1
60+
build_src_filter = ${Xiao_rp2040.build_src_filter}
61+
+<../examples/companion_radio>
62+
lib_deps = ${Xiao_rp2040.lib_deps}
63+
densaugeo/base64 @ ~1.4.0
64+
65+
; [env:Xiao_rp2040_companion_radio_ble]
66+
; extends = Xiao_rp2040
67+
; build_flags = ${Xiao_rp2040.build_flags}
68+
; -D MAX_CONTACTS=100
69+
; -D MAX_GROUP_CHANNELS=8
70+
; -D BLE_PIN_CODE=123456
71+
; -D BLE_DEBUG_LOGGING=1
72+
; ; -D MESH_PACKET_LOGGING=1
73+
; ; -D MESH_DEBUG=1
74+
; build_src_filter = ${Xiao_rp2040.build_src_filter}
75+
; +<../examples/companion_radio>
76+
; lib_deps = ${Xiao_rp2040.lib_deps}
77+
; densaugeo/base64 @ ~1.4.0
78+
79+
; [env:Xiao_rp2040_companion_radio_wifi]
80+
; extends = Xiao_rp2040
81+
; build_flags = ${Xiao_rp2040.build_flags}
82+
; -D MAX_CONTACTS=100
83+
; -D MAX_GROUP_CHANNELS=8
84+
; -D WIFI_DEBUG_LOGGING=1
85+
; -D WIFI_SSID='"myssid"'
86+
; -D WIFI_PWD='"mypwd"'
87+
; ; -D MESH_PACKET_LOGGING=1
88+
; ; -D MESH_DEBUG=1
89+
; build_src_filter = ${Xiao_rp2040.build_src_filter}
90+
; +<../examples/companion_radio>
91+
; lib_deps = ${Xiao_rp2040.lib_deps}
92+
; densaugeo/base64 @ ~1.4.0
93+
94+
[env:Xiao_rp2040_terminal_chat]
95+
extends = Xiao_rp2040
96+
build_flags = ${Xiao_rp2040.build_flags}
97+
-D MAX_CONTACTS=100
98+
-D MAX_GROUP_CHANNELS=1
99+
; -D MESH_PACKET_LOGGING=1
100+
; -D MESH_DEBUG=1
101+
build_src_filter = ${Xiao_rp2040.build_src_filter}
102+
+<../examples/simple_secure_chat/main.cpp>
103+
lib_deps = ${Xiao_rp2040.lib_deps}
104+
densaugeo/base64 @ ~1.4.0

variants/xiao_rp2040/target.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "target.h"
2+
3+
#include <Arduino.h>
4+
#include <helpers/ArduinoHelpers.h>
5+
6+
XiaoRP2040Board board;
7+
8+
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY);
9+
10+
WRAPPER_CLASS radio_driver(radio, board);
11+
12+
VolatileRTCClock fallback_clock;
13+
AutoDiscoverRTCClock rtc_clock(fallback_clock);
14+
SensorManager sensors;
15+
16+
#ifndef LORA_CR
17+
#define LORA_CR 5
18+
#endif
19+
20+
bool radio_init() {
21+
rtc_clock.begin(Wire);
22+
23+
#ifdef SX126X_DIO3_TCXO_VOLTAGE
24+
float tcxo = SX126X_DIO3_TCXO_VOLTAGE;
25+
#else
26+
float tcxo = 1.6f;
27+
#endif
28+
int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo);
29+
30+
if (status != RADIOLIB_ERR_NONE) {
31+
Serial.print("ERROR: radio init failed: ");
32+
Serial.println(status);
33+
return false; // fail
34+
}
35+
36+
radio.setCRC(1);
37+
38+
#ifdef SX126X_CURRENT_LIMIT
39+
radio.setCurrentLimit(SX126X_CURRENT_LIMIT);
40+
#endif
41+
42+
#ifdef SX126X_DIO2_AS_RF_SWITCH
43+
radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH);
44+
#endif
45+
46+
#ifdef SX126X_RX_BOOSTED_GAIN
47+
radio.setRxBoostedGainMode(SX126X_RX_BOOSTED_GAIN);
48+
#endif
49+
50+
return true; // success
51+
}
52+
53+
uint32_t radio_get_rng_seed() {
54+
return radio.random(0x7FFFFFFF);
55+
}
56+
57+
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
58+
radio.setFrequency(freq);
59+
radio.setSpreadingFactor(sf);
60+
radio.setBandwidth(bw);
61+
radio.setCodingRate(cr);
62+
}
63+
64+
void radio_set_tx_power(uint8_t dbm) {
65+
radio.setOutputPower(dbm);
66+
}
67+
68+
mesh::LocalIdentity radio_new_identity() {
69+
RadioNoiseListener rng(radio);
70+
return mesh::LocalIdentity(&rng); // create new random identity
71+
}

variants/xiao_rp2040/target.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#define RADIOLIB_STATIC_ONLY 1
4+
5+
#include <RadioLib.h>
6+
#include <helpers/AutoDiscoverRTCClock.h>
7+
#include <helpers/CustomSX1262Wrapper.h>
8+
#include <helpers/RadioLibWrappers.h>
9+
#include <helpers/SensorManager.h>
10+
#include <helpers/rp2040/XiaoRP2040Board.h>
11+
12+
extern XiaoRP2040Board board;
13+
extern WRAPPER_CLASS radio_driver;
14+
extern AutoDiscoverRTCClock rtc_clock;
15+
extern SensorManager sensors;
16+
17+
bool radio_init();
18+
uint32_t radio_get_rng_seed();
19+
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr);
20+
void radio_set_tx_power(uint8_t dbm);
21+
mesh::LocalIdentity radio_new_identity();

0 commit comments

Comments
 (0)