125 lines
2.5 KiB
C
Raw Normal View History

2021-01-09 22:01:21 +01:00
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <main.h>
#include <usart.h>
#include <spi.h>
2021-02-06 18:01:43 +01:00
#include <tim.h>
2021-01-09 22:01:21 +01:00
#include <PontCoopScheduler.h>
#include <show.h>
#include <logger.h>
#include <eeprom.h>
#include <wizHelper.h>
#include <cmdHandler.h>
#include <config.h>
void my_setup_1() {
schInit();
logInit();
showInit();
}
void my_errorHandler() {
show(LED_RED, ON);
}
2021-02-07 19:13:09 +01:00
uint32_t mainsCntSum = 0;
uint32_t mainsCntCnt = 0;
2021-02-06 15:31:03 +01:00
void second_tick(void *handle) {
2021-02-07 19:13:09 +01:00
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);
uint32_t cnt = tmpSum / tmpCnt;
double f = 1.0 / ((double)cnt) * 1.0e6;
2021-02-07 18:35:30 +01:00
t_seconds *seconds = wizGetSeconds();
2021-02-07 19:13:09 +01:00
coloredMsg(LOG_GREEN, "Tick %f %d %lu %lu", f, seconds->valid, seconds->missedUpdates, seconds->seconds);
2021-02-06 15:31:03 +01:00
}
2021-01-09 22:01:21 +01:00
void my_setup_2() {
show(LED_RED, OFF);
show(LED_GREEN, BLINK);
logMsg("Application starting");
eepromInit();
configInit();
2021-02-07 12:38:29 +01:00
wizInit();
2021-01-09 22:01:21 +01:00
2021-02-06 15:31:03 +01:00
// cmdHandlerInit();
schAdd(second_tick, NULL, 0, 60*1000);
2021-02-06 18:01:43 +01:00
2021-02-07 18:15:22 +01:00
HAL_TIM_IC_Start_IT(&mainsCnt, TIM_CHANNEL_1);
2021-02-06 18:01:43 +01:00
2021-01-30 17:56:50 +01:00
logMsg("Application running");
2021-01-09 22:01:21 +01:00
}
void my_loop() {
// show(DEBUG_2, TOGGLE);
schExec();
#ifndef LOGGER_OUTPUT_BY_INTERRUPT
logExec();
#endif //LOGGER_OUTPUT_BY_INTERRUPT
}
void SYSTICK_Callback() {
schUpdate();
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
#ifdef LOGGER_OUTPUT_BY_INTERRUPT
if (huart == &debugUart) {
debugTxCpltCallback(huart);
}
#endif //LOGGER_OUTPUT_BY_INTERRUPT
}
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) {
if (hspi == &eepromSpi) {
eepromSpiTxCpltCallback(hspi);
}
}
2021-02-06 15:31:03 +01:00
2021-02-06 18:01:43 +01:00
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);
2021-02-07 19:13:09 +01:00
//double f = 1.0 / ((double)captured) * 1.0e6;
//logMsg("CCR: %ld, %f", captured, f);
mainsCntSum += captured;
mainsCntCnt += 1;
2021-02-06 18:01:43 +01:00
state = 0;
} else {
state = 0;
}
}
2021-02-07 13:14:35 +01:00
}