ST AN2799 APPLICATION NOTE

AN2799
Application note
Measuring mains power consumption
with the STM32x and STPM01
Introduction
This document describes a software and hardware solution concerning the STM32x microcontroller and the STPM01 power meter for measuring mains power consumption and also provides hardware and firmware guidelines to interface the STPM01 with the STM32x microcontroller. Figure 1 shows the block diagram of the solution. The system described in this document is the first “brick” to build a complex system for distributed load management.

Figure 1. Block diagram

AM00076v1
November 2008 Rev 1 1/14
www.st.com
Contents AN2799
Contents
1 File organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.1 STM32F103xx microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1.1 STPM01 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 Functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.1 Power meter object description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.1.1 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.1.2 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.1.3 Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.1.4 Deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 Hardware abstraction layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2.1 HAL functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.3 Application interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.3.1 Application interface functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2/14
AN2799 File organization

1 File organization

The following table presents the firmware modules.

Table 1. Firmware modules

File Description
PowerMeterHal.h
PowerMeterHalTypes.h
PowerMeterObj.h
PowerMeterTypes.h
PowerMeterHal.c PowerMeterObj.c
Meterlayer.h Definitions of application interface types and function prototypes
Meter.c
MeterHal.c
ExampleApp.c Library usage example

2 Hardware

The solution is based on the STM32F103xx in the LQFP64 package with 128 kB of Flash and 20 kB of SRAM for demonstration purposes, but it can be easily ported on the smaller STM32F101xx microcontroller family based on the same ARM Cortex™-M3 CPU as well. The device used to measure the phase AC current is the STPM01 with a current transformer.
Definitions of power meter hardware abstraction layer types and function prototypes.
Definitions of power meter abstract objects and types.
Power meter hardware abstraction layer and abstract object implementation
Application interface and hardware abstraction layer implementation
3/14
Hardware AN2799

2.1 STM32F103xx microcontroller

The following list is a brief description of the features of the STM32F103xx microcontroller. Please refer to the STM32F103xx datasheet for a more detailed description of the device.
Core: ARM 32-bit Cortex™-M3 CPU
72 MHz, 90 DMIPS with 1.25 DMIPS/MHz
Single-cycle multiplication and hardware division
Memory
32-to-128 Kbytes of Flash memory
6-to-20 Kbytes of SRAM
Clock, reset and supply management
2.0 to 3.6 V application supply and I/Os
POR, PDR, and programmable voltage detector (PVD)
4-to-16 MHz quartz oscillator
Internal 8 MHz factory-trimmed RC
Internal 40 kHz RC
PLL for CPU clock
32 kHz oscillator for RTC with calibration
Low power
Sleep, stop and standby modes
VBAT supply for RTC and backup registers
2 x 12-bit, 1 µs A/D converters (16-channel)
Conversion range: 0 to 3.6 V
Dual-sample and hold capability
Temperature sensor
DMA
7-channel DMA controller
Peripherals supported: timers, ADC, SPIs, I
Debug mode
Serial wire debug (SWD) & JTAG interfaces
Up to 80 fast I/O ports
26/36/51/80 I/Os, all mappable on 16 external interrupt vectors, all 5 V-tolerant
except for analog inputs
Up to 7 timers
Up to three 16-bit timers, each with up to 4 IC/OC/PWM or pulse counter
16-bit, 6-channel advanced control timer: up to 6 channels for PWM output dead
time generation and emergency stop
2 watchdog timers (independent and window)
SysTick timer: a 24-bit down counter
Up to 9 communication interfaces
Up to 2 I
2
C interfaces (SMBus/PMBus)
2
Cs and USARTs
4/14
AN2799 Hardware
Up to 3 USARTs (ISO 7816 interface, LIN, IrDA capability, modem control)
Up to 2 SPIs (18 Mbit/s)
CAN interface (2.0B Active)
USB 2.0 full speed interface
Packages are ECOPACK
®
(RoHS compliant)

2.1.1 STPM01

The STPM01 is a programmable single-phase energy metering IC which is designed for effective measurement of active, reactive and apparent energy in a power line system using a Rogowski coil, a current transformer and shunt sensors. The solution presented in this document is based on a current transformer sensor. The following list is a brief description of the main features of the STPM01. For more details, please refer to the device datasheet.
Active, reactive, apparent energies and RMS values
Ripple-free active energy pulsed output
Live and neutral monitoring for tamper detection
Easy and fast digital calibration in only one point over the whole current range
OTP for calibration and configuration
Integrated linear V
Selectable RC or crystal oscillator
Supports 50-60 Hz - IEC62052-11, IEC62053-2X specification
Less than 0.1% error
Precision voltage reference: 1.23 V and 30 ppm/°C max
for digital and analog supply
REGS

2.2 Hardware description

The hardware used in managing the STPM01 by the STM32x microcontroller is described in this section. Figure 2 shows a reference schematic to interface the STM32x microcontroller with the STPM01. The STPM01 bidirectional data line is connected to the SPI-MISO of the STM32. This is because the speeds for the data read and data write operations of the STPM01 SPI interface are much too different: 32 MHz for the read operation and 100 kHz for the write operation. In fact during the write operation, the serial communication is emulated by software to have very slow communication on the STPM01 data line and during the read operation, the STM32 SPI peripheral is used to reach the maximum speed possible. Since the maximum speed of the STM32 SPI is 18 MHz, it doesn't matter which SPI peripheral is used to have the maximum read bit rate. In this application the SPI2 is used. The STPM01 clock is provided by the PA8 pin configured as the MCO alternate function. The AC current is provided to the STPM01 by a current transformer connected to the current channel 1. For more details about analog parts related to the STPM01, please refer to the STPM01 datasheet.
Note: The solution has been validated using the “SmartPlug” board. For further details please
refer to the “SmartPlug hardware user manual” .
5/14
Loading...
+ 9 hidden pages