Auto updated submodule references [01-02-2025]
[betaflight.git] / src / platform / APM32 / usb / msc / usbd_memory.c
blobb0b04fb27bce4d625d01dd35df5d697db2ea08b9
1 /**
2 * @file usbd_memory.c
4 * @brief USB device memory management program body
6 * @attention
8 * Copyright (C) 2023 Geehy Semiconductor
10 * You may not use this file except in compliance with the
11 * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
13 * The program is only for reference, which is distributed in the hope
14 * that it will be useful and instructional for customers to develop
15 * their software. Unless required by applicable law or agreed to in
16 * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
17 * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
19 * and limitations under the License.
22 /* Includes ***************************************************************/
23 #include "usbd_memory.h"
25 /* Private includes *******************************************************/
26 #include "usbd_storage.h"
27 #include "platform.h"
29 /* Private macro **********************************************************/
30 #define MEMORY_LUN_NUM 1
31 #define MEMORY_BLOCK_NUM 80
32 #define MEMORY_BLOCK_SIZE 512
34 /* Private variables ******************************************************/
36 /* USB Mass storage Standard Inquiry Data */
37 const uint8_t memoryInquiryData[] =
39 /* lun 0 */
40 0x00,
41 0x80,
42 0x02,
43 0x02,
44 (USBD_LEN_STD_INQUIRY - 5),
45 0x00,
46 0x00,
47 0x00,
48 /* Manufacturer : 8 bytes */
49 'G', 'e', 'e', 'h', 'y', ' ', ' ', ' ',
50 /* Product : 16 Bytes */
51 'S', 't', 'o', 'r', 'a', 'g', 'e', ' ',
52 'D', 'i', 's', 'k', ' ', ' ', ' ', ' ',
53 /* Version : 4 Bytes */
54 '1', '.', '0', '0',
57 /* Private typedef ********************************************************/
59 /* USB FS MSC memory management handler */
60 USBD_MSC_MEMORY_T USBD_MEMORY_INTERFACE =
62 "MSC Memory",
63 (uint8_t*)memoryInquiryData,
64 USBD_MSC_MemoryReadMaxLun,
65 USBD_MSC_MemoryInit,
66 USBD_MSC_MemoryReadCapacity,
67 USBD_MSC_MemoryCheckReady,
68 USBD_MSC_MemoryCheckWPR,
69 USBD_MSC_MemoryReadData,
70 USBD_MSC_MemoryWriteData,
73 /* Private function prototypes ********************************************/
75 /* External variables *****************************************************/
77 /* External functions *****************************************************/
79 /**
80 * @brief USB device MSC memory unit init handler
82 * @param lun: lun number
84 * @retval USB device operation status
86 USBD_STA_T USBD_MSC_MemoryInit(uint8_t lun)
88 USBD_STA_T usbStatus = USBD_OK;
90 #ifdef USE_FLASHFS
91 USBD_MSC_EMFAT_fops.Init(lun);
92 #elif defined(USE_SDCARD_SDIO)
93 USBD_MSC_MICRO_SDIO_fops.Init(lun);
94 #elif defined(USE_SDCARD_SPI)
95 USBD_MSC_MICRO_SD_SPI_fops.Init(lun);
96 #else
97 UNUSED(lun);
98 #endif
100 return usbStatus;
104 * @brief USB device MSC memory unit read capacity handler
106 * @param lun: lun number
108 * @param blockNum: block number
110 * @param blockSize: block size
112 * @retval USB device operation status
114 USBD_STA_T USBD_MSC_MemoryReadCapacity(uint8_t lun, uint32_t* blockNum, \
115 uint16_t* blockSize)
117 USBD_STA_T usbStatus = USBD_OK;
119 #ifdef USE_FLASHFS
120 USBD_MSC_EMFAT_fops.GetCapacity(lun, blockNum, blockSize);
121 #elif defined(USE_SDCARD_SDIO)
122 USBD_MSC_MICRO_SDIO_fops.GetCapacity(lun, blockNum, blockSize);
123 #elif defined(USE_SDCARD_SPI)
124 USBD_MSC_MICRO_SD_SPI_fops.GetCapacity(lun, blockNum, blockSize);
125 #else
126 UNUSED(lun);
127 UNUSED(blockNum);
128 UNUSED(blockSize);
129 #endif
131 return usbStatus;
135 * @brief USB device MSC memory unit check read status handler
137 * @param lun: lun number
139 * @retval USB device operation status
141 USBD_STA_T USBD_MSC_MemoryCheckReady(uint8_t lun)
143 USBD_STA_T usbStatus = USBD_OK;
145 #ifdef USE_FLASHFS
146 USBD_MSC_EMFAT_fops.IsReady(lun);
147 #elif defined(USE_SDCARD_SDIO)
148 USBD_MSC_MICRO_SDIO_fops.IsReady(lun);
149 #elif defined(USE_SDCARD_SPI)
150 USBD_MSC_MICRO_SD_SPI_fops.IsReady(lun);
151 #else
152 UNUSED(lun);
153 #endif
155 return usbStatus;
159 * @brief USB device MSC memory unit check write protected status handler
161 * @param lun: lun number
163 * @retval USB device operation status
165 USBD_STA_T USBD_MSC_MemoryCheckWPR(uint8_t lun)
167 USBD_STA_T usbStatus = USBD_OK;
169 #ifdef USE_FLASHFS
170 USBD_MSC_EMFAT_fops.IsWriteProtected(lun);
171 #elif defined(USE_SDCARD_SDIO)
172 USBD_MSC_MICRO_SDIO_fops.IsWriteProtected(lun);
173 #elif defined(USE_SDCARD_SPI)
174 USBD_MSC_MICRO_SD_SPI_fops.IsWriteProtected(lun);
175 #else
176 UNUSED(lun);
177 #endif
179 return usbStatus;
183 * @brief USB device MSC memory read max LUN handler
185 * @param None
187 * @retval Max LUN number
189 uint8_t USBD_MSC_MemoryReadMaxLun(void)
191 #ifdef USE_FLASHFS
192 return USBD_MSC_EMFAT_fops.GetMaxLun();
193 #elif defined(USE_SDCARD_SDIO)
194 return USBD_MSC_MICRO_SDIO_fops.GetMaxLun();
195 #elif defined(USE_SDCARD_SPI)
196 return USBD_MSC_MICRO_SD_SPI_fops.GetMaxLun();
197 #else
198 return 0;
199 #endif
203 * @brief USB device MSC memory unit read data handler
205 * @param lun: lun number
207 * @param buffer: data buffer
209 * @param blockAddr: block address
211 * @param blockLength: block number
213 * @retval USB device operation status
215 USBD_STA_T USBD_MSC_MemoryReadData(uint8_t lun, uint8_t* buffer, uint32_t blockAddr, \
216 uint16_t blockLength)
218 USBD_STA_T usbStatus = USBD_OK;
220 #ifdef USE_FLASHFS
221 USBD_MSC_EMFAT_fops.Read(lun, buffer, blockAddr, blockLength);
222 #elif defined(USE_SDCARD_SDIO)
223 USBD_MSC_MICRO_SDIO_fops.Read(lun, buffer, blockAddr, blockLength);
224 #elif defined(USE_SDCARD_SPI)
225 USBD_MSC_MICRO_SD_SPI_fops.Read(lun, buffer, blockAddr, blockLength);
226 #else
227 UNUSED(lun);
228 UNUSED(buffer);
229 UNUSED(blockAddr);
230 UNUSED(blockLength);
231 #endif
233 return usbStatus;
237 * @brief USB device MSC memory unit write data handler
239 * @param lun: lun number
241 * @param buffer: data buffer
243 * @param blockAddr: block address
245 * @param blockLength: block number
247 * @retval USB device operation status
249 USBD_STA_T USBD_MSC_MemoryWriteData(uint8_t lun, uint8_t* buffer, uint32_t blockAddr, \
250 uint16_t blockLength)
252 USBD_STA_T usbStatus = USBD_OK;
254 #ifdef USE_FLASHFS
255 USBD_MSC_EMFAT_fops.Write(lun, buffer, blockAddr, blockLength);
256 #elif defined(USE_SDCARD_SDIO)
257 USBD_MSC_MICRO_SDIO_fops.Write(lun, buffer, blockAddr, blockLength);
258 #elif defined(USE_SDCARD_SPI)
259 USBD_MSC_MICRO_SD_SPI_fops.Write(lun, buffer, blockAddr, blockLength);
260 #else
261 UNUSED(lun);
262 UNUSED(buffer);
263 UNUSED(blockAddr);
264 UNUSED(blockLength);
265 #endif
267 return usbStatus;