Compare commits

...

93 Commits

Author SHA1 Message Date
Matthias Wientapper
3b9315df8d platformio.ini: Adjust defaults for LoRa frequncies and advert interval limits 2026-01-26 12:50:18 +01:00
Matthias Wientapper
bd10790c4a Integration of upstrem PR #1297 2026-01-26 12:50:18 +01:00
Matthias Wientapper
60b72cc11b Integration of upstrem PR #1338 2026-01-26 12:50:18 +01:00
Matthias Wientapper
3a16bd3f9d Merge branch 'evo-build-scripts' into meshcore-evo_20260126 2026-01-26 12:47:53 +01:00
Matthias Wientapper
4a9137bf00 Remove PR-1199 as its functionality is part of dev now 2026-01-26 12:47:35 +01:00
Matthias Wientapper
ef5ac8177f Merge branch 'evo-build-scripts' into meshcore-evo_20260126 2026-01-26 12:46:23 +01:00
Matthias Wientapper
864a0c0421 Set default to EU/UK narrow 2026-01-26 12:45:45 +01:00
Matthias Wientapper
4bcbd54964 Merge branch 'dev' into meshcore-evo 2026-01-26 12:16:06 +01:00
ripplebiz
f1be7d0914 Merge pull request #1488 from liamcottle/firmware/boot-adverts
Change advert on boot from flood to zero hop
2026-01-26 21:55:48 +11:00
Matthias Wientapper
cb11809dff Merge branch 'dev' into meshcore-evo 2026-01-26 11:10:20 +01:00
Matthias Wientapper
58decb74b8 Fix fetching same PR twice under wrong name 2026-01-26 11:09:03 +01:00
liamcottle
7e24bd00b9 increase maximum flood advert interval to 168 hours (7 days) 2026-01-26 23:05:10 +13:00
liamcottle
d13bc446de added build flag to enable/disable boot advert 2026-01-26 22:39:39 +13:00
Matthias Wientapper
f9f177522b Add cli config flood.advert.base
0 = forwarding flood adverts off
1 = forwarding flood adverts on (unrestricted)
0.308 (default) = prob. forwarding according to #1338
2026-01-26 10:35:31 +01:00
Matthias Wientapper
6d3345c50f Limit flood advert packet forwarding for roomservers as well 2026-01-26 10:35:31 +01:00
Matthias Wientapper
bd4c4cf69d Limit flood advert packet forwarding, implements #1223 2026-01-26 10:35:31 +01:00
liamcottle
ed589f9620 boot adverts are now zero hop instead of flood 2026-01-26 22:20:36 +13:00
ripplebiz
4b7684c7df Merge pull request #1477 from Cisien/dev
Expose a counter to track RadioLib receive errors
2026-01-26 19:04:48 +11:00
Scott Powell
7ae164217c * region names now don't need '#' prefix. (SHA still adds a '#' for back compat) 2026-01-25 18:35:55 +11:00
Chris
c16bcd2fe3 Expose a counter to track RadioLib receive errors
This change counts when readData returns an err code other than RADIOLIB_ERR_NONE. In most cases this is going to be a CRC error. This counter is exposed in the `stats-packets` command, and in the repeater stats payload (4 additional bytes to the payload, which is now 56 bytes with this change. My incompetent robot claims the total payload size is 96 bytes (unverified but probably close).
2026-01-24 20:06:29 -08:00
ripplebiz
a5f3766016 Merge pull request #1429 from Snayler/dev
Fix Serial and TX LED not working on Heltec Wireless Paper V1.2
2026-01-25 14:58:07 +11:00
ripplebiz
f0269c9bff Merge pull request #1465 from recrof/rak3112-port
initial RAK 3112 support
2026-01-25 14:56:17 +11:00
ripplebiz
153bcdc6a3 Merge pull request #1457 from oltaco/remote-set-prvkey
Allow set prv.key over LoRa, clear ACL and validate key
2026-01-25 14:46:41 +11:00
taco
96ef5e5efe allow set prv.key from remote, validate new prv.key 2026-01-25 01:32:48 +11:00
taco
988287bfd7 recalc ClientACL shared_secrets at startup 2026-01-25 01:32:44 +11:00
taco
6336bd5b72 refactor ClientACL and CommonCLI, add ClientACL::clear() 2026-01-25 01:31:53 +11:00
Scott Powell
f46f0d0ed1 * WIO tracker l1: BLE companion. default node name now MAC address 2026-01-24 22:08:05 +11:00
ripplebiz
c7b3d34963 Merge pull request #1456 from Quency-D/fix-env-i2c
Fix env i2c errors
2026-01-24 21:58:14 +11:00
ripplebiz
e744adfa39 Merge pull request #1413 from entr0p1/powermgt-nrf52840-v2
nRF52840 Power Management v2 Phase 1 - LPCOMP Wake and startup lockout
2026-01-24 21:51:06 +11:00
Liam Cottle
b853c7ced5 Merge pull request #1459 from oltaco/fix-roomserver-debug
Build fix for room server with MESH_DEBUG
2026-01-24 19:31:31 +13:00
Rastislav Vysoky
266f6ee856 fixed battery measurement 2026-01-23 23:35:00 +01:00
Rastislav Vysoky
e7c72c5c6a initial port of rak3112 2026-01-23 22:26:24 +01:00
taco
9dd52bd0cc build fix for room server with MESH_DEBUG=1 2026-01-23 23:43:05 +11:00
entr0p1
1f59e52880 nRF52840 Power Management - Phase 1 - Boot Low VBAT Voltage Lockout
Added NRF52840 power management core functionality:
- Boot‑voltage lockout
- Initial support for shutdown/reset reason storage and capture (via RESETREAS/GPREGRET2)
- LPCOMP wake (for voltage-driven shutdowns)
- VBUS wake (for voltage-driven shutdowns)
- Per-board shutdown handler for board-specific tasks
- Exposed CLI queries for power‑management status in CommonCLI.cpp
- Added documentation in docs/nrf52_power_management.md.
- Enabled power management support in Xiao nRF52840, RAK4631, Heltec T114 boards
2026-01-23 17:18:41 +11:00
Scott Powell
3c27132914 * T1000e BLE - default node name is now the MAC address 2026-01-23 15:53:58 +11:00
Quency-D
fc61018d4d Fix the issue of inconsistent I2C usage in the environmental sensor. 2026-01-23 10:45:13 +08:00
ripplebiz
616eb57b16 Merge pull request #1428 from etienn01/update-t114-i2c
Update T114 I2C pins for external RTC
2026-01-23 12:25:07 +11:00
ripplebiz
537acd7ea1 Merge pull request #1437 from nakoeppen/dev
Remove _serial->isConnected() logic from buzzer notifications
2026-01-23 12:20:57 +11:00
ripplebiz
32230f6167 Merge pull request #1415 from WattleFoxxo/StationG2-tx-power-changes
Change the Station G2's default TX power
2026-01-23 11:58:53 +11:00
Liam Cottle
bccefd6e37 Merge pull request #1445 from oltaco/thinknode_m1-gps-fix
ThinkNode M1 GPS fixes
2026-01-22 20:02:41 +13:00
taco
36f230d074 thinknode m1: allow GPS to sync clock 2026-01-22 14:42:43 +11:00
taco
ea85486dca thinknode m1: add missing GPS page to new UI 2026-01-22 14:42:08 +11:00
taco
b09ddfc5e1 thinknode m1: add missing getLocationProvider() override 2026-01-22 14:41:07 +11:00
Matthias Wientapper
e6cab77670 Add scripts to help automate the fw build process 2026-01-21 09:54:56 +01:00
nakoeppen
d68bc74514 Remove _serial->isConnected() logic from buzzer notifications 2026-01-20 20:19:10 -06:00
Miguel de Matos
a7cadc8e44 Fix Serial and TX LED not working on Heltec Wireless Paper V1.2
As described on #1276, tested and working on my heltec wireless paper v1.2
2026-01-20 01:52:45 +00:00
Étienne Fesser
e51a2d1ba0 Update T114 I2C pins 2026-01-19 21:39:01 +01:00
ripplebiz
56ab59ded2 Merge pull request #1387 from chrisdavis2110/rak3401
Add Variant rak3401 (for new 1W booster kit)
2026-01-19 16:11:07 +11:00
ripplebiz
bf0777845a Merge pull request #1408 from oltaco/improved-contact-mgmt
Contact management tweaks
2026-01-19 12:29:52 +11:00
chrisdavis2110
ed5d2909fc updated variant rak3401 2026-01-17 22:54:20 -08:00
Chris Davis
5e4b33a1a0 Merge pull request #4 from chrisdavis2110/var-rak3401
Var rak3401
2026-01-17 22:47:25 -08:00
WattleFoxxo
5c7b28f110 Change the Station G2 default tx power
set the default TX power to 7dBm to avoid illegal power output by default.
2026-01-18 14:29:50 +11:00
Matthias Wientapper
7682f1085c Merge branch 'dev' of github.com:meshcore-dev/MeshCore into meshcore-evo 2026-01-17 14:08:39 +01:00
taco
b919119faf only write contacts when changed 2026-01-16 13:15:35 +11:00
taco
c61fde9328 always send PUSH_CODE_NEW_ADVERT when advert was not added to contacts[] 2026-01-16 13:15:35 +11:00
Liam Cottle
7d1f52252b Merge pull request #1402 from recrof/v3-usb-contact-fix
fix: bump max contacts for heltec v3 companion usb
2026-01-16 09:50:06 +13:00
Rastislav Vysoky
11565673c3 fix: bump max contacts for v3 companion usb 2026-01-15 15:39:44 +01:00
Liam Cottle
23f1f2a3fa Merge pull request #1399 from mannkind/patch-1
Fix Ikoka Stick builds
2026-01-15 23:32:34 +13:00
ripplebiz
d41a968d1d Merge pull request #1379 from oltaco/improved-contact-mgmt
Companion: Improved Contact Management
2026-01-15 20:36:28 +11:00
taco
df6687034a bootstrap RTC from contact.lastmod and improve slot overwrite logic
slot overwrite logic can now safely use contact.lastmod to find oldest contact for overwrite
2026-01-15 18:01:20 +11:00
taco
741564dd48 refactor: add populateContactFromAdvert() 2026-01-15 18:01:20 +11:00
taco
403ce1db08 contacts: granular autoadd and overwrite-oldest 2026-01-15 18:01:20 +11:00
Dustin Brewer
31f98bdd43 Fix Ikoka Stick builds 2026-01-14 17:53:42 -08:00
Liam Cottle
56eb5b0499 Merge pull request #1373 from liquidraver/buildwithoutdebug
DISABLE_DEBUG=1 env variable to build.sh
2026-01-15 06:36:00 +13:00
Matthias Wientapper
4fd7aa6ce8 Merge branch 'dev' into meshcore-evo 2026-01-13 23:21:57 +01:00
chrisdavis2110
06c4ca19ab added variant rak3401 2026-01-13 10:06:50 -08:00
liquidraver
a48b185189 DISABLE_DEBUG=1 env variable to build.sh 2026-01-13 12:48:53 +01:00
Matthias Wientapper
94d44eb47c Merge branch 'dev' into meshcore-evo 2026-01-10 20:37:27 +01:00
Matthias Wientapper
f8f9cddb47 Integrate pending PRs from upstream repository 2026-01-09 23:23:39 +01:00
ViezeVingertjes
eb4fa032ff Implement token bucket duty cycle enforcement 2026-01-04 21:33:46 +01:00
Liam Cottle
6b52fb3230 Merge pull request #1310 from LitBomb/patch-22
fix Station G2 output dBm typo
2026-01-03 19:39:31 +13:00
uncle lit
a93527a474 fix Station G2 output dBm typo
fix Station G2 output dBm typo reported on https://github.com/meshcore-dev/MeshCore/issues/1304

changed 26.5 dBm to 36.5 dBm
2026-01-02 22:34:10 -08:00
ripplebiz
9b08a9bd93 Merge pull request #1260 from LitBomb/patch-21
Update FAQ with new community projects and tx power settings for amped radios
2025-12-29 13:44:38 +11:00
uncle lit
27c92d2fe9 Update FAQ with new MeshCore applications and tx power settings for amped radios
Added entries for meshcore-pi and pyMC_Repeater to the FAQ
Added tx power settings for amped radios
2025-12-21 21:48:56 -08:00
Liam Cottle
2228214ded Merge pull request #1216 from mattzzw/main
Update faq.md
2025-12-15 18:18:00 +13:00
mattzzw
2bcc9c10d2 Update faq.md
Fix typo
2025-12-14 18:29:49 +01:00
ripplebiz
922e378be5 Merge pull request #1192 from LitBomb/patch-20
Update faq.md
2025-12-11 10:21:19 +11:00
uncle lit
1f5659dd26 Update faq.md
fix typo bugs found by @4np
2025-12-08 09:33:10 -08:00
uncle lit
cae37d8892 Update faq.md
add get and set prv.key
add web site to generate new private key and specific its public key's first byte value
add link to repeater observer instruction
add links to The Comms Channel's meshcore video, MCarper's Meshcore Advantages, and Austin Mesh's MeshCore vs Meshtastic comparison
add deafness instruction for agc reset interval
add reference to Liam's Windows and Intel Mac client apps
add reference to Tree's Meshcore packet decoder
add OTA BLE update addendum for Seeed Wio Tracker L1 Pro
add instruction to use T-deck's software keyboard to enter `=` at the end of the base64 public key
2025-12-07 22:31:54 -08:00
Scott Powell
6d3219329f Merge branch 'dev' 2025-11-30 18:32:49 +11:00
Scott Powell
9405e8bee3 Merge branch 'dev'
# Conflicts:
#	docs/payloads.md
2025-11-13 20:47:52 +11:00
fdlamotte
8b68b5a689 Update README.md (RAK boards don't need pio patch) 2025-11-12 16:14:57 +01:00
Liam Cottle
b2dcb06197 Merge pull request #809 from tekstrand/fixup
Change source of truth to this repo, remove whitespace
2025-10-19 12:07:53 +13:00
ripplebiz
da5dbcd274 Merge pull request #871 from spacepc-de/fix-debug-log-field
Fix debug log: use c->extra.room.push_failures instead of c->push_failures
2025-10-07 09:45:11 +11:00
tekstrand
3e3fa5b443 trim trailing whitespace, clarify repeater gps, remove outdated instructions 2025-10-04 10:54:24 -05:00
Scott Powell
f5f5886327 Merge branch 'dev' 2025-10-02 12:52:48 +10:00
Jonathan Stöcklmayer
6ee0b85195 Fix debug log: use c->extra.room.push_failures instead of non-existent c->push_failures 2025-10-01 09:50:41 +02:00
ripplebiz
86225cd24a Merge pull request #869 from LitBomb/patch-19
Update faq.md
2025-10-01 13:46:44 +10:00
uncle lit
f594f2c7e6 Update faq.md
added pyMC_core to meshcore projects
mentioned Cisien's meshcoretomqtt fork from Andrew-a-g
updated Coding Rate explanation and recommendation
updated radio presets and added how to update presets listed in the app
2025-09-30 16:01:11 -07:00
Liam Cottle
3dc04deabf Merge pull request #837 from silverphish-io/typo-fix
Typo fix
2025-09-29 10:42:23 +13:00
ripplebiz
c8a6bcf57f Update README.md 2025-09-28 21:43:30 +10:00
silverphish-io
4e886bfa90 Typo fix in faq and payloads 2025-09-25 15:01:39 +01:00
silverphish-io
816d4e2fa3 Update faq.md 2025-09-25 14:59:25 +01:00
81 changed files with 2693 additions and 313 deletions

View File

@@ -97,10 +97,10 @@ Here are some general principals you should try to adhere to:
There are a number of fairly major features in the pipeline, with no particular time-frames attached yet. In very rough chronological order: There are a number of fairly major features in the pipeline, with no particular time-frames attached yet. In very rough chronological order:
- [X] Companion radio: UI redesign - [X] Companion radio: UI redesign
- [ ] Repeater + Room Server: add ACL's (like Sensor Node has) - [X] Repeater + Room Server: add ACL's (like Sensor Node has)
- [ ] Standardise Bridge mode for repeaters - [X] Standardise Bridge mode for repeaters
- [ ] Repeater/Bridge: Standardise the Transport Codes for zoning/filtering - [ ] Repeater/Bridge: Standardise the Transport Codes for zoning/filtering
- [ ] Core + Repeater: enhanced zero-hop neighbour discovery - [X] Core + Repeater: enhanced zero-hop neighbour discovery
- [ ] Core: round-trip manual path support - [ ] Core: round-trip manual path support
- [ ] Companion + Apps: support for multiple sub-meshes (and 'off-grid' client repeat mode) - [ ] Companion + Apps: support for multiple sub-meshes (and 'off-grid' client repeat mode)
- [ ] Core + Apps: support for LZW message compression - [ ] Core + Apps: support for LZW message compression
@@ -113,12 +113,3 @@ There are a number of fairly major features in the pipeline, with no particular
- Report bugs and request features on the [GitHub Issues](https://github.com/ripplebiz/MeshCore/issues) page. - Report bugs and request features on the [GitHub Issues](https://github.com/ripplebiz/MeshCore/issues) page.
- Find additional guides and components on [my site](https://buymeacoffee.com/ripplebiz). - Find additional guides and components on [my site](https://buymeacoffee.com/ripplebiz).
- Join [MeshCore Discord](https://discord.gg/BMwCtwHj5V) to chat with the developers and get help from the community. - Join [MeshCore Discord](https://discord.gg/BMwCtwHj5V) to chat with the developers and get help from the community.
## RAK Wireless Board Support in PlatformIO
Before building/flashing the RAK4631 targets in this project, there is, unfortunately, some patching you have to do to your platformIO packages to make it work. There is a guide here on the process:
[RAK Wireless: How to Perform Installation of Board Support Package in PlatformIO](https://learn.rakwireless.com/hc/en-us/articles/26687276346775-How-To-Perform-Installation-of-Board-Support-Package-in-PlatformIO)
After building, you will need to convert the output firmware.hex file into a .uf2 file you can copy over to your RAK4631 device (after doing a full erase) by using the command `uf2conv.py -f 0xADA52840 -c firmware.hex` with the python script available from:
[GitHub: Microsoft - uf2](https://github.com/Microsoft/uf2/blob/master/utils/uf2conv.py)

72
boards/rak3401.json Normal file
View File

@@ -0,0 +1,72 @@
{
"build": {
"arduino": {
"ldscript": "nrf52840_s140_v6.ld"
},
"core": "nRF5",
"cpu": "cortex-m4",
"extra_flags": "-DARDUINO_NRF52840_FEATHER -DNRF52840_XXAA",
"f_cpu": "64000000L",
"hwids": [
[
"0x239A",
"0x8029"
],
[
"0x239A",
"0x0029"
],
[
"0x239A",
"0x002A"
],
[
"0x239A",
"0x802A"
]
],
"usb_product": "WisCore RAK3401 Board",
"mcu": "nrf52840",
"variant": "WisCore_RAK3401_Board",
"bsp": {
"name": "adafruit"
},
"softdevice": {
"sd_flags": "-DS140",
"sd_name": "s140",
"sd_version": "6.1.1",
"sd_fwid": "0x00B6"
},
"bootloader": {
"settings_addr": "0xFF000"
}
},
"connectivity": [
"bluetooth"
],
"debug": {
"jlink_device": "nRF52840_xxAA",
"svd_path": "nrf52840.svd"
},
"frameworks": [
"arduino"
],
"name": "WisCore RAK3401 Board",
"upload": {
"maximum_ram_size": 248832,
"maximum_size": 815104,
"speed": 115200,
"protocol": "nrfutil",
"protocols": [
"jlink",
"nrfjprog",
"nrfutil",
"stlink"
],
"use_1200bps_touch": true,
"require_upload_port": true,
"wait_for_upload_port": true
},
"url": "https://www.rakwireless.com",
"vendor": "RAKwireless"
}

View File

@@ -29,6 +29,20 @@ $ sh build.sh build-repeater-firmwares
Build all chat room server firmwares Build all chat room server firmwares
$ sh build.sh build-room-server-firmwares $ sh build.sh build-room-server-firmwares
Environment Variables:
DISABLE_DEBUG=1: Disables all debug logging flags (MESH_DEBUG, MESH_PACKET_LOGGING, etc.)
If not set, debug flags from variant platformio.ini files are used.
Examples:
Build without debug logging:
$ export FIRMWARE_VERSION=v1.0.0
$ export DISABLE_DEBUG=1
$ sh build.sh build-firmware RAK_4631_repeater
Build with debug logging (default, uses flags from variant files):
$ export FIRMWARE_VERSION=v1.0.0
$ sh build.sh build-firmware RAK_4631_repeater
EOF EOF
} }
@@ -68,6 +82,13 @@ get_pio_envs_ending_with_string() {
done done
} }
# disable all debug logging flags if DISABLE_DEBUG=1 is set
disable_debug_flags() {
if [ "$DISABLE_DEBUG" == "1" ]; then
export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -UMESH_DEBUG -UBLE_DEBUG_LOGGING -UWIFI_DEBUG_LOGGING -UBRIDGE_DEBUG -UGPS_NMEA_DEBUG -UCORE_DEBUG_LEVEL -UESPNOW_DEBUG_LOGGING -UDEBUG_RP2040_WIRE -UDEBUG_RP2040_SPI -UDEBUG_RP2040_CORE -UDEBUG_RP2040_PORT -URADIOLIB_DEBUG_SPI -UCFG_DEBUG -URADIOLIB_DEBUG_BASIC -URADIOLIB_DEBUG_PROTOCOL"
fi
}
# build firmware for the provided pio env in $1 # build firmware for the provided pio env in $1
build_firmware() { build_firmware() {
@@ -94,6 +115,9 @@ build_firmware() {
# add firmware version info to end of existing platformio build flags in environment vars # add firmware version info to end of existing platformio build flags in environment vars
export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DFIRMWARE_BUILD_DATE='\"${FIRMWARE_BUILD_DATE}\"' -DFIRMWARE_VERSION='\"${FIRMWARE_VERSION_STRING}\"'" export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DFIRMWARE_BUILD_DATE='\"${FIRMWARE_BUILD_DATE}\"' -DFIRMWARE_VERSION='\"${FIRMWARE_VERSION_STRING}\"'"
# disable debug flags if requested
disable_debug_flags
# build firmware target # build firmware target
pio run -e $1 pio run -e $1

View File

@@ -26,6 +26,10 @@ author: https://github.com/LitBomb<!-- omit from toc -->
- [3.2. Q: Do I need to set the location for a repeater?](#32-q-do-i-need-to-set-the-location-for-a-repeater) - [3.2. Q: Do I need to set the location for a repeater?](#32-q-do-i-need-to-set-the-location-for-a-repeater)
- [3.3. Q: What is the password to administer a repeater or a room server?](#33-q-what-is-the-password-to-administer-a-repeater-or-a-room-server) - [3.3. Q: What is the password to administer a repeater or a room server?](#33-q-what-is-the-password-to-administer-a-repeater-or-a-room-server)
- [3.4. Q: What is the password to join a room server?](#34-q-what-is-the-password-to-join-a-room-server) - [3.4. Q: What is the password to join a room server?](#34-q-what-is-the-password-to-join-a-room-server)
- [3.5. Q: Can I retrieve a repeater's private key or set a repeater's private key?](#35-q-can-i-retrieve-a-repeaters-private-key-or-set-a-repeaters-private-key)
- [3.6. Q: The first byte of my repeater's public key collides with an exisitng repeater on the mesh. How do I get a new private key with a matching public key that has its first byte of my choosing?](#36-q-the-first-byte-of-my-repeaters-public-key-collides-with-an-exisitng-repeater-on-the-mesh--how-do-i-get-a-new-private-key-with-a-matching-public-key-that-has-its-first-byte-of-my-choosing)
- [3.7. Q: My repeater maybe suffering from deafness due to high power interference near my mesh's frequency, it is not hearing other in-range MeshCore radios. what can I do?](#37-q-my-repeater-maybe-suffering-from-deafness-due-to-high-power-interference-near-my-meshs-frequency-it-is-not-hearing-other-in-range-meshcore-radios--what-can-i-do)
- [3.8 Q: How do I make my repeater an observer on the mesh](#38-q-how-do-i-make-my-repeater-an-observer-on-the-mesh)
- [4. T-Deck Related](#4-t-deck-related) - [4. T-Deck Related](#4-t-deck-related)
- [4.1. Q: Is there a user guide for T-Deck, T-Pager, T-Watch, or T-Display Pro?](#41-q-is-there-a-user-guide-for-t-deck-t-pager-t-watch-or-t-display-pro) - [4.1. Q: Is there a user guide for T-Deck, T-Pager, T-Watch, or T-Display Pro?](#41-q-is-there-a-user-guide-for-t-deck-t-pager-t-watch-or-t-display-pro)
- [4.2. Q: What are the steps to get a T-Deck into DFU (Device Firmware Update) mode?](#42-q-what-are-the-steps-to-get-a-t-deck-into-dfu-device-firmware-update-mode) - [4.2. Q: What are the steps to get a T-Deck into DFU (Device Firmware Update) mode?](#42-q-what-are-the-steps-to-get-a-t-deck-into-dfu-device-firmware-update-mode)
@@ -61,22 +65,31 @@ author: https://github.com/LitBomb<!-- omit from toc -->
- [5.14.3. Python MeshCore](#5143-python-meshcore) - [5.14.3. Python MeshCore](#5143-python-meshcore)
- [5.14.4. meshcore-cli](#5144-meshcore-cli) - [5.14.4. meshcore-cli](#5144-meshcore-cli)
- [5.14.5. meshcore.js](#5145-meshcorejs) - [5.14.5. meshcore.js](#5145-meshcorejs)
- [5.14.6. pyMC\_core](#5146-pymc_core)
- [5.14.7. MeshCore Packet Decoder](#5147-meshcore-packet-decoder)
- [5.14.8. meshcore-pi](#5148-meshcore-pi)
- [5.14.9. pyMC\_Repeater](#5149-pymc_repeater)
- [5.15. Q: Are there client applications for Windows or Mac?](#515-q-are-there-client-applications-for-windows-or-mac)
- [5.16. Q: Are there any resources that compare MeshCore to other LoRa systems?](#516-q-are-there-any-resources-that-compare-meshcore-to-other-lora-systems)
- [6. Troubleshooting](#6-troubleshooting) - [6. Troubleshooting](#6-troubleshooting)
- [6.1. Q: My client says another client or a repeater or a room server was last seen many, many days ago.](#61-q-my-client-says-another-client-or-a-repeater-or-a-room-server-was-last-seen-many-many-days-ago) - [6.1. Q: My client says another client or a repeater or a room server was last seen many, many days ago.](#61-q-my-client-says-another-client-or-a-repeater-or-a-room-server-was-last-seen-many-many-days-ago)
- [6.2. Q: A repeater or a client or a room server I expect to see on my discover list (on T-Deck) or contact list (on a smart device client) are not listed.](#62-q-a-repeater-or-a-client-or-a-room-server-i-expect-to-see-on-my-discover-list-on-t-deck-or-contact-list-on-a-smart-device-client-are-not-listed) - [6.2. Q: A repeater or a client or a room server I expect to see on my discover list (on T-Deck) or contact list (on a smart device client) are not listed.](#62-q-a-repeater-or-a-client-or-a-room-server-i-expect-to-see-on-my-discover-list-on-t-deck-or-contact-list-on-a-smart-device-client-are-not-listed)
- [6.3. Q: How to connect to a repeater via BLE (Bluetooth)?](#63-q-how-to-connect-to-a-repeater-via-ble-bluetooth) - [6.3. Q: How to connect to a repeater via BLE (Bluetooth)?](#63-q-how-to-connect-to-a-repeater-via-ble-bluetooth)
- [6.4. Q: My companion isn't showing up over Bluetooth?](#64-q-my-companion-isnt-showing-up-over-bluetooth) - [6.4. Q: My companion isn't showing up over Bluetooth?](#64-q-my-companion-isnt-showing-up-over-bluetooth)
- [6.5. Q: I can't connect via Bluetooth, what is the Bluetooth pairing code?](#64-q-i-cant-connect-via-bluetooth-what-is-the-bluetooth-pairing-code) - [6.5. Q: I can't connect via Bluetooth, what is the Bluetooth pairing code?](#65-q-i-cant-connect-via-bluetooth-what-is-the-bluetooth-pairing-code)
- [6.6. Q: My Heltec V3 keeps disconnecting from my smartphone. It can't hold a solid Bluetooth connection.](#65-q-my-heltec-v3-keeps-disconnecting-from-my-smartphone--it-cant-hold-a-solid-bluetooth-connection) - [6.6. Q: My Heltec V3 keeps disconnecting from my smartphone. It can't hold a solid Bluetooth connection.](#66-q-my-heltec-v3-keeps-disconnecting-from-my-smartphone--it-cant-hold-a-solid-bluetooth-connection)
- [6.7. Q: My RAK/T1000-E/xiao\_nRF52 device seems to be corrupted, how do I wipe it clean to start fresh?](#66-q-my-rakt1000-exiao_nrf52-device-seems-to-be-corrupted-how-do-i-wipe-it-clean-to-start-fresh) - [6.7. Q: My RAK/T1000-E/xiao\_nRF52 device seems to be corrupted, how do I wipe it clean to start fresh?](#67-q-my-rakt1000-exiao_nrf52-device-seems-to-be-corrupted-how-do-i-wipe-it-clean-to-start-fresh)
- [6.8. Q: WebFlasher fails on Linux with failed to open](#67-q-webflasher-fails-on-linux-with-failed-to-open) - [6.8. Q: WebFlasher fails on Linux with failed to open](#68-q-webflasher-fails-on-linux-with-failed-to-open)
- [7. Other Questions:](#7-other-questions) - [7. Other Questions:](#7-other-questions)
- [7.1. Q: How to update nRF (RAK, T114, Seed XIAO) repeater and room server firmware over the air using the new simpler DFU app?](#71-q-how-to-update-nrf-rak-t114-seed-xiao-repeater-and-room-server-firmware-over-the-air-using-the-new-simpler-dfu-app) - [7.1. Q: How to update nRF (RAK, T114, Seed XIAO) repeater and room server firmware over the air using the new simpler DFU app?](#71-q-how-to-update-nrf-rak-t114-seed-xiao-repeater-and-room-server-firmware-over-the-air-using-the-new-simpler-dfu-app)
- [7.1.1 Q: Can I update Seeed Studio Wio Tracker L1 Pro using OTA?](#711-q-can-i-update-seeed-studio-wio-tracker-l1-pro-using-ota)
- [7.2. Q: How to update ESP32-based devices over the air?](#72-q-how-to-update-esp32-based-devices-over-the-air) - [7.2. Q: How to update ESP32-based devices over the air?](#72-q-how-to-update-esp32-based-devices-over-the-air)
- [7.3. Q: Is there a way to lower the chance of a failed OTA device firmware update (DFU)?](#73-q-is-there-a-way-to-lower-the-chance-of-a-failed-ota-device-firmware-update-dfu) - [7.3. Q: Is there a way to lower the chance of a failed OTA device firmware update (DFU)?](#73-q-is-there-a-way-to-lower-the-chance-of-a-failed-ota-device-firmware-update-dfu)
- [7.4. Q: are the MeshCore logo and font available?](#74-q-are-the-meshcore-logo-and-font-available) - [7.4. Q: are the MeshCore logo and font available?](#74-q-are-the-meshcore-logo-and-font-available)
- [7.5. Q: What is the format of a contact or channel QR code?](#75-q-what-is-the-format-of-a-contact-or-channel-qr-code) - [7.5. Q: What is the format of a contact or channel QR code?](#75-q-what-is-the-format-of-a-contact-or-channel-qr-code)
- [7.6. Q: How do I connect to the companion via WIFI, e.g. using a heltec v3?](#76-q-how-do-i-connect-to-the-comnpanion-via-wifi-eg-using-a-heltec-v3) - [7.6. Q: How do I connect to the companion via WIFI, e.g. using a heltec v3?](#76-q-how-do-i-connect-to-the-companion-via-wifi-eg-using-a-heltec-v3)
- [7.7. Q: I have a Station G2, or a Heltec V4, or an Ikoka Stick, or a radio with a EByte E22-900M30S or a E22-900M33S module, what should their transmit power be set to?](#77-q-i-have-a-station-g2-or-a-heltec-v4-or-an-ikoka-stick-or-a-radio-with-a-ebyte-e22-900m30s-or-a-e22-900m33s-module-what-should-their-transmit-power-be-set-to)
- [| | High Output | 22 dBm | 28 dBm | |](#--high-output--22-dbm--28-dbm--)
## 1. Introduction ## 1. Introduction
@@ -180,22 +193,17 @@ The T-Deck firmware is free to download and most features are available without
### 2.3. Q: What frequencies are supported by MeshCore? ### 2.3. Q: What frequencies are supported by MeshCore?
**A:** It supports the 868MHz range in the UK/EU and the 915MHz range in New Zealand, Australia, and the USA. Countries and regions in these two frequency ranges are also supported. The firmware and client allow users to set their preferred frequency. **A:** It supports the 868MHz range in the UK/EU and the 915MHz range in New Zealand, Australia, and the USA. Countries and regions in these two frequency ranges are also supported.
- Australia and New Zealand are on **915.8MHz**
- UK and EU are on **869.525MHz**
- Canada and USA are on **910.525MHz**
- For other regions and countries, please check your local LoRa frequency
In UK and EU, 867.5MHz is not allowed to use 250kHz bandwidth and it only allows 2.5% duty cycle for clients. 869.525Mhz allows an airtime of 10%, 250KHz bandwidth, and a higher EIRP, therefore MeshCore nodes can send more often and with more power. That is why this frequency is chosen for UK and EU. This is also why Meshtastic also uses this frequency. Use the smartphone client or the repeater setup feature on there web flasher to set your radios' RF settings by choosing the preset for your regions.
[Source](https://discord.com/channels/826570251612323860/1330643963501351004/1356540643853209641) Recently, as of October 2025, many regions have moved to the "narrow" setting, aka using BW62.5 and a lower SF number (instead of the original SF11). For example, USA/Canada (Recommended) preset is 910.525MHz, SF7, BW62.5, CR5.
After extensive testing, many regions have switched or about to switch over to BW62.5 and SF7, 8, or 9. Narrower bandwidth setting and lower SF setting allow MeshCore's radio signals to fit between interference in the ISM band, provide for a lower noise floor, better SNR, and faster transmissions.
If you have consensus from your community in your region to update your region's preset recommendation, please post your update request on the [#meshcore-app](https://discord.com/channels/1343693475589263471/1391681655911088241) channel on the [MeshCore Discord server ](https://discord.gg/cYtQNYCCRK) to let Liam Cottle know.
the rest of the radio settings are the same for all frequencies:
- Spread Factor (SF): 11
- Coding Rate (CR): 5
- Bandwidth (BW): 250.00
(Originally MeshCore started with SF 10. recently (as of late April 2025) the community has advocated SF 11 also a viable option for longer range but a little slower transmission. Currently there are MeshCore meshes with SF 10 and SF 11. Liam Cottle's smartphone app's presets now recommend SF 10 for Australia and SF 11 for all other regions and countries. EU and UK has SF 10 and SF 11 presets. Work with your local meshers on deciding with SF number is best for your use cases. In the future, there may be bridge nodes that can bridge SF 10 and SF 11 (or even different frequencies) traffic.)
### 2.4. Q: What is an "advert" in MeshCore? ### 2.4. Q: What is an "advert" in MeshCore?
**A:** **A:**
@@ -243,7 +251,7 @@ Repeater or room server can be administered with one of the options below:
### 3.2. Q: Do I need to set the location for a repeater? ### 3.2. Q: Do I need to set the location for a repeater?
**A:** With location set for a repeater, it can show up on a MeshCore map in the future. Set location with the following commands: **A:** While not required, with location set for a repeater it will show up on the MeshCore map in the future. Set location with the following command:
`set lat <GPS Lat> set long <GPS Lon>` `set lat <GPS Lat> set long <GPS Lon>`
@@ -260,6 +268,34 @@ You can get the latitude and longitude from Google Maps by right-clicking the lo
`set guest.password {guest-password}` `set guest.password {guest-password}`
### 3.5. Q: Can I retrieve a repeater's private key or set a repeater's private key?
**A:** You can issue these commands to get or set a repeater's private key using a USB serial connection.
`get prv.key` to print a repeater's private key on the serial console
`set prv.key <hex>` to set a repeater's private key on the serial console
Reboot the repeater after `set prv.key <hex>` command for the new private key to take effect.
### 3.6. Q: The first byte of my repeater's public key collides with an exisitng repeater on the mesh. How do I get a new private key with a matching public key that has its first byte of my choosing?
**A:** You can generate a new private key and specific the first byte of its public key here: https://gessaman.com/mc-keygen/
### 3.7. Q: My repeater maybe suffering from deafness due to high power interference near my mesh's frequency, it is not hearing other in-range MeshCore radios. what can I do?
**A:** This may be due to the SX1262 radio's auto gain control feature. You can use this command to preiodically reset its AGC.
`set agc.reset.interval <number>`
The `<number>` unit is in seconds and is incremented by 4. `set agc.reset.interval 4` works well to cure deafness.
This is a very low cost operation. AGC reset is done by simply setting `state = STATE_IDLE;` in function `RadioLibWrapper::resetAGC()` in `RadioLibWrappers.cpp`
### 3.8 Q: How do I make my repeater an observer on the mesh
**A:** The observer instruction is available here: https://analyzer.letsme.sh/observer/onboard
--- ---
@@ -297,7 +333,9 @@ GPS on T-Deck is always enabled. You can skip the "GPS clock sync" and the T-De
**A:** **A:**
T-Deck uses the same key the smartphone apps use but in base64 T-Deck uses the same key the smartphone apps use but in base64
`izOH6cXN6mrJ5e26oRXNcg==` `izOH6cXN6mrJ5e26oRXNcg==`
The third character is the capital letter 'O', not zero `0`
There is no `=` key on the T-Deck's hardware keyboard. You can use the on-screen software keyboard to enter `=`. Tap the text box to enable the on-screen software keyboard.
The third character is the capital letter `O` (Oh), not zero `0`
The smartphone app key is in hex: The smartphone app key is in hex:
` 8b3387e9c5cdea6ac9e5edbaa115cd72` ` 8b3387e9c5cdea6ac9e5edbaa115cd72`
@@ -376,7 +414,23 @@ https://github.com/meshcore-dev/MeshCore/blob/main/src/Packet.h#L19
**SF is spreading factor** - how much should the communication spread in time **SF is spreading factor** - how much should the communication spread in time
**CR is coding rate** - https://www.thethingsnetwork.org/docs/lorawan/fec-and-code-rate/ **CR is coding rate** - from: https://www.thethingsnetwork.org/docs/lorawan/fec-and-code-rate/
TL;DR: default CR to 5 for good stable links. If it is not a solid link and is intermittent, change to CR to 7 or 8.
Forward Error Correction is a process of adding redundant bits to the data to be transmitted. During the transmission, data may get corrupted by interference (changes from 0 to 1 / 1 to 0). These error correction bits are used at the receivers for restoring corrupted bits.
The Code Rate of a forward error correction expresses the proportion of bits in a data stream that actually carry useful information.
There are 4 code rates used in LoRaWAN:
4/5
4/6
5/7
4/8
For example, if the code rate is 5/7, for every 5 bits of useful information, the coder generates a total of 7 bits of data, of which 2 bits are redundant.
Making the bandwidth 2x wider (from BW125 to BW250) allows you to send 2x more bytes in the same time. Making the spreading factor 1 step lower (from SF10 to SF9) allows you to send 2x more bytes in the same time. Making the bandwidth 2x wider (from BW125 to BW250) allows you to send 2x more bytes in the same time. Making the spreading factor 1 step lower (from SF10 to SF9) allows you to send 2x more bytes in the same time.
Lowering the spreading factor makes it more difficult for the gateway to receive a transmission, as it will be more sensitive to noise. You could compare this to two people taking in a noisy place (a bar for example). If youre far from each other, you have to talk slow (SF10), but if youre close, you can talk faster (SF7) Lowering the spreading factor makes it more difficult for the gateway to receive a transmission, as it will be more sensitive to noise. You could compare this to two people taking in a noisy place (a bar for example). If youre far from each other, you have to talk slow (SF10), but if youre close, you can talk faster (SF7)
@@ -558,7 +612,8 @@ From here, reference repeater and room server command line commands on MeshCore
**A:** Yes. See the following: **A:** Yes. See the following:
#### 5.14.1. meshcoremqtt #### 5.14.1. meshcoremqtt
A Python script to send meshore debug and packet capture data to MQTT for analysis A Python script to send meshcore debug and packet capture data to MQTT for analysis. Cisien's version is a fork of Andrew-a-g's and is being used to to collect data for https://map.w0z.is/messages and https://analyzer.letsme.sh/
https://github.com/Cisien/meshcoretomqtt
https://github.com/Andrew-a-g/meshcoretomqtt https://github.com/Andrew-a-g/meshcoretomqtt
#### 5.14.2. MeshCore for Home Assistant #### 5.14.2. MeshCore for Home Assistant
@@ -577,6 +632,40 @@ CLI interface to MeshCore companion radio over BLE, TCP, or serial. Uses Python
A JavaScript library for interacting with a MeshCore device running the companion radio firmware A JavaScript library for interacting with a MeshCore device running the companion radio firmware
https://github.com/liamcottle/meshcore.js https://github.com/liamcottle/meshcore.js
#### 5.14.6. pyMC_core
pyMC_Core is a Python port of MeshCore, designed for Raspberry Pi and similar hardware, it talks to LoRa modules over SPI.
https://github.com/rightup/pyMC_core
#### 5.14.7. MeshCore Packet Decoder
A TypeScript library for decoding MeshCore mesh networking packets with full cryptographic support. Uses WebAssembly (WASM) for Ed25519 key derivation through the orlp/ed25519 library. It powers the [MeshCore Packet Analyzer](https://analyzer.letsme.sh/packets).
https://github.com/michaelhart/meshcore-decoder
#### 5.14.8. meshcore-pi
meshcore-pi is another Python port of MeshCore, designed for Raspberry Pi and similar hardware, it talks to LoRa modules over SPI or GPIO.
https://github.com/brianwiddas/meshcore-pi
#### 5.14.9. pyMC_Repeater
pyMC_Repeater is a repeater daemon in Python built on top of the [`pymc_core`](#5146-pymc_core) library.
https://github.com/rightup/pyMC_Repeater
### 5.15. Q: Are there client applications for Windows or Mac?
**A:** Yes, the same iOS and Android client is also available for Windows and Intel Mac (sorry, not available for ARM-based Mac yet). You can find them together with the Android APK here:
https://files.liamcottle.net/MeshCore
Both the Windows and Intel Mac versions of the client app are fully unlocked and are free to use.
### 5.16. Q: Are there any resources that compare MeshCore to other LoRa systems?
**A:** Here is a list of MeshCore comparison resources:
The Comms Channel on YouTube:
https://www.youtube.com/watch?v=guDoKGs02Us
MeshCore Advantages by MCarper:
https://github.com/mikecarper/meshfirmware/blob/main/MeshCoreAdvantages.md
Meshcore vs Meshtastic by austinmesh.org
https://www.austinmesh.org/learn/meshcore-vs-meshtastic/
--- ---
## 6. Troubleshooting ## 6. Troubleshooting
@@ -653,6 +742,12 @@ Allow the browser user on it:
13. If it fails, try turning off and on Bluetooth on your phone. If that doesn't work, try rebooting your phone. 13. If it fails, try turning off and on Bluetooth on your phone. If that doesn't work, try rebooting your phone.
14. Wait for the update to complete. It can take a few minutes. 14. Wait for the update to complete. It can take a few minutes.
#### 7.1.1 Q: Can I update Seeed Studio Wio Tracker L1 Pro using OTA?
**A:** You can flash this safer bootloader to the Wio Tracker L1 Pro
https://github.com/oltaco/Adafruit_nRF52_Bootloader_OTAFIX
After this bootloader is flashed onto the device, you can trigger over the air update using bluetooth by holding the button next to the D-Pad and then click the reset button. The follow the same OTA update instructions above. You can skip pass the `start ota` instruction and start the update using the DFU app.
### 7.2. Q: How to update ESP32-based devices over the air? ### 7.2. Q: How to update ESP32-based devices over the air?
@@ -673,10 +768,14 @@ Allow the browser user on it:
Refer to https://github.com/oltaco/Adafruit_nRF52_Bootloader_OTAFIX for the latest information. Refer to https://github.com/oltaco/Adafruit_nRF52_Bootloader_OTAFIX for the latest information.
Currently, the following boards are supported: Currently, the following boards are supported:
- Nologo ProMicro - Heltec Automation Mesh Node T114 / HT-nRF5262
- Nologo ProMicro NRF52840 (aka SuperMini NRF52840)
- Seeed Studio SenseCAP Card Tracker T1000-E
- Seeed Studio Wio Tracker L1
- Seeed Studio XIAO nRF52840 BLE - Seeed Studio XIAO nRF52840 BLE
- Seeed Studio XIAO nRF52840 BLE SENSE - Seeed Studio XIAO nRF52840 BLE SENSE
- RAK 4631 - RAK 4631 (See note)
- RAK WisMesh Tag (new 28/11/2025)
### 7.4. Q: are the MeshCore logo and font available? ### 7.4. Q: are the MeshCore logo and font available?
@@ -703,4 +802,22 @@ where `&type` is:
WiFi firmware requires you to compile it yourself, as you need to set the wifi ssid and password. WiFi firmware requires you to compile it yourself, as you need to set the wifi ssid and password.
Edit WIFI_SSID and WIFI_PWD in `./variants/heltec_v3/platformio.ini` and then flash it to your device. Edit WIFI_SSID and WIFI_PWD in `./variants/heltec_v3/platformio.ini` and then flash it to your device.
### 7.7. Q: I have a Station G2, or a Heltec V4, or an Ikoka Stick, or a radio with a EByte E22-900M30S or a E22-900M33S module, what should their transmit power be set to?
**A:**
For companion radios, you can set these radios' transmit power in the smartphone app. For repeater and room server radios, you can set their transmit power using the command line command `set tx`. You can get their current value using command line comand `get tx`
> ### ⚠️ **WARNING: Set these values at your own risk. Incorrect power settings can permanently damage your radio hardware.**
| Device / Model | Region / Description | In-App Setting (dBm) | Target Radio Output | Notes |
| :--- | :--- | :--- | :--- | :--- |
| **Station G2** <br> [Reference](https://wiki.uniteng.com/en/meshtastic/station-g2) | US915 Max Output | 19 dBm | 36.5 dBm (4.46W) | |
| | US915 Recommended Max | 16 dBm | 35 dBm (3.16W) | 1dB compression point |
| | EU868 Recommended Max | 15 dBm | 34.5 dBm (2.82W) | 1dB compression point |
| | US915 1W Output | 10 dBm | 1W | |
| | EU868 1W Output | 9 dBm | 1W | |
| **Ikoka Stick E22-900M30S** | 1W Model | 19 dBm | 1W | **DO NOT EXCEED** (Risk of burn out) |
| **Ikoka Stick E22-900M33S** | 2W Model | 9 dBm | 2W | **DO NOT EXCEED** (Risk of burn out) |
| **Heltec V4** | Standard Output | 10 dBm | 22 dBm | |
| | High Output | 22 dBm | 28 dBm | |
--- ---

View File

@@ -0,0 +1,213 @@
# nRF52 Power Management
## Overview
The nRF52 Power Management module provides battery protection features to prevent over-discharge, minimise likelihood of brownout and flash corruption conditions existing, and enable safe voltage-based recovery.
## Features
### Boot Voltage Protection
- Checks battery voltage immediately after boot and before mesh operations commence
- If voltage is below a configurable threshold (e.g., 3300mV), the device configures voltage wake (LPCOMP + VBUS) and enters protective shutdown (SYSTEMOFF)
- Prevents boot loops when battery is critically low
- Skipped when external power (USB VBUS) is detected
### Voltage Wake (LPCOMP + VBUS)
- Configures the nRF52's Low Power Comparator (LPCOMP) before entering SYSTEMOFF
- Enables USB VBUS detection so external power can wake the device
- Device automatically wakes when battery voltage rises above recovery threshold or when VBUS is detected
### Early Boot Register Capture
- Captures RESETREAS (reset reason) and GPREGRET2 (shutdown reason) before SystemInit() clears them
- Allows firmware to determine why it booted (cold boot, watchdog, LPCOMP wake, etc.)
- Allows firmware to determine why it last shut down (user request, low voltage, boot protection)
### Shutdown Reason Tracking
Shutdown reason codes (stored in GPREGRET2):
| Code | Name | Description |
|------|------|-------------|
| 0x00 | NONE | Normal boot / no previous shutdown |
| 0x4C | LOW_VOLTAGE | Runtime low voltage threshold reached |
| 0x55 | USER | User requested powerOff() |
| 0x42 | BOOT_PROTECT | Boot voltage protection triggered |
## Supported Boards
| Board | Implemented | LPCOMP wake | VBUS wake |
|-------|-------------|-------------|-----------|
| Seeed Studio XIAO nRF52840 (`xiao_nrf52`) | Yes | Yes | Yes |
| RAK4631 (`rak4631`) | Yes | Yes | Yes |
| Heltec T114 (`heltec_t114`) | Yes | Yes | Yes |
| Promicro nRF52840 | No | No | No |
| RAK WisMesh Tag | No | No | No |
| Heltec Mesh Solar | No | No | No |
| LilyGo T-Echo / T-Echo Lite | No | No | No |
| SenseCAP Solar | No | No | No |
| WIO Tracker L1 / L1 E-Ink | No | No | No |
| WIO WM1110 | No | No | No |
| Mesh Pocket | No | No | No |
| Nano G2 Ultra | No | No | No |
| ThinkNode M1/M3/M6 | No | No | No |
| T1000-E | No | No | No |
| Ikoka Nano/Stick/Handheld (nRF) | No | No | No |
| Keepteen LT1 | No | No | No |
| Minewsemi ME25LS01 | No | No | No |
Notes:
- "Implemented" reflects Phase 1 (boot lockout + shutdown reason capture).
- User power-off on Heltec T114 does not enable LPCOMP wake.
- VBUS detection is used to skip boot lockout on external power, and VBUS wake is configured alongside LPCOMP when supported hardware exposes VBUS to the nRF52.
## Technical Details
### Architecture
The power management functionality is integrated into the `NRF52Board` base class in `src/helpers/NRF52Board.cpp`. Board variants provide hardware-specific configuration via a `PowerMgtConfig` struct and override `initiateShutdown(uint8_t reason)` to perform board-specific power-down work and conditionally enable voltage wake (LPCOMP + VBUS).
### Early Boot Capture
A static constructor with priority 101 in `NRF52Board.cpp` captures the RESETREAS and GPREGRET2 registers before:
- SystemInit() (priority 102) - which clears RESETREAS
- Static C++ constructors (default priority 65535)
This ensures we capture the true reset reason before any initialisation code runs.
### Board Implementation
To enable power management on a board variant:
1. **Enable in platformio.ini**:
```ini
-D NRF52_POWER_MANAGEMENT
```
2. **Define configuration in variant.h**:
```c
#define PWRMGT_VOLTAGE_BOOTLOCK 3300 // Won't boot below this voltage (mV)
#define PWRMGT_LPCOMP_AIN 7 // AIN channel for voltage sensing
#define PWRMGT_LPCOMP_REFSEL 2 // REFSEL (0-6=1/8..7/8, 7=ARef, 8-15=1/16..15/16)
```
3. **Implement in board .cpp file**:
```cpp
#ifdef NRF52_POWER_MANAGEMENT
const PowerMgtConfig power_config = {
.lpcomp_ain_channel = PWRMGT_LPCOMP_AIN,
.lpcomp_refsel = PWRMGT_LPCOMP_REFSEL,
.voltage_bootlock = PWRMGT_VOLTAGE_BOOTLOCK
};
void MyBoard::initiateShutdown(uint8_t reason) {
// Board-specific shutdown preparation (e.g., disable peripherals)
bool enable_lpcomp = (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
reason == SHUTDOWN_REASON_BOOT_PROTECT);
if (enable_lpcomp) {
configureVoltageWake(power_config.lpcomp_ain_channel, power_config.lpcomp_refsel);
}
enterSystemOff(reason);
}
#endif
void MyBoard::begin() {
NRF52Board::begin(); // or NRF52BoardDCDC::begin()
// ... board setup ...
#ifdef NRF52_POWER_MANAGEMENT
checkBootVoltage(&power_config);
#endif
}
```
For user-initiated shutdowns, `powerOff()` remains board-specific. Power management only arms LPCOMP for automated shutdown reasons (boot protection/low voltage).
4. **Declare override in board .h file**:
```cpp
#ifdef NRF52_POWER_MANAGEMENT
void initiateShutdown(uint8_t reason) override;
#endif
```
### Voltage Wake Configuration
The LPCOMP (Low Power Comparator) is configured to:
- Monitor the specified AIN channel (0-7 corresponding to P0.02-P0.05, P0.28-P0.31)
- Compare against VDD fraction reference (REFSEL: 0-6=1/8..7/8, 7=ARef, 8-15=1/16..15/16)
- Detect UP events (voltage rising above threshold)
- Use 50mV hysteresis for noise immunity
- Wake the device from SYSTEMOFF when triggered
VBUS wake is enabled via the POWER peripheral USBDETECTED event whenever `configureVoltageWake()` is used. This requires USB VBUS to be routed to the nRF52 (typical on nRF52840 boards with native USB).
**LPCOMP Reference Selection (PWRMGT_LPCOMP_REFSEL)**:
| REFSEL | Fraction | VBAT @ 1M/1M divider (VDD=3.0-3.3) | VBAT @ 1.5M/1M divider (VDD=3.0-3.3) |
|--------|----------|------------------------------------|--------------------------------------|
| 0 | 1/8 | 0.75-0.82 V | 0.94-1.03 V |
| 1 | 2/8 | 1.50-1.65 V | 1.88-2.06 V |
| 2 | 3/8 | 2.25-2.47 V | 2.81-3.09 V |
| 3 | 4/8 | 3.00-3.30 V | 3.75-4.12 V |
| 4 | 5/8 | 3.75-4.12 V | 4.69-5.16 V |
| 5 | 6/8 | 4.50-4.95 V | 5.62-6.19 V |
| 6 | 7/8 | 5.25-5.77 V | 6.56-7.22 V |
| 7 | ARef | - | - |
| 8 | 1/16 | 0.38-0.41 V | 0.47-0.52 V |
| 9 | 3/16 | 1.12-1.24 V | 1.41-1.55 V |
| 10 | 5/16 | 1.88-2.06 V | 2.34-2.58 V |
| 11 | 7/16 | 2.62-2.89 V | 3.28-3.61 V |
| 12 | 9/16 | 3.38-3.71 V | 4.22-4.64 V |
| 13 | 11/16 | 4.12-4.54 V | 5.16-5.67 V |
| 14 | 13/16 | 4.88-5.36 V | 6.09-6.70 V |
| 15 | 15/16 | 5.62-6.19 V | 7.03-7.73 V |
**Important**: For boards with a voltage divider on the battery sense pin, LPCOMP measures the divided voltage. Use:
`VBAT_threshold ≈ (VDD * fraction) * divider_scale`, where `divider_scale = (Rtop + Rbottom) / Rbottom` (e.g., 2.0 for 1M/1M, 2.5 for 1.5M/1M, 3.0 for XIAO).
### SoftDevice Compatibility
The power management code checks whether SoftDevice is enabled and uses the appropriate API:
- When SD enabled: `sd_power_*` functions
- When SD disabled: Direct register access (NRF_POWER->*)
This ensures compatibility regardless of BLE stack state.
## CLI Commands
Power management status can be queried via the CLI:
| Command | Description |
|---------|-------------|
| `get pwrmgt.support` | Returns "supported" or "unsupported" |
| `get pwrmgt.source` | Returns current power source - "battery" or "external" (5V/USB power) |
| `get pwrmgt.bootreason` | Returns reset and shutdown reason strings |
| `get pwrmgt.bootmv` | Returns boot voltage in millivolts |
On boards without power management enabled, all commands except `get pwrmgt.support` return:
```
ERROR: Power management not supported
```
## Debug Output
When `MESH_DEBUG=1` is enabled, the power management module outputs:
```
DEBUG: PWRMGT: Reset = Wake from LPCOMP (0x20000); Shutdown = Low Voltage (0x4C)
DEBUG: PWRMGT: Boot voltage = 3450 mV (threshold = 3300 mV)
DEBUG: PWRMGT: LPCOMP wake configured (AIN7, ref=3/8 VDD)
```
## Phase 2 (Planned)
- Runtime voltage monitoring
- Voltage state machine (Normal -> Warning -> Critical -> Shutdown)
- Configurable thresholds
- Load shedding callbacks for power reduction
- Deep sleep integration
- Scheduled wake-up
- Extended sleep with periodic monitoring
## References
- [nRF52840 Product Specification - POWER](https://infocenter.nordicsemi.com/topic/ps_nrf52840/power.html)
- [nRF52840 Product Specification - LPCOMP](https://infocenter.nordicsemi.com/topic/ps_nrf52840/lpcomp.html)
- [SoftDevice S140 API - Power Management](https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/group__nrf__sdm__api.html)

View File

@@ -227,6 +227,7 @@ void DataStore::loadPrefsInt(const char *filename, NodePrefs& _prefs, double& no
file.read((uint8_t *)&_prefs.buzzer_quiet, sizeof(_prefs.buzzer_quiet)); // 84 file.read((uint8_t *)&_prefs.buzzer_quiet, sizeof(_prefs.buzzer_quiet)); // 84
file.read((uint8_t *)&_prefs.gps_enabled, sizeof(_prefs.gps_enabled)); // 85 file.read((uint8_t *)&_prefs.gps_enabled, sizeof(_prefs.gps_enabled)); // 85
file.read((uint8_t *)&_prefs.gps_interval, sizeof(_prefs.gps_interval)); // 86 file.read((uint8_t *)&_prefs.gps_interval, sizeof(_prefs.gps_interval)); // 86
file.read((uint8_t *)&_prefs.autoadd_config, sizeof(_prefs.autoadd_config)); // 87
file.close(); file.close();
} }
@@ -261,6 +262,7 @@ void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_
file.write((uint8_t *)&_prefs.buzzer_quiet, sizeof(_prefs.buzzer_quiet)); // 84 file.write((uint8_t *)&_prefs.buzzer_quiet, sizeof(_prefs.buzzer_quiet)); // 84
file.write((uint8_t *)&_prefs.gps_enabled, sizeof(_prefs.gps_enabled)); // 85 file.write((uint8_t *)&_prefs.gps_enabled, sizeof(_prefs.gps_enabled)); // 85
file.write((uint8_t *)&_prefs.gps_interval, sizeof(_prefs.gps_interval)); // 86 file.write((uint8_t *)&_prefs.gps_interval, sizeof(_prefs.gps_interval)); // 86
file.write((uint8_t *)&_prefs.autoadd_config, sizeof(_prefs.autoadd_config)); // 87
file.close(); file.close();
} }

View File

@@ -54,6 +54,8 @@
#define CMD_SEND_CONTROL_DATA 55 // v8+ #define CMD_SEND_CONTROL_DATA 55 // v8+
#define CMD_GET_STATS 56 // v8+, second byte is stats type #define CMD_GET_STATS 56 // v8+, second byte is stats type
#define CMD_SEND_ANON_REQ 57 #define CMD_SEND_ANON_REQ 57
#define CMD_SET_AUTOADD_CONFIG 58
#define CMD_GET_AUTOADD_CONFIG 59
// Stats sub-types for CMD_GET_STATS // Stats sub-types for CMD_GET_STATS
#define STATS_TYPE_CORE 0 #define STATS_TYPE_CORE 0
@@ -85,6 +87,7 @@
#define RESP_CODE_ADVERT_PATH 22 #define RESP_CODE_ADVERT_PATH 22
#define RESP_CODE_TUNING_PARAMS 23 #define RESP_CODE_TUNING_PARAMS 23
#define RESP_CODE_STATS 24 // v8+, second byte is stats type #define RESP_CODE_STATS 24 // v8+, second byte is stats type
#define RESP_CODE_AUTOADD_CONFIG 25
#define SEND_TIMEOUT_BASE_MILLIS 500 #define SEND_TIMEOUT_BASE_MILLIS 500
#define FLOOD_SEND_TIMEOUT_FACTOR 16.0f #define FLOOD_SEND_TIMEOUT_FACTOR 16.0f
@@ -110,6 +113,8 @@
#define PUSH_CODE_BINARY_RESPONSE 0x8C #define PUSH_CODE_BINARY_RESPONSE 0x8C
#define PUSH_CODE_PATH_DISCOVERY_RESPONSE 0x8D #define PUSH_CODE_PATH_DISCOVERY_RESPONSE 0x8D
#define PUSH_CODE_CONTROL_DATA 0x8E // v8+ #define PUSH_CODE_CONTROL_DATA 0x8E // v8+
#define PUSH_CODE_CONTACT_DELETED 0x8F // used to notify client app of deleted contact when overwriting oldest
#define PUSH_CODE_CONTACTS_FULL 0x90 // used to notify client app that contacts storage is full
#define ERR_CODE_UNSUPPORTED_CMD 1 #define ERR_CODE_UNSUPPORTED_CMD 1
#define ERR_CODE_NOT_FOUND 2 #define ERR_CODE_NOT_FOUND 2
@@ -120,6 +125,15 @@
#define MAX_SIGN_DATA_LEN (8 * 1024) // 8K #define MAX_SIGN_DATA_LEN (8 * 1024) // 8K
// Auto-add config bitmask
// Bit 0: If set, overwrite oldest non-favourite contact when contacts file is full
// Bits 1-4: these indicate which contact types to auto-add when manual_contact_mode = 0x01
#define AUTO_ADD_OVERWRITE_OLDEST (1 << 0) // 0x01 - overwrite oldest non-favourite when full
#define AUTO_ADD_CHAT (1 << 1) // 0x02 - auto-add Chat (Companion) (ADV_TYPE_CHAT)
#define AUTO_ADD_REPEATER (1 << 2) // 0x04 - auto-add Repeater (ADV_TYPE_REPEATER)
#define AUTO_ADD_ROOM_SERVER (1 << 3) // 0x08 - auto-add Room Server (ADV_TYPE_ROOM)
#define AUTO_ADD_SENSOR (1 << 4) // 0x10 - auto-add Sensor (ADV_TYPE_SENSOR)
void MyMesh::writeOKFrame() { void MyMesh::writeOKFrame() {
uint8_t buf[1]; uint8_t buf[1];
buf[0] = RESP_CODE_OK; buf[0] = RESP_CODE_OK;
@@ -262,20 +276,64 @@ bool MyMesh::isAutoAddEnabled() const {
return (_prefs.manual_add_contacts & 1) == 0; return (_prefs.manual_add_contacts & 1) == 0;
} }
bool MyMesh::shouldAutoAddContactType(uint8_t contact_type) const {
if ((_prefs.manual_add_contacts & 1) == 0) {
return true;
}
uint8_t type_bit = 0;
switch (contact_type) {
case ADV_TYPE_CHAT:
type_bit = AUTO_ADD_CHAT;
break;
case ADV_TYPE_REPEATER:
type_bit = AUTO_ADD_REPEATER;
break;
case ADV_TYPE_ROOM:
type_bit = AUTO_ADD_ROOM_SERVER;
break;
case ADV_TYPE_SENSOR:
type_bit = AUTO_ADD_SENSOR;
break;
default:
return false; // Unknown type, don't auto-add
}
return (_prefs.autoadd_config & type_bit) != 0;
}
bool MyMesh::shouldOverwriteWhenFull() const {
return (_prefs.autoadd_config & AUTO_ADD_OVERWRITE_OLDEST) != 0;
}
void MyMesh::onContactOverwrite(const uint8_t* pub_key) {
if (_serial->isConnected()) {
out_frame[0] = PUSH_CODE_CONTACT_DELETED;
memcpy(&out_frame[1], pub_key, PUB_KEY_SIZE);
_serial->writeFrame(out_frame, 1 + PUB_KEY_SIZE);
}
}
void MyMesh::onContactsFull() {
if (_serial->isConnected()) {
out_frame[0] = PUSH_CODE_CONTACTS_FULL;
_serial->writeFrame(out_frame, 1);
}
}
void MyMesh::onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path_len, const uint8_t* path) { void MyMesh::onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path_len, const uint8_t* path) {
if (_serial->isConnected()) { if (_serial->isConnected()) {
if (!isAutoAddEnabled() && is_new) { if (is_new) {
writeContactRespFrame(PUSH_CODE_NEW_ADVERT, contact); writeContactRespFrame(PUSH_CODE_NEW_ADVERT, contact);
} else { } else {
out_frame[0] = PUSH_CODE_ADVERT; out_frame[0] = PUSH_CODE_ADVERT;
memcpy(&out_frame[1], contact.id.pub_key, PUB_KEY_SIZE); memcpy(&out_frame[1], contact.id.pub_key, PUB_KEY_SIZE);
_serial->writeFrame(out_frame, 1 + PUB_KEY_SIZE); _serial->writeFrame(out_frame, 1 + PUB_KEY_SIZE);
} }
} else {
#ifdef DISPLAY_CLASS
if (_ui) _ui->notify(UIEventType::newContactMessage);
#endif
} }
#ifdef DISPLAY_CLASS
if (_ui && !_prefs.buzzer_quiet) _ui->notify(UIEventType::newContactMessage); //buzz if enabled
#endif
// add inbound-path to mem cache // add inbound-path to mem cache
if (path && path_len <= sizeof(AdvertPath::path)) { // check path is valid if (path && path_len <= sizeof(AdvertPath::path)) { // check path is valid
@@ -299,7 +357,7 @@ void MyMesh::onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path
memcpy(p->path, path, p->path_len); memcpy(p->path, path, p->path_len);
} }
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); if (!is_new) dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // only schedule lazy write for contacts that are in contacts[]
} }
static int sort_by_recent(const void *a, const void *b) { static int sort_by_recent(const void *a, const void *b) {
@@ -382,9 +440,7 @@ void MyMesh::queueMessage(const ContactInfo &from, uint8_t txt_type, mesh::Packe
bool should_display = txt_type == TXT_TYPE_PLAIN || txt_type == TXT_TYPE_SIGNED_PLAIN; bool should_display = txt_type == TXT_TYPE_PLAIN || txt_type == TXT_TYPE_SIGNED_PLAIN;
if (should_display && _ui) { if (should_display && _ui) {
_ui->newMsg(path_len, from.name, text, offline_queue_len); _ui->newMsg(path_len, from.name, text, offline_queue_len);
if (!_serial->isConnected()) { if (!_prefs.buzzer_quiet) _ui->notify(UIEventType::contactMessage); //buzz if enabled
_ui->notify(UIEventType::contactMessage);
}
} }
#endif #endif
} }
@@ -469,11 +525,8 @@ void MyMesh::onChannelMessageRecv(const mesh::GroupChannel &channel, mesh::Packe
uint8_t frame[1]; uint8_t frame[1];
frame[0] = PUSH_CODE_MSG_WAITING; // send push 'tickle' frame[0] = PUSH_CODE_MSG_WAITING; // send push 'tickle'
_serial->writeFrame(frame, 1); _serial->writeFrame(frame, 1);
} else {
#ifdef DISPLAY_CLASS
if (_ui) _ui->notify(UIEventType::channelMessage);
#endif
} }
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS
// Get the channel name from the channel index // Get the channel name from the channel index
const char *channel_name = "Unknown"; const char *channel_name = "Unknown";
@@ -481,7 +534,10 @@ void MyMesh::onChannelMessageRecv(const mesh::GroupChannel &channel, mesh::Packe
if (getChannel(channel_idx, channel_details)) { if (getChannel(channel_idx, channel_details)) {
channel_name = channel_details.name; channel_name = channel_details.name;
} }
if (_ui) _ui->newMsg(path_len, channel_name, text, offline_queue_len); if (_ui) {
_ui->newMsg(path_len, channel_name, text, offline_queue_len);
if (!_prefs.buzzer_quiet) _ui->notify(UIEventType::channelMessage); //buzz if enabled
}
#endif #endif
} }
@@ -733,13 +789,14 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
// defaults // defaults
memset(&_prefs, 0, sizeof(_prefs)); memset(&_prefs, 0, sizeof(_prefs));
_prefs.airtime_factor = 1.0; // one half _prefs.airtime_factor = 1.0;
strcpy(_prefs.node_name, "NONAME"); strcpy(_prefs.node_name, "NONAME");
_prefs.freq = LORA_FREQ; _prefs.freq = LORA_FREQ;
_prefs.sf = LORA_SF; _prefs.sf = LORA_SF;
_prefs.bw = LORA_BW; _prefs.bw = LORA_BW;
_prefs.cr = LORA_CR; _prefs.cr = LORA_CR;
_prefs.tx_power_dbm = LORA_TX_POWER; _prefs.tx_power_dbm = LORA_TX_POWER;
_prefs.buzzer_quiet = 0;
_prefs.gps_enabled = 0; // GPS disabled by default _prefs.gps_enabled = 0; // GPS disabled by default
_prefs.gps_interval = 0; // No automatic GPS updates by default _prefs.gps_interval = 0; // No automatic GPS updates by default
//_prefs.rx_delay_base = 10.0f; enable once new algo fixed //_prefs.rx_delay_base = 10.0f; enable once new algo fixed
@@ -758,14 +815,14 @@ void MyMesh::begin(bool has_display) {
_store->saveMainIdentity(self_id); _store->saveMainIdentity(self_id);
} }
// if name is provided as a build flag, use that as default node name instead
#ifdef ADVERT_NAME
strcpy(_prefs.node_name, ADVERT_NAME);
#else
// use hex of first 4 bytes of identity public key as default node name // use hex of first 4 bytes of identity public key as default node name
char pub_key_hex[10]; char pub_key_hex[10];
mesh::Utils::toHex(pub_key_hex, self_id.pub_key, 4); mesh::Utils::toHex(pub_key_hex, self_id.pub_key, 4);
strcpy(_prefs.node_name, pub_key_hex); strcpy(_prefs.node_name, pub_key_hex);
// if name is provided as a build flag, use that as default node name instead
#ifdef ADVERT_NAME
strcpy(_prefs.node_name, ADVERT_NAME);
#endif #endif
// load persisted prefs // load persisted prefs
@@ -779,6 +836,7 @@ void MyMesh::begin(bool has_display) {
_prefs.sf = constrain(_prefs.sf, 5, 12); _prefs.sf = constrain(_prefs.sf, 5, 12);
_prefs.cr = constrain(_prefs.cr, 5, 8); _prefs.cr = constrain(_prefs.cr, 5, 8);
_prefs.tx_power_dbm = constrain(_prefs.tx_power_dbm, 1, MAX_LORA_TX_POWER); _prefs.tx_power_dbm = constrain(_prefs.tx_power_dbm, 1, MAX_LORA_TX_POWER);
_prefs.buzzer_quiet = constrain(_prefs.buzzer_quiet, 0, 1); // Ensure boolean 0 or 1
_prefs.gps_enabled = constrain(_prefs.gps_enabled, 0, 1); // Ensure boolean 0 or 1 _prefs.gps_enabled = constrain(_prefs.gps_enabled, 0, 1); // Ensure boolean 0 or 1
_prefs.gps_interval = constrain(_prefs.gps_interval, 0, 86400); // Max 24 hours _prefs.gps_interval = constrain(_prefs.gps_interval, 0, 86400); // Max 24 hours
@@ -803,6 +861,7 @@ void MyMesh::begin(bool has_display) {
resetContacts(); resetContacts();
_store->loadContacts(this); _store->loadContacts(this);
bootstrapRTCfromContacts();
addChannel("Public", PUBLIC_GROUP_PSK); // pre-configure Andy's public channel addChannel("Public", PUBLIC_GROUP_PSK); // pre-configure Andy's public channel
_store->loadChannels(this); _store->loadChannels(this);
@@ -1235,16 +1294,20 @@ void MyMesh::handleCmdFrame(size_t len) {
#endif #endif
} else if (cmd_frame[0] == CMD_IMPORT_PRIVATE_KEY && len >= 65) { } else if (cmd_frame[0] == CMD_IMPORT_PRIVATE_KEY && len >= 65) {
#if ENABLE_PRIVATE_KEY_IMPORT #if ENABLE_PRIVATE_KEY_IMPORT
mesh::LocalIdentity identity; if (!mesh::LocalIdentity::validatePrivateKey(&cmd_frame[1])) {
identity.readFrom(&cmd_frame[1], 64); writeErrFrame(ERR_CODE_ILLEGAL_ARG); // invalid key
if (_store->saveMainIdentity(identity)) {
self_id = identity;
writeOKFrame();
// re-load contacts, to invalidate ecdh shared_secrets
resetContacts();
_store->loadContacts(this);
} else { } else {
writeErrFrame(ERR_CODE_FILE_IO_ERROR); mesh::LocalIdentity identity;
identity.readFrom(&cmd_frame[1], 64);
if (_store->saveMainIdentity(identity)) {
self_id = identity;
writeOKFrame();
// re-load contacts, to invalidate ecdh shared_secrets
resetContacts();
_store->loadContacts(this);
} else {
writeErrFrame(ERR_CODE_FILE_IO_ERROR);
}
} }
#else #else
writeDisabledFrame(); writeDisabledFrame();
@@ -1663,6 +1726,15 @@ void MyMesh::handleCmdFrame(size_t len) {
} else { } else {
writeErrFrame(ERR_CODE_TABLE_FULL); writeErrFrame(ERR_CODE_TABLE_FULL);
} }
} else if (cmd_frame[0] == CMD_SET_AUTOADD_CONFIG) {
_prefs.autoadd_config = cmd_frame[1];
savePrefs();
writeOKFrame();
} else if (cmd_frame[0] == CMD_GET_AUTOADD_CONFIG) {
int i = 0;
out_frame[i++] = RESP_CODE_AUTOADD_CONFIG;
out_frame[i++] = _prefs.autoadd_config;
_serial->writeFrame(out_frame, i);
} else { } else {
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD); writeErrFrame(ERR_CODE_UNSUPPORTED_CMD);
MESH_DEBUG_PRINTLN("ERROR: unknown command: %02X", cmd_frame[0]); MESH_DEBUG_PRINTLN("ERROR: unknown command: %02X", cmd_frame[0]);

View File

@@ -114,6 +114,10 @@ protected:
void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override; void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;
bool isAutoAddEnabled() const override; bool isAutoAddEnabled() const override;
bool shouldAutoAddContactType(uint8_t type) const override;
bool shouldOverwriteWhenFull() const override;
void onContactsFull() override;
void onContactOverwrite(const uint8_t* pub_key) override;
bool onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_t in_path_len, uint8_t* out_path, uint8_t out_path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override; bool onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_t in_path_len, uint8_t* out_path, uint8_t out_path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
void onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path_len, const uint8_t* path) override; void onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path_len, const uint8_t* path) override;
void onContactPathUpdated(const ContactInfo &contact) override; void onContactPathUpdated(const ContactInfo &contact) override;

View File

@@ -27,4 +27,5 @@ struct NodePrefs { // persisted to file
uint8_t buzzer_quiet; uint8_t buzzer_quiet;
uint8_t gps_enabled; // GPS enabled flag (0=disabled, 1=enabled) uint8_t gps_enabled; // GPS enabled flag (0=disabled, 1=enabled)
uint32_t gps_interval; // GPS read interval in seconds uint32_t gps_interval; // GPS read interval in seconds
uint8_t autoadd_config; // bitmask for auto-add contacts config
}; };

View File

@@ -151,9 +151,7 @@ void setup() {
); );
#ifdef BLE_PIN_CODE #ifdef BLE_PIN_CODE
char dev_name[32+16]; serial_interface.begin(BLE_NAME_PREFIX, the_mesh.getNodePrefs()->node_name, the_mesh.getBLEPin());
sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
serial_interface.begin(dev_name, the_mesh.getBLEPin());
#else #else
serial_interface.begin(Serial); serial_interface.begin(Serial);
#endif #endif
@@ -199,9 +197,7 @@ void setup() {
WiFi.begin(WIFI_SSID, WIFI_PWD); WiFi.begin(WIFI_SSID, WIFI_PWD);
serial_interface.begin(TCP_PORT); serial_interface.begin(TCP_PORT);
#elif defined(BLE_PIN_CODE) #elif defined(BLE_PIN_CODE)
char dev_name[32+16]; serial_interface.begin(BLE_NAME_PREFIX, the_mesh.getNodePrefs()->node_name, the_mesh.getBLEPin());
sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
serial_interface.begin(dev_name, the_mesh.getBLEPin());
#elif defined(SERIAL_RX) #elif defined(SERIAL_RX)
companion_serial.setPins(SERIAL_RX, SERIAL_TX); companion_serial.setPins(SERIAL_RX, SERIAL_TX);
companion_serial.begin(115200); companion_serial.begin(115200);

View File

@@ -226,7 +226,7 @@ int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t
stats.n_direct_dups = ((SimpleMeshTables *)getTables())->getNumDirectDups(); stats.n_direct_dups = ((SimpleMeshTables *)getTables())->getNumDirectDups();
stats.n_flood_dups = ((SimpleMeshTables *)getTables())->getNumFloodDups(); stats.n_flood_dups = ((SimpleMeshTables *)getTables())->getNumFloodDups();
stats.total_rx_air_time_secs = getReceiveAirTime() / 1000; stats.total_rx_air_time_secs = getReceiveAirTime() / 1000;
stats.n_recv_errors = radio_driver.getPacketsRecvErrors();
memcpy(&reply_data[4], &stats, sizeof(stats)); memcpy(&reply_data[4], &stats, sizeof(stats));
return 4 + sizeof(stats); // reply_len return 4 + sizeof(stats); // reply_len
@@ -390,6 +390,14 @@ bool MyMesh::allowPacketForward(const mesh::Packet *packet) {
MESH_DEBUG_PRINTLN("allowPacketForward: unknown transport code, or wildcard not allowed for FLOOD packet"); MESH_DEBUG_PRINTLN("allowPacketForward: unknown transport code, or wildcard not allowed for FLOOD packet");
return false; return false;
} }
// Limit flood advert paket forwarding using a probabilistic reduction defined by P(h) = 0.308^(hops-1)
// https://github.com/meshcore-dev/MeshCore/issues/1223
double_t roll_dice = (double)rand() / RAND_MAX;
double_t forw_prob = pow(_prefs.flood_advert_base, packet->path_len - 1);
if (packet->getPayloadType() == PAYLOAD_TYPE_ADVERT && packet->isRouteFlood() && roll_dice > forw_prob)
return false;
// all other packets
return true; return true;
} }
@@ -744,7 +752,7 @@ void MyMesh::onControlDataRecv(mesh::Packet* packet) {
MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondClock &ms, mesh::RNG &rng, MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondClock &ms, mesh::RNG &rng,
mesh::RTCClock &rtc, mesh::MeshTables &tables) mesh::RTCClock &rtc, mesh::MeshTables &tables)
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables), : mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
_cli(board, rtc, sensors, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4), region_map(key_store), temp_map(key_store), _cli(board, rtc, sensors, acl, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4), region_map(key_store), temp_map(key_store),
discover_limiter(4, 120), // max 4 every 2 minutes discover_limiter(4, 120), // max 4 every 2 minutes
anon_limiter(4, 180) // max 4 every 3 minutes anon_limiter(4, 180) // max 4 every 3 minutes
#if defined(WITH_RS232_BRIDGE) #if defined(WITH_RS232_BRIDGE)
@@ -768,7 +776,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
// defaults // defaults
memset(&_prefs, 0, sizeof(_prefs)); memset(&_prefs, 0, sizeof(_prefs));
_prefs.airtime_factor = 1.0; // one half _prefs.airtime_factor = 1.0;
_prefs.rx_delay_base = 0.0f; // turn off by default, was 10.0; _prefs.rx_delay_base = 0.0f; // turn off by default, was 10.0;
_prefs.tx_delay_factor = 0.5f; // was 0.25f _prefs.tx_delay_factor = 0.5f; // was 0.25f
_prefs.direct_tx_delay_factor = 0.2f; // was zero _prefs.direct_tx_delay_factor = 0.2f; // was zero
@@ -783,6 +791,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
_prefs.tx_power_dbm = LORA_TX_POWER; _prefs.tx_power_dbm = LORA_TX_POWER;
_prefs.advert_interval = 1; // default to 2 minutes for NEW installs _prefs.advert_interval = 1; // default to 2 minutes for NEW installs
_prefs.flood_advert_interval = 12; // 12 hours _prefs.flood_advert_interval = 12; // 12 hours
_prefs.flood_advert_base = 0.308f;
_prefs.flood_max = 64; _prefs.flood_max = 64;
_prefs.interference_threshold = 0; // disabled _prefs.interference_threshold = 0; // disabled
@@ -808,7 +817,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
_fs = fs; _fs = fs;
// load persisted prefs // load persisted prefs
_cli.loadPrefs(_fs); _cli.loadPrefs(_fs);
acl.load(_fs); acl.load(_fs, self_id);
// TODO: key_store.begin(); // TODO: key_store.begin();
region_map.load(_fs); region_map.load(_fs);
@@ -854,10 +863,14 @@ bool MyMesh::formatFileSystem() {
#endif #endif
} }
void MyMesh::sendSelfAdvertisement(int delay_millis) { void MyMesh::sendSelfAdvertisement(int delay_millis, bool flood) {
mesh::Packet *pkt = createSelfAdvert(); mesh::Packet *pkt = createSelfAdvert();
if (pkt) { if (pkt) {
sendFlood(pkt, delay_millis); if (flood) {
sendFlood(pkt, delay_millis);
} else {
sendZeroHop(pkt, delay_millis);
}
} else { } else {
MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!"); MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!");
} }
@@ -968,7 +981,6 @@ void MyMesh::formatPacketStatsReply(char *reply) {
} }
void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) { void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) {
self_id = new_id;
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
IdentityStore store(*_fs, ""); IdentityStore store(*_fs, "");
#elif defined(ESP32) #elif defined(ESP32)
@@ -978,7 +990,7 @@ void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) {
#else #else
#error "need to define saveIdentity()" #error "need to define saveIdentity()"
#endif #endif
store.save("_main", self_id); store.save("_main", new_id);
} }
void MyMesh::clearStats() { void MyMesh::clearStats() {

View File

@@ -54,6 +54,7 @@ struct RepeaterStats {
int16_t last_snr; // x 4 int16_t last_snr; // x 4
uint16_t n_direct_dups, n_flood_dups; uint16_t n_direct_dups, n_flood_dups;
uint32_t total_rx_air_time_secs; uint32_t total_rx_air_time_secs;
uint32_t n_recv_errors;
}; };
#ifndef MAX_CLIENTS #ifndef MAX_CLIENTS
@@ -86,11 +87,11 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
unsigned long next_local_advert, next_flood_advert; unsigned long next_local_advert, next_flood_advert;
bool _logging; bool _logging;
NodePrefs _prefs; NodePrefs _prefs;
ClientACL acl;
CommonCLI _cli; CommonCLI _cli;
uint8_t reply_data[MAX_PACKET_PAYLOAD]; uint8_t reply_data[MAX_PACKET_PAYLOAD];
uint8_t reply_path[MAX_PATH_SIZE]; uint8_t reply_path[MAX_PATH_SIZE];
int8_t reply_path_len; int8_t reply_path_len;
ClientACL acl;
TransportKeyStore key_store; TransportKeyStore key_store;
RegionMap region_map, temp_map; RegionMap region_map, temp_map;
RegionEntry* load_stack[8]; RegionEntry* load_stack[8];
@@ -186,7 +187,7 @@ public:
void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override; void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override;
bool formatFileSystem() override; bool formatFileSystem() override;
void sendSelfAdvertisement(int delay_millis) override; void sendSelfAdvertisement(int delay_millis, bool flood) override;
void updateAdvertTimer() override; void updateAdvertTimer() override;
void updateFloodAdvertTimer() override; void updateFloodAdvertTimer() override;

View File

@@ -87,8 +87,10 @@ void setup() {
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION); ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
#endif #endif
// send out initial Advertisement to the mesh // send out initial zero hop Advertisement to the mesh
the_mesh.sendSelfAdvertisement(16000); #if ENABLE_ADVERT_ON_BOOT == 1
the_mesh.sendSelfAdvertisement(16000, false);
#endif
} }
void loop() { void loop() {

View File

@@ -275,6 +275,15 @@ uint32_t MyMesh::getDirectRetransmitDelay(const mesh::Packet *packet) {
bool MyMesh::allowPacketForward(const mesh::Packet *packet) { bool MyMesh::allowPacketForward(const mesh::Packet *packet) {
if (_prefs.disable_fwd) return false; if (_prefs.disable_fwd) return false;
if (packet->isRouteFlood() && packet->path_len >= _prefs.flood_max) return false; if (packet->isRouteFlood() && packet->path_len >= _prefs.flood_max) return false;
// Limit flood advert paket forwarding using a probabilistic reduction defined by P(h) = 0.308^(hops-1)
// https://github.com/meshcore-dev/MeshCore/issues/1223
double_t roll_dice = (double)rand() / RAND_MAX;
double_t forw_prob = pow(_prefs.flood_advert_base, packet->path_len - 1);
if (packet->getPayloadType() == PAYLOAD_TYPE_ADVERT && packet->isRouteFlood() && roll_dice > forw_prob)
return false;
// all other packets
return true; return true;
} }
@@ -587,7 +596,7 @@ void MyMesh::onAckRecv(mesh::Packet *packet, uint32_t ack_crc) {
MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondClock &ms, mesh::RNG &rng, MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondClock &ms, mesh::RNG &rng,
mesh::RTCClock &rtc, mesh::MeshTables &tables) mesh::RTCClock &rtc, mesh::MeshTables &tables)
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables), : mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
_cli(board, rtc, sensors, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4) { _cli(board, rtc, sensors, acl, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4) {
last_millis = 0; last_millis = 0;
uptime_millis = 0; uptime_millis = 0;
next_local_advert = next_flood_advert = 0; next_local_advert = next_flood_advert = 0;
@@ -597,7 +606,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
// defaults // defaults
memset(&_prefs, 0, sizeof(_prefs)); memset(&_prefs, 0, sizeof(_prefs));
_prefs.airtime_factor = 1.0; // one half _prefs.airtime_factor = 1.0;
_prefs.rx_delay_base = 0.0f; // off by default, was 10.0 _prefs.rx_delay_base = 0.0f; // off by default, was 10.0
_prefs.tx_delay_factor = 0.5f; // was 0.25f; _prefs.tx_delay_factor = 0.5f; // was 0.25f;
_prefs.direct_tx_delay_factor = 0.2f; // was zero _prefs.direct_tx_delay_factor = 0.2f; // was zero
@@ -613,6 +622,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
_prefs.disable_fwd = 1; _prefs.disable_fwd = 1;
_prefs.advert_interval = 1; // default to 2 minutes for NEW installs _prefs.advert_interval = 1; // default to 2 minutes for NEW installs
_prefs.flood_advert_interval = 12; // 12 hours _prefs.flood_advert_interval = 12; // 12 hours
_prefs.flood_advert_base = 0.308f;
_prefs.flood_max = 64; _prefs.flood_max = 64;
_prefs.interference_threshold = 0; // disabled _prefs.interference_threshold = 0; // disabled
#ifdef ROOM_PASSWORD #ifdef ROOM_PASSWORD
@@ -637,7 +647,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
// load persisted prefs // load persisted prefs
_cli.loadPrefs(_fs); _cli.loadPrefs(_fs);
acl.load(_fs); acl.load(_fs, self_id);
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
radio_set_tx_power(_prefs.tx_power_dbm); radio_set_tx_power(_prefs.tx_power_dbm);
@@ -675,10 +685,14 @@ bool MyMesh::formatFileSystem() {
#endif #endif
} }
void MyMesh::sendSelfAdvertisement(int delay_millis) { void MyMesh::sendSelfAdvertisement(int delay_millis, bool flood) {
mesh::Packet *pkt = createSelfAdvert(); mesh::Packet *pkt = createSelfAdvert();
if (pkt) { if (pkt) {
sendFlood(pkt, delay_millis); if (flood) {
sendFlood(pkt, delay_millis);
} else {
sendZeroHop(pkt, delay_millis);
}
} else { } else {
MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!"); MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!");
} }
@@ -720,7 +734,6 @@ void MyMesh::setTxPower(uint8_t power_dbm) {
} }
void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) { void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) {
self_id = new_id;
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
IdentityStore store(*_fs, ""); IdentityStore store(*_fs, "");
#elif defined(ESP32) #elif defined(ESP32)
@@ -730,7 +743,7 @@ void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) {
#else #else
#error "need to define saveIdentity()" #error "need to define saveIdentity()"
#endif #endif
store.save("_main", self_id); store.save("_main", new_id);
} }
void MyMesh::clearStats() { void MyMesh::clearStats() {
@@ -815,7 +828,7 @@ void MyMesh::loop() {
if (c->extra.room.pending_ack && millisHasNowPassed(c->extra.room.ack_timeout)) { if (c->extra.room.pending_ack && millisHasNowPassed(c->extra.room.ack_timeout)) {
c->extra.room.push_failures++; c->extra.room.push_failures++;
c->extra.room.pending_ack = 0; // reset (TODO: keep prev expected_ack's in a list, incase they arrive LATER, after we retry) c->extra.room.pending_ack = 0; // reset (TODO: keep prev expected_ack's in a list, incase they arrive LATER, after we retry)
MESH_DEBUG_PRINTLN("pending ACK timed out: push_failures: %d", (uint32_t)c->push_failures); MESH_DEBUG_PRINTLN("pending ACK timed out: push_failures: %d", (uint32_t)c->extra.room.push_failures);
} }
} }
// check next Round-Robin client, and sync next new post // check next Round-Robin client, and sync next new post

View File

@@ -94,8 +94,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
unsigned long next_local_advert, next_flood_advert; unsigned long next_local_advert, next_flood_advert;
bool _logging; bool _logging;
NodePrefs _prefs; NodePrefs _prefs;
CommonCLI _cli;
ClientACL acl; ClientACL acl;
CommonCLI _cli;
unsigned long dirty_contacts_expiry; unsigned long dirty_contacts_expiry;
uint8_t reply_data[MAX_PACKET_PAYLOAD]; uint8_t reply_data[MAX_PACKET_PAYLOAD];
unsigned long next_push; unsigned long next_push;
@@ -177,7 +177,7 @@ public:
void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override; void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override;
bool formatFileSystem() override; bool formatFileSystem() override;
void sendSelfAdvertisement(int delay_millis) override; void sendSelfAdvertisement(int delay_millis, bool flood) override;
void updateAdvertTimer() override; void updateAdvertTimer() override;
void updateFloodAdvertTimer() override; void updateFloodAdvertTimer() override;

View File

@@ -76,8 +76,10 @@ void setup() {
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION); ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
#endif #endif
// send out initial Advertisement to the mesh // send out initial zero hop Advertisement to the mesh
the_mesh.sendSelfAdvertisement(16000); #if ENABLE_ADVERT_ON_BOOT == 1
the_mesh.sendSelfAdvertisement(16000, false);
#endif
} }
void loop() { void loop() {

View File

@@ -280,7 +280,7 @@ public:
{ {
// defaults // defaults
memset(&_prefs, 0, sizeof(_prefs)); memset(&_prefs, 0, sizeof(_prefs));
_prefs.airtime_factor = 2.0; // one third _prefs.airtime_factor = 1.0;
strcpy(_prefs.node_name, "NONAME"); strcpy(_prefs.node_name, "NONAME");
_prefs.freq = LORA_FREQ; _prefs.freq = LORA_FREQ;
_prefs.tx_power_dbm = LORA_TX_POWER; _prefs.tx_power_dbm = LORA_TX_POWER;
@@ -582,7 +582,9 @@ void setup() {
the_mesh.showWelcome(); the_mesh.showWelcome();
// send out initial Advertisement to the mesh // send out initial Advertisement to the mesh
#if ENABLE_ADVERT_ON_BOOT == 1
the_mesh.sendSelfAdvert(1200); // add slight delay the_mesh.sendSelfAdvert(1200); // add slight delay
#endif
} }
void loop() { void loop() {

View File

@@ -695,7 +695,7 @@ void SensorMesh::onAckRecv(mesh::Packet* packet, uint32_t ack_crc) {
SensorMesh::SensorMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables) SensorMesh::SensorMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables)
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables), : mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
_cli(board, rtc, sensors, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4) _cli(board, rtc, sensors, acl, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4)
{ {
next_local_advert = next_flood_advert = 0; next_local_advert = next_flood_advert = 0;
dirty_contacts_expiry = 0; dirty_contacts_expiry = 0;
@@ -705,7 +705,7 @@ SensorMesh::SensorMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::Millise
// defaults // defaults
memset(&_prefs, 0, sizeof(_prefs)); memset(&_prefs, 0, sizeof(_prefs));
_prefs.airtime_factor = 1.0; // one half _prefs.airtime_factor = 1.0;
_prefs.rx_delay_base = 0.0f; // turn off by default, was 10.0; _prefs.rx_delay_base = 0.0f; // turn off by default, was 10.0;
_prefs.tx_delay_factor = 0.5f; // was 0.25f _prefs.tx_delay_factor = 0.5f; // was 0.25f
_prefs.direct_tx_delay_factor = 0.2f; // was zero _prefs.direct_tx_delay_factor = 0.2f; // was zero
@@ -736,7 +736,7 @@ void SensorMesh::begin(FILESYSTEM* fs) {
// load persisted prefs // load persisted prefs
_cli.loadPrefs(_fs); _cli.loadPrefs(_fs);
acl.load(_fs); acl.load(_fs, self_id);
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
radio_set_tx_power(_prefs.tx_power_dbm); radio_set_tx_power(_prefs.tx_power_dbm);
@@ -765,7 +765,6 @@ bool SensorMesh::formatFileSystem() {
} }
void SensorMesh::saveIdentity(const mesh::LocalIdentity& new_id) { void SensorMesh::saveIdentity(const mesh::LocalIdentity& new_id) {
self_id = new_id;
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
IdentityStore store(*_fs, ""); IdentityStore store(*_fs, "");
#elif defined(ESP32) #elif defined(ESP32)
@@ -775,7 +774,7 @@ void SensorMesh::saveIdentity(const mesh::LocalIdentity& new_id) {
#else #else
#error "need to define saveIdentity()" #error "need to define saveIdentity()"
#endif #endif
store.save("_main", self_id); store.save("_main", new_id);
} }
void SensorMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) { void SensorMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) {
@@ -788,10 +787,14 @@ void SensorMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t
revert_radio_at = futureMillis(2000 + timeout_mins*60*1000); // schedule when to revert radio params revert_radio_at = futureMillis(2000 + timeout_mins*60*1000); // schedule when to revert radio params
} }
void SensorMesh::sendSelfAdvertisement(int delay_millis) { void SensorMesh::sendSelfAdvertisement(int delay_millis, bool flood) {
mesh::Packet* pkt = createSelfAdvert(); mesh::Packet* pkt = createSelfAdvert();
if (pkt) { if (pkt) {
sendFlood(pkt, delay_millis); if (flood) {
sendFlood(pkt, delay_millis);
} else {
sendZeroHop(pkt, delay_millis);
}
} else { } else {
MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!"); MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!");
} }

View File

@@ -60,7 +60,7 @@ public:
NodePrefs* getNodePrefs() { return &_prefs; } NodePrefs* getNodePrefs() { return &_prefs; }
void savePrefs() override { _cli.savePrefs(_fs); } void savePrefs() override { _cli.savePrefs(_fs); }
bool formatFileSystem() override; bool formatFileSystem() override;
void sendSelfAdvertisement(int delay_millis) override; void sendSelfAdvertisement(int delay_millis, bool flood) override;
void updateAdvertTimer() override; void updateAdvertTimer() override;
void updateFloodAdvertTimer() override; void updateFloodAdvertTimer() override;
void setLoggingOn(bool enable) override { } void setLoggingOn(bool enable) override { }
@@ -133,9 +133,9 @@ private:
FILESYSTEM* _fs; FILESYSTEM* _fs;
unsigned long next_local_advert, next_flood_advert; unsigned long next_local_advert, next_flood_advert;
NodePrefs _prefs; NodePrefs _prefs;
ClientACL acl;
CommonCLI _cli; CommonCLI _cli;
uint8_t reply_data[MAX_PACKET_PAYLOAD]; uint8_t reply_data[MAX_PACKET_PAYLOAD];
ClientACL acl;
unsigned long dirty_contacts_expiry; unsigned long dirty_contacts_expiry;
CayenneLPP telemetry; CayenneLPP telemetry;
uint32_t last_read_time; uint32_t last_read_time;

View File

@@ -110,8 +110,10 @@ void setup() {
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION); ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
#endif #endif
// send out initial Advertisement to the mesh // send out initial zero hop Advertisement to the mesh
the_mesh.sendSelfAdvertisement(16000); #if ENABLE_ADVERT_ON_BOOT == 1
the_mesh.sendSelfAdvertisement(16000, false);
#endif
} }
void loop() { void loop() {

8
fetch_prs.sh Executable file
View File

@@ -0,0 +1,8 @@
#!/bin/sh
git branch -D pr-1297
git branch -D pr-1338
# fetch PRs
git fetch upstream pull/1338/head:pr-1338
git fetch upstream pull/1297/head:pr-1297

8
merge_prs.sh Executable file
View File

@@ -0,0 +1,8 @@
#!/bin/sh
git merge pr-1338 --no-edit -m "Integration of upstrem PR #1338"
git merge pr-1297 --no-edit -m "Integration of upstrem PR #1297"
git merge pio-ini-adjustments -m "platformio.ini: Adjust defaults for LoRa frequncies and advert interval limits"

View File

@@ -24,9 +24,11 @@ lib_deps =
melopero/Melopero RV3028 @ ^1.1.0 melopero/Melopero RV3028 @ ^1.1.0
electroniccats/CayenneLPP @ 1.6.1 electroniccats/CayenneLPP @ 1.6.1
build_flags = -w -DNDEBUG -DRADIOLIB_STATIC_ONLY=1 -DRADIOLIB_GODMODE=1 build_flags = -w -DNDEBUG -DRADIOLIB_STATIC_ONLY=1 -DRADIOLIB_GODMODE=1
-D LORA_FREQ=869.525 -D LORA_FREQ=869.618
-D LORA_BW=250 -D LORA_BW=62.5
-D LORA_SF=11 -D LORA_SF=8
-D LORA_CR=8
-D ENABLE_ADVERT_ON_BOOT=1
-D ENABLE_PRIVATE_KEY_IMPORT=1 ; NOTE: comment these out for more secure firmware -D ENABLE_PRIVATE_KEY_IMPORT=1 ; NOTE: comment these out for more secure firmware
-D ENABLE_PRIVATE_KEY_EXPORT=1 -D ENABLE_PRIVATE_KEY_EXPORT=1
-D RADIOLIB_EXCLUDE_CC1101=1 -D RADIOLIB_EXCLUDE_CC1101=1

View File

@@ -20,12 +20,34 @@ void Dispatcher::begin() {
_err_flags = 0; _err_flags = 0;
radio_nonrx_start = _ms->getMillis(); radio_nonrx_start = _ms->getMillis();
duty_cycle_window_ms = getDutyCycleWindowMs();
float duty_cycle = 1.0f / (1.0f + getAirtimeBudgetFactor());
tx_budget_ms = (unsigned long)(duty_cycle_window_ms * duty_cycle);
last_budget_update = _ms->getMillis();
_radio->begin(); _radio->begin();
prev_isrecv_mode = _radio->isInRecvMode(); prev_isrecv_mode = _radio->isInRecvMode();
} }
float Dispatcher::getAirtimeBudgetFactor() const { float Dispatcher::getAirtimeBudgetFactor() const {
return 2.0; // default, 33.3% (1/3rd) return 1.0;
}
void Dispatcher::updateTxBudget() {
unsigned long now = _ms->getMillis();
unsigned long elapsed = now - last_budget_update;
float duty_cycle = 1.0f / (1.0f + getAirtimeBudgetFactor());
unsigned long max_budget = (unsigned long)(getDutyCycleWindowMs() * duty_cycle);
unsigned long refill = (unsigned long)(elapsed * duty_cycle);
tx_budget_ms += refill;
if (tx_budget_ms > max_budget) {
tx_budget_ms = max_budget;
}
last_budget_update = now;
} }
int Dispatcher::calcRxDelay(float score, uint32_t air_time) const { int Dispatcher::calcRxDelay(float score, uint32_t air_time) const {
@@ -61,11 +83,24 @@ void Dispatcher::loop() {
if (outbound) { // waiting for outbound send to be completed if (outbound) { // waiting for outbound send to be completed
if (_radio->isSendComplete()) { if (_radio->isSendComplete()) {
long t = _ms->getMillis() - outbound_start; long t = _ms->getMillis() - outbound_start;
total_air_time += t; // keep track of how much air time we are using total_air_time += t;
//Serial.print(" airtime="); Serial.println(t); //Serial.print(" airtime="); Serial.println(t);
// will need radio silence up to next_tx_time updateTxBudget();
next_tx_time = futureMillis(t * getAirtimeBudgetFactor());
if (t > tx_budget_ms) {
tx_budget_ms = 0;
} else {
tx_budget_ms -= t;
}
if (tx_budget_ms < 100) {
float duty_cycle = 1.0f / (1.0f + getAirtimeBudgetFactor());
unsigned long needed = 100 - tx_budget_ms;
next_tx_time = futureMillis((unsigned long)(needed / duty_cycle));
} else {
next_tx_time = _ms->getMillis();
}
_radio->onSendFinished(); _radio->onSendFinished();
logTx(outbound, 2 + outbound->path_len + outbound->payload_len); logTx(outbound, 2 + outbound->path_len + outbound->payload_len);
@@ -224,9 +259,20 @@ void Dispatcher::processRecvPacket(Packet* pkt) {
} }
void Dispatcher::checkSend() { void Dispatcher::checkSend() {
if (_mgr->getOutboundCount(_ms->getMillis()) == 0) return; // nothing waiting to send if (_mgr->getOutboundCount(_ms->getMillis()) == 0) return;
if (!millisHasNowPassed(next_tx_time)) return; // still in 'radio silence' phase (from airtime budget setting)
if (_radio->isReceiving()) { // LBT - check if radio is currently mid-receive, or if channel activity updateTxBudget();
uint32_t est_airtime = _radio->getEstAirtimeFor(MAX_TRANS_UNIT);
if (tx_budget_ms < est_airtime / 2) {
float duty_cycle = 1.0f / (1.0f + getAirtimeBudgetFactor());
unsigned long needed = est_airtime / 2 - tx_budget_ms;
next_tx_time = futureMillis((unsigned long)(needed / duty_cycle));
return;
}
if (!millisHasNowPassed(next_tx_time)) return;
if (_radio->isReceiving()) {
if (cad_busy_start == 0) { if (cad_busy_start == 0) {
cad_busy_start = _ms->getMillis(); // record when CAD busy state started cad_busy_start = _ms->getMillis(); // record when CAD busy state started
} }

View File

@@ -122,8 +122,12 @@ class Dispatcher {
bool prev_isrecv_mode; bool prev_isrecv_mode;
uint32_t n_sent_flood, n_sent_direct; uint32_t n_sent_flood, n_sent_direct;
uint32_t n_recv_flood, n_recv_direct; uint32_t n_recv_flood, n_recv_direct;
unsigned long tx_budget_ms;
unsigned long last_budget_update;
unsigned long duty_cycle_window_ms;
void processRecvPacket(Packet* pkt); void processRecvPacket(Packet* pkt);
void updateTxBudget();
protected: protected:
PacketManager* _mgr; PacketManager* _mgr;
@@ -142,6 +146,9 @@ protected:
_err_flags = 0; _err_flags = 0;
radio_nonrx_start = 0; radio_nonrx_start = 0;
prev_isrecv_mode = true; prev_isrecv_mode = true;
tx_budget_ms = 0;
last_budget_update = 0;
duty_cycle_window_ms = 3600000;
} }
virtual DispatcherAction onRecvPacket(Packet* pkt) = 0; virtual DispatcherAction onRecvPacket(Packet* pkt) = 0;
@@ -159,6 +166,7 @@ protected:
virtual uint32_t getCADFailMaxDuration() const; virtual uint32_t getCADFailMaxDuration() const;
virtual int getInterferenceThreshold() const { return 0; } // disabled by default virtual int getInterferenceThreshold() const { return 0; } // disabled by default
virtual int getAGCResetInterval() const { return 0; } // disabled by default virtual int getAGCResetInterval() const { return 0; } // disabled by default
virtual unsigned long getDutyCycleWindowMs() const { return 3600000; }
public: public:
void begin(); void begin();
@@ -168,8 +176,9 @@ public:
void releasePacket(Packet* packet); void releasePacket(Packet* packet);
void sendPacket(Packet* packet, uint8_t priority, uint32_t delay_millis=0); void sendPacket(Packet* packet, uint8_t priority, uint32_t delay_millis=0);
unsigned long getTotalAirTime() const { return total_air_time; } // in milliseconds unsigned long getTotalAirTime() const { return total_air_time; }
unsigned long getReceiveAirTime() const {return rx_air_time; } unsigned long getReceiveAirTime() const {return rx_air_time; }
unsigned long getRemainingTxBudget() const { return tx_budget_ms; }
uint32_t getNumSentFlood() const { return n_sent_flood; } uint32_t getNumSentFlood() const { return n_sent_flood; }
uint32_t getNumSentDirect() const { return n_sent_direct; } uint32_t getNumSentDirect() const { return n_sent_direct; }
uint32_t getNumRecvFlood() const { return n_recv_flood; } uint32_t getNumRecvFlood() const { return n_recv_flood; }

View File

@@ -48,6 +48,50 @@ LocalIdentity::LocalIdentity(RNG* rng) {
ed25519_create_keypair(pub_key, prv_key, seed); ed25519_create_keypair(pub_key, prv_key, seed);
} }
bool LocalIdentity::validatePrivateKey(const uint8_t prv[64]) {
uint8_t pub[32];
ed25519_derive_pub(pub, prv); // derive public key from given private key
// disallow 00 or FF prefixed public keys
if (pub[0] == 0x00 || pub[0] == 0xFF) return false;
// known good test client keypair
const uint8_t test_client_prv[64] = {
0x70, 0x65, 0xe1, 0x8f, 0xd9, 0xfa, 0xbb, 0x70,
0xc1, 0xed, 0x90, 0xdc, 0xa1, 0x99, 0x07, 0xde,
0x69, 0x8c, 0x88, 0xb7, 0x09, 0xea, 0x14, 0x6e,
0xaf, 0xd9, 0x3d, 0x9b, 0x83, 0x0c, 0x7b, 0x60,
0xc4, 0x68, 0x11, 0x93, 0xc7, 0x9b, 0xbc, 0x39,
0x94, 0x5b, 0xa8, 0x06, 0x41, 0x04, 0xbb, 0x61,
0x8f, 0x8f, 0xd7, 0xa8, 0x4a, 0x0a, 0xf6, 0xf5,
0x70, 0x33, 0xd6, 0xe8, 0xdd, 0xcd, 0x64, 0x71
};
const uint8_t test_client_pub[32] = {
0x1e, 0xc7, 0x71, 0x75, 0xb0, 0x91, 0x8e, 0xd2,
0x06, 0xf9, 0xae, 0x04, 0xec, 0x13, 0x6d, 0x6d,
0x5d, 0x43, 0x15, 0xbb, 0x26, 0x30, 0x54, 0x27,
0xf6, 0x45, 0xb4, 0x92, 0xe9, 0x35, 0x0c, 0x10
};
uint8_t ss1[32], ss2[32];
// shared secret we calculte from test client pubkey and given private key
ed25519_key_exchange(ss1, test_client_pub, prv);
// shared secret they calculate from our derived public key and test client private key
ed25519_key_exchange(ss2, pub, test_client_prv);
// check that both shared secrets match
if (memcmp(ss1, ss2, 32) != 0) return false;
// reject all-zero shared secret
for (int i = 0; i < 32; i++) {
if (ss1[i] != 0) return true;
}
return false;
}
bool LocalIdentity::readFrom(Stream& s) { bool LocalIdentity::readFrom(Stream& s) {
bool success = (s.readBytes(pub_key, PUB_KEY_SIZE) == PUB_KEY_SIZE); bool success = (s.readBytes(pub_key, PUB_KEY_SIZE) == PUB_KEY_SIZE);
success = success && (s.readBytes(prv_key, PRV_KEY_SIZE) == PRV_KEY_SIZE); success = success && (s.readBytes(prv_key, PRV_KEY_SIZE) == PRV_KEY_SIZE);

View File

@@ -76,6 +76,13 @@ public:
*/ */
void calcSharedSecret(uint8_t* secret, const uint8_t* other_pub_key) const; void calcSharedSecret(uint8_t* secret, const uint8_t* other_pub_key) const;
/**
* \brief Validates that a given private key can be used for ECDH / shared-secret operations.
* \param prv IN - the private key to validate (must be PRV_KEY_SIZE bytes)
* \returns true, if the private key is valid for login.
*/
static bool validatePrivateKey(const uint8_t prv[64]);
bool readFrom(Stream& s); bool readFrom(Stream& s);
bool writeTo(Stream& s) const; bool writeTo(Stream& s) const;
void printTo(Stream& s) const; void printTo(Stream& s) const;

View File

@@ -56,6 +56,14 @@ public:
virtual void setGpio(uint32_t values) {} virtual void setGpio(uint32_t values) {}
virtual uint8_t getStartupReason() const = 0; virtual uint8_t getStartupReason() const = 0;
virtual bool startOTAUpdate(const char* id, char reply[]) { return false; } // not supported virtual bool startOTAUpdate(const char* id, char reply[]) { return false; } // not supported
// Power management interface (boards with power management override these)
virtual bool isExternalPowered() { return false; }
virtual uint16_t getBootVoltage() { return 0; }
virtual uint32_t getResetReason() const { return 0; }
virtual const char* getResetReasonString(uint32_t reason) { return "Not available"; }
virtual uint8_t getShutdownReason() const { return 0; }
virtual const char* getShutdownReasonString(uint8_t reason) { return "Not available"; }
}; };
/** /**

View File

@@ -55,6 +55,54 @@ void BaseChatMesh::sendAckTo(const ContactInfo& dest, uint32_t ack_hash) {
} }
} }
void BaseChatMesh::bootstrapRTCfromContacts() {
uint32_t latest = 0;
for (int i = 0; i < num_contacts; i++) {
if (contacts[i].lastmod > latest) {
latest = contacts[i].lastmod;
}
}
if (latest != 0) {
getRTCClock()->setCurrentTime(latest + 1);
}
}
ContactInfo* BaseChatMesh::allocateContactSlot() {
if (num_contacts < MAX_CONTACTS) {
return &contacts[num_contacts++];
} else if (shouldOverwriteWhenFull()) {
// Find oldest non-favourite contact by oldest lastmod timestamp
int oldest_idx = -1;
uint32_t oldest_lastmod = 0xFFFFFFFF;
for (int i = 0; i < num_contacts; i++) {
bool is_favourite = (contacts[i].flags & 0x01) != 0;
if (!is_favourite && contacts[i].lastmod < oldest_lastmod) {
oldest_lastmod = contacts[i].lastmod;
oldest_idx = i;
}
}
if (oldest_idx >= 0) {
onContactOverwrite(contacts[oldest_idx].id.pub_key);
return &contacts[oldest_idx];
}
}
return NULL; // no space, no overwrite or all contacts are all favourites
}
void BaseChatMesh::populateContactFromAdvert(ContactInfo& ci, const mesh::Identity& id, const AdvertDataParser& parser, uint32_t timestamp) {
memset(&ci, 0, sizeof(ci));
ci.id = id;
ci.out_path_len = -1; // initially out_path is unknown
StrHelper::strncpy(ci.name, parser.getName(), sizeof(ci.name));
ci.type = parser.getType();
if (parser.hasLatLon()) {
ci.gps_lat = parser.getIntLat();
ci.gps_lon = parser.getIntLon();
}
ci.last_advert_timestamp = timestamp;
ci.lastmod = getRTCClock()->getCurrentTime();
}
void BaseChatMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len) { void BaseChatMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len) {
AdvertDataParser parser(app_data, app_data_len); AdvertDataParser parser(app_data, app_data_len);
if (!(parser.isValid() && parser.hasName())) { if (!(parser.isValid() && parser.hasName())) {
@@ -85,50 +133,38 @@ void BaseChatMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id,
} }
putBlobByKey(id.pub_key, PUB_KEY_SIZE, temp_buf, plen); putBlobByKey(id.pub_key, PUB_KEY_SIZE, temp_buf, plen);
bool is_new = false; bool is_new = false; // true = not in contacts[], false = exists in contacts[]
if (from == NULL) { if (from == NULL) {
if (!isAutoAddEnabled()) { if (!shouldAutoAddContactType(parser.getType())) {
ContactInfo ci; ContactInfo ci;
memset(&ci, 0, sizeof(ci)); populateContactFromAdvert(ci, id, parser, timestamp);
ci.id = id;
ci.out_path_len = -1; // initially out_path is unknown
StrHelper::strncpy(ci.name, parser.getName(), sizeof(ci.name));
ci.type = parser.getType();
if (parser.hasLatLon()) {
ci.gps_lat = parser.getIntLat();
ci.gps_lon = parser.getIntLon();
}
ci.last_advert_timestamp = timestamp;
ci.lastmod = getRTCClock()->getCurrentTime();
onDiscoveredContact(ci, true, packet->path_len, packet->path); // let UI know onDiscoveredContact(ci, true, packet->path_len, packet->path); // let UI know
return; return;
} }
is_new = true; from = allocateContactSlot();
if (num_contacts < MAX_CONTACTS) { if (from == NULL) {
from = &contacts[num_contacts++]; ContactInfo ci;
from->id = id; populateContactFromAdvert(ci, id, parser, timestamp);
from->out_path_len = -1; // initially out_path is unknown onDiscoveredContact(ci, true, packet->path_len, packet->path);
from->gps_lat = 0; // initially unknown GPS loc onContactsFull();
from->gps_lon = 0; MESH_DEBUG_PRINTLN("onAdvertRecv: unable to allocate contact slot for new contact");
from->sync_since = 0;
from->shared_secret_valid = false; // ecdh shared_secret will be calculated later on demand
} else {
MESH_DEBUG_PRINTLN("onAdvertRecv: contacts table is full!");
return; return;
} }
}
// update populateContactFromAdvert(*from, id, parser, timestamp);
StrHelper::strncpy(from->name, parser.getName(), sizeof(from->name)); from->sync_since = 0;
from->type = parser.getType(); from->shared_secret_valid = false;
if (parser.hasLatLon()) {
from->gps_lat = parser.getIntLat();
from->gps_lon = parser.getIntLon();
} }
from->last_advert_timestamp = timestamp; // update
from->lastmod = getRTCClock()->getCurrentTime(); StrHelper::strncpy(from->name, parser.getName(), sizeof(from->name));
from->type = parser.getType();
if (parser.hasLatLon()) {
from->gps_lat = parser.getIntLat();
from->gps_lon = parser.getIntLon();
}
from->last_advert_timestamp = timestamp;
from->lastmod = getRTCClock()->getCurrentTime();
onDiscoveredContact(*from, is_new, packet->path_len, packet->path); // let UI know onDiscoveredContact(*from, is_new, packet->path_len, packet->path); // let UI know
} }
@@ -722,10 +758,9 @@ ContactInfo* BaseChatMesh::lookupContactByPubKey(const uint8_t* pub_key, int pre
} }
bool BaseChatMesh::addContact(const ContactInfo& contact) { bool BaseChatMesh::addContact(const ContactInfo& contact) {
if (num_contacts < MAX_CONTACTS) { ContactInfo* dest = allocateContactSlot();
auto dest = &contacts[num_contacts++]; if (dest) {
*dest = contact; *dest = contact;
dest->shared_secret_valid = false; // mark shared_secret as needing calculation dest->shared_secret_valid = false; // mark shared_secret as needing calculation
return true; // success return true; // success
} }

View File

@@ -88,10 +88,17 @@ protected:
memset(connections, 0, sizeof(connections)); memset(connections, 0, sizeof(connections));
} }
void bootstrapRTCfromContacts();
void resetContacts() { num_contacts = 0; } void resetContacts() { num_contacts = 0; }
void populateContactFromAdvert(ContactInfo& ci, const mesh::Identity& id, const AdvertDataParser& parser, uint32_t timestamp);
ContactInfo* allocateContactSlot(); // helper to find slot for new contact
// 'UI' concepts, for sub-classes to implement // 'UI' concepts, for sub-classes to implement
virtual bool isAutoAddEnabled() const { return true; } virtual bool isAutoAddEnabled() const { return true; }
virtual bool shouldAutoAddContactType(uint8_t type) const { return true; }
virtual void onContactsFull() {};
virtual bool shouldOverwriteWhenFull() const { return false; }
virtual void onContactOverwrite(const uint8_t* pub_key) {};
virtual void onDiscoveredContact(ContactInfo& contact, bool is_new, uint8_t path_len, const uint8_t* path) = 0; virtual void onDiscoveredContact(ContactInfo& contact, bool is_new, uint8_t path_len, const uint8_t* path) = 0;
virtual ContactInfo* processAck(const uint8_t *data) = 0; virtual ContactInfo* processAck(const uint8_t *data) = 0;
virtual void onContactPathUpdated(const ContactInfo& contact) = 0; virtual void onContactPathUpdated(const ContactInfo& contact) = 0;

View File

@@ -11,7 +11,8 @@ static File openWrite(FILESYSTEM* _fs, const char* filename) {
#endif #endif
} }
void ClientACL::load(FILESYSTEM* _fs) { void ClientACL::load(FILESYSTEM* fs, const mesh::LocalIdentity& self_id) {
_fs = fs;
num_clients = 0; num_clients = 0;
if (_fs->exists("/s_contacts")) { if (_fs->exists("/s_contacts")) {
#if defined(RP2040_PLATFORM) #if defined(RP2040_PLATFORM)
@@ -34,11 +35,12 @@ void ClientACL::load(FILESYSTEM* _fs) {
success = success && (file.read(unused, 2) == 2); success = success && (file.read(unused, 2) == 2);
success = success && (file.read((uint8_t *)&c.out_path_len, 1) == 1); success = success && (file.read((uint8_t *)&c.out_path_len, 1) == 1);
success = success && (file.read(c.out_path, 64) == 64); success = success && (file.read(c.out_path, 64) == 64);
success = success && (file.read(c.shared_secret, PUB_KEY_SIZE) == PUB_KEY_SIZE); success = success && (file.read(c.shared_secret, PUB_KEY_SIZE) == PUB_KEY_SIZE); // will be recalculated below
if (!success) break; // EOF if (!success) break; // EOF
c.id = mesh::Identity(pub_key); c.id = mesh::Identity(pub_key);
self_id.calcSharedSecret(c.shared_secret, pub_key); // recalculate shared secrets in case our private key changed
if (num_clients < MAX_CLIENTS) { if (num_clients < MAX_CLIENTS) {
clients[num_clients++] = c; clients[num_clients++] = c;
} else { } else {
@@ -50,7 +52,8 @@ void ClientACL::load(FILESYSTEM* _fs) {
} }
} }
void ClientACL::save(FILESYSTEM* _fs, bool (*filter)(ClientInfo*)) { void ClientACL::save(FILESYSTEM* fs, bool (*filter)(ClientInfo*)) {
_fs = fs;
File file = openWrite(_fs, "/s_contacts"); File file = openWrite(_fs, "/s_contacts");
if (file) { if (file) {
uint8_t unused[2]; uint8_t unused[2];
@@ -74,6 +77,16 @@ void ClientACL::save(FILESYSTEM* _fs, bool (*filter)(ClientInfo*)) {
} }
} }
bool ClientACL::clear() {
if (!_fs) return false; // no filesystem, nothing to clear
if (_fs->exists("/s_contacts")) {
_fs->remove("/s_contacts");
}
memset(clients, 0, sizeof(clients));
num_clients = 0;
return true;
}
ClientInfo* ClientACL::getClient(const uint8_t* pubkey, int key_len) { ClientInfo* ClientACL::getClient(const uint8_t* pubkey, int key_len) {
for (int i = 0; i < num_clients; i++) { for (int i = 0; i < num_clients; i++) {
if (memcmp(pubkey, clients[i].id.pub_key, key_len) == 0) return &clients[i]; // already known if (memcmp(pubkey, clients[i].id.pub_key, key_len) == 0) return &clients[i]; // already known

View File

@@ -36,6 +36,7 @@ struct ClientInfo {
#endif #endif
class ClientACL { class ClientACL {
FILESYSTEM* _fs;
ClientInfo clients[MAX_CLIENTS]; ClientInfo clients[MAX_CLIENTS];
int num_clients; int num_clients;
@@ -44,8 +45,9 @@ public:
memset(clients, 0, sizeof(clients)); memset(clients, 0, sizeof(clients));
num_clients = 0; num_clients = 0;
} }
void load(FILESYSTEM* _fs); void load(FILESYSTEM* _fs, const mesh::LocalIdentity& self_id);
void save(FILESYSTEM* _fs, bool (*filter)(ClientInfo*)=NULL); void save(FILESYSTEM* _fs, bool (*filter)(ClientInfo*)=NULL);
bool clear();
ClientInfo* getClient(const uint8_t* pubkey, int key_len); ClientInfo* getClient(const uint8_t* pubkey, int key_len);
ClientInfo* putClient(const mesh::Identity& id, uint8_t init_perms); ClientInfo* putClient(const mesh::Identity& id, uint8_t init_perms);

View File

@@ -81,7 +81,9 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
file.read((uint8_t *)&_prefs->discovery_mod_timestamp, sizeof(_prefs->discovery_mod_timestamp)); // 162 file.read((uint8_t *)&_prefs->discovery_mod_timestamp, sizeof(_prefs->discovery_mod_timestamp)); // 162
file.read((uint8_t *)&_prefs->adc_multiplier, sizeof(_prefs->adc_multiplier)); // 166 file.read((uint8_t *)&_prefs->adc_multiplier, sizeof(_prefs->adc_multiplier)); // 166
file.read((uint8_t *)_prefs->owner_info, sizeof(_prefs->owner_info)); // 170 file.read((uint8_t *)_prefs->owner_info, sizeof(_prefs->owner_info)); // 170
// 290 file.read((uint8_t *)&_prefs->flood_advert_base, sizeof(_prefs->flood_advert_base)); // 290
// 294
// sanitise bad pref values // sanitise bad pref values
_prefs->rx_delay_base = constrain(_prefs->rx_delay_base, 0, 20.0f); _prefs->rx_delay_base = constrain(_prefs->rx_delay_base, 0, 20.0f);
@@ -108,6 +110,8 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
_prefs->gps_enabled = constrain(_prefs->gps_enabled, 0, 1); _prefs->gps_enabled = constrain(_prefs->gps_enabled, 0, 1);
_prefs->advert_loc_policy = constrain(_prefs->advert_loc_policy, 0, 2); _prefs->advert_loc_policy = constrain(_prefs->advert_loc_policy, 0, 2);
_prefs->flood_advert_base = constrain(_prefs->flood_advert_base, 0, 1);
file.close(); file.close();
} }
} }
@@ -165,7 +169,9 @@ void CommonCLI::savePrefs(FILESYSTEM* fs) {
file.write((uint8_t *)&_prefs->discovery_mod_timestamp, sizeof(_prefs->discovery_mod_timestamp)); // 162 file.write((uint8_t *)&_prefs->discovery_mod_timestamp, sizeof(_prefs->discovery_mod_timestamp)); // 162
file.write((uint8_t *)&_prefs->adc_multiplier, sizeof(_prefs->adc_multiplier)); // 166 file.write((uint8_t *)&_prefs->adc_multiplier, sizeof(_prefs->adc_multiplier)); // 166
file.write((uint8_t *)_prefs->owner_info, sizeof(_prefs->owner_info)); // 170 file.write((uint8_t *)_prefs->owner_info, sizeof(_prefs->owner_info)); // 170
// 290 file.write((uint8_t *)&_prefs->flood_advert_base, sizeof(_prefs->flood_advert_base)); // 290
// 294
file.close(); file.close();
} }
@@ -197,7 +203,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
if (memcmp(command, "reboot", 6) == 0) { if (memcmp(command, "reboot", 6) == 0) {
_board->reboot(); // doesn't return _board->reboot(); // doesn't return
} else if (memcmp(command, "advert", 6) == 0) { } else if (memcmp(command, "advert", 6) == 0) {
_callbacks->sendSelfAdvertisement(1500); // longer delay, give CLI response time to be sent first // send flood advert
_callbacks->sendSelfAdvertisement(1500, true); // longer delay, give CLI response time to be sent first
strcpy(reply, "OK - Advert sent"); strcpy(reply, "OK - Advert sent");
} else if (memcmp(command, "clock sync", 10) == 0) { } else if (memcmp(command, "clock sync", 10) == 0) {
uint32_t curr = getRTCClock()->getCurrentTime(); uint32_t curr = getRTCClock()->getCurrentTime();
@@ -364,6 +371,35 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
} else { } else {
sprintf(reply, "> %.3f", adc_mult); sprintf(reply, "> %.3f", adc_mult);
} }
} else if (memcmp(config, "flood.advert.base", 17) == 0) {
sprintf(reply, "> %s", StrHelper::ftoa(_prefs->flood_advert_base));
// Power management commands
} else if (memcmp(config, "pwrmgt.support", 14) == 0) {
#ifdef NRF52_POWER_MANAGEMENT
strcpy(reply, "> supported");
#else
strcpy(reply, "> unsupported");
#endif
} else if (memcmp(config, "pwrmgt.source", 13) == 0) {
#ifdef NRF52_POWER_MANAGEMENT
strcpy(reply, _board->isExternalPowered() ? "> external" : "> battery");
#else
strcpy(reply, "ERROR: Power management not supported");
#endif
} else if (memcmp(config, "pwrmgt.bootreason", 17) == 0) {
#ifdef NRF52_POWER_MANAGEMENT
sprintf(reply, "> Reset: %s; Shutdown: %s",
_board->getResetReasonString(_board->getResetReason()),
_board->getShutdownReasonString(_board->getShutdownReason()));
#else
strcpy(reply, "ERROR: Power management not supported");
#endif
} else if (memcmp(config, "pwrmgt.bootmv", 13) == 0) {
#ifdef NRF52_POWER_MANAGEMENT
sprintf(reply, "> %u mV", _board->getBootVoltage());
#else
strcpy(reply, "ERROR: Power management not supported");
#endif
} else { } else {
sprintf(reply, "??: %s", config); sprintf(reply, "??: %s", config);
} }
@@ -394,8 +430,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
strcpy(reply, "OK"); strcpy(reply, "OK");
} else if (memcmp(config, "flood.advert.interval ", 22) == 0) { } else if (memcmp(config, "flood.advert.interval ", 22) == 0) {
int hours = _atoi(&config[22]); int hours = _atoi(&config[22]);
if ((hours > 0 && hours < 3) || (hours > 48)) { if ((hours > 0 && hours < 3) || (hours > 168)) {
strcpy(reply, "Error: interval range is 3-48 hours"); strcpy(reply, "Error: interval range is 3-168 hours");
} else { } else {
_prefs->flood_advert_interval = (uint8_t)(hours); _prefs->flood_advert_interval = (uint8_t)(hours);
_callbacks->updateFloodAdvertTimer(); _callbacks->updateFloodAdvertTimer();
@@ -416,17 +452,18 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
StrHelper::strncpy(_prefs->guest_password, &config[15], sizeof(_prefs->guest_password)); StrHelper::strncpy(_prefs->guest_password, &config[15], sizeof(_prefs->guest_password));
savePrefs(); savePrefs();
strcpy(reply, "OK"); strcpy(reply, "OK");
} else if (sender_timestamp == 0 && } else if (memcmp(config, "prv.key ", 8) == 0) {
memcmp(config, "prv.key ", 8) == 0) { // from serial command line only
uint8_t prv_key[PRV_KEY_SIZE]; uint8_t prv_key[PRV_KEY_SIZE];
bool success = mesh::Utils::fromHex(prv_key, PRV_KEY_SIZE, &config[8]); bool success = mesh::Utils::fromHex(prv_key, PRV_KEY_SIZE, &config[8]);
if (success) { // only allow rekey if key is valid
if (success && mesh::LocalIdentity::validatePrivateKey(prv_key)) {
mesh::LocalIdentity new_id; mesh::LocalIdentity new_id;
new_id.readFrom(prv_key, PRV_KEY_SIZE); new_id.readFrom(prv_key, PRV_KEY_SIZE);
_callbacks->saveIdentity(new_id); _callbacks->saveIdentity(new_id);
strcpy(reply, "OK"); strcpy(reply, "OK, reboot to apply! New pubkey: ");
mesh::Utils::toHex(&reply[33], new_id.pub_key, PUB_KEY_SIZE);
} else { } else {
strcpy(reply, "Error, invalid key"); strcpy(reply, "Error, bad key");
} }
} else if (memcmp(config, "name ", 5) == 0) { } else if (memcmp(config, "name ", 5) == 0) {
if (isValidName(&config[5])) { if (isValidName(&config[5])) {
@@ -583,6 +620,15 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
_prefs->adc_multiplier = 0.0f; _prefs->adc_multiplier = 0.0f;
strcpy(reply, "Error: unsupported by this board"); strcpy(reply, "Error: unsupported by this board");
}; };
} else if (memcmp(config, "flood.advert.base ", 18) == 0) {
float f = atof(&config[18]);
if((f > 0) || (f<1)) {
_prefs->flood_advert_base = f;
savePrefs();
strcpy(reply, "OK");
} else {
strcpy(reply, "Error: base must be between 0 and 1");
}
} else { } else {
sprintf(reply, "unknown config: %s", config); sprintf(reply, "unknown config: %s", config);
} }

View File

@@ -3,6 +3,7 @@
#include "Mesh.h" #include "Mesh.h"
#include <helpers/IdentityStore.h> #include <helpers/IdentityStore.h>
#include <helpers/SensorManager.h> #include <helpers/SensorManager.h>
#include <helpers/ClientACL.h>
#if defined(WITH_RS232_BRIDGE) || defined(WITH_ESPNOW_BRIDGE) #if defined(WITH_RS232_BRIDGE) || defined(WITH_ESPNOW_BRIDGE)
#define WITH_BRIDGE #define WITH_BRIDGE
@@ -35,6 +36,7 @@ struct NodePrefs { // persisted to file
uint8_t flood_max; uint8_t flood_max;
uint8_t interference_threshold; uint8_t interference_threshold;
uint8_t agc_reset_interval; // secs / 4 uint8_t agc_reset_interval; // secs / 4
float flood_advert_base;
// Bridge settings // Bridge settings
uint8_t bridge_enabled; // boolean uint8_t bridge_enabled; // boolean
uint16_t bridge_delay; // milliseconds (default 500 ms) uint16_t bridge_delay; // milliseconds (default 500 ms)
@@ -60,7 +62,7 @@ public:
virtual const char* getBuildDate() = 0; virtual const char* getBuildDate() = 0;
virtual const char* getRole() = 0; virtual const char* getRole() = 0;
virtual bool formatFileSystem() = 0; virtual bool formatFileSystem() = 0;
virtual void sendSelfAdvertisement(int delay_millis) = 0; virtual void sendSelfAdvertisement(int delay_millis, bool flood) = 0;
virtual void updateAdvertTimer() = 0; virtual void updateAdvertTimer() = 0;
virtual void updateFloodAdvertTimer() = 0; virtual void updateFloodAdvertTimer() = 0;
virtual void setLoggingOn(bool enable) = 0; virtual void setLoggingOn(bool enable) = 0;
@@ -94,6 +96,7 @@ class CommonCLI {
CommonCLICallbacks* _callbacks; CommonCLICallbacks* _callbacks;
mesh::MainBoard* _board; mesh::MainBoard* _board;
SensorManager* _sensors; SensorManager* _sensors;
ClientACL* _acl;
char tmp[PRV_KEY_SIZE*2 + 4]; char tmp[PRV_KEY_SIZE*2 + 4];
mesh::RTCClock* getRTCClock() { return _rtc; } mesh::RTCClock* getRTCClock() { return _rtc; }
@@ -101,8 +104,8 @@ class CommonCLI {
void loadPrefsInt(FILESYSTEM* _fs, const char* filename); void loadPrefsInt(FILESYSTEM* _fs, const char* filename);
public: public:
CommonCLI(mesh::MainBoard& board, mesh::RTCClock& rtc, SensorManager& sensors, NodePrefs* prefs, CommonCLICallbacks* callbacks) CommonCLI(mesh::MainBoard& board, mesh::RTCClock& rtc, SensorManager& sensors, ClientACL& acl, NodePrefs* prefs, CommonCLICallbacks* callbacks)
: _board(&board), _rtc(&rtc), _sensors(&sensors), _prefs(prefs), _callbacks(callbacks) { } : _board(&board), _rtc(&rtc), _sensors(&sensors), _acl(&acl), _prefs(prefs), _callbacks(callbacks) { }
void loadPrefs(FILESYSTEM* _fs); void loadPrefs(FILESYSTEM* _fs);
void savePrefs(FILESYSTEM* _fs); void savePrefs(FILESYSTEM* _fs);

View File

@@ -2,6 +2,7 @@
#include "NRF52Board.h" #include "NRF52Board.h"
#include <bluefruit.h> #include <bluefruit.h>
#include <nrf_soc.h>
static BLEDfu bledfu; static BLEDfu bledfu;
@@ -21,6 +22,222 @@ void NRF52Board::begin() {
startup_reason = BD_STARTUP_NORMAL; startup_reason = BD_STARTUP_NORMAL;
} }
#ifdef NRF52_POWER_MANAGEMENT
#include "nrf.h"
// Power Management global variables
uint32_t g_nrf52_reset_reason = 0; // Reset/Startup reason
uint8_t g_nrf52_shutdown_reason = 0; // Shutdown reason
// Early constructor - runs before SystemInit() clears the registers
// Priority 101 ensures this runs before SystemInit (102) and before
// any C++ static constructors (default 65535)
static void __attribute__((constructor(101))) nrf52_early_reset_capture() {
g_nrf52_reset_reason = NRF_POWER->RESETREAS;
g_nrf52_shutdown_reason = NRF_POWER->GPREGRET2;
}
void NRF52Board::initPowerMgr() {
// Copy early-captured register values
reset_reason = g_nrf52_reset_reason;
shutdown_reason = g_nrf52_shutdown_reason;
boot_voltage_mv = 0; // Will be set by checkBootVoltage()
// Clear registers for next boot
// Note: At this point SoftDevice may or may not be enabled
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
if (sd_enabled) {
sd_power_reset_reason_clr(0xFFFFFFFF);
sd_power_gpregret_clr(1, 0xFF);
} else {
NRF_POWER->RESETREAS = 0xFFFFFFFF; // Write 1s to clear
NRF_POWER->GPREGRET2 = 0;
}
// Log reset/shutdown info
if (shutdown_reason != SHUTDOWN_REASON_NONE) {
MESH_DEBUG_PRINTLN("PWRMGT: Reset = %s (0x%lX); Shutdown = %s (0x%02X)",
getResetReasonString(reset_reason), (unsigned long)reset_reason,
getShutdownReasonString(shutdown_reason), shutdown_reason);
} else {
MESH_DEBUG_PRINTLN("PWRMGT: Reset = %s (0x%lX)",
getResetReasonString(reset_reason), (unsigned long)reset_reason);
}
}
bool NRF52Board::isExternalPowered() {
// Check if SoftDevice is enabled before using its API
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
if (sd_enabled) {
uint32_t usb_status;
sd_power_usbregstatus_get(&usb_status);
return (usb_status & POWER_USBREGSTATUS_VBUSDETECT_Msk) != 0;
} else {
return (NRF_POWER->USBREGSTATUS & POWER_USBREGSTATUS_VBUSDETECT_Msk) != 0;
}
}
const char* NRF52Board::getResetReasonString(uint32_t reason) {
if (reason & POWER_RESETREAS_RESETPIN_Msk) return "Reset Pin";
if (reason & POWER_RESETREAS_DOG_Msk) return "Watchdog";
if (reason & POWER_RESETREAS_SREQ_Msk) return "Soft Reset";
if (reason & POWER_RESETREAS_LOCKUP_Msk) return "CPU Lockup";
#ifdef POWER_RESETREAS_LPCOMP_Msk
if (reason & POWER_RESETREAS_LPCOMP_Msk) return "Wake from LPCOMP";
#endif
#ifdef POWER_RESETREAS_VBUS_Msk
if (reason & POWER_RESETREAS_VBUS_Msk) return "Wake from VBUS";
#endif
#ifdef POWER_RESETREAS_OFF_Msk
if (reason & POWER_RESETREAS_OFF_Msk) return "Wake from GPIO";
#endif
#ifdef POWER_RESETREAS_DIF_Msk
if (reason & POWER_RESETREAS_DIF_Msk) return "Debug Interface";
#endif
return "Cold Boot";
}
const char* NRF52Board::getShutdownReasonString(uint8_t reason) {
switch (reason) {
case SHUTDOWN_REASON_LOW_VOLTAGE: return "Low Voltage";
case SHUTDOWN_REASON_USER: return "User Request";
case SHUTDOWN_REASON_BOOT_PROTECT: return "Boot Protection";
}
return "Unknown";
}
bool NRF52Board::checkBootVoltage(const PowerMgtConfig* config) {
initPowerMgr();
// Read boot voltage
boot_voltage_mv = getBattMilliVolts();
if (config->voltage_bootlock == 0) return true; // Protection disabled
// Skip check if externally powered
if (isExternalPowered()) {
MESH_DEBUG_PRINTLN("PWRMGT: Boot check skipped (external power)");
boot_voltage_mv = getBattMilliVolts();
return true;
}
MESH_DEBUG_PRINTLN("PWRMGT: Boot voltage = %u mV (threshold = %u mV)",
boot_voltage_mv, config->voltage_bootlock);
// Only trigger shutdown if reading is valid (>1000mV) AND below threshold
// This prevents spurious shutdowns on ADC glitches or uninitialized reads
if (boot_voltage_mv > 1000 && boot_voltage_mv < config->voltage_bootlock) {
MESH_DEBUG_PRINTLN("PWRMGT: Boot voltage too low - entering protective shutdown");
initiateShutdown(SHUTDOWN_REASON_BOOT_PROTECT);
return false; // Should never reach this
}
return true;
}
void NRF52Board::initiateShutdown(uint8_t reason) {
enterSystemOff(reason);
}
void NRF52Board::enterSystemOff(uint8_t reason) {
MESH_DEBUG_PRINTLN("PWRMGT: Entering SYSTEMOFF (%s)", getShutdownReasonString(reason));
// Record shutdown reason in GPREGRET2
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
if (sd_enabled) {
sd_power_gpregret_clr(1, 0xFF);
sd_power_gpregret_set(1, reason);
} else {
NRF_POWER->GPREGRET2 = reason;
}
// Flush serial buffers
Serial.flush();
delay(100);
// Enter SYSTEMOFF
if (sd_enabled) {
uint32_t err = sd_power_system_off();
if (err == NRF_ERROR_SOFTDEVICE_NOT_ENABLED) { //SoftDevice not enabled
sd_enabled = 0;
}
}
if (!sd_enabled) {
// SoftDevice not available; write directly to POWER->SYSTEMOFF
NRF_POWER->SYSTEMOFF = POWER_SYSTEMOFF_SYSTEMOFF_Enter;
}
// If we get here, something went wrong. Reset to recover.
NVIC_SystemReset();
}
void NRF52Board::configureVoltageWake(uint8_t ain_channel, uint8_t refsel) {
// LPCOMP is not managed by SoftDevice - direct register access required
// Halt and disable before reconfiguration
NRF_LPCOMP->TASKS_STOP = 1;
NRF_LPCOMP->ENABLE = LPCOMP_ENABLE_ENABLE_Disabled;
// Select analog input (AIN0-7 maps to PSEL 0-7)
NRF_LPCOMP->PSEL = ((uint32_t)ain_channel << LPCOMP_PSEL_PSEL_Pos) & LPCOMP_PSEL_PSEL_Msk;
// Reference: REFSEL (0-6=1/8..7/8, 7=ARef, 8-15=1/16..15/16)
NRF_LPCOMP->REFSEL = ((uint32_t)refsel << LPCOMP_REFSEL_REFSEL_Pos) & LPCOMP_REFSEL_REFSEL_Msk;
// Detect UP events (voltage rises above threshold for battery recovery)
NRF_LPCOMP->ANADETECT = LPCOMP_ANADETECT_ANADETECT_Up;
// Enable 50mV hysteresis for noise immunity
NRF_LPCOMP->HYST = LPCOMP_HYST_HYST_Hyst50mV;
// Clear stale events/interrupts before enabling wake
NRF_LPCOMP->EVENTS_READY = 0;
NRF_LPCOMP->EVENTS_DOWN = 0;
NRF_LPCOMP->EVENTS_UP = 0;
NRF_LPCOMP->EVENTS_CROSS = 0;
NRF_LPCOMP->INTENCLR = 0xFFFFFFFF;
NRF_LPCOMP->INTENSET = LPCOMP_INTENSET_UP_Msk;
// Enable LPCOMP
NRF_LPCOMP->ENABLE = LPCOMP_ENABLE_ENABLE_Enabled;
NRF_LPCOMP->TASKS_START = 1;
// Wait for comparator to settle before entering SYSTEMOFF
for (uint8_t i = 0; i < 20 && !NRF_LPCOMP->EVENTS_READY; i++) {
delayMicroseconds(50);
}
if (refsel == 7) {
MESH_DEBUG_PRINTLN("PWRMGT: LPCOMP wake configured (AIN%d, ref=ARef)", ain_channel);
} else if (refsel <= 6) {
MESH_DEBUG_PRINTLN("PWRMGT: LPCOMP wake configured (AIN%d, ref=%d/8 VDD)",
ain_channel, refsel + 1);
} else {
uint8_t ref_num = (uint8_t)((refsel - 8) * 2 + 1);
MESH_DEBUG_PRINTLN("PWRMGT: LPCOMP wake configured (AIN%d, ref=%d/16 VDD)",
ain_channel, ref_num);
}
// Configure VBUS (USB power) wake alongside LPCOMP
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
if (sd_enabled) {
sd_power_usbdetected_enable(1);
} else {
NRF_POWER->EVENTS_USBDETECTED = 0;
NRF_POWER->INTENSET = POWER_INTENSET_USBDETECTED_Msk;
}
MESH_DEBUG_PRINTLN("PWRMGT: VBUS wake configured");
}
#endif
void NRF52BoardDCDC::begin() { void NRF52BoardDCDC::begin() {
NRF52Board::begin(); NRF52Board::begin();

View File

@@ -5,15 +5,58 @@
#if defined(NRF52_PLATFORM) #if defined(NRF52_PLATFORM)
#ifdef NRF52_POWER_MANAGEMENT
// Shutdown Reason Codes (stored in GPREGRET before SYSTEMOFF)
#define SHUTDOWN_REASON_NONE 0x00
#define SHUTDOWN_REASON_LOW_VOLTAGE 0x4C // 'L' - Runtime low voltage threshold
#define SHUTDOWN_REASON_USER 0x55 // 'U' - User requested powerOff()
#define SHUTDOWN_REASON_BOOT_PROTECT 0x42 // 'B' - Boot voltage protection
// Boards provide this struct with their hardware-specific settings and callbacks.
struct PowerMgtConfig {
// LPCOMP wake configuration (for voltage recovery from SYSTEMOFF)
uint8_t lpcomp_ain_channel; // AIN0-7 for voltage sensing pin
uint8_t lpcomp_refsel; // REFSEL value: 0-6=1/8..7/8, 7=ARef, 8-15=1/16..15/16
// Boot protection voltage threshold (millivolts)
// Set to 0 to disable boot protection
uint16_t voltage_bootlock;
};
#endif
class NRF52Board : public mesh::MainBoard { class NRF52Board : public mesh::MainBoard {
#ifdef NRF52_POWER_MANAGEMENT
void initPowerMgr();
#endif
protected: protected:
uint8_t startup_reason; uint8_t startup_reason;
#ifdef NRF52_POWER_MANAGEMENT
uint32_t reset_reason; // RESETREAS register value
uint8_t shutdown_reason; // GPREGRET value (why we entered last SYSTEMOFF)
uint16_t boot_voltage_mv; // Battery voltage at boot (millivolts)
bool checkBootVoltage(const PowerMgtConfig* config);
void enterSystemOff(uint8_t reason);
void configureVoltageWake(uint8_t ain_channel, uint8_t refsel);
virtual void initiateShutdown(uint8_t reason);
#endif
public: public:
virtual void begin(); virtual void begin();
virtual uint8_t getStartupReason() const override { return startup_reason; } virtual uint8_t getStartupReason() const override { return startup_reason; }
virtual float getMCUTemperature() override; virtual float getMCUTemperature() override;
virtual void reboot() override { NVIC_SystemReset(); } virtual void reboot() override { NVIC_SystemReset(); }
#ifdef NRF52_POWER_MANAGEMENT
bool isExternalPowered() override;
uint16_t getBootVoltage() override { return boot_voltage_mv; }
virtual uint32_t getResetReason() const override { return reset_reason; }
uint8_t getShutdownReason() const override { return shutdown_reason; }
const char* getResetReasonString(uint32_t reason) override;
const char* getShutdownReasonString(uint8_t reason) override;
#endif
}; };
/* /*

View File

@@ -11,7 +11,11 @@ RegionMap::RegionMap(TransportKeyStore& store) : _store(&store) {
bool RegionMap::is_name_char(uint8_t c) { bool RegionMap::is_name_char(uint8_t c) {
// accept all alpha-num or accented characters, but exclude most punctuation chars // accept all alpha-num or accented characters, but exclude most punctuation chars
return c == '-' || c == '#' || (c >= '0' && c <= '9') || c >= 'A'; return c == '-' || c == '$' || c == '#' || (c >= '0' && c <= '9') || c >= 'A';
}
static const char* skip_hash(const char* name) {
return *name == '#' ? name + 1 : name;
} }
static File openWrite(FILESYSTEM* _fs, const char* filename) { static File openWrite(FILESYSTEM* _fs, const char* filename) {
@@ -127,11 +131,17 @@ RegionEntry* RegionMap::findMatch(mesh::Packet* packet, uint8_t mask) {
if ((region->flags & mask) == 0) { // does region allow this? (per 'mask' param) if ((region->flags & mask) == 0) { // does region allow this? (per 'mask' param)
TransportKey keys[4]; TransportKey keys[4];
int num; int num;
if (region->name[0] == '#') { // auto hashtag region if (region->name[0] == '$') { // private region
num = _store->loadKeysFor(region->id, keys, 4);
} else if (region->name[0] == '#') { // auto hashtag region
_store->getAutoKeyFor(region->id, region->name, keys[0]); _store->getAutoKeyFor(region->id, region->name, keys[0]);
num = 1; num = 1;
} else { } else { // new: implicit auto hashtag region
num = _store->loadKeysFor(region->id, keys, 4); char tmp[sizeof(region->name)];
tmp[0] = '#';
strcpy(&tmp[1], region->name);
_store->getAutoKeyFor(region->id, tmp, keys[0]);
num = 1;
} }
for (int j = 0; j < num; j++) { for (int j = 0; j < num; j++) {
uint16_t code = keys[j].calcTransportCode(packet); uint16_t code = keys[j].calcTransportCode(packet);
@@ -147,9 +157,10 @@ RegionEntry* RegionMap::findMatch(mesh::Packet* packet, uint8_t mask) {
RegionEntry* RegionMap::findByName(const char* name) { RegionEntry* RegionMap::findByName(const char* name) {
if (strcmp(name, "*") == 0) return &wildcard; if (strcmp(name, "*") == 0) return &wildcard;
if (*name == '#') { name++; } // ignore the '#' when matching by name
for (int i = 0; i < num_regions; i++) { for (int i = 0; i < num_regions; i++) {
auto region = &regions[i]; auto region = &regions[i];
if (strcmp(name, region->name) == 0) return region; if (strcmp(name, skip_hash(region->name)) == 0) return region;
} }
return NULL; // not found return NULL; // not found
} }
@@ -157,11 +168,12 @@ RegionEntry* RegionMap::findByName(const char* name) {
RegionEntry* RegionMap::findByNamePrefix(const char* prefix) { RegionEntry* RegionMap::findByNamePrefix(const char* prefix) {
if (strcmp(prefix, "*") == 0) return &wildcard; if (strcmp(prefix, "*") == 0) return &wildcard;
if (*prefix == '#') { prefix++; } // ignore the '#' when matching by name
RegionEntry* partial = NULL; RegionEntry* partial = NULL;
for (int i = 0; i < num_regions; i++) { for (int i = 0; i < num_regions; i++) {
auto region = &regions[i]; auto region = &regions[i];
if (strcmp(prefix, region->name) == 0) return region; // is a complete match, preference this one if (strcmp(prefix, skip_hash(region->name)) == 0) return region; // is a complete match, preference this one
if (memcmp(prefix, region->name, strlen(prefix)) == 0) { if (memcmp(prefix, skip_hash(region->name), strlen(prefix)) == 0) {
partial = region; partial = region;
} }
} }
@@ -220,9 +232,9 @@ void RegionMap::printChildRegions(int indent, const RegionEntry* parent, Stream&
} }
if (parent->flags & REGION_DENY_FLOOD) { if (parent->flags & REGION_DENY_FLOOD) {
out.printf("%s%s\n", parent->name, parent->id == home_id ? "^" : ""); out.printf("%s%s\n", skip_hash(parent->name), parent->id == home_id ? "^" : "");
} else { } else {
out.printf("%s%s F\n", parent->name, parent->id == home_id ? "^" : ""); out.printf("%s%s F\n", skip_hash(parent->name), parent->id == home_id ? "^" : "");
} }
for (int i = 0; i < num_regions; i++) { for (int i = 0; i < num_regions; i++) {
@@ -247,9 +259,10 @@ int RegionMap::exportNamesTo(char *dest, int max_len, uint8_t mask) {
for (int i = 0; i < num_regions; i++) { for (int i = 0; i < num_regions; i++) {
auto region = &regions[i]; auto region = &regions[i];
if ((region->flags & mask) == 0) { // region allowed? (per 'mask' param) if ((region->flags & mask) == 0) { // region allowed? (per 'mask' param)
int len = strlen(region->name); const char* name = skip_hash(region->name);
int len = strlen(name);
if ((dp - dest) + len + 2 < max_len) { // only append if name will fit if ((dp - dest) + len + 2 < max_len) { // only append if name will fit
memcpy(dp, region->name, len); memcpy(dp, name, len);
dp += len; dp += len;
*dp++ = ','; *dp++ = ',';
} }

View File

@@ -42,13 +42,14 @@ public:
uint32_t n_recv_flood, uint32_t n_recv_flood,
uint32_t n_recv_direct) { uint32_t n_recv_direct) {
sprintf(reply, sprintf(reply,
"{\"recv\":%u,\"sent\":%u,\"flood_tx\":%u,\"direct_tx\":%u,\"flood_rx\":%u,\"direct_rx\":%u}", "{\"recv\":%u,\"sent\":%u,\"flood_tx\":%u,\"direct_tx\":%u,\"flood_rx\":%u,\"direct_rx\":%u,\"recv_errors\":%u}",
driver.getPacketsRecv(), driver.getPacketsRecv(),
driver.getPacketsSent(), driver.getPacketsSent(),
n_sent_flood, n_sent_flood,
n_sent_direct, n_sent_direct,
n_recv_flood, n_recv_flood,
n_recv_direct n_recv_direct,
driver.getPacketsRecvErrors()
); );
} }
}; };

View File

@@ -9,11 +9,21 @@
#define ADVERT_RESTART_DELAY 1000 // millis #define ADVERT_RESTART_DELAY 1000 // millis
void SerialBLEInterface::begin(const char* device_name, uint32_t pin_code) { void SerialBLEInterface::begin(const char* prefix, char* name, uint32_t pin_code) {
_pin_code = pin_code; _pin_code = pin_code;
if (strcmp(name, "@@MAC") == 0) {
uint8_t addr[8];
memset(addr, 0, sizeof(addr));
esp_efuse_mac_get_default(addr);
sprintf(name, "%02X%02X%02X%02X%02X%02X", // modify (IN-OUT param)
addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
}
char dev_name[32+16];
sprintf(dev_name, "%s%s", prefix, name);
// Create the BLE Device // Create the BLE Device
BLEDevice::init(device_name); BLEDevice::init(dev_name);
BLEDevice::setSecurityCallbacks(this); BLEDevice::setSecurityCallbacks(this);
BLEDevice::setMTU(MAX_FRAME_SIZE); BLEDevice::setMTU(MAX_FRAME_SIZE);

View File

@@ -61,7 +61,13 @@ public:
send_queue_len = recv_queue_len = 0; send_queue_len = recv_queue_len = 0;
} }
void begin(const char* device_name, uint32_t pin_code); /**
* init the BLE interface.
* @param prefix a prefix for the device name
* @param name IN/OUT - a name for the device (combined with prefix). If "@@MAC", is modified and returned
* @param pin_code the BLE security pin
*/
void begin(const char* prefix, char* name, uint32_t pin_code);
// BaseSerialInterface methods // BaseSerialInterface methods
void enable() override; void enable() override;

View File

@@ -123,7 +123,7 @@ void SerialBLEInterface::onBLEEvent(ble_evt_t* evt) {
} }
} }
void SerialBLEInterface::begin(const char* device_name, uint32_t pin_code) { void SerialBLEInterface::begin(const char* prefix, char* name, uint32_t pin_code) {
instance = this; instance = this;
char charpin[20]; char charpin[20];
@@ -134,6 +134,16 @@ void SerialBLEInterface::begin(const char* device_name, uint32_t pin_code) {
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.begin(); Bluefruit.begin();
char dev_name[32+16];
if (strcmp(name, "@@MAC") == 0) {
ble_gap_addr_t addr;
if (sd_ble_gap_addr_get(&addr) == NRF_SUCCESS) {
sprintf(name, "%02X%02X%02X%02X%02X%02X", // modify (IN-OUT param)
addr.addr[5], addr.addr[4], addr.addr[3], addr.addr[2], addr.addr[1], addr.addr[0]);
}
}
sprintf(dev_name, "%s%s", prefix, name);
// Connection interval units: 1.25ms, supervision timeout units: 10ms // Connection interval units: 1.25ms, supervision timeout units: 10ms
ble_gap_conn_params_t ppcp_params; ble_gap_conn_params_t ppcp_params;
ppcp_params.min_conn_interval = BLE_MIN_CONN_INTERVAL; ppcp_params.min_conn_interval = BLE_MIN_CONN_INTERVAL;
@@ -153,7 +163,7 @@ void SerialBLEInterface::begin(const char* device_name, uint32_t pin_code) {
} }
Bluefruit.setTxPower(BLE_TX_POWER); Bluefruit.setTxPower(BLE_TX_POWER);
Bluefruit.setName(device_name); Bluefruit.setName(dev_name);
Bluefruit.Security.setMITM(true); Bluefruit.Security.setMITM(true);
Bluefruit.Security.setPIN(charpin); Bluefruit.Security.setPIN(charpin);

View File

@@ -52,7 +52,14 @@ public:
recv_queue_len = 0; recv_queue_len = 0;
} }
void begin(const char* device_name, uint32_t pin_code); /**
* init the BLE interface.
* @param prefix a prefix for the device name
* @param name IN/OUT - a name for the device (combined with prefix). If "@@MAC", is modified and returned
* @param pin_code the BLE security pin
*/
void begin(const char* prefix, char* name, uint32_t pin_code);
void disconnect(); void disconnect();
void enable() override; void enable() override;
void disable() override; void disable() override;

View File

@@ -105,6 +105,7 @@ int RadioLibWrapper::recvRaw(uint8_t* bytes, int sz) {
if (err != RADIOLIB_ERR_NONE) { if (err != RADIOLIB_ERR_NONE) {
MESH_DEBUG_PRINTLN("RadioLibWrapper: error: readData(%d)", err); MESH_DEBUG_PRINTLN("RadioLibWrapper: error: readData(%d)", err);
len = 0; len = 0;
n_recv_errors++;
} else { } else {
// Serial.print(" readData() -> "); Serial.println(len); // Serial.print(" readData() -> "); Serial.println(len);
n_recv++; n_recv++;

View File

@@ -7,7 +7,7 @@ class RadioLibWrapper : public mesh::Radio {
protected: protected:
PhysicalLayer* _radio; PhysicalLayer* _radio;
mesh::MainBoard* _board; mesh::MainBoard* _board;
uint32_t n_recv, n_sent; uint32_t n_recv, n_sent, n_recv_errors;
int16_t _noise_floor, _threshold; int16_t _noise_floor, _threshold;
uint16_t _num_floor_samples; uint16_t _num_floor_samples;
int32_t _floor_sample_sum; int32_t _floor_sample_sum;
@@ -45,8 +45,9 @@ public:
void loop() override; void loop() override;
uint32_t getPacketsRecv() const { return n_recv; } uint32_t getPacketsRecv() const { return n_recv; }
uint32_t getPacketsRecvErrors() const { return n_recv_errors; }
uint32_t getPacketsSent() const { return n_sent; } uint32_t getPacketsSent() const { return n_sent; }
void resetStats() { n_recv = n_sent = 0; } void resetStats() { n_recv = n_sent = n_recv_errors = 0; }
virtual float getLastRSSI() const override; virtual float getLastRSSI() const override;
virtual float getLastSNR() const override; virtual float getLastSNR() const override;

View File

@@ -42,7 +42,7 @@ static Adafruit_BME280 BME280;
#endif #endif
#define TELEM_BMP280_SEALEVELPRESSURE_HPA (1013.25) // Athmospheric pressure at sea level #define TELEM_BMP280_SEALEVELPRESSURE_HPA (1013.25) // Athmospheric pressure at sea level
#include <Adafruit_BMP280.h> #include <Adafruit_BMP280.h>
static Adafruit_BMP280 BMP280; static Adafruit_BMP280 BMP280(TELEM_WIRE);
#endif #endif
#if ENV_INCLUDE_SHTC3 #if ENV_INCLUDE_SHTC3
@@ -58,6 +58,7 @@ static SensirionI2cSht4x SHT4X;
#if ENV_INCLUDE_LPS22HB #if ENV_INCLUDE_LPS22HB
#include <Arduino_LPS22HB.h> #include <Arduino_LPS22HB.h>
LPS22HBClass LPS22HB(*TELEM_WIRE);
#endif #endif
#if ENV_INCLUDE_INA3221 #if ENV_INCLUDE_INA3221
@@ -218,7 +219,7 @@ bool EnvironmentSensorManager::begin() {
#endif #endif
#if ENV_INCLUDE_SHTC3 #if ENV_INCLUDE_SHTC3
if (SHTC3.begin()) { if (SHTC3.begin(TELEM_WIRE)) {
MESH_DEBUG_PRINTLN("Found sensor: SHTC3"); MESH_DEBUG_PRINTLN("Found sensor: SHTC3");
SHTC3_initialized = true; SHTC3_initialized = true;
} else { } else {
@@ -243,7 +244,7 @@ bool EnvironmentSensorManager::begin() {
#endif #endif
#if ENV_INCLUDE_LPS22HB #if ENV_INCLUDE_LPS22HB
if (BARO.begin()) { if (LPS22HB.begin()) {
MESH_DEBUG_PRINTLN("Found sensor: LPS22HB"); MESH_DEBUG_PRINTLN("Found sensor: LPS22HB");
LPS22HB_initialized = true; LPS22HB_initialized = true;
} else { } else {
@@ -407,8 +408,8 @@ bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, Cayen
#if ENV_INCLUDE_LPS22HB #if ENV_INCLUDE_LPS22HB
if (LPS22HB_initialized) { if (LPS22HB_initialized) {
telemetry.addTemperature(TELEM_CHANNEL_SELF, BARO.readTemperature()); telemetry.addTemperature(TELEM_CHANNEL_SELF, LPS22HB.readTemperature());
telemetry.addBarometricPressure(TELEM_CHANNEL_SELF, BARO.readPressure() * 10); // convert kPa to hPa telemetry.addBarometricPressure(TELEM_CHANNEL_SELF, LPS22HB.readPressure() * 10); // convert kPa to hPa
} }
#endif #endif

56
tools/maint/README.md Normal file
View File

@@ -0,0 +1,56 @@
# Maintenance Tools
This directory contains automation for managing our **Friendly Fork**. It allows us to integrate community-submitted Pull Requests from the upstream repository into our local development branches.
## Why this exists
In firmware development, critical bug fixes or hardware support often exist in the upstream "Pull Request" queue long before they are officially merged. This tool allows us to build an integrated firmware version that includes those necessary patches while remaining syncable with the official source.
## Usage
### 1. Prerequisites
You must have the original repository added as a remote named `upstream`:
```bash
git remote add upstream https://github.com/meshcore-dev/MeshCore.git
```
### 2. Basic Commands
**Apply specific patches:**
To pull in PR #1338 and PR #1400 from the upstream project:
```bash
./tools/maint/apply_patches.sh 1338 1400
```
**Start over (Reset):**
To wipe the integrated branch and reset it to match the official upstream `main` branch exactly:
```bash
./tools/maint/apply_patches.sh --reset
```
## Traceability
Every time this script runs, it updates `patch_manifest.log`. This file tracks:
* The date of the integration.
* The base commit SHA we started from.
* The specific commit SHA of every PR applied.
**This log is essential for debugging firmware regressions.** If the hardware fails, check the manifest to identify which experimental patch might be the cause.
---
### A Note on Merge Conflicts
If a PR cannot be applied automatically, the script will abort the merge. You will need to:
1. Manually merge the PR.
2. Resolve the conflicts in your editor.
3. Commit the result.
4. Manually update the `patch_manifest.log`.

65
tools/maint/apply_patches.sh Executable file
View File

@@ -0,0 +1,65 @@
#!/bin/bash
# Configuration
UPSTREAM_REMOTE="upstream"
BASE_BRANCH="main" # Change to 'master' if that's what upstream uses
TARGET_BRANCH="main-integrated"
MANIFEST_FILE="tools/maint/patch_manifest.log"
# Function to reset the branch
reset_to_upstream() {
echo "Warning: This will wipe all local changes on $TARGET_BRANCH."
read -p "Are you sure you want to reset to $UPSTREAM_REMOTE/$BASE_BRANCH? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
git fetch "$UPSTREAM_REMOTE"
git checkout -B "$TARGET_BRANCH" "$UPSTREAM_REMOTE/$BASE_BRANCH"
echo "--- Reset to Upstream: $(date) ---" > "$MANIFEST_FILE"
echo "Base Commit: $(git rev-parse HEAD)" >> "$MANIFEST_FILE"
echo "Reset successful. Branch is now clean."
else
echo "Reset aborted."
fi
}
# Check for reset flag
if [[ "$1" == "--reset" ]]; then
reset_to_upstream
exit 0
fi
# Standard PR application logic
PR_IDS=("$@")
if [ ${#PR_IDS[@]} -eq 0 ]; then
echo "Usage:"
echo " Apply PRs: $0 <PR_ID1> <PR_ID2> ..."
echo " Reset: $0 --reset"
exit 1
fi
# Ensure target branch exists and is checked out
git checkout -B "$TARGET_BRANCH"
echo "--- Patch Session: $(date) ---" >> "$MANIFEST_FILE"
for PR in "${PR_IDS[@]}"; do
echo "--------------------------------------"
echo "Fetching PR #$PR..."
if git fetch "$UPSTREAM_REMOTE" "pull/$PR/head:PR_TEMP_FETCH"; then
if git merge PR_TEMP_FETCH --no-edit -m "Integrate upstream PR #$PR"; then
echo "Successfully integrated PR #$PR"
echo "PR #$PR SHA: $(git rev-parse PR_TEMP_FETCH)" >> "$MANIFEST_FILE"
else
echo "Conflict in PR #$PR. Aborting merge."
git merge --abort
exit 1
fi
git branch -D PR_TEMP_FETCH
else
echo "Error: PR #$PR not found."
fi
done
echo "--------------------------------------"
echo "Done. See $MANIFEST_FILE for details."

View File

@@ -0,0 +1,64 @@
#!/bin/bash # Note: switched to bash for process substitution support
export PATH="$HOME/.platformio/penv/bin:$PATH"
LOGFILE="$PWD/meshcore-evo-fw.log"
FIRMWARE_VERSION="v1.11.0-evo_0.1.3"
FIRMWARE_BUILD_DATE=$(date '+%d-%b-%Y')
collect_bin_files(){
DEST_DIR="./firmwares"
mkdir -p "$DEST_DIR"
BUILD_DIR=".pio/build"
if [ ! -d "$BUILD_DIR" ]; then
echo "Error: $BUILD_DIR not found. Did you run the build process?"
exit 1
fi
echo "Copying firmware files to $DEST_DIR..."
for target_path in "$BUILD_DIR"/*/; do
echo $target_path
target_name=$(basename "$target_path")
# if ls "$target_path"*.bin >/dev/null 2>&1; then
for bin_file in "$target_path"*firmware*.{uf2,bin,zip}; do
filename=$(basename "$bin_file")
new_filename="${target_name}_${FIRMWARE_VERSION}_${FIRMWARE_BUILD_DATE}_${filename}"
cp "$bin_file" "$DEST_DIR/$new_filename"
echo "Done: $new_filename"
done
# fi
done
}
# Everything after this line goes to BOTH console and logfile
exec > >(tee -a "$LOGFILE") 2>&1
echo "-------------------- Build start ----------------"
date
echo "-------------------------------------------------"
# apply patches
# ./tools/maint/apply_patches.sh 1199 1338 1297
# build all repeater firmwares, the will be in .out
FIRMWARE_VERSION="v1.11_evo" ./build.sh build-repeater-firmwares
# build single firmwares
#FIRMWARE_VERSION=$FIRMWARE_VERSION FIRMWARE_BUILD_DATE=$FIRMWARE_BUILD_DATE ./build.sh build-firmware ProMicro_repeater
#FIRMWARE_VERSION=$FIRMWARE_VERSION FIRMWARE_BUILD_DATE=$FIRMWARE_BUILD_DATE ./build.sh build-firmware RAK_4631_repeater
#FIRMWARE_VERSION=$FIRMWARE_VERSION FIRMWARE_BUILD_DATE=$FIRMWARE_BUILD_DATE ./build.sh build-firmware heltec_v4_repeater
#FIRMWARE_VERSION=$FIRMWARE_VERSION FIRMWARE_BUILD_DATE=$FIRMWARE_BUILD_DATE ./build.sh build-firmware Heltec_v3_repeater
#FIRMWARE_VERSION=$FIRMWARE_VERSION FIRMWARE_BUILD_DATE=$FIRMWARE_BUILD_DATE ./build.sh build-firmware Xiao_nrf52_repeater
#FIRMWARE_VERSION=$FIRMWARE_VERSION FIRMWARE_BUILD_DATE=$FIRMWARE_BUILD_DATE ./build.sh build-firmware LilyGo_T3S3_sx1262_repeater
#FIRMWARE_VERSION=$FIRMWARE_VERSION FIRMWARE_BUILD_DATE=$FIRMWARE_BUILD_DATE ./build.sh build-firmware Heltec_t114_without_display_repeater
#FIRMWARE_VERSION=$FIRMWARE_VERSION FIRMWARE_BUILD_DATE=$FIRMWARE_BUILD_DATE ./build.sh build-firmware Heltec_t114_repeater
#collect_bin_files
echo "-------------------- Build end ------------------"
date
echo "-------------------------------------------------"
#grep -E " SUCCESS | FAILED " hansemesh_fw.log

View File

@@ -3,6 +3,35 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h> #include <Wire.h>
#ifdef NRF52_POWER_MANAGEMENT
// Static configuration for power management
// Values come from variant.h defines
const PowerMgtConfig power_config = {
.lpcomp_ain_channel = PWRMGT_LPCOMP_AIN,
.lpcomp_refsel = PWRMGT_LPCOMP_REFSEL,
.voltage_bootlock = PWRMGT_VOLTAGE_BOOTLOCK
};
void T114Board::initiateShutdown(uint8_t reason) {
#if ENV_INCLUDE_GPS == 1
pinMode(GPS_EN, OUTPUT);
digitalWrite(GPS_EN, LOW);
#endif
digitalWrite(SX126X_POWER_EN, LOW);
bool enable_lpcomp = (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
reason == SHUTDOWN_REASON_BOOT_PROTECT);
pinMode(PIN_BAT_CTL, OUTPUT);
digitalWrite(PIN_BAT_CTL, enable_lpcomp ? HIGH : LOW);
if (enable_lpcomp) {
configureVoltageWake(power_config.lpcomp_ain_channel, power_config.lpcomp_refsel);
}
enterSystemOff(reason);
}
#endif // NRF52_POWER_MANAGEMENT
void T114Board::begin() { void T114Board::begin() {
NRF52Board::begin(); NRF52Board::begin();
NRF_POWER->DCDCEN = 1; NRF_POWER->DCDCEN = 1;
@@ -21,6 +50,11 @@ void T114Board::begin() {
#endif #endif
pinMode(SX126X_POWER_EN, OUTPUT); pinMode(SX126X_POWER_EN, OUTPUT);
#ifdef NRF52_POWER_MANAGEMENT
// Boot voltage protection check (may not return if voltage too low)
// We need to call this after we configure SX126X_POWER_EN as output but before we pull high
checkBootVoltage(&power_config);
#endif
digitalWrite(SX126X_POWER_EN, HIGH); digitalWrite(SX126X_POWER_EN, HIGH);
delay(10); // give sx1262 some time to power up delay(10); // give sx1262 some time to power up
} }

View File

@@ -10,6 +10,11 @@
#define MV_LSB (3000.0F / 4096.0F) // 12-bit ADC with 3.0V input range #define MV_LSB (3000.0F / 4096.0F) // 12-bit ADC with 3.0V input range
class T114Board : public NRF52BoardOTA { class T114Board : public NRF52BoardOTA {
protected:
#ifdef NRF52_POWER_MANAGEMENT
void initiateShutdown(uint8_t reason) override;
#endif
public: public:
T114Board() : NRF52BoardOTA("T114_OTA") {} T114Board() : NRF52BoardOTA("T114_OTA") {}
void begin(); void begin();
@@ -42,13 +47,13 @@ public:
} }
void powerOff() override { void powerOff() override {
#ifdef LED_PIN #ifdef LED_PIN
digitalWrite(LED_PIN, HIGH); digitalWrite(LED_PIN, HIGH);
#endif #endif
#if ENV_INCLUDE_GPS == 1 #if ENV_INCLUDE_GPS == 1
pinMode(GPS_EN, OUTPUT); pinMode(GPS_EN, OUTPUT);
digitalWrite(GPS_EN, LOW); digitalWrite(GPS_EN, LOW);
#endif #endif
sd_power_system_off(); sd_power_system_off();
} }
}; };

View File

@@ -11,6 +11,7 @@ build_flags = ${nrf52_base.build_flags}
-I variants/heltec_t114 -I variants/heltec_t114
-I src/helpers/ui -I src/helpers/ui
-D HELTEC_T114 -D HELTEC_T114
-D NRF52_POWER_MANAGEMENT
-D P_LORA_DIO_1=20 -D P_LORA_DIO_1=20
-D P_LORA_NSS=24 -D P_LORA_NSS=24
-D P_LORA_RESET=25 -D P_LORA_RESET=25

View File

@@ -30,6 +30,14 @@
#define AREF_VOLTAGE (3.0) #define AREF_VOLTAGE (3.0)
// Power management boot protection threshold (millivolts)
// Set to 0 to disable boot protection
#define PWRMGT_VOLTAGE_BOOTLOCK 3300 // Won't boot below this voltage (mV)
// LPCOMP wake configuration (voltage recovery from SYSTEMOFF)
// AIN2 = P0.04 = BATTERY_PIN / PIN_VBAT_READ
#define PWRMGT_LPCOMP_AIN 2
#define PWRMGT_LPCOMP_REFSEL 1 // 2/8 VDD (~3.68-4.04V)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Number of pins // Number of pins
@@ -50,8 +58,8 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// I2C pin definition // I2C pin definition
#define PIN_WIRE_SDA (26) // P0.26 #define PIN_WIRE_SDA (16) // P0.16
#define PIN_WIRE_SCL (27) // P0.27 #define PIN_WIRE_SCL (13) // P0.13
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// SPI pin definition // SPI pin definition

View File

@@ -323,7 +323,7 @@ lib_deps =
extends = Heltec_lora32_v3 extends = Heltec_lora32_v3
build_flags = build_flags =
${Heltec_lora32_v3.build_flags} ${Heltec_lora32_v3.build_flags}
-D MAX_CONTACTS=140 -D MAX_CONTACTS=350
-D MAX_GROUP_CHANNELS=40 -D MAX_GROUP_CHANNELS=40
; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 ; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1
; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 ; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1

View File

@@ -5,7 +5,7 @@ build_flags =
${esp32_base.build_flags} ${esp32_base.build_flags}
-I variants/heltec_wireless_paper -I variants/heltec_wireless_paper
-D HELTEC_WIRELESS_PAPER -D HELTEC_WIRELESS_PAPER
-D ARDUINO_USB_CDC_ON_BOOT=1 ; need for Serial ;-D ARDUINO_USB_CDC_ON_BOOT=1 ; this breaks Serial
-D P_LORA_DIO_1=14 -D P_LORA_DIO_1=14
-D P_LORA_NSS=8 -D P_LORA_NSS=8
-D P_LORA_RESET=RADIOLIB_NC -D P_LORA_RESET=RADIOLIB_NC
@@ -17,8 +17,8 @@ build_flags =
-D WRAPPER_CLASS=CustomSX1262Wrapper -D WRAPPER_CLASS=CustomSX1262Wrapper
-D LORA_TX_POWER=22 -D LORA_TX_POWER=22
-D P_LORA_TX_LED=18 -D P_LORA_TX_LED=18
-D PIN_BOARD_SDA=17 ;-D PIN_BOARD_SDA=17
-D PIN_BOARD_SCL=18 ;-D PIN_BOARD_SCL=18 ; same GPIO as P_LORA_TX_LED
-D PIN_USER_BTN=0 -D PIN_USER_BTN=0
-D PIN_VEXT_EN=45 -D PIN_VEXT_EN=45
-D PIN_VBAT_READ=20 -D PIN_VBAT_READ=20

View File

@@ -27,6 +27,7 @@ build_flags = ${nrf52_base.build_flags}
-D PIN_USER_BTN=0 -D PIN_USER_BTN=0
-D PIN_WIRE_SCL=7 -D PIN_WIRE_SCL=7
-D PIN_WIRE_SDA=6 -D PIN_WIRE_SDA=6
-UENV_INCLUDE_GPS
lib_deps = ${nrf52_base.lib_deps} lib_deps = ${nrf52_base.lib_deps}
${sensor_base.lib_deps} ${sensor_base.lib_deps}

View File

@@ -0,0 +1,96 @@
#pragma once
#include <Arduino.h>
#include <helpers/RefCountedDigitalPin.h>
#include <helpers/ESP32Board.h>
// built-ins
#ifndef PIN_VBAT_READ
#define PIN_VBAT_READ 1
#endif
#ifndef PIN_ADC_CTRL
#define PIN_ADC_CTRL 36
#endif
#define PIN_ADC_CTRL_ACTIVE LOW
#define PIN_ADC_CTRL_INACTIVE HIGH
#define ADC_MULTIPLIER (3 * 1.73 * 1.187 * 1000)
#define BATTERY_SAMPLES 8
#include <driver/rtc_io.h>
class RAK3112Board : public ESP32Board {
private:
bool adc_active_state;
public:
RefCountedDigitalPin periph_power;
RAK3112Board() : periph_power(PIN_VEXT_EN) { }
void begin() {
ESP32Board::begin();
// Auto-detect correct ADC_CTRL pin polarity (different for boards >3.2)
pinMode(PIN_ADC_CTRL, INPUT);
adc_active_state = !digitalRead(PIN_ADC_CTRL);
pinMode(PIN_ADC_CTRL, OUTPUT);
digitalWrite(PIN_ADC_CTRL, !adc_active_state); // Initially inactive
periph_power.begin();
esp_reset_reason_t reason = esp_reset_reason();
if (reason == ESP_RST_DEEPSLEEP) {
long wakeup_source = esp_sleep_get_ext1_wakeup_status();
if (wakeup_source & (1 << P_LORA_DIO_1)) { // received a LoRa packet (while in deep sleep)
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
}
}
void enterDeepSleep(uint32_t secs, int pin_wake_btn = -1) {
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
// Make sure the DIO1 and NSS GPIOs are hold on required levels during deep sleep
rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY);
rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1);
rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS);
if (pin_wake_btn < 0) {
esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet
} else {
esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1) | (1L << pin_wake_btn), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet OR wake btn
}
if (secs > 0) {
esp_sleep_enable_timer_wakeup(secs * 1000000);
}
// Finally set ESP32 into sleep
esp_deep_sleep_start(); // CPU halts here and never returns!
}
void powerOff() override {
enterDeepSleep(0);
}
uint16_t getBattMilliVolts() override {
analogReadResolution(12);
uint32_t raw = 0;
for (int i = 0; i < BATTERY_SAMPLES; i++) {
raw += analogRead(PIN_VBAT_READ);
}
raw = raw / BATTERY_SAMPLES;
return (ADC_MULTIPLIER * raw) / 4096;
}
const char* getManufacturerName() const override {
return "RAK 3112";
}
};

View File

@@ -0,0 +1,223 @@
[rak3112]
extends = esp32_base
board = esp32-s3-devkitc-1
build_flags =
${esp32_base.build_flags}
${sensor_base.build_flags}
-I variants/rak3112
-D RAK_3112=1
-D ESP32_CPU_FREQ=80
-D P_LORA_DIO_1=47
-D P_LORA_NSS=7
-D P_LORA_RESET=8
-D P_LORA_BUSY=48
-D P_LORA_SCLK=5
-D P_LORA_MISO=3
-D P_LORA_MOSI=6
-D RADIO_CLASS=CustomSX1262
-D WRAPPER_CLASS=CustomSX1262Wrapper
-D LORA_TX_POWER=22
-D P_LORA_TX_LED=46
-D PIN_BOARD_SDA=9
-D PIN_BOARD_SCL=40
-D PIN_USER_BTN=-1
-D PIN_VEXT_EN=14
-D SX126X_DIO2_AS_RF_SWITCH=true
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
-D SX126X_CURRENT_LIMIT=140
-D SX126X_RX_BOOSTED_GAIN=1
-D PIN_GPS_RX=43
-D PIN_GPS_TX=44
; -D PIN_GPS_EN=26
build_src_filter = ${esp32_base.build_src_filter}
+<../variants/rak3112>
+<helpers/sensors>
lib_deps =
${esp32_base.lib_deps}
${sensor_base.lib_deps}
[env:RAK3112_repeater]
extends = rak3112
build_flags =
${rak3112.build_flags}
-D DISPLAY_CLASS=SSD1306Display
-D ADVERT_NAME='"RAK3112 Repeater"'
-D ADVERT_LAT=0.0
-D ADVERT_LON=0.0
-D ADMIN_PASSWORD='"password"'
-D MAX_NEIGHBOURS=50
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1
build_src_filter = ${rak3112.build_src_filter}
+<helpers/ui/SSD1306Display.cpp>
+<../examples/simple_repeater>
lib_deps =
${rak3112.lib_deps}
${esp32_ota.lib_deps}
bakercp/CRC32 @ ^2.0.0
[env:RAK3112_repeater_bridge_rs232]
extends = rak3112
build_flags =
${rak3112.build_flags}
-D DISPLAY_CLASS=SSD1306Display
-D ADVERT_NAME='"RS232 Bridge"'
-D ADVERT_LAT=0.0
-D ADVERT_LON=0.0
-D ADMIN_PASSWORD='"password"'
-D MAX_NEIGHBOURS=50
-D WITH_RS232_BRIDGE=Serial2
-D WITH_RS232_BRIDGE_RX=5
-D WITH_RS232_BRIDGE_TX=6
; -D BRIDGE_DEBUG=1
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1
build_src_filter = ${rak3112.build_src_filter}
+<helpers/bridges/RS232Bridge.cpp>
+<helpers/ui/SSD1306Display.cpp>
+<../examples/simple_repeater>
lib_deps =
${rak3112.lib_deps}
${esp32_ota.lib_deps}
[env:RAK3112_repeater_bridge_espnow]
extends = rak3112
build_flags =
${rak3112.build_flags}
-D DISPLAY_CLASS=SSD1306Display
-D ADVERT_NAME='"ESPNow Bridge"'
-D ADVERT_LAT=0.0
-D ADVERT_LON=0.0
-D ADMIN_PASSWORD='"password"'
-D MAX_NEIGHBOURS=50
-D WITH_ESPNOW_BRIDGE=1
; -D BRIDGE_DEBUG=1
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1
build_src_filter = ${rak3112.build_src_filter}
+<helpers/bridges/ESPNowBridge.cpp>
+<helpers/ui/SSD1306Display.cpp>
+<../examples/simple_repeater>
lib_deps =
${rak3112.lib_deps}
${esp32_ota.lib_deps}
[env:RAK3112_room_server]
extends = rak3112
build_flags =
${rak3112.build_flags}
-D DISPLAY_CLASS=SSD1306Display
-D ADVERT_NAME='"RAK3112 Room"'
-D ADVERT_LAT=0.0
-D ADVERT_LON=0.0
-D ADMIN_PASSWORD='"password"'
-D ROOM_PASSWORD='"hello"'
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1
build_src_filter = ${rak3112.build_src_filter}
+<helpers/ui/SSD1306Display.cpp>
+<../examples/simple_room_server>
lib_deps =
${rak3112.lib_deps}
${esp32_ota.lib_deps}
[env:RAK3112_terminal_chat]
extends = rak3112
build_flags =
${rak3112.build_flags}
-D MAX_CONTACTS=350
-D MAX_GROUP_CHANNELS=1
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1
build_src_filter = ${rak3112.build_src_filter}
+<../examples/simple_secure_chat/main.cpp>
lib_deps =
${rak3112.lib_deps}
densaugeo/base64 @ ~1.4.0
[env:RAK3112_companion_radio_usb]
extends = rak3112
build_flags =
${rak3112.build_flags}
-I examples/companion_radio/ui-new
-D MAX_CONTACTS=350
-D MAX_GROUP_CHANNELS=40
-D DISPLAY_CLASS=SSD1306Display
; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1
; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1
build_src_filter = ${rak3112.build_src_filter}
+<helpers/ui/SSD1306Display.cpp>
+<helpers/ui/MomentaryButton.cpp>
+<../examples/companion_radio/*.cpp>
+<../examples/companion_radio/ui-new/*.cpp>
lib_deps =
${rak3112.lib_deps}
densaugeo/base64 @ ~1.4.0
[env:RAK3112_companion_radio_ble]
extends = rak3112
build_flags =
${rak3112.build_flags}
-I examples/companion_radio/ui-new
-D MAX_CONTACTS=350
-D MAX_GROUP_CHANNELS=40
-D DISPLAY_CLASS=SSD1306Display
-D BLE_PIN_CODE=123456 ; dynamic, random PIN
-D AUTO_SHUTDOWN_MILLIVOLTS=3400
-D BLE_DEBUG_LOGGING=1
-D OFFLINE_QUEUE_SIZE=256
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1
build_src_filter = ${rak3112.build_src_filter}
+<helpers/ui/SSD1306Display.cpp>
+<helpers/ui/MomentaryButton.cpp>
+<helpers/esp32/*.cpp>
+<../examples/companion_radio/*.cpp>
+<../examples/companion_radio/ui-new/*.cpp>
lib_deps =
${rak3112.lib_deps}
densaugeo/base64 @ ~1.4.0
[env:RAK3112_companion_radio_wifi]
extends = rak3112
build_flags =
${rak3112.build_flags}
-I examples/companion_radio/ui-new
-D MAX_CONTACTS=350
-D MAX_GROUP_CHANNELS=40
-D DISPLAY_CLASS=SSD1306Display
-D WIFI_DEBUG_LOGGING=1
-D WIFI_SSID='"myssid"'
-D WIFI_PWD='"mypwd"'
-D OFFLINE_QUEUE_SIZE=256
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1
build_src_filter = ${rak3112.build_src_filter}
+<helpers/ui/SSD1306Display.cpp>
+<helpers/ui/MomentaryButton.cpp>
+<helpers/esp32/*.cpp>
+<../examples/companion_radio/*.cpp>
+<../examples/companion_radio/ui-new/*.cpp>
lib_deps =
${rak3112.lib_deps}
densaugeo/base64 @ ~1.4.0
[env:RAK3112_sensor]
extends = rak3112
build_flags =
${rak3112.build_flags}
-D ADVERT_NAME='"RAK3112 v3 Sensor"'
-D ADVERT_LAT=0.0
-D ADVERT_LON=0.0
-D ADMIN_PASSWORD='"password"'
-D ENV_PIN_SDA=33
-D ENV_PIN_SCL=34
-D DISPLAY_CLASS=SSD1306Display
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1
build_src_filter = ${rak3112.build_src_filter}
+<helpers/ui/SSD1306Display.cpp>
+<../examples/simple_sensor>
lib_deps =
${rak3112.lib_deps}
${esp32_ota.lib_deps}

View File

@@ -0,0 +1,60 @@
#include <Arduino.h>
#include "target.h"
RAK3112Board board;
#if defined(P_LORA_SCLK)
static SPIClass spi;
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, spi);
#else
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY);
#endif
WRAPPER_CLASS radio_driver(radio, board);
ESP32RTCClock fallback_clock;
AutoDiscoverRTCClock rtc_clock(fallback_clock);
#if ENV_INCLUDE_GPS
#include <helpers/sensors/MicroNMEALocationProvider.h>
MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1);
EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea);
#else
EnvironmentSensorManager sensors;
#endif
#ifdef DISPLAY_CLASS
DISPLAY_CLASS display;
MomentaryButton user_btn(PIN_USER_BTN, 1000, true);
#endif
bool radio_init() {
fallback_clock.begin();
rtc_clock.begin(Wire);
#if defined(P_LORA_SCLK)
return radio.std_init(&spi);
#else
return radio.std_init();
#endif
}
uint32_t radio_get_rng_seed() {
return radio.random(0x7FFFFFFF);
}
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
radio.setFrequency(freq);
radio.setSpreadingFactor(sf);
radio.setBandwidth(bw);
radio.setCodingRate(cr);
}
void radio_set_tx_power(uint8_t dbm) {
radio.setOutputPower(dbm);
}
mesh::LocalIdentity radio_new_identity() {
RadioNoiseListener rng(radio);
return mesh::LocalIdentity(&rng); // create new random identity
}

30
variants/rak3112/target.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#define RADIOLIB_STATIC_ONLY 1
#include <RadioLib.h>
#include <helpers/radiolib/RadioLibWrappers.h>
#include <RAK3112Board.h>
#include <helpers/radiolib/CustomSX1262Wrapper.h>
#include <helpers/AutoDiscoverRTCClock.h>
#include <helpers/SensorManager.h>
#include <helpers/sensors/EnvironmentSensorManager.h>
#ifdef DISPLAY_CLASS
#include <helpers/ui/SSD1306Display.h>
#include <helpers/ui/MomentaryButton.h>
#endif
extern RAK3112Board board;
extern WRAPPER_CLASS radio_driver;
extern AutoDiscoverRTCClock rtc_clock;
extern EnvironmentSensorManager sensors;
#ifdef DISPLAY_CLASS
extern DISPLAY_CLASS display;
extern MomentaryButton user_btn;
#endif
bool radio_init();
uint32_t radio_get_rng_seed();
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr);
void radio_set_tx_power(uint8_t dbm);
mesh::LocalIdentity radio_new_identity();

View File

@@ -0,0 +1,32 @@
#include <Arduino.h>
#include <Wire.h>
#include "RAK3401Board.h"
void RAK3401Board::begin() {
NRF52BoardDCDC::begin();
pinMode(PIN_VBAT_READ, INPUT);
#ifdef PIN_USER_BTN
pinMode(PIN_USER_BTN, INPUT_PULLUP);
#endif
#ifdef PIN_USER_BTN_ANA
pinMode(PIN_USER_BTN_ANA, INPUT_PULLUP);
#endif
#if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL)
Wire.setPins(PIN_BOARD_SDA, PIN_BOARD_SCL);
#endif
Wire.begin();
pinMode(PIN_3V3_EN, OUTPUT);
digitalWrite(PIN_3V3_EN, HIGH);
#ifdef P_LORA_PA_EN
// Initialize RAK13302 1W LoRa transceiver module PA control pin
pinMode(P_LORA_PA_EN, OUTPUT);
digitalWrite(P_LORA_PA_EN, LOW); // Start with PA disabled
delay(10); // Allow PA module to initialize
#endif
}

View File

@@ -0,0 +1,70 @@
#pragma once
#include <MeshCore.h>
#include <Arduino.h>
#include <helpers/NRF52Board.h>
// LoRa radio module pins for RAK13302
#define P_LORA_SCLK 3
#define P_LORA_MISO 29
#define P_LORA_MOSI 30
#define P_LORA_NSS 26
#define P_LORA_DIO_1 10
#define P_LORA_BUSY 9
#define P_LORA_RESET 4
#ifndef P_LORA_PA_EN
#define P_LORA_PA_EN 31
#endif
//#define PIN_GPS_SDA 13 //GPS SDA pin (output option)
//#define PIN_GPS_SCL 14 //GPS SCL pin (output option)
// #define PIN_GPS_TX 16 //GPS TX pin
// #define PIN_GPS_RX 15 //GPS RX pin
#define PIN_GPS_1PPS 17 //GPS PPS pin
#define GPS_BAUD_RATE 9600
#define GPS_ADDRESS 0x42 //i2c address for GPS
#define SX126X_DIO2_AS_RF_SWITCH
#define SX126X_DIO3_TCXO_VOLTAGE 1.8
// built-ins
#define PIN_VBAT_READ 5
#define ADC_MULTIPLIER (3 * 1.73 * 1.187 * 1000)
#define PIN_3V3_EN (34)
#define WB_IO2 PIN_3V3_EN
class RAK3401Board : public NRF52BoardDCDC, public NRF52BoardOTA {
public:
RAK3401Board() : NRF52BoardOTA("RAK3401_OTA") {}
void begin();
#define BATTERY_SAMPLES 8
uint16_t getBattMilliVolts() override {
analogReadResolution(12);
uint32_t raw = 0;
for (int i = 0; i < BATTERY_SAMPLES; i++) {
raw += analogRead(PIN_VBAT_READ);
}
raw = raw / BATTERY_SAMPLES;
return (ADC_MULTIPLIER * raw) / 4096;
}
const char* getManufacturerName() const override {
return "RAK 3401";
}
#ifdef P_LORA_PA_EN
void onBeforeTransmit() override {
digitalWrite(P_LORA_PA_EN, HIGH); // Enable PA before transmission
}
void onAfterTransmit() override {
digitalWrite(P_LORA_PA_EN, LOW); // Disable PA after transmission to save power
}
#endif
};

View File

@@ -0,0 +1,127 @@
[rak3401]
extends = nrf52_base
board = rak3401
board_check = true
build_flags = ${nrf52_base.build_flags}
${sensor_base.build_flags}
-I variants/rak3401
-D RAK_3401
-D RAK13302
-D RADIO_CLASS=CustomSX1262
-D WRAPPER_CLASS=CustomSX1262Wrapper
-D LORA_TX_POWER=22
-D SX126X_CURRENT_LIMIT=140
-D SX126X_RX_BOOSTED_GAIN=1
build_src_filter = ${nrf52_base.build_src_filter}
+<../variants/rak3401>
+<helpers/sensors>
+<helpers/ui/SSD1306Display.cpp>
+<helpers/ui/MomentaryButton.cpp>
lib_deps =
${nrf52_base.lib_deps}
${sensor_base.lib_deps}
adafruit/Adafruit SSD1306 @ ^2.5.13
sparkfun/SparkFun u-blox GNSS Arduino Library@^2.2.27
[env:RAK_3401_repeater]
extends = rak3401
build_flags =
${rak3401.build_flags}
-D DISPLAY_CLASS=SSD1306Display
-D ADVERT_NAME='"RAK3401 1W Repeater"'
-D ADVERT_LAT=0.0
-D ADVERT_LON=0.0
-D ADMIN_PASSWORD='"password"'
-D MAX_NEIGHBOURS=50
;-D MESH_PACKET_LOGGING=1
;-D MESH_DEBUG=1
build_src_filter = ${rak3401.build_src_filter}
+<helpers/ui/SSD1306Display.cpp>
+<../examples/simple_repeater>
[env:RAK_3401_room_server]
extends = rak3401
build_flags =
${rak3401.build_flags}
-D DISPLAY_CLASS=SSD1306Display
-D ADVERT_NAME='"Test Room"'
-D ADVERT_LAT=0.0
-D ADVERT_LON=0.0
-D ADMIN_PASSWORD='"password"'
-D ROOM_PASSWORD='"hello"'
;-D MESH_PACKET_LOGGING=1
;-D MESH_DEBUG=1
build_src_filter = ${rak3401.build_src_filter}
+<helpers/ui/SSD1306Display.cpp>
+<../examples/simple_room_server>
[env:RAK_3401_companion_radio_usb]
extends = rak3401
board_build.ldscript = boards/nrf52840_s140_v6_extrafs.ld
board_upload.maximum_size = 712704
build_flags =
${rak3401.build_flags}
-I examples/companion_radio/ui-new
-D DISPLAY_CLASS=SSD1306Display
-D MAX_CONTACTS=350
-D MAX_GROUP_CHANNELS=40
; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1
; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1
build_src_filter = ${rak3401.build_src_filter}
+<../examples/companion_radio/*.cpp>
+<../examples/companion_radio/ui-new/*.cpp>
lib_deps =
${rak3401.lib_deps}
densaugeo/base64 @ ~1.4.0
[env:RAK_3401_companion_radio_ble]
extends = rak3401
board_build.ldscript = boards/nrf52840_s140_v6_extrafs.ld
board_upload.maximum_size = 712704
build_flags =
${rak3401.build_flags}
-I examples/companion_radio/ui-new
-D DISPLAY_CLASS=SSD1306Display
-D MAX_CONTACTS=350
-D MAX_GROUP_CHANNELS=40
-D BLE_PIN_CODE=123456
-D BLE_DEBUG_LOGGING=1
-D OFFLINE_QUEUE_SIZE=256
;-D MESH_PACKET_LOGGING=1
;-D MESH_DEBUG=1
build_src_filter = ${rak3401.build_src_filter}
+<helpers/nrf52/SerialBLEInterface.cpp>
+<../examples/companion_radio/*.cpp>
+<../examples/companion_radio/ui-new/*.cpp>
lib_deps =
${rak3401.lib_deps}
densaugeo/base64 @ ~1.4.0
[env:RAK_3401_terminal_chat]
extends = rak3401
build_flags =
${rak3401.build_flags}
-D MAX_CONTACTS=100
-D MAX_GROUP_CHANNELS=1
;-D MESH_PACKET_LOGGING=1
;-D MESH_DEBUG=1
build_src_filter = ${rak3401.build_src_filter}
+<../examples/simple_secure_chat/main.cpp>
lib_deps =
${rak3401.lib_deps}
densaugeo/base64 @ ~1.4.0
[env:RAK_3401_sensor]
extends = rak3401
build_flags =
${rak3401.build_flags}
-D DISPLAY_CLASS=SSD1306Display
-D ADVERT_NAME='"RAK3401 Sensor"'
-D ADVERT_LAT=0.0
-D ADVERT_LON=0.0
-D ADMIN_PASSWORD='"password"'
;-D MESH_PACKET_LOGGING=1
;-D MESH_DEBUG=1
build_src_filter = ${rak3401.build_src_filter}
+<helpers/ui/SSD1306Display.cpp>
+<../examples/simple_sensor>

View File

@@ -0,0 +1,58 @@
#include <Arduino.h>
#include "target.h"
#include <helpers/ArduinoHelpers.h>
RAK3401Board board;
#ifndef PIN_USER_BTN
#define PIN_USER_BTN (-1)
#endif
#ifdef DISPLAY_CLASS
DISPLAY_CLASS display;
MomentaryButton user_btn(PIN_USER_BTN, 1000, true, true);
#if defined(PIN_USER_BTN_ANA)
MomentaryButton analog_btn(PIN_USER_BTN_ANA, 1000, 20);
#endif
#endif
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, SPI);
WRAPPER_CLASS radio_driver(radio, board);
VolatileRTCClock fallback_clock;
AutoDiscoverRTCClock rtc_clock(fallback_clock);
#if ENV_INCLUDE_GPS
#include <helpers/sensors/MicroNMEALocationProvider.h>
MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1);
EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea);
#else
EnvironmentSensorManager sensors;
#endif
bool radio_init() {
rtc_clock.begin(Wire);
return radio.std_init(&SPI);
}
uint32_t radio_get_rng_seed() {
return radio.random(0x7FFFFFFF);
}
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
radio.setFrequency(freq);
radio.setSpreadingFactor(sf);
radio.setBandwidth(bw);
radio.setCodingRate(cr);
}
void radio_set_tx_power(uint8_t dbm) {
radio.setOutputPower(dbm);
}
mesh::LocalIdentity radio_new_identity() {
RadioNoiseListener rng(radio);
return mesh::LocalIdentity(&rng); // create new random identity
}

30
variants/rak3401/target.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#define RADIOLIB_STATIC_ONLY 1
#include <RadioLib.h>
#include <helpers/radiolib/RadioLibWrappers.h>
#include <RAK3401Board.h>
#include <helpers/radiolib/CustomSX1262Wrapper.h>
#include <helpers/AutoDiscoverRTCClock.h>
#include <helpers/sensors/EnvironmentSensorManager.h>
#ifdef DISPLAY_CLASS
#include <helpers/ui/SSD1306Display.h>
extern DISPLAY_CLASS display;
#include <helpers/ui/MomentaryButton.h>
extern MomentaryButton user_btn;
#if defined(PIN_USER_BTN_ANA)
extern MomentaryButton analog_btn;
#endif
#endif
extern RAK3401Board board;
extern WRAPPER_CLASS radio_driver;
extern AutoDiscoverRTCClock rtc_clock;
extern EnvironmentSensorManager sensors;
bool radio_init();
uint32_t radio_get_rng_seed();
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr);
void radio_set_tx_power(uint8_t dbm);
mesh::LocalIdentity radio_new_identity();

View File

@@ -0,0 +1,52 @@
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "variant.h"
#include "wiring_constants.h"
#include "wiring_digital.h"
#include "nrf.h"
const uint32_t g_ADigitalPinMap[] =
{
// P0
0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ,
8 , 9 , 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
// P1
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47
};
void initVariant()
{
// LED1 & LED2
pinMode(PIN_LED1, OUTPUT);
ledOff(PIN_LED1);
pinMode(PIN_LED2, OUTPUT);
ledOff(PIN_LED2);
// 3V3 Power Rail
pinMode(PIN_3V3_EN, OUTPUT);
digitalWrite(PIN_3V3_EN, HIGH);
}

199
variants/rak3401/variant.h Normal file
View File

@@ -0,0 +1,199 @@
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _VARIANT_RAK3401_
#define _VARIANT_RAK3401_
#define RAK4630
/** Master clock frequency */
#define VARIANT_MCK (64000000ul)
#define USE_LFXO // Board uses 32khz crystal for LF
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
#include "WVariant.h"
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
// Number of pins defined in PinDescription array
#define PINS_COUNT (48)
#define NUM_DIGITAL_PINS (48)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
#define PIN_LED1 (35)
#define PIN_LED2 (36)
#define LED_BUILTIN PIN_LED1
#define LED_CONN PIN_LED2
#define LED_GREEN PIN_LED1
#define LED_BLUE PIN_LED2
#define LED_STATE_ON 1 // State when LED is litted
/*
* Analog pins
*/
#define PIN_A0 (5) //(3)
#define PIN_A1 (31) //(4)
#define PIN_A2 (28)
#define PIN_A3 (29)
#define PIN_A4 (30)
#define PIN_A5 (31)
#define PIN_A6 (0xff)
#define PIN_A7 (0xff)
static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2;
static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5;
static const uint8_t A6 = PIN_A6;
static const uint8_t A7 = PIN_A7;
#define ADC_RESOLUTION 14
// Other pins
#define WB_I2C1_SDA (13) // SENSOR_SLOT IO_SLOT
#define WB_I2C1_SCL (14) // SENSOR_SLOT IO_SLOT
#define PIN_AREF (2)
#define PIN_NFC1 (9)
#define WB_IO5 PIN_NFC1
#define WB_IO4 (4)
#define PIN_NFC2 (10)
static const uint8_t AREF = PIN_AREF;
/*
* Serial interfaces
*/
// TXD1 RXD1 on Base Board
#define PIN_SERIAL1_RX (15)
#define PIN_SERIAL1_TX (16)
// Connected to Jlink CDC
#define PIN_SERIAL2_RX (8)
#define PIN_SERIAL2_TX (6)
/*
* SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 2
#define PIN_SPI_MISO (45)
#define PIN_SPI_MOSI (44)
#define PIN_SPI_SCK (43)
#define PIN_SPI1_MISO (29)
#define PIN_SPI1_MOSI (30)
#define PIN_SPI1_SCK (3)
static const uint8_t SS = 42;
static const uint8_t MOSI = PIN_SPI_MOSI;
static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK = PIN_SPI_SCK;
/*
* Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (WB_I2C1_SDA)
#define PIN_WIRE_SCL (WB_I2C1_SCL)
// QSPI Pins
// QSPI occupied by GPIO's
#define PIN_QSPI_SCK 3
#define PIN_QSPI_CS 26
#define PIN_QSPI_IO0 30
#define PIN_QSPI_IO1 29
#define PIN_QSPI_IO2 28
#define PIN_QSPI_IO3 2
// On-board QSPI Flash
// No onboard flash
#define EXTERNAL_FLASH_DEVICES IS25LP080D
#define EXTERNAL_FLASH_USE_QSPI
#define P_LORA_SCK PIN_SPI1_SCK
#define P_LORA_MISO PIN_SPI1_MISO
#define P_LORA_MOSI PIN_SPI1_MOSI
#define P_LORA_CS 26
#define USE_SX1262
#define SX126X_CS (26)
#define SX126X_DIO1 (10)
#define SX126X_BUSY (9)
#define SX126X_RESET (4)
#define SX126X_POWER_EN (21)
// DIO2 controlls an antenna switch and the TCXO voltage is controlled by DIO3
#define SX126X_DIO2_AS_RF_SWITCH
#define SX126X_DIO3_TCXO_VOLTAGE 1.8
// enables 3.3V periphery like GPS or IO Module
// Do not toggle this for GPS power savings
#define PIN_3V3_EN (34)
#define WB_IO2 PIN_3V3_EN
// RAK1910 GPS module
// If using the wisblock GPS module and pluged into Port A on WisBlock base
// IO1 is hooked to PPS (pin 12 on header) = gpio 17
// IO2 is hooked to GPS RESET = gpio 34, but it can not be used to this because IO2 is ALSO used to control 3V3_S power (1 is on).
// Therefore must be 1 to keep peripherals powered
// Power is on the controllable 3V3_S rail
#define PIN_GPS_PPS (17) // Pulse per second input from the GPS
#define PIN_GPS_RX PIN_SERIAL1_RX
#define PIN_GPS_TX PIN_SERIAL1_TX
// Battery
// The battery sense is hooked to pin A0 (5)
#define BATTERY_PIN PIN_A0
// and has 12 bit resolution
#define BATTERY_SENSE_RESOLUTION_BITS 12
#define BATTERY_SENSE_RESOLUTION 4096.0
#undef AREF_VOLTAGE
#define AREF_VOLTAGE 3.0
#define VBAT_AR_INTERNAL AR_INTERNAL_3_0
#define ADC_MULTIPLIER 1.73
#define HAS_RTC 1
#define RAK_4631 1
#ifdef __cplusplus
}
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
#endif

View File

@@ -3,6 +3,28 @@
#include "RAK4631Board.h" #include "RAK4631Board.h"
#ifdef NRF52_POWER_MANAGEMENT
// Static configuration for power management
// Values set in variant.h defines
const PowerMgtConfig power_config = {
.lpcomp_ain_channel = PWRMGT_LPCOMP_AIN,
.lpcomp_refsel = PWRMGT_LPCOMP_REFSEL,
.voltage_bootlock = PWRMGT_VOLTAGE_BOOTLOCK
};
void RAK4631Board::initiateShutdown(uint8_t reason) {
// Disable LoRa module power before shutdown
digitalWrite(SX126X_POWER_EN, LOW);
if (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
reason == SHUTDOWN_REASON_BOOT_PROTECT) {
configureVoltageWake(power_config.lpcomp_ain_channel, power_config.lpcomp_refsel);
}
enterSystemOff(reason);
}
#endif // NRF52_POWER_MANAGEMENT
void RAK4631Board::begin() { void RAK4631Board::begin() {
NRF52BoardDCDC::begin(); NRF52BoardDCDC::begin();
pinMode(PIN_VBAT_READ, INPUT); pinMode(PIN_VBAT_READ, INPUT);
@@ -21,6 +43,11 @@ void RAK4631Board::begin() {
Wire.begin(); Wire.begin();
pinMode(SX126X_POWER_EN, OUTPUT); pinMode(SX126X_POWER_EN, OUTPUT);
#ifdef NRF52_POWER_MANAGEMENT
// Boot voltage protection check (may not return if voltage too low)
// We need to call this after we configure SX126X_POWER_EN as output but before we pull high
checkBootVoltage(&power_config);
#endif
digitalWrite(SX126X_POWER_EN, HIGH); digitalWrite(SX126X_POWER_EN, HIGH);
delay(10); // give sx1262 some time to power up delay(10); // give sx1262 some time to power up
} }

View File

@@ -30,6 +30,11 @@
#define ADC_MULTIPLIER (3 * 1.73 * 1.187 * 1000) #define ADC_MULTIPLIER (3 * 1.73 * 1.187 * 1000)
class RAK4631Board : public NRF52BoardDCDC, public NRF52BoardOTA { class RAK4631Board : public NRF52BoardDCDC, public NRF52BoardOTA {
protected:
#ifdef NRF52_POWER_MANAGEMENT
void initiateShutdown(uint8_t reason) override;
#endif
public: public:
RAK4631Board() : NRF52BoardOTA("RAK4631_OTA") {} RAK4631Board() : NRF52BoardOTA("RAK4631_OTA") {}
void begin(); void begin();

View File

@@ -7,6 +7,7 @@ build_flags = ${nrf52_base.build_flags}
-I variants/rak4631 -I variants/rak4631
-D RAK_4631 -D RAK_4631
-D RAK_BOARD -D RAK_BOARD
-D NRF52_POWER_MANAGEMENT
-D PIN_BOARD_SCL=14 -D PIN_BOARD_SCL=14
-D PIN_BOARD_SDA=13 -D PIN_BOARD_SDA=13
-D PIN_GPS_TX=PIN_SERIAL1_RX -D PIN_GPS_TX=PIN_SERIAL1_RX

View File

@@ -104,6 +104,14 @@ extern "C"
static const uint8_t A7 = PIN_A7; static const uint8_t A7 = PIN_A7;
#define ADC_RESOLUTION 14 #define ADC_RESOLUTION 14
// Power management boot protection threshold (millivolts)
// Set to 0 to disable boot protection
#define PWRMGT_VOLTAGE_BOOTLOCK 3300 // Won't boot below this voltage (mV)
// LPCOMP wake configuration (voltage recovery from SYSTEMOFF)
// AIN3 = P0.05 = PIN_A0 / PIN_VBAT_READ
#define PWRMGT_LPCOMP_AIN 3
#define PWRMGT_LPCOMP_REFSEL 4 // 5/8 VDD (~3.13-3.44V)
// Other pins // Other pins
#define PIN_AREF (2) #define PIN_AREF (2)
#define PIN_NFC1 (9) #define PIN_NFC1 (9)

View File

@@ -16,7 +16,8 @@ build_flags =
-D P_LORA_SCLK=12 -D P_LORA_SCLK=12
-D P_LORA_MISO=14 -D P_LORA_MISO=14
-D P_LORA_MOSI=13 -D P_LORA_MOSI=13
-D LORA_TX_POWER=19 -D LORA_TX_POWER=7 ; configured as 7dbm, because the final output will be ~27dbm (~0.5w) if the PA is enabled.
-D MAX_LORA_TX_POWER=19 ; max output without burning out the PA
; -D P_LORA_TX_LED=35 ; -D P_LORA_TX_LED=35
-D PIN_BOARD_SDA=5 -D PIN_BOARD_SDA=5
-D PIN_BOARD_SCL=6 -D PIN_BOARD_SCL=6

View File

@@ -107,6 +107,7 @@ build_flags = ${t1000-e.build_flags}
-D DISPLAY_CLASS=NullDisplayDriver -D DISPLAY_CLASS=NullDisplayDriver
-D PIN_BUZZER=25 -D PIN_BUZZER=25
-D PIN_BUZZER_EN=37 ; P1/5 - required for T1000-E -D PIN_BUZZER_EN=37 ; P1/5 - required for T1000-E
-D ADVERT_NAME='"@@MAC"'
build_src_filter = ${t1000-e.build_src_filter} build_src_filter = ${t1000-e.build_src_filter}
+<helpers/nrf52/SerialBLEInterface.cpp> +<helpers/nrf52/SerialBLEInterface.cpp>
+<helpers/ui/buzzer.cpp> +<helpers/ui/buzzer.cpp>

View File

@@ -83,6 +83,7 @@ build_flags =
-D PIN_BUZZER=6 -D PIN_BUZZER=6
-D AUTO_SHUTDOWN_MILLIVOLTS=3300 -D AUTO_SHUTDOWN_MILLIVOLTS=3300
-D QSPIFLASH=1 -D QSPIFLASH=1
-D ENV_INCLUDE_GPS=1
; -D MESH_PACKET_LOGGING=1 ; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1 ; -D MESH_DEBUG=1
build_src_filter = ${ThinkNode_M1.build_src_filter} build_src_filter = ${ThinkNode_M1.build_src_filter}

View File

@@ -11,7 +11,7 @@ WRAPPER_CLASS radio_driver(radio, board);
VolatileRTCClock fallback_clock; VolatileRTCClock fallback_clock;
AutoDiscoverRTCClock rtc_clock(fallback_clock); AutoDiscoverRTCClock rtc_clock(fallback_clock);
MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1); MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock);
ThinkNodeM1SensorManager sensors = ThinkNodeM1SensorManager(nmea); ThinkNodeM1SensorManager sensors = ThinkNodeM1SensorManager(nmea);
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS

View File

@@ -22,6 +22,7 @@ class ThinkNodeM1SensorManager : public SensorManager {
void stop_gps(); void stop_gps();
public: public:
ThinkNodeM1SensorManager(LocationProvider &location): _location(&location) { } ThinkNodeM1SensorManager(LocationProvider &location): _location(&location) { }
LocationProvider* getLocationProvider() override { return _location; }
bool begin() override; bool begin() override;
bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override; bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override;
void loop() override; void loop() override;

View File

@@ -95,6 +95,7 @@ build_flags = ${WioTrackerL1.build_flags}
-D UI_HAS_JOYSTICK=1 -D UI_HAS_JOYSTICK=1
-D PIN_BUZZER=12 -D PIN_BUZZER=12
-D QSPIFLASH=1 -D QSPIFLASH=1
-D ADVERT_NAME='"@@MAC"'
; -D MESH_PACKET_LOGGING=1 ; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1 ; -D MESH_DEBUG=1
build_src_filter = ${WioTrackerL1.build_src_filter} build_src_filter = ${WioTrackerL1.build_src_filter}

View File

@@ -5,12 +5,40 @@
#include "XiaoNrf52Board.h" #include "XiaoNrf52Board.h"
#ifdef NRF52_POWER_MANAGEMENT
// Static configuration for power management
// Values set in variant.h defines
const PowerMgtConfig power_config = {
.lpcomp_ain_channel = PWRMGT_LPCOMP_AIN,
.lpcomp_refsel = PWRMGT_LPCOMP_REFSEL,
.voltage_bootlock = PWRMGT_VOLTAGE_BOOTLOCK
};
void XiaoNrf52Board::initiateShutdown(uint8_t reason) {
bool enable_lpcomp = (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
reason == SHUTDOWN_REASON_BOOT_PROTECT);
pinMode(VBAT_ENABLE, OUTPUT);
digitalWrite(VBAT_ENABLE, enable_lpcomp ? LOW : HIGH);
if (enable_lpcomp) {
configureVoltageWake(power_config.lpcomp_ain_channel, power_config.lpcomp_refsel);
}
enterSystemOff(reason);
}
#endif // NRF52_POWER_MANAGEMENT
void XiaoNrf52Board::begin() { void XiaoNrf52Board::begin() {
NRF52BoardDCDC::begin(); NRF52BoardDCDC::begin();
// Configure battery voltage ADC
pinMode(PIN_VBAT, INPUT); pinMode(PIN_VBAT, INPUT);
pinMode(VBAT_ENABLE, OUTPUT); pinMode(VBAT_ENABLE, OUTPUT);
digitalWrite(VBAT_ENABLE, HIGH); digitalWrite(VBAT_ENABLE, LOW); // Enable VBAT divider for reading
analogReadResolution(12);
analogReference(AR_INTERNAL_3_0);
delay(50); // Allow ADC to settle
#ifdef PIN_USER_BTN #ifdef PIN_USER_BTN
pinMode(PIN_USER_BTN, INPUT_PULLUP); pinMode(PIN_USER_BTN, INPUT_PULLUP);
@@ -27,9 +55,20 @@ void XiaoNrf52Board::begin() {
digitalWrite(P_LORA_TX_LED, HIGH); digitalWrite(P_LORA_TX_LED, HIGH);
#endif #endif
// pinMode(SX126X_POWER_EN, OUTPUT); #ifdef NRF52_POWER_MANAGEMENT
// digitalWrite(SX126X_POWER_EN, HIGH); // Boot voltage protection check (may not return if voltage too low)
delay(10); // give sx1262 some time to power up checkBootVoltage(&power_config);
#endif
delay(10); // Give sx1262 some time to power up
}
uint16_t XiaoNrf52Board::getBattMilliVolts() {
// https://wiki.seeedstudio.com/XIAO_BLE#q3-what-are-the-considerations-when-using-xiao-nrf52840-sense-for-battery-charging
// VBAT_ENABLE must be LOW to read battery voltage
digitalWrite(VBAT_ENABLE, LOW);
int adcvalue = analogRead(PIN_VBAT);
return (adcvalue * ADC_MULTIPLIER * AREF_VOLTAGE) / 4.096;
} }
#endif #endif

View File

@@ -7,6 +7,11 @@
#ifdef XIAO_NRF52 #ifdef XIAO_NRF52
class XiaoNrf52Board : public NRF52BoardDCDC, public NRF52BoardOTA { class XiaoNrf52Board : public NRF52BoardDCDC, public NRF52BoardOTA {
protected:
#if NRF52_POWER_MANAGEMENT
void initiateShutdown(uint8_t reason) override;
#endif
public: public:
XiaoNrf52Board() : NRF52BoardOTA("XIAO_NRF52_OTA") {} XiaoNrf52Board() : NRF52BoardOTA("XIAO_NRF52_OTA") {}
void begin(); void begin();
@@ -20,21 +25,7 @@ public:
} }
#endif #endif
uint16_t getBattMilliVolts() override { uint16_t getBattMilliVolts() override;
// Please read befor going further ;)
// https://wiki.seeedstudio.com/XIAO_BLE#q3-what-are-the-considerations-when-using-xiao-nrf52840-sense-for-battery-charging
// We can't drive VBAT_ENABLE to HIGH as long
// as we don't know wether we are charging or not ...
// this is a 3mA loss (4/1500)
digitalWrite(VBAT_ENABLE, LOW);
int adcvalue = 0;
analogReadResolution(12);
analogReference(AR_INTERNAL_3_0);
delay(10);
adcvalue = analogRead(PIN_VBAT);
return (adcvalue * ADC_MULTIPLIER * AREF_VOLTAGE) / 4.096;
}
const char* getManufacturerName() const override { const char* getManufacturerName() const override {
return "Seeed Xiao-nrf52"; return "Seeed Xiao-nrf52";

View File

@@ -9,6 +9,7 @@ build_flags = ${nrf52_base.build_flags}
-I variants/xiao_nrf52 -I variants/xiao_nrf52
-UENV_INCLUDE_GPS -UENV_INCLUDE_GPS
-D NRF52_PLATFORM -D NRF52_PLATFORM
-D NRF52_POWER_MANAGEMENT
-D XIAO_NRF52 -D XIAO_NRF52
-D RADIO_CLASS=CustomSX1262 -D RADIO_CLASS=CustomSX1262
-D WRAPPER_CLASS=CustomSX1262Wrapper -D WRAPPER_CLASS=CustomSX1262Wrapper

View File

@@ -75,6 +75,21 @@ static const uint8_t D10 = 10;
#define AREF_VOLTAGE (3.0) #define AREF_VOLTAGE (3.0)
#define ADC_MULTIPLIER (3.0F) // 1M, 512k divider bridge #define ADC_MULTIPLIER (3.0F) // 1M, 512k divider bridge
// Power management boot protection threshold (millivolts)
// Set to 0 to disable boot protection
#define PWRMGT_VOLTAGE_BOOTLOCK 3300 // Won't boot below this voltage
// LPCOMP wake configuration (voltage recovery from SYSTEMOFF)
#define PWRMGT_LPCOMP_AIN 7 // AIN7 = P0.31 = PIN_VBAT
// IMPORTANT: The XIAO exposes battery via a resistor divider (ADC_MULTIPLIER = 3.0).
// LPCOMP measures the divided voltage, not the battery voltage directly.
// Vpin = VDD * (REFSEL fraction), and VBAT ≈ Vpin * ADC_MULTIPLIER.
//
// Using 3/8 VDD gives a wake threshold above the boot protection point:
// - If VDD ≈ 3.0V: VBAT ≈ (3.0 * 3/8) * 3 ≈ 3375mV
// - If VDD ≈ 3.3V: VBAT ≈ (3.3 * 3/8) * 3 ≈ 3712mV
#define PWRMGT_LPCOMP_REFSEL 2 // 3/8 VDD (~3.38-3.71V)
static const uint8_t A0 = PIN_A0; static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1; static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2; static const uint8_t A2 = PIN_A2;