Skip to content

Commit

Permalink
Merge pull request #177 from cyberman54/development
Browse files Browse the repository at this point in the history
v1.6.0
  • Loading branch information
cyberman54 authored Oct 5, 2018
2 parents d02462e + d587a8b commit aa00cb8
Show file tree
Hide file tree
Showing 26 changed files with 361 additions and 334 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ description = Paxcounter is a proof-of-concept ESP32 device for metering passeng

[common]
; for release_version use max. 10 chars total, use any decimal format like "a.b.c"
release_version = 1.5.16
release_version = 1.6.0
; 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
debug_level = 0
Expand Down
13 changes: 1 addition & 12 deletions src/button.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
#ifdef HAS_BUTTON

#include "globals.h"
#include "senddata.h"
#include "button.h"

// Local logging tag
static const char TAG[] = "main";

portMUX_TYPE mutexButton = portMUX_INITIALIZER_UNLOCKED;

void IRAM_ATTR ButtonIRQ() {
portENTER_CRITICAL(&mutexButton);
ButtonPressedIRQ++;
portEXIT_CRITICAL(&mutexButton);
}

void readButton() {
portENTER_CRITICAL(&mutexButton);
ButtonPressedIRQ = 0;
portEXIT_CRITICAL(&mutexButton);
ESP_LOGI(TAG, "Button pressed");
payload.reset();
payload.addButton(0x01);
Expand Down
5 changes: 3 additions & 2 deletions src/button.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef _BUTTON_H
#define _BUTTON_H

void IRAM_ATTR ButtonIRQ(void);
void readButton(void);
#include "senddata.h"

void readButton();

#endif
30 changes: 9 additions & 21 deletions src/cyclic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@
/* Interval can be set in paxcounter.conf (HOMECYCLE) */

// Basic config
#include "globals.h"
#include "senddata.h"
#include "ota.h"
#include "cyclic.h"

// Local logging tag
static const char TAG[] = "main";

portMUX_TYPE mutexHomeCycle = portMUX_INITIALIZER_UNLOCKED;

// do all housekeeping
void doHousekeeping() {

portENTER_CRITICAL(&mutexHomeCycle);
HomeCycleIRQ = 0;
portEXIT_CRITICAL(&mutexHomeCycle);

// update uptime counter
uptime();

Expand All @@ -26,17 +18,19 @@ void doHousekeeping() {
ESP.restart();

// task storage debugging //
#ifdef HAS_LORA
ESP_LOGD(TAG, "Loraloop %d bytes left",
uxTaskGetStackHighWaterMark(LoraTask));
#endif
ESP_LOGD(TAG, "Wifiloop %d bytes left",
uxTaskGetStackHighWaterMark(wifiSwitchTask));
ESP_LOGD(TAG, "Statemachine %d bytes left",
uxTaskGetStackHighWaterMark(stateMachineTask));
ESP_LOGD(TAG, "IRQhandler %d bytes left",
uxTaskGetStackHighWaterMark(irqHandlerTask));
#ifdef HAS_GPS
ESP_LOGD(TAG, "Gpsloop %d bytes left", uxTaskGetStackHighWaterMark(GpsTask));
#endif
#ifdef HAS_SPI
ESP_LOGD(TAG, "Spiloop %d bytes left", uxTaskGetStackHighWaterMark(SpiTask));
#endif
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
ESP_LOGD(TAG, "LEDloop %d bytes left", uxTaskGetStackHighWaterMark(ledLoopTask));
#endif

// read battery voltage into global variable
#ifdef HAS_BATTERY_PROBE
Expand Down Expand Up @@ -70,12 +64,6 @@ void doHousekeeping() {
}
} // doHousekeeping()

void IRAM_ATTR homeCycleIRQ() {
portENTER_CRITICAL(&mutexHomeCycle);
HomeCycleIRQ++;
portEXIT_CRITICAL(&mutexHomeCycle);
}

// uptime counter 64bit to prevent millis() rollover after 49 days
uint64_t uptime() {
static uint32_t low32, high32;
Expand Down
4 changes: 3 additions & 1 deletion src/cyclic.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef _CYCLIC_H
#define _CYCLIC_H

#include "globals.h"
#include "senddata.h"

void doHousekeeping(void);
void IRAM_ATTR homeCycleIRQ(void);
uint64_t uptime(void);
void reset_counters(void);
int redirect_log(const char *fmt, va_list args);
Expand Down
17 changes: 3 additions & 14 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const char lora_datarate[] = {"100908078CNA121110090807"};

uint8_t volatile DisplayState = 0;

portMUX_TYPE mutexDisplay = portMUX_INITIALIZER_UNLOCKED;

// helper function, prints a hex key on display
void DisplayKey(const uint8_t *key, uint8_t len, bool lsb) {
const uint8_t *p;
Expand All @@ -27,8 +25,9 @@ void DisplayKey(const uint8_t *key, uint8_t len, bool lsb) {
u8x8.printf("\n");
}

// show startup screen
void init_display(const char *Productname, const char *Version) {

// show startup screen
uint8_t buf[32];
u8x8.begin();
u8x8.setFont(u8x8_font_chroma48medium8_r);
Expand Down Expand Up @@ -93,10 +92,6 @@ void init_display(const char *Productname, const char *Version) {

void refreshtheDisplay() {

portENTER_CRITICAL(&mutexDisplay);
DisplayTimerIRQ = 0;
portEXIT_CRITICAL(&mutexDisplay);

// set display on/off according to current device configuration
if (DisplayState != cfg.screenon) {
DisplayState = cfg.screenon;
Expand All @@ -107,7 +102,7 @@ void refreshtheDisplay() {
if (!DisplayState)
return;

uint8_t msgWaiting = 0;
uint8_t msgWaiting;
char buff[16]; // 16 chars line buffer

// update counter (lines 0-1)
Expand Down Expand Up @@ -192,10 +187,4 @@ void refreshtheDisplay() {

} // refreshDisplay()

void IRAM_ATTR DisplayIRQ() {
portENTER_CRITICAL_ISR(&mutexDisplay);
DisplayTimerIRQ++;
portEXIT_CRITICAL_ISR(&mutexDisplay);
}

#endif // HAS_DISPLAY
1 change: 0 additions & 1 deletion src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ extern HAS_DISPLAY u8x8;
void init_display(const char *Productname, const char *Version);
void refreshtheDisplay(void);
void DisplayKey(const uint8_t *key, uint8_t len, bool lsb);
void IRAM_ATTR DisplayIRQ(void);

#endif
12 changes: 3 additions & 9 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,28 @@ extern uint16_t volatile macs_total, macs_wifi, macs_ble,
batt_voltage; // display values
extern std::set<uint16_t> macs; // temp storage for MACs
extern hw_timer_t *channelSwitch, *sendCycle;
extern volatile uint8_t SendCycleTimerIRQ, HomeCycleIRQ, DisplayTimerIRQ,
ChannelTimerIRQ, ButtonPressedIRQ;

extern std::array<uint64_t, 0xff>::iterator it;
extern std::array<uint64_t, 0xff> beacons;

extern SemaphoreHandle_t xWifiChannelSwitchSemaphore;
extern TaskHandle_t stateMachineTask, wifiSwitchTask;
extern TaskHandle_t irqHandlerTask, wifiSwitchTask;

#ifdef HAS_GPS
extern TaskHandle_t GpsTask;
#include "gps.h"
#endif

#ifdef HAS_LED
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
#include "led.h"
#endif

#include "payload.h"

#ifdef HAS_LORA
extern QueueHandle_t LoraSendQueue;
extern TaskHandle_t LoraTask;
#include "lorawan.h"
#endif

#ifdef HAS_SPI
extern QueueHandle_t SPISendQueue;
#include "spi.h"
#endif

#ifdef HAS_DISPLAY
Expand Down
3 changes: 3 additions & 0 deletions src/gps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ static const char TAG[] = "main";

TinyGPSPlus gps;
gpsStatus_t gps_status;
TaskHandle_t GpsTask;

// read GPS data and cast to global struct
void gps_read() {
Expand Down Expand Up @@ -67,6 +68,8 @@ void gps_loop(void *pvParameters) {

} // end of infinite loop

vTaskDelete(NULL); // shoud never be reached

} // gps_loop()

#endif // HAS_GPS
1 change: 1 addition & 0 deletions src/gps.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef struct {
extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe
extern gpsStatus_t
gps_status; // Make struct for storing gps data globally available
extern TaskHandle_t GpsTask;

void gps_read(void);
void gps_loop(void *pvParameters);
Expand Down
74 changes: 74 additions & 0 deletions src/irqhandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "irqhandler.h"

// Local logging tag
static const char TAG[] = "main";

// irq handler task, handles all our application level interrupts
void irqHandler(void *pvParameters) {

configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check

uint32_t InterruptStatus;

// task remains in blocked state until it is notified by an irq
for (;;) {
xTaskNotifyWait(
0x00, // Don't clear any bits on entry
ULONG_MAX, // Clear all bits on exit
&InterruptStatus, // Receives the notification value
portMAX_DELAY); // wait forever (missing error handling here...)

// button pressed?
#ifdef HAS_BUTTON
if (InterruptStatus & BUTTON_IRQ)
readButton();
#endif

// display needs refresh?
#ifdef HAS_DISPLAY
if (InterruptStatus & DISPLAY_IRQ)
refreshtheDisplay();
#endif

// are cyclic tasks due?
if (InterruptStatus & CYCLIC_IRQ)
doHousekeeping();

// is time to send the payload?
if (InterruptStatus & SENDPAYLOAD_IRQ)
sendPayload();
}
vTaskDelete(NULL); // shoud never be reached
}

// esp32 hardware timer triggered interrupt service routines
// they notify the irq handler task

void IRAM_ATTR ChannelSwitchIRQ() {
xTaskNotifyGive(wifiSwitchTask);
portYIELD_FROM_ISR();
}

void IRAM_ATTR homeCycleIRQ() {
xTaskNotifyFromISR(irqHandlerTask, CYCLIC_IRQ, eSetBits, NULL);
portYIELD_FROM_ISR();
}

void IRAM_ATTR SendCycleIRQ() {
xTaskNotifyFromISR(irqHandlerTask, SENDPAYLOAD_IRQ, eSetBits, NULL);
portYIELD_FROM_ISR();
}

#ifdef HAS_DISPLAY
void IRAM_ATTR DisplayIRQ() {
xTaskNotifyFromISR(irqHandlerTask, DISPLAY_IRQ, eSetBits, NULL);
portYIELD_FROM_ISR();
}
#endif

#ifdef HAS_BUTTON
void IRAM_ATTR ButtonIRQ() {
xTaskNotifyFromISR(irqHandlerTask, BUTTON_IRQ, eSetBits, NULL);
portYIELD_FROM_ISR();
}
#endif
28 changes: 28 additions & 0 deletions src/irqhandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef _IRQHANDLER_H
#define _IRQHANDLER_H

#define DISPLAY_IRQ 0x01
#define BUTTON_IRQ 0x02
#define SENDPAYLOAD_IRQ 0x04
#define CYCLIC_IRQ 0x08

#include "globals.h"
#include "cyclic.h"
#include "senddata.h"

void irqHandler(void *pvParameters);
void IRAM_ATTR ChannelSwitchIRQ();
void IRAM_ATTR homeCycleIRQ();
void IRAM_ATTR SendCycleIRQ();

#ifdef HAS_DISPLAY
#include "display.h"
void IRAM_ATTR DisplayIRQ();
#endif

#ifdef HAS_BUTTON
#include "button.h"
void IRAM_ATTR ButtonIRQ();
#endif

#endif
Loading

0 comments on commit aa00cb8

Please sign in to comment.