CuVoodoo STM32F1 firmware template
sensor_pzem.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
22 /* standard libraries */
23 #include <stdint.h> // standard integer types
24 #include <stdlib.h> // general utilities
25 
26 /* STM32 (including CM3) libraries */
27 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
28 #include <libopencm3/cm3/nvic.h> // interrupt handler
29 #include <libopencm3/stm32/rcc.h> // real-time control clock library
30 #include <libopencm3/stm32/gpio.h> // general purpose input output library
31 #include <libopencm3/stm32/usart.h> // universal synchronous asynchronous receiver transmitter library
32 #include <libopencm3/stm32/timer.h> // timer utilities
33 
34 /* own libraries */
35 #include "sensor_pzem.h" // PZEM electricity meter header and definitions
36 #include "global.h" // common methods
37 
41 #define SENSOR_PZEM_USART 2
47 #define SENSOR_PZEM_TIMER 2
50 /* input and output ring buffer, indexes, and available memory */
51 static uint8_t rx_buffer[7] = {0};
52 static volatile uint8_t rx_i = 0;
53 static uint8_t tx_buffer[7] = {0};
54 static volatile uint8_t tx_i = 0;
56 volatile bool sensor_pzem_measurement_received = false;
57 
59 {
60  /* enable USART I/O peripheral */
61  rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
62  rcc_periph_clock_enable(USART_PORT_RCC(SENSOR_PZEM_USART)); // enable clock for USART port peripheral
63  rcc_periph_clock_enable(USART_RCC(SENSOR_PZEM_USART)); // enable clock for USART peripheral
64  gpio_set_mode(USART_PORT(SENSOR_PZEM_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(SENSOR_PZEM_USART)); // setup GPIO pin USART transmit
65  gpio_set_mode(USART_PORT(SENSOR_PZEM_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(SENSOR_PZEM_USART)); // setup GPIO pin USART receive
66  gpio_set(USART_PORT(SENSOR_PZEM_USART), USART_PIN_RX(SENSOR_PZEM_USART)); // pull up to avoid noise when not connected
67 
68  /* setup USART parameters for electricity meter: 9600 8N1 */
69  usart_set_baudrate(USART(SENSOR_PZEM_USART), 9600); // the electricity meter uses a fixed baud rate of 9600 bps
70  usart_set_databits(USART(SENSOR_PZEM_USART), 8);
71  usart_set_stopbits(USART(SENSOR_PZEM_USART), USART_STOPBITS_1);
72  usart_set_mode(USART(SENSOR_PZEM_USART), USART_MODE_TX_RX);
73  usart_set_parity(USART(SENSOR_PZEM_USART), USART_PARITY_NONE);
74  usart_set_flow_control(USART(SENSOR_PZEM_USART), USART_FLOWCONTROL_NONE);
75 
76  nvic_enable_irq(USART_IRQ(SENSOR_PZEM_USART)); // enable the USART interrupt
77  usart_enable_rx_interrupt(USART(SENSOR_PZEM_USART)); // enable receive interrupt
78  usart_enable(USART(SENSOR_PZEM_USART)); // enable USART
79 
80  // setup timer to wait for minimal time before next transmission (after previous transmission or reception)
81  rcc_periph_clock_enable(RCC_TIM(SENSOR_PZEM_TIMER)); // enable clock for timer block
82  timer_reset(TIM(SENSOR_PZEM_TIMER)); // reset timer state
83  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
84  timer_one_shot_mode(TIM(SENSOR_PZEM_TIMER)); // stop counter after update event (we only need to count down once)
85  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 )
86  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)
87  timer_clear_flag(TIM(SENSOR_PZEM_TIMER), TIM_SR_UIF); // clear flag
88  timer_enable_irq(TIM(SENSOR_PZEM_TIMER), TIM_DIER_UIE); // enable update interrupt for timer
89  nvic_enable_irq(NVIC_TIM_IRQ(SENSOR_PZEM_TIMER)); // catch interrupt in service routine
90 
91  /* reset buffer states */
93  rx_i = 0;
95 }
96 
98 {
99  if (tx_i<LENGTH(tx_buffer)) { // transmission is ongoing
100  return;
101  }
102  if (type>=SENSOR_PZEM_MAX) { // invalid type
103  return;
104  }
105  tx_buffer[0] = 0xB0+type; // set request nibble and type nibble
106  tx_buffer[1] = (address>>24)&0xff; // set address
107  tx_buffer[2] = (address>>16)&0xff; // set address
108  tx_buffer[3] = (address>>8)&0xff; // set address
109  tx_buffer[4] = (address>>0)&0xff; // set address
110  tx_buffer[5] = 0; // only used to set alarm
111  tx_buffer[6] = 0; // to calculate checksum (sum of all previous bytes)
112  for (uint8_t i=0; i<LENGTH(tx_buffer)-1; i++) {
113  tx_buffer[6] += tx_buffer[i]; // calculate buffer
114  }
115  tx_i = 0; // remember we have a message to send
116 
117  if (TIM_CR1(TIM(SENSOR_PZEM_TIMER))&TIM_CR1_CEN) { // timer is already running
118  // at the end of the timer the transmission will start automatically
119  } else { // no timer is running
120  usart_enable_tx_interrupt(USART(SENSOR_PZEM_USART)); // enable interrupt to start sending bytes
121  //usart_send(USART(SENSOR_PZEM_USART),tx_buffer[tx_i++]); // start transmission
122  }
123 
124  sensor_pzem_measurement_received = false; // reset flag
125  rx_i = 0; // prepare buffer to receive next measurement
126 }
127 
129 {
130  struct sensor_pzem_measurement_t measurement; // decoded measurement to return
131  measurement.valid = false; // wait until the end to ensure validity
132  if (rx_i<LENGTH(rx_buffer)) { // buffer is not full, thus no measurement received
133  return measurement;
134  }
135  if ((rx_buffer[0]&0xf0)!=0xa0) { // not a response received
136  return measurement;
137  }
138  if ((rx_buffer[0]&0x0f)>=SENSOR_PZEM_MAX) { // not a valid response type received (actually 4 and 5 are valid, but should not happen when using this code
139  return measurement;
140  }
141  uint8_t checksum = 0; // calculate checksum (sum of all other bytes)
142  for (uint8_t i=0; i<LENGTH(rx_buffer)-1; i++) {
143  checksum += rx_buffer[i]; // calculate buffer
144  }
145  if (checksum!=rx_buffer[6]) { // checksum does not match
146  return measurement;
147  }
148  measurement.valid = true; // all checks passed
149  measurement.type = rx_buffer[0]&0x0f; // save type
150  switch (measurement.type) { // decode value depending on type
151  case SENSOR_PZEM_VOLTAGE:
152  measurement.value.voltage = ((uint16_t)rx_buffer[1]<<8)+rx_buffer[2]+rx_buffer[3]*0.1;
153  break;
154  case SENSOR_PZEM_CURRENT:
155  measurement.value.current = rx_buffer[2]+rx_buffer[3]*0.01;
156  break;
157  case SENSOR_PZEM_POWER:
158  measurement.value.power = ((uint16_t)rx_buffer[1]<<8)+rx_buffer[2];
159  break;
160  case SENSOR_PZEM_ENERGY:
161  measurement.value.energy = ((uint32_t)rx_buffer[1]<<16)+((uint16_t)rx_buffer[2]<<8)+rx_buffer[3];
162  break;
163 /* not used in this application
164  case SENSOR_PZEM_ADDRESS:
165  case SENSOR_PZEM_ALARM:
166  break; // no value is returned
167 */
168  default:
169  measurement.valid = false; // unexpected type
170  }
171  sensor_pzem_measurement_received = false; // reset flag
172  rx_i = 0; // prepare buffer to receive next measurement
173  return measurement;
174 }
175 
178 {
179  if (usart_get_interrupt_source(USART(SENSOR_PZEM_USART), USART_SR_TXE)) { // data has been transmitted
180  if (tx_i<LENGTH(tx_buffer)) { // not all bytes transmitted
181  usart_send(USART(SENSOR_PZEM_USART),tx_buffer[tx_i++]); // transmit next byte
182  } else { // request transmitted
183  usart_disable_tx_interrupt(USART(SENSOR_PZEM_USART)); // disable transmit interrupt
184  timer_set_counter(TIM(SENSOR_PZEM_TIMER), 0); // reset timer counter to get preset waiting time
185  timer_enable_counter(TIM(SENSOR_PZEM_TIMER)); // start timer between requests
186 
187  }
188  }
189  if (usart_get_interrupt_source(USART(SENSOR_PZEM_USART), USART_SR_RXNE)) { // data has been received
190  if (rx_i<LENGTH(rx_buffer)) { // receiving response
191  rx_buffer[rx_i++] = usart_recv(USART(SENSOR_PZEM_USART)); // put received byte in buffer
192  if (rx_i>=LENGTH(rx_buffer)) { // buffer full
193  sensor_pzem_measurement_received = true; // notify used response has been received
194  }
195  } else { // previous response not read before receiving the next
196  usart_recv(USART(SENSOR_PZEM_USART)); // drop received buffer
197  }
198  timer_set_counter(TIM(SENSOR_PZEM_TIMER), 0); // reset timer counter to get preset waiting time
199  timer_enable_counter(TIM(SENSOR_PZEM_TIMER)); // start timer between requests
200  }
201 }
202 
205 {
206  if (timer_get_flag(TIM(SENSOR_PZEM_TIMER), TIM_SR_UIF)) { // update event happened
207  timer_clear_flag(TIM(SENSOR_PZEM_TIMER), TIM_SR_UIF); // clear flag
208  if (tx_i<LENGTH(tx_buffer)) { // bytes are waiting to be sent
209  usart_enable_tx_interrupt(USART(SENSOR_PZEM_USART)); // enable interrupt to start sending bytes
210  }
211  }
212 }
213 
214 
library to query measurements from peacefair PZEM-004 and PZEM-004T electricity meter (API) ...
#define TIM_ISR(x)
get interrupt service routine for timer base on TIM identifier
Definition: global.h:113
void sensor_pzem_setup(void)
setup peripherals to communicate with electricity meter
Definition: sensor_pzem.c:58
measurement returned by electricity meter
Definition: sensor_pzem.h:38
enum sensor_pzem_measurement_type_t type
measurement type
Definition: sensor_pzem.h:39
#define USART_ISR(x)
get interrupt service routine for USART based on USART identifier
Definition: global.h:188
void sensor_pzem_measurement_request(uint32_t address, enum sensor_pzem_measurement_type_t type)
request measurement from electricity meter
Definition: sensor_pzem.c:97
#define NVIC_TIM_IRQ(x)
get NVIC IRQ for timer base on TIM identifier
Definition: global.h:111
static volatile uint8_t rx_i
current position of read received data
Definition: sensor_pzem.c:52
global definitions and methods (API)
#define SENSOR_PZEM_USART
USART peripheral.
Definition: sensor_pzem.c:41
#define RCC_TIM(x)
get RCC for timer based on TIM identifier
Definition: global.h:109
struct sensor_pzem_measurement_t sensor_pzem_measurement_decode(void)
decode received measurement
Definition: sensor_pzem.c:128
#define SENSOR_PZEM_TIMER
timer peripheral
Definition: sensor_pzem.c:47
static uint8_t tx_buffer[7]
buffer for request to transmit
Definition: sensor_pzem.c:53
static volatile uint8_t tx_i
current position of transmitted data
Definition: sensor_pzem.c:54
#define USART_IRQ(x)
get NVIC IRQ for USART based on USART identifier
Definition: global.h:186
#define USART(x)
get USART based on USART identifier
Definition: global.h:182
#define USART_PORT(x)
get port for USART based on USART identifier
Definition: global.h:190
#define USART_PORT_RCC(x)
get RCC for USART port based on USART identifier
Definition: global.h:195
uint32_t energy
measured energy in watts/hour (24 bits)
Definition: sensor_pzem.h:46
sensor_pzem_measurement_type_t
measurements (and configurations) offered by electricity meter
Definition: sensor_pzem.h:27
volatile bool sensor_pzem_measurement_received
a measurement response has been received
Definition: sensor_pzem.c:56
bool valid
is the measurement valid (e.g.
Definition: sensor_pzem.h:40
uint16_t power
measured power in watts
Definition: sensor_pzem.h:45
#define LENGTH(x)
get the length of an array
Definition: global.h:26
union sensor_pzem_measurement_t::measurement_t value
measurement value
float current
measured current in amperes
Definition: sensor_pzem.h:44
float voltage
measured voltage in volts
Definition: sensor_pzem.h:43
#define USART_RCC(x)
get RCC for USART based on USART identifier
Definition: global.h:184
#define TIM(x)
get TIM based on TIM identifier
Definition: global.h:107
#define USART_PIN_RX(x)
get receive pin for USART based on USART identifier
Definition: global.h:202
#define USART_PIN_TX(x)
get transmit pin for USART based on USART identifier
Definition: global.h:200
static uint8_t rx_buffer[7]
buffer for received response
Definition: sensor_pzem.c:51