This commit is contained in:
2025-04-28 00:07:43 +02:00
parent 0c71409c30
commit 8b9e72ef71
9 changed files with 266 additions and 212 deletions

View File

@@ -6,11 +6,13 @@
#include "string.h"
#include "esp_timer.h"
#include "esp_rom_crc.h"
#include <hw/buscfg.h>
#include "sensors.h"
#define TAG "LoRa"
#define ACK_TIMEOUT_MS 500 // Wait 300ms for ACK
#define MAX_RETRIES 3
#define ACK_TIMEOUT_MS 250 // Wait 300ms for ACK
#define MAX_RETRIES 1
uint32_t packetIndexTX = 0;
uint32_t packetIndexRX = 0;
@@ -44,10 +46,10 @@ void setup_lora(void)
}
}
uint8_t spreadingFactor = 7;
uint8_t spreadingFactor = 8;
uint8_t bandwidth = SX126X_LORA_BW_250_0;
uint8_t codingRate = SX126X_LORA_CR_4_8;
uint16_t preambleLength = 8;
uint16_t preambleLength = 4;
bool crcOn = true;
bool invertIrq = false;
@@ -65,6 +67,23 @@ static void prepare_downbound_packet(DownBoundPacket *packet, uint8_t type, uint
strcpy(packet->syncPhrase, "PlechDole");
}
static void send_packet_without_retries(uint8_t *data, uint16_t size)
{
if (xSemaphoreTake(loraRadioMutex, portMAX_DELAY) == pdTRUE)
{
if (!LoRaSend(data, size, SX126x_TXMODE_SYNC))
{
ESP_LOGW(TAG, "LoRaSend failed");
xSemaphoreGive(loraRadioMutex);
}
else
{
ESP_LOGI(TAG, "%d byte packet sent", size);
xSemaphoreGive(loraRadioMutex);
}
}
}
static void send_packet_with_retries(uint8_t *data, uint16_t size)
{
for (int retry = 0; retry <= MAX_RETRIES; retry++)
@@ -116,7 +135,7 @@ void prepare_and_send_telemetry(uint64_t missionTimer)
memcpy(bufOut + offset, &telemetryPacket, sizeof(telemetryPacket));
offset += sizeof(telemetryPacket);
send_packet_with_retries(bufOut, offset);
send_packet_without_retries(bufOut, offset);
packetReadiness = 0;
}
@@ -208,6 +227,7 @@ void process_uplink_packet(uint8_t *data, uint8_t len, uint64_t missionTimer)
{
SystemControlPacket sysCtrl;
memcpy(&sysCtrl, data + sizeof(UplinkPacket), sizeof(SystemControlPacket));
setPowerMode(sysCtrl.powerMode);
// TODO: Process sysCtrl
}
else
@@ -265,13 +285,18 @@ void lora_receive_task(void *pvParameters)
void lora_comms_task(void *pvParameters)
{
ESP_LOGI(TAG, "lora_comms_task started");
while (foundDevices[0] != 2) {
vTaskDelay(10);
}
// Initialize the semaphore for radio access (binary semaphore, 1 = available)
loraRadioMutex = xSemaphoreCreateMutex();
xSemaphoreGive(loraRadioMutex); // Set semaphore as available
const int64_t interval_us = 400000; // 400 ms
setup_lora();
xTaskCreate(
lora_receive_task,
@@ -291,6 +316,8 @@ void lora_comms_task(void *pvParameters)
prepare_and_send_telemetry(start_time);
}
const int64_t interval_us = ((powerMode == HIGH_POWER_MODE) ? 10000 : 10000000); // 10 ms or 10 000 ms
int64_t end_time = esp_timer_get_time();
int64_t elapsed = end_time - start_time;
@@ -299,50 +326,4 @@ void lora_comms_task(void *pvParameters)
vTaskDelay(pdMS_TO_TICKS((interval_us - elapsed) / 1000));
}
}
}
// void lora_comms_task(void *pvParameters)
// {
// const int64_t interval_us = 400000; // 100 ms
// int64_t start_time, end_time, elapsed;
// setup_lora();
// uint8_t bufIn[256];
// while (1)
// {
// start_time = esp_timer_get_time();
// if (packetReadiness)
// {
// ESP_LOGI(TAG, "Preparing telemetry");
// prepare_and_send_telemetry(start_time);
// }
// uint8_t rxLen = LoRaReceive(bufIn, sizeof(bufIn));
// if (rxLen > 0)
// {
// ESP_LOGI(TAG, "%d byte packet received", rxLen);
// process_uplink_packet(bufIn, rxLen, start_time);
// int8_t rssi, snr;
// GetPacketStatus(&rssi, &snr);
// ESP_LOGI(TAG, "rssi=%d[dBm], snr=%d[dB]", rssi, snr);
// }
// int lost = GetPacketLost();
// if (lost != 0)
// {
// ESP_LOGW(TAG, "%d packets lost", lost);
// }
// end_time = esp_timer_get_time();
// elapsed = end_time - start_time;
// if (elapsed < interval_us)
// {
// vTaskDelay(pdMS_TO_TICKS((interval_us - elapsed) / 1000));
// }
// }
// }
}