Continuing the STM8 Expedition

STM8S105 Discovery

Auto Wakeup Mode (AWU)

Imagine that you have to design a data acquisition system like a solar-powered weather station that will log data periodically. You can guess that there is a limitation of available energy to keep up the system and continuous monitoring is unnecessary. Thus weather data is periodically obtained and the system has no other task rather than to sleep. In situations like this we have to rely on STM8’s low power mode with auto wakeup feature.

ATMega328P Weather Station (2)

The Auto Wakeup Unit (AWU) of STM8 microcontrollers is like an alarm clock. All we have to do is to set the time for wake up and put our device to sleep. Time ticks and the CPU wakes up to do assigned tasks once the time is over.

Hardware Connection

Hardware

Code Example

 

stm8s_it.h (top part only)

#ifndef __STM8S_IT_H
#define __STM8S_IT_H

@far @interrupt void AWU_trigger_IRQHandler(void);

/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
....

 

 

stm8s_it.c (top part only)

#include "stm8s.h"
#include "stm8s_it.h"


void AWU_trigger_IRQHandler(void)
{
    AWU_GetFlagStatus();
}
....

 

stm8_interrupt_vector.c (shortened)

#include "stm8s_it.h"


typedef void @far (*interrupt_handler_t)(void);

struct interrupt_vector {
    unsigned char interrupt_instruction;
    interrupt_handler_t interrupt_handler;
};

//@far @interrupt void NonHandledInterrupt (void)
//{
    /* in order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction
    */
//  return;
//}

extern void _stext();     /* startup routine */


struct interrupt_vector const _vectab[] = {
    {0x82, (interrupt_handler_t)_stext}, /* reset */
    ....
    {0x82, AWU_trigger_IRQHandler}, /* irq1  */
    ....
    {0x82, NonHandledInterrupt}, /* irq29 */
};

 

main.c

#include "STM8S.h"


void clock_setup(void);
void GPIO_setup(void);
void AWU_setup(void);


void main(void)
{
       unsigned char s = 0x00;

       clock_setup();
       GPIO_setup();
       AWU_setup();

       while(TRUE)
       {
              for(s = 0x00; s < 0x04; s++)
              {
                     GPIO_WriteLow(GPIOD, GPIO_PIN_0);
                     delay_ms(60);
                     GPIO_WriteHigh(GPIOD, GPIO_PIN_0);
                     delay_ms(60);
              }

              halt();                          
       };
}


void clock_setup(void)
{
       CLK_DeInit();

       CLK_HSECmd(DISABLE);

       CLK_LSICmd(ENABLE);
       while(CLK_GetFlagStatus(CLK_FLAG_LSIRDY) == FALSE);
       CLK_HSICmd(ENABLE);
       while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);

       CLK_ClockSwitchCmd(ENABLE);
       CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
       CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);

       CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
       DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);

       CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, ENABLE);    
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
}


void GPIO_setup(void)
{            
       GPIO_DeInit(GPIOD);
       GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_OD_HIZ_FAST);
}


void AWU_setup(void)
{
       AWU_IdleModeEnable();    
       AWU_DeInit();
       AWU_LSICalibrationConfig(128000);
       AWU_Init(AWU_TIMEBASE_2S);
       AWU_Cmd(ENABLE);
       enableInterrupts();
}

 

Explanation

To keep things simple, again the on-board LED of Discovery board is used.

The main clock settings are not that important as AWU usually uses LSI as clock source. However, the AWU peripheral clock must be enabled.

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);

CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);

CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, ENABLE);

AWU setup is simple as shown below:

void AWU_setup(void)
{
       AWU_IdleModeEnable();    
       AWU_DeInit();
       AWU_LSICalibrationConfig(128000);
       AWU_Init(AWU_TIMEBASE_2S);
       AWU_Cmd(ENABLE);
       enableInterrupts();
}

First, we enable idle mode. Then the AWU peripheral is deinitialized. Optionally LSI can be calibrated but in most cases, this is not needed and can be avoided. Initializing the AWU requires only the wake-up time. This time value can be some of the prefixed time values only and it dictates the time for wake up after the CPU has gone sleeping.

Lastly, we have to enable the AWU unit and its respective interrupt – IRQ 1.

{0x82, AWU_trigger_IRQHandler}, /* irq1  */

In the interrupt routine, we don’t need to do anything except checking its flag’s status.

void AWU_trigger_IRQHandler(void)
{
    AWU_GetFlagStatus();
}

With these settings, we will see that the on-board LED will blink briefly and then remain turned off for about 2 seconds. Since the AWU is set for 2 seconds, the CPU wakes up after 2 seconds from halted state and the process repeats over and over again.

