Skip to content

Commit

Permalink
Radio health check status task added
Browse files Browse the repository at this point in the history
  • Loading branch information
luk6xff committed Nov 25, 2020
1 parent bc6ebee commit ad54e8e
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 21 deletions.
12 changes: 8 additions & 4 deletions esp32-app/lib/LORA/lora.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,14 @@ bool lora_init(lora *const dev)
//-----------------------------------------------------------------------------
void lora_end(lora *const dev)
{
// put in sleep mode
lora_sleep(dev);
// put in sleep mode
lora_sleep(dev);
// Perform Reset
lora_reset(dev);

// Deinitialize IO interfaces
lora_io_deinit(dev);
lora_ioirq_deinit(dev);
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -779,7 +785,5 @@ ISR_PREFIX void on_dio0_irq(void *ctx)
{
lora *const dev = (lora*)ctx;
// Disable interrupts and switch into idle mode
lora_ioirq_deinit(dev);
lora_idle(dev);
lora_handle_dio0_rise(dev);
}
9 changes: 6 additions & 3 deletions esp32-app/lib/LORA/platform/arduino/lora-arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ bool lora_arduino_init(lora *const dev, lora_arduino *const arduino_dev)
//-----------------------------------------------------------------------------
void lora_arduino_deinit(lora *const dev)
{
// IO
lora_io_deinit(dev);
lora_end(dev);
}

//-----------------------------------------------------------------------------
Expand All @@ -42,7 +41,11 @@ void lora_io_init(lora *const dev)
//-----------------------------------------------------------------------------
void lora_io_deinit(lora *const dev)
{
// Empty
// Get arduino platform data
lora_arduino *const pd = (lora_arduino*)dev->platform_dev;
// Init SPI
pd->spi->end();
lora_delay_ms(10);
}

//-----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion esp32-app/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ build_flags =
;-DCONFIG_INT_WDT=0

; Upload port
upload_port = /dev/ttyUSB0
upload_port = /dev/ttyUSB1
;upload_port = COM38

; Extra scripts
Expand Down
4 changes: 2 additions & 2 deletions esp32-app/src/App/i18n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ const i18n_map I18N::k_i18n =
{ "Weather", "Pogoda" }, // M_WEATHER_NAME
{ "Temperature", "Temperatura" }, // M_WEATHER_TEMP
{ "Temp", "T" }, // M_WEATHER_TEMP_SHORT
{ "Today", "Dzisiaj" }, // M_WEATHER_TODAY
{ "Today", "Dziś" }, // M_WEATHER_TODAY
{ "Tommorow", "Jutro" }, // M_WEATHER_TOMM
{ "Now", "Teraz" }, // M_WEATHER_NOW
// Sensor
{ "Sensor", "PomiarZewn" }, // M_SENSOR_NAME
{ "Sensor", "PomiarZewn" }, // M_SENSOR_NAME
{ "Temp", "Temp" }, // M_SENSOR_TEMP
{ "Pressure", "Ciśn" }, // M_SENSOR_PRESS
{ "Humidity", "Wilg" }, // M_SENSOR_HUMID
Expand Down
23 changes: 20 additions & 3 deletions esp32-app/src/Radio/radio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,25 @@ void Radio::sendResponseToSensor()

// Switch to RX mode
lora_receive(&dev, 0);
lora_ioirq_init(&dev);
}

//-----------------------------------------------------------------------------
void Radio::restart()
{
// Deinitilaize radio
lora_arduino_deinit(&dev);
lora_delay_ms(100);

// Initialize it back
while (!lora_arduino_init(&dev, &arduino_dev))
{
utils::dbg("LORA Radio cannot be detected!, check your connections.");
lora_delay_ms(1000);
}

lora_on_receive(&dev, &Radio::on_rx_done);
// Switch into RX mode
lora_receive(&dev, 0);
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -189,9 +207,8 @@ void Radio::on_rx_done(void*ctx, int packetSize)
}

err:
// Enable interrupts and switch into rx mode
// Switch back into rx mode
lora_receive(dev, 0);
lora_ioirq_init(dev);
}

//-----------------------------------------------------------------------------
Expand Down
6 changes: 2 additions & 4 deletions esp32-app/src/Radio/radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ class Radio final
explicit Radio(RadioSensorSettings& radioSensorCfg);
void sendResponseToSensor();

void restart();

static radio_msg_sensor_frame_status parse_incoming_msg_sensor(uint8_t *payload, uint16_t size);

private:
static uint16_t radio_msg_frame_checksum(const uint8_t *data, const uint8_t data_len);

