usbd_storage_if.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : usbd_storage_if.c
  5. * @version : v1.0_Cube
  6. * @brief : Memory management layer.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software component is licensed by ST under Ultimate Liberty license
  14. * SLA0044, the "License"; You may not use this file except in compliance with
  15. * the License. You may obtain a copy of the License at:
  16. * www.st.com/SLA0044
  17. *
  18. ******************************************************************************
  19. */
  20. /* USER CODE END Header */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "usbd_storage_if.h"
  23. /* USER CODE BEGIN INCLUDE */
  24. /* USER CODE END INCLUDE */
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* Private define ------------------------------------------------------------*/
  27. /* Private macro -------------------------------------------------------------*/
  28. /* USER CODE BEGIN PV */
  29. /* Private variables ---------------------------------------------------------*/
  30. /* USER CODE END PV */
  31. /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  32. * @brief Usb device.
  33. * @{
  34. */
  35. /** @defgroup USBD_STORAGE
  36. * @brief Usb mass storage device module
  37. * @{
  38. */
  39. /** @defgroup USBD_STORAGE_Private_TypesDefinitions
  40. * @brief Private types.
  41. * @{
  42. */
  43. /* USER CODE BEGIN PRIVATE_TYPES */
  44. /* USER CODE END PRIVATE_TYPES */
  45. /**
  46. * @}
  47. */
  48. /** @defgroup USBD_STORAGE_Private_Defines
  49. * @brief Private defines.
  50. * @{
  51. */
  52. #define STORAGE_LUN_NBR 1
  53. #define STORAGE_BLK_NBR 0x10000
  54. #define STORAGE_BLK_SIZ 0x200
  55. /* USER CODE BEGIN PRIVATE_DEFINES */
  56. /* USER CODE END PRIVATE_DEFINES */
  57. /**
  58. * @}
  59. */
  60. /** @defgroup USBD_STORAGE_Private_Macros
  61. * @brief Private macros.
  62. * @{
  63. */
  64. /* USER CODE BEGIN PRIVATE_MACRO */
  65. /* USER CODE END PRIVATE_MACRO */
  66. /**
  67. * @}
  68. */
  69. /** @defgroup USBD_STORAGE_Private_Variables
  70. * @brief Private variables.
  71. * @{
  72. */
  73. /* USER CODE BEGIN INQUIRY_DATA_FS */
  74. /** USB Mass storage Standard Inquiry Data. */
  75. const int8_t STORAGE_Inquirydata_FS[] = {/* 36 */
  76. /* LUN 0 */
  77. 0x00,
  78. 0x80,
  79. 0x02,
  80. 0x02,
  81. (STANDARD_INQUIRY_DATA_LEN - 5),
  82. 0x00,
  83. 0x00,
  84. 0x00,
  85. 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
  86. 'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */
  87. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  88. '0', '.', '0' ,'1' /* Version : 4 Bytes */
  89. };
  90. /* USER CODE END INQUIRY_DATA_FS */
  91. /* USER CODE BEGIN INQUIRY_DATA_HS */
  92. /** USB Mass storage Standard Inquiry Data. */
  93. const int8_t STORAGE_Inquirydata_HS[] = {/* 36 */
  94. /* LUN 0 */
  95. 0x00,
  96. 0x80,
  97. 0x02,
  98. 0x02,
  99. (STANDARD_INQUIRY_DATA_LEN - 5),
  100. 0x00,
  101. 0x00,
  102. 0x00,
  103. 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
  104. 'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */
  105. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  106. '0', '.', '0' ,'1' /* Version : 4 Bytes */
  107. };
  108. /* USER CODE END INQUIRY_DATA_HS */
  109. /* USER CODE BEGIN PRIVATE_VARIABLES */
  110. /* USER CODE END PRIVATE_VARIABLES */
  111. /**
  112. * @}
  113. */
  114. /** @defgroup USBD_STORAGE_Exported_Variables
  115. * @brief Public variables.
  116. * @{
  117. */
  118. extern USBD_HandleTypeDef hUsbDeviceFS;
  119. extern USBD_HandleTypeDef hUsbDeviceHS;
  120. /* USER CODE BEGIN EXPORTED_VARIABLES */
  121. /* USER CODE END EXPORTED_VARIABLES */
  122. /**
  123. * @}
  124. */
  125. /** @defgroup USBD_STORAGE_Private_FunctionPrototypes
  126. * @brief Private functions declaration.
  127. * @{
  128. */
  129. static int8_t STORAGE_Init_FS(uint8_t lun);
  130. static int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
  131. static int8_t STORAGE_IsReady_FS(uint8_t lun);
  132. static int8_t STORAGE_IsWriteProtected_FS(uint8_t lun);
  133. static int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
  134. static int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
  135. static int8_t STORAGE_GetMaxLun_FS(void);
  136. static int8_t STORAGE_Init_HS(uint8_t lun);
  137. static int8_t STORAGE_GetCapacity_HS(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
  138. static int8_t STORAGE_IsReady_HS(uint8_t lun);
  139. static int8_t STORAGE_IsWriteProtected_HS(uint8_t lun);
  140. static int8_t STORAGE_Read_HS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
  141. static int8_t STORAGE_Write_HS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
  142. static int8_t STORAGE_GetMaxLun_HS(void);
  143. /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
  144. /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
  145. /**
  146. * @}
  147. */
  148. USBD_StorageTypeDef USBD_Storage_Interface_fops_FS =
  149. {
  150. STORAGE_Init_FS,
  151. STORAGE_GetCapacity_FS,
  152. STORAGE_IsReady_FS,
  153. STORAGE_IsWriteProtected_FS,
  154. STORAGE_Read_FS,
  155. STORAGE_Write_FS,
  156. STORAGE_GetMaxLun_FS,
  157. (int8_t *)STORAGE_Inquirydata_FS
  158. };
  159. USBD_StorageTypeDef USBD_Storage_Interface_fops_HS =
  160. {
  161. STORAGE_Init_HS,
  162. STORAGE_GetCapacity_HS,
  163. STORAGE_IsReady_HS,
  164. STORAGE_IsWriteProtected_HS,
  165. STORAGE_Read_HS,
  166. STORAGE_Write_HS,
  167. STORAGE_GetMaxLun_HS,
  168. (int8_t *)STORAGE_Inquirydata_HS
  169. };
  170. /* Private functions ---------------------------------------------------------*/
  171. /**
  172. * @brief Initializes over USB FS IP
  173. * @param lun:
  174. * @retval USBD_OK if all operations are OK else USBD_FAIL
  175. */
  176. int8_t STORAGE_Init_FS(uint8_t lun)
  177. {
  178. /* USER CODE BEGIN 2 */
  179. return (USBD_OK);
  180. /* USER CODE END 2 */
  181. }
  182. /**
  183. * @brief .
  184. * @param lun: .
  185. * @param block_num: .
  186. * @param block_size: .
  187. * @retval USBD_OK if all operations are OK else USBD_FAIL
  188. */
  189. int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
  190. {
  191. /* USER CODE BEGIN 3 */
  192. *block_num = STORAGE_BLK_NBR;
  193. *block_size = STORAGE_BLK_SIZ;
  194. return (USBD_OK);
  195. /* USER CODE END 3 */
  196. }
  197. /**
  198. * @brief .
  199. * @param lun: .
  200. * @retval USBD_OK if all operations are OK else USBD_FAIL
  201. */
  202. int8_t STORAGE_IsReady_FS(uint8_t lun)
  203. {
  204. /* USER CODE BEGIN 4 */
  205. return (USBD_OK);
  206. /* USER CODE END 4 */
  207. }
  208. /**
  209. * @brief .
  210. * @param lun: .
  211. * @retval USBD_OK if all operations are OK else USBD_FAIL
  212. */
  213. int8_t STORAGE_IsWriteProtected_FS(uint8_t lun)
  214. {
  215. /* USER CODE BEGIN 5 */
  216. return (USBD_OK);
  217. /* USER CODE END 5 */
  218. }
  219. /**
  220. * @brief .
  221. * @param lun: .
  222. * @retval USBD_OK if all operations are OK else USBD_FAIL
  223. */
  224. int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
  225. {
  226. /* USER CODE BEGIN 6 */
  227. return (USBD_OK);
  228. /* USER CODE END 6 */
  229. }
  230. /**
  231. * @brief .
  232. * @param lun: .
  233. * @retval USBD_OK if all operations are OK else USBD_FAIL
  234. */
  235. int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
  236. {
  237. /* USER CODE BEGIN 7 */
  238. return (USBD_OK);
  239. /* USER CODE END 7 */
  240. }
  241. /**
  242. * @brief .
  243. * @param None
  244. * @retval .
  245. */
  246. int8_t STORAGE_GetMaxLun_FS(void)
  247. {
  248. /* USER CODE BEGIN 8 */
  249. return (STORAGE_LUN_NBR - 1);
  250. /* USER CODE END 8 */
  251. }
  252. /**
  253. * @brief .
  254. * @param lun: .
  255. * @retval USBD_OK if all operations are OK else USBD_FAIL
  256. */
  257. int8_t STORAGE_Init_HS(uint8_t lun)
  258. {
  259. /* USER CODE BEGIN 9 */
  260. return (USBD_OK);
  261. /* USER CODE END 9 */
  262. }
  263. /**
  264. * @brief .
  265. * @param lun: .
  266. * @param block_num: .
  267. * @param block_size: .
  268. * @retval USBD_OK if all operations are OK else USBD_FAIL
  269. */
  270. int8_t STORAGE_GetCapacity_HS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
  271. {
  272. /* USER CODE BEGIN 10 */
  273. *block_num = STORAGE_BLK_NBR;
  274. *block_size = STORAGE_BLK_SIZ;
  275. return (USBD_OK);
  276. /* USER CODE END 10 */
  277. }
  278. /**
  279. * @brief .
  280. * @param lun: .
  281. * @retval USBD_OK if all operations are OK else USBD_FAIL
  282. */
  283. int8_t STORAGE_IsReady_HS(uint8_t lun)
  284. {
  285. /* USER CODE BEGIN 11 */
  286. return (USBD_OK);
  287. /* USER CODE END 11 */
  288. }
  289. /**
  290. * @brief .
  291. * @param lun: .
  292. * @retval USBD_OK if all operations are OK else USBD_FAIL
  293. */
  294. int8_t STORAGE_IsWriteProtected_HS(uint8_t lun)
  295. {
  296. /* USER CODE BEGIN 12 */
  297. return (USBD_OK);
  298. /* USER CODE END 12 */
  299. }
  300. /**
  301. * @brief .
  302. * @param lun: .
  303. * @param buf: .
  304. * @param blk_addr: .
  305. * @param blk_len: .
  306. * @retval USBD_OK if all operations are OK else USBD_FAIL
  307. */
  308. int8_t STORAGE_Read_HS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
  309. {
  310. /* USER CODE BEGIN 13 */
  311. return (USBD_OK);
  312. /* USER CODE END 13 */
  313. }
  314. /**
  315. * @brief .
  316. * @param lun: .
  317. * @param buf: .
  318. * @param blk_addr: .
  319. * @param blk_len: .
  320. * @retval USBD_OK if all operations are OK else USBD_FAIL
  321. */
  322. int8_t STORAGE_Write_HS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
  323. {
  324. /* USER CODE BEGIN 14 */
  325. return (USBD_OK);
  326. /* USER CODE END 14 */
  327. }
  328. /**
  329. * @brief .
  330. * @param None
  331. * @retval .
  332. */
  333. int8_t STORAGE_GetMaxLun_HS(void)
  334. {
  335. /* USER CODE BEGIN 15 */
  336. return (STORAGE_LUN_NBR - 1);
  337. /* USER CODE END 15 */
  338. }
  339. /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
  340. /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
  341. /**
  342. * @}
  343. */
  344. /**
  345. * @}
  346. */
  347. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/