usbd_msc_storage.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. */
  4. /**
  5. ******************************************************************************
  6. * @file usbd_storage_msd.c
  7. * @author MCD application Team
  8. * @version V1.1.0
  9. * @date 19-March-2012
  10. * @brief This file provides the disk operations functions.
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  15. *
  16. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  17. * You may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at:
  19. *
  20. * http://www.st.com/software_license_agreement_liberty_v2
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. * Heavily modified by dpgeorge for MicroPython.
  29. *
  30. ******************************************************************************
  31. */
  32. #include <stdint.h>
  33. #include "usbd_cdc_msc_hid.h"
  34. #include "usbd_msc_storage.h"
  35. #include "py/mpstate.h"
  36. #include "storage.h"
  37. #include "sdcard.h"
  38. // These are needed to support removal of the medium, so that the USB drive
  39. // can be unmounted, and won't be remounted automatically.
  40. static uint8_t flash_started = 0;
  41. #if MICROPY_HW_HAS_SDCARD
  42. static uint8_t sdcard_started = 0;
  43. #endif
  44. /******************************************************************************/
  45. // Callback functions for when the internal flash is the mass storage device
  46. static const int8_t FLASH_STORAGE_Inquirydata[] = { // 36 bytes
  47. // LUN 0
  48. 0x00,
  49. 0x80, // 0x00 for a fixed drive, 0x80 for a removable drive
  50. 0x02,
  51. 0x02,
  52. (STANDARD_INQUIRY_DATA_LEN - 5),
  53. 0x00,
  54. 0x00,
  55. 0x00,
  56. 'u', 'P', 'y', ' ', ' ', ' ', ' ', ' ', // Manufacturer : 8 bytes
  57. 'm', 'i', 'c', 'r', 'o', 'S', 'D', ' ', // Product : 16 Bytes
  58. 'F', 'l', 'a', 's', 'h', ' ', ' ', ' ',
  59. '1', '.', '0' ,'0', // Version : 4 Bytes
  60. };
  61. /**
  62. * @brief Initialize the storage medium
  63. * @param lun : logical unit number
  64. * @retval Status
  65. */
  66. int8_t FLASH_STORAGE_Init(uint8_t lun) {
  67. storage_init();
  68. flash_started = 1;
  69. return 0;
  70. }
  71. /**
  72. * @brief return medium capacity and block size
  73. * @param lun : logical unit number
  74. * @param block_num : number of physical block
  75. * @param block_size : size of a physical block
  76. * @retval Status
  77. */
  78. int8_t FLASH_STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size) {
  79. *block_size = storage_get_block_size();
  80. *block_num = storage_get_block_count();
  81. return 0;
  82. }
  83. /**
  84. * @brief check whether the medium is ready
  85. * @param lun : logical unit number
  86. * @retval Status
  87. */
  88. int8_t FLASH_STORAGE_IsReady(uint8_t lun) {
  89. if (flash_started) {
  90. return 0;
  91. }
  92. return -1;
  93. }
  94. /**
  95. * @brief check whether the medium is write-protected
  96. * @param lun : logical unit number
  97. * @retval Status
  98. */
  99. int8_t FLASH_STORAGE_IsWriteProtected(uint8_t lun) {
  100. return 0;
  101. }
  102. // Remove the lun
  103. int8_t FLASH_STORAGE_StartStopUnit(uint8_t lun, uint8_t started) {
  104. flash_started = started;
  105. return 0;
  106. }
  107. int8_t FLASH_STORAGE_PreventAllowMediumRemoval(uint8_t lun, uint8_t param) {
  108. // sync the flash so that the cache is cleared and the device can be unplugged/turned off
  109. storage_flush();
  110. return 0;
  111. }
  112. /**
  113. * @brief Read data from the medium
  114. * @param lun : logical unit number
  115. * @param buf : Pointer to the buffer to save data
  116. * @param blk_addr : address of 1st block to be read
  117. * @param blk_len : nmber of blocks to be read
  118. * @retval Status
  119. */
  120. int8_t FLASH_STORAGE_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) {
  121. storage_read_blocks(buf, blk_addr, blk_len);
  122. return 0;
  123. }
  124. /**
  125. * @brief Write data to the medium
  126. * @param lun : logical unit number
  127. * @param buf : Pointer to the buffer to write from
  128. * @param blk_addr : address of 1st block to be written
  129. * @param blk_len : nmber of blocks to be read
  130. * @retval Status
  131. */
  132. int8_t FLASH_STORAGE_Write (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) {
  133. storage_write_blocks(buf, blk_addr, blk_len);
  134. return 0;
  135. }
  136. /**
  137. * @brief Return number of supported logical unit
  138. * @param None
  139. * @retval number of logical unit
  140. */
  141. int8_t FLASH_STORAGE_GetMaxLun(void) {
  142. return 0;
  143. }
  144. const USBD_StorageTypeDef USBD_FLASH_STORAGE_fops = {
  145. FLASH_STORAGE_Init,
  146. FLASH_STORAGE_GetCapacity,
  147. FLASH_STORAGE_IsReady,
  148. FLASH_STORAGE_IsWriteProtected,
  149. FLASH_STORAGE_StartStopUnit,
  150. FLASH_STORAGE_PreventAllowMediumRemoval,
  151. FLASH_STORAGE_Read,
  152. FLASH_STORAGE_Write,
  153. FLASH_STORAGE_GetMaxLun,
  154. (int8_t *)FLASH_STORAGE_Inquirydata,
  155. };
  156. /******************************************************************************/
  157. // Callback functions for when the SD card is the mass storage device
  158. #if MICROPY_HW_HAS_SDCARD
  159. static const int8_t SDCARD_STORAGE_Inquirydata[] = { // 36 bytes
  160. // LUN 0
  161. 0x00,
  162. 0x80, // 0x00 for a fixed drive, 0x80 for a removable drive
  163. 0x02,
  164. 0x02,
  165. (STANDARD_INQUIRY_DATA_LEN - 5),
  166. 0x00,
  167. 0x00,
  168. 0x00,
  169. 'u', 'P', 'y', ' ', ' ', ' ', ' ', ' ', // Manufacturer : 8 bytes
  170. 'm', 'i', 'c', 'r', 'o', 'S', 'D', ' ', // Product : 16 Bytes
  171. 'S', 'D', ' ', 'c', 'a', 'r', 'd', ' ',
  172. '1', '.', '0' ,'0', // Version : 4 Bytes
  173. };
  174. /**
  175. * @brief Initialize the storage medium
  176. * @param lun : logical unit number
  177. * @retval Status
  178. */
  179. int8_t SDCARD_STORAGE_Init(uint8_t lun) {
  180. if (!sdcard_power_on()) {
  181. return -1;
  182. }
  183. sdcard_started = 1;
  184. return 0;
  185. }
  186. /**
  187. * @brief return medium capacity and block size
  188. * @param lun : logical unit number
  189. * @param block_num : number of physical block
  190. * @param block_size : size of a physical block
  191. * @retval Status
  192. */
  193. int8_t SDCARD_STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size) {
  194. *block_size = SDCARD_BLOCK_SIZE;
  195. *block_num = sdcard_get_capacity_in_bytes() / SDCARD_BLOCK_SIZE;
  196. return 0;
  197. }
  198. /**
  199. * @brief check whether the medium is ready
  200. * @param lun : logical unit number
  201. * @retval Status
  202. */
  203. int8_t SDCARD_STORAGE_IsReady(uint8_t lun) {
  204. if (sdcard_started) {
  205. return 0;
  206. }
  207. return -1;
  208. }
  209. /**
  210. * @brief check whether the medium is write-protected
  211. * @param lun : logical unit number
  212. * @retval Status
  213. */
  214. int8_t SDCARD_STORAGE_IsWriteProtected(uint8_t lun) {
  215. return 0;
  216. }
  217. // Remove the lun
  218. int8_t SDCARD_STORAGE_StartStopUnit(uint8_t lun, uint8_t started) {
  219. sdcard_started = started;
  220. return 0;
  221. }
  222. int8_t SDCARD_STORAGE_PreventAllowMediumRemoval(uint8_t lun, uint8_t param) {
  223. return 0;
  224. }
  225. /**
  226. * @brief Read data from the medium
  227. * @param lun : logical unit number
  228. * @param buf : Pointer to the buffer to save data
  229. * @param blk_addr : address of 1st block to be read
  230. * @param blk_len : nmber of blocks to be read
  231. * @retval Status
  232. */
  233. int8_t SDCARD_STORAGE_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) {
  234. if (sdcard_read_blocks(buf, blk_addr, blk_len) != 0) {
  235. return -1;
  236. }
  237. return 0;
  238. }
  239. /**
  240. * @brief Write data to the medium
  241. * @param lun : logical unit number
  242. * @param buf : Pointer to the buffer to write from
  243. * @param blk_addr : address of 1st block to be written
  244. * @param blk_len : nmber of blocks to be read
  245. * @retval Status
  246. */
  247. int8_t SDCARD_STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) {
  248. if (sdcard_write_blocks(buf, blk_addr, blk_len) != 0) {
  249. return -1;
  250. }
  251. return 0;
  252. }
  253. /**
  254. * @brief Return number of supported logical unit
  255. * @param None
  256. * @retval number of logical unit
  257. */
  258. int8_t SDCARD_STORAGE_GetMaxLun(void) {
  259. return 0;
  260. }
  261. const USBD_StorageTypeDef USBD_SDCARD_STORAGE_fops = {
  262. SDCARD_STORAGE_Init,
  263. SDCARD_STORAGE_GetCapacity,
  264. SDCARD_STORAGE_IsReady,
  265. SDCARD_STORAGE_IsWriteProtected,
  266. SDCARD_STORAGE_StartStopUnit,
  267. SDCARD_STORAGE_PreventAllowMediumRemoval,
  268. SDCARD_STORAGE_Read,
  269. SDCARD_STORAGE_Write,
  270. SDCARD_STORAGE_GetMaxLun,
  271. (int8_t *)SDCARD_STORAGE_Inquirydata,
  272. };
  273. #endif // MICROPY_HW_HAS_SDCARD