Refactor common USB MSC code and improve activity LED
[betaflight.git] / src / main / drivers / usb_msc_common.c
blobada2bdfb6678b18e70d0bbe09e165b6dc4ba475d
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 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
75 // out of MSC mode.
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) {
89 LED0_OFF;
90 nextToggleMs = 0;
91 } else if (nowMs > nextToggleMs) {
92 LED0_TOGGLE;
93 nextToggleMs = nowMs + ACTIVITY_LED_PERIOD_MS;
97 bool mscCheckButton(void)
99 bool result = false;
100 if (mscButton) {
101 uint8_t state = IORead(mscButton);
102 if (usbDevConfig()->mscButtonUsePullup) {
103 result = state == 0;
104 } else {
105 result = state == 1;
109 return result;
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);
117 while (true) {
118 asm("NOP");
119 if (mscCheckButton()) {
120 systemResetFromMsc();
122 mscActivityLed();
126 void systemResetToMsc(int timezoneOffsetMinutes)
128 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_MSC_REQUEST);
130 __disable_irq();
132 // Persist the RTC across the reboot to use as the file timestamp
133 #ifdef USE_PERSISTENT_MSC_RTC
134 rtcPersistWrite(timezoneOffsetMinutes);
135 #else
136 UNUSED(timezoneOffsetMinutes);
137 #endif
138 NVIC_SystemReset();
141 void systemResetFromMsc(void)
143 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_NONE);
144 __disable_irq();
145 NVIC_SystemReset();
148 #endif