add timer to periodically request meter measurments
This commit is contained in:
parent
37e9c01596
commit
978b325ba4
45
main.c
45
main.c
|
@ -37,6 +37,7 @@
|
|||
#include <libopencm3/stm32/iwdg.h> // independent watchdog utilities
|
||||
#include <libopencm3/stm32/dbgmcu.h> // debug utilities
|
||||
#include <libopencm3/stm32/flash.h> // flash utilities
|
||||
#include <libopencm3/stm32/timer.h> // timer utilities
|
||||
|
||||
/* own libraries */
|
||||
#include "global.h" // board definitions
|
||||
|
@ -54,6 +55,14 @@
|
|||
volatile bool rtc_internal_tick_flag = false; /**< flag set when internal RTC ticked */
|
||||
/** @} */
|
||||
|
||||
/** @defgroup main_timer timer used to periodically query the meter measurements
|
||||
* @{
|
||||
*/
|
||||
#define MAIN_TIMER 4 /**< timer number */
|
||||
#define MAIN_PERIOD 10.0 /**< period in seconds to query meter measurements */
|
||||
/** @} */
|
||||
|
||||
|
||||
int _write(int file, char *ptr, int len)
|
||||
{
|
||||
int i; // how much data has been sent
|
||||
|
@ -266,6 +275,20 @@ void main(void)
|
|||
radio_esp8266_setup();
|
||||
printf("OK\n");
|
||||
|
||||
// setup timer to periodically query meter measurements
|
||||
printf("setup query timer (%.0fs): ", MAIN_PERIOD);
|
||||
rcc_periph_clock_enable(RCC_TIM(MAIN_TIMER)); // enable clock for timer block
|
||||
timer_reset(TIM(MAIN_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(MAIN_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock,edge alignment (simple count), and count up
|
||||
timer_set_prescaler(TIM(MAIN_TIMER), 0xffff); // set the prescaler so this 16 bits timer allows to wait for ~60s ( (2**16)/(2**16)/72E6=59.65s )
|
||||
timer_set_period(TIM(MAIN_TIMER), MAIN_PERIOD*(rcc_ahb_frequency/(TIM_PSC(TIM(MAIN_TIMER))+1))); // set period for meter measurements queries
|
||||
timer_set_counter(TIM(MAIN_TIMER), 0); // reset timer counter to get preset waiting time
|
||||
timer_clear_flag(TIM(MAIN_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_irq(TIM(MAIN_TIMER), TIM_DIER_UIE); // enable update interrupt for timer (timer overflows when period is reached)
|
||||
//nvic_enable_irq(NVIC_TIM_IRQ(MAIN_TIMER)); // allow interrupt for timer just to wake up
|
||||
timer_enable_counter(TIM(MAIN_TIMER)); // start timer for periodic queries
|
||||
printf("OK\n");
|
||||
|
||||
#if !(DEBUG)
|
||||
//setup watchdog to reset in case we get stuck (i.e. when an error occurred)
|
||||
printf("setup watchdog (%.2fs): ",WATCHDOG_PERIOD/1000.0);
|
||||
|
@ -285,20 +308,18 @@ void main(void)
|
|||
printf("command input: ready\n");
|
||||
bool action = false; // if an action has been performed don't go to sleep
|
||||
button_flag = false; // reset button flag
|
||||
char c = ' '; // to store received character
|
||||
char c = '\0'; // to store received character
|
||||
bool char_flag = false; // a new character has been received
|
||||
|
||||
// start getting all PZEM-004T measurements from all meters
|
||||
// variables for PZEM-004T meter measurements
|
||||
struct sensor_pzem_measurement_t pzem_measurements[2][SENSOR_PZEM_MAX]; // PZEM-004T measurements (2 meters, all measurements)
|
||||
uint8_t pzem_meter = 0; // PZEM-004T meter index (add to prefix)
|
||||
uint8_t pzem_measurement = 0; // PZEM-004T measurement index (matches the type)
|
||||
sensor_pzem_measurement_request(0xc0a80100+pzem_meter, pzem_measurement); // request first measurement
|
||||
|
||||
// start getting all SDM120 measurements from all meters
|
||||
// variables for SDM120 meter measurements
|
||||
float sdm120_measurements[2][SENSOR_SDM120_MEASUREMENT_MAX]; // SDM120 measurements (2 meters, all measurements)
|
||||
uint8_t sdm120_meter = 0; // SDM120 meter index (add to 1 to get ID)
|
||||
uint8_t sdm120_measurement = 0; // SDM120 measurement index
|
||||
sensor_sdm120_measurement_request(1+sdm120_meter, sdm120_measurement); // request first measurement
|
||||
|
||||
while (true) { // infinite loop
|
||||
iwdg_reset(); // kick the dog
|
||||
|
@ -426,6 +447,7 @@ void main(void)
|
|||
}
|
||||
}
|
||||
while (pzem_meter>=LENGTH(pzem_measurements) && sdm120_meter>=LENGTH(sdm120_measurements)) { // all measurements received for all meter
|
||||
action = true; // action has been performed
|
||||
printf("saving measurements to database: ");
|
||||
const char* pzem_strings[SENSOR_PZEM_MAX] = {
|
||||
"voltage,meter=PZEM-004T,phase=%u value=%.1f\n",
|
||||
|
@ -534,6 +556,19 @@ void main(void)
|
|||
sdm120_meter = 0; // reset meter
|
||||
|
||||
}
|
||||
while (timer_get_flag(TIM(MAIN_TIMER), TIM_SR_UIF)) { // timer update event -> query measurements
|
||||
timer_clear_flag(TIM(MAIN_TIMER), TIM_SR_UIF); // clear flag
|
||||
action = true; // action has been performed
|
||||
printf("query meter measurements\n");
|
||||
// start getting all PZEM-004T measurements from all meters
|
||||
pzem_meter = 0; // reset PZEM meter number
|
||||
pzem_measurement = 0; // reset PZEM measurement index
|
||||
sensor_pzem_measurement_request(0xc0a80100+pzem_meter, pzem_measurement); // request first measurement
|
||||
// start getting all SDM120 measurements from all meters
|
||||
sdm120_meter = 0; // reset SDM120 meter number
|
||||
sdm120_measurement = 0; // reset SDM120 measurement index
|
||||
sensor_sdm120_measurement_request(1+sdm120_meter, sdm120_measurement); // request first measurement
|
||||
}
|
||||
if (action) { // go to sleep if nothing had to be done, else recheck for activity
|
||||
action = false;
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue