mbed-os github

Dependencies:   ADS1015 Faulhaber HTU21D_mod MS5837_potless Sensor_Head_RevB_3 USBDevice_dfu Utilsdfu beep

Fork of ARNSRS_testDFU by POTLESS

Revision:
53:84571a5e2cda
Parent:
52:4e36a88711d6
Child:
54:2062e5786a66
--- a/main.cpp	Wed May 30 16:31:43 2018 +0000
+++ b/main.cpp	Thu May 31 07:53:44 2018 +0000
@@ -82,6 +82,9 @@
 //Flag pour interrompre les demandes O2 en cours ed calibration...
 bool FLAG_O2 = true;
 
+//Flag pour passage en DFU...
+bool FLAG_DFU = false;
+
 //Variables de stockage des infos capteurs
 int co2 = 0;
 float pression = 0;
@@ -158,40 +161,44 @@
 
 void FLASH_set_boot_bank(int bank)
 {
-  FLASH_OBProgramInitTypeDef    OBInit;
-  /* Set BFB2 bit to enable boot from Flash Bank2 */
-  /* Allow Access to the Flash control registers and user Flash. */
-  HAL_FLASH_Unlock();
-  /* Clear OPTVERR bit set on virgin samples. */
-  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
-  /* Allow Access to the option bytes sector. */
-  HAL_FLASH_OB_Unlock();
-  /* Get the Dual boot configuration status. */
-  HAL_FLASHEx_OBGetConfig(&OBInit);
+    HAL_DeInit();
+    FLASH_OBProgramInitTypeDef    OBInit;
+    /* Set BFB2 bit to enable boot from Flash Bank2 */
+    /* Allow Access to the Flash control registers and user Flash. */
+    HAL_FLASH_Unlock();
+    /* Clear OPTVERR bit set on virgin samples. */
+    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
+    /* Allow Access to the option bytes sector. */
+    HAL_FLASH_OB_Unlock();
+    /* Get the Dual boot configuration status. */
+    HAL_FLASHEx_OBGetConfig(&OBInit);
 
-  /* Enable/Disable dual boot feature */
-  OBInit.OptionType = OPTIONBYTE_USER;
-  OBInit.USERType   = OB_USER_BFB2;
-  switch (bank)
-  {
-    case 1:
-      OBInit.USERConfig = OB_BFB2_DISABLE;
-      break;
-    case 2:
-      OBInit.USERConfig = OB_BFB2_ENABLE;
-      break;
+    /* Enable/Disable dual boot feature */
+    OBInit.OptionType = OPTIONBYTE_USER;
+    OBInit.USERType   = OB_USER_BFB2;
+    switch (bank) {
+        case 1:
+            OBInit.USERConfig = OB_BFB2_DISABLE;
+            break;
+        case 2:
+            OBInit.USERConfig = OB_BFB2_ENABLE;
+            break;
 
-  }
+    }
 
-  if(HAL_FLASHEx_OBProgram (&OBInit) != HAL_OK)
-  { /* Failed setting the option bytes configuration.
-     * Call 'HAL_FLASH_GetError()' for details. */
-    serialMonit.printf("Impossible de changer BFB2.\n");
-  }
+    if(HAL_FLASHEx_OBProgram (&OBInit) != HAL_OK) {
+        /* Failed setting the option bytes configuration.
+         * Call 'HAL_FLASH_GetError()' for details. */
+        serialMonit.printf("Impossible de changer BFB2.\n");
+    }
+    
+    HAL_FLASH_OB_Launch();  
+    HAL_FLASH_OB_Lock(); 
+    serialMonit.printf("Fin BFB2.\n");
 }
 
 void Affichage()
