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:
52:4e36a88711d6
Parent:
51:16a055fd287c
Child:
53:84571a5e2cda
--- a/main.cpp	Wed May 30 13:02:01 2018 +0000
+++ b/main.cpp	Wed May 30 16:31:43 2018 +0000
@@ -156,6 +156,39 @@
 //Buzzer
 Beep buzzer(PC_8);
 
+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);
+
+  /* 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");
+  }
+}
 
 void Affichage()
 {   
@@ -575,11 +608,8 @@
         
     } else if (0 == strcmp(com, "dfu")) {
         OUTPUT("  Mode DFU main \r\n");
-        *((unsigned long *) 0x10007FF0) = 0xDEADBEEF;
-        wait(1);
-        NVIC_SystemReset();
-        
-            
+        FLASH_set_boot_bank(2);
+        HAL_NVIC_SystemReset;           
     } else if (0 == strcmp(com, "PID")) {
         if (FLAG_PID == 1) {
             control_Servo.setMode(MANUAL_MODE);
@@ -636,7 +666,7 @@
     
 
     //Ci-dessous commande pour formater une nouvelle carte
-    //UTILS::Format_Flash();
+    UTILS::Format_Flash();
 
     //Montage Flash
     UTILS::Mount_Flash();