// Callbacks
static void on_tx_done(void *args);
//static void on_rx_done(void *args, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
static void on_rx_done(void *ctx, int packetSize);
static void on_tx_timeout(void *args);
static void on_rx_timeout(void *args);
Expand All @@ -97,9 +98,6 @@ class Radio final
private:
RadioSensorSettings& cfg;
SPIClass spi;
// RadioEvents_t events;
// sx1278 dev;
// sx1278_arduino arduino_dev;
lora dev;
lora_arduino arduino_dev;
/**
Expand Down
45 changes: 43 additions & 2 deletions esp32-app/src/Radio/radio_sensor_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,29 @@
RadioSensorTask::RadioSensorTask(RadioSensorSettings& radioSensorCfg)
: Task("RadioSensorTask", RADIO_SENSOR_TASK_STACK_SIZE, RADIO_SENSOR_TASK_PRIORITY, 1)
, m_radioSensorCfg(radioSensorCfg)
, m_radioSensor(m_radioSensorCfg)
, m_radioSensorQ(nullptr)
{
m_radioSensorQ = xQueueCreate(3, sizeof(RadioSensorData));
if (!m_radioSensorQ)
{
utils::err("%s m_radioSensorQ has not been created!.", MODULE_NAME);
}

// Create radio health state task
m_radioHealthStateTask.radioHandle = &m_radioSensor;
m_radioHealthStateTask.resetTimeoutSecs = m_radioSensorCfg.update_data_interval * 4; // When no radio msg received in 4x update interval, reset the radio
m_radioHealthStateTask.lastRadioMsgReceivedTimeMs = 0;
BaseType_t ret = xTaskCreate(healtStateCheckCb,
"RHSTask",
4096,
&m_radioHealthStateTask,
1,
&m_radioHealthStateTask.task);
if (ret != pdPASS)
{
utils::err("%s m_radioHealthStateTask start failed", MODULE_NAME);
}
}

//------------------------------------------------------------------------------
Expand All @@ -31,7 +47,6 @@ const QueueHandle_t& RadioSensorTask::getRadioSensorQ()
void RadioSensorTask::run()
{
const TickType_t sleepTime = (1000 / portTICK_PERIOD_MS);
Radio radioSensor(m_radioSensorCfg);
radio_msg_queue_data msg;
for(;;)
{
Expand All @@ -47,7 +62,7 @@ void RadioSensorTask::run()
utils::dbg("SNR: %f", msg.snr);
radio_msg_sensor_frame_status ret = Radio::parse_incoming_msg_sensor((uint8_t*)&(msg.frame), sizeof(msg.frame));
// Send response to the sensor
radioSensor.sendResponseToSensor();
m_radioSensor.sendResponseToSensor();
if (ret == MSG_NO_ERROR || ret == MSG_BATT_LOW)
{
RadioSensorData data =
Expand All @@ -59,6 +74,11 @@ void RadioSensorTask::run()
};
pushRadioSensorMsg(data);
}
{
rtos::LockGuard<rtos::Mutex> lock(m_radioHealthStateTask.mtx);
m_radioHealthStateTask.lastRadioMsgReceivedTimeMs = millis();
}

}
vTaskDelay(sleepTime);
}
Expand Down Expand Up @@ -87,3 +107,24 @@ bool RadioSensorTask::pushRadioSensorMsg(const RadioSensorData& data)
}

//------------------------------------------------------------------------------
void RadioSensorTask::healtStateCheckCb(void *arg)
{
RadioHealthStateTask *rhst = static_cast<RadioHealthStateTask*>(arg);
const TickType_t timeoutMs = (10000 / portTICK_PERIOD_MS);
for(;;)
{
{
rtos::LockGuard<rtos::Mutex> lock(rhst->mtx);
if (((millis() - rhst->lastRadioMsgReceivedTimeMs) / 1000) > rhst->resetTimeoutSecs)
{
// No Frame received for too long, restart radio
utils::dbg("%s No Radio frame msg received for too long:[%d] seconds, restarting radio...", MODULE_NAME, rhst->resetTimeoutSecs);
rhst->radioHandle->restart();
rhst->lastRadioMsgReceivedTimeMs = 0;
}
}
vTaskDelay(timeoutMs);
}
}

//------------------------------------------------------------------------------
21 changes: 19 additions & 2 deletions esp32-app/src/Radio/radio_sensor_task.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "App/task.h"
#include "App/rtos_common.h"
#include "radio.h"

struct RadioSensorData
Expand All @@ -10,18 +12,33 @@ struct RadioSensorData
float humidity;
};


struct RadioHealthStateTask
{
TaskHandle_t task;
rtos::Mutex mtx;
Radio *radioHandle;
uint32_t resetTimeoutSecs;
uint32_t lastRadioMsgReceivedTimeMs;
};


class RadioSensorTask : public Task
{
public:
explicit RadioSensorTask(RadioSensorSettings& radioSensorCfg);

const QueueHandle_t& getRadioSensorQ();

static void healtStateCheckCb(void *arg);

private:
virtual void run() override;
bool pushRadioSensorMsg(const RadioSensorData& dt);

private:
RadioSensorSettings& m_radioSensorCfg;
Radio m_radioSensor;
QueueHandle_t m_radioSensorQ;
};
RadioHealthStateTask m_radioHealthStateTask;
};

0 comments on commit ad54e8e

Please sign in to comment.