![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Watchdog Code --> IWDG Register Enable Code
main.cpp@0:57d5f9a58ad1, 2020-03-31 (annotated)
- Committer:
- amirchaudhary
- Date:
- Tue Mar 31 10:17:51 2020 +0000
- Revision:
- 0:57d5f9a58ad1
Watchdog Test code --> IWDG Register Enable
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amirchaudhary | 0:57d5f9a58ad1 | 1 | #include "mbed.h" |
amirchaudhary | 0:57d5f9a58ad1 | 2 | #include "stm32l0xx_hal_iwdg.h" |
amirchaudhary | 0:57d5f9a58ad1 | 3 | |
amirchaudhary | 0:57d5f9a58ad1 | 4 | /*------------------------------------------------------------------------------ |
amirchaudhary | 0:57d5f9a58ad1 | 5 | Before to use this example, ensure that you an hyperterminal installed on your |
amirchaudhary | 0:57d5f9a58ad1 | 6 | computer. More info here: https://developer.mbed.org/handbook/Terminals |
amirchaudhary | 0:57d5f9a58ad1 | 7 | |
amirchaudhary | 0:57d5f9a58ad1 | 8 | The default serial comm port uses the SERIAL_TX and SERIAL_RX pins (see their |
amirchaudhary | 0:57d5f9a58ad1 | 9 | definition in the PinNames.h file). |
amirchaudhary | 0:57d5f9a58ad1 | 10 | |
amirchaudhary | 0:57d5f9a58ad1 | 11 | The default serial configuration in this case is 9600 bauds, 8-bit data, no parity |
amirchaudhary | 0:57d5f9a58ad1 | 12 | |
amirchaudhary | 0:57d5f9a58ad1 | 13 | If you want to change the baudrate for example, you have to redeclare the |
amirchaudhary | 0:57d5f9a58ad1 | 14 | serial object in your code: |
amirchaudhary | 0:57d5f9a58ad1 | 15 | |
amirchaudhary | 0:57d5f9a58ad1 | 16 | Serial pc(SERIAL_TX, SERIAL_RX); |
amirchaudhary | 0:57d5f9a58ad1 | 17 | |
amirchaudhary | 0:57d5f9a58ad1 | 18 | Then, you can modify the baudrate and print like this: |
amirchaudhary | 0:57d5f9a58ad1 | 19 | |
amirchaudhary | 0:57d5f9a58ad1 | 20 | pc.baud(115200); |
amirchaudhary | 0:57d5f9a58ad1 | 21 | pc.printf("Hello World !\n"); |
amirchaudhary | 0:57d5f9a58ad1 | 22 | ------------------------------------------------------------------------------*/ |
amirchaudhary | 0:57d5f9a58ad1 | 23 | DigitalOut led(D7); |
amirchaudhary | 0:57d5f9a58ad1 | 24 | DigitalOut myBlueLed(PA_11); |
amirchaudhary | 0:57d5f9a58ad1 | 25 | DigitalOut relayPin(PB_10); |
amirchaudhary | 0:57d5f9a58ad1 | 26 | |
amirchaudhary | 0:57d5f9a58ad1 | 27 | int MY_SetSysClock_PLL_HSE(void) |
amirchaudhary | 0:57d5f9a58ad1 | 28 | { |
amirchaudhary | 0:57d5f9a58ad1 | 29 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
amirchaudhary | 0:57d5f9a58ad1 | 30 | RCC_OscInitTypeDef RCC_OscInitStruct; |
amirchaudhary | 0:57d5f9a58ad1 | 31 | |
amirchaudhary | 0:57d5f9a58ad1 | 32 | /* Enable HSE and activate PLL with HSE as source */ |
amirchaudhary | 0:57d5f9a58ad1 | 33 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
amirchaudhary | 0:57d5f9a58ad1 | 34 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */ |
amirchaudhary | 0:57d5f9a58ad1 | 35 | |
amirchaudhary | 0:57d5f9a58ad1 | 36 | // PLLCLK = (8 MHz * 8)/2 = 32 MHz |
amirchaudhary | 0:57d5f9a58ad1 | 37 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
amirchaudhary | 0:57d5f9a58ad1 | 38 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
amirchaudhary | 0:57d5f9a58ad1 | 39 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_8; |
amirchaudhary | 0:57d5f9a58ad1 | 40 | RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2; |
amirchaudhary | 0:57d5f9a58ad1 | 41 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
amirchaudhary | 0:57d5f9a58ad1 | 42 | return (-1); // FAIL |
amirchaudhary | 0:57d5f9a58ad1 | 43 | } |
amirchaudhary | 0:57d5f9a58ad1 | 44 | |
amirchaudhary | 0:57d5f9a58ad1 | 45 | /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ |
amirchaudhary | 0:57d5f9a58ad1 | 46 | RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
amirchaudhary | 0:57d5f9a58ad1 | 47 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 32 MHz |
amirchaudhary | 0:57d5f9a58ad1 | 48 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 32 MHz |
amirchaudhary | 0:57d5f9a58ad1 | 49 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
amirchaudhary | 0:57d5f9a58ad1 | 50 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
amirchaudhary | 0:57d5f9a58ad1 | 51 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
amirchaudhary | 0:57d5f9a58ad1 | 52 | return (-2); // FAIL |
amirchaudhary | 0:57d5f9a58ad1 | 53 | } |
amirchaudhary | 0:57d5f9a58ad1 | 54 | |
amirchaudhary | 0:57d5f9a58ad1 | 55 | /* Enable HSE and activate PLL with HSE as source */ |
amirchaudhary | 0:57d5f9a58ad1 | 56 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI; |
amirchaudhary | 0:57d5f9a58ad1 | 57 | RCC_OscInitStruct.HSIState = RCC_HSI_OFF; |
amirchaudhary | 0:57d5f9a58ad1 | 58 | RCC_OscInitStruct.MSIState = RCC_MSI_OFF; |
amirchaudhary | 0:57d5f9a58ad1 | 59 | RCC_OscInitStruct.HSI48State = RCC_HSI48_OFF; |
amirchaudhary | 0:57d5f9a58ad1 | 60 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
amirchaudhary | 0:57d5f9a58ad1 | 61 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
amirchaudhary | 0:57d5f9a58ad1 | 62 | return (-3); // FAIL |
amirchaudhary | 0:57d5f9a58ad1 | 63 | } |
amirchaudhary | 0:57d5f9a58ad1 | 64 | |
amirchaudhary | 0:57d5f9a58ad1 | 65 | return 0; // OK |
amirchaudhary | 0:57d5f9a58ad1 | 66 | } |
amirchaudhary | 0:57d5f9a58ad1 | 67 | |
amirchaudhary | 0:57d5f9a58ad1 | 68 | void my_patch(void) |
amirchaudhary | 0:57d5f9a58ad1 | 69 | { |
amirchaudhary | 0:57d5f9a58ad1 | 70 | int retVal; |
amirchaudhary | 0:57d5f9a58ad1 | 71 | |
amirchaudhary | 0:57d5f9a58ad1 | 72 | // Put device into default clock, i.e using MSI = 2MHz |
amirchaudhary | 0:57d5f9a58ad1 | 73 | HAL_RCC_DeInit(); |
amirchaudhary | 0:57d5f9a58ad1 | 74 | |
amirchaudhary | 0:57d5f9a58ad1 | 75 | // Enable HSE clock |
amirchaudhary | 0:57d5f9a58ad1 | 76 | retVal = MY_SetSysClock_PLL_HSE(); |
amirchaudhary | 0:57d5f9a58ad1 | 77 | if(retVal< 0) |
amirchaudhary | 0:57d5f9a58ad1 | 78 | { |
amirchaudhary | 0:57d5f9a58ad1 | 79 | //pc.printf("Failed to start HSE, ERR= %d\r\n", retVal); |
amirchaudhary | 0:57d5f9a58ad1 | 80 | |
amirchaudhary | 0:57d5f9a58ad1 | 81 | // indicate error |
amirchaudhary | 0:57d5f9a58ad1 | 82 | while(1) |
amirchaudhary | 0:57d5f9a58ad1 | 83 | { |
amirchaudhary | 0:57d5f9a58ad1 | 84 | led = !led; |
amirchaudhary | 0:57d5f9a58ad1 | 85 | wait(0.2); |
amirchaudhary | 0:57d5f9a58ad1 | 86 | } |
amirchaudhary | 0:57d5f9a58ad1 | 87 | } |
amirchaudhary | 0:57d5f9a58ad1 | 88 | } |
amirchaudhary | 0:57d5f9a58ad1 | 89 | |
amirchaudhary | 0:57d5f9a58ad1 | 90 | int main() |
amirchaudhary | 0:57d5f9a58ad1 | 91 | { |
amirchaudhary | 0:57d5f9a58ad1 | 92 | Serial pc(SERIAL_TX, SERIAL_RX); |
amirchaudhary | 0:57d5f9a58ad1 | 93 | pc.baud(115200); |
amirchaudhary | 0:57d5f9a58ad1 | 94 | |
amirchaudhary | 0:57d5f9a58ad1 | 95 | IWDG_HandleTypeDef wd; |
amirchaudhary | 0:57d5f9a58ad1 | 96 | wd.Instance = IWDG; |
amirchaudhary | 0:57d5f9a58ad1 | 97 | wd.Init.Prescaler = IWDG_PRESCALER_256; //About 32s |
amirchaudhary | 0:57d5f9a58ad1 | 98 | wd.Init.Reload = 0x0FFF; |
amirchaudhary | 0:57d5f9a58ad1 | 99 | HAL_IWDG_Init(&wd); |
amirchaudhary | 0:57d5f9a58ad1 | 100 | pc.printf("initialized watchdog CSR= %0X\r\n", RCC->CSR); |
amirchaudhary | 0:57d5f9a58ad1 | 101 | |
amirchaudhary | 0:57d5f9a58ad1 | 102 | |
amirchaudhary | 0:57d5f9a58ad1 | 103 | pc.printf("mbed-os-rev: %d.%d.%d lib-rev: %d\r\n", \ |
amirchaudhary | 0:57d5f9a58ad1 | 104 | MBED_MAJOR_VERSION, MBED_MINOR_VERSION,MBED_PATCH_VERSION,MBED_LIBRARY_VERSION); |
amirchaudhary | 0:57d5f9a58ad1 | 105 | pc.printf("BUILD= %s, SysClock= %d, RCC= %0X CSR= %0X\r\n", __TIME__, SystemCoreClock, RCC->CR, RCC->CSR); |
amirchaudhary | 0:57d5f9a58ad1 | 106 | my_patch(); |
amirchaudhary | 0:57d5f9a58ad1 | 107 | pc.printf("NEW BUILD= %s, SysClock= %d, RCC= %0X CSR= %0X\r\n", __TIME__, SystemCoreClock, RCC->CR, RCC->CSR); |
amirchaudhary | 0:57d5f9a58ad1 | 108 | wait(3); |
amirchaudhary | 0:57d5f9a58ad1 | 109 | |
amirchaudhary | 0:57d5f9a58ad1 | 110 | int i = 1; |
amirchaudhary | 0:57d5f9a58ad1 | 111 | |
amirchaudhary | 0:57d5f9a58ad1 | 112 | |
amirchaudhary | 0:57d5f9a58ad1 | 113 | myBlueLed = 1; // LED is ON |
amirchaudhary | 0:57d5f9a58ad1 | 114 | wait(0.5); // 500 ms |
amirchaudhary | 0:57d5f9a58ad1 | 115 | myBlueLed = 0; // LED is ON |
amirchaudhary | 0:57d5f9a58ad1 | 116 | wait(0.5); // 500 ms |
amirchaudhary | 0:57d5f9a58ad1 | 117 | relayPin = 1; // LED is ON |
amirchaudhary | 0:57d5f9a58ad1 | 118 | wait(0.5); // 500 ms |
amirchaudhary | 0:57d5f9a58ad1 | 119 | relayPin = 0; // LED is ON |
amirchaudhary | 0:57d5f9a58ad1 | 120 | wait(0.5); // 500 ms |
amirchaudhary | 0:57d5f9a58ad1 | 121 | |
amirchaudhary | 0:57d5f9a58ad1 | 122 | |
amirchaudhary | 0:57d5f9a58ad1 | 123 | pc.printf("Hello World 2 !\r\n"); |
amirchaudhary | 0:57d5f9a58ad1 | 124 | |
amirchaudhary | 0:57d5f9a58ad1 | 125 | while(1) { |
amirchaudhary | 0:57d5f9a58ad1 | 126 | wait(1); // 1 second |
amirchaudhary | 0:57d5f9a58ad1 | 127 | led = !led; |
amirchaudhary | 0:57d5f9a58ad1 | 128 | relayPin = !relayPin; // LED is ON |
amirchaudhary | 0:57d5f9a58ad1 | 129 | |
amirchaudhary | 0:57d5f9a58ad1 | 130 | |
amirchaudhary | 0:57d5f9a58ad1 | 131 | |
amirchaudhary | 0:57d5f9a58ad1 | 132 | if (i == 15) { |
amirchaudhary | 0:57d5f9a58ad1 | 133 | |
amirchaudhary | 0:57d5f9a58ad1 | 134 | //https://github.com/ARMmbed/mbed-os/blob/mbed_lib_rev164/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_iwdg.c |
amirchaudhary | 0:57d5f9a58ad1 | 135 | /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing 0x5555 in KR |
amirchaudhary | 0:57d5f9a58ad1 | 136 | * This is not done in HAL_IWDG_Refresh() (I THINK SO :->) |
amirchaudhary | 0:57d5f9a58ad1 | 137 | */ |
amirchaudhary | 0:57d5f9a58ad1 | 138 | IWDG_ENABLE_WRITE_ACCESS(&wd); |
amirchaudhary | 0:57d5f9a58ad1 | 139 | |
amirchaudhary | 0:57d5f9a58ad1 | 140 | HAL_IWDG_Refresh(&wd); |
amirchaudhary | 0:57d5f9a58ad1 | 141 | pc.printf("Fed the dog\r\n"); |
amirchaudhary | 0:57d5f9a58ad1 | 142 | } |
amirchaudhary | 0:57d5f9a58ad1 | 143 | pc.printf("This program runs since %d seconds.\r\n", i++); |
amirchaudhary | 0:57d5f9a58ad1 | 144 | } |
amirchaudhary | 0:57d5f9a58ad1 | 145 | |
amirchaudhary | 0:57d5f9a58ad1 | 146 | pc.printf("should never reach here\n"); |
amirchaudhary | 0:57d5f9a58ad1 | 147 | } |