deep sleep, clock error rate, static callbacks

This commit is contained in:
Verkehrsrot 2019-10-19 21:14:04 +02:00
parent d413c5d39f
commit f3b3838e09
13 changed files with 63 additions and 57 deletions

View File

@ -4,6 +4,7 @@
#include "globals.h" #include "globals.h"
#include "rcommand.h" #include "rcommand.h"
#include "timekeeper.h" #include "timekeeper.h"
#include <driver/rtc_io.h>
#if (TIME_SYNC_LORASERVER) #if (TIME_SYNC_LORASERVER)
#include "timesync.h" #include "timesync.h"
#endif #endif
@ -43,10 +44,10 @@ void showLoraKeys(void);
void lora_send(void *pvParameters); void lora_send(void *pvParameters);
void lora_enqueuedata(MessageBuffer_t *message); void lora_enqueuedata(MessageBuffer_t *message);
void lora_queuereset(void); void lora_queuereset(void);
void myEventCallback(void *pUserData, ev_t ev); static void IRAM_ATTR myEventCallback(void *pUserData, ev_t ev);
void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, static void IRAM_ATTR myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg,
size_t nMsg); size_t nMsg);
void myTxCallback(void *pUserData, int fSuccess); static void IRAM_ATTR myTxCallback(void *pUserData, int fSuccess);
void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[],
const uint8_t tablesize); const uint8_t tablesize);
uint8_t getBattLevel(void); uint8_t getBattLevel(void);
@ -55,7 +56,7 @@ const char *getBwName(rps_t rps);
const char *getCrName(rps_t rps); const char *getCrName(rps_t rps);
#if (TIME_SYNC_LORAWAN) #if (TIME_SYNC_LORAWAN)
void user_request_network_time_callback(void *pVoidUserUTCTime, static void user_request_network_time_callback(void *pVoidUserUTCTime,
int flagSuccess); int flagSuccess);
#endif #endif

View File

@ -17,7 +17,7 @@ void start_ota_update();
int version_compare(const String v1, const String v2); int version_compare(const String v1, const String v2);
void ota_display(const uint8_t row, const std::string status, void ota_display(const uint8_t row, const std::string status,
const std::string msg); const std::string msg);
void show_progress(unsigned long current, unsigned long size); static void show_progress(unsigned long current, unsigned long size);
#endif // USE_OTA #endif // USE_OTA

View File

@ -16,7 +16,7 @@ uint16_t read_voltage(void);
void calibrate_voltage(void); void calibrate_voltage(void);
bool batt_sufficient(void); bool batt_sufficient(void);
void enter_deepsleep(const int wakeup_sec, const gpio_num_t wakeup_gpio); void enter_deepsleep(const int wakeup_sec, const gpio_num_t wakeup_gpio);
int exit_deepsleep(void); int64_t exit_deepsleep(void);
#ifdef HAS_PMU #ifdef HAS_PMU
#include <axp20x.h> #include <axp20x.h>

View File

@ -8,7 +8,7 @@
#include "hash.h" #include "hash.h"
void wifi_sniffer_init(void); void wifi_sniffer_init(void);
void IRAM_ATTR wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); static void IRAM_ATTR wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type);
void switchWifiChannel(TimerHandle_t xTimer); void switchWifiChannel(TimerHandle_t xTimer);
#endif #endif

View File

@ -43,7 +43,7 @@ description = Paxcounter is a device for metering passenger flows in realtime. I
[common] [common]
; for release_version use max. 10 chars total, use any decimal format like "a.b.c" ; for release_version use max. 10 chars total, use any decimal format like "a.b.c"
release_version = 1.9.63 release_version = 1.9.64
; DEBUG LEVEL: For production run set to 0, otherwise device will leak RAM while running! ; DEBUG LEVEL: For production run set to 0, otherwise device will leak RAM while running!
; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose ; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose
debug_level = 4 debug_level = 4
@ -51,7 +51,7 @@ extra_scripts = pre:build.py
otakeyfile = ota.conf otakeyfile = ota.conf
lorakeyfile = loraconf.h lorakeyfile = loraconf.h
lmicconfigfile = lmic_config.h lmicconfigfile = lmic_config.h
platform_espressif32 = espressif32@1.8.0 platform_espressif32 = espressif32@1.11.0
monitor_speed = 115200 monitor_speed = 115200
upload_speed = 115200 upload_speed = 115200
lib_deps_lora = lib_deps_lora =

