From 1cdca167cd41f51ca587a265b41f79446d828e48 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 22 Mar 2014 22:50:08 +0700 Subject: rename dcd_11u_13u_qhd_t::total_bytes to nbytes to prevent confusion introduce scsi_data to mscd_interface_t to make tusbd_msc_scsi_cb buffer's address no longer to be required in USB ram section --> save usb ram for lpc11/13u --- tinyusb/class/hid_device.c | 131 --------------------------------------------- tinyusb/class/msc_device.c | 26 +++++---- tinyusb/class/msc_device.h | 6 +-- 3 files changed, 15 insertions(+), 148 deletions(-) (limited to 'tinyusb/class') diff --git a/tinyusb/class/hid_device.c b/tinyusb/class/hid_device.c index 1f266da21..91fe0f9d6 100644 --- a/tinyusb/class/hid_device.c +++ b/tinyusb/class/hid_device.c @@ -321,135 +321,4 @@ tusb_error_t hidd_xfer_cb(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32 return TUSB_ERROR_NONE; } -#if defined(CAP_DEVICE_ROMDRIVER) - -//--------------------------------------------------------------------+ -// MACRO CONSTANT TYPEDEF -//--------------------------------------------------------------------+ -#if TUSB_CFG_DEVICE_HID_KEYBOARD -TUSB_CFG_ATTR_USBRAM uint8_t hidd_keyboard_buffer[1024]; // TODO memory reduce -TUSB_CFG_ATTR_USBRAM hid_keyboard_report_t hid_keyboard_report; -static volatile bool bKeyChanged = false; -#endif - -#if TUSB_CFG_DEVICE_HID_MOUSE -TUSB_CFG_ATTR_USBRAM uint8_t hidd_mouse_buffer[1024]; // TODO memory reduce -TUSB_CFG_ATTR_USBRAM hid_mouse_report_t hid_mouse_report; -static volatile bool bMouseChanged = false; -#endif - -//--------------------------------------------------------------------+ -// APPLICATION API -//--------------------------------------------------------------------+ - -//--------------------------------------------------------------------+ -// IMPLEMENTATION -//--------------------------------------------------------------------+ -ErrorCode_t HID_GetReport( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t* plength) -{ - USB_HID_CTRL_T* pHidCtrl = (USB_HID_CTRL_T*) hHid; - - /* ReportID = SetupPacket.wValue.WB.L; */ - if (pSetup->wValue.WB.H == HID_REQUEST_REPORT_INPUT) - return (ERR_USBD_STALL); /* Not Supported */ - - switch (pHidCtrl->protocol) - { - #if TUSB_CFG_DEVICE_HID_KEYBOARD - case HID_PROTOCOL_KEYBOARD: - *pBuffer = (uint8_t*) &hid_keyboard_report; - *plength = sizeof(hid_keyboard_report_t); - - if (!bKeyChanged) - { - memset(pBuffer, 0, *plength); - } - bKeyChanged = false; - break; - #endif - - #if TUSB_CFG_DEVICE_HID_MOUSE - case HID_PROTOCOL_MOUSE: - *pBuffer = (uint8_t*) &hid_mouse_report; - *plength = sizeof(hid_mouse_report_t); - - if (!bMouseChanged) - { - memset(pBuffer, 0, *plength); - } - bMouseChanged = false; - break; - #endif - - default: - break; - } - - return (LPC_OK); -} - -ErrorCode_t HID_SetReport( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t length) -{ - /* we will reuse standard EP0Buf */ - if (length == 0) - return LPC_OK; - - /* ReportID = SetupPacket.wValue.WB.L; */ - if (pSetup->wValue.WB.H != HID_REQUEST_REPORT_OUTPUT) - return (ERR_USBD_STALL); /* Not Supported */ - - return (LPC_OK); -} - -ErrorCode_t HID_EpIn_Hdlr (USBD_HANDLE_T hUsb, void* data, uint32_t event) -{ - if (USB_EVT_IN == event) - { - USB_HID_CTRL_T* pHidCtrl = (USB_HID_CTRL_T*)data; - switch(pHidCtrl->protocol) - { - #if TUSB_CFG_DEVICE_HID_KEYBOARD - case HID_PROTOCOL_KEYBOARD: - if (!bKeyChanged) - { - memset(&hid_keyboard_report, 0, sizeof(hid_keyboard_report_t)); - } - ROM_API->hw->WriteEP(hUsb, pHidCtrl->epin_adr, (uint8_t*) &hid_keyboard_report, sizeof(hid_keyboard_report_t)); - bKeyChanged = false; - break; - #endif - - #if TUSB_CFG_DEVICE_HID_MOUSE - case HID_PROTOCOL_MOUSE: - if (!bMouseChanged) - { - memset(&hid_mouse_report, 0, sizeof(hid_mouse_report_t)); - } - ROM_API->hw->WriteEP(hUsb, pHidCtrl->epin_adr, (uint8_t*) &hid_mouse_report, sizeof(hid_mouse_report_t)); - bMouseChanged = false; - break; - #endif - - default: - break; - } - } - - return LPC_OK; -} - -ErrorCode_t HID_EpOut_Hdlr (USBD_HANDLE_T hUsb, void* data, uint32_t event) -{ - if (USB_EVT_OUT == event) - { - // not used yet - // uint8_t outreport[8]; - // USB_HID_CTRL_T* pHidCtrl = (USB_HID_CTRL_T*)data; - // ROM_API->hw->ReadEP(hUsb, pHidCtrl->epout_adr, outreport); - } - return LPC_OK; -} - -#endif - #endif diff --git a/tinyusb/class/msc_device.c b/tinyusb/class/msc_device.c index 5f66e64b2..99c082cf5 100644 --- a/tinyusb/class/msc_device.c +++ b/tinyusb/class/msc_device.c @@ -51,6 +51,7 @@ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ typedef struct { + uint8_t scsi_data[64]; // buffer for scsi's response other than read10 & write10. NOTE should be multiple of 64 to be compatible with lpc11/13u ATTR_USB_MIN_ALIGNMENT msc_cmd_block_wrapper_t cbw; #if defined (__ICCARM__) && (TUSB_CFG_MCU == MCU_LPC11UXX || TUSB_CFG_MCU == MCU_LPC13UXX) @@ -59,12 +60,7 @@ typedef struct { ATTR_USB_MIN_ALIGNMENT msc_cmd_status_wrapper_t csw; -#if defined (__ICCARM__) && (TUSB_CFG_MCU == MCU_LPC11UXX || TUSB_CFG_MCU == MCU_LPC13UXX) - uint8_t padding2[64-sizeof(msc_cmd_status_wrapper_t)]; // IAR cannot align struct's member -#endif - - ATTR_USB_MIN_ALIGNMENT uint8_t max_lun; // can STALL for one LUN - + uint8_t max_lun; uint8_t interface_number; endpoint_handle_t edpt_in, edpt_out; }mscd_interface_t; @@ -134,7 +130,8 @@ tusb_error_t mscd_control_request_subtask(uint8_t coreid, tusb_control_request_t break; case MSC_REQUEST_GET_MAX_LUN: - dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, &p_msc->max_lun, 1, false); + p_msc->scsi_data[0] = p_msc->max_lun; // Note: lpc11/13u need xfer data's address to be aligned 64 -> make use of scsi_data instead of using max_lun directly + dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, p_msc->scsi_data, 1, false); break; default: @@ -149,7 +146,6 @@ tusb_error_t mscd_control_request_subtask(uint8_t coreid, tusb_control_request_t //--------------------------------------------------------------------+ tusb_error_t mscd_xfer_cb(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes) { - // TODO failed --> STALL pipe, on clear STALL --> queue endpoint OUT static bool is_waiting_read10_write10 = false; // indicate we are transferring data in READ10, WRITE10 command mscd_interface_t * const p_msc = &mscd_data; @@ -172,15 +168,20 @@ tusb_error_t mscd_xfer_cb(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32 if ( (SCSI_CMD_READ_10 != p_cbw->command[0]) && (SCSI_CMD_WRITE_10 != p_cbw->command[0]) ) { - void *p_buffer = NULL; + void const *p_buffer = NULL; uint16_t actual_length = (uint16_t) p_cbw->xfer_bytes; + // TODO SCSI data out transfer is not yet supported + ASSERT_FALSE( p_cbw->xfer_bytes > 0 && !BIT_TEST_(p_cbw->dir, 7), TUSB_ERROR_NOT_SUPPORTED_YET); + p_csw->status = tusbd_msc_scsi_cb(edpt_hdl.coreid, p_cbw->lun, p_cbw->command, &p_buffer, &actual_length); //------------- Data Phase (non READ10, WRITE10) -------------// if ( p_cbw->xfer_bytes ) { ASSERT( p_cbw->xfer_bytes >= actual_length, TUSB_ERROR_INVALID_PARA ); + ASSERT( sizeof(p_msc->scsi_data) >= actual_length, TUSB_ERROR_NOT_ENOUGH_MEMORY); // needs to increase size for scsi_data + endpoint_handle_t const edpt_data = BIT_TEST_(p_cbw->dir, 7) ? p_msc->edpt_in : p_msc->edpt_out; if ( p_buffer == NULL || actual_length == 0 ) @@ -189,7 +190,8 @@ tusb_error_t mscd_xfer_cb(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32 p_csw->status = MSC_CSW_STATUS_FAILED; }else { - ASSERT_STATUS( dcd_pipe_queue_xfer( edpt_data, p_buffer, min16_of(actual_length, (uint16_t) p_cbw->xfer_bytes)) ); + memcpy(p_msc->scsi_data, p_buffer, actual_length); + ASSERT_STATUS( dcd_pipe_queue_xfer( edpt_data, p_msc->scsi_data, actual_length ) ); } } } @@ -198,10 +200,6 @@ tusb_error_t mscd_xfer_cb(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32 //------------- Data Phase For READ10 & WRITE10 (can be executed several times) -------------// if ( (SCSI_CMD_READ_10 == p_cbw->command[0]) || (SCSI_CMD_WRITE_10 == p_cbw->command[0]) ) { -// if (is_waiting_read10_write10) -// { // continue with read10, write10 data transfer, interrupt must come from endpoint IN -// ASSERT( endpointhandle_is_equal(p_msc->edpt_in, edpt_hdl) && event == TUSB_EVENT_XFER_COMPLETE, TUSB_ERROR_INVALID_PARA); -// } is_waiting_read10_write10 = !read10_write10_data_xfer(p_msc); } diff --git a/tinyusb/class/msc_device.h b/tinyusb/class/msc_device.h index eba3e0b92..a50e507f5 100644 --- a/tinyusb/class/msc_device.h +++ b/tinyusb/class/msc_device.h @@ -105,8 +105,8 @@ uint16_t tusbd_msc_write10_cb(uint8_t coreid, uint8_t lun, void** pp_buffer, uin * \param[in] coreid USB Controller ID * \param[in] lun Targeted Logical Unit * \param[in] scsi_cmd SCSI command contents, application should examine this command block to know which command host requested - * \param[out] pp_buffer Pointer to buffer which application need to update with the address to transceive data with host - * Must be accessible by USB controller (see \ref TUSB_CFG_ATTR_USBRAM) + * \param[out] pp_buffer Pointer to buffer which application need to update with the address to transfer data with host. + * The buffer address can be anywhere since the stack will copy its contents to a internal USB-accessible buffer. * \param[in] p_length length * \retval non-zero Actual number of block that application can receive and must be less than or equal to \a \b block_count. * \retval zero Indicate error in retrieving data from application. Tinyusb device stack will \b STALL the corresponding @@ -114,7 +114,7 @@ uint16_t tusbd_msc_write10_cb(uint8_t coreid, uint8_t lun, void** pp_buffer, uin * \note Although this callback is called by tinyusb device task (non-isr context), however as all the classes share * the same task (to save resource), any delay in this callback will cause delay in reponse on other classes. */ -msc_csw_status_t tusbd_msc_scsi_cb (uint8_t coreid, uint8_t lun, uint8_t scsi_cmd[16], void ** pp_buffer, uint16_t* p_length); +msc_csw_status_t tusbd_msc_scsi_cb (uint8_t coreid, uint8_t lun, uint8_t scsi_cmd[16], void const ** pp_buffer, uint16_t* p_length); /** @} */ /** @} */ -- cgit v1.3.1