add timer for inter-messsage silence to improve response rate

This commit is contained in:
King Kévin 2017-01-30 09:54:58 +01:00
parent db3ce48069
commit 4e9d7efcba
2 changed files with 55 additions and 11 deletions

View File

@ -16,7 +16,7 @@
* @file sensor_pzem.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @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 <stdlib.h> // general utilities
/* STM32 (including CM3) libraries */
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
#include <libopencm3/cm3/nvic.h> // interrupt handler
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/usart.h> // universal synchronous asynchronous receiver transmitter library
#include <libopencm3/cm3/nvic.h> // interrupt handler
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
#include <libopencm3/stm32/timer.h> // 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<LENGTH(tx_buffer)) { // transmission is ongoing
return;
}
if (type>=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<LENGTH(tx_buffer)-1; i++) {
tx_buffer[6] += tx_buffer[i]; // calculate buffer
}
tx_i = 0; // remember we have a message to send
usart_enable_tx_interrupt(USART(SENSOR_PZEM_USART)); // enable interrupt to send other bytes
usart_send(USART(SENSOR_PZEM_USART),tx_buffer[tx_i++]); // start transmission
if (TIM_CR1(TIM(SENSOR_PZEM_TIMER))&TIM_CR1_CEN) { // timer is already running
// at the end of the timer the transmission will start automatically
} else { // no timer is running
usart_enable_tx_interrupt(USART(SENSOR_PZEM_USART)); // enable interrupt to start sending bytes
//usart_send(USART(SENSOR_PZEM_USART),tx_buffer[tx_i++]); // start transmission
}
sensor_pzem_measurement_received = false; // reset flag
rx_i = 0; // prepare buffer to receive next measurement
}
struct sensor_pzem_measurement_t sensor_pzem_measurement_decode(void)
{
struct sensor_pzem_measurement_t measurement; // decoded measurement to return
measurement.valid = false; // wait until the end to ensure validity
if (!sensor_pzem_measurement_received) { // no measurement received
if (rx_i<LENGTH(rx_buffer)) { // buffer is not full, thus no measurement received
return measurement;
}
if ((rx_buffer[0]&0xf0)!=0xa0) { // not a response received
@ -125,7 +152,7 @@ struct sensor_pzem_measurement_t sensor_pzem_measurement_decode(void)
measurement.value.voltage = ((uint16_t)rx_buffer[1]<<8)+rx_buffer[2]+rx_buffer[3]*0.1;
break;
case SENSOR_PZEM_CURRENT:
measurement.value.current = rx_buffer[2]+rx_buffer[3]/100;
measurement.value.current = rx_buffer[2]+rx_buffer[3]*0.01;
break;
case SENSOR_PZEM_POWER:
measurement.value.power = ((uint16_t)rx_buffer[1]<<8)+rx_buffer[2];
@ -154,7 +181,9 @@ void USART_ISR(SENSOR_PZEM_USART)(void)
usart_send(USART(SENSOR_PZEM_USART),tx_buffer[tx_i++]); // transmit next byte
} else { // request transmitted
usart_disable_tx_interrupt(USART(SENSOR_PZEM_USART)); // disable transmit interrupt
tx_i = 0; // ready for next transmission
timer_set_counter(TIM(SENSOR_PZEM_TIMER), 0); // reset timer counter to get preset waiting time
timer_enable_counter(TIM(SENSOR_PZEM_TIMER)); // start timer between requests
}
}
if (usart_get_interrupt_source(USART(SENSOR_PZEM_USART), USART_SR_RXNE)) { // data has been received
@ -166,5 +195,20 @@ void USART_ISR(SENSOR_PZEM_USART)(void)
} else { // previous response not read before receiving the next
usart_recv(USART(SENSOR_PZEM_USART)); // drop received buffer
}
timer_set_counter(TIM(SENSOR_PZEM_TIMER), 0); // reset timer counter to get preset waiting time
timer_enable_counter(TIM(SENSOR_PZEM_TIMER)); // start timer between requests
}
}
/** interrupt service routine called on timeout */
void TIM_ISR(SENSOR_PZEM_TIMER)(void)
{
if (timer_get_flag(TIM(SENSOR_PZEM_TIMER), TIM_SR_UIF)) { // update event happened
timer_clear_flag(TIM(SENSOR_PZEM_TIMER), TIM_SR_UIF); // clear flag
if (tx_i<LENGTH(tx_buffer)) { // bytes are waiting to be sent
usart_enable_tx_interrupt(USART(SENSOR_PZEM_USART)); // enable interrupt to start sending bytes
}
}
}

View File

@ -16,7 +16,7 @@
* @file sensor_pzem.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @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