Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / usb_msc_h7xx.c
blob9bd6bdc8b37f3420da2a194e830ea20bd9865b40
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 "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"
49 #include "usb_io.h"
50 #include "usbd_msc.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;
60 #endif
62 void mscInit(void)
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);
70 } else {
71 IOConfigGPIO(mscButton, IOCFG_IPD);
74 #endif
77 uint8_t mscStart(void)
79 ledInit(false);
81 //Start USB
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);
89 /** Regsiter class */
90 USBD_RegisterClass(&USBD_Device, USBD_MSC_CLASS);
92 /** Register interface callbacks */
93 switch (blackboxConfig()->device) {
94 #ifdef USE_SDCARD
95 case BLACKBOX_DEVICE_SDCARD:
96 USBD_MSC_RegisterStorage(&USBD_Device, &USBD_MSC_MICRO_SDIO_fops);
97 break;
98 #endif
100 #ifdef USE_FLASHFS
101 case BLACKBOX_DEVICE_FLASH:
102 USBD_MSC_RegisterStorage(&USBD_Device, &USBD_MSC_EMFAT_fops);
103 break;
104 #endif
106 default:
107 return 1;
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);
117 return 0;
120 bool mscCheckButton(void)
122 bool result = false;
123 #if defined(MSC_USE_BUTTON)
124 if (mscButton) {
125 uint8_t state = IORead(mscButton);
126 if (usbDevConfig()->mscButtonUsePullup) {
127 result = state == 0;
128 } else {
129 result = state == 1;
132 #endif
134 return result;
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);
142 while (true) {
143 asm("NOP");
144 if (mscCheckButton()) {
145 *((uint32_t *)0x2001FFF0) = 0xFFFFFFFF;
146 delay(1);
147 NVIC_SystemReset();
151 #endif