for(s = 0x00; s < 0x04; s++)
{
       GPIO_WriteLow(GPIOD, GPIO_PIN_0);
       delay_ms(60);
       GPIO_WriteHigh(GPIOD, GPIO_PIN_0);
       delay_ms(60);
}

halt();

 

Demo

AWU (1) AWU (2)

Related Posts

23 comments

  • Ϝirst ᧐f aⅼl I would ⅼike tօ say awesome blog!
    Ι had а quick question tһɑt I’ɗ ⅼike to aѕk
    іf yoᥙ don’t mind. Ӏ was curious to knoᴡ how you center yourself ɑnd clear yοur thoսghts prior to writing.
    Ӏ’ѵе had difficulty clearing mү mind in getting mу ideas out tһere.
    I tгuly do tɑke pleasure in writing howeᴠer
    іt just ѕeems lіke tһe first 10 to 15 minutes tend to be lost simply ϳust trʏing to figure օut һow to begin. Any ideas or hints?
    Kudos!

    • Well firstly, to me the concepts of all microcontrollers in world is same and so provided that you have some degree of knowledge of internal peripherals, you can unlock anyone of them by systematically trying out each peripheral on your own…. Secondly, how to start is up to you…. When I compose a blog and plan what I would be focusing on, I take things as such that my audience is in front of me and would likely to ask me the very basics…. I try to write things in simplest possible language and with minimum word count while highlighting what is most important…. A blog on any microcontroller should focus on every aspect and not just a few topics…. Lastly, planning and persistently going by the plan to achieve a vision will surely bring out a good result…. Just don’t lose focus and don’t let yourself be pressurized by too many unknown variables….

  • It’s not my first time to ѵisіt thіѕ web page, i am visiting this webѕite
    dailly and obtain pleasɑnt information from herе
    all the time.

  • I visited multiple blogs except tһe audio quality f᧐r audio songs existing аt this web paɡe iѕ tгuly superb.

  • Dear Shawon,
    Thank you for your useful website and articles.
    I have taken a look at ADC1_Init definition and I found it uses ADC1_ConversionConfig to set channels and conversion mode. So it seems using ADC1_Init once with all needed channels is enough. Am I right?
    Thank you very much.

  • Hello,
    I am learning stm8s, but i have a project in my mind, i am making Pong game with two encoders and PCD8544 LCD. Your tutorials are great and it’s very big source of knowledge for me, and i have a question about this article:
    Is it possible to use two encoder this way? Or can it be done in the other way? I have stm8s103f3p6, and i know it has 4 interrupt pins, so I thought about using these for two encoders, but then i saw your article about QEI

  • Hello Friend!!
    Your stuff is great !!!
    Would you have an example 433 mhz rf signal capture with flash code writing?

  • Ola amigo !!!
    Seu material é excelente!
    Você teria algum exemplo de captura de sinal rf 433 mhz com gravação na flash?

  • Hello friends, pleasant paragraph ɑnd good urging commented ɑt tһiѕ ρlace,
    I ɑm truⅼy enjoying ƅy tһese.

  • Hi,
    Thats great work
    Do you have steps for to work ST7735 TFT IN 8 BIT 8080 mode

  • I don’t even understand how I ended up right here, however
    I believed this publish used to be good. I don’t know who you are however definitely
    you’re going to a well-known blogger should you aren’t already.
    Cheers!

  • Sankalp Rai Gambhir

    Thanks Shawon, your blogs are indeed very helpful. Keep Growing.

  • why are you sending the received data back through TX pin?

  • Hi SHAWON SHAHRYIAR

    I am wondering how to get a Max31855 to talk to a STM8s via SPI.

    JP

    • What’s to wonder about it? It is a simple SPI communication and SPI for STM8 is no different from the SPI of other MCUs…. The following lines are taken from the device’s datasheet and the write up there states how to communicate with it:

      “Drive CS low to output the first bit on the SO pin. A complete serial-interface read of the cold-junction compensated thermocouple temperature requires 14 clock cycles. Thirty-two clock cycles are required to read both the thermocouple and reference junction temperatures (Table 2 and Table 3.) The first bit, D31, is the thermocouple temperature sign bit, and is presented to the SO pin within tDV of the falling edge of CS. Bits D[30:18] contain the converted temperature in the order of MSB to LSB, and are presented to the SO pin within tD0 of the falling edge of SCK. Bit D16 is normally low and goes high when the thermocouple input is open or shorted to GND or VCC. The reference junction temperature data begins with D15. CS can be taken high at any point while clocking out con-version data. If T+ and T- are unconnected, the thermocouple temperature sign bit (D31) is 0, and the remainder of the thermocouple temperature value (D[30:18]) is 1.”

Leave a Reply

Your email address will not be published. Required fields are marked *