2023-01-14 00:11:02 +01:00
|
|
|
// Copyright (C) 2023, Mark Qvist
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2018-12-01 19:31:51 +01:00
|
|
|
#include <Arduino.h>
|
2018-04-05 18:10:42 +02:00
|
|
|
#include <SPI.h>
|
2019-01-01 15:52:48 +01:00
|
|
|
#include "Utilities.h"
|
2018-04-05 18:10:42 +02:00
|
|
|
|
2020-05-28 22:18:19 +02:00
|
|
|
FIFOBuffer serialFIFO;
|
2020-06-01 12:43:59 +02:00
|
|
|
uint8_t serialBuffer[CONFIG_UART_BUFFER_SIZE+1];
|
2020-05-28 22:18:19 +02:00
|
|
|
|
|
|
|
FIFOBuffer16 packet_starts;
|
2022-01-09 23:45:40 +01:00
|
|
|
uint16_t packet_starts_buf[CONFIG_QUEUE_MAX_LENGTH+1];
|
2020-05-28 22:18:19 +02:00
|
|
|
|
|
|
|
FIFOBuffer16 packet_lengths;
|
2022-01-09 23:45:40 +01:00
|
|
|
uint16_t packet_lengths_buf[CONFIG_QUEUE_MAX_LENGTH+1];
|
2020-05-28 22:18:19 +02:00
|
|
|
|
|
|
|
uint8_t packet_queue[CONFIG_QUEUE_SIZE];
|
|
|
|
|
|
|
|
volatile uint8_t queue_height = 0;
|
2022-01-09 23:45:40 +01:00
|
|
|
volatile uint16_t queued_bytes = 0;
|
|
|
|
volatile uint16_t queue_cursor = 0;
|
|
|
|
volatile uint16_t current_packet_start = 0;
|
2020-05-29 14:58:10 +02:00
|
|
|
volatile bool serial_buffering = false;
|
2024-04-23 00:52:57 +02:00
|
|
|
#if HAS_BLUETOOTH || HAS_BLE == true
|
2022-10-30 14:52:22 +01:00
|
|
|
bool bt_init_ran = false;
|
|
|
|
#endif
|
2020-05-29 14:58:10 +02:00
|
|
|
|
2023-01-06 22:29:23 +01:00
|
|
|
#if HAS_CONSOLE
|
|
|
|
#include "Console.h"
|
|
|
|
#endif
|
|
|
|
|
2020-05-29 14:58:10 +02:00
|
|
|
char sbuf[128];
|
2020-05-28 22:18:19 +02:00
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2022-01-10 22:14:30 +01:00
|
|
|
bool packet_ready = false;
|
|
|
|
#endif
|
|
|
|
|
2018-04-05 18:10:42 +02:00
|
|
|
void setup() {
|
2022-01-09 23:40:30 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32
|
2022-11-02 19:02:22 +01:00
|
|
|
boot_seq();
|
2022-01-09 23:40:30 +01:00
|
|
|
EEPROM.begin(EEPROM_SIZE);
|
2022-01-11 02:54:32 +01:00
|
|
|
Serial.setRxBufferSize(CONFIG_UART_BUFFER_SIZE);
|
2022-01-09 23:40:30 +01:00
|
|
|
#endif
|
|
|
|
|
2024-01-21 16:12:20 +01:00
|
|
|
#if MCU_VARIANT == MCU_NRF52
|
|
|
|
if (!eeprom_begin()) {
|
|
|
|
Serial.write("EEPROM initialisation failed.\r\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-03-30 15:11:24 +01:00
|
|
|
// Seed the PRNG for CSMA R-value selection
|
|
|
|
# if MCU_VARIANT == MCU_ESP32
|
|
|
|
// On ESP32, get the seed value from the
|
|
|
|
// hardware RNG
|
|
|
|
int seed_val = (int)esp_random();
|
|
|
|
#else
|
|
|
|
// Otherwise, get a pseudo-random seed
|
|
|
|
// value from an unconnected analog pin
|
|
|
|
int seed_val = analogRead(0);
|
|
|
|
#endif
|
|
|
|
randomSeed(seed_val);
|
2018-04-05 18:10:42 +02:00
|
|
|
|
|
|
|
// Initialise serial communication
|
2020-05-28 22:18:19 +02:00
|
|
|
memset(serialBuffer, 0, sizeof(serialBuffer));
|
2020-06-01 12:43:59 +02:00
|
|
|
fifo_init(&serialFIFO, serialBuffer, CONFIG_UART_BUFFER_SIZE);
|
2020-05-28 22:18:19 +02:00
|
|
|
|
2018-04-05 18:10:42 +02:00
|
|
|
Serial.begin(serial_baudrate);
|
2024-05-18 01:07:38 +02:00
|
|
|
#if BOARD_MODEL != BOARD_RNODE_NG_22
|
|
|
|
while (!Serial);
|
|
|
|
#endif
|
2018-04-05 18:10:42 +02:00
|
|
|
|
2020-05-29 14:58:10 +02:00
|
|
|
serial_interrupt_init();
|
2020-05-28 22:18:19 +02:00
|
|
|
|
2018-04-05 18:10:42 +02:00
|
|
|
// Configure input and output pins
|
2024-05-02 01:20:33 +02:00
|
|
|
#if HAS_INPUT
|
|
|
|
input_init();
|
|
|
|
#endif
|
|
|
|
|
2022-12-04 20:00:21 +01:00
|
|
|
#if HAS_NP == false
|
|
|
|
pinMode(pin_led_rx, OUTPUT);
|
|
|
|
pinMode(pin_led_tx, OUTPUT);
|
|
|
|
#endif
|
2018-04-05 18:10:42 +02:00
|
|
|
|
2024-01-19 00:10:24 +01:00
|
|
|
#if HAS_TCXO == true
|
2024-01-21 15:23:28 +01:00
|
|
|
if (pin_tcxo_enable != -1) {
|
|
|
|
pinMode(pin_tcxo_enable, OUTPUT);
|
|
|
|
digitalWrite(pin_tcxo_enable, HIGH);
|
|
|
|
}
|
2024-01-19 00:10:24 +01:00
|
|
|
#endif
|
|
|
|
|
2018-06-27 10:22:44 +02:00
|
|
|
// Initialise buffers
|
2018-04-05 18:10:42 +02:00
|
|
|
memset(pbuf, 0, sizeof(pbuf));
|
2022-11-01 21:11:41 +01:00
|
|
|
memset(cmdbuf, 0, sizeof(cmdbuf));
|
2018-06-27 10:22:44 +02:00
|
|
|
|
2020-05-28 22:18:19 +02:00
|
|
|
memset(packet_queue, 0, sizeof(packet_queue));
|
|
|
|
|
2020-06-01 12:43:59 +02:00
|
|
|
memset(packet_starts_buf, 0, sizeof(packet_starts_buf));
|
|
|
|
fifo16_init(&packet_starts, packet_starts_buf, CONFIG_QUEUE_MAX_LENGTH);
|
|
|
|
|
|
|
|
memset(packet_lengths_buf, 0, sizeof(packet_starts_buf));
|
|
|
|
fifo16_init(&packet_lengths, packet_lengths_buf, CONFIG_QUEUE_MAX_LENGTH);
|
2018-04-05 18:10:42 +02:00
|
|
|
|
|
|
|
// Set chip select, reset and interrupt
|
|
|
|
// pins for the LoRa module
|
2024-02-09 21:46:39 +01:00
|
|
|
#if MODEM == SX1276 || MODEM == SX1278
|
|
|
|
LoRa->setPins(pin_cs, pin_reset, pin_dio, pin_busy);
|
|
|
|
#elif MODEM == SX1262
|
|
|
|
LoRa->setPins(pin_cs, pin_reset, pin_dio, pin_busy, pin_rxen);
|
|
|
|
#elif MODEM == SX1280
|
|
|
|
LoRa->setPins(pin_cs, pin_reset, pin_dio, pin_busy, pin_rxen, pin_txen);
|
|
|
|
#endif
|
2023-01-07 16:35:07 +01:00
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2023-09-12 21:51:51 +02:00
|
|
|
init_channel_stats();
|
2023-09-12 14:48:47 +02:00
|
|
|
|
2023-02-18 12:14:15 +01:00
|
|
|
// Check installed transceiver chip and
|
|
|
|
// probe boot parameters.
|
2024-02-09 21:46:39 +01:00
|
|
|
if (LoRa->preInit()) {
|
2024-01-19 11:08:55 +01:00
|
|
|
modem_installed = true;
|
2024-02-09 21:46:39 +01:00
|
|
|
uint32_t lfr = LoRa->getFrequency();
|
2023-02-18 12:14:15 +01:00
|
|
|
if (lfr == 0) {
|
|
|
|
// Normal boot
|
|
|
|
} else if (lfr == M_FRQ_R) {
|
|
|
|
// Quick reboot
|
|
|
|
#if HAS_CONSOLE
|
|
|
|
if (rtc_get_reset_reason(0) == POWERON_RESET) {
|
|
|
|
console_active = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
// Unknown boot
|
|
|
|
}
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->setFrequency(M_FRQ_S);
|
2023-01-07 16:35:07 +01:00
|
|
|
} else {
|
2024-01-19 11:08:55 +01:00
|
|
|
modem_installed = false;
|
2023-01-07 16:35:07 +01:00
|
|
|
}
|
2023-02-18 12:14:15 +01:00
|
|
|
#else
|
|
|
|
// Older variants only came with SX1276/78 chips,
|
|
|
|
// so assume that to be the case for now.
|
2024-01-19 11:08:55 +01:00
|
|
|
modem_installed = true;
|
2023-02-18 12:14:15 +01:00
|
|
|
#endif
|
2018-06-20 08:45:11 +02:00
|
|
|
|
2022-11-02 19:02:22 +01:00
|
|
|
#if HAS_DISPLAY
|
2024-05-05 18:15:55 +02:00
|
|
|
#if HAS_EEPROM
|
2023-05-03 14:05:49 +02:00
|
|
|
if (EEPROM.read(eeprom_addr(ADDR_CONF_DSET)) != CONF_OK_BYTE) {
|
2024-05-05 18:15:55 +02:00
|
|
|
#elif MCU_VARIANT == MCU_NRF52
|
|
|
|
if (eeprom_read(eeprom_addr(ADDR_CONF_DSET)) != CONF_OK_BYTE) {
|
|
|
|
#endif
|
2023-05-03 14:05:49 +02:00
|
|
|
eeprom_update(eeprom_addr(ADDR_CONF_DSET), CONF_OK_BYTE);
|
|
|
|
eeprom_update(eeprom_addr(ADDR_CONF_DINT), 0xFF);
|
|
|
|
}
|
2022-11-02 19:02:22 +01:00
|
|
|
disp_ready = display_init();
|
|
|
|
update_display();
|
|
|
|
#endif
|
|
|
|
|
2024-01-25 22:35:40 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2022-10-29 00:53:39 +02:00
|
|
|
#if HAS_PMU == true
|
|
|
|
pmu_ready = init_pmu();
|
2022-01-21 20:43:29 +01:00
|
|
|
#endif
|
|
|
|
|
2024-04-23 00:52:57 +02:00
|
|
|
#if HAS_BLUETOOTH || HAS_BLE == true
|
2022-11-02 19:02:22 +01:00
|
|
|
bt_init();
|
|
|
|
bt_init_ran = true;
|
|
|
|
#endif
|
2022-01-09 23:40:30 +01:00
|
|
|
|
2023-01-06 22:29:23 +01:00
|
|
|
if (console_active) {
|
2023-01-11 09:42:11 +01:00
|
|
|
#if HAS_CONSOLE
|
|
|
|
console_start();
|
|
|
|
#else
|
|
|
|
kiss_indicate_reset();
|
|
|
|
#endif
|
2023-01-07 16:35:07 +01:00
|
|
|
} else {
|
|
|
|
kiss_indicate_reset();
|
2023-01-06 22:29:23 +01:00
|
|
|
}
|
2022-10-29 00:53:39 +02:00
|
|
|
#endif
|
|
|
|
|
2022-10-29 16:44:49 +02:00
|
|
|
// Validate board health, EEPROM and config
|
2022-11-01 21:11:41 +01:00
|
|
|
validate_status();
|
2023-01-07 16:35:07 +01:00
|
|
|
|
2024-02-09 21:46:39 +01:00
|
|
|
if (op_mode != MODE_TNC) LoRa->setFrequency(0);
|
2018-04-05 18:10:42 +02:00
|
|
|
}
|
|
|
|
|
2021-03-12 18:48:50 +01:00
|
|
|
void lora_receive() {
|
|
|
|
if (!implicit) {
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->receive();
|
2021-03-12 18:48:50 +01:00
|
|
|
} else {
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->receive(implicit_l);
|
2021-03-12 18:48:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:14:30 +01:00
|
|
|
inline void kiss_write_packet() {
|
2022-10-30 18:35:53 +01:00
|
|
|
serial_write(FEND);
|
|
|
|
serial_write(CMD_DATA);
|
2022-01-14 21:12:33 +01:00
|
|
|
for (uint16_t i = 0; i < read_len; i++) {
|
2022-01-10 22:14:30 +01:00
|
|
|
uint8_t byte = pbuf[i];
|
2022-10-30 18:35:53 +01:00
|
|
|
if (byte == FEND) { serial_write(FESC); byte = TFEND; }
|
|
|
|
if (byte == FESC) { serial_write(FESC); byte = TFESC; }
|
|
|
|
serial_write(byte);
|
2018-04-05 18:10:42 +02:00
|
|
|
}
|
2022-10-30 18:35:53 +01:00
|
|
|
serial_write(FEND);
|
2022-01-10 22:14:30 +01:00
|
|
|
read_len = 0;
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2022-01-10 22:14:30 +01:00
|
|
|
packet_ready = false;
|
|
|
|
#endif
|
2018-04-05 18:10:42 +02:00
|
|
|
}
|
|
|
|
|
2022-01-14 21:12:33 +01:00
|
|
|
inline void getPacketData(uint16_t len) {
|
2022-01-11 02:54:32 +01:00
|
|
|
while (len-- && read_len < MTU) {
|
2024-02-09 21:46:39 +01:00
|
|
|
pbuf[read_len++] = LoRa->read();
|
2022-01-11 02:54:32 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-10 22:14:30 +01:00
|
|
|
|
2022-01-11 02:54:32 +01:00
|
|
|
void ISR_VECT receive_callback(int packet_size) {
|
2022-01-14 21:12:33 +01:00
|
|
|
if (!promisc) {
|
|
|
|
// The standard operating mode allows large
|
|
|
|
// packets with a payload up to 500 bytes,
|
|
|
|
// by combining two raw LoRa packets.
|
|
|
|
// We read the 1-byte header and extract
|
|
|
|
// packet sequence number and split flags
|
2024-02-09 21:46:39 +01:00
|
|
|
uint8_t header = LoRa->read(); packet_size--;
|
2022-01-14 21:12:33 +01:00
|
|
|
uint8_t sequence = packetSequence(header);
|
|
|
|
bool ready = false;
|
|
|
|
|
|
|
|
if (isSplitPacket(header) && seq == SEQ_UNSET) {
|
|
|
|
// This is the first part of a split
|
|
|
|
// packet, so we set the seq variable
|
|
|
|
// and add the data to the buffer
|
|
|
|
read_len = 0;
|
|
|
|
seq = sequence;
|
2022-01-10 22:14:30 +01:00
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
|
2024-02-09 21:46:39 +01:00
|
|
|
last_rssi = LoRa->packetRssi();
|
|
|
|
last_snr_raw = LoRa->packetSnrRaw();
|
2022-01-14 21:12:33 +01:00
|
|
|
#endif
|
2022-01-11 02:54:32 +01:00
|
|
|
|
2022-01-14 21:12:33 +01:00
|
|
|
getPacketData(packet_size);
|
2022-01-11 02:54:32 +01:00
|
|
|
|
2022-01-14 21:12:33 +01:00
|
|
|
} else if (isSplitPacket(header) && seq == sequence) {
|
|
|
|
// This is the second part of a split
|
|
|
|
// packet, so we add it to the buffer
|
|
|
|
// and set the ready flag.
|
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
|
|
|
|
#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
|
2024-02-09 21:46:39 +01:00
|
|
|
last_rssi = (last_rssi+LoRa->packetRssi())/2;
|
|
|
|
last_snr_raw = (last_snr_raw+LoRa->packetSnrRaw())/2;
|
2022-01-14 21:12:33 +01:00
|
|
|
#endif
|
2024-01-19 11:08:55 +01:00
|
|
|
|
2022-01-14 21:12:33 +01:00
|
|
|
getPacketData(packet_size);
|
2024-01-19 11:08:55 +01:00
|
|
|
|
2022-01-14 21:12:33 +01:00
|
|
|
seq = SEQ_UNSET;
|
|
|
|
ready = true;
|
|
|
|
|
|
|
|
} else if (isSplitPacket(header) && seq != sequence) {
|
|
|
|
// This split packet does not carry the
|
|
|
|
// same sequence id, so we must assume
|
|
|
|
// that we are seeing the first part of
|
|
|
|
// a new split packet.
|
|
|
|
read_len = 0;
|
|
|
|
seq = sequence;
|
2022-01-11 02:54:32 +01:00
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
|
2024-02-09 21:46:39 +01:00
|
|
|
last_rssi = LoRa->packetRssi();
|
|
|
|
last_snr_raw = LoRa->packetSnrRaw();
|
2022-01-14 21:12:33 +01:00
|
|
|
#endif
|
2022-01-11 02:54:32 +01:00
|
|
|
|
2022-01-14 21:12:33 +01:00
|
|
|
getPacketData(packet_size);
|
2022-01-11 02:54:32 +01:00
|
|
|
|
2022-01-14 21:12:33 +01:00
|
|
|
} else if (!isSplitPacket(header)) {
|
|
|
|
// This is not a split packet, so we
|
|
|
|
// just read it and set the ready
|
|
|
|
// flag to true.
|
2022-01-11 02:54:32 +01:00
|
|
|
|
2022-01-14 21:12:33 +01:00
|
|
|
if (seq != SEQ_UNSET) {
|
|
|
|
// If we already had part of a split
|
|
|
|
// packet in the buffer, we clear it.
|
|
|
|
read_len = 0;
|
|
|
|
seq = SEQ_UNSET;
|
2018-06-27 11:42:48 +02:00
|
|
|
}
|
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
|
2024-02-09 21:46:39 +01:00
|
|
|
last_rssi = LoRa->packetRssi();
|
|
|
|
last_snr_raw = LoRa->packetSnrRaw();
|
2022-01-14 21:12:33 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
getPacketData(packet_size);
|
|
|
|
ready = true;
|
|
|
|
}
|
2018-04-05 18:10:42 +02:00
|
|
|
|
2022-01-14 21:12:33 +01:00
|
|
|
if (ready) {
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
|
2022-01-10 22:14:30 +01:00
|
|
|
// We first signal the RSSI of the
|
|
|
|
// recieved packet to the host.
|
|
|
|
kiss_indicate_stat_rssi();
|
|
|
|
kiss_indicate_stat_snr();
|
|
|
|
|
|
|
|
// And then write the entire packet
|
|
|
|
kiss_write_packet();
|
|
|
|
#else
|
|
|
|
packet_ready = true;
|
|
|
|
#endif
|
2022-01-14 21:12:33 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-14 21:41:10 +01:00
|
|
|
// In promiscuous mode, raw packets are
|
|
|
|
// output directly to the host
|
|
|
|
read_len = 0;
|
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
|
2024-02-09 21:46:39 +01:00
|
|
|
last_rssi = LoRa->packetRssi();
|
|
|
|
last_snr_raw = LoRa->packetSnrRaw();
|
2022-01-14 21:12:33 +01:00
|
|
|
getPacketData(packet_size);
|
|
|
|
|
|
|
|
// We first signal the RSSI of the
|
|
|
|
// recieved packet to the host.
|
|
|
|
kiss_indicate_stat_rssi();
|
|
|
|
kiss_indicate_stat_snr();
|
|
|
|
|
|
|
|
// And then write the entire packet
|
|
|
|
kiss_write_packet();
|
|
|
|
|
|
|
|
#else
|
|
|
|
getPacketData(packet_size);
|
|
|
|
packet_ready = true;
|
2022-01-10 22:14:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool startRadio() {
|
|
|
|
update_radio_lock();
|
2023-01-10 18:12:40 +01:00
|
|
|
if (!radio_online && !console_active) {
|
2022-01-10 22:14:30 +01:00
|
|
|
if (!radio_locked && hw_ready) {
|
2024-02-09 21:46:39 +01:00
|
|
|
if (!LoRa->begin(lora_freq)) {
|
2022-01-10 22:14:30 +01:00
|
|
|
// The radio could not be started.
|
|
|
|
// Indicate this failure over both the
|
|
|
|
// serial port and with the onboard LEDs
|
2022-10-29 16:44:49 +02:00
|
|
|
radio_error = true;
|
2022-01-10 22:14:30 +01:00
|
|
|
kiss_indicate_error(ERROR_INITRADIO);
|
|
|
|
led_indicate_error(0);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
radio_online = true;
|
|
|
|
|
2023-09-13 20:05:05 +02:00
|
|
|
init_channel_stats();
|
|
|
|
|
2022-01-10 22:14:30 +01:00
|
|
|
setTXPower();
|
|
|
|
setBandwidth();
|
|
|
|
setSpreadingFactor();
|
|
|
|
setCodingRate();
|
|
|
|
getFrequency();
|
|
|
|
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->enableCrc();
|
2022-01-10 22:14:30 +01:00
|
|
|
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->onReceive(receive_callback);
|
2022-01-10 22:14:30 +01:00
|
|
|
|
|
|
|
lora_receive();
|
|
|
|
|
|
|
|
// Flash an info pattern to indicate
|
|
|
|
// that the radio is now on
|
2022-01-22 21:43:52 +01:00
|
|
|
kiss_indicate_radiostate();
|
2022-01-10 22:14:30 +01:00
|
|
|
led_indicate_info(3);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Flash a warning pattern to indicate
|
|
|
|
// that the radio was locked, and thus
|
|
|
|
// not started
|
2022-01-22 21:43:52 +01:00
|
|
|
radio_online = false;
|
|
|
|
kiss_indicate_radiostate();
|
2022-01-10 22:14:30 +01:00
|
|
|
led_indicate_warning(3);
|
|
|
|
return false;
|
2018-04-05 18:10:42 +02:00
|
|
|
}
|
2022-01-10 22:14:30 +01:00
|
|
|
} else {
|
|
|
|
// If radio is already on, we silently
|
|
|
|
// ignore the request.
|
2022-01-22 21:43:52 +01:00
|
|
|
kiss_indicate_radiostate();
|
2022-01-10 22:14:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopRadio() {
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->end();
|
2022-01-10 22:14:30 +01:00
|
|
|
radio_online = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_radio_lock() {
|
|
|
|
if (lora_freq != 0 && lora_bw != 0 && lora_txp != 0xFF && lora_sf != 0) {
|
|
|
|
radio_locked = false;
|
|
|
|
} else {
|
|
|
|
radio_locked = true;
|
2018-04-05 18:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 10:22:44 +02:00
|
|
|
bool queueFull() {
|
2020-05-29 14:58:10 +02:00
|
|
|
return (queue_height >= CONFIG_QUEUE_MAX_LENGTH || queued_bytes >= CONFIG_QUEUE_SIZE);
|
2018-06-27 10:22:44 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:18:19 +02:00
|
|
|
volatile bool queue_flushing = false;
|
|
|
|
void flushQueue(void) {
|
|
|
|
if (!queue_flushing) {
|
|
|
|
queue_flushing = true;
|
2018-06-27 10:22:44 +02:00
|
|
|
|
2023-09-14 22:32:26 +02:00
|
|
|
led_tx_on();
|
2022-01-09 23:45:40 +01:00
|
|
|
uint16_t processed = 0;
|
2022-01-09 23:40:30 +01:00
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2022-01-09 23:40:30 +01:00
|
|
|
while (!fifo16_isempty(&packet_starts)) {
|
|
|
|
#else
|
2020-05-29 14:58:10 +02:00
|
|
|
while (!fifo16_isempty_locked(&packet_starts)) {
|
2022-01-09 23:40:30 +01:00
|
|
|
#endif
|
|
|
|
|
2022-01-09 23:45:40 +01:00
|
|
|
uint16_t start = fifo16_pop(&packet_starts);
|
|
|
|
uint16_t length = fifo16_pop(&packet_lengths);
|
2018-06-27 10:22:44 +02:00
|
|
|
|
2020-05-29 14:58:10 +02:00
|
|
|
if (length >= MIN_L && length <= MTU) {
|
2022-01-09 23:45:40 +01:00
|
|
|
for (uint16_t i = 0; i < length; i++) {
|
|
|
|
uint16_t pos = (start+i)%CONFIG_QUEUE_SIZE;
|
2020-05-28 22:18:19 +02:00
|
|
|
tbuf[i] = packet_queue[pos];
|
|
|
|
}
|
2018-06-27 10:22:44 +02:00
|
|
|
|
2020-05-28 22:18:19 +02:00
|
|
|
transmit(length);
|
|
|
|
processed++;
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 22:32:26 +02:00
|
|
|
|
|
|
|
lora_receive();
|
|
|
|
led_tx_off();
|
2023-09-17 16:23:35 +02:00
|
|
|
post_tx_yield_timeout = millis()+(lora_post_tx_yield_slots*csma_slot_ms);
|
2018-06-27 10:22:44 +02:00
|
|
|
}
|
2020-05-28 22:18:19 +02:00
|
|
|
|
|
|
|
queue_height = 0;
|
|
|
|
queued_bytes = 0;
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2023-09-13 20:05:05 +02:00
|
|
|
update_airtime();
|
|
|
|
#endif
|
2020-05-28 22:18:19 +02:00
|
|
|
queue_flushing = false;
|
2018-06-27 10:22:44 +02:00
|
|
|
}
|
|
|
|
|
2023-09-14 13:00:40 +02:00
|
|
|
#define PHY_HEADER_LORA_SYMBOLS 8
|
2023-09-12 14:48:47 +02:00
|
|
|
void add_airtime(uint16_t written) {
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2023-09-14 13:00:40 +02:00
|
|
|
float packet_cost_ms = 0.0;
|
|
|
|
float payload_cost_ms = ((float)written * lora_us_per_byte)/1000.0;
|
|
|
|
packet_cost_ms += payload_cost_ms;
|
|
|
|
packet_cost_ms += (lora_preamble_symbols+4.25)*lora_symbol_time_ms;
|
|
|
|
packet_cost_ms += PHY_HEADER_LORA_SYMBOLS * lora_symbol_time_ms;
|
2023-09-12 21:51:51 +02:00
|
|
|
uint16_t cb = current_airtime_bin();
|
|
|
|
uint16_t nb = cb+1; if (nb == AIRTIME_BINS) { nb = 0; }
|
2023-09-14 13:00:40 +02:00
|
|
|
airtime_bins[cb] += packet_cost_ms;
|
2023-09-12 21:51:51 +02:00
|
|
|
airtime_bins[nb] = 0;
|
2023-09-12 14:48:47 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_airtime() {
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2023-09-12 14:48:47 +02:00
|
|
|
uint16_t cb = current_airtime_bin();
|
2023-12-10 22:40:06 +01:00
|
|
|
uint16_t pb = cb-1; if (cb-1 < 0) { pb = AIRTIME_BINS-1; }
|
2023-09-12 21:51:51 +02:00
|
|
|
uint16_t nb = cb+1; if (nb == AIRTIME_BINS) { nb = 0; }
|
|
|
|
airtime_bins[nb] = 0;
|
2023-09-12 14:48:47 +02:00
|
|
|
airtime = (float)(airtime_bins[cb]+airtime_bins[pb])/(2.0*AIRTIME_BINLEN_MS);
|
|
|
|
|
|
|
|
uint32_t longterm_airtime_sum = 0;
|
|
|
|
for (uint16_t bin = 0; bin < AIRTIME_BINS; bin++) {
|
|
|
|
longterm_airtime_sum += airtime_bins[bin];
|
|
|
|
}
|
|
|
|
longterm_airtime = (float)longterm_airtime_sum/(float)AIRTIME_LONGTERM_MS;
|
2023-09-12 21:51:51 +02:00
|
|
|
|
|
|
|
float longterm_channel_util_sum = 0.0;
|
|
|
|
for (uint16_t bin = 0; bin < AIRTIME_BINS; bin++) {
|
|
|
|
longterm_channel_util_sum += longterm_bins[bin];
|
|
|
|
}
|
|
|
|
longterm_channel_util = (float)longterm_channel_util_sum/(float)AIRTIME_BINS;
|
2023-09-13 20:05:05 +02:00
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2023-09-15 09:10:46 +02:00
|
|
|
update_csma_p();
|
|
|
|
#endif
|
2023-09-13 20:05:05 +02:00
|
|
|
kiss_indicate_channel_stats();
|
2023-09-12 14:48:47 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-01-09 23:45:40 +01:00
|
|
|
void transmit(uint16_t size) {
|
2018-04-05 18:10:42 +02:00
|
|
|
if (radio_online) {
|
2018-06-27 11:42:48 +02:00
|
|
|
if (!promisc) {
|
2022-01-09 23:45:40 +01:00
|
|
|
uint16_t written = 0;
|
2018-06-27 11:42:48 +02:00
|
|
|
uint8_t header = random(256) & 0xF0;
|
2018-04-05 18:10:42 +02:00
|
|
|
|
2018-06-27 11:42:48 +02:00
|
|
|
if (size > SINGLE_MTU - HEADER_L) {
|
|
|
|
header = header | FLAG_SPLIT;
|
|
|
|
}
|
2018-04-05 18:10:42 +02:00
|
|
|
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->beginPacket();
|
|
|
|
LoRa->write(header); written++;
|
2018-04-05 18:10:42 +02:00
|
|
|
|
2022-01-14 10:51:40 +01:00
|
|
|
for (uint16_t i=0; i < size; i++) {
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->write(tbuf[i]);
|
2018-06-27 10:22:44 +02:00
|
|
|
|
2018-06-27 11:42:48 +02:00
|
|
|
written++;
|
2018-04-05 18:10:42 +02:00
|
|
|
|
2018-06-27 11:42:48 +02:00
|
|
|
if (written == 255) {
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->endPacket(); add_airtime(written);
|
|
|
|
LoRa->beginPacket();
|
|
|
|
LoRa->write(header);
|
2018-06-27 11:42:48 +02:00
|
|
|
written = 1;
|
|
|
|
}
|
2018-04-05 18:10:42 +02:00
|
|
|
}
|
|
|
|
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->endPacket(); add_airtime(written);
|
2018-06-27 11:42:48 +02:00
|
|
|
} else {
|
|
|
|
// In promiscuous mode, we only send out
|
|
|
|
// plain raw LoRa packets with a maximum
|
|
|
|
// payload of 255 bytes
|
|
|
|
led_tx_on();
|
2022-01-09 23:45:40 +01:00
|
|
|
uint16_t written = 0;
|
2018-06-27 11:42:48 +02:00
|
|
|
|
|
|
|
// Cap packets at 255 bytes
|
|
|
|
if (size > SINGLE_MTU) {
|
|
|
|
size = SINGLE_MTU;
|
|
|
|
}
|
2018-04-26 10:34:39 +02:00
|
|
|
|
2021-03-12 18:48:50 +01:00
|
|
|
// If implicit header mode has been set,
|
|
|
|
// set packet length to payload data length
|
|
|
|
if (!implicit) {
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->beginPacket();
|
2021-03-12 18:48:50 +01:00
|
|
|
} else {
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->beginPacket(size);
|
2021-03-12 18:48:50 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 10:51:40 +01:00
|
|
|
for (uint16_t i=0; i < size; i++) {
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->write(tbuf[i]);
|
2018-06-27 11:42:48 +02:00
|
|
|
|
|
|
|
written++;
|
|
|
|
}
|
2024-02-09 21:46:39 +01:00
|
|
|
LoRa->endPacket(); add_airtime(written);
|
2018-06-27 11:42:48 +02:00
|
|
|
}
|
2018-04-05 18:10:42 +02:00
|
|
|
} else {
|
|
|
|
kiss_indicate_error(ERROR_TXFAILED);
|
|
|
|
led_indicate_error(5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialCallback(uint8_t sbyte) {
|
|
|
|
if (IN_FRAME && sbyte == FEND && command == CMD_DATA) {
|
|
|
|
IN_FRAME = false;
|
2018-06-27 10:22:44 +02:00
|
|
|
|
2020-05-29 14:58:10 +02:00
|
|
|
if (!fifo16_isfull(&packet_starts) && queued_bytes < CONFIG_QUEUE_SIZE) {
|
2022-01-09 23:45:40 +01:00
|
|
|
uint16_t s = current_packet_start;
|
2022-01-14 10:51:40 +01:00
|
|
|
int16_t e = queue_cursor-1; if (e == -1) e = CONFIG_QUEUE_SIZE-1;
|
2022-01-09 23:45:40 +01:00
|
|
|
uint16_t l;
|
2020-05-28 22:18:19 +02:00
|
|
|
|
|
|
|
if (s != e) {
|
|
|
|
l = (s < e) ? e - s + 1 : CONFIG_QUEUE_SIZE - s + e + 1;
|
|
|
|
} else {
|
|
|
|
l = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l >= MIN_L) {
|
|
|
|
queue_height++;
|
|
|
|
|
2020-05-29 14:58:10 +02:00
|
|
|
fifo16_push(&packet_starts, s);
|
|
|
|
fifo16_push(&packet_lengths, l);
|
2020-05-28 22:18:19 +02:00
|
|
|
|
|
|
|
current_packet_start = queue_cursor;
|
|
|
|
}
|
2020-05-29 14:58:10 +02:00
|
|
|
|
2018-06-27 10:22:44 +02:00
|
|
|
}
|
2020-05-28 22:18:19 +02:00
|
|
|
|
2018-04-05 18:10:42 +02:00
|
|
|
} else if (sbyte == FEND) {
|
|
|
|
IN_FRAME = true;
|
|
|
|
command = CMD_UNKNOWN;
|
|
|
|
frame_len = 0;
|
|
|
|
} else if (IN_FRAME && frame_len < MTU) {
|
|
|
|
// Have a look at the command byte first
|
|
|
|
if (frame_len == 0 && command == CMD_UNKNOWN) {
|
|
|
|
command = sbyte;
|
|
|
|
} else if (command == CMD_DATA) {
|
2022-10-30 18:35:53 +01:00
|
|
|
if (bt_state != BT_STATE_CONNECTED) cable_state = CABLE_STATE_CONNECTED;
|
2018-04-05 18:10:42 +02:00
|
|
|
if (sbyte == FESC) {
|
|
|
|
ESCAPE = true;
|
|
|
|
} else {
|
|
|
|
if (ESCAPE) {
|
|
|
|
if (sbyte == TFEND) sbyte = FEND;
|
|
|
|
if (sbyte == TFESC) sbyte = FESC;
|
|
|
|
ESCAPE = false;
|
|
|
|
}
|
2020-05-28 22:18:19 +02:00
|
|
|
if (queue_height < CONFIG_QUEUE_MAX_LENGTH && queued_bytes < CONFIG_QUEUE_SIZE) {
|
|
|
|
queued_bytes++;
|
|
|
|
packet_queue[queue_cursor++] = sbyte;
|
|
|
|
if (queue_cursor == CONFIG_QUEUE_SIZE) queue_cursor = 0;
|
|
|
|
}
|
2018-04-05 18:10:42 +02:00
|
|
|
}
|
|
|
|
} else if (command == CMD_FREQUENCY) {
|
|
|
|
if (sbyte == FESC) {
|
|
|
|
ESCAPE = true;
|
|
|
|
} else {
|
|
|
|
if (ESCAPE) {
|
|
|
|
if (sbyte == TFEND) sbyte = FEND;
|
|
|
|
if (sbyte == TFESC) sbyte = FESC;
|
|
|
|
ESCAPE = false;
|
|
|
|
}
|
2022-11-01 21:11:41 +01:00
|
|
|
if (frame_len < CMD_L) cmdbuf[frame_len++] = sbyte;
|
2018-04-05 18:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_len == 4) {
|
2022-11-01 21:11:41 +01:00
|
|
|
uint32_t freq = (uint32_t)cmdbuf[0] << 24 | (uint32_t)cmdbuf[1] << 16 | (uint32_t)cmdbuf[2] << 8 | (uint32_t)cmdbuf[3];
|
2018-04-05 18:10:42 +02:00
|
|
|
|
|
|
|
if (freq == 0) {
|
|
|
|
kiss_indicate_frequency();
|
|
|
|
} else {
|
|
|
|
lora_freq = freq;
|
2018-06-20 17:45:22 +02:00
|
|
|
if (op_mode == MODE_HOST) setFrequency();
|
2018-04-05 18:10:42 +02:00
|
|
|
kiss_indicate_frequency();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (command == CMD_BANDWIDTH) {
|
|
|
|
if (sbyte == FESC) {
|
|
|
|
ESCAPE = true;
|
|
|
|
} else {
|
|
|
|
if (ESCAPE) {
|
|
|
|
if (sbyte == TFEND) sbyte = FEND;
|
|
|
|
if (sbyte == TFESC) sbyte = FESC;
|
|
|
|
ESCAPE = false;
|
|
|
|
}
|
2022-11-01 21:11:41 +01:00
|
|
|
if (frame_len < CMD_L) cmdbuf[frame_len++] = sbyte;
|
2018-04-05 18:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_len == 4) {
|
2022-11-01 21:11:41 +01:00
|
|
|
uint32_t bw = (uint32_t)cmdbuf[0] << 24 | (uint32_t)cmdbuf[1] << 16 | (uint32_t)cmdbuf[2] << 8 | (uint32_t)cmdbuf[3];
|
2018-04-05 18:10:42 +02:00
|
|
|
|
|
|
|
if (bw == 0) {
|
|
|
|
kiss_indicate_bandwidth();
|
|
|
|
} else {
|
|
|
|
lora_bw = bw;
|
2018-06-20 17:45:22 +02:00
|
|
|
if (op_mode == MODE_HOST) setBandwidth();
|
2018-04-05 18:10:42 +02:00
|
|
|
kiss_indicate_bandwidth();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (command == CMD_TXPOWER) {
|
|
|
|
if (sbyte == 0xFF) {
|
|
|
|
kiss_indicate_txpower();
|
|
|
|
} else {
|
|
|
|
int txp = sbyte;
|
2024-02-13 14:18:41 +01:00
|
|
|
#if MODEM == SX1262
|
|
|
|
if (txp > 22) txp = 22;
|
|
|
|
#else
|
|
|
|
if (txp > 17) txp = 17;
|
|
|
|
#endif
|
2018-04-05 18:10:42 +02:00
|
|
|
|
|
|
|
lora_txp = txp;
|
2018-06-20 17:45:22 +02:00
|
|
|
if (op_mode == MODE_HOST) setTXPower();
|
2018-04-05 18:10:42 +02:00
|
|
|
kiss_indicate_txpower();
|
|
|
|
}
|
|
|
|
} else if (command == CMD_SF) {
|
|
|
|
if (sbyte == 0xFF) {
|
|
|
|
kiss_indicate_spreadingfactor();
|
|
|
|
} else {
|
|
|
|
int sf = sbyte;
|
2024-02-09 21:46:39 +01:00
|
|
|
if (sf < 5) sf = 5;
|
2018-04-05 18:10:42 +02:00
|
|
|
if (sf > 12) sf = 12;
|
|
|
|
|
|
|
|
lora_sf = sf;
|
2018-06-20 17:45:22 +02:00
|
|
|
if (op_mode == MODE_HOST) setSpreadingFactor();
|
2018-04-05 18:10:42 +02:00
|
|
|
kiss_indicate_spreadingfactor();
|
|
|
|
}
|
2018-06-20 08:45:11 +02:00
|
|
|
} else if (command == CMD_CR) {
|
|
|
|
if (sbyte == 0xFF) {
|
|
|
|
kiss_indicate_codingrate();
|
|
|
|
} else {
|
|
|
|
int cr = sbyte;
|
|
|
|
if (cr < 5) cr = 5;
|
|
|
|
if (cr > 8) cr = 8;
|
|
|
|
|
|
|
|
lora_cr = cr;
|
2018-06-20 17:45:22 +02:00
|
|
|
if (op_mode == MODE_HOST) setCodingRate();
|
2018-06-20 08:45:11 +02:00
|
|
|
kiss_indicate_codingrate();
|
|
|
|
}
|
2021-03-12 18:48:50 +01:00
|
|
|
} else if (command == CMD_IMPLICIT) {
|
|
|
|
set_implicit_length(sbyte);
|
|
|
|
kiss_indicate_implicit_length();
|
2022-10-29 16:44:49 +02:00
|
|
|
} else if (command == CMD_LEAVE) {
|
|
|
|
if (sbyte == 0xFF) {
|
2022-10-29 17:34:08 +02:00
|
|
|
cable_state = CABLE_STATE_DISCONNECTED;
|
|
|
|
current_rssi = -292;
|
|
|
|
last_rssi = -292;
|
|
|
|
last_rssi_raw = 0x00;
|
|
|
|
last_snr_raw = 0x80;
|
2022-10-29 16:44:49 +02:00
|
|
|
}
|
2018-04-05 18:10:42 +02:00
|
|
|
} else if (command == CMD_RADIO_STATE) {
|
2022-10-30 18:35:53 +01:00
|
|
|
if (bt_state != BT_STATE_CONNECTED) cable_state = CABLE_STATE_CONNECTED;
|
2018-04-05 18:10:42 +02:00
|
|
|
if (sbyte == 0xFF) {
|
|
|
|
kiss_indicate_radiostate();
|
|
|
|
} else if (sbyte == 0x00) {
|
|
|
|
stopRadio();
|
|
|
|
kiss_indicate_radiostate();
|
|
|
|
} else if (sbyte == 0x01) {
|
|
|
|
startRadio();
|
|
|
|
kiss_indicate_radiostate();
|
|
|
|
}
|
2023-09-13 20:05:05 +02:00
|
|
|
} else if (command == CMD_ST_ALOCK) {
|
|
|
|
if (sbyte == FESC) {
|
|
|
|
ESCAPE = true;
|
|
|
|
} else {
|
|
|
|
if (ESCAPE) {
|
|
|
|
if (sbyte == TFEND) sbyte = FEND;
|
|
|
|
if (sbyte == TFESC) sbyte = FESC;
|
|
|
|
ESCAPE = false;
|
|
|
|
}
|
|
|
|
if (frame_len < CMD_L) cmdbuf[frame_len++] = sbyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_len == 2) {
|
|
|
|
uint16_t at = (uint16_t)cmdbuf[0] << 8 | (uint16_t)cmdbuf[1];
|
|
|
|
|
|
|
|
if (at == 0) {
|
|
|
|
st_airtime_limit = 0.0;
|
|
|
|
} else {
|
|
|
|
st_airtime_limit = (float)at/(100.0*100.0);
|
|
|
|
if (st_airtime_limit >= 1.0) { st_airtime_limit = 0.0; }
|
|
|
|
}
|
|
|
|
kiss_indicate_st_alock();
|
|
|
|
}
|
|
|
|
} else if (command == CMD_LT_ALOCK) {
|
|
|
|
if (sbyte == FESC) {
|
|
|
|
ESCAPE = true;
|
|
|
|
} else {
|
|
|
|
if (ESCAPE) {
|
|
|
|
if (sbyte == TFEND) sbyte = FEND;
|
|
|
|
if (sbyte == TFESC) sbyte = FESC;
|
|
|
|
ESCAPE = false;
|
|
|
|
}
|
|
|
|
if (frame_len < CMD_L) cmdbuf[frame_len++] = sbyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_len == 2) {
|
|
|
|
uint16_t at = (uint16_t)cmdbuf[0] << 8 | (uint16_t)cmdbuf[1];
|
|
|
|
|
|
|
|
if (at == 0) {
|
|
|
|
lt_airtime_limit = 0.0;
|
|
|
|
} else {
|
|
|
|
lt_airtime_limit = (float)at/(100.0*100.0);
|
|
|
|
if (lt_airtime_limit >= 1.0) { lt_airtime_limit = 0.0; }
|
|
|
|
}
|
|
|
|
kiss_indicate_lt_alock();
|
|
|
|
}
|
2018-04-05 18:10:42 +02:00
|
|
|
} else if (command == CMD_STAT_RX) {
|
|
|
|
kiss_indicate_stat_rx();
|
|
|
|
} else if (command == CMD_STAT_TX) {
|
|
|
|
kiss_indicate_stat_tx();
|
|
|
|
} else if (command == CMD_STAT_RSSI) {
|
|
|
|
kiss_indicate_stat_rssi();
|
|
|
|
} else if (command == CMD_RADIO_LOCK) {
|
|
|
|
update_radio_lock();
|
|
|
|
kiss_indicate_radio_lock();
|
|
|
|
} else if (command == CMD_BLINK) {
|
|
|
|
led_indicate_info(sbyte);
|
|
|
|
} else if (command == CMD_RANDOM) {
|
|
|
|
kiss_indicate_random(getRandom());
|
2018-06-20 08:45:11 +02:00
|
|
|
} else if (command == CMD_DETECT) {
|
|
|
|
if (sbyte == DETECT_REQ) {
|
2022-10-30 18:35:53 +01:00
|
|
|
if (bt_state != BT_STATE_CONNECTED) cable_state = CABLE_STATE_CONNECTED;
|
2018-06-20 08:45:11 +02:00
|
|
|
kiss_indicate_detect();
|
|
|
|
}
|
2018-06-27 11:42:48 +02:00
|
|
|
} else if (command == CMD_PROMISC) {
|
|
|
|
if (sbyte == 0x01) {
|
|
|
|
promisc_enable();
|
|
|
|
} else if (sbyte == 0x00) {
|
|
|
|
promisc_disable();
|
|
|
|
}
|
|
|
|
kiss_indicate_promisc();
|
2020-05-20 16:16:04 +02:00
|
|
|
} else if (command == CMD_READY) {
|
|
|
|
if (!queueFull()) {
|
|
|
|
kiss_indicate_ready();
|
|
|
|
} else {
|
|
|
|
kiss_indicate_not_ready();
|
|
|
|
}
|
2018-06-20 08:45:11 +02:00
|
|
|
} else if (command == CMD_UNLOCK_ROM) {
|
|
|
|
if (sbyte == ROM_UNLOCK_BYTE) {
|
|
|
|
unlock_rom();
|
|
|
|
}
|
2022-01-09 23:40:30 +01:00
|
|
|
} else if (command == CMD_RESET) {
|
|
|
|
if (sbyte == CMD_RESET_BYTE) {
|
|
|
|
hard_reset();
|
|
|
|
}
|
2018-06-20 08:45:11 +02:00
|
|
|
} else if (command == CMD_ROM_READ) {
|
|
|
|
kiss_dump_eeprom();
|
|
|
|
} else if (command == CMD_ROM_WRITE) {
|
|
|
|
if (sbyte == FESC) {
|
|
|
|
ESCAPE = true;
|
|
|
|
} else {
|
|
|
|
if (ESCAPE) {
|
|
|
|
if (sbyte == TFEND) sbyte = FEND;
|
|
|
|
if (sbyte == TFESC) sbyte = FESC;
|
|
|
|
ESCAPE = false;
|
|
|
|
}
|
2022-11-01 21:11:41 +01:00
|
|
|
if (frame_len < CMD_L) cmdbuf[frame_len++] = sbyte;
|
2018-06-20 08:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_len == 2) {
|
2022-11-01 21:11:41 +01:00
|
|
|
eeprom_write(cmdbuf[0], cmdbuf[1]);
|
2018-06-20 08:45:11 +02:00
|
|
|
}
|
|
|
|
} else if (command == CMD_FW_VERSION) {
|
|
|
|
kiss_indicate_version();
|
2022-01-09 23:40:30 +01:00
|
|
|
} else if (command == CMD_PLATFORM) {
|
|
|
|
kiss_indicate_platform();
|
|
|
|
} else if (command == CMD_MCU) {
|
|
|
|
kiss_indicate_mcu();
|
2022-01-22 22:34:03 +01:00
|
|
|
} else if (command == CMD_BOARD) {
|
|
|
|
kiss_indicate_board();
|
2018-06-20 16:32:30 +02:00
|
|
|
} else if (command == CMD_CONF_SAVE) {
|
|
|
|
eeprom_conf_save();
|
|
|
|
} else if (command == CMD_CONF_DELETE) {
|
|
|
|
eeprom_conf_delete();
|
2022-10-29 00:53:39 +02:00
|
|
|
} else if (command == CMD_FB_EXT) {
|
|
|
|
#if HAS_DISPLAY == true
|
|
|
|
if (sbyte == 0xFF) {
|
|
|
|
kiss_indicate_fbstate();
|
|
|
|
} else if (sbyte == 0x00) {
|
|
|
|
ext_fb_disable();
|
|
|
|
kiss_indicate_fbstate();
|
|
|
|
} else if (sbyte == 0x01) {
|
|
|
|
ext_fb_enable();
|
|
|
|
kiss_indicate_fbstate();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else if (command == CMD_FB_WRITE) {
|
|
|
|
if (sbyte == FESC) {
|
|
|
|
ESCAPE = true;
|
|
|
|
} else {
|
|
|
|
if (ESCAPE) {
|
|
|
|
if (sbyte == TFEND) sbyte = FEND;
|
|
|
|
if (sbyte == TFESC) sbyte = FESC;
|
|
|
|
ESCAPE = false;
|
|
|
|
}
|
2022-11-01 21:11:41 +01:00
|
|
|
if (frame_len < CMD_L) cmdbuf[frame_len++] = sbyte;
|
2022-10-29 00:53:39 +02:00
|
|
|
}
|
2022-10-30 18:58:12 +01:00
|
|
|
#if HAS_DISPLAY
|
|
|
|
if (frame_len == 9) {
|
2022-11-01 21:11:41 +01:00
|
|
|
uint8_t line = cmdbuf[0];
|
2022-10-30 18:58:12 +01:00
|
|
|
if (line > 63) line = 63;
|
|
|
|
int fb_o = line*8;
|
2022-11-01 21:11:41 +01:00
|
|
|
memcpy(fb+fb_o, cmdbuf+1, 8);
|
2022-10-30 18:58:12 +01:00
|
|
|
}
|
|
|
|
#endif
|
2022-10-29 00:53:39 +02:00
|
|
|
} else if (command == CMD_FB_READ) {
|
|
|
|
if (sbyte != 0x00) {
|
|
|
|
kiss_indicate_fb();
|
|
|
|
}
|
2022-11-01 21:11:41 +01:00
|
|
|
} else if (command == CMD_DEV_HASH) {
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2022-11-01 21:11:41 +01:00
|
|
|
if (sbyte != 0x00) {
|
|
|
|
kiss_indicate_device_hash();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else if (command == CMD_DEV_SIG) {
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2022-11-01 21:11:41 +01:00
|
|
|
if (sbyte == FESC) {
|
|
|
|
ESCAPE = true;
|
|
|
|
} else {
|
|
|
|
if (ESCAPE) {
|
|
|
|
if (sbyte == TFEND) sbyte = FEND;
|
|
|
|
if (sbyte == TFESC) sbyte = FESC;
|
|
|
|
ESCAPE = false;
|
|
|
|
}
|
|
|
|
if (frame_len < CMD_L) cmdbuf[frame_len++] = sbyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_len == DEV_SIG_LEN) {
|
|
|
|
memcpy(dev_sig, cmdbuf, DEV_SIG_LEN);
|
|
|
|
device_save_signature();
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-03 00:58:45 +01:00
|
|
|
} else if (command == CMD_FW_UPD) {
|
|
|
|
if (sbyte == 0x01) {
|
|
|
|
firmware_update_mode = true;
|
|
|
|
} else {
|
|
|
|
firmware_update_mode = false;
|
|
|
|
}
|
2022-11-01 22:21:07 +01:00
|
|
|
} else if (command == CMD_HASHES) {
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2022-11-01 22:21:07 +01:00
|
|
|
if (sbyte == 0x01) {
|
|
|
|
kiss_indicate_target_fw_hash();
|
|
|
|
} else if (sbyte == 0x02) {
|
|
|
|
kiss_indicate_fw_hash();
|
|
|
|
} else if (sbyte == 0x03) {
|
|
|
|
kiss_indicate_bootloader_hash();
|
|
|
|
} else if (sbyte == 0x04) {
|
|
|
|
kiss_indicate_partition_table_hash();
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-01 21:11:41 +01:00
|
|
|
} else if (command == CMD_FW_HASH) {
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2022-11-01 21:11:41 +01:00
|
|
|
if (sbyte == FESC) {
|
|
|
|
ESCAPE = true;
|
|
|
|
} else {
|
|
|
|
if (ESCAPE) {
|
|
|
|
if (sbyte == TFEND) sbyte = FEND;
|
|
|
|
if (sbyte == TFESC) sbyte = FESC;
|
|
|
|
ESCAPE = false;
|
|
|
|
}
|
|
|
|
if (frame_len < CMD_L) cmdbuf[frame_len++] = sbyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_len == DEV_HASH_LEN) {
|
2023-12-10 22:40:06 +01:00
|
|
|
memcpy(dev_firmware_hash_target, cmdbuf, DEV_HASH_LEN);
|
2022-11-01 21:11:41 +01:00
|
|
|
device_save_firmware_hash();
|
|
|
|
}
|
|
|
|
#endif
|
2022-10-30 13:51:36 +01:00
|
|
|
} else if (command == CMD_BT_CTRL) {
|
2024-04-23 00:52:57 +02:00
|
|
|
#if HAS_BLUETOOTH || HAS_BLE
|
2022-10-30 13:51:36 +01:00
|
|
|
if (sbyte == 0x00) {
|
2022-10-30 14:52:22 +01:00
|
|
|
bt_stop();
|
|
|
|
bt_conf_save(false);
|
2022-10-30 13:51:36 +01:00
|
|
|
} else if (sbyte == 0x01) {
|
2022-10-30 14:52:22 +01:00
|
|
|
bt_start();
|
|
|
|
bt_conf_save(true);
|
2022-10-30 13:51:36 +01:00
|
|
|
} else if (sbyte == 0x02) {
|
|
|
|
bt_enable_pairing();
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-01 21:11:41 +01:00
|
|
|
} else if (command == CMD_DISP_INT) {
|
|
|
|
#if HAS_DISPLAY
|
|
|
|
if (sbyte == FESC) {
|
|
|
|
ESCAPE = true;
|
|
|
|
} else {
|
|
|
|
if (ESCAPE) {
|
|
|
|
if (sbyte == TFEND) sbyte = FEND;
|
|
|
|
if (sbyte == TFESC) sbyte = FESC;
|
|
|
|
ESCAPE = false;
|
|
|
|
}
|
|
|
|
display_intensity = sbyte;
|
2023-05-03 14:05:49 +02:00
|
|
|
di_conf_save(display_intensity);
|
2022-11-01 21:11:41 +01:00
|
|
|
}
|
|
|
|
|
2023-09-19 18:32:29 +02:00
|
|
|
#endif
|
|
|
|
} else if (command == CMD_DISP_ADDR) {
|
|
|
|
#if HAS_DISPLAY
|
|
|
|
if (sbyte == FESC) {
|
|
|
|
ESCAPE = true;
|
|
|
|
} else {
|
|
|
|
if (ESCAPE) {
|
|
|
|
if (sbyte == TFEND) sbyte = FEND;
|
|
|
|
if (sbyte == TFESC) sbyte = FESC;
|
|
|
|
ESCAPE = false;
|
|
|
|
}
|
|
|
|
display_addr = sbyte;
|
|
|
|
da_conf_save(display_addr);
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:11:41 +01:00
|
|
|
#endif
|
2018-04-05 18:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 21:12:33 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32
|
|
|
|
portMUX_TYPE update_lock = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
#endif
|
|
|
|
|
2018-04-26 15:52:43 +02:00
|
|
|
void updateModemStatus() {
|
2022-01-14 21:12:33 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32
|
|
|
|
portENTER_CRITICAL(&update_lock);
|
2024-01-19 11:08:55 +01:00
|
|
|
#elif MCU_VARIANT == MCU_NRF52
|
|
|
|
portENTER_CRITICAL();
|
2022-01-14 21:12:33 +01:00
|
|
|
#endif
|
|
|
|
|
2024-02-09 21:46:39 +01:00
|
|
|
uint8_t status = LoRa->modemStatus();
|
|
|
|
current_rssi = LoRa->currentRssi();
|
2018-04-26 15:52:43 +02:00
|
|
|
last_status_update = millis();
|
2022-01-14 21:12:33 +01:00
|
|
|
|
|
|
|
#if MCU_VARIANT == MCU_ESP32
|
|
|
|
portEXIT_CRITICAL(&update_lock);
|
2024-01-19 11:08:55 +01:00
|
|
|
#elif MCU_VARIANT == MCU_NRF52
|
|
|
|
portEXIT_CRITICAL();
|
2022-01-14 21:12:33 +01:00
|
|
|
#endif
|
|
|
|
|
2023-12-10 22:40:06 +01:00
|
|
|
if ((status & SIG_DETECT) == SIG_DETECT) { stat_signal_detected = true; } else { stat_signal_detected = false; }
|
|
|
|
if ((status & SIG_SYNCED) == SIG_SYNCED) { stat_signal_synced = true; } else { stat_signal_synced = false; }
|
|
|
|
if ((status & RX_ONGOING) == RX_ONGOING) { stat_rx_ongoing = true; } else { stat_rx_ongoing = false; }
|
2018-04-26 15:52:43 +02:00
|
|
|
|
2023-12-10 22:40:06 +01:00
|
|
|
// if (stat_signal_detected || stat_signal_synced || stat_rx_ongoing) {
|
|
|
|
if (stat_signal_detected || stat_signal_synced) {
|
2023-09-17 16:23:35 +02:00
|
|
|
if (stat_rx_ongoing) {
|
|
|
|
if (dcd_count < dcd_threshold) {
|
|
|
|
dcd_count++;
|
|
|
|
} else {
|
|
|
|
last_dcd = last_status_update;
|
|
|
|
dcd_led = true;
|
|
|
|
dcd = true;
|
|
|
|
}
|
2018-04-26 15:52:43 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-09-17 16:23:35 +02:00
|
|
|
#define DCD_LED_STEP_D 3
|
|
|
|
if (dcd_count == 0) {
|
|
|
|
dcd_led = false;
|
|
|
|
} else if (dcd_count > DCD_LED_STEP_D) {
|
|
|
|
dcd_count -= DCD_LED_STEP_D;
|
2018-04-26 15:52:43 +02:00
|
|
|
} else {
|
2023-09-17 16:23:35 +02:00
|
|
|
dcd_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_status_update > last_dcd+csma_slot_ms) {
|
|
|
|
dcd = false;
|
2018-04-26 15:52:43 +02:00
|
|
|
dcd_led = false;
|
2023-09-17 16:23:35 +02:00
|
|
|
dcd_count = 0;
|
2018-04-26 15:52:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dcd_led) {
|
|
|
|
led_rx_on();
|
|
|
|
} else {
|
2023-09-13 20:05:05 +02:00
|
|
|
if (airtime_lock) {
|
|
|
|
led_indicate_airtime_lock();
|
|
|
|
} else {
|
|
|
|
led_rx_off();
|
|
|
|
}
|
2018-04-26 15:52:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkModemStatus() {
|
|
|
|
if (millis()-last_status_update >= status_interval_ms) {
|
|
|
|
updateModemStatus();
|
2023-09-12 14:48:47 +02:00
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2023-09-12 14:48:47 +02:00
|
|
|
util_samples[dcd_sample] = dcd;
|
|
|
|
dcd_sample = (dcd_sample+1)%DCD_SAMPLES;
|
|
|
|
if (dcd_sample % UTIL_UPDATE_INTERVAL == 0) {
|
|
|
|
int util_count = 0;
|
|
|
|
for (int ui = 0; ui < DCD_SAMPLES; ui++) {
|
|
|
|
if (util_samples[ui]) util_count++;
|
|
|
|
}
|
|
|
|
local_channel_util = (float)util_count / (float)DCD_SAMPLES;
|
|
|
|
total_channel_util = local_channel_util + airtime;
|
|
|
|
if (total_channel_util > 1.0) total_channel_util = 1.0;
|
|
|
|
|
2023-09-13 20:05:05 +02:00
|
|
|
int16_t cb = current_airtime_bin();
|
|
|
|
uint16_t nb = cb+1; if (nb == AIRTIME_BINS) { nb = 0; }
|
|
|
|
if (total_channel_util > longterm_bins[cb]) longterm_bins[cb] = total_channel_util;
|
|
|
|
longterm_bins[nb] = 0.0;
|
2023-09-12 21:51:51 +02:00
|
|
|
|
2023-09-12 14:48:47 +02:00
|
|
|
update_airtime();
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-26 15:52:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:11:41 +01:00
|
|
|
void validate_status() {
|
2022-01-21 20:43:29 +01:00
|
|
|
#if MCU_VARIANT == MCU_1284P
|
|
|
|
uint8_t boot_flags = OPTIBOOT_MCUSR;
|
|
|
|
uint8_t F_POR = PORF;
|
|
|
|
uint8_t F_BOR = BORF;
|
|
|
|
uint8_t F_WDR = WDRF;
|
|
|
|
#elif MCU_VARIANT == MCU_2560
|
2022-01-09 23:40:30 +01:00
|
|
|
uint8_t boot_flags = OPTIBOOT_MCUSR;
|
2022-01-21 20:43:29 +01:00
|
|
|
if (boot_flags == 0x00) boot_flags = 0x03;
|
2022-01-09 23:40:30 +01:00
|
|
|
uint8_t F_POR = PORF;
|
|
|
|
uint8_t F_BOR = BORF;
|
|
|
|
uint8_t F_WDR = WDRF;
|
|
|
|
#elif MCU_VARIANT == MCU_ESP32
|
|
|
|
// TODO: Get ESP32 boot flags
|
|
|
|
uint8_t boot_flags = 0x02;
|
|
|
|
uint8_t F_POR = 0x00;
|
|
|
|
uint8_t F_BOR = 0x00;
|
|
|
|
uint8_t F_WDR = 0x01;
|
2024-01-19 11:08:55 +01:00
|
|
|
#elif MCU_VARIANT == MCU_NRF52
|
|
|
|
// TODO: Get NRF52 boot flags
|
|
|
|
uint8_t boot_flags = 0x02;
|
|
|
|
uint8_t F_POR = 0x00;
|
|
|
|
uint8_t F_BOR = 0x00;
|
|
|
|
uint8_t F_WDR = 0x01;
|
2022-01-09 23:40:30 +01:00
|
|
|
#endif
|
|
|
|
|
2022-11-01 21:11:41 +01:00
|
|
|
if (hw_ready || device_init_done) {
|
|
|
|
hw_ready = false;
|
|
|
|
Serial.write("Error, invalid hardware check state\r\n");
|
|
|
|
#if HAS_DISPLAY
|
2022-11-02 19:02:22 +01:00
|
|
|
if (disp_ready) {
|
|
|
|
device_init_done = true;
|
|
|
|
update_display();
|
|
|
|
}
|
2022-11-01 21:11:41 +01:00
|
|
|
#endif
|
|
|
|
led_indicate_boot_error();
|
|
|
|
}
|
|
|
|
|
2022-01-09 23:40:30 +01:00
|
|
|
if (boot_flags & (1<<F_POR)) {
|
2021-12-26 11:27:32 +01:00
|
|
|
boot_vector = START_FROM_POWERON;
|
2022-01-09 23:40:30 +01:00
|
|
|
} else if (boot_flags & (1<<F_BOR)) {
|
2021-12-26 11:27:32 +01:00
|
|
|
boot_vector = START_FROM_BROWNOUT;
|
2022-01-09 23:40:30 +01:00
|
|
|
} else if (boot_flags & (1<<F_WDR)) {
|
2021-12-26 11:27:32 +01:00
|
|
|
boot_vector = START_FROM_BOOTLOADER;
|
|
|
|
} else {
|
|
|
|
Serial.write("Error, indeterminate boot vector\r\n");
|
2022-10-29 16:44:49 +02:00
|
|
|
#if HAS_DISPLAY
|
2022-11-02 19:02:22 +01:00
|
|
|
if (disp_ready) {
|
|
|
|
device_init_done = true;
|
|
|
|
update_display();
|
|
|
|
}
|
2022-10-29 16:44:49 +02:00
|
|
|
#endif
|
2021-12-26 11:27:32 +01:00
|
|
|
led_indicate_boot_error();
|
|
|
|
}
|
|
|
|
|
2022-01-09 23:40:30 +01:00
|
|
|
if (boot_vector == START_FROM_BOOTLOADER || boot_vector == START_FROM_POWERON) {
|
2021-12-26 11:27:32 +01:00
|
|
|
if (eeprom_lock_set()) {
|
|
|
|
if (eeprom_product_valid() && eeprom_model_valid() && eeprom_hwrev_valid()) {
|
|
|
|
if (eeprom_checksum_valid()) {
|
2022-11-03 00:58:45 +01:00
|
|
|
eeprom_ok = true;
|
2024-01-19 11:08:55 +01:00
|
|
|
if (modem_installed) {
|
2023-01-07 16:35:07 +01:00
|
|
|
#if PLATFORM == PLATFORM_ESP32
|
|
|
|
if (device_init()) {
|
|
|
|
hw_ready = true;
|
|
|
|
} else {
|
2024-04-23 00:52:57 +02:00
|
|
|
hw_ready = false;
|
2023-01-07 16:35:07 +01:00
|
|
|
}
|
|
|
|
#else
|
2022-11-01 21:11:41 +01:00
|
|
|
hw_ready = true;
|
2023-01-07 16:35:07 +01:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
hw_ready = false;
|
2024-01-19 11:08:55 +01:00
|
|
|
Serial.write("No valid radio module found\r\n");
|
2023-01-07 16:35:07 +01:00
|
|
|
#if HAS_DISPLAY
|
|
|
|
if (disp_ready) {
|
|
|
|
device_init_done = true;
|
|
|
|
update_display();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:11:41 +01:00
|
|
|
if (hw_ready && eeprom_have_conf()) {
|
2021-12-26 11:27:32 +01:00
|
|
|
eeprom_conf_load();
|
|
|
|
op_mode = MODE_TNC;
|
|
|
|
startRadio();
|
|
|
|
}
|
2022-11-03 00:58:45 +01:00
|
|
|
} else {
|
|
|
|
hw_ready = false;
|
|
|
|
#if HAS_DISPLAY
|
|
|
|
if (disp_ready) {
|
|
|
|
device_init_done = true;
|
|
|
|
update_display();
|
|
|
|
}
|
|
|
|
#endif
|
2018-06-20 16:32:30 +02:00
|
|
|
}
|
2021-12-26 11:27:32 +01:00
|
|
|
} else {
|
|
|
|
hw_ready = false;
|
2022-11-03 00:58:45 +01:00
|
|
|
#if HAS_DISPLAY
|
|
|
|
if (disp_ready) {
|
|
|
|
device_init_done = true;
|
|
|
|
update_display();
|
|
|
|
}
|
|
|
|
#endif
|
2018-06-20 08:45:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hw_ready = false;
|
2022-11-03 00:58:45 +01:00
|
|
|
#if HAS_DISPLAY
|
|
|
|
if (disp_ready) {
|
|
|
|
device_init_done = true;
|
|
|
|
update_display();
|
|
|
|
}
|
|
|
|
#endif
|
2018-06-20 08:45:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hw_ready = false;
|
2021-12-26 11:27:32 +01:00
|
|
|
Serial.write("Error, incorrect boot vector\r\n");
|
2022-10-29 16:44:49 +02:00
|
|
|
#if HAS_DISPLAY
|
2022-11-02 19:02:22 +01:00
|
|
|
if (disp_ready) {
|
|
|
|
device_init_done = true;
|
|
|
|
update_display();
|
|
|
|
}
|
2022-10-29 16:44:49 +02:00
|
|
|
#endif
|
2021-12-26 11:27:32 +01:00
|
|
|
led_indicate_boot_error();
|
2018-06-20 08:45:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2023-09-15 09:10:46 +02:00
|
|
|
#define _e 2.71828183
|
|
|
|
#define _S 10.0
|
|
|
|
float csma_slope(float u) { return (pow(_e,_S*u-_S/2.0))/(pow(_e,_S*u-_S/2.0)+1.0); }
|
|
|
|
void update_csma_p() {
|
|
|
|
csma_p = (uint8_t)((1.0-(csma_p_min+(csma_p_max-csma_p_min)*csma_slope(airtime)))*255.0);
|
2023-09-14 22:32:26 +02:00
|
|
|
}
|
2023-09-15 09:10:46 +02:00
|
|
|
#endif
|
2023-09-14 22:32:26 +02:00
|
|
|
|
2018-04-05 18:10:42 +02:00
|
|
|
void loop() {
|
2018-04-26 15:52:43 +02:00
|
|
|
if (radio_online) {
|
2022-01-09 23:40:30 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32
|
2022-01-10 22:14:30 +01:00
|
|
|
if (packet_ready) {
|
2022-01-14 21:41:10 +01:00
|
|
|
portENTER_CRITICAL(&update_lock);
|
2024-02-09 21:46:39 +01:00
|
|
|
last_rssi = LoRa->packetRssi();
|
|
|
|
last_snr_raw = LoRa->packetSnrRaw();
|
2022-01-14 21:41:10 +01:00
|
|
|
portEXIT_CRITICAL(&update_lock);
|
|
|
|
kiss_indicate_stat_rssi();
|
|
|
|
kiss_indicate_stat_snr();
|
2022-01-10 22:14:30 +01:00
|
|
|
kiss_write_packet();
|
2022-01-09 23:40:30 +01:00
|
|
|
}
|
2023-09-13 20:05:05 +02:00
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
airtime_lock = false;
|
|
|
|
if (st_airtime_limit != 0.0 && airtime >= st_airtime_limit) airtime_lock = true;
|
|
|
|
if (lt_airtime_limit != 0.0 && longterm_airtime >= lt_airtime_limit) airtime_lock = true;
|
|
|
|
|
|
|
|
#elif MCU_VARIANT == MCU_NRF52
|
|
|
|
if (packet_ready) {
|
|
|
|
portENTER_CRITICAL();
|
2024-02-09 21:46:39 +01:00
|
|
|
last_rssi = LoRa->packetRssi();
|
|
|
|
last_snr_raw = LoRa->packetSnrRaw();
|
2024-01-19 11:08:55 +01:00
|
|
|
portEXIT_CRITICAL();
|
|
|
|
kiss_indicate_stat_rssi();
|
|
|
|
kiss_indicate_stat_snr();
|
|
|
|
kiss_write_packet();
|
|
|
|
}
|
|
|
|
|
2023-09-13 20:05:05 +02:00
|
|
|
airtime_lock = false;
|
|
|
|
if (st_airtime_limit != 0.0 && airtime >= st_airtime_limit) airtime_lock = true;
|
|
|
|
if (lt_airtime_limit != 0.0 && longterm_airtime >= lt_airtime_limit) airtime_lock = true;
|
2022-01-09 23:40:30 +01:00
|
|
|
#endif
|
|
|
|
|
2023-09-17 16:23:35 +02:00
|
|
|
checkModemStatus();
|
2023-09-13 20:05:05 +02:00
|
|
|
if (!airtime_lock) {
|
|
|
|
if (queue_height > 0) {
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2023-09-17 16:23:35 +02:00
|
|
|
long check_time = millis();
|
|
|
|
if (check_time > post_tx_yield_timeout) {
|
|
|
|
if (dcd_waiting && (check_time >= dcd_wait_until)) { dcd_waiting = false; }
|
|
|
|
if (!dcd_waiting) {
|
|
|
|
for (uint8_t dcd_i = 0; dcd_i < dcd_threshold*2; dcd_i++) {
|
|
|
|
delay(STATUS_INTERVAL_MS); updateModemStatus();
|
|
|
|
}
|
2020-05-28 22:18:19 +02:00
|
|
|
|
2023-09-17 16:23:35 +02:00
|
|
|
if (!dcd) {
|
2024-02-11 18:30:13 +01:00
|
|
|
uint8_t csma_r = (uint8_t)random(256);
|
2023-09-17 16:23:35 +02:00
|
|
|
if (csma_p >= csma_r) {
|
|
|
|
flushQueue();
|
|
|
|
} else {
|
|
|
|
dcd_waiting = true;
|
|
|
|
dcd_wait_until = millis()+csma_slot_ms;
|
|
|
|
}
|
2023-09-14 22:32:26 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-13 20:05:05 +02:00
|
|
|
}
|
2023-09-14 22:32:26 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
if (!dcd_waiting) updateModemStatus();
|
2023-09-12 14:48:47 +02:00
|
|
|
|
2023-09-14 22:32:26 +02:00
|
|
|
if (!dcd && !dcd_led) {
|
|
|
|
if (dcd_waiting) delay(lora_rx_turnaround_ms);
|
|
|
|
|
|
|
|
updateModemStatus();
|
|
|
|
|
|
|
|
if (!dcd) {
|
|
|
|
dcd_waiting = false;
|
|
|
|
flushQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
dcd_waiting = true;
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-26 15:52:43 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-27 10:22:44 +02:00
|
|
|
|
2018-06-18 22:30:24 +02:00
|
|
|
} else {
|
2018-06-20 08:45:11 +02:00
|
|
|
if (hw_ready) {
|
2023-01-06 22:29:23 +01:00
|
|
|
if (console_active) {
|
2023-01-11 09:42:11 +01:00
|
|
|
#if HAS_CONSOLE
|
|
|
|
console_loop();
|
|
|
|
#endif
|
2023-01-06 22:29:23 +01:00
|
|
|
} else {
|
2023-01-10 14:27:03 +01:00
|
|
|
led_indicate_standby();
|
2023-01-06 22:29:23 +01:00
|
|
|
}
|
2018-06-20 08:45:11 +02:00
|
|
|
} else {
|
2022-11-03 00:58:45 +01:00
|
|
|
|
2018-06-20 08:45:11 +02:00
|
|
|
led_indicate_not_ready();
|
|
|
|
stopRadio();
|
|
|
|
}
|
2018-04-26 15:52:43 +02:00
|
|
|
}
|
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
2023-01-07 16:35:07 +01:00
|
|
|
buffer_serial();
|
|
|
|
if (!fifo_isempty(&serialFIFO)) serial_poll();
|
2022-01-10 22:14:30 +01:00
|
|
|
#else
|
2022-01-21 20:43:29 +01:00
|
|
|
if (!fifo_isempty_locked(&serialFIFO)) serial_poll();
|
2022-01-09 23:40:30 +01:00
|
|
|
#endif
|
2022-10-29 00:53:39 +02:00
|
|
|
|
|
|
|
#if HAS_DISPLAY
|
2022-10-29 16:44:49 +02:00
|
|
|
if (disp_ready) update_display();
|
2022-10-29 00:53:39 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAS_PMU
|
2022-10-29 16:44:49 +02:00
|
|
|
if (pmu_ready) update_pmu();
|
2022-10-29 00:53:39 +02:00
|
|
|
#endif
|
2022-10-30 13:51:36 +01:00
|
|
|
|
2024-04-23 00:52:57 +02:00
|
|
|
#if HAS_BLUETOOTH || HAS_BLE == true
|
2023-01-07 16:35:07 +01:00
|
|
|
if (!console_active && bt_ready) update_bt();
|
2022-10-30 13:51:36 +01:00
|
|
|
#endif
|
2024-05-02 01:20:33 +02:00
|
|
|
|
|
|
|
#if HAS_INPUT
|
|
|
|
input_read();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void sleep_now() {
|
|
|
|
#if HAS_SLEEP == true
|
2024-05-18 01:07:38 +02:00
|
|
|
#if BOARD_MODEL == BOARD_RNODE_NG_22
|
|
|
|
display_intensity = 0;
|
|
|
|
update_display(true);
|
|
|
|
#endif
|
|
|
|
#if PIN_DISP_SLEEP >= 0
|
|
|
|
pinMode(PIN_DISP_SLEEP, OUTPUT);
|
|
|
|
digitalWrite(PIN_DISP_SLEEP, DISP_SLEEP_LEVEL);
|
|
|
|
#endif
|
2024-05-02 01:20:33 +02:00
|
|
|
esp_sleep_enable_ext0_wakeup(PIN_WAKEUP, WAKEUP_LEVEL);
|
|
|
|
esp_deep_sleep_start();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void button_event(uint8_t event, unsigned long duration) {
|
|
|
|
if (duration > 2000) {
|
|
|
|
sleep_now();
|
|
|
|
}
|
2020-05-28 22:18:19 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 14:58:10 +02:00
|
|
|
volatile bool serial_polling = false;
|
2020-05-28 22:18:19 +02:00
|
|
|
void serial_poll() {
|
2020-05-29 14:58:10 +02:00
|
|
|
serial_polling = true;
|
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
|
2020-05-28 22:18:19 +02:00
|
|
|
while (!fifo_isempty_locked(&serialFIFO)) {
|
2022-01-09 23:40:30 +01:00
|
|
|
#else
|
|
|
|
while (!fifo_isempty(&serialFIFO)) {
|
|
|
|
#endif
|
2020-05-29 14:58:10 +02:00
|
|
|
char sbyte = fifo_pop(&serialFIFO);
|
2018-04-05 18:10:42 +02:00
|
|
|
serialCallback(sbyte);
|
2020-05-28 22:18:19 +02:00
|
|
|
}
|
2020-05-29 14:58:10 +02:00
|
|
|
|
|
|
|
serial_polling = false;
|
2020-05-28 22:18:19 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 19:33:47 +01:00
|
|
|
#if MCU_VARIANT != MCU_ESP32
|
|
|
|
#define MAX_CYCLES 20
|
|
|
|
#else
|
2022-01-11 02:54:32 +01:00
|
|
|
#define MAX_CYCLES 10
|
2022-01-10 19:33:47 +01:00
|
|
|
#endif
|
2020-05-28 22:18:19 +02:00
|
|
|
void buffer_serial() {
|
2020-05-29 14:58:10 +02:00
|
|
|
if (!serial_buffering) {
|
|
|
|
serial_buffering = true;
|
|
|
|
|
|
|
|
uint8_t c = 0;
|
2022-10-30 18:35:53 +01:00
|
|
|
|
2024-04-23 00:52:57 +02:00
|
|
|
#if HAS_BLUETOOTH || HAS_BLE == true
|
2022-10-30 18:35:53 +01:00
|
|
|
while (
|
|
|
|
c < MAX_CYCLES &&
|
|
|
|
( (bt_state != BT_STATE_CONNECTED && Serial.available()) || (bt_state == BT_STATE_CONNECTED && SerialBT.available()) )
|
|
|
|
)
|
|
|
|
#else
|
|
|
|
while (c < MAX_CYCLES && Serial.available())
|
|
|
|
#endif
|
|
|
|
{
|
2020-05-29 14:58:10 +02:00
|
|
|
c++;
|
|
|
|
|
2024-01-19 11:08:55 +01:00
|
|
|
#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
|
2022-01-09 23:40:30 +01:00
|
|
|
if (!fifo_isfull_locked(&serialFIFO)) {
|
|
|
|
fifo_push_locked(&serialFIFO, Serial.read());
|
|
|
|
}
|
2024-04-23 00:52:57 +02:00
|
|
|
#elif HAS_BLUETOOTH || HAS_BLE == true
|
2024-01-21 10:05:02 +01:00
|
|
|
if (bt_state == BT_STATE_CONNECTED) {
|
2022-10-30 18:35:53 +01:00
|
|
|
if (!fifo_isfull(&serialFIFO)) {
|
|
|
|
fifo_push(&serialFIFO, SerialBT.read());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!fifo_isfull(&serialFIFO)) {
|
|
|
|
fifo_push(&serialFIFO, Serial.read());
|
|
|
|
}
|
2022-01-09 23:40:30 +01:00
|
|
|
}
|
2024-01-21 10:05:02 +01:00
|
|
|
#else
|
|
|
|
if (!fifo_isfull(&serialFIFO)) {
|
|
|
|
fifo_push(&serialFIFO, Serial.read());
|
|
|
|
}
|
2022-01-09 23:40:30 +01:00
|
|
|
#endif
|
2018-06-27 10:22:44 +02:00
|
|
|
}
|
2020-05-29 14:58:10 +02:00
|
|
|
|
|
|
|
serial_buffering = false;
|
2018-04-05 18:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 22:18:19 +02:00
|
|
|
|
2020-05-29 14:58:10 +02:00
|
|
|
void serial_interrupt_init() {
|
2022-01-09 23:40:30 +01:00
|
|
|
#if MCU_VARIANT == MCU_1284P
|
|
|
|
TCCR3A = 0;
|
|
|
|
TCCR3B = _BV(CS10) |
|
|
|
|
_BV(WGM33)|
|
|
|
|
_BV(WGM32);
|
|
|
|
|
|
|
|
// Buffer incoming frames every 1ms
|
|
|
|
ICR3 = 16000;
|
|
|
|
|
|
|
|
TIMSK3 = _BV(ICIE3);
|
2020-05-28 22:18:19 +02:00
|
|
|
|
2022-01-09 23:40:30 +01:00
|
|
|
#elif MCU_VARIANT == MCU_2560
|
|
|
|
// TODO: This should probably be updated for
|
|
|
|
// atmega2560 support. Might be source of
|
|
|
|
// reported issues from snh.
|
|
|
|
TCCR3A = 0;
|
|
|
|
TCCR3B = _BV(CS10) |
|
|
|
|
_BV(WGM33)|
|
|
|
|
_BV(WGM32);
|
|
|
|
|
|
|
|
// Buffer incoming frames every 1ms
|
|
|
|
ICR3 = 16000;
|
|
|
|
|
|
|
|
TIMSK3 = _BV(ICIE3);
|
|
|
|
|
|
|
|
#elif MCU_VARIANT == MCU_ESP32
|
|
|
|
// No interrupt-based polling on ESP32
|
|
|
|
#endif
|
2020-05-28 22:18:19 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-09 23:40:30 +01:00
|
|
|
#if MCU_VARIANT == MCU_1284P || MCU_VARIANT == MCU_2560
|
|
|
|
ISR(TIMER3_CAPT_vect) {
|
|
|
|
buffer_serial();
|
|
|
|
}
|
2022-01-13 22:18:18 +01:00
|
|
|
#endif
|