Merge pull request #2526 from hathach/fix-uvc-hardfault

Fix uvc hardfault
This commit is contained in:
Ha Thach 2024-03-22 21:23:14 +07:00 committed by GitHub
commit 67cd8349d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 262 additions and 273 deletions

View File

@ -160,22 +160,23 @@ typedef enum {
/* A.9.1 VideoControl Interface Control Selectors */ /* A.9.1 VideoControl Interface Control Selectors */
typedef enum { typedef enum {
VIDEO_VC_CTL_UNDEFINED = 0x00, VIDEO_VC_CTL_UNDEFINED = 0x00,
VIDEO_VC_CTL_VIDEO_POWER_MODE, VIDEO_VC_CTL_VIDEO_POWER_MODE, // 0x01
VIDEO_VC_CTL_REQUEST_ERROR_CODE, VIDEO_VC_CTL_REQUEST_ERROR_CODE, // 0x02
} video_interface_control_selector_t; } video_interface_control_selector_t;
/* A.9.8 VideoStreaming Interface Control Selectors */ /* A.9.8 VideoStreaming Interface Control Selectors */
typedef enum { typedef enum {
VIDEO_VS_CTL_UNDEFINED = 0x00, VIDEO_VS_CTL_UNDEFINED = 0x00,
VIDEO_VS_CTL_PROBE, VIDEO_VS_CTL_PROBE, // 0x01
VIDEO_VS_CTL_COMMIT, VIDEO_VS_CTL_COMMIT, // 0x02
VIDEO_VS_CTL_STILL_PROBE, VIDEO_VS_CTL_STILL_PROBE, // 0x03
VIDEO_VS_CTL_STILL_COMMIT, VIDEO_VS_CTL_STILL_COMMIT, // 0x04
VIDEO_VS_CTL_STILL_IMAGE_TRIGGER, VIDEO_VS_CTL_STILL_IMAGE_TRIGGER, // 0x05
VIDEO_VS_CTL_STREAM_ERROR_CODE, VIDEO_VS_CTL_STREAM_ERROR_CODE, // 0x06
VIDEO_VS_CTL_GENERATE_KEY_FRAME, VIDEO_VS_CTL_GENERATE_KEY_FRAME, // 0x07
VIDEO_VS_CTL_UPDATE_FRAME_SEGMENT, VIDEO_VS_CTL_UPDATE_FRAME_SEGMENT, // 0x08
VIDEO_VS_CTL_SYNCH_DELAY_CONTROL, VIDEO_VS_CTL_SYNCH_DELAY_CONTROL, // 0x09
} video_interface_streaming_selector_t; } video_interface_streaming_selector_t;
/* B. Terminal Types */ /* B. Terminal Types */

View File

@ -114,6 +114,8 @@ typedef struct TU_ATTR_PACKED {
uint32_t max_payload_transfer_size; uint32_t max_payload_transfer_size;
uint8_t error_code;/* error code */ uint8_t error_code;/* error code */
uint8_t state; /* 0:probing 1:committed 2:streaming */ uint8_t state; /* 0:probing 1:committed 2:streaming */
video_probe_and_commit_control_t probe_commit_payload; /* Probe and Commit control */
/*------------- From this point, data is not cleared by bus reset -------------*/ /*------------- From this point, data is not cleared by bus reset -------------*/
CFG_TUSB_MEM_ALIGN uint8_t ep_buf[CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE]; /* EP transfer buffer for streaming */ CFG_TUSB_MEM_ALIGN uint8_t ep_buf[CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE]; /* EP transfer buffer for streaming */
} videod_streaming_interface_t; } videod_streaming_interface_t;
@ -143,13 +145,65 @@ CFG_TUD_MEM_SECTION tu_static videod_streaming_interface_t _videod_streaming_itf
tu_static uint8_t const _cap_get = 0x1u; /* support for GET */ tu_static uint8_t const _cap_get = 0x1u; /* support for GET */
tu_static uint8_t const _cap_get_set = 0x3u; /* support for GET and SET */ tu_static uint8_t const _cap_get_set = 0x3u; /* support for GET and SET */
//--------------------------------------------------------------------+
// Debug
//--------------------------------------------------------------------+
#if CFG_TUSB_DEBUG >= CFG_TUD_VIDEO_LOG_LEVEL
static tu_lookup_entry_t const tu_lookup_video_request[] = {
{.key = VIDEO_REQUEST_UNDEFINED, .data = "Undefined"},
{.key = VIDEO_REQUEST_SET_CUR, .data = "SetCur"},
{.key = VIDEO_REQUEST_SET_CUR_ALL, .data = "SetCurAll"},
{.key = VIDEO_REQUEST_GET_CUR, .data = "GetCur"},
{.key = VIDEO_REQUEST_GET_MIN, .data = "GetMin"},
{.key = VIDEO_REQUEST_GET_MAX, .data = "GetMax"},
{.key = VIDEO_REQUEST_GET_RES, .data = "GetRes"},
{.key = VIDEO_REQUEST_GET_LEN, .data = "GetLen"},
{.key = VIDEO_REQUEST_GET_INFO, .data = "GetInfo"},
{.key = VIDEO_REQUEST_GET_DEF, .data = "GetDef"},
{.key = VIDEO_REQUEST_GET_CUR_ALL, .data = "GetCurAll"},
{.key = VIDEO_REQUEST_GET_MIN_ALL, .data = "GetMinAll"},
{.key = VIDEO_REQUEST_GET_MAX_ALL, .data = "GetMaxAll"},
{.key = VIDEO_REQUEST_GET_RES_ALL, .data = "GetResAll"},
{.key = VIDEO_REQUEST_GET_DEF_ALL, .data = "GetDefAll"},
};
static tu_lookup_table_t const tu_table_video_request = {
.count = TU_ARRAY_SIZE(tu_lookup_video_request),
.items = tu_lookup_video_request
};
static char const* const tu_str_video_vc_control_selector[] = {
"Undefined",
"Video Power Mode",
"Request Error Code",
};
static char const* const tu_str_video_vs_control_selector[] = {
"Undefined",
"Probe",
"Commit",
"Still Probe",
"Still Commit",
"Still Image Trigger",
"Stream Error Code",
"Generate Key Frame",
"Update Frame Segment",
"Sync Delay",
};
#endif
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
/** Get interface number from the interface descriptor /** Get interface number from the interface descriptor
* *
* @param[in] desc interface descriptor * @param[in] desc interface descriptor
* *
* @return bInterfaceNumber */ * @return bInterfaceNumber */
static inline uint8_t _desc_itfnum(void const *desc) static inline uint8_t _desc_itfnum(void const *desc) {
{
return ((uint8_t const*)desc)[2]; return ((uint8_t const*)desc)[2];
} }
@ -158,8 +212,7 @@ static inline uint8_t _desc_itfnum(void const *desc)
* @param[in] desc endpoint descriptor * @param[in] desc endpoint descriptor
* *
* @return bEndpointAddress */ * @return bEndpointAddress */
static inline uint8_t _desc_ep_addr(void const *desc) static inline uint8_t _desc_ep_addr(void const *desc) {
{
return ((uint8_t const*)desc)[2]; return ((uint8_t const*)desc)[2];
} }
@ -169,8 +222,7 @@ static inline uint8_t _desc_ep_addr(void const *desc)
* @param[in] stm_idx index number of streaming interface * @param[in] stm_idx index number of streaming interface
* *
* @return instance */ * @return instance */
static videod_streaming_interface_t* _get_instance_streaming(uint_fast8_t ctl_idx, uint_fast8_t stm_idx) static videod_streaming_interface_t* _get_instance_streaming(uint_fast8_t ctl_idx, uint_fast8_t stm_idx) {
{
videod_interface_t *ctl = &_videod_itf[ctl_idx]; videod_interface_t *ctl = &_videod_itf[ctl_idx];
if (!ctl->beg) return NULL; if (!ctl->beg) return NULL;
videod_streaming_interface_t *stm = &_videod_streaming_itf[ctl->stm[stm_idx]]; videod_streaming_interface_t *stm = &_videod_streaming_itf[ctl->stm[stm_idx]];
@ -178,13 +230,11 @@ static videod_streaming_interface_t* _get_instance_streaming(uint_fast8_t ctl_id
return stm; return stm;
} }
static tusb_desc_vc_itf_t const* _get_desc_vc(videod_interface_t const *self) static tusb_desc_vc_itf_t const* _get_desc_vc(videod_interface_t const *self) {
{
return (tusb_desc_vc_itf_t const *)(self->beg + self->cur); return (tusb_desc_vc_itf_t const *)(self->beg + self->cur);
} }
static tusb_desc_vs_itf_t const* _get_desc_vs(videod_streaming_interface_t const *self) static tusb_desc_vs_itf_t const* _get_desc_vs(videod_streaming_interface_t const *self) {
{
if (!self->desc.cur) return NULL; if (!self->desc.cur) return NULL;
uint8_t const *desc = _videod_itf[self->index_vc].beg; uint8_t const *desc = _videod_itf[self->index_vc].beg;
return (tusb_desc_vs_itf_t const*)(desc + self->desc.cur); return (tusb_desc_vs_itf_t const*)(desc + self->desc.cur);
@ -198,8 +248,7 @@ static tusb_desc_vs_itf_t const* _get_desc_vs(videod_streaming_interface_t const
* *
* @return The pointer for interface descriptor. * @return The pointer for interface descriptor.
* @retval end did not found interface descriptor */ * @retval end did not found interface descriptor */
static void const* _find_desc(void const *beg, void const *end, uint_fast8_t desc_type) static void const* _find_desc(void const *beg, void const *end, uint_fast8_t desc_type) {
{
void const *cur = beg; void const *cur = beg;
while ((cur < end) && (desc_type != tu_desc_type(cur))) { while ((cur < end) && (desc_type != tu_desc_type(cur))) {
cur = tu_desc_next(cur); cur = tu_desc_next(cur);
@ -238,8 +287,7 @@ static void const* _find_desc_2_type(void const *beg, void const *end, uint_fast
static void const* _find_desc_3(void const *beg, void const *end, static void const* _find_desc_3(void const *beg, void const *end,
uint_fast8_t desc_type, uint_fast8_t desc_type,
uint_fast8_t element_0, uint_fast8_t element_0,
uint_fast8_t element_1) uint_fast8_t element_1) {
{
for (void const *cur = beg; cur < end; cur = _find_desc(cur, end, desc_type)) { for (void const *cur = beg; cur < end; cur = _find_desc(cur, end, desc_type)) {
uint8_t const *p = (uint8_t const *)cur; uint8_t const *p = (uint8_t const *)cur;
if ((p[2] == element_0) && (p[3] == element_1)) { if ((p[2] == element_0) && (p[3] == element_1)) {
@ -261,8 +309,7 @@ static void const* _find_desc_3(void const *beg, void const *end,
* *
* @return The pointer for interface descriptor. * @return The pointer for interface descriptor.
* @retval end did not found interface descriptor */ * @retval end did not found interface descriptor */
static void const* _next_desc_itf(void const *beg, void const *end) static void const* _next_desc_itf(void const *beg, void const *end) {
{
void const *cur = beg; void const *cur = beg;
uint_fast8_t itfnum = ((tusb_desc_interface_t const*)cur)->bInterfaceNumber; uint_fast8_t itfnum = ((tusb_desc_interface_t const*)cur)->bInterfaceNumber;
while ((cur < end) && while ((cur < end) &&
@ -413,8 +460,10 @@ static bool _update_streaming_parameters(videod_streaming_interface_t const *stm
case VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED: case VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED:
param->wCompQuality = 1; /* 1 to 10000 */ param->wCompQuality = 1; /* 1 to 10000 */
break; break;
case VIDEO_CS_ITF_VS_FORMAT_MJPEG:
case VIDEO_CS_ITF_VS_FORMAT_MJPEG:
break; break;
default: return false; default: return false;
} }
@ -435,9 +484,11 @@ static bool _update_streaming_parameters(videod_streaming_interface_t const *stm
case VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED: case VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED:
frame_size = (uint_fast32_t)frm->wWidth * frm->wHeight * fmt->uncompressed.bBitsPerPixel / 8; frame_size = (uint_fast32_t)frm->wWidth * frm->wHeight * fmt->uncompressed.bBitsPerPixel / 8;
break; break;
case VIDEO_CS_ITF_VS_FORMAT_MJPEG: case VIDEO_CS_ITF_VS_FORMAT_MJPEG:
frame_size = (uint_fast32_t)frm->wWidth * frm->wHeight * 16 / 8; /* YUV422 */ frame_size = (uint_fast32_t)frm->wWidth * frm->wHeight * 16 / 8; /* YUV422 */
break; break;
default: break; default: break;
} }
param->dwMaxVideoFrameSize = frame_size; param->dwMaxVideoFrameSize = frame_size;
@ -478,10 +529,12 @@ static bool _negotiate_streaming_parameters(videod_streaming_interface_t const *
if (_get_desc_vs(stm)) if (_get_desc_vs(stm))
param->bFormatIndex = _get_desc_vs(stm)->stm.bNumFormats; param->bFormatIndex = _get_desc_vs(stm)->stm.bNumFormats;
break; break;
case VIDEO_REQUEST_GET_MIN: case VIDEO_REQUEST_GET_MIN:
case VIDEO_REQUEST_GET_DEF: case VIDEO_REQUEST_GET_DEF:
param->bFormatIndex = 1; param->bFormatIndex = 1;
break; break;
default: return false; default: return false;
} }
/* Set the parameters determined by the format */ /* Set the parameters determined by the format */
@ -510,18 +563,22 @@ static bool _negotiate_streaming_parameters(videod_streaming_interface_t const *
case VIDEO_REQUEST_GET_MAX: case VIDEO_REQUEST_GET_MAX:
frmnum = fmt->bNumFrameDescriptors; frmnum = fmt->bNumFrameDescriptors;
break; break;
case VIDEO_REQUEST_GET_MIN: case VIDEO_REQUEST_GET_MIN:
frmnum = 1; frmnum = 1;
break; break;
case VIDEO_REQUEST_GET_DEF: case VIDEO_REQUEST_GET_DEF:
switch (fmt->bDescriptorSubType) { switch (fmt->bDescriptorSubType) {
case VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED: case VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED:
frmnum = fmt->uncompressed.bDefaultFrameIndex; frmnum = fmt->uncompressed.bDefaultFrameIndex;
break; break;
case VIDEO_CS_ITF_VS_FORMAT_MJPEG:
frmnum = fmt->mjpeg.bDefaultFrameIndex; case VIDEO_CS_ITF_VS_FORMAT_MJPEG:
break; frmnum = fmt->mjpeg.bDefaultFrameIndex;
default: return false; break;
default: return false;
} }
break; break;
default: return false; default: return false;
@ -534,9 +591,11 @@ static bool _negotiate_streaming_parameters(videod_streaming_interface_t const *
case VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED: case VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED:
frame_size = (uint_fast32_t)frm->wWidth * frm->wHeight * fmt->uncompressed.bBitsPerPixel / 8; frame_size = (uint_fast32_t)frm->wWidth * frm->wHeight * fmt->uncompressed.bBitsPerPixel / 8;
break; break;
case VIDEO_CS_ITF_VS_FORMAT_MJPEG: case VIDEO_CS_ITF_VS_FORMAT_MJPEG:
frame_size = (uint_fast32_t)frm->wWidth * frm->wHeight * 16 / 8; /* YUV422 */ frame_size = (uint_fast32_t)frm->wWidth * frm->wHeight * 16 / 8; /* YUV422 */
break; break;
default: return false; default: return false;
} }
param->dwMaxVideoFrameSize = frame_size; param->dwMaxVideoFrameSize = frame_size;
@ -552,42 +611,43 @@ static bool _negotiate_streaming_parameters(videod_streaming_interface_t const *
uint_fast32_t interval, interval_ms; uint_fast32_t interval, interval_ms;
switch (request) { switch (request) {
case VIDEO_REQUEST_GET_MAX: case VIDEO_REQUEST_GET_MAX: {
{ uint_fast32_t min_interval, max_interval;
uint_fast32_t min_interval, max_interval; uint_fast8_t num_intervals = frm->uncompressed.bFrameIntervalType;
uint_fast8_t num_intervals = frm->uncompressed.bFrameIntervalType; max_interval = num_intervals ? frm->uncompressed.dwFrameInterval[num_intervals - 1]: frm->uncompressed.dwFrameInterval[1];
max_interval = num_intervals ? frm->uncompressed.dwFrameInterval[num_intervals - 1]: frm->uncompressed.dwFrameInterval[1]; min_interval = frm->uncompressed.dwFrameInterval[0];
min_interval = frm->uncompressed.dwFrameInterval[0]; interval = max_interval;
interval = max_interval; interval_ms = min_interval / 10000;
interval_ms = min_interval / 10000;
}
break; break;
case VIDEO_REQUEST_GET_MIN: }
{
uint_fast32_t min_interval, max_interval; case VIDEO_REQUEST_GET_MIN: {
uint_fast8_t num_intervals = frm->uncompressed.bFrameIntervalType; uint_fast32_t min_interval, max_interval;
max_interval = num_intervals ? frm->uncompressed.dwFrameInterval[num_intervals - 1]: frm->uncompressed.dwFrameInterval[1]; uint_fast8_t num_intervals = frm->uncompressed.bFrameIntervalType;
min_interval = frm->uncompressed.dwFrameInterval[0]; max_interval = num_intervals ? frm->uncompressed.dwFrameInterval[num_intervals - 1]: frm->uncompressed.dwFrameInterval[1];
interval = min_interval; min_interval = frm->uncompressed.dwFrameInterval[0];
interval_ms = max_interval / 10000; interval = min_interval;
} interval_ms = max_interval / 10000;
break; break;
}
case VIDEO_REQUEST_GET_DEF: case VIDEO_REQUEST_GET_DEF:
interval = frm->uncompressed.dwDefaultFrameInterval; interval = frm->uncompressed.dwDefaultFrameInterval;
interval_ms = interval / 10000; interval_ms = interval / 10000;
break; break;
case VIDEO_REQUEST_GET_RES:
{ case VIDEO_REQUEST_GET_RES: {
uint_fast8_t num_intervals = frm->uncompressed.bFrameIntervalType; uint_fast8_t num_intervals = frm->uncompressed.bFrameIntervalType;
if (num_intervals) { if (num_intervals) {
interval = 0; interval = 0;
interval_ms = 0; interval_ms = 0;
} else { } else {
interval = frm->uncompressed.dwFrameInterval[2]; interval = frm->uncompressed.dwFrameInterval[2];
interval_ms = interval / 10000; interval_ms = interval / 10000;
}
} }
break; break;
}
default: return false; default: return false;
} }
param->dwFrameInterval = interval; param->dwFrameInterval = interval;
@ -676,13 +736,11 @@ static bool _open_vc_itf(uint8_t rhport, videod_interface_t *self, uint_fast8_t
return true; return true;
} }
static bool _init_vs_configuration(videod_streaming_interface_t *stm) static bool _init_vs_configuration(videod_streaming_interface_t *stm) {
{
/* initialize streaming settings */ /* initialize streaming settings */
stm->state = VS_STATE_PROBING; stm->state = VS_STATE_PROBING;
stm->max_payload_transfer_size = 0; stm->max_payload_transfer_size = 0;
video_probe_and_commit_control_t *param = video_probe_and_commit_control_t *param = &stm->probe_commit_payload;
(video_probe_and_commit_control_t *)&stm->ep_buf;
tu_memclr(param, sizeof(*param)); tu_memclr(param, sizeof(*param));
return _update_streaming_parameters(stm, param); return _update_streaming_parameters(stm, param);
} }
@ -759,6 +817,7 @@ static uint_fast16_t _prepare_in_payload(videod_streaming_interface_t *stm)
if (hdr_len + remaining < pkt_len) { if (hdr_len + remaining < pkt_len) {
pkt_len = hdr_len + remaining; pkt_len = hdr_len + remaining;
} }
TU_ASSERT(pkt_len >= hdr_len);
uint_fast16_t data_len = pkt_len - hdr_len; uint_fast16_t data_len = pkt_len - hdr_len;
memcpy(&stm->ep_buf[hdr_len], stm->buffer + stm->offset, data_len); memcpy(&stm->ep_buf[hdr_len], stm->buffer + stm->offset, data_len);
stm->offset += data_len; stm->offset += data_len;
@ -775,6 +834,7 @@ static int handle_video_ctl_std_req(uint8_t rhport, uint8_t stage,
tusb_control_request_t const *request, tusb_control_request_t const *request,
uint_fast8_t ctl_idx) uint_fast8_t ctl_idx)
{ {
TU_LOG_DRV("\r\n");
switch (request->bRequest) { switch (request->bRequest) {
case TUSB_REQ_GET_INTERFACE: case TUSB_REQ_GET_INTERFACE:
if (stage == CONTROL_STAGE_SETUP) if (stage == CONTROL_STAGE_SETUP)
@ -812,7 +872,10 @@ static int handle_video_ctl_cs_req(uint8_t rhport, uint8_t stage,
videod_interface_t *self = &_videod_itf[ctl_idx]; videod_interface_t *self = &_videod_itf[ctl_idx];
/* 4.2.1 Interface Control Request */ /* 4.2.1 Interface Control Request */
switch (TU_U16_HIGH(request->wValue)) { uint8_t const ctrl_sel = TU_U16_HIGH(request->wValue);
TU_LOG_DRV("%s_Control(%s)\r\n", tu_str_video_vc_control_selector[ctrl_sel], tu_lookup_find(&tu_table_video_request, request->bRequest));
switch (ctrl_sel) {
case VIDEO_VC_CTL_VIDEO_POWER_MODE: case VIDEO_VC_CTL_VIDEO_POWER_MODE:
switch (request->bRequest) { switch (request->bRequest) {
case VIDEO_REQUEST_SET_CUR: case VIDEO_REQUEST_SET_CUR:
@ -876,19 +939,19 @@ static int handle_video_ctl_req(uint8_t rhport, uint8_t stage,
tusb_control_request_t const *request, tusb_control_request_t const *request,
uint_fast8_t ctl_idx) uint_fast8_t ctl_idx)
{ {
uint_fast8_t entity_id;
switch (request->bmRequestType_bit.type) { switch (request->bmRequestType_bit.type) {
case TUSB_REQ_TYPE_STANDARD: case TUSB_REQ_TYPE_STANDARD:
return handle_video_ctl_std_req(rhport, stage, request, ctl_idx); return handle_video_ctl_std_req(rhport, stage, request, ctl_idx);
case TUSB_REQ_TYPE_CLASS: case TUSB_REQ_TYPE_CLASS: {
entity_id = TU_U16_HIGH(request->wIndex); uint_fast8_t entity_id = TU_U16_HIGH(request->wIndex);
if (!entity_id) { if (!entity_id) {
return handle_video_ctl_cs_req(rhport, stage, request, ctl_idx); return handle_video_ctl_cs_req(rhport, stage, request, ctl_idx);
} else { } else {
TU_VERIFY(_find_desc_entity(_get_desc_vc(&_videod_itf[ctl_idx]), entity_id), VIDEO_ERROR_INVALID_REQUEST); TU_VERIFY(_find_desc_entity(_get_desc_vc(&_videod_itf[ctl_idx]), entity_id), VIDEO_ERROR_INVALID_REQUEST);
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
} }
}
default: default:
return VIDEO_ERROR_INVALID_REQUEST; return VIDEO_ERROR_INVALID_REQUEST;
@ -899,6 +962,7 @@ static int handle_video_stm_std_req(uint8_t rhport, uint8_t stage,
tusb_control_request_t const *request, tusb_control_request_t const *request,
uint_fast8_t stm_idx) uint_fast8_t stm_idx)
{ {
TU_LOG_DRV("\r\n");
videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx]; videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx];
switch (request->bRequest) { switch (request->bRequest) {
case TUSB_REQ_GET_INTERFACE: case TUSB_REQ_GET_INTERFACE:
@ -914,8 +978,7 @@ static int handle_video_stm_std_req(uint8_t rhport, uint8_t stage,
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
case TUSB_REQ_SET_INTERFACE: case TUSB_REQ_SET_INTERFACE:
if (stage == CONTROL_STAGE_SETUP) if (stage == CONTROL_STAGE_SETUP) {
{
TU_VERIFY(_open_vs_itf(rhport, self, request->wValue), VIDEO_ERROR_UNKNOWN); TU_VERIFY(_open_vs_itf(rhport, self, request->wValue), VIDEO_ERROR_UNKNOWN);
tud_control_status(rhport, request); tud_control_status(rhport, request);
} }
@ -929,26 +992,26 @@ static int handle_video_stm_std_req(uint8_t rhport, uint8_t stage,
static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage, static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
tusb_control_request_t const *request, tusb_control_request_t const *request,
uint_fast8_t stm_idx) uint_fast8_t stm_idx) {
{
(void)rhport; (void)rhport;
videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx]; videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx];
uint8_t const ctrl_sel = TU_U16_HIGH(request->wValue);
TU_LOG_DRV("%s_Control(%s)\r\n", tu_str_video_vs_control_selector[ctrl_sel], tu_lookup_find(&tu_table_video_request, request->bRequest));
/* 4.2.1 Interface Control Request */ /* 4.2.1 Interface Control Request */
switch (TU_U16_HIGH(request->wValue)) { switch (ctrl_sel) {
case VIDEO_VS_CTL_STREAM_ERROR_CODE: case VIDEO_VS_CTL_STREAM_ERROR_CODE:
switch (request->bRequest) { switch (request->bRequest) {
case VIDEO_REQUEST_GET_CUR: case VIDEO_REQUEST_GET_CUR:
if (stage == CONTROL_STAGE_SETUP) if (stage == CONTROL_STAGE_SETUP) {
{
/* TODO */ /* TODO */
TU_VERIFY(tud_control_xfer(rhport, request, &self->error_code, sizeof(uint8_t)), VIDEO_ERROR_UNKNOWN); TU_VERIFY(tud_control_xfer(rhport, request, &self->error_code, sizeof(uint8_t)), VIDEO_ERROR_UNKNOWN);
} }
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
case VIDEO_REQUEST_GET_INFO: case VIDEO_REQUEST_GET_INFO:
if (stage == CONTROL_STAGE_SETUP) if (stage == CONTROL_STAGE_SETUP) {
{
TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)(uintptr_t) &_cap_get, sizeof(_cap_get)), VIDEO_ERROR_UNKNOWN); TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)(uintptr_t) &_cap_get, sizeof(_cap_get)), VIDEO_ERROR_UNKNOWN);
} }
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
@ -960,25 +1023,23 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
case VIDEO_VS_CTL_PROBE: case VIDEO_VS_CTL_PROBE:
if (self->state != VS_STATE_PROBING) { if (self->state != VS_STATE_PROBING) {
self->state = VS_STATE_PROBING; self->state = VS_STATE_PROBING;
_init_vs_configuration(self);
} }
switch (request->bRequest) { switch (request->bRequest) {
case VIDEO_REQUEST_SET_CUR: case VIDEO_REQUEST_SET_CUR:
if (stage == CONTROL_STAGE_SETUP) { if (stage == CONTROL_STAGE_SETUP) {
TU_VERIFY(sizeof(video_probe_and_commit_control_t) >= request->wLength, VIDEO_ERROR_UNKNOWN); TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)),
TU_VERIFY(tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)),
VIDEO_ERROR_UNKNOWN); VIDEO_ERROR_UNKNOWN);
} else if (stage == CONTROL_STAGE_DATA) { } else if (stage == CONTROL_STAGE_DATA) {
TU_VERIFY(_update_streaming_parameters(self, (video_probe_and_commit_control_t*)self->ep_buf), TU_VERIFY(_update_streaming_parameters(self, &self->probe_commit_payload),
VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE); VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
} }
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
case VIDEO_REQUEST_GET_CUR: case VIDEO_REQUEST_GET_CUR:
if (stage == CONTROL_STAGE_SETUP) if (stage == CONTROL_STAGE_SETUP) {
{
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN); TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
TU_VERIFY(tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN); TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
} }
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
@ -986,19 +1047,16 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
case VIDEO_REQUEST_GET_MAX: case VIDEO_REQUEST_GET_MAX:
case VIDEO_REQUEST_GET_RES: case VIDEO_REQUEST_GET_RES:
case VIDEO_REQUEST_GET_DEF: case VIDEO_REQUEST_GET_DEF:
if (stage == CONTROL_STAGE_SETUP) if (stage == CONTROL_STAGE_SETUP) {
{
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN); TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
video_probe_and_commit_control_t tmp; video_probe_and_commit_control_t tmp = self->probe_commit_payload;
tmp = *(video_probe_and_commit_control_t*)&self->ep_buf;
TU_VERIFY(_negotiate_streaming_parameters(self, request->bRequest, &tmp), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE); TU_VERIFY(_negotiate_streaming_parameters(self, request->bRequest, &tmp), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
TU_VERIFY(tud_control_xfer(rhport, request, &tmp, sizeof(tmp)), VIDEO_ERROR_UNKNOWN); TU_VERIFY(tud_control_xfer(rhport, request, &tmp, sizeof(tmp)), VIDEO_ERROR_UNKNOWN);
} }
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
case VIDEO_REQUEST_GET_LEN: case VIDEO_REQUEST_GET_LEN:
if (stage == CONTROL_STAGE_SETUP) if (stage == CONTROL_STAGE_SETUP) {
{
TU_VERIFY(2 == request->wLength, VIDEO_ERROR_UNKNOWN); TU_VERIFY(2 == request->wLength, VIDEO_ERROR_UNKNOWN);
uint16_t len = sizeof(video_probe_and_commit_control_t); uint16_t len = sizeof(video_probe_and_commit_control_t);
TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)&len, sizeof(len)), VIDEO_ERROR_UNKNOWN); TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)&len, sizeof(len)), VIDEO_ERROR_UNKNOWN);
@ -1006,8 +1064,7 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
case VIDEO_REQUEST_GET_INFO: case VIDEO_REQUEST_GET_INFO:
if (stage == CONTROL_STAGE_SETUP) if (stage == CONTROL_STAGE_SETUP) {
{
TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN); TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)(uintptr_t)&_cap_get_set, sizeof(_cap_get_set)), VIDEO_ERROR_UNKNOWN); TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)(uintptr_t)&_cap_get_set, sizeof(_cap_get_set)), VIDEO_ERROR_UNKNOWN);
} }
@ -1021,10 +1078,9 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
switch (request->bRequest) { switch (request->bRequest) {
case VIDEO_REQUEST_SET_CUR: case VIDEO_REQUEST_SET_CUR:
if (stage == CONTROL_STAGE_SETUP) { if (stage == CONTROL_STAGE_SETUP) {
TU_VERIFY(sizeof(video_probe_and_commit_control_t) >= request->wLength, VIDEO_ERROR_UNKNOWN); TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
TU_VERIFY(tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
} else if (stage == CONTROL_STAGE_DATA) { } else if (stage == CONTROL_STAGE_DATA) {
video_probe_and_commit_control_t *param = (video_probe_and_commit_control_t*)self->ep_buf; video_probe_and_commit_control_t *param = &self->probe_commit_payload;
TU_VERIFY(_update_streaming_parameters(self, param), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE); TU_VERIFY(_update_streaming_parameters(self, param), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
/* Set the negotiated value */ /* Set the negotiated value */
self->max_payload_transfer_size = param->dwMaxPayloadTransferSize; self->max_payload_transfer_size = param->dwMaxPayloadTransferSize;
@ -1046,16 +1102,14 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
case VIDEO_REQUEST_GET_CUR: case VIDEO_REQUEST_GET_CUR:
if (stage == CONTROL_STAGE_SETUP) if (stage == CONTROL_STAGE_SETUP) {
{
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN); TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
TU_VERIFY(tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN); TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
} }
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
case VIDEO_REQUEST_GET_LEN: case VIDEO_REQUEST_GET_LEN:
if (stage == CONTROL_STAGE_SETUP) if (stage == CONTROL_STAGE_SETUP) {
{
TU_VERIFY(2 == request->wLength, VIDEO_ERROR_UNKNOWN); TU_VERIFY(2 == request->wLength, VIDEO_ERROR_UNKNOWN);
uint16_t len = sizeof(video_probe_and_commit_control_t); uint16_t len = sizeof(video_probe_and_commit_control_t);
TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)&len, sizeof(len)), VIDEO_ERROR_UNKNOWN); TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)&len, sizeof(len)), VIDEO_ERROR_UNKNOWN);
@ -1063,8 +1117,7 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
case VIDEO_REQUEST_GET_INFO: case VIDEO_REQUEST_GET_INFO:
if (stage == CONTROL_STAGE_SETUP) if (stage == CONTROL_STAGE_SETUP) {
{
TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN); TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)(uintptr_t) &_cap_get_set, sizeof(_cap_get_set)), VIDEO_ERROR_UNKNOWN); TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)(uintptr_t) &_cap_get_set, sizeof(_cap_get_set)), VIDEO_ERROR_UNKNOWN);
} }
@ -1165,8 +1218,7 @@ bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *bu
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// USBD Driver API // USBD Driver API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void videod_init(void) void videod_init(void) {
{
for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO; ++i) { for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO; ++i) {
videod_interface_t* ctl = &_videod_itf[i]; videod_interface_t* ctl = &_videod_itf[i];
tu_memclr(ctl, sizeof(*ctl)); tu_memclr(ctl, sizeof(*ctl));
@ -1177,8 +1229,7 @@ void videod_init(void)
} }
} }
void videod_reset(uint8_t rhport) void videod_reset(uint8_t rhport) {
{
(void) rhport; (void) rhport;
for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO; ++i) { for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO; ++i) {
videod_interface_t* ctl = &_videod_itf[i]; videod_interface_t* ctl = &_videod_itf[i];
@ -1190,8 +1241,7 @@ void videod_reset(uint8_t rhport)
} }
} }
uint16_t videod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) uint16_t videod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) {
{
TU_VERIFY((TUSB_CLASS_VIDEO == itf_desc->bInterfaceClass) && TU_VERIFY((TUSB_CLASS_VIDEO == itf_desc->bInterfaceClass) &&
(VIDEO_SUBCLASS_CONTROL == itf_desc->bInterfaceSubClass) && (VIDEO_SUBCLASS_CONTROL == itf_desc->bInterfaceSubClass) &&
(VIDEO_ITF_PROTOCOL_15 == itf_desc->bInterfaceProtocol), 0); (VIDEO_ITF_PROTOCOL_15 == itf_desc->bInterfaceProtocol), 0);
@ -1250,8 +1300,7 @@ uint16_t videod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uin
// Invoked when a control transfer occurred on an interface of this class // Invoked when a control transfer occurred on an interface of this class
// Driver response accordingly to the request and the transfer stage (setup/data/ack) // Driver response accordingly to the request and the transfer stage (setup/data/ack)
// return false to stall control endpoint (e.g unsupported request) // return false to stall control endpoint (e.g unsupported request)
bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) {
{
int err; int err;
TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE); TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE);
uint_fast8_t itfnum = tu_u16_low(request->wIndex); uint_fast8_t itfnum = tu_u16_low(request->wIndex);
@ -1264,6 +1313,7 @@ bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_
} }
if (itf < CFG_TUD_VIDEO) { if (itf < CFG_TUD_VIDEO) {
TU_LOG_DRV(" VC[%d]: ", itf);
err = handle_video_ctl_req(rhport, stage, request, itf); err = handle_video_ctl_req(rhport, stage, request, itf);
_videod_itf[itf].error_code = (uint8_t)err; _videod_itf[itf].error_code = (uint8_t)err;
if (err) return false; if (err) return false;
@ -1279,6 +1329,7 @@ bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_
} }
if (itf < CFG_TUD_VIDEO_STREAMING) { if (itf < CFG_TUD_VIDEO_STREAMING) {
TU_LOG_DRV(" VS[%d]: ", itf);
err = handle_video_stm_req(rhport, stage, request, itf); err = handle_video_stm_req(rhport, stage, request, itf);
_videod_streaming_itf[itf].error_code = (uint8_t)err; _videod_streaming_itf[itf].error_code = (uint8_t)err;
if (err) return false; if (err) return false;
@ -1287,8 +1338,7 @@ bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_
return false; return false;
} }
bool videod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) bool videod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
{
(void)result; (void)xferred_bytes; (void)result; (void)xferred_bytes;
/* find streaming handle */ /* find streaming handle */

View File

@ -319,7 +319,7 @@ void usbd_driver_print_control_complete_name(usbd_control_xfer_cb_t callback) {
for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) { for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) {
usbd_class_driver_t const* driver = get_driver(i); usbd_class_driver_t const* driver = get_driver(i);
if (driver && driver->control_xfer_cb == callback) { if (driver && driver->control_xfer_cb == callback) {
TU_LOG_USBD(" %s control complete\r\n", driver->name); TU_LOG_USBD("%s control complete\r\n", driver->name);
return; return;
} }
} }

