mirror of
https://github.com/hathach/tinyusb.git
synced 2025-03-30 04:20:26 +00:00
implement msc device class
usbd auto stall control for not supported return from class control request usbd implement xfer isr callback mechanism DCD - implement dcd multiple qtd support - dcd dcd_pipe_stall - implement dcd_pipe_queue_xfer - xfer_complete_isr - flush control endpoint if received new setup while previous transfer is not complete change msc_cmd_block_wrapper_t flags field to dir force full speed for easy testing NOTEs: somehow unable to get endpoint IN interrupt with ioc
This commit is contained in:
parent
c760c69d51
commit
3a54ad4c0d
95
demos/device/keyboard/mscd_app.c
Normal file
95
demos/device/keyboard/mscd_app.c
Normal file
@ -0,0 +1,95 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file mscd_app.c
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2013, hathach (tinyusb.org)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holders nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
This file is part of the tinyusb stack.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#include "mscd_app.h"
|
||||
|
||||
#if TUSB_CFG_DEVICE_MSC
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
static scsi_inquiry_data_t mscd_inquiry_data TUSB_CFG_ATTR_USBRAM =
|
||||
{
|
||||
.is_removable = 1,
|
||||
.version = 2,
|
||||
.response_data_format = 2,
|
||||
.vendor_id = "tinyusb",
|
||||
.product_id = "MSC Example",
|
||||
.product_revision = "0.01"
|
||||
} ;
|
||||
|
||||
static scsi_read_capacity10_data_t mscd_read_capacity10_data TUSB_CFG_ATTR_USBRAM =
|
||||
{
|
||||
.last_lba = (16*1024*1024)/512,
|
||||
.block_size = 512
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// tinyusb callback (ISR context)
|
||||
//--------------------------------------------------------------------+
|
||||
msc_csw_status_t tusbd_msc_scsi_received_isr(uint8_t coreid, uint8_t lun, uint8_t scsi_cmd[16], void ** pp_buffer, uint16_t expected_length)
|
||||
{
|
||||
switch(scsi_cmd[0])
|
||||
{
|
||||
case SCSI_CMD_INQUIRY:
|
||||
(*pp_buffer) = &mscd_inquiry_data;
|
||||
break;
|
||||
|
||||
case SCSI_CMD_READ_CAPACITY_10:
|
||||
|
||||
break;
|
||||
|
||||
default: return MSC_CSW_STATUS_FAILED;
|
||||
}
|
||||
|
||||
return MSC_CSW_STATUS_PASSED;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// IMPLEMENTATION
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
|
||||
#endif
|
66
demos/device/keyboard/mscd_app.h
Normal file
66
demos/device/keyboard/mscd_app.h
Normal file
@ -0,0 +1,66 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file mscd_app.h
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2013, hathach (tinyusb.org)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holders nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
This file is part of the tinyusb stack.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
/** \ingroup TBD
|
||||
* \defgroup TBD
|
||||
* \brief TBD
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_MSCD_APP_H_
|
||||
#define _TUSB_MSCD_APP_H_
|
||||
|
||||
#include "boards/board.h"
|
||||
#include "tusb.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if TUSB_CFG_DEVICE_MSC
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_MSCD_APP_H_ */
|
||||
|
||||
/** @} */
|
@ -81,6 +81,8 @@
|
||||
//--------------------------------------------------------------------+
|
||||
// DEVICE CONFIGURATION
|
||||
//--------------------------------------------------------------------+
|
||||
#define TUSB_CFG_DEVICE_FULLSPEED 1 // TODO refractor
|
||||
|
||||
#define TUSB_CFG_DEVICE_USE_ROM_DRIVER 0
|
||||
|
||||
//------------- descriptors -------------//
|
||||
@ -90,13 +92,13 @@
|
||||
#define TUSB_CFG_DEVICE_VENDORID 0x1FC9 // NXP
|
||||
//#define TUSB_CFG_DEVICE_PRODUCTID 0x4567
|
||||
|
||||
#define TUSB_CFG_DEVICE_CONTROL_PACKET_SIZE 64
|
||||
#define TUSB_CFG_DEVICE_CONTROL_ENDOINT_SIZE 64
|
||||
|
||||
//------------- CLASS -------------//
|
||||
#define TUSB_CFG_DEVICE_HID_KEYBOARD 1
|
||||
#define TUSB_CFG_DEVICE_HID_MOUSE 1
|
||||
#define TUSB_CFG_DEVICE_HID_KEYBOARD 0
|
||||
#define TUSB_CFG_DEVICE_HID_MOUSE 0
|
||||
#define TUSB_CFG_DEVICE_HID_GENERIC 0
|
||||
#define TUSB_CFG_DEVICE_MSC 0
|
||||
#define TUSB_CFG_DEVICE_MSC 1
|
||||
#define TUSB_CFG_DEVICE_CDC 0
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
|
@ -138,7 +138,7 @@ tusb_descriptor_device_t app_tusb_desc_device =
|
||||
.bDeviceSubClass = 0x00,
|
||||
.bDeviceProtocol = 0x00,
|
||||
|
||||
.bMaxPacketSize0 = TUSB_CFG_DEVICE_CONTROL_PACKET_SIZE,
|
||||
.bMaxPacketSize0 = TUSB_CFG_DEVICE_CONTROL_ENDOINT_SIZE,
|
||||
|
||||
.idVendor = TUSB_CFG_DEVICE_VENDORID,
|
||||
.idProduct = TUSB_CFG_PRODUCT_ID,
|
||||
@ -356,7 +356,7 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
|
||||
{
|
||||
.bLength = sizeof(tusb_descriptor_interface_t),
|
||||
.bDescriptorType = TUSB_DESC_TYPE_INTERFACE,
|
||||
.bInterfaceNumber = 3,
|
||||
.bInterfaceNumber = INTERFACE_INDEX_MASS_STORAGE,
|
||||
.bAlternateSetting = 0x00,
|
||||
.bNumEndpoints = 2,
|
||||
.bInterfaceClass = TUSB_CLASS_MSC,
|
||||
@ -371,7 +371,7 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
|
||||
.bDescriptorType = TUSB_DESC_TYPE_ENDPOINT,
|
||||
.bEndpointAddress = 0x83,
|
||||
.bmAttributes = { .xfer = TUSB_XFER_BULK },
|
||||
.wMaxPacketSize = 512,
|
||||
.wMaxPacketSize = { .size = 64 /*512*/ },
|
||||
.bInterval = 1
|
||||
},
|
||||
|
||||
@ -381,7 +381,7 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
|
||||
.bDescriptorType = TUSB_DESC_TYPE_ENDPOINT,
|
||||
.bEndpointAddress = 0x03,
|
||||
.bmAttributes = { .xfer = TUSB_XFER_BULK },
|
||||
.wMaxPacketSize = 512,
|
||||
.wMaxPacketSize = { .size = 64 /*512*/ },
|
||||
.bInterval = 1
|
||||
},
|
||||
#endif
|
||||
|
@ -39,7 +39,7 @@
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
#include "mouse_app.h"
|
||||
#include "msc_app.h"
|
||||
|
||||
#if TUSB_CFG_OS != TUSB_OS_NONE
|
||||
#include "app_os_prio.h"
|
||||
|
@ -46,9 +46,6 @@
|
||||
#ifndef _TUSB_MSC_APP_H_
|
||||
#define _TUSB_MSC_APP_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "boards/board.h"
|
||||
#include "tusb.h"
|
||||
|
||||
|
@ -138,13 +138,10 @@ tusb_error_t hidd_control_request(uint8_t coreid, tusb_control_request_t const *
|
||||
uint8_t const desc_type = u16_high_u8(p_request->wValue);
|
||||
uint8_t const desc_index = u16_low_u8 (p_request->wValue);
|
||||
|
||||
if ( p_request->bRequest == TUSB_REQUEST_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT)
|
||||
{
|
||||
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, p_hid->p_report_desc, p_hid->report_length);
|
||||
}else
|
||||
{
|
||||
dcd_pipe_control_stall(coreid);
|
||||
}
|
||||
ASSERT ( p_request->bRequest == TUSB_REQUEST_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT,
|
||||
TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT);
|
||||
|
||||
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, p_hid->p_report_desc, p_hid->report_length);
|
||||
}
|
||||
//------------- Class Specific Request -------------//
|
||||
else if (p_request->bmRequestType_bit.type == TUSB_REQUEST_TYPE_CLASS)
|
||||
@ -170,12 +167,11 @@ tusb_error_t hidd_control_request(uint8_t coreid, tusb_control_request_t const *
|
||||
case HID_REQUEST_CONTROL_GET_PROTOCOL:
|
||||
case HID_REQUEST_CONTROL_SET_PROTOCOL:
|
||||
default:
|
||||
dcd_pipe_control_stall(coreid);
|
||||
return TUSB_ERROR_NOT_SUPPORTED_YET;
|
||||
return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
|
||||
}
|
||||
}else
|
||||
{
|
||||
dcd_pipe_control_stall(coreid);
|
||||
return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
|
||||
}
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
@ -215,7 +211,7 @@ tusb_error_t hidd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_int
|
||||
|
||||
p_hid->interface_number = p_interface_desc->bInterfaceNumber;
|
||||
p_hid->report_length = p_desc_hid->wReportLength;
|
||||
p_hid->ept_handle = dcd_pipe_open(coreid, p_desc_endpoint);
|
||||
p_hid->ept_handle = dcd_pipe_open(coreid, p_desc_endpoint, p_interface_desc->bInterfaceClass);
|
||||
ASSERT( endpointhandle_is_valid(p_hid->ept_handle), TUSB_ERROR_DCD_FAILED);
|
||||
}
|
||||
break;
|
||||
@ -233,6 +229,7 @@ tusb_error_t hidd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_int
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
void hidd_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes);
|
||||
|
||||
#if defined(CAP_DEVICE_ROMDRIVER) && TUSB_CFG_DEVICE_USE_ROM_DRIVER
|
||||
#include "device/dcd_nxp_romdriver.h" // TODO remove rom driver dependency
|
||||
|
@ -135,6 +135,7 @@ void tusbd_hid_mouse_isr(uint8_t coreid, tusb_event_t event);
|
||||
|
||||
tusb_error_t hidd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_interface_desc, uint16_t *p_length);
|
||||
tusb_error_t hidd_control_request(uint8_t coreid, tusb_control_request_t const * p_request);
|
||||
void hidd_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes);
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -101,7 +101,7 @@ typedef ATTR_PACKED_STRUCT(struct) {
|
||||
uint32_t signature ; ///< Signature that helps identify this data packet as a CBW. The signature field shall contain the value 43425355h (little endian), indicating a CBW.
|
||||
uint32_t tag ; ///< Tag sent by the host. The device shall echo the contents of this field back to the host in the dCSWTagfield of the associated CSW. The dCSWTagpositively associates a CSW with the corresponding CBW.
|
||||
uint32_t xfer_bytes ; ///< The number of bytes of data that the host expects to transfer on the Bulk-In or Bulk-Out endpoint (as indicated by the Directionbit) during the execution of this command. If this field is zero, the device and the host shall transfer no data between the CBW and the associated CSW, and the device shall ignore the value of the Directionbit in bmCBWFlags.
|
||||
uint8_t flags ; ///< Bit 7 of this field define transfer direction \n - 0 : Data-Out from host to the device. \n - 1 : Data-In from the device to the host.
|
||||
uint8_t dir ; ///< Bit 7 of this field define transfer direction \n - 0 : Data-Out from host to the device. \n - 1 : Data-In from the device to the host.
|
||||
uint8_t lun ; ///< The device Logical Unit Number (LUN) to which the command block is being sent. For devices that support multiple LUNs, the host shall place into this field the LUN to which this command block is addressed. Otherwise, the host shall set this field to zero.
|
||||
uint8_t cmd_len ; ///< The valid length of the CBWCBin bytes. This defines the valid length of the command block. The only legal values are 1 through 16
|
||||
uint8_t command[16] ; ///< The command block to be executed by the device. The device shall interpret the first cmd_len bytes in this field as a command block
|
||||
|
173
tinyusb/class/msc_device.c
Normal file
173
tinyusb/class/msc_device.c
Normal file
@ -0,0 +1,173 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file msc_device.c
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2013, hathach (tinyusb.org)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holders nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
This file is part of the tinyusb stack.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if (MODE_DEVICE_SUPPORTED && TUSB_CFG_DEVICE_MSC)
|
||||
|
||||
#define _TINY_USB_SOURCE_FILE_
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
#include "common/common.h"
|
||||
#include "msc_device.h"
|
||||
#include "tusb_descriptors.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct {
|
||||
uint8_t interface_number;
|
||||
endpoint_handle_t edpt_in, edpt_out;
|
||||
|
||||
// must be in USB ram
|
||||
uint8_t max_lun;
|
||||
msc_cmd_block_wrapper_t cbw;
|
||||
msc_cmd_status_wrapper_t csw;
|
||||
}mscd_interface_t;
|
||||
|
||||
STATIC_VAR mscd_interface_t mscd_data TUSB_CFG_ATTR_USBRAM;
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD-CLASS API
|
||||
//--------------------------------------------------------------------+
|
||||
tusb_error_t mscd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_interface_desc, uint16_t *p_length)
|
||||
{
|
||||
ASSERT( ( MSC_SUBCLASS_SCSI == p_interface_desc->bInterfaceSubClass &&
|
||||
MSC_PROTOCOL_BOT == p_interface_desc->bInterfaceProtocol ), TUSB_ERROR_MSC_UNSUPPORTED_PROTOCOL );
|
||||
|
||||
mscd_interface_t * p_msc = &mscd_data;
|
||||
|
||||
//------------- Open Data Pipe -------------//
|
||||
tusb_descriptor_endpoint_t const *p_endpoint = (tusb_descriptor_endpoint_t const *) descriptor_next( (uint8_t const*) p_interface_desc );
|
||||
for(uint32_t i=0; i<2; i++)
|
||||
{
|
||||
ASSERT(TUSB_DESC_TYPE_ENDPOINT == p_endpoint->bDescriptorType &&
|
||||
TUSB_XFER_BULK == p_endpoint->bmAttributes.xfer, TUSB_ERROR_DESCRIPTOR_CORRUPTED);
|
||||
|
||||
endpoint_handle_t * p_edpt_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_DEV_TO_HOST_MASK ) ?
|
||||
&p_msc->edpt_in : &p_msc->edpt_out;
|
||||
|
||||
(*p_edpt_hdl) = dcd_pipe_open(coreid, p_endpoint, p_interface_desc->bInterfaceClass);
|
||||
ASSERT( endpointhandle_is_valid(*p_edpt_hdl), TUSB_ERROR_DCD_FAILED);
|
||||
|
||||
p_endpoint = (tusb_descriptor_endpoint_t const *) descriptor_next( (uint8_t const*) p_endpoint );
|
||||
}
|
||||
|
||||
p_msc->interface_number = p_interface_desc->bInterfaceNumber;
|
||||
|
||||
(*p_length) += sizeof(tusb_descriptor_interface_t) + 2*sizeof(tusb_descriptor_endpoint_t);
|
||||
|
||||
//------------- Queue Endpoint OUT for Command Block Wrapper -------------//
|
||||
ASSERT_STATUS( dcd_pipe_xfer(p_msc->edpt_out, &p_msc->cbw, sizeof(msc_cmd_block_wrapper_t), true) );
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
tusb_error_t mscd_control_request(uint8_t coreid, tusb_control_request_t const * p_request)
|
||||
{
|
||||
ASSERT(p_request->bmRequestType_bit.type == TUSB_REQUEST_TYPE_CLASS, TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT);
|
||||
|
||||
mscd_interface_t * p_msc = &mscd_data;
|
||||
|
||||
switch(p_request->bRequest)
|
||||
{
|
||||
case MSC_REQUEST_RESET:
|
||||
dcd_pipe_control_xfer(coreid, TUSB_DIR_HOST_TO_DEV, NULL, 0);
|
||||
break;
|
||||
|
||||
case MSC_REQUEST_GET_MAX_LUN:
|
||||
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, &p_msc->max_lun, 1);
|
||||
break;
|
||||
|
||||
default:
|
||||
return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
|
||||
}
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
void mscd_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes)
|
||||
{
|
||||
// TODO failed --> STALL pipe, on clear STALL --> queue endpoint OUT
|
||||
mscd_interface_t * p_msc = &mscd_data;
|
||||
|
||||
if ( endpointhandle_is_equal(p_msc->edpt_in, edpt_hdl) )
|
||||
{
|
||||
return; // currently no need to handle bulk in
|
||||
}
|
||||
|
||||
ASSERT( endpointhandle_is_equal(p_msc->edpt_out, edpt_hdl) &&
|
||||
xferred_bytes == sizeof(msc_cmd_block_wrapper_t) &&
|
||||
event == TUSB_EVENT_XFER_COMPLETE &&
|
||||
p_msc->cbw.signature == MSC_CBW_SIGNATURE, VOID_RETURN );
|
||||
|
||||
void *p_buffer = NULL;
|
||||
|
||||
p_msc->csw.signature = MSC_CSW_SIGNATURE;
|
||||
p_msc->csw.tag = p_msc->cbw.tag;
|
||||
|
||||
p_msc->csw.status = tusbd_msc_scsi_received_isr(edpt_hdl.coreid, p_msc->cbw.lun, p_msc->cbw.command, &p_buffer, p_msc->cbw.xfer_bytes);
|
||||
p_msc->csw.data_residue = 0; // TODO expected length, response length
|
||||
|
||||
//------------- Data Phase -------------//
|
||||
if ( (p_msc->cbw.dir & BIT_(7)) && p_buffer == NULL )
|
||||
{ // application does not provide data to response --> possibly unsupported SCSI command
|
||||
ASSERT( TUSB_ERROR_NONE == dcd_pipe_stall(p_msc->edpt_in), VOID_RETURN );
|
||||
}
|
||||
|
||||
if ( p_msc->cbw.dir & BIT_(7) )
|
||||
{
|
||||
ASSERT( dcd_pipe_queue_xfer(p_msc->edpt_in, p_buffer, p_msc->cbw.xfer_bytes) == TUSB_ERROR_NONE, VOID_RETURN);
|
||||
} else
|
||||
{
|
||||
ASSERT( dcd_pipe_queue_xfer(p_msc->edpt_out, p_buffer, p_msc->cbw.xfer_bytes) == TUSB_ERROR_NONE, VOID_RETURN);
|
||||
}
|
||||
|
||||
//------------- Status Phase -------------//
|
||||
ASSERT( dcd_pipe_xfer( p_msc->edpt_in , &p_msc->csw, sizeof(msc_cmd_status_wrapper_t), true) == TUSB_ERROR_NONE, VOID_RETURN );
|
||||
|
||||
//------------- Queue the next CBW -------------//
|
||||
// ASSERT( dcd_pipe_xfer( p_msc->edpt_out, &p_msc->cbw, sizeof(msc_cmd_block_wrapper_t), true) == TUSB_ERROR_NONE, VOID_RETURN );
|
||||
|
||||
}
|
||||
|
||||
#endif
|
72
tinyusb/class/msc_device.h
Normal file
72
tinyusb/class/msc_device.h
Normal file
@ -0,0 +1,72 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file msc_device.h
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2013, hathach (tinyusb.org)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holders nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
This file is part of the tinyusb stack.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef _TUSB_MSC_DEVICE_H_
|
||||
#define _TUSB_MSC_DEVICE_H_
|
||||
|
||||
#include "common/common.h"
|
||||
#include "device/usbd.h"
|
||||
#include "msc.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION API
|
||||
//--------------------------------------------------------------------+
|
||||
msc_csw_status_t tusbd_msc_scsi_received_isr(uint8_t coreid, uint8_t lun, uint8_t scsi_cmd[16], void ** pp_buffer, uint16_t expected_length);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD-CLASS DRIVER API
|
||||
//--------------------------------------------------------------------+
|
||||
#ifdef _TINY_USB_SOURCE_FILE_
|
||||
|
||||
tusb_error_t mscd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_interface_desc, uint16_t *p_length);
|
||||
tusb_error_t mscd_control_request(uint8_t coreid, tusb_control_request_t const * p_request);
|
||||
void mscd_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_MSC_DEVICE_H_ */
|
||||
|
||||
/** @} */
|
@ -58,7 +58,7 @@ STATIC_VAR msch_interface_t msch_data[TUSB_CFG_HOST_DEVICE_MAX] TUSB_CFG_ATTR_US
|
||||
OSAL_SEM_DEF(msch_semaphore);
|
||||
static osal_semaphore_handle_t msch_sem_hdl;
|
||||
|
||||
// buffer used to read scsi information when mounted, largest reponse data currently is inquiry
|
||||
// buffer used to read scsi information when mounted, largest response data currently is inquiry
|
||||
ATTR_ALIGNED(4) STATIC_VAR uint8_t msch_buffer[sizeof(scsi_inquiry_data_t)] TUSB_CFG_ATTR_USBRAM;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@ -125,7 +125,7 @@ static tusb_error_t msch_command_xfer(msch_interface_t * p_msch, void* p_buffer)
|
||||
{
|
||||
if ( NULL != p_buffer)
|
||||
{ // there is data phase
|
||||
if (p_msch->cbw.flags & TUSB_DIR_DEV_TO_HOST_MASK)
|
||||
if (p_msch->cbw.dir & TUSB_DIR_DEV_TO_HOST_MASK)
|
||||
{
|
||||
ASSERT_STATUS( hcd_pipe_xfer(p_msch->bulk_out, (uint8_t*) &p_msch->cbw, sizeof(msc_cmd_block_wrapper_t), false) );
|
||||
ASSERT_STATUS( hcd_pipe_queue_xfer(p_msch->bulk_in , p_buffer, p_msch->cbw.xfer_bytes) );
|
||||
@ -148,7 +148,7 @@ tusb_error_t tusbh_msc_inquiry(uint8_t dev_addr, uint8_t lun, uint8_t *p_data)
|
||||
//------------- Command Block Wrapper -------------//
|
||||
msc_cbw_add_signature(&p_msch->cbw, lun);
|
||||
p_msch->cbw.xfer_bytes = sizeof(scsi_inquiry_data_t);
|
||||
p_msch->cbw.flags = TUSB_DIR_DEV_TO_HOST_MASK;
|
||||
p_msch->cbw.dir = TUSB_DIR_DEV_TO_HOST_MASK;
|
||||
p_msch->cbw.cmd_len = sizeof(scsi_inquiry_t);
|
||||
|
||||
//------------- SCSI command -------------//
|
||||
@ -172,7 +172,7 @@ tusb_error_t tusbh_msc_read_capacity10(uint8_t dev_addr, uint8_t lun, uint8_t *p
|
||||
//------------- Command Block Wrapper -------------//
|
||||
msc_cbw_add_signature(&p_msch->cbw, lun);
|
||||
p_msch->cbw.xfer_bytes = sizeof(scsi_read_capacity10_data_t);
|
||||
p_msch->cbw.flags = TUSB_DIR_DEV_TO_HOST_MASK;
|
||||
p_msch->cbw.dir = TUSB_DIR_DEV_TO_HOST_MASK;
|
||||
p_msch->cbw.cmd_len = sizeof(scsi_read_capacity10_t);
|
||||
|
||||
//------------- SCSI command -------------//
|
||||
@ -196,7 +196,7 @@ tusb_error_t tusbh_msc_request_sense(uint8_t dev_addr, uint8_t lun, uint8_t *p_d
|
||||
|
||||
//------------- Command Block Wrapper -------------//
|
||||
p_msch->cbw.xfer_bytes = 18;
|
||||
p_msch->cbw.flags = TUSB_DIR_DEV_TO_HOST_MASK;
|
||||
p_msch->cbw.dir = TUSB_DIR_DEV_TO_HOST_MASK;
|
||||
p_msch->cbw.cmd_len = sizeof(scsi_request_sense_t);
|
||||
|
||||
//------------- SCSI command -------------//
|
||||
@ -221,7 +221,7 @@ tusb_error_t tusbh_msc_test_unit_ready(uint8_t dev_addr, uint8_t lun, msc_cmd_s
|
||||
msc_cbw_add_signature(&p_msch->cbw, lun);
|
||||
|
||||
p_msch->cbw.xfer_bytes = 0; // Number of bytes
|
||||
p_msch->cbw.flags = TUSB_DIR_HOST_TO_DEV;
|
||||
p_msch->cbw.dir = TUSB_DIR_HOST_TO_DEV;
|
||||
p_msch->cbw.cmd_len = sizeof(scsi_test_unit_ready_t);
|
||||
|
||||
//------------- SCSI command -------------//
|
||||
@ -248,7 +248,7 @@ tusb_error_t tusbh_msc_read10(uint8_t dev_addr, uint8_t lun, void * p_buffer, u
|
||||
msc_cbw_add_signature(&p_msch->cbw, lun);
|
||||
|
||||
p_msch->cbw.xfer_bytes = p_msch->block_size*block_count; // Number of bytes
|
||||
p_msch->cbw.flags = TUSB_DIR_DEV_TO_HOST_MASK;
|
||||
p_msch->cbw.dir = TUSB_DIR_DEV_TO_HOST_MASK;
|
||||
p_msch->cbw.cmd_len = sizeof(scsi_read10_t);
|
||||
|
||||
//------------- SCSI command -------------//
|
||||
@ -274,7 +274,7 @@ tusb_error_t tusbh_msc_write10(uint8_t dev_addr, uint8_t lun, void const * p_buf
|
||||
msc_cbw_add_signature(&p_msch->cbw, lun);
|
||||
|
||||
p_msch->cbw.xfer_bytes = p_msch->block_size*block_count; // Number of bytes
|
||||
p_msch->cbw.flags = TUSB_DIR_HOST_TO_DEV;
|
||||
p_msch->cbw.dir = TUSB_DIR_HOST_TO_DEV;
|
||||
p_msch->cbw.cmd_len = sizeof(scsi_write10_t);
|
||||
|
||||
//------------- SCSI command -------------//
|
||||
@ -310,7 +310,7 @@ tusb_error_t msch_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t con
|
||||
if (! ( MSC_SUBCLASS_SCSI == p_interface_desc->bInterfaceSubClass &&
|
||||
MSC_PROTOCOL_BOT == p_interface_desc->bInterfaceProtocol ) )
|
||||
{
|
||||
return TUSB_ERROR_MSCH_UNSUPPORTED_PROTOCOL;
|
||||
return TUSB_ERROR_MSC_UNSUPPORTED_PROTOCOL;
|
||||
}
|
||||
|
||||
//------------- Open Data Pipe -------------//
|
||||
|
@ -51,6 +51,14 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACROS
|
||||
//--------------------------------------------------------------------+
|
||||
#define STRING_(x) #x // stringify without expand
|
||||
#define XSTRING_(x) STRING_(x) // expand then stringify
|
||||
#define STRING_CONCAT_(a, b) a##b // concat without expand
|
||||
#define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDES
|
||||
//--------------------------------------------------------------------+
|
||||
@ -75,6 +83,7 @@
|
||||
#include "std_descriptors.h"
|
||||
#include "std_request.h"
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACROS
|
||||
//--------------------------------------------------------------------+
|
||||
|
@ -86,11 +86,14 @@
|
||||
ENTRY(TUSB_ERROR_CDCH_UNSUPPORTED_SUBCLASS )\
|
||||
ENTRY(TUSB_ERROR_CDCH_UNSUPPORTED_PROTOCOL )\
|
||||
ENTRY(TUSB_ERROR_CDCH_DEVICE_NOT_MOUNTED )\
|
||||
ENTRY(TUSB_ERROR_MSCH_UNSUPPORTED_PROTOCOL )\
|
||||
ENTRY(TUSB_ERROR_MSC_UNSUPPORTED_PROTOCOL )\
|
||||
ENTRY(TUSB_ERROR_MSCH_UNKNOWN_SCSI_COMMAND )\
|
||||
ENTRY(TUSB_ERROR_MSCH_DEVICE_NOT_MOUNTED )\
|
||||
ENTRY(TUSB_ERROR_HUB_FEATURE_NOT_SUPPORTED )\
|
||||
ENTRY(TUSB_ERROR_DESCRIPTOR_CORRUPTED )\
|
||||
ENTRY(TUSB_ERROR_DCD_FAILED )\
|
||||
ENTRY(TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT )\
|
||||
ENTRY(TUSB_ERROR_DCD_NOT_ENOUGH_QTD )\
|
||||
ENTRY(TUSB_ERROR_NOT_SUPPORTED_YET )\
|
||||
ENTRY(TUSB_ERROR_FAILED )\
|
||||
|
||||
|
@ -36,12 +36,6 @@
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
/** \file
|
||||
* \brief TBD
|
||||
*
|
||||
* \note TBD
|
||||
*/
|
||||
|
||||
/** \ingroup TBD
|
||||
* \defgroup TBD
|
||||
* \brief TBD
|
||||
@ -72,7 +66,7 @@ typedef ATTR_PACKED_STRUCT(struct){
|
||||
uint16_t wLength;
|
||||
} tusb_control_request_t;
|
||||
|
||||
//STATIC_ASSERT(sizeof(tusb_control_request_t) == 8, "mostly compiler option issue");
|
||||
STATIC_ASSERT( sizeof(tusb_control_request_t) == 8, "mostly compiler option issue");
|
||||
|
||||
// TODO move to somewhere suitable
|
||||
static inline uint8_t bm_request_type(uint8_t direction, uint8_t type, uint8_t recipient) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
|
@ -210,6 +210,18 @@ enum {
|
||||
DESCRIPTOR_OFFSET_TYPE = 1
|
||||
};
|
||||
|
||||
static inline uint8_t std_class_code_to_index(uint8_t std_class_code) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t std_class_code_to_index(uint8_t std_class_code)
|
||||
{
|
||||
return (std_class_code <= TUSB_CLASS_AUDIO_VIDEO ) ? std_class_code :
|
||||
(std_class_code == TUSB_CLASS_DIAGNOSTIC ) ? TUSB_CLASS_MAPPED_INDEX_START :
|
||||
(std_class_code == TUSB_CLASS_WIRELESS_CONTROLLER ) ? TUSB_CLASS_MAPPED_INDEX_START + 1 :
|
||||
(std_class_code == TUSB_CLASS_MISC ) ? TUSB_CLASS_MAPPED_INDEX_START + 2 :
|
||||
(std_class_code == TUSB_CLASS_APPLICATION_SPECIFIC ) ? TUSB_CLASS_MAPPED_INDEX_START + 3 :
|
||||
(std_class_code == TUSB_CLASS_VENDOR_SPECIFIC ) ? TUSB_CLASS_MAPPED_INDEX_START + 4 : 0;
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -53,21 +53,21 @@
|
||||
|
||||
typedef struct {
|
||||
uint8_t coreid;
|
||||
uint8_t xfer_type; // cannot be control as control uses separated API
|
||||
uint8_t xfer_type; // TODO redundant, cannot be control as control uses separated API
|
||||
uint8_t index;
|
||||
uint8_t reserved;
|
||||
uint8_t class_code;
|
||||
} endpoint_handle_t;
|
||||
|
||||
static inline bool endpointhandle_is_valid(endpoint_handle_t endpoint_handle) ATTR_CONST ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
|
||||
static inline bool endpointhandle_is_valid(endpoint_handle_t endpoint_handle)
|
||||
static inline bool endpointhandle_is_valid(endpoint_handle_t edpt_hdl) ATTR_CONST ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
|
||||
static inline bool endpointhandle_is_valid(endpoint_handle_t edpt_hdl)
|
||||
{
|
||||
return endpoint_handle.xfer_type != TUSB_XFER_CONTROL;
|
||||
return (edpt_hdl.xfer_type != TUSB_XFER_CONTROL) && (edpt_hdl.class_code != 0);
|
||||
}
|
||||
|
||||
static inline bool endpointhandle_is_equal(endpoint_handle_t x, endpoint_handle_t y) ATTR_CONST ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
|
||||
static inline bool endpointhandle_is_equal(endpoint_handle_t x, endpoint_handle_t y)
|
||||
{
|
||||
return (x.coreid == y.coreid) && (x.xfer_type == y.xfer_type) && (x.index == y.index);
|
||||
return (x.coreid == y.coreid) && (x.xfer_type == y.xfer_type) && (x.index == y.index) && (x.class_code == y.class_code);
|
||||
}
|
||||
|
||||
tusb_error_t dcd_init(void) ATTR_WARN_UNUSED_RESULT;
|
||||
@ -89,8 +89,10 @@ void dcd_pipe_control_stall(uint8_t coreid);
|
||||
//tusb_error_t dcd_pipe_control_read(uint8_t coreid, void * buffer, uint16_t length);
|
||||
//void dcd_pipe_control_write_zero_length(uint8_t coreid);
|
||||
|
||||
endpoint_handle_t dcd_pipe_open(uint8_t coreid, tusb_descriptor_endpoint_t const * p_endpoint_desc) ATTR_WARN_UNUSED_RESULT;
|
||||
tusb_error_t dcd_pipe_xfer(endpoint_handle_t pipe_hdl, uint8_t buffer[], uint16_t total_bytes, bool int_on_complete) ATTR_WARN_UNUSED_RESULT;
|
||||
endpoint_handle_t dcd_pipe_open(uint8_t coreid, tusb_descriptor_endpoint_t const * p_endpoint_desc, uint8_t class_code) ATTR_WARN_UNUSED_RESULT;
|
||||
tusb_error_t dcd_pipe_queue_xfer(endpoint_handle_t edpt_hdl, void * buffer, uint16_t total_bytes) ATTR_WARN_UNUSED_RESULT; // only queue, not transferring yet
|
||||
tusb_error_t dcd_pipe_xfer(endpoint_handle_t edpt_hdl, void * buffer, uint16_t total_bytes, bool int_on_complete) ATTR_WARN_UNUSED_RESULT;
|
||||
tusb_error_t dcd_pipe_stall(endpoint_handle_t edpt_hdl) ATTR_WARN_UNUSED_RESULT;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -187,8 +187,8 @@ tusb_error_t dcd_init(void)
|
||||
{
|
||||
//------------- user manual 11.13 usb device controller initialization -------------// LPC_USB->USBEpInd = 0;
|
||||
// step 6 : set up control endpoint
|
||||
endpoint_set_max_packet_size(0, TUSB_CFG_DEVICE_CONTROL_PACKET_SIZE);
|
||||
endpoint_set_max_packet_size(1, TUSB_CFG_DEVICE_CONTROL_PACKET_SIZE);
|
||||
endpoint_set_max_packet_size(0, TUSB_CFG_DEVICE_CONTROL_ENDOINT_SIZE);
|
||||
endpoint_set_max_packet_size(1, TUSB_CFG_DEVICE_CONTROL_ENDOINT_SIZE);
|
||||
|
||||
// step 7 : slave mode set up
|
||||
LPC_USB->USBEpIntEn = (uint32_t) BIN8(11); // control endpoint cannot use DMA, non-control all use DMA
|
||||
|
@ -57,102 +57,10 @@
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
#define DCD_QHD_MAX 12
|
||||
#define QTD_INVALID 0x01
|
||||
#define CONTROL_ENDOINT_SIZE 64
|
||||
#define DCD_QTD_MAX 12
|
||||
#define DCD_QTD_PER_QHD_MAX 2 // maximum number of qtd that are linked into one queue head at a time
|
||||
|
||||
typedef struct {
|
||||
// Word 0: Next QTD Pointer
|
||||
uint32_t next; ///< Next link pointer This field contains the physical memory address of the next dTD to be processed
|
||||
|
||||
// Word 1: qTQ Token
|
||||
uint32_t : 3 ;
|
||||
volatile uint32_t xact_err : 1 ;
|
||||
uint32_t : 1 ;
|
||||
volatile uint32_t buffer_err : 1 ;
|
||||
volatile uint32_t halted : 1 ;
|
||||
volatile uint32_t active : 1 ;
|
||||
uint32_t : 2 ;
|
||||
uint32_t iso_mult_override : 2 ; ///< This field can be used for transmit ISOs to override the MULT field in the dQH. This field must be zero for all packet types that are not transmit-ISO.
|
||||
uint32_t : 3 ;
|
||||
uint32_t int_on_complete : 1 ;
|
||||
volatile uint32_t total_bytes : 15 ;
|
||||
uint32_t : 0 ;
|
||||
|
||||
// Word 2-6: Buffer Page Pointer List, Each element in the list is a 4K page aligned, physical memory address. The lower 12 bits in each pointer are reserved (except for the first one) as each memory pointer must reference the start of a 4K page
|
||||
uint32_t buffer[5]; ///< buffer1 has frame_n for TODO Isochronous
|
||||
|
||||
//------------- -------------//
|
||||
uint32_t reserved;
|
||||
} dcd_qtd_t;
|
||||
|
||||
STATIC_ASSERT( sizeof(dcd_qtd_t) == 32, "size is not correct");
|
||||
|
||||
typedef struct {
|
||||
// Word 0: Capabilities and Characteristics
|
||||
uint32_t : 15 ; ///< Number of packets executed per transaction descriptor 00 - Execute N transactions as demonstrated by the USB variable length protocol where N is computed using Max_packet_length and the Total_bytes field in the dTD. 01 - Execute one transaction 10 - Execute two transactions 11 - Execute three transactions Remark: Non-isochronous endpoints must set MULT = 00. Remark: Isochronous endpoints must set MULT = 01, 10, or 11 as needed.
|
||||
uint32_t int_on_setup : 1 ; ///< Interrupt on setup This bit is used on control type endpoints to indicate if USBINT is set in response to a setup being received.
|
||||
uint32_t max_package_size : 11 ; ///< This directly corresponds to the maximum packet size of the associated endpoint (wMaxPacketSize)
|
||||
uint32_t : 2 ;
|
||||
uint32_t zero_length_termination : 1 ; ///< This bit is used for non-isochronous endpoints to indicate when a zero-length packet is received to terminate transfers in case the total transfer length is “multiple”. 0 - Enable zero-length packet to terminate transfers equal to a multiple of Max_packet_length (default). 1 - Disable zero-length packet on transfers that are equal in length to a multiple Max_packet_length.
|
||||
uint32_t iso_mult : 2 ; ///<
|
||||
uint32_t : 0 ;
|
||||
|
||||
// Word 1: Current qTD Pointer
|
||||
volatile uint32_t qtd_addr;
|
||||
|
||||
// Word 2-9: Transfer Overlay
|
||||
volatile dcd_qtd_t qtd_overlay;
|
||||
|
||||
// Word 10-11: Setup request (control OUT only)
|
||||
volatile tusb_control_request_t setup_request;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
/// Due to the fact QHD is 64 bytes aligned but occupies only 48 bytes
|
||||
/// thus there are 16 bytes padding free that we can make use of.
|
||||
//--------------------------------------------------------------------+
|
||||
uint8_t reserved[16];
|
||||
|
||||
} ATTR_ALIGNED(64) dcd_qhd_t;
|
||||
|
||||
STATIC_ASSERT( sizeof(dcd_qhd_t) == 64, "size is not correct");
|
||||
|
||||
typedef struct {
|
||||
dcd_qhd_t qhd[DCD_QHD_MAX]; ///< Must be at 2K alignment
|
||||
dcd_qtd_t qtd[DCD_QHD_MAX] ATTR_ALIGNED(32);
|
||||
|
||||
}dcd_data_t;
|
||||
|
||||
ATTR_ALIGNED(2048) dcd_data_t dcd_data TUSB_CFG_ATTR_USBRAM;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD-DCD API
|
||||
//--------------------------------------------------------------------+
|
||||
tusb_error_t dcd_controller_reset(uint8_t coreid)
|
||||
{
|
||||
volatile uint32_t * p_reg_usbcmd;
|
||||
|
||||
p_reg_usbcmd = (coreid ? &LPC_USB1->USBCMD_D : &LPC_USB0->USBCMD_D);
|
||||
// NXP chip powered with non-host mode --> sts bit is not correctly reflected
|
||||
(*p_reg_usbcmd) |= BIT_(1); // TODO refractor reset controller
|
||||
|
||||
// timeout_timer_t timeout;
|
||||
// timeout_set(&timeout, 2); // should not take longer the time to stop controller
|
||||
while( ((*p_reg_usbcmd) & BIT_(1)) /*&& !timeout_expired(&timeout)*/) {}
|
||||
//
|
||||
// return timeout_expired(&timeout) ? TUSB_ERROR_OSAL_TIMEOUT : TUSB_ERROR_NONE;
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
void dcd_controller_connect(uint8_t coreid)
|
||||
{
|
||||
volatile uint32_t * p_reg_usbcmd = (coreid ? &LPC_USB1->USBCMD_D : &LPC_USB0->USBCMD_D);
|
||||
|
||||
(*p_reg_usbcmd) |= BIT_(0);
|
||||
}
|
||||
#define QTD_NEXT_INVALID 0x01
|
||||
|
||||
/*---------- ENDPTCTRL ----------*/
|
||||
enum {
|
||||
@ -182,9 +90,105 @@ enum {
|
||||
INT_MASK_NAK = BIT_(16)
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
// Word 0: Next QTD Pointer
|
||||
uint32_t next; ///< Next link pointer This field contains the physical memory address of the next dTD to be processed
|
||||
|
||||
// Word 1: qTQ Token
|
||||
uint32_t : 3 ;
|
||||
volatile uint32_t xact_err : 1 ;
|
||||
uint32_t : 1 ;
|
||||
volatile uint32_t buffer_err : 1 ;
|
||||
volatile uint32_t halted : 1 ;
|
||||
volatile uint32_t active : 1 ;
|
||||
uint32_t : 2 ;
|
||||
uint32_t iso_mult_override : 2 ; ///< This field can be used for transmit ISOs to override the MULT field in the dQH. This field must be zero for all packet types that are not transmit-ISO.
|
||||
uint32_t : 3 ;
|
||||
uint32_t int_on_complete : 1 ;
|
||||
volatile uint32_t total_bytes : 15 ;
|
||||
uint32_t : 0 ;
|
||||
|
||||
// Word 2-6: Buffer Page Pointer List, Each element in the list is a 4K page aligned, physical memory address. The lower 12 bits in each pointer are reserved (except for the first one) as each memory pointer must reference the start of a 4K page
|
||||
uint32_t buffer[5]; ///< buffer1 has frame_n for TODO Isochronous
|
||||
|
||||
//------------- DCD Area -------------//
|
||||
uint16_t expected_bytes;
|
||||
uint8_t used;
|
||||
uint8_t reserved;
|
||||
} dcd_qtd_t;
|
||||
|
||||
STATIC_ASSERT( sizeof(dcd_qtd_t) == 32, "size is not correct");
|
||||
|
||||
typedef struct {
|
||||
// Word 0: Capabilities and Characteristics
|
||||
uint32_t : 15 ; ///< Number of packets executed per transaction descriptor 00 - Execute N transactions as demonstrated by the USB variable length protocol where N is computed using Max_packet_length and the Total_bytes field in the dTD. 01 - Execute one transaction 10 - Execute two transactions 11 - Execute three transactions Remark: Non-isochronous endpoints must set MULT = 00. Remark: Isochronous endpoints must set MULT = 01, 10, or 11 as needed.
|
||||
uint32_t int_on_setup : 1 ; ///< Interrupt on setup This bit is used on control type endpoints to indicate if USBINT is set in response to a setup being received.
|
||||
uint32_t max_package_size : 11 ; ///< This directly corresponds to the maximum packet size of the associated endpoint (wMaxPacketSize)
|
||||
uint32_t : 2 ;
|
||||
uint32_t zero_length_termination : 1 ; ///< This bit is used for non-isochronous endpoints to indicate when a zero-length packet is received to terminate transfers in case the total transfer length is “multiple”. 0 - Enable zero-length packet to terminate transfers equal to a multiple of Max_packet_length (default). 1 - Disable zero-length packet on transfers that are equal in length to a multiple Max_packet_length.
|
||||
uint32_t iso_mult : 2 ; ///<
|
||||
uint32_t : 0 ;
|
||||
|
||||
// Word 1: Current qTD Pointer
|
||||
volatile uint32_t qtd_addr;
|
||||
|
||||
// Word 2-9: Transfer Overlay
|
||||
volatile dcd_qtd_t qtd_overlay;
|
||||
|
||||
// Word 10-11: Setup request (control OUT only)
|
||||
volatile tusb_control_request_t setup_request;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
/// Due to the fact QHD is 64 bytes aligned but occupies only 48 bytes
|
||||
/// thus there are 16 bytes padding free that we can make use of.
|
||||
//--------------------------------------------------------------------+
|
||||
uint8_t class_code; // Class code that endpoint belongs to
|
||||
uint8_t xfer_type;
|
||||
uint8_t list_qtd_idx[DCD_QTD_PER_QHD_MAX];
|
||||
|
||||
uint8_t reserved[14-DCD_QTD_PER_QHD_MAX];
|
||||
} ATTR_ALIGNED(64) dcd_qhd_t;
|
||||
|
||||
STATIC_ASSERT( sizeof(dcd_qhd_t) == 64, "size is not correct");
|
||||
|
||||
typedef struct {
|
||||
dcd_qhd_t qhd[DCD_QHD_MAX]; ///< Must be at 2K alignment
|
||||
dcd_qtd_t qtd[DCD_QTD_MAX] ATTR_ALIGNED(32);
|
||||
|
||||
}dcd_data_t;
|
||||
|
||||
ATTR_ALIGNED(2048) dcd_data_t dcd_data TUSB_CFG_ATTR_USBRAM;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// CONTROLLER API
|
||||
//--------------------------------------------------------------------+
|
||||
tusb_error_t dcd_controller_reset(uint8_t coreid)
|
||||
{
|
||||
volatile uint32_t * p_reg_usbcmd;
|
||||
|
||||
p_reg_usbcmd = (coreid ? &LPC_USB1->USBCMD_D : &LPC_USB0->USBCMD_D);
|
||||
// NXP chip powered with non-host mode --> sts bit is not correctly reflected
|
||||
(*p_reg_usbcmd) |= BIT_(1); // TODO refractor reset controller
|
||||
|
||||
// timeout_timer_t timeout;
|
||||
// timeout_set(&timeout, 2); // should not take longer the time to stop controller
|
||||
while( ((*p_reg_usbcmd) & BIT_(1)) /*&& !timeout_expired(&timeout)*/) {}
|
||||
//
|
||||
// return timeout_expired(&timeout) ? TUSB_ERROR_OSAL_TIMEOUT : TUSB_ERROR_NONE;
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
void dcd_controller_connect(uint8_t coreid)
|
||||
{
|
||||
volatile uint32_t * p_reg_usbcmd = (coreid ? &LPC_USB1->USBCMD_D : &LPC_USB0->USBCMD_D);
|
||||
|
||||
(*p_reg_usbcmd) |= BIT_(0);
|
||||
}
|
||||
|
||||
void dcd_controller_set_address(uint8_t coreid, uint8_t dev_addr)
|
||||
{
|
||||
LPC_USB0->DEVICEADDR = (dev_addr << 25) | BIT_(24);
|
||||
@ -226,8 +230,8 @@ void bus_reset(uint8_t coreid)
|
||||
|
||||
//------------- Set up Control Endpoints (0 OUT, 1 IN) -------------//
|
||||
dcd_data.qhd[0].zero_length_termination = dcd_data.qhd[1].zero_length_termination = 1;
|
||||
dcd_data.qhd[0].max_package_size = dcd_data.qhd[1].max_package_size = CONTROL_ENDOINT_SIZE;
|
||||
dcd_data.qhd[0].qtd_overlay.next = dcd_data.qhd[1].qtd_overlay.next = QTD_INVALID;
|
||||
dcd_data.qhd[0].max_package_size = dcd_data.qhd[1].max_package_size = TUSB_CFG_DEVICE_CONTROL_ENDOINT_SIZE;
|
||||
dcd_data.qhd[0].qtd_overlay.next = dcd_data.qhd[1].qtd_overlay.next = QTD_NEXT_INVALID;
|
||||
|
||||
dcd_data.qhd[0].int_on_setup = 1; // OUT only
|
||||
}
|
||||
@ -247,39 +251,41 @@ tusb_error_t dcd_init(void)
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// PIPE API
|
||||
// PIPE HELPER
|
||||
//--------------------------------------------------------------------+
|
||||
static inline uint8_t endpoint_to_pos(uint8_t logical_endpoint, tusb_direction_t dir) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t endpoint_to_pos(uint8_t logical_endpoint, tusb_direction_t dir)
|
||||
{
|
||||
return logical_endpoint + (dir == TUSB_DIR_HOST_TO_DEV ? 0 : 16);
|
||||
static inline uint8_t edpt_pos2phy(uint8_t pos) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t edpt_pos2phy(uint8_t pos)
|
||||
{ // 0-5 --> OUT, 16-21 IN
|
||||
return (pos < DCD_QHD_MAX/2) ? (2*pos) : (2*(pos-16)+1);
|
||||
}
|
||||
|
||||
static inline uint8_t endpoint_phy2pos(uint8_t physical_endpoint) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t endpoint_phy2pos(uint8_t physical_endpoint)
|
||||
static inline uint8_t edpt_phy2pos(uint8_t physical_endpoint) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t edpt_phy2pos(uint8_t physical_endpoint)
|
||||
{
|
||||
return physical_endpoint/2 + ( (physical_endpoint%2) ? 16 : 0);
|
||||
}
|
||||
|
||||
static inline uint8_t endpoint_log2phy(uint8_t logical_endpoint, tusb_direction_t dir) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t endpoint_log2phy(uint8_t logical_endpoint, tusb_direction_t dir)
|
||||
{
|
||||
return 2*logical_endpoint + (dir == TUSB_DIR_DEV_TO_HOST ? 1 : 0);
|
||||
}
|
||||
|
||||
static inline uint8_t endpoint_addr2phy(uint8_t endpoint_addr) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t endpoint_addr2phy(uint8_t endpoint_addr)
|
||||
static inline uint8_t edpt_addr2phy(uint8_t endpoint_addr) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t edpt_addr2phy(uint8_t endpoint_addr)
|
||||
{
|
||||
return 2*(endpoint_addr & 0x0F) + ((endpoint_addr & TUSB_DIR_DEV_TO_HOST_MASK) ? 1 : 0);
|
||||
}
|
||||
|
||||
static inline uint8_t edpt_phy2log(uint8_t physical_endpoint) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t edpt_phy2log(uint8_t physical_endpoint)
|
||||
{
|
||||
return physical_endpoint/2;
|
||||
}
|
||||
|
||||
static void qtd_init(dcd_qtd_t* p_qtd, void * data_ptr, uint16_t total_bytes)
|
||||
{
|
||||
memclr_(p_qtd, sizeof(dcd_qtd_t));
|
||||
|
||||
p_qtd->next = QTD_INVALID;
|
||||
p_qtd->used = 1;
|
||||
|
||||
p_qtd->next = QTD_NEXT_INVALID;
|
||||
p_qtd->active = 1;
|
||||
p_qtd->total_bytes = total_bytes;
|
||||
p_qtd->total_bytes = p_qtd->expected_bytes = total_bytes;
|
||||
|
||||
if (data_ptr != NULL)
|
||||
{
|
||||
@ -291,23 +297,39 @@ static void qtd_init(dcd_qtd_t* p_qtd, void * data_ptr, uint16_t total_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
// retval 0: invalid
|
||||
static inline uint8_t qtd_find_free(uint8_t coreid) ATTR_PURE ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t qtd_find_free(uint8_t coreid)
|
||||
{
|
||||
for(uint8_t i=2; i<DCD_QTD_MAX; i++)
|
||||
{ // exclude control's qtd
|
||||
if ( dcd_data.qtd[i].used == 0) return i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// CONTROL PIPE API
|
||||
//--------------------------------------------------------------------+
|
||||
void dcd_pipe_control_stall(uint8_t coreid)
|
||||
{
|
||||
LPC_USB0->ENDPTCTRL0 |= (ENDPTCTRL_MASK_STALL << 16); // stall Control IN
|
||||
}
|
||||
|
||||
// control transfer does not need to use qtd find function
|
||||
tusb_error_t dcd_pipe_control_xfer(uint8_t coreid, tusb_direction_t dir, void * buffer, uint16_t length)
|
||||
{
|
||||
uint8_t const endpoint_data = (dir == TUSB_DIR_DEV_TO_HOST) ? 1 : 0; // IN xfer --> data phase on Control IN, other Control OUT
|
||||
|
||||
ASSERT_FALSE(dcd_data.qhd[0].qtd_overlay.active || dcd_data.qhd[1].qtd_overlay.active, TUSB_ERROR_FAILED);
|
||||
|
||||
//------------- Data Phase -------------//
|
||||
if (length)
|
||||
{
|
||||
dcd_qtd_t* p_data = &dcd_data.qtd[0];
|
||||
qtd_init(p_data, buffer, length);
|
||||
dcd_data.qhd[endpoint_data].qtd_overlay.next = (uint32_t) p_data;
|
||||
|
||||
LPC_USB0->ENDPTPRIME |= BIT_( endpoint_phy2pos(endpoint_data) );
|
||||
}
|
||||
|
||||
//------------- Status Phase (other endpoint, opposite direction) -------------//
|
||||
@ -315,12 +337,26 @@ tusb_error_t dcd_pipe_control_xfer(uint8_t coreid, tusb_direction_t dir, void *
|
||||
qtd_init(p_status, NULL, 0); // zero length xfer
|
||||
dcd_data.qhd[1 - endpoint_data].qtd_overlay.next = (uint32_t) p_status;
|
||||
|
||||
LPC_USB0->ENDPTPRIME |= BIT_( endpoint_phy2pos(1 - endpoint_data) );
|
||||
//------------- Prime Endpoint -------------//
|
||||
LPC_USB0->ENDPTPRIME |= BIT_( edpt_phy2pos(1 - endpoint_data) ) |
|
||||
(length ? BIT_( edpt_phy2pos(endpoint_data) ) : 0) ;
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
endpoint_handle_t dcd_pipe_open(uint8_t coreid, tusb_descriptor_endpoint_t const * p_endpoint_desc)
|
||||
//--------------------------------------------------------------------+
|
||||
// BULK/INTERRUPT/ISOCHRONOUS PIPE API
|
||||
//--------------------------------------------------------------------+
|
||||
tusb_error_t dcd_pipe_stall(endpoint_handle_t edpt_hdl)
|
||||
{
|
||||
volatile uint32_t * reg_control = (&LPC_USB0->ENDPTCTRL0) + edpt_phy2log(edpt_hdl.index);
|
||||
|
||||
(*reg_control) |= ENDPTCTRL_MASK_STALL << (edpt_hdl.index & 0x01 ? 16 : 0);
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
endpoint_handle_t dcd_pipe_open(uint8_t coreid, tusb_descriptor_endpoint_t const * p_endpoint_desc, uint8_t class_code)
|
||||
{
|
||||
// TODO USB1 only has 4 non-control enpoint (USB0 has 5)
|
||||
endpoint_handle_t const null_handle = { .coreid = 0, .xfer_type = 0, .index = 0 };
|
||||
@ -336,53 +372,123 @@ endpoint_handle_t dcd_pipe_open(uint8_t coreid, tusb_descriptor_endpoint_t const
|
||||
ASSERT_FALSE( (*reg_control) & (ENDPTCTRL_MASK_ENABLE << (dir ? 16 : 0)), null_handle ); // endpoint must not be already enabled
|
||||
|
||||
//------------- Prepare Queue Head -------------//
|
||||
uint8_t ep_idx = endpoint_addr2phy(p_endpoint_desc->bEndpointAddress);
|
||||
uint8_t ep_idx = edpt_addr2phy(p_endpoint_desc->bEndpointAddress);
|
||||
dcd_qhd_t * p_qhd = &dcd_data.qhd[ep_idx];
|
||||
|
||||
memclr_(p_qhd, sizeof(dcd_qhd_t));
|
||||
|
||||
p_qhd->class_code = class_code;
|
||||
p_qhd->xfer_type = p_endpoint_desc->bmAttributes.xfer;
|
||||
p_qhd->zero_length_termination = 1;
|
||||
p_qhd->max_package_size = p_endpoint_desc->wMaxPacketSize.size;
|
||||
p_qhd->qtd_overlay.next = QTD_INVALID;
|
||||
p_qhd->max_package_size = p_endpoint_desc->wMaxPacketSize.size;
|
||||
p_qhd->qtd_overlay.next = QTD_NEXT_INVALID;
|
||||
|
||||
(*reg_control) |= ((p_endpoint_desc->bmAttributes.xfer << 2) | ENDPTCTRL_MASK_ENABLE | ENDPTCTRL_MASK_TOGGLE_RESET) << (dir ? 16 : 0);
|
||||
|
||||
return (endpoint_handle_t) { .coreid = coreid, .xfer_type = p_endpoint_desc->bmAttributes.xfer, .index = ep_idx };
|
||||
}
|
||||
|
||||
STATIC_ INLINE_ dcd_qhd_t* qhd_get_from_endpoint_handle(endpoint_handle_t edpt_hdl) ATTR_PURE ATTR_ALWAYS_INLINE;
|
||||
STATIC_ INLINE_ dcd_qhd_t* qhd_get_from_endpoint_handle(endpoint_handle_t edpt_hdl)
|
||||
{
|
||||
return &dcd_data.qhd[edpt_hdl.index];
|
||||
return (endpoint_handle_t) { .coreid = coreid, .xfer_type = p_endpoint_desc->bmAttributes.xfer, .index = ep_idx, .class_code = class_code};
|
||||
}
|
||||
|
||||
bool dcd_pipe_is_busy(endpoint_handle_t edpt_hdl)
|
||||
{
|
||||
dcd_qhd_t* p_qhd = qhd_get_from_endpoint_handle(edpt_hdl);
|
||||
dcd_qhd_t* p_qhd = &dcd_data.qhd[edpt_hdl.index];
|
||||
|
||||
// LPC_USB0->ENDPTSTAT & endpoint_phy2pos(edpt_hdl.index)
|
||||
return !p_qhd->qtd_overlay.halted && p_qhd->qtd_overlay.active;
|
||||
}
|
||||
|
||||
tusb_error_t dcd_pipe_xfer(endpoint_handle_t edpt_hdl, uint8_t buffer[], uint16_t total_bytes, bool int_on_complete)
|
||||
// add only, controller virtually cannot know
|
||||
static tusb_error_t pipe_add_xfer(endpoint_handle_t edpt_hdl, void * buffer, uint16_t total_bytes, bool int_on_complete)
|
||||
{
|
||||
dcd_qhd_t* p_qhd = qhd_get_from_endpoint_handle(edpt_hdl);
|
||||
dcd_qtd_t* p_qtd = &dcd_data.qtd[edpt_hdl.index]; // TODO allocate qtd
|
||||
|
||||
ASSERT(edpt_hdl.xfer_type != TUSB_XFER_ISOCHRONOUS, TUSB_ERROR_NOT_SUPPORTED_YET);
|
||||
|
||||
uint8_t qtd_idx = qtd_find_free(edpt_hdl.coreid);
|
||||
ASSERT(qtd_idx != 0, TUSB_ERROR_DCD_NOT_ENOUGH_QTD);
|
||||
|
||||
dcd_qhd_t* p_qhd = &dcd_data.qhd[edpt_hdl.index];
|
||||
dcd_qtd_t* p_qtd = &dcd_data.qtd[qtd_idx];
|
||||
|
||||
//------------- Find free slot in qhd's array list -------------//
|
||||
uint8_t free_slot;
|
||||
for(free_slot=0; free_slot < DCD_QTD_PER_QHD_MAX; free_slot++)
|
||||
{
|
||||
if ( p_qhd->list_qtd_idx[free_slot] == 0 ) break; // found free slot
|
||||
}
|
||||
ASSERT(free_slot < DCD_QTD_PER_QHD_MAX, TUSB_ERROR_DCD_NOT_ENOUGH_QTD);
|
||||
|
||||
p_qhd->list_qtd_idx[free_slot] = qtd_idx; // add new qtd to qhd's array list
|
||||
|
||||
//------------- Prepare qtd -------------//
|
||||
qtd_init(p_qtd, buffer, total_bytes);
|
||||
p_qtd->int_on_complete = int_on_complete;
|
||||
|
||||
p_qhd->qtd_overlay.next = (uint32_t) p_qtd;
|
||||
if ( free_slot > 0 ) dcd_data.qtd[ p_qhd->list_qtd_idx[free_slot-1] ].next = (uint32_t) p_qtd;
|
||||
|
||||
LPC_USB0->ENDPTPRIME |= BIT_( endpoint_phy2pos(edpt_hdl.index) ) ;
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
tusb_error_t dcd_pipe_queue_xfer(endpoint_handle_t edpt_hdl, void * buffer, uint16_t total_bytes)
|
||||
{
|
||||
return pipe_add_xfer( edpt_hdl, buffer, total_bytes, false);
|
||||
}
|
||||
|
||||
tusb_error_t dcd_pipe_xfer(endpoint_handle_t edpt_hdl, void* buffer, uint16_t total_bytes, bool int_on_complete)
|
||||
{
|
||||
ASSERT_STATUS ( pipe_add_xfer(edpt_hdl, buffer, total_bytes, int_on_complete) );
|
||||
|
||||
dcd_qhd_t* p_qhd = &dcd_data.qhd[ edpt_hdl.index ];
|
||||
dcd_qtd_t* p_qtd = &dcd_data.qtd[ p_qhd->list_qtd_idx[0] ];
|
||||
|
||||
p_qhd->qtd_overlay.next = (uint32_t) p_qtd; // attach head QTD to QHD start transferring
|
||||
|
||||
LPC_USB0->ENDPTPRIME |= BIT_( edpt_phy2pos(edpt_hdl.index) ) ;
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
//------------- Device Controller Driver's Interrupt Handler -------------//
|
||||
void xfer_complete_isr(uint8_t coreid, uint8_t reg_complete)
|
||||
{
|
||||
if (reg_complete & BIT_(3+16))
|
||||
{
|
||||
hal_debugger_breakpoint();
|
||||
}
|
||||
|
||||
// TODO currently exclude control
|
||||
for(uint8_t ep_idx = 2; ep_idx < DCD_QHD_MAX; ep_idx++)
|
||||
{
|
||||
if ( BIT_TEST_(reg_complete, edpt_phy2pos(ep_idx)) )
|
||||
{ // 23.10.12.3 Failed QTD also get ENDPTCOMPLETE set
|
||||
dcd_qhd_t * p_qhd = &dcd_data.qhd[ep_idx];
|
||||
|
||||
endpoint_handle_t edpt_hdl =
|
||||
{
|
||||
.coreid = coreid,
|
||||
.xfer_type = p_qhd->xfer_type,
|
||||
.index = ep_idx,
|
||||
.class_code = p_qhd->class_code
|
||||
};
|
||||
|
||||
// retire all QTDs in array list, up to 1st still-active QTD
|
||||
while( p_qhd->list_qtd_idx[0] != 0 )
|
||||
{
|
||||
dcd_qtd_t * p_qtd = &dcd_data.qtd[ p_qhd->list_qtd_idx[0] ];
|
||||
|
||||
if (p_qtd->active) break; // stop immediately if found still-active QTD and shift array list
|
||||
|
||||
//------------- Free QTD and shift array list -------------//
|
||||
p_qtd->used = 0; // free QTD
|
||||
memmove(p_qhd->list_qtd_idx, p_qhd->list_qtd_idx+1, DCD_QTD_PER_QHD_MAX-1);
|
||||
|
||||
if (p_qtd->int_on_complete)
|
||||
{
|
||||
tusb_event_t event = ( p_qtd->xact_err || p_qtd->halted || p_qtd->buffer_err ) ? TUSB_EVENT_XFER_ERROR : TUSB_EVENT_XFER_COMPLETE;
|
||||
usbd_xfer_isr(edpt_hdl, event, p_qtd->expected_bytes - p_qtd->total_bytes); // only number of bytes in the IOC qtd
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dcd_isr(uint8_t coreid)
|
||||
{
|
||||
uint32_t int_status = LPC_USB0->USBSTS_D;
|
||||
@ -405,13 +511,28 @@ void dcd_isr(uint8_t coreid)
|
||||
tusb_control_request_t control_request = dcd_data.qhd[0].setup_request;
|
||||
|
||||
LPC_USB0->ENDPTSETUPSTAT = LPC_USB0->ENDPTSETUPSTAT;
|
||||
|
||||
//------------- Flush if previous transfer is not done -------------//
|
||||
if (dcd_data.qhd[0].qtd_overlay.active || dcd_data.qhd[1].qtd_overlay.active)
|
||||
{
|
||||
do
|
||||
{
|
||||
LPC_USB0->ENDPTFLUSH = BIT_(0) | BIT_(16);
|
||||
while(LPC_USB0->ENDPTFLUSH) {} // TODO refractor later
|
||||
}while( LPC_USB0->ENDPTSTAT & (BIT_(0) | BIT_(16)) );
|
||||
|
||||
dcd_data.qhd[0].qtd_overlay.active = dcd_data.qhd[1].qtd_overlay.active = 0;
|
||||
}
|
||||
|
||||
usbd_setup_received_isr(coreid, &control_request);
|
||||
}
|
||||
|
||||
if (LPC_USB0->ENDPTCOMPLETE)
|
||||
{
|
||||
// hal_debugger_breakpoint();
|
||||
LPC_USB0->ENDPTCOMPLETE = LPC_USB0->ENDPTCOMPLETE;
|
||||
uint32_t edpt_complete = LPC_USB0->ENDPTCOMPLETE;
|
||||
LPC_USB0->ENDPTCOMPLETE = edpt_complete; // acknowledge
|
||||
|
||||
xfer_complete_isr(coreid, edpt_complete);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,8 +61,18 @@ static device_class_driver_t const usbd_class_drivers[TUSB_CLASS_MAPPED_INDEX_ST
|
||||
[TUSB_CLASS_HID] = {
|
||||
.open = hidd_open,
|
||||
.control_request = hidd_control_request,
|
||||
.isr = hidd_isr
|
||||
},
|
||||
#endif
|
||||
|
||||
#if TUSB_CFG_DEVICE_MSC
|
||||
[TUSB_CLASS_MSC] = {
|
||||
.open = mscd_open,
|
||||
.control_request = mscd_control_request,
|
||||
.isr = mscd_isr
|
||||
},
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@ -86,39 +96,6 @@ void usbd_bus_reset(uint32_t coreid)
|
||||
memclr_(&usbd_devices[coreid], sizeof(usbd_device_info_t));
|
||||
}
|
||||
|
||||
void std_get_descriptor(uint8_t coreid, tusb_control_request_t * p_request)
|
||||
{
|
||||
tusb_std_descriptor_type_t const desc_type = p_request->wValue >> 8;
|
||||
uint8_t const desc_index = u16_low_u8( p_request->wValue );
|
||||
switch ( desc_type )
|
||||
{
|
||||
case TUSB_DESC_TYPE_DEVICE:
|
||||
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, &app_tusb_desc_device,
|
||||
min16_of( p_request->wLength, sizeof(tusb_descriptor_device_t)) );
|
||||
break;
|
||||
|
||||
case TUSB_DESC_TYPE_CONFIGURATION:
|
||||
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, &app_tusb_desc_configuration,
|
||||
min16_of( p_request->wLength, sizeof(app_tusb_desc_configuration)) );
|
||||
break;
|
||||
|
||||
case TUSB_DESC_TYPE_STRING:
|
||||
{
|
||||
uint8_t *p_string = (uint8_t*) &app_tusb_desc_strings;
|
||||
for(uint8_t index =0; index < desc_index; index++)
|
||||
{
|
||||
p_string += (*p_string);
|
||||
}
|
||||
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, p_string, *p_string);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
// ASSERT(false, (void) 0); // descriptors that is not supported yet
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
tusb_error_t usbh_set_configure_received(uint8_t coreid, uint8_t config_number)
|
||||
{
|
||||
dcd_controller_set_configuration(coreid, config_number);
|
||||
@ -154,9 +131,45 @@ tusb_error_t usbh_set_configure_received(uint8_t coreid, uint8_t config_number)
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
tusb_error_t std_get_descriptor(uint8_t coreid, tusb_control_request_t * p_request)
|
||||
{
|
||||
tusb_std_descriptor_type_t const desc_type = p_request->wValue >> 8;
|
||||
uint8_t const desc_index = u16_low_u8( p_request->wValue );
|
||||
switch ( desc_type )
|
||||
{
|
||||
case TUSB_DESC_TYPE_DEVICE:
|
||||
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, &app_tusb_desc_device,
|
||||
min16_of( p_request->wLength, sizeof(tusb_descriptor_device_t)) );
|
||||
break;
|
||||
|
||||
case TUSB_DESC_TYPE_CONFIGURATION:
|
||||
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, &app_tusb_desc_configuration,
|
||||
min16_of( p_request->wLength, sizeof(app_tusb_desc_configuration)) );
|
||||
break;
|
||||
|
||||
case TUSB_DESC_TYPE_STRING:
|
||||
{
|
||||
uint8_t *p_string = (uint8_t*) &app_tusb_desc_strings;
|
||||
for(uint8_t index =0; index < desc_index; index++)
|
||||
{
|
||||
p_string += (*p_string);
|
||||
}
|
||||
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, p_string, *p_string);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
|
||||
}
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
void usbd_setup_received_isr(uint8_t coreid, tusb_control_request_t * p_request)
|
||||
{
|
||||
usbd_device_info_t *p_device = &usbd_devices[coreid];
|
||||
tusb_error_t error = TUSB_ERROR_NONE;
|
||||
|
||||
switch(p_request->bmRequestType_bit.recipient)
|
||||
{
|
||||
//------------- Standard Control such as those in enumeration -------------//
|
||||
@ -164,7 +177,7 @@ void usbd_setup_received_isr(uint8_t coreid, tusb_control_request_t * p_request)
|
||||
switch ( p_request->bRequest )
|
||||
{
|
||||
case TUSB_REQUEST_GET_DESCRIPTOR:
|
||||
std_get_descriptor(coreid, p_request);
|
||||
error = std_get_descriptor(coreid, p_request);
|
||||
break;
|
||||
|
||||
case TUSB_REQUEST_SET_ADDRESS:
|
||||
@ -176,10 +189,11 @@ void usbd_setup_received_isr(uint8_t coreid, tusb_control_request_t * p_request)
|
||||
|
||||
case TUSB_REQUEST_SET_CONFIGURATION:
|
||||
usbh_set_configure_received(coreid, (uint8_t) p_request->wValue);
|
||||
|
||||
dcd_pipe_control_xfer(coreid, TUSB_DIR_HOST_TO_DEV, NULL, 0); // zero length
|
||||
break;
|
||||
|
||||
default: ASSERT(false, VOID_RETURN); break;
|
||||
default: error = TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; break;
|
||||
}
|
||||
break;
|
||||
|
||||
@ -191,12 +205,18 @@ void usbd_setup_received_isr(uint8_t coreid, tusb_control_request_t * p_request)
|
||||
|
||||
if ( usbd_class_drivers[class_code].control_request )
|
||||
{
|
||||
usbd_class_drivers[class_code].control_request(coreid, p_request);
|
||||
error = usbd_class_drivers[class_code].control_request(coreid, p_request);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default: ASSERT(false, VOID_RETURN); break;
|
||||
default: error = TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; break;
|
||||
}
|
||||
|
||||
if(TUSB_ERROR_NONE != error)
|
||||
{ // Response with Protocol Stall if request is not supported
|
||||
dcd_pipe_control_stall(coreid);
|
||||
ASSERT(error == TUSB_ERROR_NONE, VOID_RETURN);
|
||||
}
|
||||
}
|
||||
|
||||
@ -224,7 +244,19 @@ tusb_error_t usbd_pipe_open(uint8_t coreid, tusb_descriptor_interface_t const *
|
||||
//--------------------------------------------------------------------+
|
||||
void usbd_xfer_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes)
|
||||
{
|
||||
usbd_device_info_t *p_device = &usbd_devices[edpt_hdl.coreid];
|
||||
// usbd_device_info_t *p_device = &usbd_devices[edpt_hdl.coreid];
|
||||
uint8_t class_index = std_class_code_to_index(edpt_hdl.class_code);
|
||||
|
||||
if (class_index == 0) // Control Transfer
|
||||
{
|
||||
|
||||
}else if (usbd_class_drivers[class_index].isr)
|
||||
{
|
||||
usbd_class_drivers[class_index].isr(edpt_hdl, event, xferred_bytes);
|
||||
}else
|
||||
{
|
||||
ASSERT(false, VOID_RETURN); // something wrong, no one claims the isr's source
|
||||
}
|
||||
|
||||
}
|
||||
//void usbd_isr(uint8_t coreid, tusb_event_t event)
|
||||
|
@ -65,7 +65,7 @@
|
||||
typedef struct {
|
||||
tusb_error_t (* const open)(uint8_t, tusb_descriptor_interface_t const *, uint16_t*);
|
||||
tusb_error_t (* const control_request) (uint8_t, tusb_control_request_t const *);
|
||||
// void (* const isr) (pipe_handle_t, tusb_event_t);
|
||||
void (* const isr) (endpoint_handle_t, tusb_event_t, uint32_t);
|
||||
// void (* const close) (uint8_t);
|
||||
} device_class_driver_t;
|
||||
|
||||
|
@ -57,6 +57,8 @@
|
||||
|
||||
#define USBD_MAX_INTERFACE 16 // TODO refractor later
|
||||
#define USBD_MAX_ENDPOINT 32 // TODO refractor later
|
||||
|
||||
|
||||
typedef struct {
|
||||
volatile uint8_t state;
|
||||
uint8_t interface2class[USBD_MAX_INTERFACE]; // determine interface number belongs to which class
|
||||
@ -66,7 +68,9 @@ extern usbd_device_info_t usbd_devices[CONTROLLER_DEVICE_NUMBER];
|
||||
//--------------------------------------------------------------------+
|
||||
// callback from DCD ISR
|
||||
//--------------------------------------------------------------------+
|
||||
void usbd_xfer_isr(endpoint_handle_t pipe_hdl, tusb_event_t event, uint32_t xferred_bytes);
|
||||
void usbd_xfer_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes);
|
||||
void usbd_bus_reset(uint32_t coreid);
|
||||
void usbd_setup_received_isr(uint8_t coreid, tusb_control_request_t * p_request);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -72,8 +72,10 @@ tusb_error_t hal_init(void)
|
||||
dcd_controller_reset(0);
|
||||
LPC_USB0->USBMODE_D = LPC43XX_USBMODE_DEVICE;
|
||||
LPC_USB0->OTGSC = (1<<3) | (1<<0) /*| (1<<16)| (1<<24)| (1<<25)| (1<<26)| (1<<27)| (1<<28)| (1<<29)| (1<<30)*/;
|
||||
// LPC_USB0->PORTSC1_D |= (1<<24); // force full speed
|
||||
// dcd_controller_connect(0);
|
||||
|
||||
#if TUSB_CFG_DEVICE_FULLSPEED // TODO for easy testing
|
||||
LPC_USB0->PORTSC1_D |= (1<<24); // force full speed
|
||||
#endif
|
||||
#endif
|
||||
|
||||
hal_interrupt_enable(0);
|
||||
|
@ -438,7 +438,7 @@ tusb_error_t hcd_pipe_xfer(pipe_handle_t pipe_hdl, uint8_t buffer[], uint16_t t
|
||||
{ // the just added qtd is pointed by list_tail
|
||||
p_qhd->p_qtd_list_tail->int_on_complete = 1;
|
||||
}
|
||||
p_qhd->qtd_overlay.next.address = (uint32_t) p_qhd->p_qtd_list_head; // attach to queue head to start transferring
|
||||
p_qhd->qtd_overlay.next.address = (uint32_t) p_qhd->p_qtd_list_head; // attach head QTD to QHD start transferring
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
@ -291,7 +291,7 @@ typedef struct {
|
||||
|
||||
/// SITD is 32-byte aligned but occupies only 28 --> 6 bytes for storing extra data
|
||||
uint8_t used;
|
||||
uint8_t IhdIdx;
|
||||
uint8_t ihd_idx;
|
||||
uint8_t reserved2[2];
|
||||
}ATTR_ALIGNED(32) ehci_sitd_t;
|
||||
|
||||
|
@ -121,17 +121,6 @@ ATTR_ALIGNED(4) STATIC_VAR uint8_t enum_data_buffer[TUSB_CFG_HOST_ENUM_BUFFER_SI
|
||||
static inline uint8_t get_new_address(void) ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t get_configure_number_for_device(tusb_descriptor_device_t* dev_desc) ATTR_ALWAYS_INLINE;
|
||||
|
||||
static inline uint8_t std_class_code_to_index(uint8_t std_class_code) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t std_class_code_to_index(uint8_t std_class_code)
|
||||
{
|
||||
return (std_class_code <= TUSB_CLASS_AUDIO_VIDEO ) ? std_class_code :
|
||||
(std_class_code == TUSB_CLASS_DIAGNOSTIC ) ? TUSB_CLASS_MAPPED_INDEX_START :
|
||||
(std_class_code == TUSB_CLASS_WIRELESS_CONTROLLER ) ? TUSB_CLASS_MAPPED_INDEX_START + 1 :
|
||||
(std_class_code == TUSB_CLASS_MISC ) ? TUSB_CLASS_MAPPED_INDEX_START + 2 :
|
||||
(std_class_code == TUSB_CLASS_APPLICATION_SPECIFIC ) ? TUSB_CLASS_MAPPED_INDEX_START + 3 :
|
||||
(std_class_code == TUSB_CLASS_VENDOR_SPECIFIC ) ? TUSB_CLASS_MAPPED_INDEX_START + 4 : 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// PUBLIC API (Parameter Verification is required)
|
||||
//--------------------------------------------------------------------+
|
||||
|
@ -89,6 +89,10 @@
|
||||
#if TUSB_CFG_DEVICE_CDC
|
||||
#include "class/cdc.h"
|
||||
#endif
|
||||
|
||||
#if TUSB_CFG_DEVICE_MSC
|
||||
#include "class/msc_device.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -171,6 +171,9 @@
|
||||
|
||||
#define DEVICE_CLASS_HID ( TUSB_CFG_DEVICE_HID_KEYBOARD + TUSB_CFG_DEVICE_HID_MOUSE + TUSB_CFG_DEVICE_HID_GENERIC )
|
||||
|
||||
#if TUSB_CFG_DEVICE_CONTROL_ENDOINT_SIZE > 64
|
||||
#error Control Endpoint Max Package Size cannot larger than 64
|
||||
#endif
|
||||
|
||||
#endif // MODE_DEVICE_SUPPORTED
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user