View File

@ -20,7 +20,11 @@ void irqHandler(void *pvParameters) {
if (InterruptStatus & UNMASK_IRQ) // interrupt handler to be enabled? if (InterruptStatus & UNMASK_IRQ) // interrupt handler to be enabled?
mask_irq = false; mask_irq = false;
else if (mask_irq) // suppress processing if interrupt handler is disabled // else if (mask_irq) // suppress processing if interrupt handler is
// disabled
else if (mask_irq || os_queryTimeCriticalJobs(ms2osticks(100)))
// suppress processing if interrupt handler is disabled
// or time critical lmic jobs are pending in next 100ms
continue; continue;
else if (InterruptStatus & MASK_IRQ) { // interrupt handler to be disabled? else if (InterruptStatus & MASK_IRQ) { // interrupt handler to be disabled?
mask_irq = true; mask_irq = true;

View File

@ -28,14 +28,14 @@
// so consuming more power. You may sharpen (reduce) this value if you are // so consuming more power. You may sharpen (reduce) this value if you are
// limited on battery. // limited on battery.
// ATTN: VALUES > 7 WILL CAUSE RECEPTION AND JOIN PROBLEMS WITH HIGH SF RATES // ATTN: VALUES > 7 WILL CAUSE RECEPTION AND JOIN PROBLEMS WITH HIGH SF RATES
//#define CLOCK_ERROR_PROCENTAGE 5 #define CLOCK_ERROR_PROCENTAGE 5
// Set this to 1 to enable some basic debug output (using printf) about // Set this to 1 to enable some basic debug output (using printf) about
// RF settings used during transmission and reception. Set to 2 to // RF settings used during transmission and reception. Set to 2 to
// enable more verbose output. Make sure that printf is actually // enable more verbose output. Make sure that printf is actually
// configured (e.g. on AVR it is not by default), otherwise using it can // configured (e.g. on AVR it is not by default), otherwise using it can
// cause crashing. // cause crashing.
//#define LMIC_DEBUG_LEVEL 1 //#define LMIC_DEBUG_LEVEL 2
// Enable this to allow using printf() to print to the given serial port // Enable this to allow using printf() to print to the given serial port
// (or any other Print object). This can be easy for debugging. The // (or any other Print object). This can be easy for debugging. The
@ -57,8 +57,7 @@
// Uncomment this to disable all code related to ping // Uncomment this to disable all code related to ping
#define DISABLE_PING #define DISABLE_PING
// Uncomment this to disable all code related to beacon tracking. // Uncomment this to disable all code related to beacon tracking.
// Requires ping to be disabled too // Requires ping to be disabled too#define DISABLE_BEACONS
#define DISABLE_BEACONS
// Uncomment these to disable the corresponding MAC commands. // Uncomment these to disable the corresponding MAC commands.
// Class A // Class A
@ -88,7 +87,7 @@
// implementation is optimized for speed on 32-bit processors using // implementation is optimized for speed on 32-bit processors using
// fairly big lookup tables, but it takes up big amounts of flash on the // fairly big lookup tables, but it takes up big amounts of flash on the
// AVR architecture. // AVR architecture.
//#define USE_ORIGINAL_AES #define USE_ORIGINAL_AES
// //
// This selects the AES implementation written by Ideetroon for their // This selects the AES implementation written by Ideetroon for their
// own LoRaWAN library. It also uses lookup tables, but smaller // own LoRaWAN library. It also uses lookup tables, but smaller

View File

@ -18,6 +18,10 @@ static const char TAG[] = "lora";
#endif #endif
#endif #endif
RTC_DATA_ATTR u4_t RTCnetid, RTCdevaddr;
RTC_DATA_ATTR u1_t RTCnwkKey[16], RTCartKey[16];
RTC_DATA_ATTR int RTCseqnoUp, RTCseqnoDn;
QueueHandle_t LoraSendQueue; QueueHandle_t LoraSendQueue;
TaskHandle_t lmicTask = NULL, lorasendTask = NULL; TaskHandle_t lmicTask = NULL, lorasendTask = NULL;
@ -73,10 +77,6 @@ static const lmic_pinmap myPinmap = {
.spi_freq = 8000000, // 8MHz .spi_freq = 8000000, // 8MHz
.pConfig = &myHalConfig}; .pConfig = &myHalConfig};
RTC_DATA_ATTR u4_t RTCnetid, RTCdevaddr;
RTC_DATA_ATTR u1_t RTCnwkKey[16], RTCartKey[16];
RTC_DATA_ATTR int RTCseqnoUp, RTCseqnoDn;
void lora_setupForNetwork(bool preJoin) { void lora_setupForNetwork(bool preJoin) {
if (preJoin) { if (preJoin) {
@ -311,7 +311,7 @@ esp_err_t lora_stack_init(bool joined) {
"lmictask", // name of task "lmictask", // name of task
4096, // stack size of task 4096, // stack size of task
(void *)1, // parameter of the task (void *)1, // parameter of the task
5, // priority of the task 2, // priority of the task
&lmicTask, // task handle &lmicTask, // task handle
1); // CPU core 1); // CPU core
@ -373,7 +373,7 @@ void lora_enqueuedata(MessageBuffer_t *message) {
void lora_queuereset(void) { xQueueReset(LoraSendQueue); } void lora_queuereset(void) { xQueueReset(LoraSendQueue); }
#if (TIME_SYNC_LORAWAN) #if (TIME_SYNC_LORAWAN)
void IRAM_ATTR user_request_network_time_callback(void *pVoidUserUTCTime, static void IRAM_ATTR user_request_network_time_callback(void *pVoidUserUTCTime,
int flagSuccess) { int flagSuccess) {
// Explicit conversion from void* to uint32_t* to avoid compiler errors // Explicit conversion from void* to uint32_t* to avoid compiler errors
time_t *pUserUTCTime = (time_t *)pVoidUserUTCTime; time_t *pUserUTCTime = (time_t *)pVoidUserUTCTime;
@ -444,7 +444,7 @@ void lmictask(void *pvParameters) {
// so consuming more power. You may sharpen (reduce) CLOCK_ERROR_PERCENTAGE // so consuming more power. You may sharpen (reduce) CLOCK_ERROR_PERCENTAGE
// in src/lmic_config.h if you are limited on battery. // in src/lmic_config.h if you are limited on battery.
#ifdef CLOCK_ERROR_PROCENTAGE #ifdef CLOCK_ERROR_PROCENTAGE
LMIC_setClockError(CLOCK_ERROR_PROCENTAGE * MAX_CLOCK_ERROR / 100); LMIC_setClockError(CLOCK_ERROR_PROCENTAGE * MAX_CLOCK_ERROR / 1000);
#endif #endif
while (1) { while (1) {
@ -454,7 +454,7 @@ void lmictask(void *pvParameters) {
} // lmictask } // lmictask
// lmic event handler // lmic event handler
void myEventCallback(void *pUserData, ev_t ev) { static void myEventCallback(void *pUserData, ev_t ev) {
// using message descriptors from LMIC library // using message descriptors from LMIC library
static const char *const evNames[] = {LMIC_EVENT_NAME_TABLE__INIT}; static const char *const evNames[] = {LMIC_EVENT_NAME_TABLE__INIT};
@ -498,8 +498,8 @@ void myEventCallback(void *pUserData, ev_t ev) {
} }
// receive message handler // receive message handler
void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, static void myRxCallback(void *pUserData, uint8_t port,
size_t nMsg) { const uint8_t *pMsg, size_t nMsg) {
// display type of received data // display type of received data
if (nMsg) if (nMsg)
@ -553,7 +553,7 @@ void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg,
} }
// transmit complete message handler // transmit complete message handler
void myTxCallback(void *pUserData, int fSuccess) { static void myTxCallback(void *pUserData, int fSuccess) {
#if (TIME_SYNC_LORASERVER) #if (TIME_SYNC_LORASERVER)
// if last packet sent was a timesync request, store TX timestamp // if last packet sent was a timesync request, store TX timestamp

View File

@ -175,6 +175,8 @@ void setup() {
#if (HAS_GPS) #if (HAS_GPS)
ESP_LOGI(TAG, "TinyGPS+ version %s", TinyGPSPlus::libraryVersion()); ESP_LOGI(TAG, "TinyGPS+ version %s", TinyGPSPlus::libraryVersion());
#endif #endif
#endif // VERBOSE
} }
// open i2c bus // open i2c bus
@ -191,8 +193,6 @@ void setup() {
strcat_P(features, " PMU"); strcat_P(features, " PMU");
#endif #endif
#endif // verbose
// read (and initialize on first run) runtime settings from NVRAM // read (and initialize on first run) runtime settings from NVRAM
loadConfig(); // includes initialize if necessary loadConfig(); // includes initialize if necessary
@ -200,7 +200,8 @@ void setup() {
#ifdef HAS_DISPLAY #ifdef HAS_DISPLAY
strcat_P(features, " OLED"); strcat_P(features, " OLED");
DisplayIsOn = cfg.screenon; DisplayIsOn = cfg.screenon;
init_display(RTC_runmode == RUNMODE_NORMAL); // note: blocking call init_display(RTC_runmode == RUNMODE_NORMAL ? true
: false); // note: blocking call
#endif #endif
// scan i2c bus for devices // scan i2c bus for devices
@ -316,7 +317,8 @@ void setup() {
// initialize LoRa // initialize LoRa
#if (HAS_LORA) #if (HAS_LORA)
strcat_P(features, " LORA"); strcat_P(features, " LORA");
assert(lora_stack_init(RTC_runmode == RUNMODE_WAKEUP) == ESP_OK); assert(lora_stack_init(RTC_runmode == RUNMODE_WAKEUP ? true : false) ==
ESP_OK);
#endif #endif
// initialize SPI // initialize SPI

View File

@ -320,7 +320,7 @@ void ota_display(const uint8_t row, const std::string status,
} }
// callback function to show download progress while streaming data // callback function to show download progress while streaming data
void show_progress(unsigned long current, unsigned long size) { static void show_progress(unsigned long current, unsigned long size) {
#ifdef HAS_DISPLAY #ifdef HAS_DISPLAY
char buf[17]; char buf[17];
snprintf(buf, 17, "%-9lu (%3lu%%)", current, current * 100 / size); snprintf(buf, 17, "%-9lu (%3lu%%)", current, current * 100 / size);

View File

@ -7,7 +7,7 @@
// Note: After editing, before "build", use "clean" button in PlatformIO! // Note: After editing, before "build", use "clean" button in PlatformIO!
// Verbose enables serial output // Verbose enables serial output
#define VERBOSE 1 // set to 0 to silence the device, for mute use build option #define VERBOSE 0 // set to 0 to silence the device, for mute use build option
// Payload send cycle and encoding // Payload send cycle and encoding
#define SENDCYCLE 30 // payload send cycle [seconds/2], 0 .. 255 #define SENDCYCLE 30 // payload send cycle [seconds/2], 0 .. 255
@ -75,7 +75,7 @@
#define TIME_SYNC_INTERVAL_RETRY 10 // retry time sync after lost sync each .. minutes [default = 10], 0 means off #define TIME_SYNC_INTERVAL_RETRY 10 // retry time sync after lost sync each .. minutes [default = 10], 0 means off
#define TIME_SYNC_COMPILEDATE 0 // set to 1 to use compile date to initialize RTC after power outage [default = 0] #define TIME_SYNC_COMPILEDATE 0 // set to 1 to use compile date to initialize RTC after power outage [default = 0]
#define TIME_SYNC_LORAWAN 0 // set to 1 to use LORA network as time source, 0 means off [default = 0] #define TIME_SYNC_LORAWAN 0 // set to 1 to use LORA network as time source, 0 means off [default = 0]
#define TIME_SYNC_LORASERVER 0 // set to 1 to use LORA timeserver as time source, 0 means off [default = 0] #define TIME_SYNC_LORASERVER 1 // set to 1 to use LORA timeserver as time source, 0 means off [default = 0]
// settings for syncing time with timeserver applications // settings for syncing time with timeserver applications
#define TIME_SYNC_SAMPLES 1 // number of time requests for averaging #define TIME_SYNC_SAMPLES 1 // number of time requests for averaging

View File

@ -5,7 +5,7 @@
// Local logging tag // Local logging tag
static const char TAG[] = __FILE__; static const char TAG[] = __FILE__;
RTC_DATA_ATTR struct timeval sleep_enter_time; RTC_DATA_ATTR int64_t sleep_enter_time;
RTC_DATA_ATTR runmode_t RTC_runmode = RUNMODE_NORMAL; RTC_DATA_ATTR runmode_t RTC_runmode = RUNMODE_NORMAL;
#ifdef HAS_PMU #ifdef HAS_PMU
@ -61,8 +61,8 @@ void AXP192_power(bool on) {
pmu.setPowerOutPut(AXP192_LDO2, AXP202_ON); // Lora on T-Beam V1.0 pmu.setPowerOutPut(AXP192_LDO2, AXP202_ON); // Lora on T-Beam V1.0
pmu.setPowerOutPut(AXP192_LDO3, AXP202_ON); // Gps on T-Beam V1.0 pmu.setPowerOutPut(AXP192_LDO3, AXP202_ON); // Gps on T-Beam V1.0
pmu.setPowerOutPut(AXP192_DCDC1, AXP202_ON); // OLED on T-Beam v1.0 pmu.setPowerOutPut(AXP192_DCDC1, AXP202_ON); // OLED on T-Beam v1.0
// pmu.setChgLEDMode(AXP20X_LED_LOW_LEVEL); pmu.setChgLEDMode(AXP20X_LED_LOW_LEVEL);
pmu.setChgLEDMode(AXP20X_LED_BLINK_1HZ); // pmu.setChgLEDMode(AXP20X_LED_BLINK_1HZ);
} else { } else {
pmu.setChgLEDMode(AXP20X_LED_OFF); pmu.setChgLEDMode(AXP20X_LED_OFF);
// we don't cut off power of display, because then display blocks i2c bus // we don't cut off power of display, because then display blocks i2c bus
@ -196,6 +196,9 @@ void enter_deepsleep(const int wakeup_sec, const gpio_num_t wakeup_gpio) {
if ((!wakeup_sec) && (!wakeup_gpio) && (RTC_runmode == RUNMODE_NORMAL)) if ((!wakeup_sec) && (!wakeup_gpio) && (RTC_runmode == RUNMODE_NORMAL))
return; return;
// set up power domains
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
// set wakeup timer // set wakeup timer
if (wakeup_sec) if (wakeup_sec)
esp_sleep_enable_timer_wakeup(wakeup_sec * 1000000); esp_sleep_enable_timer_wakeup(wakeup_sec * 1000000);
@ -211,7 +214,7 @@ void enter_deepsleep(const int wakeup_sec, const gpio_num_t wakeup_gpio) {
RTCseqnoDn = LMIC.seqnoDn; RTCseqnoDn = LMIC.seqnoDn;
// store sleep enter time // store sleep enter time
gettimeofday(&sleep_enter_time, NULL); sleep_enter_time = esp_timer_get_time();
// halt interrupts accessing i2c bus // halt interrupts accessing i2c bus
mask_user_IRQ(); mask_user_IRQ();
@ -238,12 +241,9 @@ void enter_deepsleep(const int wakeup_sec, const gpio_num_t wakeup_gpio) {
esp_deep_sleep_start(); esp_deep_sleep_start();
} }
int exit_deepsleep(void) { int64_t exit_deepsleep(void) {
struct timeval now; int64_t sleep_time_ms = (esp_timer_get_time() - sleep_enter_time) / 1000;
gettimeofday(&now, NULL);
int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 +
(now.tv_usec - sleep_enter_time.tv_usec) / 1000;
// switch on power if has PMU // switch on power if has PMU
#ifdef HAS_PMU #ifdef HAS_PMU

View File

@ -29,7 +29,7 @@ typedef struct {
} wifi_ieee80211_packet_t; } wifi_ieee80211_packet_t;
// using IRAM_:ATTR here to speed up callback function // using IRAM_:ATTR here to speed up callback function
IRAM_ATTR void wifi_sniffer_packet_handler(void *buff, static IRAM_ATTR void wifi_sniffer_packet_handler(void *buff,
wifi_promiscuous_pkt_type_t type) { wifi_promiscuous_pkt_type_t type) {
const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buff; const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buff;