| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,187 @@ |
| 1 |
+/** |
|
| 2 |
+ ****************************************************************************** |
|
| 3 |
+ * @file usbd_msc_storage_template.c |
|
| 4 |
+ * @author MCD Application Team |
|
| 5 |
+ * @brief Memory management layer |
|
| 6 |
+ ****************************************************************************** |
|
| 7 |
+ * @attention |
|
| 8 |
+ * |
|
| 9 |
+ * Copyright (c) 2015 STMicroelectronics. |
|
| 10 |
+ * All rights reserved. |
|
| 11 |
+ * |
|
| 12 |
+ * This software is licensed under terms that can be found in the LICENSE file |
|
| 13 |
+ * in the root directory of this software component. |
|
| 14 |
+ * If no LICENSE file comes with this software, it is provided AS-IS. |
|
| 15 |
+ * |
|
| 16 |
+ ****************************************************************************** |
|
| 17 |
+ */ |
|
| 18 |
+ |
|
| 19 |
+/* Includes ------------------------------------------------------------------*/ |
|
| 20 |
+#include "usbd_storage_if.h" |
|
| 21 |
+#include "../usb.h" |
|
| 22 |
+ |
|
| 23 |
+ |
|
| 24 |
+/* Private typedef -----------------------------------------------------------*/ |
|
| 25 |
+/* Private define ------------------------------------------------------------*/ |
|
| 26 |
+/* Private macro -------------------------------------------------------------*/ |
|
| 27 |
+/* Private variables ---------------------------------------------------------*/ |
|
| 28 |
+/* Private function prototypes -----------------------------------------------*/ |
|
| 29 |
+/* Extern function prototypes ------------------------------------------------*/ |
|
| 30 |
+/* Private functions ---------------------------------------------------------*/ |
|
| 31 |
+ |
|
| 32 |
+#define STORAGE_LUN_NBR 1U |
|
| 33 |
+#define STORAGE_BLK_NBR USB_FS_SECTOR_QUANTITY |
|
| 34 |
+#define STORAGE_BLK_SIZ USB_FS_SECTOR_SIZE |
|
| 35 |
+ |
|
| 36 |
+int8_t STORAGE_Init(uint8_t lun); |
|
| 37 |
+ |
|
| 38 |
+int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, |
|
| 39 |
+ uint16_t *block_size); |
|
| 40 |
+ |
|
| 41 |
+int8_t STORAGE_IsReady(uint8_t lun); |
|
| 42 |
+ |
|
| 43 |
+int8_t STORAGE_IsWriteProtected(uint8_t lun); |
|
| 44 |
+ |
|
| 45 |
+int8_t STORAGE_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, |
|
| 46 |
+ uint16_t blk_len); |
|
| 47 |
+ |
|
| 48 |
+int8_t STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, |
|
| 49 |
+ uint16_t blk_len); |
|
| 50 |
+ |
|
| 51 |
+int8_t STORAGE_GetMaxLun(void); |
|
| 52 |
+ |
|
| 53 |
+/* USB Mass storage Standard Inquiry Data */ |
|
| 54 |
+int8_t STORAGE_Inquirydata[] = /* 36 */ |
|
| 55 |
+{
|
|
| 56 |
+ |
|
| 57 |
+ /* LUN 0 */ |
|
| 58 |
+ 0x00, |
|
| 59 |
+ 0x80, |
|
| 60 |
+ 0x02, |
|
| 61 |
+ 0x02, |
|
| 62 |
+ (STANDARD_INQUIRY_DATA_LEN - 5), |
|
| 63 |
+ 0x00, |
|
| 64 |
+ 0x00, |
|
| 65 |
+ 0x00, |
|
| 66 |
+ 'X', 'I', 'R', 'X', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */ |
|
| 67 |
+ 'X', 'i', 'P', 'C', ' ', 'S', 't', 'o', /* Product : 16 Bytes */ |
|
| 68 |
+ 'r', 'a', 'g', 'e', ' ', ' ', ' ', ' ', |
|
| 69 |
+ '1', '.', '0' ,' ', /* Version : 4 Bytes */ |
|
| 70 |
+}; |
|
| 71 |
+ |
|
| 72 |
+USBD_StorageTypeDef USBD_MSC_Fops = |
|
| 73 |
+{
|
|
| 74 |
+ STORAGE_Init, |
|
| 75 |
+ STORAGE_GetCapacity, |
|
| 76 |
+ STORAGE_IsReady, |
|
| 77 |
+ STORAGE_IsWriteProtected, |
|
| 78 |
+ STORAGE_Read, |
|
| 79 |
+ STORAGE_Write, |
|
| 80 |
+ STORAGE_GetMaxLun, |
|
| 81 |
+ STORAGE_Inquirydata, |
|
| 82 |
+ |
|
| 83 |
+}; |
|
| 84 |
+ |
|
| 85 |
+/** |
|
| 86 |
+ * @brief Initializes the storage unit (medium) |
|
| 87 |
+ * @param lun: Logical unit number |
|
| 88 |
+ * @retval Status (0 : OK / -1 : Error) |
|
| 89 |
+ */ |
|
| 90 |
+int8_t STORAGE_Init(uint8_t lun) |
|
| 91 |
+{
|
|
| 92 |
+ return(USBD_OK); |
|
| 93 |
+} |
|
| 94 |
+ |
|
| 95 |
+/** |
|
| 96 |
+ * @brief Returns the medium capacity. |
|
| 97 |
+ * @param lun: Logical unit number |
|
| 98 |
+ * @param block_num: Number of total block number |
|
| 99 |
+ * @param block_size: Block size |
|
| 100 |
+ * @retval Status (0: OK / -1: Error) |
|
| 101 |
+ */ |
|
| 102 |
+int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size) |
|
| 103 |
+{
|
|
| 104 |
+ UNUSED(lun); |
|
| 105 |
+ |
|
| 106 |
+ *block_num = STORAGE_BLK_NBR; |
|
| 107 |
+ *block_size = STORAGE_BLK_SIZ; |
|
| 108 |
+ |
|
| 109 |
+ return(USBD_OK); |
|
| 110 |
+} |
|
| 111 |
+ |
|
| 112 |
+ |
|
| 113 |
+/** |
|
| 114 |
+ * @brief Checks whether the medium is ready. |
|
| 115 |
+ * @param lun: Logical unit number |
|
| 116 |
+ * @retval Status (0: OK / -1: Error) |
|
| 117 |
+ */ |
|
| 118 |
+int8_t STORAGE_IsReady(uint8_t lun) |
|
| 119 |
+{
|
|
| 120 |
+ return(USBD_OK); |
|
| 121 |
+} |
|
| 122 |
+ |
|
| 123 |
+/** |
|
| 124 |
+ * @brief Checks whether the medium is write protected. |
|
| 125 |
+ * @param lun: Logical unit number |
|
| 126 |
+ * @retval Status (0: write enabled / -1: otherwise) |
|
| 127 |
+ */ |
|
| 128 |
+int8_t STORAGE_IsWriteProtected(uint8_t lun) |
|
| 129 |
+{
|
|
| 130 |
+ return(USBD_OK); |
|
| 131 |
+} |
|
| 132 |
+ |
|
| 133 |
+/** |
|
| 134 |
+ * @brief Reads data from the medium. |
|
| 135 |
+ * @param lun: Logical unit number |
|
| 136 |
+ * @param buf: data buffer |
|
| 137 |
+ * @param blk_addr: Logical block address |
|
| 138 |
+ * @param blk_len: Blocks number |
|
| 139 |
+ * @retval Status (0: OK / -1: Error) |
|
| 140 |
+ */ |
|
| 141 |
+int8_t STORAGE_Read(uint8_t lun, uint8_t *buf, |
|
| 142 |
+ uint32_t blk_addr, uint16_t blk_len) |
|
| 143 |
+{
|
|
| 144 |
+ for(uint32_t i = 0; i < blk_len; i++) |
|
| 145 |
+ {
|
|
| 146 |
+ if(!flashRead((blk_addr + i), (buf + (FLASH_SECTOR_SIZE * i)))) |
|
| 147 |
+ {
|
|
| 148 |
+ return(USBD_FAIL); |
|
| 149 |
+ } |
|
| 150 |
+ } |
|
| 151 |
+ |
|
| 152 |
+ return(USBD_OK); |
|
| 153 |
+} |
|
| 154 |
+ |
|
| 155 |
+/** |
|
| 156 |
+ * @brief Writes data into the medium. |
|
| 157 |
+ * @param lun: Logical unit number |
|
| 158 |
+ * @param buf: data buffer |
|
| 159 |
+ * @param blk_addr: Logical block address |
|
| 160 |
+ * @param blk_len: Blocks number |
|
| 161 |
+ * @retval Status (0 : OK / -1 : Error) |
|
| 162 |
+ */ |
|
| 163 |
+int8_t STORAGE_Write(uint8_t lun, uint8_t *buf, |
|
| 164 |
+ uint32_t blk_addr, uint16_t blk_len) |
|
| 165 |
+{
|
|
| 166 |
+ for(uint32_t i = 0; i < blk_len; i++) |
|
| 167 |
+ {
|
|
| 168 |
+ if(!flashWrite((blk_addr + i), (buf + (FLASH_SECTOR_SIZE * i)))) |
|
| 169 |
+ {
|
|
| 170 |
+ return(USBD_FAIL); |
|
| 171 |
+ } |
|
| 172 |
+ } |
|
| 173 |
+ |
|
| 174 |
+ return(USBD_OK); |
|
| 175 |
+} |
|
| 176 |
+ |
|
| 177 |
+/** |
|
| 178 |
+ * @brief Returns the Max Supported LUNs. |
|
| 179 |
+ * @param None |
|
| 180 |
+ * @retval Lun(s) number |
|
| 181 |
+ */ |
|
| 182 |
+int8_t STORAGE_GetMaxLun(void) |
|
| 183 |
+{
|
|
| 184 |
+ return(STORAGE_LUN_NBR - 1); |
|
| 185 |
+} |
|
| 186 |
+ |
|
| 187 |
+ |