mavishak / cnss-embedded

1 stars 0 forks source link

Adding watchdog #31

Open NaomiCreate opened 3 years ago

NaomiCreate commented 3 years ago

Our instructor told us that adding a watchdog to embedded projects is a recommended thing, which increases the quality of the embedded product. The reason is that if our software gets stuck, after a certain amount of time, it will go through a reset. We decided to add the Independent watchdog (IWDG)- chapter 19 in the reference manual.

Links we received help from: STM32F1 notes (six) independent watchdog IWDG:

NaomiCreate commented 3 years ago

We tested the implementation of the watchdog as follows:

We initialized the prescaler value to be 4 and the reload value to be 625, so that the amount of time that passes before the reset will be one second.

watchdog.c:

void WATCHDOG_init(void){
    IWDG->KR =  0x5555; //Writing the key value 5555h to enable access to the IWDG_PR and IWDG_RLR registers
    //the dogs time is calculated as: Tout = ((4 * 2 ^ prer) * rlr) / 40
    IWDG->PR =0x4;      //the IWDG Prescaler value
    IWDG->RLR =0x0271;  //the IWDG Reload value
    IWDG->KR = 0xCCCC;//reference manual 19.4.1: Writing the key value CCCCh starts the watchdog
}

void WATCHDOG_kick(){
    IWDG->KR |= 0xAAAA;//reference manual 19.4.1: These bits must be written with the key value AAAAh,otherwise the watchdog generates a reset when the counter reaches 0
}

main.c:

int main(void)
{
    USART2_init(); // for debugging
    USART1_init(); // for ESP8266

    USART2_write((uint8_t*)("\r\n_______________\r\n"));//For test
    WATCHDOG_init();
    while(1)
    {
        USART2_write((uint8_t*)("\r\n*******\r\n"));//For test
        while(1);
        WATCHDOG_kick();
        USART2_write((uint8_t*)("\r\n#######\r\n"));//For test
    }
}

Expected results: We kick the watchdog only after the infinite while loop, that's why we'll never kick the watchdog. So our hypothesis is that the program will go through a reset every second. Meaning that every second will be printed in tera term :

_______________

*******

Results: After running the program we got the result we assumed would happen.