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 "blackbox/blackbox.h"
37 #include "blackbox/blackbox_io.h"
39 #include "common/utils.h"
41 #include "drivers/io.h"
42 #include "drivers/light_led.h"
43 #include "drivers/nvic.h"
44 #include "drivers/persistent.h"
45 #include "drivers/time.h"
46 #include "drivers/usb_msc.h"
48 #include "vcp_hal/usbd_cdc_interface.h"
51 #include "msc/usbd_storage.h"
53 // declared in drivers/serial_usb_vcp.c for F7
54 extern USBD_HandleTypeDef USBD_Device
;
56 #define DEBOUNCE_TIME_MS 20
58 #if defined(MSC_USE_BUTTON)
59 static IO_t mscButton
;
64 #if defined(MSC_USE_BUTTON)
65 if (usbDevConfig()->mscButtonPin
) {
66 mscButton
= IOGetByTag(usbDevConfig()->mscButtonPin
);
67 IOInit(mscButton
, OWNER_USB_MSC_PIN
, 0);
68 if (usbDevConfig()->mscButtonUsePullup
) {
69 IOConfigGPIO(mscButton
, IOCFG_IPU
);
71 IOConfigGPIO(mscButton
, IOCFG_IPD
);
77 uint8_t mscStart(void)
82 usbGenerateDisconnectPulse();
84 IOInit(IOGetByTag(IO_TAG(PA11
)), OWNER_USB
, 0, 0);
85 IOInit(IOGetByTag(IO_TAG(PA12
)), OWNER_USB
, 0, 0);
87 USBD_Init(&USBD_Device
, &VCP_Desc
, 0);
90 USBD_RegisterClass(&USBD_Device
, USBD_MSC_CLASS
);
92 /** Register interface callbacks */
93 switch (blackboxConfig()->device
) {
95 case BLACKBOX_DEVICE_SDCARD
:
96 USBD_MSC_RegisterStorage(&USBD_Device
, &USBD_MSC_MICRO_SDIO_fops
);
101 case BLACKBOX_DEVICE_FLASH
:
102 USBD_MSC_RegisterStorage(&USBD_Device
, &USBD_MSC_EMFAT_fops
);
110 USBD_Start(&USBD_Device
);
112 // NVIC configuration for SYSTick
113 NVIC_DisableIRQ(SysTick_IRQn
);
114 NVIC_SetPriority(SysTick_IRQn
, 0);
115 NVIC_EnableIRQ(SysTick_IRQn
);
120 bool mscCheckButton(void)
123 #if defined(MSC_USE_BUTTON)
125 uint8_t state
= IORead(mscButton
);
126 if (usbDevConfig()->mscButtonUsePullup
) {
137 void mscWaitForButton(void)
139 // In order to exit MSC mode simply disconnect the board, or push the button again.
140 while (mscCheckButton());
141 delay(DEBOUNCE_TIME_MS
);
144 if (mscCheckButton()) {
145 *((uint32_t *)0x2001FFF0) = 0xFFFFFFFF;