Skip to content

Commit

Permalink
Merge pull request #273 from cyberman54/development
Browse files Browse the repository at this point in the history
v1.7.31
  • Loading branch information
cyberman54 authored Feb 17, 2019
2 parents b538c6d + 8b4d1e0 commit 4608a3b
Show file tree
Hide file tree
Showing 25 changed files with 455 additions and 481 deletions.
16 changes: 16 additions & 0 deletions include/clockcontroller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _CLOCKCONTROLLER_H
#define _CLOCKCONTROLLER_H

#include "globals.h"

#ifdef HAS_IF482
#include "if482.h"
#elif defined HAS_DCF77
#include "dcf77.h"
#endif

void clock_init(void);
void clock_loop(void *pvParameters);
time_t telegram_time(void);

#endif // _CLOCKCONTROLLER_H
5 changes: 2 additions & 3 deletions include/cyclic.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
#include "senddata.h"
#include "rcommand.h"
#include "spislave.h"
#include "rtctime.h"
#include <lmic.h>

#ifdef HAS_BME
#include "bme680mems.h"
#endif

#ifdef HAS_RTC
#include "rtctime.h"
#endif


void doHousekeeping(void);
uint64_t uptime(void);
Expand Down
13 changes: 7 additions & 6 deletions include/dcf77.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
#define _DCF77_H

#include "globals.h"
#include "rtctime.h"

#define DCF77_FRAME_SIZE (60)
#define DCF77_PULSE_LENGTH (100)

extern uint8_t DCFpulse[];

enum dcf_pulses { dcf_off, dcf_zero, dcf_one };
enum dcf_pinstate { dcf_low, dcf_high };

int dcf77_init(void);
void dcf77_loop(void *pvParameters);
void sendDCF77(void);
void DCF_Out(uint8_t startsec);
void generateTimeframe(time_t t);
void DCF_Pulse(time_t t);
void IRAM_ATTR DCF77_Frame(time_t t);
void set_DCF77_pin(dcf_pinstate state);
uint8_t dec2bcd(uint8_t dec, uint8_t startpos, uint8_t endpos, uint8_t pArray[]);

Expand Down
33 changes: 13 additions & 20 deletions include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
#define SCREEN_MODE (0x80)

