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)
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)
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"
50 #define DEBOUNCE_TIME_MS 20
51 #define ACTIVITY_LED_PERIOD_MS 50
53 static IO_t mscButton
;
54 static timeMs_t lastActiveTimeMs
= 0;
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
);
64 IOConfigGPIO(mscButton
, IOCFG_IPD
);
69 bool mscCheckBootAndReset(void)
71 static bool firstCheck
= true;
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
) {
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
);
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
) {
102 } else if (nowMs
> nextToggleMs
) {
104 nextToggleMs
= nowMs
+ ACTIVITY_LED_PERIOD_MS
;
108 bool mscCheckButton(void)
112 uint8_t state
= IORead(mscButton
);
113 if (usbDevConfig()->mscButtonUsePullup
) {
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
);
130 if (mscCheckButton()) {
131 systemResetFromMsc();
137 void systemResetToMsc(int timezoneOffsetMinutes
)
139 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON
, RESET_MSC_REQUEST
);
143 // Persist the RTC across the reboot to use as the file timestamp
144 #ifdef USE_PERSISTENT_MSC_RTC
145 rtcPersistWrite(timezoneOffsetMinutes
);
147 UNUSED(timezoneOffsetMinutes
);
152 void systemResetFromMsc(void)
154 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON
, RESET_NONE
);