ST AN2780 Application note

AN2780

Application note

Real-time keeping on STM8S and STM8A devices and usage of auto-wakeup unit (AWU) and beeper

Introduction

The purpose of this application note is to explain how to keep real-time information on a STM8S or STM8A device maintained in low power mode.

Only simple solutions which do not require any additional hardware are described in this application note. They are suitable for applications which do not need a very accurate realtime measurement.

To benefit fully from the information in this application note, the user should be familiar with the STM8S and STM8A microcontroller architecture and with the basics of C language.

For further information on the STM8S and STM8A family features, pinout and electrical characteristics, please refer to the STM8S and STM8A datasheets.

An STM8S and STM8A firmware package is delivered with this application note. It contains the complete C source code of an example described in this application note, so that the user can compile, link or modify it as necessary.

This document and its associated firmware package, are written to accompany the STM8S and STM8A firmware library. It includes many examples describing how to use the STM8S and STM8A timers, the clock system, the beeper and the auto-wakeup unit mentioned in this application note.

Related documents (available for download from www.st.com):

AN2780 associated firmware package

STM8S and STM8A firmware library

AN2822 “STM8S and STM8A high speed internal oscillator calibration”

AN2857 “STM8S and STM8A family power management”

AN2645 “Migration and compatibility guidelines for STM8S and STM8A microcontroller applications”

August 2011

Doc ID 14759 Rev 2

1/20

www.st.com

Contents

AN2780

 

 

Contents

1

Real-time measurement capability . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.1

Time base common to auto-wakeup unit and beeper . . . . . . . . . . . . . . . .

5

 

1.2

Solution using the auto-wakeup unit in Active-halt mode . . . . . . . . . . . . . .

6

1.2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.2.2 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.3 Solution using the beeper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

 

 

1.3.1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 9

 

 

1.3.2

Beeper solution implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 9

 

1.4

Other solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

2

Associated software example description . . . . . . . . . . . . . . . . . . . . . .

11

 

2.1

Main variables, definitions and constants used by the software . . . . . . . .

11

 

 

2.1.1

Variables defined in main.c file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

 

 

2.1.2

Configuration definitions in rtc.h file . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

 

 

2.1.3

Configuration constants defined in STM8S and STM8A firmware

 

 

 

 

library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

2.2 Initialization procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3 Application interrupt routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.4 Application interface - real-time conversions . . . . . . . . . . . . . . . . . . . . . . 13

2.4.1 First alternative solution: real-time kept continuously . . . . . . . . . . . . . . 14 2.4.2 Second alternative solution: real-time converted on request . . . . . . . . . 14

3

Firmware environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16

4

Test environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

17

5

Accuracy of the used method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

 

5.1 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

6

Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

19

2/20

Doc ID 14759 Rev 2

AN2780

List of tables

 

 

List of tables

Table 1. List of files included in the STM8S and STM8A firmware package . . . . . . . . . . . . . . . . . . 16 Table 2. Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Doc ID 14759 Rev 2

3/20

List of figures

AN2780

 

 

List of figures

Figure 1. Time base block diagram for AWU and beeper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Figure 2. AWU block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Figure 3. AWU operation in applications using only AWU interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Figure 4. AWU operation in applications using the AWU interrupt and other

interrupt source. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 5. Beeper block diagram example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Figure 6. BEEP signal edge external interrupt - example of increment & compare

cycle performed on pair of registers of fx_time structure . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Figure 7. Main loop service checking the one-second events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Figure 8. Main loop service checking the real-time update request. . . . . . . . . . . . . . . . . . . . . . . . . . 15

4/20

Doc ID 14759 Rev 2

ST AN2780 Application note

AN2780

Real-time measurement capability

 

 

1 Real-time measurement capability

1.1Time base common to auto-wakeup unit and beeper

This application note focuses on solutions using one of the following STM8S and STM8A peripherals: auto-wakeup unit or beeper. These peripherals share a common 128 kHz clock (LS clock) which can be driven either by a low speed internal RC oscillator (LSI) or by a high speed external crystal/ceramic resonator (HSE), depending on the CLKAWUSEL option bit (see Figure below).

Figure 1. Time base block diagram for AWU and beeper

PRSC[1:0]

CKAWUSEL

 

option bit

 

option bits

 

 

 

HSE clock

 

 

(1 - 24 MHz)

 

 

Prescaler

 

MSR

 

~ 128 kHz LS clock

to timer input capture

 

 

LSI RC

 

(for measurement)

 

 

128 kHz

 

 

 

fLS

AWU, beeper

 

 

The HSE and LSI clock sources have different characteristics:

The HSE source has the advantage of performing more accurate measurements but the power consumption is higher (increased by about 0.5 mA) as the system must keep the HSE clock running while it stays in low power mode. The HSE clock can be divided by setting the PRSC option bits to get a LS clock frequency close to 128 kHz or a lower frequency.

The LSI clock system guarantees a much lower power consumption (consumption of about 5 µA) but it has the disadvantage of being less accurate and highly temperaturedependent (about ±12.5% within the whole temperature range).

The time interval measurement accuracy can be improved by measuring the LSI frequency internally. This can be performed by connecting the LSI clock to a given timer input capture through the switch controlled by the MSR bit in the AWU_CSR register and by setting properly the timer system for input capture capability. TIM3 is the dedicated timer for this LSI measurement in most STM8S and STM8A devices. TIM1 is used if TIM3 is not present. Some examples of input capture setting can be found in the STM8S and STM8A firmware library.

Note:

If the LSI clock frequency is measured while the microcontroller is driven by the HSI clock

 

(internal high speed oscillator), it is possible to achieve a greater accuracy by calibrating the

 

HSI clock before measuring the LSI frequency. The user can find further information on HSI

 

calibration in the AN2822 application note (STM8S and STM8A high speed internal

 

oscillator calibration).

Doc ID 14759 Rev 2

5/20

Real-time measurement capability

AN2780

 

 

1.2Solution using the auto-wakeup unit in Active-halt mode

1.2.1Introduction

The basic software principle is to keep track of the number of elapsed time intervals detected by the AWU. The accumulated time can easily be converted from this data by loading a structure keeping real-time information either on request or at regular intervals.

1.2.2Description

The auto-wakeup unit (AWU) provides an internal wakeup time base that is used when the MCU goes into Active-halt mode. The measurement of this wakeup time can be used to keep real-time information but some limitations must be considered, as described in the following sections.

Figure 2. AWU block diagram

 

APR[5:0]

 

~ 128 kHz LS clock

6-bit prog

 

 

 

 

counter

AWUTB[3:0]

 

 

 

AWU counters

AWU interrupt

 

 

15 time bases

 

 

AWUEN & HALT/WAIT

The AWU time intervals depend on the prescaler value defined in the APR bits of the AWU_APR register and on the AWUTB bits programmed in the AWU_TBR register. To enter Active-halt mode, the auto-wakeup unit must be enabled by setting the AWUEN bit in the AWU_CSR register before HALT instruction execution.

The AWU counters are running and counting only when the device is in Active-halt mode. They are stopped when the CPU is in Run mode and initialized again when the microcontroller enters Active-halt mode. This means that the total period is the sum of the AWU interval (AWU timeout) and the duration of the AWU interrupt routine.

Note:

As the AL bit in the CFG_GCR register cannot be used in low power mode, when the device

 

goes back from an interrupt, the user must handle by software the device return to Halt

 

mode.

6/20

Doc ID 14759 Rev 2

Loading...
+ 14 hidden pages