CuVoodoo STM32F1 firmware template
sensor_sdm120.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  */
21 /* standard libraries */
22 #include <stdint.h> // standard integer types
23 #include <stdlib.h> // general utilities
24 #include <math.h> // mathematical utilities
25 
26 /* STM32 (including CM3) libraries */
27 #include <libopencm3/stm32/rcc.h> // real-time control clock library
28 #include <libopencm3/stm32/gpio.h> // general purpose input output library
29 #include <libopencm3/stm32/usart.h> // universal synchronous asynchronous receiver transmitter library
30 #include <libopencm3/stm32/timer.h> // timer utilities
31 #include <libopencm3/cm3/nvic.h> // interrupt handler
32 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
33 
34 #include "sensor_sdm120.h" // SDM120 electricity meter header and definitions
35 #include "global.h" // common methods
36 
40 #define SENSOR_SDM120_USART 3
47 #define SENSOR_SDM120_REDE_PORT B
48 #define SENSOR_SDM120_REDE_PIN 12
55 #define SENSOR_SDM120_TIMER 3
58 /* input and output ring buffer, indexes, and available memory */
59 static uint8_t rx_buffer[9] = {0};
60 static volatile uint8_t rx_used = 0;
61 static uint8_t tx_buffer[13] = {0};
62 static volatile uint8_t tx_used = 0;
64 volatile bool sensor_sdm120_measurement_received = false;
65 
67 static enum timeout_t {
72 } timeout;
74 static uint16_t timeout_times[TIMEOUT_MAX] = {0};
75 
77 static const uint16_t register_input[] = {
78  0x0000, // 30001 voltage (in volts)
79  0x0006, // 30007 current (in amperes)
80  0x000c, // 30013 active power (in watts)
81  0x0012, // 30019 apparent power (in volt amperes)
82  0x0018, // 30025 reactive power (in volt amperes reactive)
83  0x001e, // 30031 power factor (0-1)
84  0x0046, // 30071 frequency (in hertz)
85  0x0048, // 30073 import active energy (in kWh)
86  0x004a, // 30075 export active energy (in kWh)
87  0x004c, // 30077 import reactive energy (in kVArh)
88  0x004e, // 30079 export reactive energy (in kVArh)
89  0x0156, // 30343 total active energy (in kWh)
90  0x0158 // 30345 total reactive energy (in kVArh)
91 };
92 
94 static const uint16_t register_holding[] = {
95  0x000c, // relay pulse width (60, 100, or 200 ms)
96  0x0012, // network parity stop (0: 1 stop bit no parity, 1: one stop bit even parity, 2: one stop bit odd parity, 3: two stop bits no parity)
97  0x0014, // meter slave address (1-247)
98  0x001c, // baud rate (0: 2400 bps, 1: 4800 bps, 2: 9600 bps, 5: 1200 bps)
99  0x0056, // pulse 1 output mode (1: import active energy, 2: import+export active energy, 4: export active energy, 5: import reactive energy, 6: import+export reactive energy, 8: export reactive energy)
100  0xf900, // time of scroll display (0-30 s)
101  0xf910, // pulse 1 output (0: 0.001 kWh/imp, 1: 0.01 kWh/imp, 2: 0.1 kWh/imp, 3: 1 kWh/imp)
102  0xf920 // measurement mode (1: total=import, 2: total=import+export, 3: total=import-export)
103 };
104 
111 static uint16_t crc_modbus(uint8_t* buffer, uint8_t size)
112 {
113  uint16_t crc = 0xffff; // initial value (for ModBus)
114  for (uint8_t i=0; i<size; i++) { // go through every byte
115  crc ^= (uint16_t)buffer[i]; // XOR byte
116  for (uint8_t b=0; b<8; b++) { // go through every bit
117  if (crc&0x0001) { // least significant bit is set (we are using the reverse way)
118  crc = (crc>>1)^0xA001; // // shift to the right (for the next bit) and XOR with (reverse) polynomial
119  } else {
120  crc >>= 1; // just shift right (for the next bit)
121  }
122  }
123  }
124  return crc;
125 }
126 
127 void sensor_sdm120_setup(uint32_t baudrate)
128 {
129  // enable USART I/O peripheral
130  rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
131  rcc_periph_clock_enable(USART_PORT_RCC(SENSOR_SDM120_USART)); // enable clock for USART port peripheral
132  rcc_periph_clock_enable(USART_RCC(SENSOR_SDM120_USART)); // enable clock for USART peripheral
133  gpio_set_mode(USART_PORT(SENSOR_SDM120_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(SENSOR_SDM120_USART)); // setup GPIO pin USART transmit
134  gpio_set_mode(USART_PORT(SENSOR_SDM120_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(SENSOR_SDM120_USART)); // setup GPIO pin USART receive
135  gpio_clear(USART_PORT(SENSOR_SDM120_USART), USART_PIN_RX(SENSOR_SDM120_USART)); // pull down to avoid noise when not connected (it will be set low by RS485 chip when RO is enabled)
136 
137  // setup USART parameters for electricity meter
138  usart_set_baudrate(USART(SENSOR_SDM120_USART), baudrate); // get baud rate by scrolling through the measurements on the electricity meter's screen (default 2400)
139  usart_set_databits(USART(SENSOR_SDM120_USART), 8);
140  usart_set_stopbits(USART(SENSOR_SDM120_USART), USART_STOPBITS_1);
141  usart_set_mode(USART(SENSOR_SDM120_USART), USART_MODE_TX_RX);
142  usart_set_parity(USART(SENSOR_SDM120_USART), USART_PARITY_NONE); // get parity by scrolling through the measurements on the electricity meter's screen (default none)
143  usart_set_flow_control(USART(SENSOR_SDM120_USART), USART_FLOWCONTROL_NONE);
144 
145  nvic_enable_irq(USART_IRQ(SENSOR_SDM120_USART)); // enable the USART interrupt
146  usart_enable_rx_interrupt(USART(SENSOR_SDM120_USART)); // enable receive interrupt
147  usart_enable(USART(SENSOR_SDM120_USART)); // enable USART
148 
149  // setup GPIO
150  rcc_periph_clock_enable(RCC_GPIO(SENSOR_SDM120_REDE_PORT)); // enable clock for GPIO peripheral
151  gpio_set_mode(GPIO(SENSOR_SDM120_REDE_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(SENSOR_SDM120_REDE_PIN)); // setup GPIO pin for receiver and driver output enable pin
152  gpio_clear(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // disable driver output and enable receive output
153 
154  // setup timer to wait for minimal time before next transmission
155  rcc_periph_clock_enable(RCC_TIM(SENSOR_SDM120_TIMER)); // enable clock for timer block
156  timer_reset(TIM(SENSOR_SDM120_TIMER)); // reset timer state
157  timer_set_mode(TIM(SENSOR_SDM120_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
158  timer_one_shot_mode(TIM(SENSOR_SDM120_TIMER)); // stop counter after update event (we only need to count down once)
159  timer_set_prescaler(TIM(SENSOR_SDM120_TIMER), 66-1); // set the prescaler so this 16 bits timer allows to wait for 60 ms ( 1/(72E6/66/(2**16))=60.07ms )
160  timeout_times[TIMEOUT_BEGIN] = (rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_SDM120_TIMER))+1))/baudrate/8/2.5; // wait at least 2.5 characters before sending data
161  timeout_times[TIMEOUT_END] = (rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_SDM120_TIMER))+1))/baudrate/8/2.5; // wait at least 2.5 characters after sending data
162  timeout_times[TIMEOUT_BETWEEN] = 0.06*(rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_SDM120_TIMER))+1)); // wait at least 60 ms before sending the next message
163  timer_clear_flag(TIM(SENSOR_SDM120_TIMER), TIM_SR_UIF); // clear flag
164  timer_enable_irq(TIM(SENSOR_SDM120_TIMER), TIM_DIER_UIE); // enable update interrupt for timer
165  nvic_enable_irq(NVIC_TIM_IRQ(SENSOR_SDM120_TIMER)); // catch interrupt in service routine
166 
167  // reset states
168  tx_used = 0;
169  rx_used = 0;
171 }
172 
180 static bool sensor_sdm120_transmit_request(uint8_t meter_id, uint8_t function, uint16_t address, float value)
181 {
182  if (meter_id==0) { // broadcast request are not supported
183  return false;
184  }
185  if (function!=0x03 && function!=0x04 && function!=0x10) { // function not supported
186  return false;
187  }
188  if (address%2) { // even register addresses are not supported by device
189  return false;
190  }
191  while (tx_used) { // transmission is ongoing
192  __WFI(); // wait until something happens (transmission ended)
193  }
194  // build request packet
195  uint8_t packet[11]; // buffer to build ModBus message (without error check)
196  uint8_t packet_size = 0; // ModBus message size (without error check)
197  packet[0] = meter_id; // set slave device address
198  packet[1] = function; // set function
199  packet[2] = address>>8; // set high register address
200  packet[3] = address; // set low register address
201  packet[4] = 0; // set high number of registers to read
202  packet[5] = 2; // set low number of register to read (the measurement are encoded using 32 bits IEE745 float, and register hold 16 bits, thus we want to read 2 registers
203  if (function==0x03 || function==0x04) { // read register
204  packet_size = 6; // set message size
205  } else if (function==0x10) { // write register
206  packet[6] = 4; // byte count (writing two 16 bits registers)
207  // store little endian encoded value in big endian encoded data
208  uint8_t* data = (uint8_t*)&value;
209  packet[7] = data[3];
210  packet[8] = data[2];
211  packet[9] = data[1];
212  packet[10] = data[0];
213  packet_size = 11; // set message size
214  }
215  uint16_t crc = crc_modbus(packet, packet_size); // compute error check
216  for (uint8_t i=0; i<packet_size; i++) {
217  tx_buffer[packet_size-i+1] = packet[i]; // copy packet to tx buffer in reverse order (this is how sending is implemented)
218  }
219  tx_buffer[1] = crc; // set low error check
220  tx_buffer[0] = crc>>8; // set high error check
221  tx_used = packet_size+2; // set request size
222  rx_used = 0; // reset reset buffer
223  sensor_sdm120_measurement_received = false; // reset measurement flag
224  while (TIM_CR1(TIM(SENSOR_SDM120_TIMER))&TIM_CR1_CEN) { // timer is already used
225  __WFI(); // wait until something happens (timer is available again)
226  }
227  gpio_set(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // enable driver output and disable receive output
228  // start timeout
229  timeout = TIMEOUT_BEGIN; // select time before sending message
230  timer_set_period(TIM(SENSOR_SDM120_TIMER), timeout_times[timeout]); // set corresponding timeout
231  timer_set_counter(TIM(SENSOR_SDM120_TIMER), 0); // reset timer counter to get preset waiting time
232  timer_enable_counter(TIM(SENSOR_SDM120_TIMER)); // wait
233 
234  return true;
235 }
236 
238 {
239  if (type>=SENSOR_SDM120_MEASUREMENT_MAX) { // invalid type
240  return false;
241  }
242  return sensor_sdm120_transmit_request(meter_id, 0x04, register_input[type], 0);
243 }
244 
246 {
247  if (type>=SENSOR_SDM120_CONFIGURATION_MAX) { // invalid type
248  return false;
249  }
250  return sensor_sdm120_transmit_request(meter_id, 0x03, register_holding[type], 0);
251 }
252 
254 {
255  if (type>=SENSOR_SDM120_CONFIGURATION_MAX) { // invalid type
256  return false;
257  }
258  return sensor_sdm120_transmit_request(meter_id, 0x10, register_holding[type], value);
259 }
260 
262 {
263  float measurement = NAN; // decoded measurement to return (invalid in the beginning)
264  if (!sensor_sdm120_measurement_received) { // no measurement received
265  return NAN;
266  } else {
267  sensor_sdm120_measurement_received = false; // reset flag
268  }
269  if (rx_used<5) { // not a complete response (minimum is address, function, size/error, error check low, error check high)
270  return NAN;
271  }
272  // a complete message has been received
273  if (crc_modbus(rx_buffer,rx_used)) { // checksum error, error check failed
274  measurement = NAN;
275  } else if (rx_buffer[1]&0x80) { // error condition received
276  measurement = INFINITY; // indicate we received and error
277  } else {
278  switch (rx_buffer[1]) {
279  case 0x03: // read 4xxx holding register response received
280  case 0x04: // read 3xxxx input register response received
281  if (rx_buffer[2]==0x04 && rx_used>=(4+5)) { // 2 registers received, corresponds to implemented request
282  // convert big endian received float value to little endian return value
283  uint8_t* convert = (uint8_t*)&measurement;
284  convert[0] = rx_buffer[6];
285  convert[1] = rx_buffer[5];
286  convert[2] = rx_buffer[4];
287  convert[3] = rx_buffer[3];
288  }
289  break;
290  case 0x10: // write 4xxx holding register response received
291  measurement = (rx_buffer[4]<<8)+rx_buffer[5]; // number of registers written
292  break; // not supported currently
293  default: // unknown function response received
294  measurement = INFINITY;
295  break; // nothing to do
296  }
297  }
298  rx_used = 0; // reset rx_buffer usage
299  return measurement;
300 }
301 
304 {
305  if (usart_get_interrupt_source(USART(SENSOR_SDM120_USART), USART_SR_TXE)) { // data has been transmitted
306  if (tx_used) { // not all bytes transmitted
307  usart_send(USART(SENSOR_SDM120_USART),tx_buffer[--tx_used]); // transmit next byte (clears flag)
308  } else { // all bytes transmitted
309  usart_disable_tx_interrupt(USART(SENSOR_SDM120_USART)); // disable transmit interrupt
310  USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_TXE; // clear flag
311  USART_CR1(USART(SENSOR_SDM120_USART)) |= USART_CR1_TCIE; // enable transfer complete interrupt
312  }
313  }
314  if (usart_get_interrupt_source(USART(SENSOR_SDM120_USART), USART_SR_TC)) { // data has been completely transmitted
315  USART_CR1(USART(SENSOR_SDM120_USART)) |= USART_CR1_TCIE; // disable transfer complete interrupt
316  USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_TC; // clear flag
317  timeout = TIMEOUT_END; // select wait time after sending data
318  timer_set_period(TIM(SENSOR_SDM120_TIMER), timeout_times[timeout]); // set corresponding timeout
319  timer_set_counter(TIM(SENSOR_SDM120_TIMER), 0); // reset timer counter to get preset waiting time
320  timer_enable_counter(TIM(SENSOR_SDM120_TIMER)); // wait
321  }
322  if (usart_get_interrupt_source(USART(SENSOR_SDM120_USART), USART_SR_RXNE)) { // data has been received
323  if (gpio_get(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN))) { // not in receiver mode
324  USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_RXNE; // clear flag, ignore received data
325  } else if (rx_used<LENGTH(rx_buffer)) { // receiving response
326  rx_buffer[rx_used++] = usart_recv(USART(SENSOR_SDM120_USART)); // put received byte in buffer (clears flag)
327  if (rx_used==1 && rx_buffer[0]==0) { // this is wrong decoding because the signal is going low on idle, which is misinterpreted as start bit (and the 0 broadcast device address is not supported by this device)
328  rx_used = 0; // reset buffer
329  } else if (rx_used>=5 && (rx_buffer[1]&0x80)) { // error condition response received
330  sensor_sdm120_measurement_received = true; // notify used response has been received
331  } else if (rx_used>=5 && (uint8_t)(rx_used-5)>=rx_buffer[2] && (rx_buffer[1]==0x04 || rx_buffer[1]==0x03)) { // read input or holding register response received
332  sensor_sdm120_measurement_received = true; // notify used response has been receive
333  } else if (rx_used>=8 && rx_buffer[1]==0x10) { // write holding register response received
334  sensor_sdm120_measurement_received = true; // notify used response has been receive
335  }
336  } else { // buffer full and unknown response received
337  USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_RXNE; // clear flag (wait for user to read measurement, this clears the buffer)
338  }
339  timeout = TIMEOUT_END; // select time after receiving data
340  timer_set_period(TIM(SENSOR_SDM120_TIMER), timeout_times[timeout]); // set corresponding timeout
341  timer_set_counter(TIM(SENSOR_SDM120_TIMER), 0); // reset timer counter to get preset waiting time
342  timer_enable_counter(TIM(SENSOR_SDM120_TIMER)); // wait
343  }
344 }
345 
348 {
349  if (timer_get_flag(TIM(SENSOR_SDM120_TIMER), TIM_SR_UIF)) { // update event happened
350  timer_clear_flag(TIM(SENSOR_SDM120_TIMER), TIM_SR_UIF); // clear flag
351  // because of the one pulse mode the timer is stopped automatically
352  switch (timeout) { // timeout before action passed
353  case (TIMEOUT_BEGIN): // we can now send the data
354  USART_SR(USART(SENSOR_SDM120_USART)) &= USART_SR_TXE; // clear interrupt flag
355  usart_enable_tx_interrupt(USART(SENSOR_SDM120_USART)); // enable interrupt to send other bytes
356  usart_send(USART(SENSOR_SDM120_USART),tx_buffer[--tx_used]); // start transmission
357  break;
358  case (TIMEOUT_END): // we now have to wait before sending the next message
359  gpio_clear(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // disable driver output (and enable receive output)
360  timeout = TIMEOUT_BETWEEN; // select time between sending message
361  timer_set_period(TIM(SENSOR_SDM120_TIMER), timeout_times[timeout]); // set corresponding timeout
362  timer_set_counter(TIM(SENSOR_SDM120_TIMER), 0); // reset timer counter to get preset waiting time
363  timer_enable_counter(TIM(SENSOR_SDM120_TIMER)); // wait
364  case (TIMEOUT_BETWEEN): // nothing to do, we are allowed to send the next message
365  break;
366  default:
367  break;
368  }
369  }
370 }
371 
#define TIM_ISR(x)
get interrupt service routine for timer base on TIM identifier
Definition: global.h:113
sensor_sdm120_configuration_type_t
configuration types for electricity meter in 4xxx holding registers
Definition: sensor_sdm120.h:45
static volatile uint8_t tx_used
number of byte to transmit
Definition: sensor_sdm120.c:62
#define RCC_GPIO(x)
get RCC for GPIO based on GPIO identifier
Definition: global.h:105
float sensor_sdm120_measurement_decode(void)
decode received measurement
static volatile uint8_t rx_used
number of received data bytes in buffer
Definition: sensor_sdm120.c:60
void sensor_sdm120_setup(uint32_t baudrate)
setup peripherals to communicate with electricity meter
enum sensor_pzem_measurement_type_t type
measurement type
Definition: sensor_pzem.h:39
silent time before sending data
Definition: sensor_sdm120.c:68
library to query measurements from eastron SDM120-ModBus electricity meter (API)
#define USART_ISR(x)
get interrupt service routine for USART based on USART identifier
Definition: global.h:188
#define NVIC_TIM_IRQ(x)
get NVIC IRQ for timer base on TIM identifier
Definition: global.h:111
volatile bool sensor_sdm120_measurement_received
a measurement response has been received
Definition: sensor_sdm120.c:64
sensor_sdm120_measurement_type_t
measurement types offered by electricity meter in 3xxx input registers
Definition: sensor_sdm120.h:27
silent time after sending data
Definition: sensor_sdm120.c:69
global definitions and methods (API)
static const uint16_t register_input[]
SDM120 3xxxx input register start addresses for the measurement types.
Definition: sensor_sdm120.c:77
#define GPIO(x)
get GPIO based on GPIO identifier
Definition: global.h:103
static uint16_t crc_modbus(uint8_t *buffer, uint8_t size)
compute CRC for ModBus
#define SENSOR_SDM120_TIMER
timer number to count time
Definition: sensor_sdm120.c:55
bool sensor_sdm120_measurement_request(uint8_t meter_id, enum sensor_sdm120_measurement_type_t type)
request measurement from electricity meter
#define RCC_TIM(x)
get RCC for timer based on TIM identifier
Definition: global.h:109
bool sensor_sdm120_configuration_request(uint8_t meter_id, enum sensor_sdm120_configuration_type_t type)
request configuration from electricity meter
#define SENSOR_SDM120_USART
USART peripheral.
Definition: sensor_sdm120.c:40
#define USART_IRQ(x)
get NVIC IRQ for USART based on USART identifier
Definition: global.h:186
time to wait between messages
Definition: sensor_sdm120.c:70
#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
timeout_t
the ModBus timeouts to respect for sending messages
Definition: sensor_sdm120.c:67
static uint8_t * buffer
input/output buffer for read/write commands/functions
static uint16_t timeout_times[TIMEOUT_MAX]
current timeout used
Definition: sensor_sdm120.c:74
static uint8_t rx_buffer[9]
buffer for received response (ModBus response messages can be 2+256+2 long but we will only read up t...
Definition: sensor_sdm120.c:59
static bool sensor_sdm120_transmit_request(uint8_t meter_id, uint8_t function, uint16_t address, float value)
send request to electricity meter
static const uint16_t register_holding[]
SDM120 4xxxx holding register start addresses for the configuration types.
Definition: sensor_sdm120.c:94
static uint8_t tx_buffer[13]
buffer for request to transmit (ModBus request messages can be 7+256+2 long but we will only write up...
Definition: sensor_sdm120.c:61
last element (useful to no the number of elements)
Definition: sensor_sdm120.c:71
#define SENSOR_SDM120_REDE_PORT
GPIO port for RS-485 receiver and driver output enable signal.
Definition: sensor_sdm120.c:47
#define LENGTH(x)
get the length of an array
Definition: global.h:26
union sensor_pzem_measurement_t::measurement_t value
measurement value
#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
bool sensor_sdm120_configuration_set(uint8_t meter_id, enum sensor_sdm120_configuration_type_t type, float value)
set configuration in electricity meter
#define SENSOR_SDM120_REDE_PIN
GPIO pin for RS-485 receiver and driver output enable signal.
Definition: sensor_sdm120.c:48
static enum timeout_t timeout
the current timeout used