Repository navigation
RFM95Socket: a LoRa transport, and the pin map that makes it wire up - #16
Conversation
Adds RFM95Socket implementing the abstract Socket interface over
sandeepmistry/arduino-LoRa, for the SparkFun ESP32 LoRa 1-CH Gateway.
Library only -- no WLED or HMTL_Module integration.
The substantive work turned out to be the pin map, which was not
recorded anywhere: platformio.ini and the HMTL config JSON both list the
reserved SET (GPIO 12/13/14/16/26/32/33) and neither says which pin
plays which role. Derived here from the schematic NETLIST
(sparkfunX/ESP32_LoRa_1CH_Gateway, esp32_lora_gateway.sch -- EAGLE 9
schematics are XML) rather than from a reading of the PDF:
SCK 14, MISO 12, MOSI 13, CS 16, DIO0 26, DIO1 33, DIO2 32.
That accounts for all seven reserved pins and nothing else, so the
reserved list needed no correction. Two findings follow from it, and
both produce the identical symptom -- initialized() false -- which is
why they are documented rather than left for a bench session:
* RESET IS NOT WIRED TO THE ESP32. Net RFM_RST reaches U2.!RESET and
R5.1 and stops; R5.2 is on 3.3V, so the line is a 10k pull-up and
nothing more. Pass -1; LoRa.cpp guards its reset sequence on
_reset != -1. SparkFun's own sketches pass 5 (Firmware/) or 27
(Production/) -- two values in one repo, neither connected to
anything, both broken out to headers where driving them is someone
else's pin.
* SPI MUST BE BOUND EXPLICITLY. LoRa.begin() calls _spi->begin() on
the global SPI, which under board = esp32dev is VSPI
(18/19/23/5). None of those reach the radio, and GPIO 23 is
PIXELS_CLOCK in [env:esp32_lora_gw] -- so the default configuration
fails to talk to the radio AND drives the pixel clock. setup()
calls SPI.begin(14, 12, 13, 16) first. SparkFun's examples never hit
this because their board variant redefines the SPI defaults; we do
not build with that variant.
LoRa has no node addressing, so unlike RFM69Socket there is no driver
destination and no driver broadcast address: rfm95_socket_hdr_t plus the
software filter in getMsg() is the entire addressing mechanism, and
SOCKET_ADDR_ANY travels unconverted. The header is the same 6-byte field
set as rfm69_socket_hdr_t, packed with static_asserts on size and every
field offset -- prophylactic here rather than protecting deployed nodes,
since no AVR LoRa peer exists, and kept generic because LoRa<->RS485
bridging is planned and a bridge should not have to strip radio-specific
fields.
Two deliberate divergences from RFM69Socket, both noted in the source:
radio bring-up is in setup() rather than the constructor (starting SPI
from a global's constructor runs before the core is ready), and an
oversized initBuffer clamps and complains instead of calling
DEBUG_ERR_STATE(), which is a while(true) LED flash with no LED on the
other end of an ESP32.
Tests: 73 checks, built in two configurations from one source. The
second build is not symmetry -- RFM69Socket's runt-frame length check
sits inside `#if DEBUG_LEVEL == DEBUG_TRACE` and so is absent from the
build that ships. Moving this library's check into the same #if was
tried as a mutation: the trace binary stayed GREEN and only the quiet
binary caught it (3 failures). Also adds a cross-ABI layout target
compiling the header under the host ABI and -fpack-struct=1.
Every guard was mutation-tested rather than assumed: RESET=5, CS=5,
default SPI, runt check removed, address filter removed, short-read
check removed -- all caught, baseline green. The first attempt at that
sweep found a real defect in the new Makefile rules: the binaries did
not depend on RFM95Socket.h, so changing the pin-map macros left the
suite green against a stale binary. Fixed by adding RFM95_HEADERS.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Target build now verified — and it needed a workaround worth knowing aboutThe PR description listed "no Both envs build: Zero compiler diagnostics from any file under The green was proved, not trustedA
The
|
aphelps-reviewer-bot
left a comment
There was a problem hiding this comment.
Reviewed against the schematic and the pinned toolchain rather than the diff alone. One verification, two cross-file gaps.
Verified, not disputed — the RESET = -1 claim holds. I re-derived it independently from the EAGLE XML netlist (Hardware/esp32_lora_gateway.sch), walking every <net>/<pinref>:
RFM_RST -> ['U2.!RESET', 'R5.1']
3.3V -> [... 'U1.3V3', 'U2.3.3V', 'R5.2', 'J4.7' ...]
SCK -> ['U1.SCK_H/IO14', 'U2.SCK', 'J4.3']
MISO -> ['U1.MISO_H/IO12', 'U2.MISO', 'J4.2']
MOSI -> ['U1.MOSI_H/IO13', 'U2.MOSI', 'J4.1']
RFM_CS -> ['U1.IO16', 'U2.NSS']
RFM_INT/26 -> ['U1.IO26', 'U2.DIO0']
RFM_DIO1/33 -> ['U1.XTAL_N/IO33', 'U2.DIO1']
RFM_DIO2/32 -> ['U1.XTAL_P/IO32', 'U2.DIO2']
No U1.* pinref appears anywhere on RFM_RST, and R5.2 is on the 3.3V net. The complete set of nets touching U2 is exactly the nine above plus ANT/GND, so there is no second reset path either. LoRa.cpp:113 does guard on _reset != -1. Nothing to change here — recording it so the claim is checked rather than inherited.
DISABLE_LORA is still consumed by no source, so the HMTL half of this change is unverifiable. grep -rn DISABLE_LORA over ArduinoLibs/ and HMTL/ returns hits only in the .ini. Consequences: (a) the plan's headline Test Plan item — "put a deliberate #error inside the #ifndef DISABLE_LORA branch and confirm pio run -e esp32_lora_gw turns red" — has no branch to put it in and cannot be run as written; (b) that env compiles byte-identical code before and after the OPTION_FLAGS / LORA_BUILDFLAGS split, so a green build there is exactly the reassuring-but-uninformative diagnostic the new comment warns about; (c) the AVR envs are actually being protected by lib_ignore / esp_only_libs, a second and independent mechanism — two mechanisms for one job, one of them inert. I did check the one env that does not interpolate GLOBAL_BUILDFLAGS ([env:simavr_nano], build_flags = %(GLOBAL_COMPILEFLAGS)s %(OPTION_FLAGS)s); it re-lists -DDISABLE_LORA explicitly at :275, so the "every other env byte-identical" claim does survive.
Suggested post-merge check: re-run the #error-inside-#ifndef DISABLE_LORA mutation once a source guard actually exists, and until then mark the first two Test Plan items not-yet-runnable rather than leaving them implying otherwise.
[env:esp32_lora_gw] declares no lib_deps. HMTL/platformio/HMTL_Module/platformio.ini has no lib_deps key at all, so LoRa resolves purely from the machine-local lib_dir. sandeepmistry/[email protected] is pinned only in platformio/RFM95Socket/RFM95_SocketTest/platformio.ini. So the subtask "add arduino-LoRa as the driver dependency, pinned rather than floating" is satisfied for the standalone example project and not for the env the Test Plan builds. Adding LoRa to esp_only_libs is a lib_ignore entry, not a declaration.
The remaining findings are inline: a reachable transmit-side length overflow (with a reproduction), and two places where a test asserts less than its comment claims.
Found in post-PR self-review of #16, reproduced before fixing. RFM95_BUFFER_TOTAL() casts to uint8_t and Socket::sendMsgTo takes a `byte` length, so 250..255 are all expressible and all wrap: sendMsgTo(..., 250) -> total 0 -> an EMPTY frame transmitted sendMsgTo(..., 255) -> total 5 -> a 5-byte RUNT frame transmitted A runt is precisely what getMsg()'s unconditional length check exists to reject, so the transmit path was manufacturing the frames the receive path defends against: a peer running this same code drops them as "runt frame" while the sender reports success and the payload is gone. RFM69Socket cannot reach this -- RF69_MAX_DATA_LEN is 61 and the sum never approaches 256. It is reachable here only because RFM95_MAX_PACKET is exactly 255. Dropped rather than clamped: a clamped send would put a well-formed frame on the air with the tail of the caller's message missing, which the receiver has no way to detect. It survived the pre-PR mutation sweep because the "maximum-size frame" tests only covered RECEIVE. Six checks added for transmit, including the 249-byte boundary still working and a normal send succeeding straight after a dropped one. Mutation-verified: removing the guard fails exactly the two new oversized cases. Also fixes initBuffer() underflowing uint16_t for a buffer smaller than the header -- data_size 0 reported 65530 usable bytes and returned a pointer past the end of the caller's array. Two documentation corrections from the same review, both cases of a claim being stronger than the evidence: * rfm95-layout's -fpack-struct=1 half CANNOT catch a missing __packed__ attribute. Mutation: deleting the attribute fails 5 static_asserts under the default ABI and 0 under -fpack-struct=1, because that flag packs every struct in the TU and so reconstructs by flag what the attribute was supposed to guarantee. The default-ABI compile is what pins the packing. Comment rewritten to say which half does the work rather than implying both do. * SPIClass::begin() on ESP32 opens with `if (_spi) return;`, so the explicit SPI.begin(14,12,13,16) in setup() is silently discarded if anything brought the global SPI up first -- and shim/SPI.h records pins on every call, so the host suite stays green in exactly that case. Recorded as a known limit rather than papered over; it matters for the planned WLED integration, where bus_manager is a second SPI consumer. Suite: 73 -> 79 checks, both configurations green. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Left one paragraph still claiming the AVR-proxy compile is what keeps the packing checked, which the Tests section directly below had just been corrected to contradict. The host-ABI static_asserts are what pin it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Self-review found a shipping bug. Fixed in
|
| result | |
|---|---|
| default ABI | 5 static_asserts fail — caught |
-fpack-struct=1 |
0 failures — not caught |
Because -fpack-struct=1 packs every struct in the translation unit, it reconstructs by flag precisely what the attribute was supposed to guarantee, and cannot notice the attribute is gone. The default-ABI compile is what pins the packing; the AVR-proxy leg only confirms the asserted offsets are also what AVR produces.
Kept, but the Makefile comment and the README now say which half does the work instead of implying both do. The README had a second paragraph still making the old claim — f8a03ff.
3. SPI.begin() is a no-op if the bus is already up — recorded, not fixed
SPIClass::begin() opens with if (_spi) return;, so the explicit SPI.begin(14,12,13,16) is silently discarded if anything brought the global SPI up first — landing you in the exact failure this library exists to prevent, with the pin-map assertions still green because shim/SPI.h records pins on every call (more forgiving than the hardware).
Not fixed here: the real fix is owning an SPIClass instance and handing it to LoRa.setSPI() rather than sharing the global, and that wants a second SPI consumer to test against. Documented as a known limit in the source and README, and flagged for the WLED integration, where bus_manager is that second consumer.
Also fixed: initBuffer() underflowed uint16_t for a buffer smaller than the header — data_size 0 reported 65530 usable bytes and returned a pointer past the end of the caller's array.
Re-verified after the fixes
- 79 checks, both configurations green; full
make test STRICT=1at the super-repo green. - ESP32 target build still good:
esp32_lora_gw_aandesp32_lora_gw_bboth SUCCESS.
Left alone, deliberately
The review also noted that -DDISABLE_LORA is still consumed by no source, so the plan's "put an #error in the #ifndef DISABLE_LORA branch" test has no branch to go in, and that [env:esp32_lora_gw] has no lib_deps. Both are true and both are correct for this PR: the flag has no consumer until something in HMTL_Module includes LoRa.h, and adding lib_deps before then puts a network fetch into builds that don't need one. They belong with the integration work, and the #error proof was instead done directly against the standalone example project — where it does turn the build red.
Adds
RFM95Socketimplementing the abstractSocketinterface oversandeepmistry/arduino-LoRa, for the SparkFun ESP32
LoRa 1-CH Gateway. Library only — no WLED or HMTL_Module integration; that is deliberately later.
Pairs with aphelps/HMTL#15, which fixes the
-DDISABLE_LORAplumbing.The pin map was the real work
It was not recorded anywhere.
platformio.iniandHMTL_Fire_Control_ESP32_LoRaGW.jsonboth listthe reserved set (GPIO 12/13/14/16/26/32/33); neither says which pin plays which role. Derived
here from the schematic netlist (
sparkfunX/ESP32_LoRa_1CH_Gateway,esp32_lora_gateway.sch—EAGLE 9 schematics are XML, so the nets read directly) rather than from a reading of the PDF:
U1.SCK_H/IO14 <-> U2.SCKU1.MISO_H/IO12 <-> U2.MISOU1.MOSI_H/IO13 <-> U2.MOSIU1.IO16 <-> U2.NSSU1.IO26 <-> U2.DIO0U1.XTAL_N/IO33 <-> U2.DIO1U1.XTAL_P/IO32 <-> U2.DIO2That accounts for all seven reserved pins and nothing else, so the reserved list needed no
correction. Two findings follow, and both present identically as
initialized() == false—which is why they are documented rather than left for a bench session to rediscover.
RESET is not wired to the ESP32
Net
RFM_RSTreachesU2.!RESETandR5.1and stops;R5.2is on the3.3Vnet, so the line is a10 k pull-up and nothing else. Pass
-1—LoRa.cpp:113guards its reset sequence on_reset != -1.SparkFun's own sketches disagree with each other because the value is never used:
.rstsparkfun/…/Firmware/ESP-1CH-TTN-Device-ABP.inosparkfunX/…/Firmware/ESP-1CH-TTN-Device-ABP.inosparkfunX/…/Production/ESP32_Lora_Send_sketch.inosparkfunX/…/Production/Receiver/…_Receiver_sketch.inoTwo files in one repo, two answers, neither connected to anything — and both are pins broken out to
the headers, where driving them is someone else's problem.
SPI must be bound explicitly, and the default is actively harmful
LoRa.begin()calls_spi->begin()on the globalSPI, which underboard = esp32devis VSPI:SCK 18 / MISO 19 / MOSI 23 / SS 5. None reach the radio — and GPIO 23 is
PIXELS_CLOCKin[env:esp32_lora_gw], so the default configuration fails to talk to the radio and drives thepixel clock line.
setup()callsSPI.begin(14, 12, 13, 16)first.SparkFun's examples never hit this because their board variant (
sparkx_esp32_lora/pins_arduino.h)redefines
SS/MOSI/MISO/SCK. We do not build with that variant.Design notes
LoRa has no node addressing. Unlike
RFM69Socketthere is no driver destination and no driverbroadcast address, so
rfm95_socket_hdr_tplus the software filter ingetMsg()is the entiremechanism, and
SOCKET_ADDR_ANYtravels unconverted. Remove that filter and every node acts onevery packet.
Unencrypted, deliberately. The RFM69 has AES in hardware and
RFM69Socketuses it; the SX1276has no equivalent and no software cipher is applied. Stated plainly in the README so the gap reads as
a decision.
Header is the same 6-byte field set as
rfm69_socket_hdr_t, packed withstatic_asserts on sizeand every field offset. Prophylactic rather than protecting deployed nodes — no AVR LoRa peer exists
— and kept generic because LoRa↔RS485/HMTL bridging is planned and a bridge should not have to strip
radio-specific fields. RSSI/SNR are an API, not wire fields.
Two deliberate divergences from
RFM69Socket, both explained in the source: radio bring-up moved tosetup()(starting SPI from a global's constructor runs before the core is ready), and an oversizedinitBufferclamps and complains rather than callingDEBUG_ERR_STATE(), which is awhile (true)LED flash with no LED on the other end of an ESP32.
Tests
73 checks, built in two configurations from one source, plus a cross-ABI layout target.
The second build is not symmetry.
RFM69Socket's runt-frame length check sits inside#if DEBUG_LEVEL == DEBUG_TRACEand so is absent from the build that ships. Moving thislibrary's check into the same
#ifwas tried as a mutation: the trace binary stayed green, andonly the quiet binary caught it (3 failures).
Every guard was mutation-tested rather than assumed:
RESET = 5CS = 5#ifThe first attempt at that sweep found a real defect in the new Makefile rules: the binaries did
not depend on
RFM95Socket.h, so changing the pin-map macros left the suite green against a stalebinary.
M1came back 0 failures and only a clean rebuild revealed it. Fixed by addingRFM95_HEADERSas a prerequisite.make test STRICT=1at the super-repo namesrfm95_socket_testandrfm95_socket_test_quiet;verified, not assumed.
Not done, on purpose
## Testing Requiredin the plan covers thetwo-node bench test, and
examples/RFM95_SocketTest/exists to be that test — it counts packetsaccepted and packets rejected by the address filter separately, because only the second number
says addressing works.
Socketso every transport gets them.getMsg()polls. DIO0 is wired if that changes.🤖 Generated with Claude Code