GPS Home altitude is added to blackbox logging (#13939)
[betaflight.git] / src / main / drivers / usb_msc_common.c
blob347ec159f71e8448b5a94709d881bacedc121880
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
22 * Author: Chris Hockuba (https://github.com/conkerkh)
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include <string.h>
30 #include "platform.h"
32 #if defined(USE_USB_MSC)
34 #include "build/build_config.h"
36 #include "common/utils.h"
38 #include "drivers/io.h"
39 #include "drivers/light_led.h"
40 #include "drivers/nvic.h"
41 #include "drivers/persistent.h"
42 #include "drivers/system.h"
43 #include "drivers/time.h"
44 #include "drivers/usb_msc.h"
46 #include "msc/usbd_storage.h"
48 #include "pg/usb.h"
50 #define DEBOUNCE_TIME_MS 20
51 #define ACTIVITY_LED_PERIOD_MS 50
53 static IO_t mscButton;
54 static timeMs_t lastActiveTimeMs = 0;
56 void mscInit(void)
58 if (usbDevConfig()->mscButtonPin) {
59 mscButton = IOGetByTag(usbDevConfig()->mscButtonPin);
60 IOInit(mscButton, OWNER_USB_MSC_PIN, 0);
61 if (usbDevConfig()->mscButtonUsePullup) {
62 IOConfigGPIO(mscButton, IOCFG_IPU);
63 } else {
64 IOConfigGPIO(mscButton, IOCFG_IPD);
69 bool mscCheckBootAndReset(void)
71 static bool firstCheck = true;
72 static bool mscMode;
74 if (firstCheck) {
75 // Cache the bootup value of RESET_MSC_REQUEST
76 const uint32_t bootModeRequest = persistentObjectRead(PERSISTENT_OBJECT_RESET_REASON);
77 if (bootModeRequest == RESET_MSC_REQUEST) {
78 mscMode = true;
79 // Ensure the next reset is to the configurator as the H7 processor retains the RTC value so
80 // a brief interruption of power is not enough to switch out of MSC mode
81 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_NONE);
82 firstCheck = false;
86 return mscMode;
89 void mscSetActive(void)
91 lastActiveTimeMs = millis();
94 void mscActivityLed(void)
96 static timeMs_t nextToggleMs = 0;
97 const timeMs_t nowMs = millis();
99 if (nowMs - lastActiveTimeMs > ACTIVITY_LED_PERIOD_MS) {
100 LED0_OFF;
101 nextToggleMs = 0;
102 } else if (nowMs > nextToggleMs) {
103 LED0_TOGGLE;
104 nextToggleMs = nowMs + ACTIVITY_LED_PERIOD_MS;
108 bool mscCheckButton(void)
110 bool result = false;
111 if (mscButton) {
112 uint8_t state = IORead(mscButton);
113 if (usbDevConfig()->mscButtonUsePullup) {
114 result = state == 0;
115 } else {
116 result = state == 1;
120 return result;
123 void mscWaitForButton(void)
125 // In order to exit MSC mode simply disconnect the board, or push the button again.
126 while (mscCheckButton());
127 delay(DEBOUNCE_TIME_MS);
128 while (true) {
129 asm("NOP");
130 if (mscCheckButton()) {
131 systemResetFromMsc();
133 mscActivityLed();
137 void systemResetToMsc(int timezoneOffsetMinutes)
139 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_MSC_REQUEST);
141 __disable_irq();
143 // Persist the RTC across the reboot to use as the file timestamp
144 #ifdef USE_PERSISTENT_MSC_RTC
145 rtcPersistWrite(timezoneOffsetMinutes);
146 #else
147 UNUSED(timezoneOffsetMinutes);
148 #endif
149 NVIC_SystemReset();
152 void systemResetFromMsc(void)
154 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_NONE);
155 __disable_irq();
156 NVIC_SystemReset();
159 #endif