-{   
+{
     //serialMonit.printf("\r\n");
     serialMonit.printf("  CO2             = %d ppm\r\n"  , co2);
     serialMonit.printf("  PPO2            = %d mb\r\n", ppO2);
@@ -231,13 +238,13 @@
     Volets_Speed = 0.1;
     Volet_DeadBand = 10;
 
-/*
-    while(1) {
-        wait_ms(100);
-        if (Servo_Poumon.Pos_OK() == true) break;
-        if (Servo_Fuite.Pos_OK() == true) break;
-    }
-*/
+    /*
+        while(1) {
+            wait_ms(100);
+            if (Servo_Poumon.Pos_OK() == true) break;
+            if (Servo_Fuite.Pos_OK() == true) break;
+        }
+    */
 
     OUTPUT("  Appareil en mode SECU\r\n");
 
@@ -250,12 +257,12 @@
     UTILS::Store_A_Val((float)Pos_P, "Servo_Poumon.sys");
     DEBUG("  position volet poumon sauvegardée = %d pulse(s)\r\n", Pos_P);
     OUTPUT("  Volet poumon en sécu\r\n");
-    
+
     int Pos_F = Servo_Fuite.getPulses();
     UTILS::Store_A_Val((float)Pos_F, "Servo_Fuite.sys");
     DEBUG("  position volet fuite sauvegardée = %d pulse(s)\r\n", Pos_F);
     OUTPUT("  Volet fuite en sécu\r\n");
-    
+
     Servo_Poumon.Sleep();
     Servo_Fuite.Sleep();
 }
@@ -268,11 +275,11 @@
     wait(1);
 
     Mode_SECU();
-    
+
     wait(1);
     //ejection de la flash pour pas crasher le system de fichiers
     UTILS::UnMount_Flash();
-    
+
     OUTPUT("  Ejection de la Flash\r\n");
     wait(1);
     buzzer.beep(1000,0.5);
@@ -295,7 +302,7 @@
 
     sleep_manager_sleep_auto(); // ne marche pas, je ne sais pas pk
     */
-    
+
     /*
     HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN2); // pour PC_13
     // Clear wake up Flag
@@ -303,7 +310,7 @@
     // Enable wakeup pin WKUP2
     HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN2_LOW); // low parce qu'on veut que ça redémarre avec le user button TODO à passer sur High pour que ça redémarrage avec l'arrivée du hus sur l'USB
     */
-    
+
     HAL_PWREx_EnablePullUpPullDownConfig() ;
     HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_A, PWR_GPIO_BIT_0);
 
@@ -425,7 +432,6 @@
 void callbackParam()
 {
 
-
     while(serialMonit.readable()) {
         if ((indexParam  == sizeParam) || newParamFlag  == true) { //éviter la saturation du buffer
             NVIC_DisableIRQ(USART2_IRQn);  // USART 2 pour carte ARNSRS
@@ -605,11 +611,9 @@
         consigne = atoi(numb);
         control_Servo.setSetPoint(consigne);
         OUTPUT("  MAJ CONSIGNE PID -->  Consigne = %d?\r\n", consigne);
-        
     } else if (0 == strcmp(com, "dfu")) {
-        OUTPUT("  Mode DFU main \r\n");
-        FLASH_set_boot_bank(2);
-        HAL_NVIC_SystemReset;           
+        OUTPUT("  Passage en DFU...\r\n");
+        FLAG_DFU = true;
     } else if (0 == strcmp(com, "PID")) {
         if (FLAG_PID == 1) {
             control_Servo.setMode(MANUAL_MODE);
@@ -655,15 +659,15 @@
 
 int main()
 {
-    
+
     HAL_Init();
-    
+
 
     __HAL_RCC_PWR_CLK_ENABLE();
     HAL_PWR_EnableBkUpAccess();
-    
-    
-    
+
+
+
 
     //Ci-dessous commande pour formater une nouvelle carte
     UTILS::Format_Flash();
@@ -679,7 +683,7 @@
         DEBUG("ARNSRS ID forcée à 000\r\n");
     }
     /*
-while(1){
+    while(1){
     OUTPUT("\r\n\r\n  Démarrage de l'appareil, veuillez patienter...\r\n\r\n");}
     */
 
@@ -780,19 +784,19 @@
 
     //OUTPUT("  Cliquez sur le bouton help pour voir la liste des \r\n  commandes administrateur disponibles.\r\n");
 
-    
+
     OUTPUT("$\r\n");
     wait(0.5);
     UTILS::Dir_Flash(&serialMonit);
 
     serialMonit.attach(&callbackParam);
-    
+
     vusb_off_on.fall(&Stop_Sequence);
-    
+
     E5V = 1;
 
     buzzer.beep(1000,0.5);
-    
+
     while (true) {
 
         //Démarrage du Timer mesurant le temps d'éxecution du code
@@ -834,12 +838,12 @@
         if (FLAG_WINDEV) {
             OUTPUT("%s\r\n", to_store);
         }
-        
+
         //Pour l'IHM
         if (FLAG_DISPLAY) {
             IHM("%s\r\n", to_store);
         }
-       
+
         //Vers le moniteur série
         if (FLAG_AFF) {
             NVIC_DisableIRQ(USART3_IRQn); // USART 2 si ARNSRS mais plus utilisé
@@ -861,6 +865,23 @@
             }
         }
 
+        //Passage en DFU
+        if (FLAG_DFU) {
+            FLAG_DFU = false;
+            FLAG_REC = false;
+            //__disable_irq();
+            thread_Secu.terminate();
+            wait_ms(300);
+            thread_Head.terminate();
+            wait_ms(300);
+            thread_Volets.terminate();
+            wait_ms(300);
+            UTILS::UnMount_Flash();
+            FLASH_set_boot_bank(2);
+            OUTPUT("  Reboot...\r\n");
+            //HAL_NVIC_SystemReset;
+        }
+        
         //Update du PID
         control_Servo.setProcessValue(ppO2);
         //Nouvelle sortie servo fuite si on est pas en mode SECU