Tutorial: Wireless C HAL, Hardware Overlays & Zero-Copy asyncio
Phase 4 introduces a unified, multi-radio Pure-C Wireless HAL (drivers/wireless/* + cmodules/hal/mod_hal_wireless.c) paired with the MicroPython pylotos.wireless SDK module.
1. Core Architectural Guarantees
- Zero Python IRQ Callbacks (
0 BISR Heap Allocation): Hardware interrupts (SX1262 DIO1,RMT RX,NimBLE,ESP-NOW) push frames into static C SPSC ringbuffers (hal_wireless_ring.h), setMP_STREAM_POLL_RD, and callmp_hal_wake_main_task_from_isr(). The MicroPython GC is never touched from an ISR or Core 0 FreeRTOS task. - Zero-Copy Buffer Protocol (
recv_into): Packet receivers write directly into your pre-allocatedbytearrayormemoryview, avoiding per-packet heap fragmentation on 0-PSRAM boards (m5stack_nanoc6,heltec_wifi_lora_32_v3). - Shared SPI/I2C Bus Mutexing: On boards sharing
SPI2acrossST7789LCD + microSD +SX1262LoRa (such as LilyGO T-Deck),DIO1ISRs never touch SPI directly — FIFO reads acquirehal_spi_bus_lock()cleanly. - NimBLE-Only Bluetooth: Bluetooth Classic (BR/EDR) is excluded by design so all 30 boards (including 4 MiB RISC-V
ESP32-C3andESP32-C6) maintain generous flash and SRAM headroom.
2. Sub-GHz / LoRa (hal.rf + EU868 1% Duty-Cycle + Replay Guard)
Works out of the box on heltec_wifi_lora_32_v3 and lilygo_t_deck, or via expansion-port attach() / overlays on m5stack_cardputer_adv:
import hal
from pylotos import wireless
KEY = b"16-byte-fleet-k!"
rx_buf = bytearray(256)
meta = {}
hal.rf.open()
hal.rf.set_region("EU868") # Enforces 863..870 MHz, <= +14 dBm, and 1% (36 s/hr) duty cycle!
hal.rf.set_freq(868_100_000)
hal.rf.set_power(14)
hal.rf.set_param("sf", 7) # Hybrid KV params: "sf", "bw_hz", "cr", "sync_word", "crc"
hal.rf.start_rx(0) # Continuous RX into static C ringbuffer
# Inspect remaining sliding-window airtime budget:
print(hal.rf.duty_info()) # {'region': 'EU868', 'max_dbm': 14, 'used_ms': 0, 'budget_ms': 36000}
# Transmit an authenticated, replay-protected frame:
hal.rf.send(wireless.pack_frame(KEY, seq=1, payload=b"temp=22.4"))
# Cooperatively await next packet with zero heap copies:
n = await wireless.recv_rf_into(rx_buf, meta=meta, timeout_ms=5000)
if n > 0:
seq, data = wireless.unpack_frame(KEY, memoryview(rx_buf)[:n], min_seq=1)
print("RX #", seq, data, "RSSI:", meta["rssi"], "SNR:", meta["snr"])
3. Pin-Guarded Runtime attach() & Hardware Overlays (pylotos.overlays)
Every board declares its free expansion_ports (grove_a, ext_cap, etc.) in board.json, while hal_pin_guard.h locks system Flash/PSRAM, USB D+/D-, LCD, keyboard, and POWER_HOLD pins. Attempting to attach a module to a reserved system pin raises OSError(errno.EBUSY) immediately.
Zero-Code Overlay Activation (e.g. Cardputer ADV Cap LoRa-1262)
The M5Stack Cap LoRa-1262 on m5stack_cardputer_adv requires toggling the PI4IOE5V6408 @ 0x43 I2C expander pin P0 RF switch before enabling the SX1262 PA. The built-in overlay configures both the ext_cap SPI pins and the rf_switch_i2c guard automatically:
from pylotos import overlays
# Apply for current session (or pass persist=True to auto-attach on boot):
overlays.apply_overlay("cardputer_adv_cap_lora1262", persist=True)
Or from the pylot> USB/SSH console:
pylot> overlay list
pylot> overlay apply cardputer_adv_cap_lora1262 --persist
pylot> wireless status
Built-in overlays include:
cardputer_adv_cap_lora1262(SX1262+PI4IOE5V6408 @ 0x43 P0RF switch)grove_nfc_ws1850s(M5Stack RFID2 Unit@ 0x28)grove_nfc_pn532(Grove PN532@ 0x24)grove_gnss_uart(Grove UART GPS/GNSS9600baud)
You can also drop custom JSON descriptors into /apps/.overlays/<my_hat>.json.
4. ESP-NOW v1/v2 (hal.espnow), Wi-Fi 6 iTWT (hal.net) & BLE Coded PHY (hal.ble)
import hal
from pylotos import wireless
# 2.4 GHz coexistence & ESP32-C6 Wi-Fi 6 Target Wake Time (iTWT):
hal.net.coex("balance") # "wifi" | "ble" | "balance"
hal.net.twt(True, wake_interval_ms=2000, min_wake_ms=16)
# NimBLE 5.0 / 5.3 Long-Range Coded PHY (S=8) on ESP32-S3 / C3 / C6:
hal.ble.coded_phy(True, s8_long_range=True)
# Connectionless zero-copy ESP-NOW P2P:
PEER = b"\xff\xff\xff\xff\xff\xff"
hal.espnow.open()
hal.espnow.add_peer(PEER, channel=0)
hal.espnow.send(PEER, b"hello-peer")
buf = bytearray(250)
meta = {}
n = await wireless.recv_espnow_into(buf, meta=meta, timeout_ms=1000)
5. 13.56 MHz NFC (hal.nfc + NDEF), RMT IR (hal.ir), GNSS & 802.15.4
import hal
from pylotos import wireless
# 1. NFC / RFID (`m5stack_dial` onboard WS1850S @ 0x28, or MFRC522 / PN532):
hal.nfc.open()
uid = bytearray(10)
ulen = await wireless.poll_nfc_into(uid, timeout_ms=3000)
if ulen > 0:
blk = bytearray(16)
hal.nfc.read_block(4, blk)
print(wireless.decode_ndef_record(blk)) # {'type': 'uri', 'value': 'https://sdk.pylotos.org'}
# 2. Hardware ESP-IDF RMT v2 Infrared (38 kHz carrier, 0% CPU bit-banging):
hal.ir.open(carrier_hz=38000)
hal.ir.tx_nec(0x04, 0x08)
# 3. Streaming C NMEA-0183 / UBX-NAV-PVT GNSS Fix Parser (fixed-point, 0 string GC):
hal.gnss.open(uart_port=1, baudrate=9600)
fix = hal.gnss.poll() # {'valid': True, 'sats': 8, 'lat_e7': ..., 'lon_e7': ...}
# 4. ESP32-C6 Raw IEEE 802.15.4 MAC (built with `--wireless-profile 802154`):
hal.ieee802154.open(channel=15, pan_id=0x1234, short_addr=0x0001)
hal.ieee802154.send(b"\x61\x88\x01\x34\x12\xff\xff\x01\x00hello")