Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / usb_msc_f4xx.c
blob8bc9a9ade3ee6a04acee57d5562a7afa4eb45ad6
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 "blackbox/blackbox.h"
39 #include "blackbox/blackbox_io.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/sdmmc_sdio.h"
46 #include "drivers/time.h"
47 #include "drivers/usb_msc.h"
49 #if defined(STM32F4)
50 #include "usb_core.h"
51 #include "usbd_cdc_vcp.h"
52 #include "usb_io.h"
53 #elif defined(STM32F7)
54 #include "vcp_hal/usbd_cdc_interface.h"
55 #include "usb_io.h"
56 USBD_HandleTypeDef USBD_Device;
57 #else
58 #include "usb_core.h"
59 #include "usb_init.h"
60 #include "hw_config.h"
61 #endif
63 #include "msc/usbd_storage.h"
65 #define DEBOUNCE_TIME_MS 20
67 #if defined(MSC_USE_BUTTON)
68 static IO_t mscButton;
69 #endif
71 void mscInit(void)
73 #if defined(MSC_USE_BUTTON)
74 if (usbDevConfig()->mscButtonPin) {
75 mscButton = IOGetByTag(usbDevConfig()->mscButtonPin);
76 IOInit(mscButton, OWNER_USB_MSC_PIN, 0);
77 if (usbDevConfig()->mscButtonUsePullup) {
78 IOConfigGPIO(mscButton, IOCFG_IPU);
79 } else {
80 IOConfigGPIO(mscButton, IOCFG_IPD);
83 #endif
86 uint8_t mscStart(void)
88 ledInit(false);
90 //Start USB
91 usbGenerateDisconnectPulse();
93 IOInit(IOGetByTag(IO_TAG(PA11)), OWNER_USB, 0, 0);
94 IOInit(IOGetByTag(IO_TAG(PA12)), OWNER_USB, 0, 0);
96 switch (blackboxConfig()->device) {
97 #ifdef USE_SDCARD
98 case BLACKBOX_DEVICE_SDCARD:
99 USBD_STORAGE_fops = &USBD_MSC_MICRO_SDIO_fops;
100 break;
101 #endif
103 #ifdef USE_FLASHFS
104 case BLACKBOX_DEVICE_FLASH:
105 USBD_STORAGE_fops = &USBD_MSC_EMFAT_fops;
106 break;
107 #endif
108 default:
109 return 1;
112 USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &MSC_desc, &USBD_MSC_cb, &USR_cb);
114 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_NONE);
116 // NVIC configuration for SYSTick
117 NVIC_DisableIRQ(SysTick_IRQn);
118 NVIC_SetPriority(SysTick_IRQn, 0);
119 NVIC_EnableIRQ(SysTick_IRQn);
121 return 0;
124 bool mscCheckButton(void)
126 bool result = false;
127 #if defined(MSC_USE_BUTTON)
128 if (mscButton) {
129 uint8_t state = IORead(mscButton);
130 if (usbDevConfig()->mscButtonUsePullup) {
131 result = state == 0;
132 } else {
133 result = state == 1;
136 #endif
137 return result;
140 void mscWaitForButton(void)
142 // In order to exit MSC mode simply disconnect the board, or push the button again.
143 while (mscCheckButton());
144 delay(DEBOUNCE_TIME_MS);
145 while (true) {
146 asm("NOP");
147 if (mscCheckButton()) {
148 delay(1);
149 NVIC_SystemReset();
153 #endif