Merge development
This commit is contained in:
@@ -116,9 +116,8 @@ bool gatewayTransportInit() {
|
||||
(void)WiFi.begin(MY_ESP8266_SSID, MY_ESP8266_PASSWORD);
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
wait(500);
|
||||
MY_SERIALDEVICE.print(F("."));
|
||||
yield();
|
||||
}
|
||||
MY_SERIALDEVICE.print(F("IP: "));
|
||||
MY_SERIALDEVICE.println(WiFi.localIP());
|
||||
|
||||
@@ -88,7 +88,7 @@ bool reconnectMQTT() {
|
||||
bool gatewayTransportConnect() {
|
||||
#if defined(MY_GATEWAY_ESP8266)
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500); // delay calls yield
|
||||
wait(500);
|
||||
MY_SERIALDEVICE.print(".");
|
||||
}
|
||||
MY_SERIALDEVICE.print("IP: ");
|
||||
|
||||
@@ -219,7 +219,7 @@ uint16_t hwFreeMem() {
|
||||
|
||||
#ifdef MY_DEBUG
|
||||
void hwDebugPrint(const char *fmt, ... ) {
|
||||
char fmtBuffer[MY_DEBUG_BUFFER_SIZE];
|
||||
char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
|
||||
snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
|
||||
@@ -233,9 +233,9 @@ void hwDebugPrint(const char *fmt, ... ) {
|
||||
va_start (args, fmt );
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// Truncate message if this is gateway node
|
||||
vsnprintf_P(fmtBuffer, MY_GATEWAY_MAX_SEND_LENGTH, fmt, args);
|
||||
fmtBuffer[MY_GATEWAY_MAX_SEND_LENGTH-1] = '\n';
|
||||
fmtBuffer[MY_GATEWAY_MAX_SEND_LENGTH] = '\0';
|
||||
vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
|
||||
fmtBuffer[sizeof(fmtBuffer) - 2] = '\n';
|
||||
fmtBuffer[sizeof(fmtBuffer) - 1] = '\0';
|
||||
#else
|
||||
vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
|
||||
#endif
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
#endif
|
||||
|
||||
#define MY_SERIALDEVICE Serial
|
||||
#define MY_DEBUG_BUFFER_SIZE 300
|
||||
|
||||
#if defined __AVR_ATmega328P__
|
||||
#ifndef sleep_bod_disable
|
||||
@@ -73,21 +72,20 @@ do { \
|
||||
#define hwReboot() wdt_enable(WDTO_15MS); while (1)
|
||||
#define hwMillis() millis()
|
||||
#define hwRandomNumberInit() randomSeed(analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN))
|
||||
#define hwReadConfig(__pos) (eeprom_read_byte((uint8_t*)(__pos)))
|
||||
#define hwReadConfig(__pos) eeprom_read_byte((uint8_t*)(__pos))
|
||||
|
||||
#ifndef eeprom_update_byte
|
||||
#define hwWriteConfig(loc, val) if((uint8_t)(val) != eeprom_read_byte((uint8_t*)(loc))) { eeprom_write_byte((uint8_t*)(loc), (val)); }
|
||||
#define hwWriteConfig(__loc, __val) if((uint8_t)(__val) != eeprom_read_byte((uint8_t*)(__loc))) { eeprom_write_byte((uint8_t*)(__loc), (__val)); }
|
||||
#else
|
||||
#define hwWriteConfig(__pos, __value) (eeprom_update_byte((uint8_t*)(__pos), (__value)))
|
||||
#define hwWriteConfig(__pos, __val) eeprom_update_byte((uint8_t*)(__pos), (__val))
|
||||
#endif
|
||||
|
||||
#define hwReadConfigBlock(__buf, __pos, __length) (eeprom_read_block((__buf), (void*)(__pos), (__length)))
|
||||
#define hwWriteConfigBlock(__buf, __pos, __length) (eeprom_write_block((void*)(__buf), (void*)(__pos), (__length)))
|
||||
#define hwReadConfigBlock(__buf, __pos, __length) eeprom_read_block((void*)(__buf), (void*)(__pos), (__length))
|
||||
#define hwWriteConfigBlock(__buf, __pos, __length) eeprom_write_block((void*)(__buf), (void*)(__pos), (__length))
|
||||
|
||||
|
||||
|
||||
enum period_t
|
||||
{
|
||||
enum period_t {
|
||||
SLEEP_15MS,
|
||||
SLEEP_30MS,
|
||||
SLEEP_60MS,
|
||||
|
||||
@@ -134,7 +134,7 @@ uint16_t hwFreeMem() {
|
||||
|
||||
#ifdef MY_DEBUG
|
||||
void hwDebugPrint(const char *fmt, ... ) {
|
||||
char fmtBuffer[MY_DEBUG_BUFFER_SIZE];
|
||||
char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
|
||||
snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
|
||||
@@ -144,9 +144,9 @@ void hwDebugPrint(const char *fmt, ... ) {
|
||||
va_start (args, fmt );
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// Truncate message if this is gateway node
|
||||
vsnprintf_P(fmtBuffer, MY_GATEWAY_MAX_SEND_LENGTH, fmt, args);
|
||||
fmtBuffer[MY_GATEWAY_MAX_SEND_LENGTH-1] = '\n';
|
||||
fmtBuffer[MY_GATEWAY_MAX_SEND_LENGTH] = '\0';
|
||||
vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
|
||||
fmtBuffer[sizeof(fmtBuffer) - 2] = '\n';
|
||||
fmtBuffer[sizeof(fmtBuffer) - 1] = '\0';
|
||||
#else
|
||||
vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
|
||||
#endif
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
#endif
|
||||
|
||||
#define MY_SERIALDEVICE Serial
|
||||
#define MY_DEBUG_BUFFER_SIZE 300
|
||||
|
||||
#define min(a,b) ((a)<(b)?(a):(b))
|
||||
#define max(a,b) ((a)>(b)?(a):(b))
|
||||
|
||||
+41
-9
@@ -113,7 +113,8 @@ void hwWatchdogReset() {
|
||||
}
|
||||
|
||||
void hwReboot() {
|
||||
// TODO: Not supported!
|
||||
NVIC_SystemReset();
|
||||
while (true);
|
||||
}
|
||||
|
||||
int8_t hwSleep(unsigned long ms) {
|
||||
@@ -142,13 +143,44 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
|
||||
|
||||
#if defined(MY_DEBUG) || defined(MY_SPECIAL_DEBUG)
|
||||
uint16_t hwCPUVoltage() {
|
||||
// TODO: Not supported!
|
||||
return 0;
|
||||
|
||||
// disable ADC
|
||||
while (ADC->STATUS.bit.SYNCBUSY);
|
||||
ADC->CTRLA.bit.ENABLE = 0x00;
|
||||
|
||||
// internal 1V reference (default)
|
||||
analogReference(AR_INTERNAL1V0);
|
||||
// 12 bit resolution (default)
|
||||
analogWriteResolution(12);
|
||||
// MUXp 0x1B = SCALEDIOVCC/4 => connected to Vcc
|
||||
ADC->INPUTCTRL.bit.MUXPOS = 0x1B ;
|
||||
|
||||
// enable ADC
|
||||
while (ADC->STATUS.bit.SYNCBUSY);
|
||||
ADC->CTRLA.bit.ENABLE = 0x01;
|
||||
// start conversion
|
||||
while (ADC->STATUS.bit.SYNCBUSY);
|
||||
ADC->SWTRIG.bit.START = 1;
|
||||
// clear the Data Ready flag
|
||||
ADC->INTFLAG.bit.RESRDY = 1;
|
||||
// start conversion again, since The first conversion after the reference is changed must not be used.
|
||||
while (ADC->STATUS.bit.SYNCBUSY);
|
||||
ADC->SWTRIG.bit.START = 1;
|
||||
|
||||
// waiting for conversion to complete
|
||||
while (!ADC->INTFLAG.bit.RESRDY);
|
||||
const uint32_t valueRead = ADC->RESULT.reg;
|
||||
|
||||
// disable ADC
|
||||
while (ADC->STATUS.bit.SYNCBUSY);
|
||||
ADC->CTRLA.bit.ENABLE = 0x00;
|
||||
|
||||
return valueRead * 4;
|
||||
}
|
||||
|
||||
uint16_t hwCPUFrequency() {
|
||||
// TODO: Not supported!
|
||||
return 0;
|
||||
// TODO: currently reporting compile time frequency (in 1/10MHz)
|
||||
return F_CPU / 100000UL;
|
||||
}
|
||||
|
||||
uint16_t hwFreeMem() {
|
||||
@@ -160,7 +192,7 @@ uint16_t hwFreeMem() {
|
||||
#ifdef MY_DEBUG
|
||||
void hwDebugPrint(const char *fmt, ... ) {
|
||||
if (MY_SERIALDEVICE) {
|
||||
char fmtBuffer[MY_DEBUG_BUFFER_SIZE];
|
||||
char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
|
||||
snprintf(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
|
||||
@@ -170,9 +202,9 @@ void hwDebugPrint(const char *fmt, ... ) {
|
||||
va_start (args, fmt );
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// Truncate message if this is gateway node
|
||||
vsnprintf(fmtBuffer, 60, fmt, args);
|
||||
fmtBuffer[59] = '\n';
|
||||
fmtBuffer[60] = '\0';
|
||||
vsnprintf(fmtBuffer, sizeof(fmtBuffer), fmt, args);
|
||||
fmtBuffer[sizeof(fmtBuffer) - 2] = '\n';
|
||||
fmtBuffer[sizeof(fmtBuffer) - 1] = '\0';
|
||||
#else
|
||||
vsnprintf(fmtBuffer, sizeof(fmtBuffer), fmt, args);
|
||||
#endif
|
||||
|
||||
@@ -52,15 +52,6 @@ void hwWriteConfig(int adr, uint8_t value);
|
||||
uint8_t hwReadConfig(int adr);
|
||||
|
||||
#define MY_SERIALDEVICE SerialUSB
|
||||
#define MY_DEBUG_BUFFER_SIZE 300
|
||||
|
||||
|
||||
/*
|
||||
#define hwReadConfigBlock(__buf, __adr, __length) ( __length = __length)
|
||||
#define hwWriteConfigBlock(__buf, __adr, __length) ( __length = __length)
|
||||
#define hwWriteConfig(__adr, __value) ( __value = __value)
|
||||
#define hwReadConfig(__adr) (0)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Disable all interrupts.
|
||||
|
||||
+47
-35
@@ -26,8 +26,7 @@
|
||||
static uint8_t countRx;
|
||||
static uint8_t countTx;
|
||||
static uint8_t countErr;
|
||||
static unsigned long prevTime = hwMillis() - LED_PROCESS_INTERVAL_MS; // Substract some, to make sure leds gets updated on first run.
|
||||
|
||||
static unsigned long prevTime;
|
||||
|
||||
inline void ledsInit()
|
||||
{
|
||||
@@ -37,16 +36,17 @@ inline void ledsInit()
|
||||
countErr = 0;
|
||||
|
||||
// Setup led pins
|
||||
#if defined(MY_DEFAULT_RX_LED_PIN)
|
||||
pinMode(MY_DEFAULT_RX_LED_PIN, OUTPUT);
|
||||
#endif
|
||||
#if defined(MY_DEFAULT_TX_LED_PIN)
|
||||
pinMode(MY_DEFAULT_TX_LED_PIN, OUTPUT);
|
||||
#endif
|
||||
#if defined(MY_DEFAULT_ERR_LED_PIN)
|
||||
pinMode(MY_DEFAULT_ERR_LED_PIN, OUTPUT);
|
||||
#endif
|
||||
ledsProcess();
|
||||
#if defined(MY_DEFAULT_RX_LED_PIN)
|
||||
pinMode(MY_DEFAULT_RX_LED_PIN, OUTPUT);
|
||||
#endif
|
||||
#if defined(MY_DEFAULT_TX_LED_PIN)
|
||||
pinMode(MY_DEFAULT_TX_LED_PIN, OUTPUT);
|
||||
#endif
|
||||
#if defined(MY_DEFAULT_ERR_LED_PIN)
|
||||
pinMode(MY_DEFAULT_ERR_LED_PIN, OUTPUT);
|
||||
#endif
|
||||
prevTime = hwMillis() - LED_PROCESS_INTERVAL_MS; // Substract some, to make sure leds gets updated on first run.
|
||||
ledsProcess();
|
||||
}
|
||||
|
||||
void ledsProcess() {
|
||||
@@ -54,46 +54,58 @@ void ledsProcess() {
|
||||
if ((hwMillis() - prevTime) < LED_PROCESS_INTERVAL_MS) {
|
||||
return;
|
||||
}
|
||||
prevTime = hwMillis();
|
||||
|
||||
prevTime += LED_PROCESS_INTERVAL_MS;
|
||||
uint8_t state;
|
||||
|
||||
uint8_t state;
|
||||
|
||||
// For an On/Off ratio of 4, the pattern repeated will be [on, on, on, off]
|
||||
// until the counter becomes 0.
|
||||
#if defined(MY_DEFAULT_RX_LED_PIN)
|
||||
state = (countRx & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF;
|
||||
hwDigitalWrite(MY_DEFAULT_RX_LED_PIN, state);
|
||||
if (countRx) {
|
||||
// For an On/Off ratio of 4, the pattern repeated will be [on, on, on, off]
|
||||
// until the counter becomes 0.
|
||||
#if defined(MY_DEFAULT_RX_LED_PIN)
|
||||
if (countRx) {
|
||||
--countRx;
|
||||
}
|
||||
#endif
|
||||
state = (countRx & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF;
|
||||
hwDigitalWrite(MY_DEFAULT_RX_LED_PIN, state);
|
||||
#endif
|
||||
|
||||
#if defined(MY_DEFAULT_TX_LED_PIN)
|
||||
state = (countTx & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF;
|
||||
hwDigitalWrite(MY_DEFAULT_TX_LED_PIN, state);
|
||||
if (countTx) {
|
||||
#if defined(MY_DEFAULT_TX_LED_PIN)
|
||||
if (countTx) {
|
||||
--countTx;
|
||||
}
|
||||
#endif
|
||||
state = (countTx & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF;
|
||||
hwDigitalWrite(MY_DEFAULT_TX_LED_PIN, state);
|
||||
#endif
|
||||
|
||||
#if defined(MY_DEFAULT_ERR_LED_PIN)
|
||||
state = (countErr & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF;
|
||||
hwDigitalWrite(MY_DEFAULT_ERR_LED_PIN, state);
|
||||
if (countErr) {
|
||||
#if defined(MY_DEFAULT_ERR_LED_PIN)
|
||||
if (countErr) {
|
||||
--countErr;
|
||||
}
|
||||
#endif
|
||||
state = (countErr & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF;
|
||||
hwDigitalWrite(MY_DEFAULT_ERR_LED_PIN, state);
|
||||
#endif
|
||||
}
|
||||
|
||||
void ledsBlinkRx(uint8_t cnt) {
|
||||
if (!countRx) { countRx = cnt*LED_ON_OFF_RATIO; }
|
||||
if (!countRx) {
|
||||
countRx = cnt*LED_ON_OFF_RATIO;
|
||||
}
|
||||
ledsProcess();
|
||||
}
|
||||
|
||||
void ledsBlinkTx(uint8_t cnt) {
|
||||
if(!countTx) { countTx = cnt*LED_ON_OFF_RATIO; }
|
||||
if(!countTx) {
|
||||
countTx = cnt*LED_ON_OFF_RATIO;
|
||||
}
|
||||
ledsProcess();
|
||||
}
|
||||
|
||||
void ledsBlinkErr(uint8_t cnt) {
|
||||
if(!countErr) { countErr = cnt*LED_ON_OFF_RATIO; }
|
||||
if(!countErr) {
|
||||
countErr = cnt*LED_ON_OFF_RATIO;
|
||||
}
|
||||
ledsProcess();
|
||||
}
|
||||
|
||||
bool ledsBlinking() {
|
||||
return countRx || countTx || countErr;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,11 @@
|
||||
void ledsBlinkTx(uint8_t cnt);
|
||||
void ledsBlinkErr(uint8_t cnt);
|
||||
void ledsProcess(); // do the actual blinking
|
||||
/**
|
||||
* Test if any LED is currently blinking.
|
||||
* @return true when one or more LEDs are blinking, false otherwise.
|
||||
*/
|
||||
bool ledsBlinking();
|
||||
|
||||
#else
|
||||
// Remove led functions if feature is disabled
|
||||
|
||||
+21
-15
@@ -54,11 +54,7 @@ void _callbackTransportOk()
|
||||
}
|
||||
|
||||
void _process(void) {
|
||||
hwWatchdogReset();
|
||||
|
||||
#if defined (MY_DEFAULT_TX_LED_PIN) || defined(MY_DEFAULT_RX_LED_PIN) || defined(MY_DEFAULT_ERR_LED_PIN)
|
||||
ledsProcess();
|
||||
#endif
|
||||
doYield();
|
||||
|
||||
#if defined(MY_INCLUSION_MODE_FEATURE)
|
||||
inclusionProcess();
|
||||
@@ -80,10 +76,7 @@ void _process(void) {
|
||||
|
||||
void _infiniteLoop(void) {
|
||||
while(1) {
|
||||
yield();
|
||||
#if defined (MY_DEFAULT_TX_LED_PIN) || defined(MY_DEFAULT_RX_LED_PIN) || defined(MY_DEFAULT_ERR_LED_PIN)
|
||||
ledsProcess();
|
||||
#endif
|
||||
doYield();
|
||||
#if defined(__linux__)
|
||||
exit(1);
|
||||
#endif
|
||||
@@ -139,9 +132,8 @@ void _begin(void) {
|
||||
#if !defined(MY_TRANSPORT_RELAX)
|
||||
// check if transport ready
|
||||
while (!isTransportReady()) {
|
||||
hwWatchdogReset();
|
||||
transportProcess();
|
||||
yield();
|
||||
doYield();
|
||||
}
|
||||
#else
|
||||
CORE_DEBUG(PSTR("MCO:BGN:MTR\n"));
|
||||
@@ -465,7 +457,6 @@ void wait(const uint32_t waitingMS) {
|
||||
const uint32_t enteringMS = hwMillis();
|
||||
while (hwMillis() - enteringMS < waitingMS) {
|
||||
_process();
|
||||
yield();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -476,12 +467,21 @@ bool wait(const uint32_t waitingMS, const uint8_t cmd, const uint8_t msgtype) {
|
||||
bool expectedResponse = false;
|
||||
while ( (hwMillis() - enteringMS < waitingMS) && !expectedResponse ) {
|
||||
_process();
|
||||
yield();
|
||||
expectedResponse = (mGetCommand(_msg) == cmd && _msg.type == msgtype);
|
||||
}
|
||||
return expectedResponse;
|
||||
}
|
||||
|
||||
void doYield(void) {
|
||||
hwWatchdogReset();
|
||||
|
||||
yield();
|
||||
|
||||
#if defined (MY_DEFAULT_TX_LED_PIN) || defined(MY_DEFAULT_RX_LED_PIN) || defined(MY_DEFAULT_ERR_LED_PIN)
|
||||
ledsProcess();
|
||||
#endif
|
||||
}
|
||||
|
||||
int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2) {
|
||||
CORE_DEBUG(PSTR("MCO:SLP:MS=%lu,SMS=%d,I1=%d,M1=%d,I2=%d,M2=%d\n"), sleepingMS, smartSleep, interrupt1, mode1, interrupt2, mode2);
|
||||
// OTA FW feature: do not sleep if FW update ongoing
|
||||
@@ -513,7 +513,6 @@ int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t in
|
||||
uint32_t sleepDeltaMS = 0;
|
||||
while (!isTransportReady() && (sleepDeltaMS < sleepingTimeMS) && (sleepDeltaMS < MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS)) {
|
||||
_process();
|
||||
yield();
|
||||
sleepDeltaMS = hwMillis() - sleepEnterMS;
|
||||
}
|
||||
// sleep remainder
|
||||
@@ -539,6 +538,13 @@ int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t in
|
||||
transportPowerDown();
|
||||
#endif
|
||||
|
||||
#if defined (MY_DEFAULT_TX_LED_PIN) || defined(MY_DEFAULT_RX_LED_PIN) || defined(MY_DEFAULT_ERR_LED_PIN)
|
||||
// Wait until leds finish their blinking pattern
|
||||
while (ledsBlinking()) {
|
||||
doYield();
|
||||
}
|
||||
#endif
|
||||
|
||||
setIndication(INDICATION_SLEEP);
|
||||
|
||||
int8_t result = MY_SLEEP_NOT_POSSIBLE; // default
|
||||
@@ -601,7 +607,7 @@ void _nodeLock(const char* str)
|
||||
while (1) {
|
||||
setIndication(INDICATION_ERR_LOCKED);
|
||||
CORE_DEBUG(PSTR("MCO:NLK:NODE LOCKED. TO UNLOCK, GND PIN %d AND RESET\n"), MY_NODE_UNLOCK_PIN);
|
||||
yield();
|
||||
doYield();
|
||||
(void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID,C_INTERNAL, I_LOCKED).set(str));
|
||||
#if defined(MY_SENSOR_NETWORK)
|
||||
transportPowerDown();
|
||||
|
||||
@@ -246,6 +246,13 @@ void wait(const uint32_t waitingMS);
|
||||
*/
|
||||
bool wait(const uint32_t waitingMS, const uint8_t cmd, const uint8_t msgtype);
|
||||
|
||||
/**
|
||||
* Function to allow scheduler to do some work.
|
||||
* @remark Internally it will call yield, kick the watchdog and update led states.
|
||||
*/
|
||||
void doYield(void);
|
||||
|
||||
|
||||
/**
|
||||
* Sleep (PowerDownMode) the MCU and radio. Wake up on timer.
|
||||
* @param sleepingMS Number of milliseconds to sleep.
|
||||
|
||||
@@ -504,7 +504,7 @@ bool transportWait(const uint32_t ms, const uint8_t cmd, const uint8_t msgtype){
|
||||
while ((hwMillis() - enter < ms) && !expectedResponse) {
|
||||
// process incoming messages
|
||||
transportProcessFIFO();
|
||||
yield(); // process esp8266 stack, ignored for AVR
|
||||
doYield();
|
||||
expectedResponse = (mGetCommand(_msg) == cmd && _msg.type == msgtype);
|
||||
}
|
||||
return expectedResponse;
|
||||
|
||||
+91
-88
@@ -25,125 +25,128 @@
|
||||
#include "drivers/CircularBuffer/CircularBuffer.h"
|
||||
|
||||
#if defined(MY_RF24_ENABLE_ENCRYPTION)
|
||||
#include "drivers/AES/AES.h"
|
||||
#include "drivers/AES/AES.h"
|
||||
#endif
|
||||
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
typedef struct _transportQueuedMessage
|
||||
{
|
||||
uint8_t m_len; // Length of the data
|
||||
uint8_t m_data[MAX_MESSAGE_LENGTH]; // The raw data
|
||||
typedef struct _transportQueuedMessage {
|
||||
uint8_t m_len; // Length of the data
|
||||
uint8_t m_data[MAX_MESSAGE_LENGTH]; // The raw data
|
||||
} transportQueuedMessage;
|
||||
|
||||
/** Buffer to store queued messages in. */
|
||||
static transportQueuedMessage transportRxQueueStorage[MY_RX_MESSAGE_BUFFER_SIZE];
|
||||
/** Circular buffer, which uses the transportRxQueueStorage and administers stored messages. */
|
||||
static CircularBuffer<transportQueuedMessage> transportRxQueue(transportRxQueueStorage, MY_RX_MESSAGE_BUFFER_SIZE);
|
||||
static CircularBuffer<transportQueuedMessage> transportRxQueue(transportRxQueueStorage, MY_RX_MESSAGE_BUFFER_SIZE);
|
||||
|
||||
static volatile uint8_t transportLostMessageCount = 0;
|
||||
|
||||
static void transportRxCallback(void)
|
||||
{
|
||||
// Called for each message received by radio, from interrupt context.
|
||||
// This function _must_ call RF24_readMessage() to de-assert interrupt line!
|
||||
if (!transportRxQueue.full())
|
||||
{
|
||||
transportQueuedMessage* msg = transportRxQueue.getFront();
|
||||
msg->m_len = RF24_readMessage(msg->m_data); // Read payload & clear RX_DR
|
||||
(void)transportRxQueue.pushFront(msg);
|
||||
} else {
|
||||
// Queue is full. Discard message.
|
||||
(void)RF24_readMessage(NULL); // Read payload & clear RX_DR
|
||||
// Keep track of messages lost. Max 255, prevent wrapping.
|
||||
if (transportLostMessageCount < 255)
|
||||
{
|
||||
++transportLostMessageCount;
|
||||
}
|
||||
}
|
||||
// Called for each message received by radio, from interrupt context.
|
||||
// This function _must_ call RF24_readMessage() to de-assert interrupt line!
|
||||
if (!transportRxQueue.full()) {
|
||||
transportQueuedMessage* msg = transportRxQueue.getFront();
|
||||
msg->m_len = RF24_readMessage(msg->m_data); // Read payload & clear RX_DR
|
||||
(void)transportRxQueue.pushFront(msg);
|
||||
} else {
|
||||
// Queue is full. Discard message.
|
||||
(void)RF24_readMessage(NULL); // Read payload & clear RX_DR
|
||||
// Keep track of messages lost. Max 255, prevent wrapping.
|
||||
if (transportLostMessageCount < 255) {
|
||||
++transportLostMessageCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MY_RF24_ENABLE_ENCRYPTION)
|
||||
AES _aes;
|
||||
uint8_t _dataenc[32] = {0};
|
||||
uint8_t _psk[16];
|
||||
AES _aes;
|
||||
uint8_t _dataenc[32] = {0};
|
||||
uint8_t _psk[16];
|
||||
#endif
|
||||
|
||||
bool transportInit() {
|
||||
#if defined(MY_RF24_ENABLE_ENCRYPTION)
|
||||
hwReadConfigBlock((void*)_psk, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
|
||||
//set up AES-key
|
||||
_aes.set_key(_psk, 16);
|
||||
// Make sure it is purged from memory when set
|
||||
memset(_psk, 0, 16);
|
||||
#endif
|
||||
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
RF24_registerReceiveCallback( transportRxCallback );
|
||||
#endif
|
||||
return RF24_initialize();
|
||||
bool transportInit(void)
|
||||
{
|
||||
#if defined(MY_RF24_ENABLE_ENCRYPTION)
|
||||
hwReadConfigBlock((void*)_psk, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
|
||||
//set up AES-key
|
||||
_aes.set_key(_psk, 16);
|
||||
// Make sure it is purged from memory when set
|
||||
memset(_psk, 0, 16);
|
||||
#endif
|
||||
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
RF24_registerReceiveCallback( transportRxCallback );
|
||||
#endif
|
||||
return RF24_initialize();
|
||||
}
|
||||
|
||||
void transportSetAddress(uint8_t address) {
|
||||
RF24_setNodeAddress(address);
|
||||
RF24_startListening();
|
||||
void transportSetAddress(const uint8_t address)
|
||||
{
|
||||
RF24_setNodeAddress(address);
|
||||
RF24_startListening();
|
||||
}
|
||||
|
||||
uint8_t transportGetAddress() {
|
||||
return RF24_getNodeID();
|
||||
uint8_t transportGetAddress(void)
|
||||
{
|
||||
return RF24_getNodeID();
|
||||
}
|
||||
|
||||
bool transportSend(uint8_t recipient, const void* data, uint8_t len) {
|
||||
#if defined(MY_RF24_ENABLE_ENCRYPTION)
|
||||
// copy input data because it is read-only
|
||||
memcpy(_dataenc,data,len);
|
||||
// has to be adjusted, WIP!
|
||||
_aes.set_IV(0);
|
||||
len = len > 16 ? 32 : 16;
|
||||
//encrypt data
|
||||
_aes.cbc_encrypt(_dataenc, _dataenc, len/16);
|
||||
bool status = RF24_sendMessage( recipient, _dataenc, len );
|
||||
#else
|
||||
bool status = RF24_sendMessage( recipient, data, len );
|
||||
#endif
|
||||
|
||||
return status;
|
||||
bool transportSend(const uint8_t recipient, const void* data, uint8_t len)
|
||||
{
|
||||
#if defined(MY_RF24_ENABLE_ENCRYPTION)
|
||||
// copy input data because it is read-only
|
||||
(void)memcpy(_dataenc,data,len);
|
||||
// has to be adjusted, WIP!
|
||||
_aes.set_IV(0);
|
||||
len = len > 16 ? 32 : 16;
|
||||
//encrypt data
|
||||
_aes.cbc_encrypt(_dataenc, _dataenc, len/16);
|
||||
return RF24_sendMessage(recipient, _dataenc, len);
|
||||
#else
|
||||
return RF24_sendMessage(recipient, data, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool transportAvailable() {
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
(void)RF24_isDataAvailable; // Prevent 'defined but not used' warning
|
||||
return !transportRxQueue.empty();
|
||||
#else
|
||||
return RF24_isDataAvailable();
|
||||
#endif
|
||||
bool transportAvailable(void)
|
||||
{
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
(void)RF24_isDataAvailable; // Prevent 'defined but not used' warning
|
||||
return !transportRxQueue.empty();
|
||||
#else
|
||||
return RF24_isDataAvailable();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool transportSanityCheck() {
|
||||
return RF24_sanityCheck();
|
||||
bool transportSanityCheck(void)
|
||||
{
|
||||
return RF24_sanityCheck();
|
||||
}
|
||||
|
||||
uint8_t transportReceive(void* data) {
|
||||
uint8_t len = 0;
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
transportQueuedMessage* msg = transportRxQueue.getBack();
|
||||
if (msg) {
|
||||
len = msg->m_len;
|
||||
(void)memcpy(data, msg->m_data, len);
|
||||
(void)transportRxQueue.popBack();
|
||||
}
|
||||
#else
|
||||
len = RF24_readMessage(data);
|
||||
#endif
|
||||
#if defined(MY_RF24_ENABLE_ENCRYPTION)
|
||||
// has to be adjusted, WIP!
|
||||
_aes.set_IV(0);
|
||||
// decrypt data
|
||||
_aes.cbc_decrypt((byte*)(data), (byte*)(data), len>16?2:1);
|
||||
#endif
|
||||
return len;
|
||||
uint8_t transportReceive(void* data)
|
||||
{
|
||||
uint8_t len = 0;
|
||||
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
|
||||
transportQueuedMessage* msg = transportRxQueue.getBack();
|
||||
if (msg) {
|
||||
len = msg->m_len;
|
||||
(void)memcpy(data, msg->m_data, len);
|
||||
(void)transportRxQueue.popBack();
|
||||
}
|
||||
#else
|
||||
len = RF24_readMessage(data);
|
||||
#endif
|
||||
#if defined(MY_RF24_ENABLE_ENCRYPTION)
|
||||
// has to be adjusted, WIP!
|
||||
_aes.set_IV(0);
|
||||
// decrypt data
|
||||
_aes.cbc_decrypt((byte*)(data), (byte*)(data), len>16?2:1);
|
||||
#endif
|
||||
return len;
|
||||
}
|
||||
|
||||
void transportPowerDown() {
|
||||
RF24_powerDown();
|
||||
void transportPowerDown(void)
|
||||
{
|
||||
RF24_powerDown();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user