Merge branch 'pigrew-ZLP_Request2' into cr1901-msp430f5529
This commit is contained in:
@@ -79,7 +79,7 @@ typedef struct TU_ATTR_ALIGNED(4)
|
||||
};
|
||||
} dcd_event_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct");
|
||||
//TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct");
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Device API
|
||||
@@ -119,20 +119,51 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr);
|
||||
// clear stall, data toggle is also reset to DATA0
|
||||
void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Event Function
|
||||
* Called by DCD to notify device stack
|
||||
*------------------------------------------------------------------*/
|
||||
void dcd_event_handler(dcd_event_t const * event, bool in_isr);
|
||||
//--------------------------------------------------------------------+
|
||||
// Event API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Called by DCD to notify device stack
|
||||
extern void dcd_event_handler(dcd_event_t const * event, bool in_isr);
|
||||
|
||||
// helper to send bus signal event
|
||||
void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr);
|
||||
static inline void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr);
|
||||
|
||||
// helper to send setup received
|
||||
void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr);
|
||||
static inline void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr);
|
||||
|
||||
// helper to send transfer complete event
|
||||
void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr);
|
||||
static inline void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Inline helper
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
static inline void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr)
|
||||
{
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = eid, };
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
static inline void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr)
|
||||
{
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SETUP_RECEIVED };
|
||||
memcpy(&event.setup_received, setup, 8);
|
||||
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
static inline void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr)
|
||||
{
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_XFER_COMPLETE };
|
||||
|
||||
event.xfer_complete.ep_addr = ep_addr;
|
||||
event.xfer_complete.len = xferred_bytes;
|
||||
event.xfer_complete.result = result;
|
||||
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -168,6 +168,20 @@ static usbd_class_driver_t const usbd_class_drivers[] =
|
||||
.sof = NULL
|
||||
},
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_DFU_RT
|
||||
{
|
||||
.class_code = TUD_DFU_APP_CLASS,
|
||||
//.subclass_code = TUD_DFU_APP_SUBCLASS
|
||||
.init = dfu_rtd_init,
|
||||
.reset = dfu_rtd_reset,
|
||||
.open = dfu_rtd_open,
|
||||
.control_request = dfu_rtd_control_request,
|
||||
.control_complete = dfu_rtd_control_complete,
|
||||
.xfer_cb = dfu_rtd_xfer_cb,
|
||||
.sof = NULL
|
||||
},
|
||||
#endif
|
||||
};
|
||||
|
||||
enum { USBD_CLASS_DRIVER_COUNT = TU_ARRAY_SIZE(usbd_class_drivers) };
|
||||
@@ -277,7 +291,7 @@ bool tud_remote_wakeup(void)
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD Task
|
||||
//--------------------------------------------------------------------+
|
||||
bool usbd_init (void)
|
||||
bool tud_init (void)
|
||||
{
|
||||
TU_LOG2("USBD init\r\n");
|
||||
|
||||
@@ -391,7 +405,6 @@ void tud_task (void)
|
||||
|
||||
if ( 0 == epnum )
|
||||
{
|
||||
// control transfer DATA stage callback
|
||||
usbd_control_xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
|
||||
}
|
||||
else
|
||||
@@ -783,6 +796,8 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
|
||||
case TUSB_DESC_CONFIGURATION:
|
||||
{
|
||||
tusb_desc_configuration_t const* desc_config = (tusb_desc_configuration_t const*) tud_descriptor_configuration_cb(desc_index);
|
||||
TU_ASSERT(desc_config);
|
||||
|
||||
uint16_t total_len;
|
||||
memcpy(&total_len, &desc_config->wTotalLength, 2); // possibly mis-aligned memory
|
||||
|
||||
@@ -866,10 +881,6 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr)
|
||||
break;
|
||||
|
||||
case DCD_EVENT_XFER_COMPLETE:
|
||||
// skip zero-length control status complete event, should DCD notify us.
|
||||
// TODO could cause issue with actual zero length data used by class such as DFU
|
||||
if ( (0 == tu_edpt_number(event->xfer_complete.ep_addr)) && (event->xfer_complete.len == 0) ) break;
|
||||
|
||||
osal_queue_send(_usbd_q, event, in_isr);
|
||||
TU_ASSERT(event->xfer_complete.result == XFER_RESULT_SUCCESS,);
|
||||
break;
|
||||
@@ -883,34 +894,6 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr)
|
||||
}
|
||||
}
|
||||
|
||||
// helper to send bus signal event
|
||||
void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr)
|
||||
{
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = eid, };
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
// helper to send setup received
|
||||
void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr)
|
||||
{
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SETUP_RECEIVED };
|
||||
memcpy(&event.setup_received, setup, 8);
|
||||
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
// helper to send transfer complete event
|
||||
void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr)
|
||||
{
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_XFER_COMPLETE };
|
||||
|
||||
event.xfer_complete.ep_addr = ep_addr;
|
||||
event.xfer_complete.len = xferred_bytes;
|
||||
event.xfer_complete.result = result;
|
||||
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Helper
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
@@ -41,6 +41,9 @@
|
||||
// Application API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Init device stack
|
||||
bool tud_init (void);
|
||||
|
||||
// Task function should be called in main/rtos loop
|
||||
void tud_task (void);
|
||||
|
||||
@@ -304,6 +307,21 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re
|
||||
/* Endpoint In */\
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
|
||||
|
||||
//------------- DFU Runtime -------------//
|
||||
#define TUD_DFU_APP_CLASS (TUSB_CLASS_APPLICATION_SPECIFIC)
|
||||
#define TUD_DFU_APP_SUBCLASS 0x01u
|
||||
|
||||
// Length of template descriptr: 18 bytes
|
||||
#define TUD_DFU_RT_DESC_LEN (9 + 9)
|
||||
|
||||
// DFU runtime descriptor
|
||||
// Interface number, string index, attributes, detach timeout, transfer size
|
||||
#define TUD_DFU_RT_DESCRIPTOR(_itfnum, _stridx, _attr, _timeout, _xfer_size) \
|
||||
/* Interface */ \
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 0, TUD_DFU_APP_CLASS, TUD_DFU_APP_SUBCLASS, DFU_PROTOCOL_RT, _stridx, \
|
||||
/* Function */ \
|
||||
9, DFU_DESC_FUNCTIONAL, _attr, U16_TO_U8S_LE(_timeout), U16_TO_U8S_LE(_xfer_size), U16_TO_U8S_LE(0x0101)
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -42,23 +42,21 @@ typedef struct
|
||||
{
|
||||
tusb_control_request_t request;
|
||||
|
||||
void* buffer;
|
||||
uint16_t len;
|
||||
uint16_t total_transferred;
|
||||
uint16_t requested_len;
|
||||
uint8_t* buffer;
|
||||
uint16_t data_len;
|
||||
uint16_t total_xferred;
|
||||
|
||||
bool (*complete_cb) (uint8_t, tusb_control_request_t const *);
|
||||
} usbd_control_xfer_t;
|
||||
|
||||
static usbd_control_xfer_t _control_state;
|
||||
static usbd_control_xfer_t _ctrl_xfer;
|
||||
|
||||
CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN uint8_t _usbd_ctrl_buf[CFG_TUD_ENDPOINT0_SIZE];
|
||||
|
||||
void usbd_control_reset (uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
tu_varclr(&_control_state);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request)
|
||||
{
|
||||
@@ -66,47 +64,37 @@ bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request)
|
||||
return dcd_edpt_xfer(rhport, request->bmRequestType_bit.direction ? EDPT_CTRL_OUT : EDPT_CTRL_IN, NULL, 0);
|
||||
}
|
||||
|
||||
// Each transaction is up to endpoint0's max packet size
|
||||
static bool start_control_data_xact(uint8_t rhport)
|
||||
// Transfer an transaction in Data Stage
|
||||
// Each transaction has up to Endpoint0's max packet size.
|
||||
// This function can also transfer an zero-length packet
|
||||
static bool _data_stage_xact(uint8_t rhport)
|
||||
{
|
||||
uint16_t const xact_len = tu_min16(_control_state.len - _control_state.total_transferred, CFG_TUD_ENDPOINT0_SIZE);
|
||||
uint16_t const xact_len = tu_min16(_ctrl_xfer.data_len - _ctrl_xfer.total_xferred, CFG_TUD_ENDPOINT0_SIZE);
|
||||
|
||||
uint8_t ep_addr = EDPT_CTRL_OUT;
|
||||
|
||||
if ( _control_state.request.bmRequestType_bit.direction == TUSB_DIR_IN )
|
||||
if ( _ctrl_xfer.request.bmRequestType_bit.direction == TUSB_DIR_IN )
|
||||
{
|
||||
ep_addr = EDPT_CTRL_IN;
|
||||
memcpy(_usbd_ctrl_buf, _control_state.buffer, xact_len);
|
||||
if ( xact_len ) memcpy(_usbd_ctrl_buf, _ctrl_xfer.buffer, xact_len);
|
||||
}
|
||||
|
||||
return dcd_edpt_xfer(rhport, ep_addr, _usbd_ctrl_buf, xact_len);
|
||||
}
|
||||
|
||||
// TODO may find a better way
|
||||
void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) )
|
||||
{
|
||||
_control_state.complete_cb = fp;
|
||||
return dcd_edpt_xfer(rhport, ep_addr, xact_len ? _usbd_ctrl_buf : NULL, xact_len);
|
||||
}
|
||||
|
||||
bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len)
|
||||
{
|
||||
// transmitted length must be <= requested length (USB 2.0 spec: 8.5.3.1 )
|
||||
// FIXME: Should logic be here or in place that calls this function?
|
||||
if(len > request->wLength)
|
||||
len = request->wLength;
|
||||
_ctrl_xfer.request = (*request);
|
||||
_ctrl_xfer.buffer = (uint8_t*) buffer;
|
||||
_ctrl_xfer.total_xferred = 0;
|
||||
_ctrl_xfer.data_len = tu_min16(len, request->wLength);
|
||||
|
||||
_control_state.request = (*request);
|
||||
_control_state.buffer = buffer;
|
||||
_control_state.total_transferred = 0;
|
||||
_control_state.requested_len = request->wLength;
|
||||
_control_state.len = len;
|
||||
|
||||
if ( len )
|
||||
if ( _ctrl_xfer.data_len )
|
||||
{
|
||||
TU_ASSERT(buffer);
|
||||
|
||||
// Data stage
|
||||
TU_ASSERT( start_control_data_xact(rhport) );
|
||||
TU_ASSERT( _data_stage_xact(rhport) );
|
||||
}else
|
||||
{
|
||||
// Status stage
|
||||
@@ -116,38 +104,61 @@ bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, vo
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
void usbd_control_reset (uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
tu_varclr(&_ctrl_xfer);
|
||||
}
|
||||
|
||||
// TODO may find a better way
|
||||
void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) )
|
||||
{
|
||||
_ctrl_xfer.complete_cb = fp;
|
||||
}
|
||||
|
||||
// callback when a transaction complete on DATA stage of control endpoint
|
||||
bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
|
||||
{
|
||||
(void) result;
|
||||
(void) ep_addr;
|
||||
|
||||
if ( _control_state.request.bmRequestType_bit.direction == TUSB_DIR_OUT )
|
||||
// Endpoint Address is opposite to direction bit, this is Status Stage complete event
|
||||
if ( tu_edpt_dir(ep_addr) != _ctrl_xfer.request.bmRequestType_bit.direction )
|
||||
{
|
||||
TU_VERIFY(_control_state.buffer);
|
||||
memcpy(_control_state.buffer, _usbd_ctrl_buf, xferred_bytes);
|
||||
TU_ASSERT(0 == xferred_bytes);
|
||||
return true;
|
||||
}
|
||||
|
||||
_control_state.total_transferred += xferred_bytes;
|
||||
_control_state.buffer = ((uint8_t*)_control_state.buffer) + xferred_bytes;
|
||||
if ( _ctrl_xfer.request.bmRequestType_bit.direction == TUSB_DIR_OUT )
|
||||
{
|
||||
TU_VERIFY(_ctrl_xfer.buffer);
|
||||
memcpy(_ctrl_xfer.buffer, _usbd_ctrl_buf, xferred_bytes);
|
||||
}
|
||||
|
||||
if ( (_control_state.requested_len == _control_state.total_transferred) || xferred_bytes < CFG_TUD_ENDPOINT0_SIZE )
|
||||
_ctrl_xfer.total_xferred += xferred_bytes;
|
||||
_ctrl_xfer.buffer += xferred_bytes;
|
||||
|
||||
// Data Stage is complete when all request's length are transferred or
|
||||
// a short packet is sent including zero-length packet.
|
||||
if ( (_ctrl_xfer.request.wLength == _ctrl_xfer.total_xferred) || xferred_bytes < CFG_TUD_ENDPOINT0_SIZE )
|
||||
{
|
||||
// DATA stage is complete
|
||||
bool is_ok = true;
|
||||
|
||||
// invoke complete callback if set
|
||||
// callback can still stall control in status phase e.g out data does not make sense
|
||||
if ( _control_state.complete_cb )
|
||||
if ( _ctrl_xfer.complete_cb )
|
||||
{
|
||||
is_ok = _control_state.complete_cb(rhport, &_control_state.request);
|
||||
is_ok = _ctrl_xfer.complete_cb(rhport, &_ctrl_xfer.request);
|
||||
}
|
||||
|
||||
if ( is_ok )
|
||||
{
|
||||
// Send status
|
||||
TU_ASSERT( tud_control_status(rhport, &_control_state.request) );
|
||||
TU_ASSERT( tud_control_status(rhport, &_ctrl_xfer.request) );
|
||||
}else
|
||||
{
|
||||
// Stall both IN and OUT control endpoint
|
||||
@@ -158,7 +169,7 @@ bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result
|
||||
else
|
||||
{
|
||||
// More data to transfer
|
||||
TU_ASSERT( start_control_data_xact(rhport) );
|
||||
TU_ASSERT( _data_stage_xact(rhport) );
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -33,8 +33,6 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
bool usbd_init (void);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD Endpoint API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
Reference in New Issue
Block a user