View File

@ -246,7 +246,7 @@ static void xact_in_dma(uint8_t epnum)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void dcd_init (uint8_t rhport) void dcd_init (uint8_t rhport)
{ {
TU_LOG1("dcd init\r\n"); TU_LOG2("dcd init\r\n");
(void) rhport; (void) rhport;
} }
@ -685,7 +685,7 @@ void dcd_int_handler(uint8_t rhport)
if ( int_status & USBD_INTEN_USBEVENT_Msk ) if ( int_status & USBD_INTEN_USBEVENT_Msk )
{ {
TU_LOG(2, "EVENTCAUSE = 0x%04lX\r\n", NRF_USBD->EVENTCAUSE); TU_LOG(3, "EVENTCAUSE = 0x%04lX\r\n", NRF_USBD->EVENTCAUSE);
enum { EVT_CAUSE_MASK = USBD_EVENTCAUSE_SUSPEND_Msk | USBD_EVENTCAUSE_RESUME_Msk | USBD_EVENTCAUSE_USBWUALLOWED_Msk | USBD_EVENTCAUSE_ISOOUTCRC_Msk }; enum { EVT_CAUSE_MASK = USBD_EVENTCAUSE_SUSPEND_Msk | USBD_EVENTCAUSE_RESUME_Msk | USBD_EVENTCAUSE_USBWUALLOWED_Msk | USBD_EVENTCAUSE_ISOOUTCRC_Msk };
uint32_t const evt_cause = NRF_USBD->EVENTCAUSE & EVT_CAUSE_MASK; uint32_t const evt_cause = NRF_USBD->EVENTCAUSE & EVT_CAUSE_MASK;

View File

@ -43,8 +43,7 @@
// Public API // Public API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
bool tusb_init(void) bool tusb_init(void) {
{
#if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT) #if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT)
// init device stack CFG_TUSB_RHPORTx_MODE must be defined // init device stack CFG_TUSB_RHPORTx_MODE must be defined
TU_ASSERT ( tud_init(TUD_OPT_RHPORT) ); TU_ASSERT ( tud_init(TUD_OPT_RHPORT) );
@ -58,8 +57,7 @@ bool tusb_init(void)
return true; return true;
} }
bool tusb_inited(void) bool tusb_inited(void) {
{
bool ret = false; bool ret = false;
#if CFG_TUD_ENABLED #if CFG_TUD_ENABLED
@ -77,37 +75,30 @@ bool tusb_inited(void)
// Descriptor helper // Descriptor helper
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
uint8_t const * tu_desc_find(uint8_t const* desc, uint8_t const* end, uint8_t byte1) uint8_t const* tu_desc_find(uint8_t const* desc, uint8_t const* end, uint8_t byte1) {
{ while (desc + 1 < end) {
while(desc+1 < end) if (desc[1] == byte1) return desc;
{
if ( desc[1] == byte1 ) return desc;
desc += desc[DESC_OFFSET_LEN]; desc += desc[DESC_OFFSET_LEN];
} }
return NULL; return NULL;
} }
uint8_t const * tu_desc_find2(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2) uint8_t const* tu_desc_find2(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2) {
{ while (desc + 2 < end) {
while(desc+2 < end) if (desc[1] == byte1 && desc[2] == byte2) return desc;
{
if ( desc[1] == byte1 && desc[2] == byte2) return desc;
desc += desc[DESC_OFFSET_LEN]; desc += desc[DESC_OFFSET_LEN];
} }
return NULL; return NULL;
} }
uint8_t const * tu_desc_find3(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2, uint8_t byte3) uint8_t const* tu_desc_find3(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2, uint8_t byte3) {
{ while (desc + 3 < end) {
while(desc+3 < end)
{
if (desc[1] == byte1 && desc[2] == byte2 && desc[3] == byte3) return desc; if (desc[1] == byte1 && desc[2] == byte2 && desc[3] == byte3) return desc;
desc += desc[DESC_OFFSET_LEN]; desc += desc[DESC_OFFSET_LEN];
} }
return NULL; return NULL;
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Endpoint Helper for both Host and Device stack // Endpoint Helper for both Host and Device stack
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -126,13 +117,11 @@ bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex) {
} }
(void) osal_mutex_unlock(mutex); (void) osal_mutex_unlock(mutex);
return available; return available;
} }
bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex) { bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex) {
(void) mutex; (void) mutex;
(void) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER); (void) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
// can only release the endpoint if it is claimed and not busy // can only release the endpoint if it is claimed and not busy
@ -142,58 +131,49 @@ bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex) {
} }
(void) osal_mutex_unlock(mutex); (void) osal_mutex_unlock(mutex);
return ret; return ret;
} }
bool tu_edpt_validate(tusb_desc_endpoint_t const * desc_ep, tusb_speed_t speed) bool tu_edpt_validate(tusb_desc_endpoint_t const* desc_ep, tusb_speed_t speed) {
{
uint16_t const max_packet_size = tu_edpt_packet_size(desc_ep); uint16_t const max_packet_size = tu_edpt_packet_size(desc_ep);
TU_LOG2(" Open EP %02X with Size = %u\r\n", desc_ep->bEndpointAddress, max_packet_size); TU_LOG2(" Open EP %02X with Size = %u\r\n", desc_ep->bEndpointAddress, max_packet_size);
switch (desc_ep->bmAttributes.xfer) switch (desc_ep->bmAttributes.xfer) {
{ case TUSB_XFER_ISOCHRONOUS: {
case TUSB_XFER_ISOCHRONOUS:
{
uint16_t const spec_size = (speed == TUSB_SPEED_HIGH ? 1024 : 1023); uint16_t const spec_size = (speed == TUSB_SPEED_HIGH ? 1024 : 1023);
TU_ASSERT(max_packet_size <= spec_size); TU_ASSERT(max_packet_size <= spec_size);
break;
} }
break;
case TUSB_XFER_BULK: case TUSB_XFER_BULK:
if (speed == TUSB_SPEED_HIGH) if (speed == TUSB_SPEED_HIGH) {
{
// Bulk highspeed must be EXACTLY 512 // Bulk highspeed must be EXACTLY 512
TU_ASSERT(max_packet_size == 512); TU_ASSERT(max_packet_size == 512);
}else } else {
{
// TODO Bulk fullspeed can only be 8, 16, 32, 64 // TODO Bulk fullspeed can only be 8, 16, 32, 64
TU_ASSERT(max_packet_size <= 64); TU_ASSERT(max_packet_size <= 64);
} }
break; break;
case TUSB_XFER_INTERRUPT: case TUSB_XFER_INTERRUPT: {
{
uint16_t const spec_size = (speed == TUSB_SPEED_HIGH ? 1024 : 64); uint16_t const spec_size = (speed == TUSB_SPEED_HIGH ? 1024 : 64);
TU_ASSERT(max_packet_size <= spec_size); TU_ASSERT(max_packet_size <= spec_size);
break;
} }
break;
default: return false; default:
return false;
} }
return true; return true;
} }
void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const* desc_itf, uint16_t desc_len, uint8_t driver_id) void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const* desc_itf, uint16_t desc_len, uint8_t driver_id) {
{
uint8_t const* p_desc = (uint8_t const*) desc_itf; uint8_t const* p_desc = (uint8_t const*) desc_itf;
uint8_t const* desc_end = p_desc + desc_len; uint8_t const* desc_end = p_desc + desc_len;
while( p_desc < desc_end ) while (p_desc < desc_end) {
{ if (TUSB_DESC_ENDPOINT == tu_desc_type(p_desc)) {
if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
{
uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress; uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress;
TU_LOG(2, " Bind EP %02x to driver id %u\r\n", ep_addr, driver_id); TU_LOG(2, " Bind EP %02x to driver id %u\r\n", ep_addr, driver_id);
@ -204,25 +184,21 @@ void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const* desc_
} }
} }
uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len) uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len) {
{
uint8_t const* p_desc = (uint8_t const*) desc_itf; uint8_t const* p_desc = (uint8_t const*) desc_itf;
uint16_t len = 0; uint16_t len = 0;
while (itf_count--) while (itf_count--) {
{
// Next on interface desc // Next on interface desc
len += tu_desc_len(desc_itf); len += tu_desc_len(desc_itf);
p_desc = tu_desc_next(p_desc); p_desc = tu_desc_next(p_desc);
while (len < max_len) while (len < max_len) {
{
// return on IAD regardless of itf count // return on IAD regardless of itf count
if ( tu_desc_type(p_desc) == TUSB_DESC_INTERFACE_ASSOCIATION ) return len; if (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE_ASSOCIATION) return len;
if ( (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE) && if ((tu_desc_type(p_desc) == TUSB_DESC_INTERFACE) &&
((tusb_desc_interface_t const*) p_desc)->bAlternateSetting == 0 ) ((tusb_desc_interface_t const*) p_desc)->bAlternateSetting == 0) {
{
break; break;
} }
@ -239,8 +215,7 @@ uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf,
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable, bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable,
void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize) void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize) {
{
osal_mutex_t new_mutex = osal_mutex_create(&s->ff_mutex); osal_mutex_t new_mutex = osal_mutex_create(&s->ff_mutex);
(void) new_mutex; (void) new_mutex;
(void) is_tx; (void) is_tx;
@ -255,16 +230,12 @@ bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool ove
return true; return true;
} }
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline bool stream_claim(tu_edpt_stream_t* s) {
bool stream_claim(tu_edpt_stream_t* s) if (s->is_host) {
{
if (s->is_host)
{
#if CFG_TUH_ENABLED #if CFG_TUH_ENABLED
return usbh_edpt_claim(s->daddr, s->ep_addr); return usbh_edpt_claim(s->daddr, s->ep_addr);
#endif #endif
}else } else {
{
#if CFG_TUD_ENABLED #if CFG_TUD_ENABLED
return usbd_edpt_claim(s->rhport, s->ep_addr); return usbd_edpt_claim(s->rhport, s->ep_addr);
#endif #endif
@ -273,16 +244,12 @@ bool stream_claim(tu_edpt_stream_t* s)
return false; return false;
} }
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline bool stream_xfer(tu_edpt_stream_t* s, uint16_t count) {
bool stream_xfer(tu_edpt_stream_t* s, uint16_t count) if (s->is_host) {
{
if (s->is_host)
{
#if CFG_TUH_ENABLED #if CFG_TUH_ENABLED
return usbh_edpt_xfer(s->daddr, s->ep_addr, count ? s->ep_buf : NULL, count); return usbh_edpt_xfer(s->daddr, s->ep_addr, count ? s->ep_buf : NULL, count);
#endif #endif
}else } else {
{
#if CFG_TUD_ENABLED #if CFG_TUD_ENABLED
return usbd_edpt_xfer(s->rhport, s->ep_addr, count ? s->ep_buf : NULL, count); return usbd_edpt_xfer(s->rhport, s->ep_addr, count ? s->ep_buf : NULL, count);
#endif #endif
@ -291,16 +258,12 @@ bool stream_xfer(tu_edpt_stream_t* s, uint16_t count)
return false; return false;
} }
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline bool stream_release(tu_edpt_stream_t* s) {
bool stream_release(tu_edpt_stream_t* s) if (s->is_host) {
{
if (s->is_host)
{
#if CFG_TUH_ENABLED #if CFG_TUH_ENABLED
return usbh_edpt_release(s->daddr, s->ep_addr); return usbh_edpt_release(s->daddr, s->ep_addr);
#endif #endif
}else } else {
{
#if CFG_TUD_ENABLED #if CFG_TUD_ENABLED
return usbd_edpt_release(s->rhport, s->ep_addr); return usbd_edpt_release(s->rhport, s->ep_addr);
#endif #endif
@ -313,34 +276,29 @@ bool stream_release(tu_edpt_stream_t* s)
// Stream Write // Stream Write
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes) bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes) {
{
// ZLP condition: no pending data, last transferred bytes is multiple of packet size // ZLP condition: no pending data, last transferred bytes is multiple of packet size
TU_VERIFY( !tu_fifo_count(&s->ff) && last_xferred_bytes && (0 == (last_xferred_bytes & (s->ep_packetsize-1))) ); TU_VERIFY(!tu_fifo_count(&s->ff) && last_xferred_bytes && (0 == (last_xferred_bytes & (s->ep_packetsize - 1))));
TU_VERIFY(stream_claim(s));
TU_VERIFY( stream_claim(s) ); TU_ASSERT(stream_xfer(s, 0));
TU_ASSERT( stream_xfer(s, 0) );
return true; return true;
} }
uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s) uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s) {
{
// skip if no data // skip if no data
TU_VERIFY( tu_fifo_count(&s->ff), 0 ); TU_VERIFY(tu_fifo_count(&s->ff), 0);
// Claim the endpoint // Claim the endpoint
TU_VERIFY( stream_claim(s), 0 ); TU_VERIFY(stream_claim(s), 0);
// Pull data from FIFO -> EP buf // Pull data from FIFO -> EP buf
uint16_t const count = tu_fifo_read_n(&s->ff, s->ep_buf, s->ep_bufsize); uint16_t const count = tu_fifo_read_n(&s->ff, s->ep_buf, s->ep_bufsize);
if ( count ) if (count) {
{ TU_ASSERT(stream_xfer(s, count), 0);
TU_ASSERT( stream_xfer(s, count), 0 );
return count; return count;
}else } else {
{
// Release endpoint since we don't make any transfer // Release endpoint since we don't make any transfer
// Note: data is dropped if terminal is not connected // Note: data is dropped if terminal is not connected
stream_release(s); stream_release(s);
@ -348,16 +306,14 @@ uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s)
} }
} }
uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const *buffer, uint32_t bufsize) uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const* buffer, uint32_t bufsize) {
{
TU_VERIFY(bufsize); // TODO support ZLP TU_VERIFY(bufsize); // TODO support ZLP
uint16_t ret = tu_fifo_write_n(&s->ff, buffer, (uint16_t) bufsize); uint16_t ret = tu_fifo_write_n(&s->ff, buffer, (uint16_t) bufsize);
// flush if fifo has more than packet size or // flush if fifo has more than packet size or
// in rare case: fifo depth is configured too small (which never reach packet size) // in rare case: fifo depth is configured too small (which never reach packet size)
if ( (tu_fifo_count(&s->ff) >= s->ep_packetsize) || (tu_fifo_depth(&s->ff) < s->ep_packetsize) ) if ((tu_fifo_count(&s->ff) >= s->ep_packetsize) || (tu_fifo_depth(&s->ff) < s->ep_packetsize)) {
{
tu_edpt_stream_write_xfer(s); tu_edpt_stream_write_xfer(s);
} }
@ -368,8 +324,7 @@ uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const *buffer, uint32_t
// Stream Read // Stream Read
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s) uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s) {
{
uint16_t available = tu_fifo_remaining(&s->ff); uint16_t available = tu_fifo_remaining(&s->ff);
// Prepare for incoming data but only allow what we can store in the ring buffer. // Prepare for incoming data but only allow what we can store in the ring buffer.
@ -384,25 +339,20 @@ uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s)
// get available again since fifo can be changed before endpoint is claimed // get available again since fifo can be changed before endpoint is claimed
available = tu_fifo_remaining(&s->ff); available = tu_fifo_remaining(&s->ff);
if ( available >= s->ep_packetsize ) if (available >= s->ep_packetsize) {
{
// multiple of packet size limit by ep bufsize // multiple of packet size limit by ep bufsize
uint16_t count = (uint16_t) (available & ~(s->ep_packetsize -1)); uint16_t count = (uint16_t) (available & ~(s->ep_packetsize - 1));
count = tu_min16(count, s->ep_bufsize); count = tu_min16(count, s->ep_bufsize);
TU_ASSERT(stream_xfer(s, count), 0);
TU_ASSERT( stream_xfer(s, count), 0 );
return count; return count;
}else } else {
{
// Release endpoint since we don't make any transfer // Release endpoint since we don't make any transfer
stream_release(s); stream_release(s);
return 0; return 0;
} }
} }
uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize) uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize) {
{
uint32_t num_read = tu_fifo_read_n(&s->ff, buffer, (uint16_t) bufsize); uint32_t num_read = tu_fifo_read_n(&s->ff, buffer, (uint16_t) bufsize);
tu_edpt_stream_read_xfer(s); tu_edpt_stream_read_xfer(s);
return num_read; return num_read;
@ -416,38 +366,33 @@ uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize
#include <ctype.h> #include <ctype.h>
#if CFG_TUSB_DEBUG >= CFG_TUH_LOG_LEVEL || CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL #if CFG_TUSB_DEBUG >= CFG_TUH_LOG_LEVEL || CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL
char const* const tu_str_speed[] = {"Full", "Low", "High"};
char const* const tu_str_speed[] = { "Full", "Low", "High" }; char const* const tu_str_std_request[] = {
char const* const tu_str_std_request[] = "Get Status",
{ "Clear Feature",
"Get Status" , "Reserved",
"Clear Feature" , "Set Feature",
"Reserved" , "Reserved",
"Set Feature" , "Set Address",
"Reserved" , "Get Descriptor",
"Set Address" , "Set Descriptor",
"Get Descriptor" , "Get Configuration",
"Set Descriptor" , "Set Configuration",
"Get Configuration" , "Get Interface",
"Set Configuration" , "Set Interface",
"Get Interface" , "Synch Frame"
"Set Interface" ,
"Synch Frame"
}; };
char const* const tu_str_xfer_result[] = { char const* const tu_str_xfer_result[] = {
"OK", "FAILED", "STALLED", "TIMEOUT" "OK", "FAILED", "STALLED", "TIMEOUT"
}; };
#endif #endif
static void dump_str_line(uint8_t const* buf, uint16_t count) static void dump_str_line(uint8_t const* buf, uint16_t count) {
{
tu_printf(" |"); tu_printf(" |");
// each line is 16 bytes // each line is 16 bytes
for(uint16_t i=0; i<count; i++) for (uint16_t i = 0; i < count; i++) {
{
const char ch = buf[i]; const char ch = buf[i];
tu_printf("%c", isprint(ch) ? ch : '.'); tu_printf("%c", isprint(ch) ? ch : '.');
} }
@ -460,39 +405,34 @@ static void dump_str_line(uint8_t const* buf, uint16_t count)
* - count : number of item * - count : number of item
* - indent: prefix spaces on every line * - indent: prefix spaces on every line
*/ */
void tu_print_mem(void const *buf, uint32_t count, uint8_t indent) void tu_print_mem(void const* buf, uint32_t count, uint8_t indent) {
{
uint8_t const size = 1; // fixed 1 byte for now uint8_t const size = 1; // fixed 1 byte for now
if ( !buf || !count ) if (!buf || !count) {
{
tu_printf("NULL\r\n"); tu_printf("NULL\r\n");
return; return;
} }
uint8_t const *buf8 = (uint8_t const *) buf; uint8_t const* buf8 = (uint8_t const*) buf;
char format[] = "%00X"; char format[] = "%00X";
format[2] += (uint8_t) (2*size); // 1 byte = 2 hex digits format[2] += (uint8_t) (2 * size); // 1 byte = 2 hex digits
const uint8_t item_per_line = 16 / size; const uint8_t item_per_line = 16 / size;
for(unsigned int i=0; i<count; i++) for (unsigned int i = 0; i < count; i++) {
{ unsigned int value = 0;
unsigned int value=0;
if ( i%item_per_line == 0 ) if (i % item_per_line == 0) {
{
// Print Ascii // Print Ascii
if ( i != 0 ) if (i != 0) {
{ dump_str_line(buf8 - 16, 16);
dump_str_line(buf8-16, 16);
} }
for(uint8_t s=0; s < indent; s++) tu_printf(" "); for (uint8_t s = 0; s < indent; s++) tu_printf(" ");
// print offset or absolute address // print offset or absolute address
tu_printf("%04X: ", 16*i/item_per_line); tu_printf("%04X: ", 16 * i / item_per_line);
} }
tu_memcpy_s(&value, sizeof(value), buf8, size); tu_memcpy_s(&value, sizeof(value), buf8, size);
@ -503,19 +443,17 @@ void tu_print_mem(void const *buf, uint32_t count, uint8_t indent)
} }
// fill up last row to 16 for printing ascii // fill up last row to 16 for printing ascii
const uint32_t remain = count%16; const uint32_t remain = count % 16;
uint8_t nback = (uint8_t)(remain ? remain : 16); uint8_t nback = (uint8_t) (remain ? remain : 16);
if ( remain ) if (remain) {
{ for (uint32_t i = 0; i < 16 - remain; i++) {
for(uint32_t i=0; i< 16-remain; i++)
{
tu_printf(" "); tu_printf(" ");
for(int j=0; j<2*size; j++) tu_printf(" "); for (int j = 0; j < 2 * size; j++) tu_printf(" ");
} }
} }
dump_str_line(buf8-nback, nback); dump_str_line(buf8 - nback, nback);
} }
#endif #endif