refactor counter
This commit is contained in:
parent
0b1c0fda48
commit
74dc3ca220
@ -37,7 +37,7 @@ BUILD_DIR = build
|
||||
######################################
|
||||
# C sources
|
||||
C_SOURCES = \
|
||||
User/Src/adminCmds.c User/Src/cmdHandler.c User/Src/config.c User/Src/configCmds.c User/Src/eeprom.c User/Src/logger.c User/Src/main2.c User/Src/ports.c User/Src/regularCmds.c User/Src/ringbuffer.c User/Src/show.c User/Src/utils.c User/Src/wizHelper.c hottislib/PontCoopScheduler.c \
|
||||
User/Src/counter.c User/Src/adminCmds.c User/Src/cmdHandler.c User/Src/config.c User/Src/configCmds.c User/Src/eeprom.c User/Src/logger.c User/Src/main2.c User/Src/ports.c User/Src/regularCmds.c User/Src/ringbuffer.c User/Src/show.c User/Src/utils.c User/Src/wizHelper.c hottislib/PontCoopScheduler.c \
|
||||
Core/Src/main.c \
|
||||
Core/Src/gpio.c \
|
||||
Core/Src/spi.c \
|
||||
@ -204,4 +204,4 @@ clean:
|
||||
#######################################
|
||||
-include $(wildcard $(BUILD_DIR)/*.d)
|
||||
|
||||
# *** EOF ***
|
||||
# *** EOF ***
|
||||
|
10
cube/User/Inc/counter.h
Normal file
10
cube/User/Inc/counter.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef _COUNTER_H_
|
||||
#define _COUNTER_H_
|
||||
|
||||
|
||||
|
||||
void counterInit();
|
||||
void mainsCntsInputCaptureCallback(TIM_HandleTypeDef *htim);
|
||||
|
||||
|
||||
#endif /* _COUNTER_H_ */
|
62
cube/User/Src/counter.c
Normal file
62
cube/User/Src/counter.c
Normal file
@ -0,0 +1,62 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include <tim.h>
|
||||
|
||||
#include <counter.h>
|
||||
#include <show.h>
|
||||
#include <logger.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
#include <wizHelper.h>
|
||||
|
||||
|
||||
|
||||
static uint32_t mainsCntSum = 0;
|
||||
static uint32_t mainsCntCnt = 0;
|
||||
|
||||
void counterTick(void *handle) {
|
||||
uint32_t tmpSum = 0;
|
||||
uint32_t tmpCnt = 0;
|
||||
|
||||
HAL_NVIC_DisableIRQ(TIM1_CC_IRQn);
|
||||
tmpSum = mainsCntSum;
|
||||
mainsCntSum = 0;
|
||||
tmpCnt = mainsCntCnt;
|
||||
mainsCntCnt = 0;
|
||||
HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
|
||||
|
||||
uint64_t cnt = tmpSum / tmpCnt;
|
||||
// double f = 1.0 / ((double)cnt) * 1.0e6;
|
||||
uint64_t freq = 100 * 1.0e6 / cnt;
|
||||
// uint32_t ff = (uint32_t) ((f + 0.005) * 100);
|
||||
|
||||
t_seconds *seconds = wizGetSeconds();
|
||||
coloredMsg(LOG_GREEN, "Tick %lu %d %lu %lu", freq, seconds->valid, seconds->missedUpdates, seconds->seconds);
|
||||
}
|
||||
|
||||
void counterInit() {
|
||||
schAdd(counterTick, NULL, 0, 1000);
|
||||
|
||||
HAL_TIM_IC_Start_IT(&mainsCnt, TIM_CHANNEL_1);
|
||||
}
|
||||
|
||||
void mainsCntsInputCaptureCallback(TIM_HandleTypeDef *htim) {
|
||||
static uint8_t state = 0;
|
||||
static uint32_t savedV = 0;
|
||||
|
||||
uint32_t v = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
|
||||
if (state == 0) {
|
||||
show(DEBUG_2, ON);
|
||||
savedV = v;
|
||||
state = 1;
|
||||
} else if (state == 1) {
|
||||
show(DEBUG_2, OFF);
|
||||
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;
|
||||
mainsCntCnt += 1;
|
||||
state = 0;
|
||||
} else {
|
||||
state = 0;
|
||||
}
|
||||
}
|
@ -16,6 +16,7 @@
|
||||
#include <wizHelper.h>
|
||||
#include <cmdHandler.h>
|
||||
#include <config.h>
|
||||
#include <counter.h>
|
||||
|
||||
|
||||
void my_setup_1() {
|
||||
@ -28,31 +29,6 @@ void my_errorHandler() {
|
||||
show(LED_RED, ON);
|
||||
}
|
||||
|
||||
|
||||
uint32_t mainsCntSum = 0;
|
||||
uint32_t mainsCntCnt = 0;
|
||||
void second_tick(void *handle) {
|
||||
uint32_t tmpSum = 0;
|
||||
uint32_t tmpCnt = 0;
|
||||
|
||||
HAL_NVIC_DisableIRQ(TIM1_CC_IRQn);
|
||||
tmpSum = mainsCntSum;
|
||||
mainsCntSum = 0;
|
||||
tmpCnt = mainsCntCnt;
|
||||
mainsCntCnt = 0;
|
||||
HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
|
||||
|
||||
uint64_t cnt = tmpSum / tmpCnt;
|
||||
// double f = 1.0 / ((double)cnt) * 1.0e6;
|
||||
uint64_t freq = 100 * 1.0e6 / cnt;
|
||||
// uint32_t ff = (uint32_t) ((f + 0.005) * 100);
|
||||
|
||||
t_seconds *seconds = wizGetSeconds();
|
||||
coloredMsg(LOG_GREEN, "Tick %lu %d %lu %lu", freq, seconds->valid, seconds->missedUpdates, seconds->seconds);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void my_setup_2() {
|
||||
show(LED_RED, OFF);
|
||||
show(LED_GREEN, BLINK);
|
||||
@ -66,10 +42,7 @@ void my_setup_2() {
|
||||
|
||||
// cmdHandlerInit();
|
||||
|
||||
schAdd(second_tick, NULL, 0, 1000);
|
||||
|
||||
|
||||
HAL_TIM_IC_Start_IT(&mainsCnt, TIM_CHANNEL_1);
|
||||
counterInit();
|
||||
|
||||
logMsg("Application running");
|
||||
}
|
||||
@ -103,24 +76,7 @@ void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) {
|
||||
}
|
||||
|
||||
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
|
||||
static uint8_t state = 0;
|
||||
static uint32_t savedV = 0;
|
||||
if (htim == &mainsCnt) {
|
||||
uint32_t v = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
|
||||
if (state == 0) {
|
||||
show(DEBUG_2, ON);
|
||||
savedV = v;
|
||||
state = 1;
|
||||
} else if (state == 1) {
|
||||
show(DEBUG_2, OFF);
|
||||
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;
|
||||
mainsCntCnt += 1;
|
||||
state = 0;
|
||||
} else {
|
||||
state = 0;
|
||||
}
|
||||
mainsCntsInputCaptureCallback(htim);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user