39#define umass_dbg_msg(...)
41#define umass_dbg_msg printf
46static uint16_t bulk_in_ep_addr, bulk_out_ep_addr;
47static uint8_t bulk_in_toggle, bulk_out_toggle;
49int g_disk_lun, g_max_lun;
53#pragma data_alignment=4
54static struct bulk_cb_wrap g_cb;
56#pragma data_alignment=4
57static struct bulk_cs_wrap g_cs;
65static uint32_t g_total_sector_num, g_sector_size;
68int umas_bulk_xfer(uint16_t ep_addr, uint8_t *toggle,
69 uint8_t *data_buff,
int data_len,
int timeout)
73 for ( ; retry; retry--)
83static void get_max_lun()
97 umass_dbg_msg(
"Get Max Lun command failed!\n");
103 umass_dbg_msg(
"Max lun is %d\n", g_max_lun);
107static void umass_reset()
112 printf(
"Reset UMAS device...\n");
117 umass_dbg_msg(
"UAMSS reset request failed!\n");
125static int __tag = 0x10e24388;
127static int umass_run_command(uint8_t *buff,
int data_len,
int is_data_in,
int timeout)
131 g_cb.Signature = UMAS_BULK_CB_SIGN;
133 g_cb.DataTransferLength = data_len;
134 g_cb.Lun = g_disk_lun;
136 ret = umas_bulk_xfer(bulk_out_ep_addr, &bulk_out_toggle, (uint8_t *)&g_cb, 31, 10000);
143 ret = umas_bulk_xfer(bulk_in_ep_addr, &bulk_in_toggle, buff, data_len, timeout);
145 ret = umas_bulk_xfer(bulk_out_ep_addr, &bulk_out_toggle, buff, data_len, timeout);
151 ret = umas_bulk_xfer(bulk_in_ep_addr, &bulk_in_toggle, (uint8_t *)&g_cs, 13, 1000);
155 if (g_cs.Status != 0)
157 umass_dbg_msg(
"CSW status error.\n");
167static int umass_inquiry()
171 umass_dbg_msg(
"INQUIRY...\n");
173 memset((uint8_t *)&g_cb, 0,
sizeof(g_cb));
176 g_cb.CDB[0] = INQUIRY;
177 g_cb.CDB[1] = g_disk_lun << 5;
180 ret = umass_run_command(_transfer_buffer, 36, 1, 10000);
183 umass_dbg_msg(
"INQUIRY command failed. [%d]\n", ret);
188 umass_dbg_msg(
"INQUIRY command success.\n");
194static int umass_request_sense()
198 umass_dbg_msg(
"REQUEST_SENSE...\n");
200 memset((uint8_t *)&g_cb, 0,
sizeof(g_cb));
203 g_cb.CDB[0] = REQUEST_SENSE;
204 g_cb.CDB[1] = g_disk_lun << 5;
207 ret = umass_run_command(_transfer_buffer, 18, 1, 10000);
210 umass_dbg_msg(
"REQUEST_SENSE command failed.\n");
217 umass_dbg_msg(
"REQUEST_SENSE command success.\n");
218 if (_transfer_buffer[2] != 0x6)
220 umass_dbg_msg(
"Device is still not attention. 0x%x\n", _transfer_buffer[2]);
229static int umass_test_unit_ready()
233 umass_dbg_msg(
"TEST_UNIT_READY...\n");
235 memset((uint8_t *)&g_cb, 0,
sizeof(g_cb));
238 g_cb.CDB[0] = TEST_UNIT_READY;
239 g_cb.CDB[1] = g_disk_lun << 5;
241 ret = umass_run_command(
NULL, 0, 1, 10000);
250 umass_dbg_msg(
"TEST_UNIT_READY command success.\n");
256DRESULT usbh_umas_read(uint8_t *buff, uint32_t sector_no,
int number_of_sector)
262 memset((uint8_t *)&g_cb, 0,
sizeof(g_cb));
265 g_cb.CDB[0] = READ_10;
266 g_cb.CDB[1] = g_disk_lun << 5;
267 g_cb.CDB[2] = (sector_no >> 24) & 0xFF;
268 g_cb.CDB[3] = (sector_no >> 16) & 0xFF;
269 g_cb.CDB[4] = (sector_no >> 8) & 0xFF;
270 g_cb.CDB[5] = sector_no & 0xFF;
271 g_cb.CDB[7] = (number_of_sector >> 8) & 0xFF;
272 g_cb.CDB[8] = number_of_sector & 0xFF;
274 ret = umass_run_command(buff, number_of_sector * 512, 1, 3000);
277 umass_dbg_msg(
"usbh_umas_read failed!\n");
284DRESULT usbh_umas_write(uint8_t *buff, uint32_t sector_no,
int number_of_sector)
288 memset((uint8_t *)&g_cb, 0,
sizeof(g_cb));
291 g_cb.CDB[0] = WRITE_10;
292 g_cb.CDB[1] = g_disk_lun << 5;
293 g_cb.CDB[2] = (sector_no >> 24) & 0xFF;
294 g_cb.CDB[3] = (sector_no >> 16) & 0xFF;
295 g_cb.CDB[4] = (sector_no >> 8) & 0xFF;
296 g_cb.CDB[5] = sector_no & 0xFF;
297 g_cb.CDB[7] = (number_of_sector >> 8) & 0xFF;
298 g_cb.CDB[8] = number_of_sector & 0xFF;
300 ret = umass_run_command(buff, number_of_sector * 512, 0, 3000);
303 umass_dbg_msg(
"usbh_umas_write failed!\n");
310DRESULT usbh_umas_ioctl(
int cmd,
void *buff)
317 case GET_SECTOR_COUNT:
318 *(uint32_t *)buff = g_total_sector_num;
321 case GET_SECTOR_SIZE:
322 *(uint32_t *)buff = g_sector_size;
326 *(uint32_t *)buff = g_sector_size;
330 case CTRL_ERASE_SECTOR:
341int usbh_umas_disk_status(
void)
350static int umass_init_device(
void)
354 int8_t bHasMedia = 0;
356 for (g_disk_lun = 0; g_disk_lun <= g_max_lun; g_disk_lun++)
358 umass_dbg_msg(
"\n\n\n******* Read lun %d ******\n\n", g_disk_lun);
361 for (retries = 0; retries < 3; retries++)
378 for (i = 0; i < MINISEC_100; i++);
384 for (retries = 0; retries < 3; retries++)
386 umass_dbg_msg(
"READ CAPACITY ==>\n");
388 memset((uint8_t *)&g_cb, 0,
sizeof(g_cb));
391 g_cb.CDB[0] = READ_CAPACITY;
392 g_cb.CDB[1] = g_disk_lun << 5;
394 ret = umass_run_command(_transfer_buffer, 8, 1, 1000);
397 umass_dbg_msg(
"READ_CAPACITY failed!\n");
409 g_total_sector_num = (_transfer_buffer[0] << 24) | (_transfer_buffer[1] << 16) |
410 (_transfer_buffer[2] << 8) | _transfer_buffer[3];
411 g_sector_size = (_transfer_buffer[4] << 24) | (_transfer_buffer[5] << 16) |
412 (_transfer_buffer[6] << 8) | _transfer_buffer[7];
413 umass_dbg_msg(
"USB disk found: size=%d MB, uTotalSectorN=%d\n", g_total_sector_num / 2048, g_total_sector_num);
419 umass_dbg_msg(
"g_disk_lun = %d\n", g_disk_lun);
449 umass_dbg_msg(
"GET Config Descriptor command failed!!\n");
458 bulk_out_ep_addr = 0;
461 tlen = cfg_desc->wTotalLength;
462 bptr = (uint8_t *)cfg_desc;
468 case USB_DT_INTERFACE:
470 if ((if_desc->bInterfaceClass == USB_CLASS_MASS_STORAGE) &&
471 ((if_desc->bInterfaceSubClass == UMAS_SC_SCSI) || (if_desc->bInterfaceSubClass == UMAS_SC_8070)) &&
472 (if_desc->bInterfaceProtocol == UMAS_PR_BULK))
473 iface_num = if_desc->bInterfaceNumber;
478 bulk_out_ep_addr = 0;
482 case USB_DT_ENDPOINT:
484 if ((iface_num != -1) && ((ep_desc->bmAttributes & 0x3) == 0x2))
486 if (ep_desc->bEndpointAddress & 0x80)
487 bulk_in_ep_addr = ep_desc->bEndpointAddress;
489 bulk_out_ep_addr = ep_desc->bEndpointAddress;
499 if ((iface_num == -1) || (bulk_in_ep_addr == 0) || (bulk_out_ep_addr == 0))
505 umass_dbg_msg(
"USB Mass Storage device found.\n");
514 return umass_init_device();
void *__dso_handle __attribute__((weak))
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
#define USBH_RET_UNSUPPORT
#define USBH_RET_ERR_DEV_INIT
#define USBH_RET_DEV_CONN_KEEP
#define USBH_RET_ERR_CLASS_CMD
int usbh_probe_port(uint32_t port)
Probe USB root-hub port connect/disconnect status. A newly connected device will be initialized in th...
int usbh_set_configuration(int conf_val)
Issue a standard request SET_CONFIGURATION to USB device.
int usbh_drv_ctrl_req(uint8_t requesttype, uint8_t request, uint16_t value, uint16_t index, uint16_t length, int data_len, uint8_t *buffer, int dir)
Execute a control transfer request.
int usbh_probe_umass(void)
Try to probe and initialize an USB mass storage device.
int usbh_clear_halt(uint16_t ep_addr)
Issue a standard request SET_FEATURE to clear USB device endpoint halt state.
int usbh_drv_bulk_xfer(uint16_t ep_addr, uint8_t *toggle, uint8_t *data_buff, int data_len, int timeout)
Execute a control transfer request.
int get_config_descriptor(uint8_t *desc_buff)
Get configuration descriptor from the USB device.
#define NULL
NULL pointer.