From 5476234c94891ed499e64d938763cb24245222b2 Mon Sep 17 00:00:00 2001
From: C-Glick <colton.glick@gmail.com>
Date: Sun, 13 Feb 2022 23:20:02 -0600
Subject: [PATCH] Cleaned up dfu bootloader files

---
 .../src/hal/src/usb.c                         | 10 +---
 .../src/modules/interface/bootloader.h        | 47 +++++++++++++++++--
 .../src/modules/src/bootloader.c              | 44 +++++++++++++++--
 3 files changed, 82 insertions(+), 19 deletions(-)

diff --git a/crazyflie_software/crazyflie-firmware-2021.06/src/hal/src/usb.c b/crazyflie_software/crazyflie-firmware-2021.06/src/hal/src/usb.c
index 5b6c8f5a9..2a10496e6 100644
--- a/crazyflie_software/crazyflie-firmware-2021.06/src/hal/src/usb.c
+++ b/crazyflie_software/crazyflie-firmware-2021.06/src/hal/src/usb.c
@@ -48,8 +48,6 @@
 #include "crtp.h"
 #include "static_mem.h"
 
-#include "debug.h"
-
 #include "bootloader.h"
 
 
@@ -183,15 +181,9 @@ static uint8_t usbd_cf_Setup(void *pdev , USB_SETUP_REQ  *req)
     }
   } else if(command == 0x02){
     //restart system and transition to DFU bootloader mode
-
-    
     //enter bootloader specific to STM32f4xx
     enter_bootloader(0, 0x00000000);
-
-    
-  }
-  
-   else {
+  } else {
     crtpSetLink(radiolinkGetLink());
   }
   return USBD_OK;
diff --git a/crazyflie_software/crazyflie-firmware-2021.06/src/modules/interface/bootloader.h b/crazyflie_software/crazyflie-firmware-2021.06/src/modules/interface/bootloader.h
index 67bd7d069..cb993271e 100644
--- a/crazyflie_software/crazyflie-firmware-2021.06/src/modules/interface/bootloader.h
+++ b/crazyflie_software/crazyflie-firmware-2021.06/src/modules/interface/bootloader.h
@@ -1,12 +1,49 @@
+/**
+ *    ||          ____  _ __
+ * +------+      / __ )(_) /_______________ _____  ___
+ * | 0xBC |     / __  / / __/ ___/ ___/ __ `/_  / / _ \
+ * +------+    / /_/ / / /_/ /__/ /  / /_/ / / /_/  __/
+ *  ||  ||    /_____/_/\__/\___/_/   \__,_/ /___/\___/
+ *
+ * Crazyflie Firmware
+ *
+ * Copyright (C) Bitcraze AB
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, in version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @file bootloader.h
+ * Functions to handle transitioning from the firmware to bootloader (DFU) mode on startup
+ *
+ */
 
-/* FreeRtos includes */
-#include "FreeRTOS.h"
 /* ST includes */
 #include "stm32f4xx.h"
 
-
-void branch_to_bootloader(uint32_t r0, uint32_t bl_addr);
-
+/**
+ * @brief Check if memory is set after software 
+ * reboot indicating we need to branch to the bootloader.
+ * Run everytime on startup.
+ */
 void check_enter_bootloader();
 
+/**
+ * @brief Initiate the procedure to reboot into the bootloader.
+ * 
+ * This function does not return, instead setting a flag to 
+ * jump to the bootloader on the next start and
+ * issuing a software reset.
+ * 
+ * @param r0 The register to utilize when jumping
+ * @param bl_addr The bootloader address to jump to
+ */
 void enter_bootloader(uint32_t r0, uint32_t bl_addr);
\ No newline at end of file
diff --git a/crazyflie_software/crazyflie-firmware-2021.06/src/modules/src/bootloader.c b/crazyflie_software/crazyflie-firmware-2021.06/src/modules/src/bootloader.c
index eb5fdfb26..57e168c9a 100644
--- a/crazyflie_software/crazyflie-firmware-2021.06/src/modules/src/bootloader.c
+++ b/crazyflie_software/crazyflie-firmware-2021.06/src/modules/src/bootloader.c
@@ -1,6 +1,34 @@
+/**
+ *    ||          ____  _ __
+ * +------+      / __ )(_) /_______________ _____  ___
+ * | 0xBC |     / __  / / __/ ___/ ___/ __ `/_  / / _ \
+ * +------+    / /_/ / / /_/ /__/ /  / /_/ / / /_/  __/
+ *  ||  ||    /_____/_/\__/\___/_/   \__,_/ /___/\___/
+ *
+ * Crazyflie Firmware
+ *
+ * Copyright (C) Bitcraze AB
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, in version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @file bootloader.c
+ * Functions to handle transitioning from the firmware to bootloader (DFU) mode on startup
+ *
+ */
+
 #include "bootloader.h"
 
-//bootloader code used from micropython machine_bootloader
+// bootloader code based from micropython machine_bootloader function
 
 // STM32H7 has ECC and writes to RAM must be 64-bit so they are fully committed
 // to actual SRAM before a system reset occurs.
@@ -15,7 +43,15 @@
 #define BL_STATE_GET_ADDR(s)        (((s) >> BL_STATE_KEY_SHIFT) & ~BL_STATE_KEY_MASK)
 
 
-void branch_to_bootloader(uint32_t r0, uint32_t bl_addr){
+/**
+ * @brief Branch directly to the bootloader address, setting the 
+ * stack pointer and destination address first.
+ * Based from the micropython machine_bootloader function.
+ * 
+ * @param r0 The register to utilize
+ * @param bl_addr The bootloader address to jump to
+ */
+static void branch_to_bootloader(uint32_t r0, uint32_t bl_addr){
     __asm volatile (
         "ldr r2, [r1, #0]\n"    // get address of stack pointer
         "msr msp, r2\n"         // get stack pointer
@@ -26,15 +62,13 @@ void branch_to_bootloader(uint32_t r0, uint32_t bl_addr){
     while(1);
 }
 
-//check if memory is set after software reboot indicating we need to branch to the bootloader
 void check_enter_bootloader(){
     uint64_t bl_state = *BL_STATE_PTR;
     //set to invalid for next boot
     *BL_STATE_PTR = BL_STATE_INVALID;
 
-    //TODO test if statement
     if(BL_STATE_GET_KEY(bl_state) == BL_STATE_KEY && (RCC->CSR & RCC_CSR_SFTRSTF)){
-        //botloader data valid and was just reset with NVIC_SystemReset
+        //if botloader data valid and was just reset with NVIC_SystemReset
 
         //remap memory to system flash
         SYSCFG_MemoryRemapConfig(SYSCFG_MemoryRemap_SystemFlash);
-- 
GitLab