minute buffer
This commit is contained in:
parent
fcb1ac8947
commit
5ee6368677
@ -7,13 +7,45 @@
|
|||||||
#include <logger.h>
|
#include <logger.h>
|
||||||
#include <PontCoopScheduler.h>
|
#include <PontCoopScheduler.h>
|
||||||
#include <wizHelper.h>
|
#include <wizHelper.h>
|
||||||
|
#include <config.h>
|
||||||
|
|
||||||
|
|
||||||
|
const uint32_t COUNTER_FREQUENCY = 1.0e6;
|
||||||
|
|
||||||
static uint32_t mainsCntSum = 0;
|
// 100: 2 digits behind decimal point
|
||||||
static uint32_t mainsCntCnt = 0;
|
// 1000: 3 digits behind decimal point
|
||||||
|
const uint32_t PRECISION = 1000;
|
||||||
|
|
||||||
void counterTick(void *handle) {
|
volatile static uint32_t mainsCntSum = 0;
|
||||||
|
volatile static uint32_t mainsCntCnt = 0;
|
||||||
|
|
||||||
|
static t_seconds *seconds;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint64_t timestamp;
|
||||||
|
uint32_t frequency;
|
||||||
|
} t_event;
|
||||||
|
|
||||||
|
|
||||||
|
const uint8_t SECONDS_PER_MINUTE = 60;
|
||||||
|
typedef struct {
|
||||||
|
char deviceId[sizeof(((t_configBlock*)0)->location)];
|
||||||
|
uint8_t done;
|
||||||
|
t_event events[SECONDS_PER_MINUTE];
|
||||||
|
} t_minuteStruct;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
t_minuteStruct s;
|
||||||
|
uint8_t b[sizeof(t_minuteStruct)];
|
||||||
|
} t_minuteBuffer;
|
||||||
|
|
||||||
|
const uint8_t NUM_OF_MINUTE_BUFFERS = 2;
|
||||||
|
t_minuteBuffer minuteBuffers[NUM_OF_MINUTE_BUFFERS];
|
||||||
|
uint8_t activeMinuteBuffer = 0;
|
||||||
|
|
||||||
|
|
||||||
|
void counterSecondTick(void *handle) {
|
||||||
|
static uint8_t bufferIdx = 0;
|
||||||
uint32_t tmpSum = 0;
|
uint32_t tmpSum = 0;
|
||||||
uint32_t tmpCnt = 0;
|
uint32_t tmpCnt = 0;
|
||||||
|
|
||||||
@ -24,20 +56,30 @@ void counterTick(void *handle) {
|
|||||||
mainsCntCnt = 0;
|
mainsCntCnt = 0;
|
||||||
HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
|
HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
|
||||||
|
|
||||||
uint64_t cnt = tmpSum / tmpCnt;
|
uint32_t cnt = tmpSum / tmpCnt;
|
||||||
// double f = 1.0 / ((double)cnt) * 1.0e6;
|
uint32_t freq = PRECISION * COUNTER_FREQUENCY / cnt;
|
||||||
uint64_t freq = 1000 * 1.0e6 / cnt;
|
|
||||||
// uint32_t ff = (uint32_t) ((f + 0.005) * 100);
|
|
||||||
|
|
||||||
t_seconds *seconds = wizGetSeconds();
|
coloredMsg(LOG_GREEN, "cst, Freq: %lu", freq);
|
||||||
coloredMsg(LOG_GREEN, "Freq: %lu", freq);
|
coloredMsg(LOG_GREEN, "cst, Tick: %d %lu %lu", seconds->valid, seconds->missedUpdates, seconds->seconds);
|
||||||
coloredMsg(LOG_GREEN, "Tick: %d %lu %lu", seconds->valid, seconds->missedUpdates, seconds->seconds);
|
|
||||||
}
|
|
||||||
|
|
||||||
void counterInit() {
|
if (! seconds->valid) {
|
||||||
schAdd(counterTick, NULL, 0, 1000);
|
coloredMsg(LOG_RED, "cst, time is not yet valid");
|
||||||
|
} else if (minuteBuffers[activeMinuteBuffer].s.done == 1) {
|
||||||
HAL_TIM_IC_Start_IT(&mainsCnt, TIM_CHANNEL_1);
|
coloredMsg(LOG_RED, "cst, minute buffer overrun");
|
||||||
|
} else {
|
||||||
|
minuteBuffers[activeMinuteBuffer].s.events[bufferIdx].timestamp = seconds->seconds;
|
||||||
|
minuteBuffers[activeMinuteBuffer].s.events[bufferIdx].frequency = freq;
|
||||||
|
coloredMsg(LOG_GREEN, "cst, added to buffer %d, slot %d", activeMinuteBuffer, bufferIdx);
|
||||||
|
bufferIdx += 1;
|
||||||
|
if (bufferIdx == SECONDS_PER_MINUTE) {
|
||||||
|
coloredMsg(LOG_GREEN, "cst, buffer %d is done", activeMinuteBuffer);
|
||||||
|
minuteBuffers[activeMinuteBuffer].s.done = 1;
|
||||||
|
activeMinuteBuffer += 1;
|
||||||
|
if (activeMinuteBuffer >= NUM_OF_MINUTE_BUFFERS) {
|
||||||
|
activeMinuteBuffer = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mainsCntsInputCaptureCallback(TIM_HandleTypeDef *htim) {
|
void mainsCntsInputCaptureCallback(TIM_HandleTypeDef *htim) {
|
||||||
@ -52,8 +94,6 @@ void mainsCntsInputCaptureCallback(TIM_HandleTypeDef *htim) {
|
|||||||
} else if (state == 1) {
|
} else if (state == 1) {
|
||||||
show(DEBUG_2, OFF);
|
show(DEBUG_2, OFF);
|
||||||
uint32_t captured = (savedV < v) ? (v - savedV) : ((htim->Init.Period - savedV) + v);
|
uint32_t captured = (savedV < v) ? (v - savedV) : ((htim->Init.Period - savedV) + v);
|
||||||
//double f = 1.0 / ((double)captured) * 1.0e6;
|
|
||||||
//logMsg("CCR: %ld, %f", captured, f);
|
|
||||||
mainsCntSum += captured;
|
mainsCntSum += captured;
|
||||||
mainsCntCnt += 1;
|
mainsCntCnt += 1;
|
||||||
state = 0;
|
state = 0;
|
||||||
@ -61,3 +101,14 @@ void mainsCntsInputCaptureCallback(TIM_HandleTypeDef *htim) {
|
|||||||
state = 0;
|
state = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void counterInit() {
|
||||||
|
seconds = wizGetSeconds();
|
||||||
|
minuteBuffers[0].s.done = 0;
|
||||||
|
minuteBuffers[1].s.done = 0;
|
||||||
|
|
||||||
|
schAdd(counterSecondTick, NULL, 0, 1000);
|
||||||
|
|
||||||
|
HAL_TIM_IC_Start_IT(&mainsCnt, TIM_CHANNEL_1);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user