add SDM120 electricity meter library (only transmit works

This commit is contained in:
King Kévin 2016-09-13 22:42:33 +02:00
parent 2d8edda833
commit 8916359b7e
1 changed files with 226 additions and 0 deletions

226
lib/sensor_sdm120.c Normal file
View File

@ -0,0 +1,226 @@
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** library to query measurements from eastron SDM120-ModBus electricity meter (code)
* @file sensor_sdm120.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2016
* @note peripherals used: USART @ref sensor_sdm120_usart , GPIO @ref sensor_sdm120_gpio
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdlib.h> // general utilities
#include <math.h> // mathematical utilities
/* STM32 (including CM3) libraries */
#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 "sensor_sdm120.h" // SDM120 electricity meter header and definitions
#include "global.h" // common methods
/** @defgroup sensor_psdm120_usart USART peripheral used for communication with electricity meter
* @{
*/
#define SENSOR_SDM120_USART 3 /**< USART peripheral */
/** @} */
/** @defgroup sensor_psdm120_gpio GPIO peripheral used for controlling RS-485 adapter
* @note driver output is enabled on high while receiver output is enabled on low, thus one pin can be used to control both
* @{
*/
#define SENSOR_SDM120_REDE_PORT B /**< GPIO port for RS-485 receiver and driver output enable signal */
#define SENSOR_SDM120_REDE_PIN 12 /**< GPIO pin for RS-485 receiver and driver output enable signal */
/** @} */
/* input and output ring buffer, indexes, and available memory */
static uint8_t rx_buffer[9] = {0}; /**< buffer for received response (ModBus response messages can be 2+256+2 long but we will only read up to 2 registers) */
static volatile uint8_t rx_i = 0; /**< current position of read received data */
static uint8_t tx_buffer[13] = {0}; /**< buffer for request to transmit (ModBus request messages can be 7+256+2 long but we will only write up to 2 registers */
static volatile uint8_t tx_i = 0; /**< current position of transmitted data */
static volatile uint8_t tx_size = 0; /**< size of data to transmitted */
volatile bool sensor_sdm120_measurement_received = false;
/** SDM120 3xxxx input register start addresses for the measurement types */
static const uint16_t register_input[] = {
0x0000, // 30001 voltage (in volts)
0x0006, // 30007 current (in amperes)
0x000c, // 30013 active power (in watts)
0x0012, // 30019 apparent power (in volt amperes)
0x0018, // 30025 reactive power (in volt amperes reactive)
0x001e, // 30031 power factor (0-1)
0x0046, // 30071 frequency (in hertz)
0x0048, // 30073 import active energy (in kWh)
0x004a, // 30075 export active energy (in kWh)
0x004c, // 30077 import reactive energy (in kVArh)
0x004e, // 30079 export reactive energy (in kVArh)
0x0156, // 30343 total active energy (in kWh)
0x0158 // 30345 total reactive energy (in kVArh)
};
/** compute CRC for ModBus
* @note ModBus uses ANSi/IBM 16-bits CRC (with normal polynomial 0x8005, reverse polynomial 0xA001, start value 0xfff)
* @param[in] buffer data on which to compute the CRC for
* @param[in] number of byte to compute the CRC for
* @return computed CRC checksum
*/
static uint16_t crc_modbus(uint8_t* buffer, uint8_t size)
{
uint16_t crc = 0xffff; // initial value (for ModBus)
for (uint8_t i=0; i<size; i++) { // go through every byte
crc ^= (uint16_t)buffer[i]; // XOR byte
for (uint8_t b=0; b<8; b++) { // go through every bit
if (crc&0x0001) { // least significant bit is set (we are using the reverse way)
crc = (crc>>1)^0xA001; // // shift to the right (for the next bit) and XOR with (reverse) polynomial
} else {
crc >>= 1; // just shift right (for the next bit)
}
}
}
return crc;
}
void sensor_sdm120_setup(void)
{
// enable USART I/O peripheral
rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
rcc_periph_clock_enable(USART_PORT_RCC(SENSOR_SDM120_USART)); // enable clock for USART port peripheral
rcc_periph_clock_enable(USART_RCC(SENSOR_SDM120_USART)); // enable clock for USART peripheral
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
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
gpio_set(USART_PORT(SENSOR_SDM120_USART), USART_PIN_RX(SENSOR_SDM120_USART)); // pull up to avoid noise when not connected (it will be set low by RS485 chip when RO is enabled)
// setup USART parameters for electricity meter
usart_set_baudrate(USART(SENSOR_SDM120_USART), 2400); // get baud rate by scrolling through the measurements on the electricity meter's screen (default 2400)
usart_set_databits(USART(SENSOR_SDM120_USART), 8);
usart_set_stopbits(USART(SENSOR_SDM120_USART), USART_STOPBITS_1);
usart_set_mode(USART(SENSOR_SDM120_USART), USART_MODE_TX_RX);
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)
usart_set_flow_control(USART(SENSOR_SDM120_USART), USART_FLOWCONTROL_NONE);
nvic_enable_irq(USART_IRQ(SENSOR_SDM120_USART)); // enable the USART interrupt
usart_enable_rx_interrupt(USART(SENSOR_SDM120_USART)); // enable receive interrupt
usart_enable(USART(SENSOR_SDM120_USART)); // enable USART
// setup GPIO
rcc_periph_clock_enable(RCC_GPIO(SENSOR_SDM120_REDE_PORT)); // enable clock for GPIO peripheral
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
gpio_clear(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // disable driver output and enable receive output
// reset states
tx_i = 0;
tx_size = 0;
rx_i = 0;
sensor_sdm120_measurement_received = false;
}
void sensor_sdm120_measurement_request(uint8_t address, enum sensor_sdm120_measurement_type_t type)
{
if (tx_i!=0) { // transmission is ongoing
return;
}
if (type>=SENSOR_SDM120_MAX) { // invalid type
return;
}
tx_buffer[0] = address; // set slave device address
tx_buffer[1] = 0x04; // set function to read 3X registers
tx_buffer[2] = register_input[type]>>8; // set high register address
tx_buffer[3] = register_input[type]; // set low register address
tx_buffer[4] = 0; // set high number of registers to read
tx_buffer[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
uint16_t crc = crc_modbus(tx_buffer, 6); // compute error check
tx_buffer[6] = crc; // set low error check
tx_buffer[7] = crc>>8; // set high error check
tx_size = 8; // set request size
gpio_set(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // enable driver output and disable receive output
USART_SR(USART(SENSOR_SDM120_USART)) &= USART_SR_TXE; // clear interrupt flag
usart_enable_tx_interrupt(USART(SENSOR_SDM120_USART)); // enable interrupt to send other bytes
usart_send(USART(SENSOR_SDM120_USART),tx_buffer[tx_i++]); // start transmission
}
float sensor_sdm120_measurement_decode(void)
{
float measurement = 0; //NAN; // decoded measurement to return (invalid in the beginning)
if (!sensor_sdm120_measurement_received) { // no measurement received
return NAN;
} else {
sensor_sdm120_measurement_received = false; // reset flag
}
if (rx_i<5) { // not a complete response (minimum is address, function, size/error, error check low, error check high)
return NAN;
}
if (crc_modbus(rx_buffer,rx_i)) { // checksum error
return NAN;
}
if (rx_buffer[1]&0x80) { // error condition received
return measurement; // TODO decode the error condition
} else if (rx_buffer[1]==0x04) { // read 3xxxx input register response received
if (rx_i<5 || rx_i-5!=tx_buffer[2]) { // wrong response size
return NAN;
}
if (tx_buffer[2]!=4) { // the code only supports reading a measurement on two registers
return NAN;
}
measurement = (float)((tx_buffer[3]<<24)+(tx_buffer[4]<<16)+(tx_buffer[5]<<8)+(tx_buffer[6]<<0)); // decode big endian encoded float measurement value
measurement = 0;
} else if (rx_buffer[1]==0x04) { // read 4xxx holding register response received
// not supported currently
} else if (rx_buffer[1]==0x10) { // write 4xxx holding register response received
// not supported currently
} else { // unknown function response received
return NAN;
}
return measurement;
}
/** USART interrupt service routine called when data has been transmitted or received */
void USART_ISR(SENSOR_SDM120_USART)(void)
{
if (usart_get_interrupt_source(USART(SENSOR_SDM120_USART), USART_SR_TXE)) { // data has been transmitted
if (tx_i<tx_size && tx_i<LENGTH(tx_buffer)) { // not all bytes transmitted
usart_send(USART(SENSOR_SDM120_USART),tx_buffer[tx_i++]); // transmit next byte (clears flag)
} else { // request transmitted
usart_disable_tx_interrupt(USART(SENSOR_SDM120_USART)); // disable transmit interrupt
USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_TXE; // clear flag
USART_CR1(USART(SENSOR_SDM120_USART)) |= USART_CR1_TCIE; // enable transfer complete interrupt
tx_i = 0; // ready for next transmission
}
}
if (usart_get_interrupt_source(USART(SENSOR_SDM120_USART), USART_SR_TC)) { // data has been transmitted
USART_CR1(USART(SENSOR_SDM120_USART)) |= USART_CR1_TCIE; // disable transfer complete interrupt
USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_TC; // clear flag
gpio_clear(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // disable driver output and enable receive output
tx_i = 0; // ready for next transmission
}
if (usart_get_interrupt_source(USART(SENSOR_SDM120_USART), USART_SR_RXNE)) { // data has been received
led_toggle();
if (rx_i<LENGTH(rx_buffer)) { // receiving response
rx_buffer[rx_i++] = usart_recv(USART(SENSOR_SDM120_USART)); // put received byte in buffer (clears flag)
if (rx_i>=5 && rx_buffer[2]&0x80) { // error condition response received
sensor_sdm120_measurement_received = true; // notify used response has been received
} else if (rx_i>=5 && (uint8_t)(rx_i-5)>=rx_buffer[3] && (rx_buffer[2]==0x04 || rx_buffer[2]==0x03)) { // read input or holding register response received
sensor_sdm120_measurement_received = true; // notify used response has been receive
} else if (rx_i>=8 && rx_buffer[2]==0x10) { // write holding register response received
sensor_sdm120_measurement_received = true; // notify used response has been receive
}
} else { // buffer full and unknown response received
rx_i = 0; // clear buffer (not clearing the flag will restart this ISR)
//USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_RXNE; // clear flag
}
}
}