STM8 MCU reset source judgment

Publisher:浅唱梦幻Latest update time:2019-11-18 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Recently, when debugging the system, I found that the microcontroller was always resetting, so I thought of using a program to determine where the reset signal of a microcontroller came from. I found that there are 9 reset sources for STM8 microcontrollers.


●External reset generated by NRST pin

● Power-On Reset (POR)

● Brown-out Reset (BOR)

● Independent watchdog reset

● Window watchdog reset

● Software reset

● SWIM reset

● Illegal opcode reset

● EMS reset: A reset that occurs when some critical registers are corrupted or incorrectly loaded


However, only 5 of these 9 reset sources can be seen through the reset register


Reset Status Register (RST_SR)


Bits 7:5 are reserved and must remain zero.

Bit 4 EMCF: EMC reset flag

Bit 3 SWIMF: SWIM reset flag

Bit 2 ILLOPF: Illegal opcode reset flag

Bit 1 IWDGF: Independent watchdog reset flag

Bit 0 WWDGF: Windowed watchdog reset flag

 


Since the MCU does not use a watchdog, these two watchdog resets are not used. The SWIM reset is used when burning programs or emulating. Then there is only one EMC reset and illegal opcode reset left to judge. But what I want to see more is whether the MCU is reset by power failure caused by unstable power supply or NRST reset caused by interference with the reset pin of the MCU. However, there is no flag bit in the register that can judge these two reset sources. So can I find a way to judge these two reset sources by myself?


  First, let's analyze the difference between power-off reset and NRST reset. Power-off reset means that the microcontroller is completely powered off, and everything in the memory will be lost after the reset. The memory data is not lost during NRST reset, and the contents in the memory are still the data stored before the reset. So based on this feature, can we store a flag at a certain address in the memory? Read this flag after reset. If this flag is the same as the stored one, it means that the microcontroller is not powered off, and it is a NRST reset. If the read value is not the stored value, it means that the microcontroller is powered off, and the contents stored in the memory are lost, which is a power-off reset. In this way, we can distinguish whether it is a power-off reset or a NRST reset.


I wrote a program to test it, and the test results were in line with expectations.


The test code is as follows:


#include "iostm8s103F3.h"

#include "main.h"

#include "uart.h"

#include "stdio.h"

 

void SysClkInit( void )

{

    CLK_SWR = 0xe1; //HSI is the main clock source 16MHz CPU clock frequency

    CLK_CKDIVR = 0x00; //CPU clock divided by 0, system clock divided by 0

}

 

void main( void )

{

    __asm( "sim" ); //Disable interrupts

    SysClkInit();

    Uart1_Heat( 9600 );

    __asm( "rim" ); // Enable interrupt

    

    //Reset source judgment

    if( ( * ( u16* )( 0x000102 ) ) == 0x55 ) //Judge whether the value stored in the address 0x000100 is 0x55

    {

        printf( " key reset!rn" ); //The value is equal to 0x55, which means it is written by the program and belongs to key reset

    }

    else //If the value of this address is not equal to 0x55, it means that the microcontroller has just been powered on

    {

      printf( " power reset!rn" ); //It is a power-on reset and then writes the specified value to the specified address. If the microcontroller is not powered off, this value will remain unchanged

        * ( u16* )( 0x000102 ) = 0x55;

    }

    if( RST_SR_WWDGF )

    {

        printf( "wwdg reset!rn" );

    }

    if( RST_SR_IWDGF )

    {

        printf( "iwdg reset!rn" );

    }

    if( RST_SR_ILLOPF )

    {

        printf( "illop reset!rn" );

    }

    if( RST_SR_SWIMF )

    {

        printf( "swim reset!rn" );

    }

    if( RST_SR_EMCF )

    {

        printf( "emcf reset!rn" );

    }

    //WWDG_CR = 0x80; //Start independent watchdog

    // IWDG_KR = 0xCC; // Start window watchdog

    while( 1 )

    {

    }

}


After each power-on, first determine whether the value of the address 0x000102 is 0x55. If not, it means that the microcontroller has just been powered on and the value in the memory is random. So change the value in the address 0x000102 to 0x55. If the value of this address is 0x55 after the next microcontroller reset, it means that the microcontroller is not powered off. It is a reset caused by the reset pin. The address 0x000102 must be guaranteed not to be used by the program, otherwise the value will be rewritten during the program execution, and the value read after the next microcontroller reset will not be 0x55.


For the other reset sources, just read the registers directly.

Keywords:STM8 Reference address:STM8 MCU reset source judgment

Previous article:STM8S——Clock control(CLK)
Next article:STM8S——Analog/digital converter (ADC)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号