diff --git a/lib/sensor_pzem.c b/lib/sensor_pzem.c index f714dce..0d81d99 100644 --- a/lib/sensor_pzem.c +++ b/lib/sensor_pzem.c @@ -16,7 +16,7 @@ * @file sensor_pzem.c * @author King Kévin * @date 2016 - * @note peripherals used: USART @ref sensor_pzem_usart + * @note peripherals used: USART @ref sensor_pzem_usart, timer @ref sensor_pzem_timer */ /* standard libraries */ @@ -24,12 +24,14 @@ #include // general utilities /* STM32 (including CM3) libraries */ +#include // Cortex M3 utilities +#include // interrupt handler #include // real-time control clock library #include // general purpose input output library #include // universal synchronous asynchronous receiver transmitter library -#include // interrupt handler -#include // Cortex M3 utilities +#include // timer utilities +/* own libraries */ #include "sensor_pzem.h" // PZEM electricity meter header and definitions #include "global.h" // common methods @@ -39,6 +41,12 @@ #define SENSOR_PZEM_USART 2 /**< USART peripheral */ /** @} */ +/** @defgroup sensor_pzem_timer timer peripheral used for waiting before sending the next request + * @{ + */ +#define SENSOR_PZEM_TIMER 2 /**< timer peripheral */ +/** @} */ + /* input and output ring buffer, indexes, and available memory */ static uint8_t rx_buffer[7] = {0}; /**< buffer for received response */ static volatile uint8_t rx_i = 0; /**< current position of read received data */ @@ -69,15 +77,26 @@ void sensor_pzem_setup(void) usart_enable_rx_interrupt(USART(SENSOR_PZEM_USART)); // enable receive interrupt usart_enable(USART(SENSOR_PZEM_USART)); // enable USART + // setup timer to wait for minimal time before next transmission (after previous transmission or reception) + rcc_periph_clock_enable(RCC_TIM(SENSOR_PZEM_TIMER)); // enable clock for timer block + timer_reset(TIM(SENSOR_PZEM_TIMER)); // reset timer state + timer_set_mode(TIM(SENSOR_PZEM_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_one_shot_mode(TIM(SENSOR_PZEM_TIMER)); // stop counter after update event (we only need to count down once) + timer_set_prescaler(TIM(SENSOR_PZEM_TIMER), 550-1); // set the prescaler so this 16 bits timer allows to wait for maximum 500 ms ( 1/(72E6/550/(2**16))=500.62ms ) + timer_set_period(TIM(SENSOR_PZEM_TIMER), 0xffff/2); // the timing is not defined in the specification. I tested until the communication was reliable (all requests get an response) + timer_clear_flag(TIM(SENSOR_PZEM_TIMER), TIM_SR_UIF); // clear flag + timer_enable_irq(TIM(SENSOR_PZEM_TIMER), TIM_DIER_UIE); // enable update interrupt for timer + nvic_enable_irq(NVIC_TIM_IRQ(SENSOR_PZEM_TIMER)); // catch interrupt in service routine + /* reset buffer states */ - tx_i = 0; + tx_i = LENGTH(tx_buffer); rx_i = 0; sensor_pzem_measurement_received = false; } void sensor_pzem_measurement_request(uint32_t address, enum sensor_pzem_measurement_type_t type) { - if (tx_i!=0) { // transmission is ongoing + if (tx_i=SENSOR_PZEM_MAX) { // invalid type @@ -93,16 +112,24 @@ void sensor_pzem_measurement_request(uint32_t address, enum sensor_pzem_measurem for (uint8_t i=0; i * @date 2016 - * @note peripherals used: USART @ref sensor_pzem_usart + * @note peripherals used: USART @ref sensor_pzem_usart, timer @ref sensor_pzem_timer */ #pragma once