// I2C bus access control
#define I2C_MUTEX_LOCK() xSemaphoreTake(I2Caccess, (3 * DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == pdTRUE
#define I2C_MUTEX_UNLOCK() xSemaphoreGive(I2Caccess)
#define I2C_MUTEX_LOCK() \
xSemaphoreTake(I2Caccess, (3 * DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == \
pdTRUE
#define I2C_MUTEX_UNLOCK() xSemaphoreGive(I2Caccess)

// Struct holding devices's runtime configuration
typedef struct {
Expand All @@ -63,7 +65,8 @@ typedef struct {
uint8_t runmode; // 0=normal, 1=update
uint8_t payloadmask; // bitswitches for payload data
char version[10]; // Firmware version
uint8_t bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1]; // BSEC state for BME680 sensor
uint8_t
bsecstate[BSEC_MAX_STATE_BLOB_SIZE + 1]; // BSEC state for BME680 sensor
} configData_t;

// Struct holding payload for data send queue
Expand Down Expand Up @@ -94,21 +97,19 @@ typedef struct {

enum sendprio_t { prio_low, prio_normal, prio_high };

// global variables
extern std::set<uint16_t, std::less<uint16_t>, Mallocator<uint16_t>> macs;
extern std::array<uint64_t, 0xff>::iterator it;
extern std::array<uint64_t, 0xff> beacons;

extern configData_t cfg; // current device configuration
extern char display_line6[], display_line7[]; // screen buffers
extern uint8_t volatile channel; // wifi channel rotation counter
extern uint16_t volatile macs_total, macs_wifi, macs_ble,
batt_voltage; // display values
extern bool volatile TimePulseTick;
extern hw_timer_t *sendCycle, *displaytimer;
extern SemaphoreHandle_t I2Caccess;
extern bool volatile BitsPending;

extern std::set<uint16_t, std::less<uint16_t>, Mallocator<uint16_t>> macs;
extern std::array<uint64_t, 0xff>::iterator it;
extern std::array<uint64_t, 0xff> beacons;

extern TaskHandle_t irqHandlerTask;
extern SemaphoreHandle_t I2Caccess, TimePulse;
extern TaskHandle_t irqHandlerTask, ClockTask;
extern TimerHandle_t WifiChanTimer;
extern Timezone myTZ;

Expand Down Expand Up @@ -149,12 +150,4 @@ extern Timezone myTZ;
#include "bme680mems.h"
#endif

#ifdef HAS_IF482
#include "if482.h"
#endif

#ifdef HAS_DCF77
#include "dcf77.h"
#endif

#endif
12 changes: 8 additions & 4 deletions include/if482.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
#define _IF482_H

#include "globals.h"
#include "rtctime.h"

int if482_init(void);
void if482_loop(void *pvParameters);
TickType_t tx_time(unsigned long baud, uint32_t config, int8_t rxPin,
#define IF482_FRAME_SIZE (17)
#define IF482_PULSE_LENGTH (1000)

extern HardwareSerial IF482;

void IF482_Pulse(time_t t);
String IRAM_ATTR IF482_Frame(time_t tt);
TickType_t tx_Ticks(unsigned long baud, uint32_t config, int8_t rxPin,
int8_t txPins);

#endif
6 changes: 2 additions & 4 deletions include/lorawan.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
#include <SPI.h>
#include <arduino_lmic_hal_boards.h>
#include "loraconf.h"
#include "rtctime.h"

// Needed for 24AA02E64, does not hurt anything if included and not used
#ifdef MCP_24AA02E64_I2C_ADDRESS
#include <Wire.h>
#endif

// Needed for RTC time sync if RTC present on board
#ifdef HAS_RTC
#include "rtctime.h"
#endif


extern QueueHandle_t LoraSendQueue;

Expand Down
3 changes: 2 additions & 1 deletion include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
#include "led.h"
#include "spislave.h"
#include "lorawan.h"

#include "rtctime.h"
#include "clockcontroller.h"
#endif
13 changes: 6 additions & 7 deletions include/rtctime.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@

extern RtcDS3231<TwoWire> Rtc; // make RTC instance globally available

extern TaskHandle_t ClockTask;
extern hw_timer_t *clockCycle;

int rtc_init(void);
int set_rtctime(uint32_t t);
int set_rtctime(time_t t);
void sync_rtctime(void);
time_t get_rtctime(void);
float get_rtctemp(void);
void IRAM_ATTR CLOCKIRQ();
int timepulse_init(uint32_t pps_freq);
void timepulse_start();
uint8_t sync_clock(time_t t);
void IRAM_ATTR CLOCKIRQ(void);
int timepulse_init(void);
void timepulse_start(void);
int sync_TimePulse(void);
int sync_SysTime(time_t);
int sync_SysTime(uint32_t t);

#endif // _RTCTIME_H
7 changes: 4 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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.7.2
release_version = 1.7.31
; 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 All @@ -40,10 +40,11 @@ upload_protocol = esptool
extra_scripts = pre:build.py
keyfile = ota.conf
platform_espressif32 = espressif32@1.6.0
;platform_espressif32 = https://github.com/platformio/platform-espressif32.git#feature/stage
board_build.partitions = min_spiffs.csv
monitor_speed = 115200
lib_deps_lora =
MCCI LoRaWAN LMIC library@^2.3.1
MCCI LoRaWAN LMIC library@>=2.3.2
lib_deps_display =
U8g2@>=2.25.7
lib_deps_rgbled =
Expand All @@ -66,7 +67,7 @@ lib_deps_all =
build_flags_basic =
-include "src/hal/${PIOENV}.h"
-include "src/paxcounter.conf"
-w
; -w
'-DARDUINO_LMIC_PROJECT_CONFIG_H=../../../src/lmic_config.h'
'-DCORE_DEBUG_LEVEL=${common.debug_level}'
'-DLOG_LOCAL_LEVEL=${common.debug_level}'
Expand Down
94 changes: 94 additions & 0 deletions src/clockcontroller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "clockcontroller.h"

#if defined HAS_IF482 || defined HAS_DCF77

#if defined HAS_DCF77 && defined HAS_IF482
#error You must define at most one of IF482 or DCF77!
#endif

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

void clock_init(void) {

// setup clock output interface
#ifdef HAS_IF482
IF482.begin(HAS_IF482);
#elif defined HAS_DCF77
pinMode(HAS_DCF77, OUTPUT);
#endif

xTaskCreatePinnedToCore(clock_loop, // task function
"clockloop", // name of task
2048, // stack size of task
(void *)1, // task parameter
4, // priority of the task
&ClockTask, // task handle
0); // CPU core

assert(ClockTask); // has clock task started?
} // clock_init

void clock_loop(void *pvParameters) { // ClockTask

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

TickType_t wakeTime;
time_t t;

#define t1(t) (t + DCF77_FRAME_SIZE + 1) // future time for next frame

// preload first DCF frame before start
#ifdef HAS_DCF77
DCF77_Frame(t1(telegram_time()));
#endif

// output time telegram for second following sec beginning with timepulse
for (;;) {
xTaskNotifyWait(0x00, ULONG_MAX, &wakeTime,
portMAX_DELAY); // wait for timepulse

if (timeStatus() == timeNotSet) // do we have valid time?
continue;

t = telegram_time(); // time to send to clock

#if defined HAS_IF482

IF482_Pulse(t + 1); // next second

#elif defined HAS_DCF77

if (ts == DCF77_FRAME_SIZE - 1) // moment to reload frame?
DCF77_Frame(t1(t)); // generate next frame

if (DCFpulse[DCF77_FRAME_SIZE] ==
minute(t1(t))) // do he have a recent frame?
DCF_Pulse(t + 1); // then output next second of current frame

#endif

} // for
} // clock_loop()

// helper function to fetch current second from most precise time source
time_t telegram_time(void) {
time_t t;

#ifdef HAS_GPS // gps is our primary time source if present
t = get_gpstime();
if (t) // did we get a valid time?
return t;
#endif

#ifdef HAS_RTC // rtc is our secondary time source if present
t = get_rtctime();
if (t)
return t;
#endif

// else we use systime as fallback source
return now();
}

#endif // HAS_IF482 || defined HAS_DCF77
37 changes: 21 additions & 16 deletions src/cyclic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static const char TAG[] = "main";

time_t userUTCTime; // Seconds since the UTC epoch
unsigned long nextLoraTimeSync = millis();
unsigned long nextRTCTimeSync = millis() + TIME_WRITE_INTERVAL_RTC * 60000;
unsigned long nextGPSTimeSync = millis();

// do all housekeeping
void doHousekeeping() {
Expand All @@ -24,8 +24,26 @@ void doHousekeeping() {
spi_housekeeping();
lora_housekeeping();

// do cyclic time sync with LORA network
#ifdef TIME_SYNC_INTERVAL_LORA
// do cyclic sync of systime with GPS timepulse, if present
#if defined HAS_GPS && defined TIME_SYNC_INTERVAL_GPS
if (millis() >= nextGPSTimeSync) {
nextGPSTimeSync = millis() + TIME_SYNC_INTERVAL_GPS *
60000; // set up next time sync period

// sync systime on next timepulse
if (sync_SysTime(get_gpstime())) {
//setSyncProvider(get_gpstime);
#ifdef HAS_RTC
set_rtctime(now()); // epoch time
#endif
ESP_LOGI(TAG, "GPS has set the system time");
} else
ESP_LOGI(TAG, "Unable to sync system time with GPS");
} // if
#endif

// do cyclic time sync with LORA network, if present
#if defined HAS_LORA && defined TIME_SYNC_INTERVAL_LORA
if (millis() >= nextLoraTimeSync) {
nextLoraTimeSync = millis() + TIME_SYNC_INTERVAL_LORA *
60000; // set up next time sync period
Expand All @@ -35,19 +53,6 @@ void doHousekeeping() {
}
#endif

// do cyclic write back system time to RTC if we have an external time source
#if (defined TIME_SYNC_INTERVAL_LORA || defined TIME_SYNC_INTERVAL_GPS) && \
defined HAS_RTC
if ((millis() >= nextRTCTimeSync) && (timeStatus() == timeSet)) {
nextRTCTimeSync = millis() + TIME_WRITE_INTERVAL_RTC *
60000; // set up next time sync period
if (!set_rtctime(now())) // epoch time
ESP_LOGE(TAG, "RTC set time failure");
else
ESP_LOGI(TAG, "RTC time updated");
}
#endif

// task storage debugging //
ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(irqHandlerTask),
Expand Down
Loading

0 comments on commit 4608a3b

Please sign in to comment.