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 mscCheckBoot(void)
71 const uint32_t bootModeRequest
= persistentObjectRead(PERSISTENT_OBJECT_RESET_REASON
);
72 return bootModeRequest
== RESET_MSC_REQUEST
;
73 // Note that we can't clear the persisent object after checking here. This is because
74 // this function is called multiple times during initialization. So we clear on a reset
78 void mscSetActive(void)
80 lastActiveTimeMs
= millis();
83 void mscActivityLed(void)
85 static timeMs_t nextToggleMs
= 0;
86 const timeMs_t nowMs
= millis();
88 if (nowMs
- lastActiveTimeMs
> ACTIVITY_LED_PERIOD_MS
) {
91 } else if (nowMs
> nextToggleMs
) {
93 nextToggleMs
= nowMs
+ ACTIVITY_LED_PERIOD_MS
;
97 bool mscCheckButton(void)
101 uint8_t state
= IORead(mscButton
);
102 if (usbDevConfig()->mscButtonUsePullup
) {
112 void mscWaitForButton(void)
114 // In order to exit MSC mode simply disconnect the board, or push the button again.
115 while (mscCheckButton());
116 delay(DEBOUNCE_TIME_MS
);
119 if (mscCheckButton()) {
120 systemResetFromMsc();
126 void systemResetToMsc(int timezoneOffsetMinutes
)
128 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON
, RESET_MSC_REQUEST
);
132 // Persist the RTC across the reboot to use as the file timestamp
133 #ifdef USE_PERSISTENT_MSC_RTC
134 rtcPersistWrite(timezoneOffsetMinutes
);
136 UNUSED(timezoneOffsetMinutes
);
141 void systemResetFromMsc(void)
143 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON
, RESET_NONE
);