HSE modfied

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
amirchaudhary
Date:
Tue Apr 07 18:18:11 2020 +0000
Parent:
0:57d5f9a58ad1
Commit message:
Watchdog code updated

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 57d5f9a58ad1 -r 8128e59de0e6 main.cpp
--- a/main.cpp	Tue Mar 31 10:17:51 2020 +0000
+++ b/main.cpp	Tue Apr 07 18:18:11 2020 +0000
@@ -24,6 +24,9 @@
 DigitalOut myBlueLed(PA_11);
 DigitalOut relayPin(PB_10);
 
+    Serial pc(SERIAL_TX, SERIAL_RX);
+
+
 int MY_SetSysClock_PLL_HSE(void)
 {
     RCC_ClkInitTypeDef RCC_ClkInitStruct;
@@ -39,9 +42,12 @@
     RCC_OscInitStruct.PLL.PLLMUL          = RCC_PLLMUL_8;
     RCC_OscInitStruct.PLL.PLLDIV          = RCC_PLLDIV_2;
     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
+        relayPin = 1;
         return (-1); // FAIL
     }
 
+    led = 0;
+
     /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
     RCC_ClkInitStruct.ClockType      = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
     RCC_ClkInitStruct.SYSCLKSource   = RCC_SYSCLKSOURCE_PLLCLK; // 32 MHz
@@ -52,6 +58,8 @@
         return (-2); // FAIL
     }
 
+    myBlueLed = 0;
+
     /* Enable HSE and activate PLL with HSE as source */
     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;
     RCC_OscInitStruct.HSIState       = RCC_HSI_OFF;
@@ -61,7 +69,9 @@
     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
         return (-3); // FAIL
     }
-        
+    
+    relayPin = 0;
+    
     return 0; // OK
 }
 
@@ -69,44 +79,97 @@
 {
     int retVal;
     
+    relayPin = 1;
+    led = 1;
+    myBlueLed = 1;
+    
     // Put device into default clock, i.e using MSI = 2MHz
     HAL_RCC_DeInit();
     
     // Enable HSE clock
     retVal = MY_SetSysClock_PLL_HSE();
-    if(retVal< 0)
+    if(retVal == -1)
+    {   
+        while(1)
+        {
+            led = !led;            
+            wait(1);
+        }
+    }
+    else if(retVal == -2)
     {
-        //pc.printf("Failed to start HSE, ERR= %d\r\n", retVal);
-        
-        // indicate error
+        while(1)
+        {
+            myBlueLed = !myBlueLed;            
+            wait(1);
+        }        
+    }   
+    else if(retVal == -3)
+    {
         while(1)
         {
-            led = !led;
-            wait(0.2);
+            relayPin = !relayPin;
+            led = !led;            
+            myBlueLed = !myBlueLed;            
+            wait(1);    
         }
-    }    
+    } 
+}
+
+void reset_cause(unsigned int csr){
+
+    if(csr & (1U<<24))
+        pc.printf("!!! FIREWALL RESET\r\n");
+    if(csr & (1U<<25))
+        pc.printf("!!! OBL RESET\r\n");
+    if(csr & (1U<<26))
+        pc.printf("!!! PIN RESET\r\n");
+    if(csr & (1U<<27))
+        pc.printf("!!! POR RESET\r\n");
+    if(csr & (1U<<28))
+        pc.printf("!!! SOFTWARE RESET\r\n");
+    if(csr & (1U<<29))
+        pc.printf("!!! IWDG RESET\r\n");
+    if(csr & (1U<<30))
+        pc.printf("!!! WWDG RESET\r\n");
+    if(csr & (1U<<31))
+        pc.printf("!!! LPWR RESET\r\n");
+
+    RCC->CSR |= (1U<<23); // clear reset cause
 }
 
 int main()
 {
-    Serial pc(SERIAL_TX, SERIAL_RX);
+//    Serial pc(SERIAL_TX, SERIAL_RX);
+//    pc.baud(115200);
+
+//    pc.printf("mbed-os-rev: %d.%d.%d   lib-rev: %d\r\n", \
+//            MBED_MAJOR_VERSION, MBED_MINOR_VERSION,MBED_PATCH_VERSION,MBED_LIBRARY_VERSION);
+//    pc.printf("BUILD= %s %s\r\n\r\n", __TIME__, __DATE__);
+    
+//    pc.printf("OLD SysClock= %d, CR= %08X CSR= %08X\r\n\r\n", SystemCoreClock, RCC->CR, RCC->CSR);
+
+    my_patch();
     pc.baud(115200);
 
+    reset_cause(RCC->CSR);
+
+    pc.printf("NEW SysClock= %d, CR= %08X CSR= %08X\r\n\r\n", SystemCoreClock, RCC->CR, RCC->CSR);
+    
+    
+    wait(3);
+
     IWDG_HandleTypeDef wd;
     wd.Instance = IWDG;
     wd.Init.Prescaler = IWDG_PRESCALER_256; //About 32s
     wd.Init.Reload = 0x0FFF;
+    wd.Init.Window = 0x0FFF;
     HAL_IWDG_Init(&wd);
-    pc.printf("initialized watchdog CSR= %0X\r\n", RCC->CSR);
-
+    pc.printf("initialized watchdog CSR= %0X IWDG->SR= %0X\r\n", RCC->CSR, IWDG->SR);
 
-    pc.printf("mbed-os-rev: %d.%d.%d   lib-rev: %d\r\n", \
-            MBED_MAJOR_VERSION, MBED_MINOR_VERSION,MBED_PATCH_VERSION,MBED_LIBRARY_VERSION);
-    pc.printf("BUILD= %s, SysClock= %d, RCC= %0X CSR= %0X\r\n", __TIME__, SystemCoreClock, RCC->CR, RCC->CSR);
-    my_patch();
-     pc.printf("NEW BUILD= %s, SysClock= %d, RCC= %0X CSR= %0X\r\n", __TIME__, SystemCoreClock, RCC->CR, RCC->CSR);
-    wait(3);
-    
+    IWDG->KR = 0x00005555;
+    pc.printf(">> IWDG PR= %0X RLR= %0X SR= %0X\r\n", IWDG->PR, IWDG->RLR, IWDG->SR);    
+
     int i = 1;
 
 
@@ -120,14 +183,12 @@
         wait(0.5); // 500 ms
 
                 
-    pc.printf("Hello World 2 !\r\n");
+//    pc.printf("Hello World 2 !\r\n");
 
     while(1) {
         wait(1); // 1 second
         led = !led;
-        relayPin = !relayPin; // LED is ON
-      
-        
+        relayPin = !relayPin; // LED is ON   
         
         if (i == 15) { 
             
@@ -135,8 +196,9 @@
             /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing 0x5555 in KR 
             *  This is not done in HAL_IWDG_Refresh() (I THINK SO :->)
             */
-            IWDG_ENABLE_WRITE_ACCESS(&wd); 
-            
+//            IWDG_ENABLE_WRITE_ACCESS(&wd); 
+//            IWDG->KR = 0x0000AAAA;
+        
             HAL_IWDG_Refresh(&wd);
             pc.printf("Fed the dog\r\n");
         }