Skip to content

Commit 1eb72af

Browse files
committed
TUD_EPBUF_TYPE_DEF video_device.c
1 parent 7831af3 commit 1eb72af

File tree

1 file changed

+54
-53
lines changed

1 file changed

+54
-53
lines changed

src/class/video/video_device.c

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -116,34 +116,32 @@ typedef struct TU_ATTR_PACKED {
116116
uint8_t state; /* 0:probing 1:committed 2:streaming */
117117

118118
video_probe_and_commit_control_t probe_commit_payload; /* Probe and Commit control */
119-
/*------------- From this point, data is not cleared by bus reset -------------*/
120-
CFG_TUSB_MEM_ALIGN uint8_t ep_buf[CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE]; /* EP transfer buffer for streaming */
121119
} videod_streaming_interface_t;
122120

121+
typedef struct {
122+
TUD_EPBUF_DEF(buf, CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE);
123+
} videod_streaming_epbuf_t;
124+
123125
/* video control interface */
124126
typedef struct TU_ATTR_PACKED {
125-
uint8_t const *beg; /* The head of the first video control interface descriptor */
126-
uint16_t len; /* Byte length of the descriptors */
127-
uint16_t cur; /* offset for current video control interface */
128-
uint8_t stm[CFG_TUD_VIDEO_STREAMING]; /* Indices of streaming interface */
129-
uint8_t error_code; /* error code */
130-
uint8_t power_mode;
131-
132-
/*------------- From this point, data is not cleared by bus reset -------------*/
133-
// CFG_TUSB_MEM_ALIGN uint8_t ctl_buf[64]; /* EP transfer buffer for interrupt transfer */
134-
127+
const uint8_t*beg; /* The head of the first video control interface descriptor */
128+
uint16_t len; /* Byte length of the descriptors */
129+
uint16_t cur; /* offset for current video control interface */
130+
uint8_t stm[CFG_TUD_VIDEO_STREAMING]; /* Indices of streaming interface */
131+
uint8_t error_code; /* error code */
132+
uint8_t power_mode;
135133
} videod_interface_t;
136134

137-
#define ITF_STM_MEM_RESET_SIZE offsetof(videod_streaming_interface_t, ep_buf)
138-
139135
//--------------------------------------------------------------------+
140136
// INTERNAL OBJECT & FUNCTION DECLARATION
141137
//--------------------------------------------------------------------+
142-
CFG_TUD_MEM_SECTION tu_static videod_interface_t _videod_itf[CFG_TUD_VIDEO];
143-
CFG_TUD_MEM_SECTION tu_static videod_streaming_interface_t _videod_streaming_itf[CFG_TUD_VIDEO_STREAMING];
138+
static videod_interface_t _videod_itf[CFG_TUD_VIDEO];
139+
140+
static videod_streaming_interface_t _videod_streaming_itf[CFG_TUD_VIDEO_STREAMING];
141+
CFG_TUD_MEM_SECTION static videod_streaming_epbuf_t _videod_streaming_epbuf[CFG_TUD_VIDEO_STREAMING];
144142

145-
tu_static uint8_t const _cap_get = 0x1u; /* support for GET */
146-
tu_static uint8_t const _cap_get_set = 0x3u; /* support for GET and SET */
143+
static uint8_t const _cap_get = 0x1u; /* support for GET */
144+
static uint8_t const _cap_get_set = 0x3u; /* support for GET and SET */
147145

148146
//--------------------------------------------------------------------+
149147
// Debug
@@ -816,21 +814,20 @@ static bool _open_vs_itf(uint8_t rhport, videod_streaming_interface_t *stm, uint
816814
}
817815

818816
/** Prepare the next packet payload. */
819-
static uint_fast16_t _prepare_in_payload(videod_streaming_interface_t *stm)
820-
{
817+
static uint_fast16_t _prepare_in_payload(videod_streaming_interface_t *stm, uint8_t* ep_buf) {
821818
uint_fast16_t remaining = stm->bufsize - stm->offset;
822-
uint_fast16_t hdr_len = stm->ep_buf[0];
819+
uint_fast16_t hdr_len = ep_buf[0];
823820
uint_fast16_t pkt_len = stm->max_payload_transfer_size;
824821
if (hdr_len + remaining < pkt_len) {
825822
pkt_len = hdr_len + remaining;
826823
}
827824
TU_ASSERT(pkt_len >= hdr_len);
828825
uint_fast16_t data_len = pkt_len - hdr_len;
829-
memcpy(&stm->ep_buf[hdr_len], stm->buffer + stm->offset, data_len);
826+
memcpy(&ep_buf[hdr_len], stm->buffer + stm->offset, data_len);
830827
stm->offset += data_len;
831828
remaining -= data_len;
832829
if (!remaining) {
833-
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm->ep_buf;
830+
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*) ep_buf;
834831
hdr->EndOfFrame = 1;
835832
}
836833
return hdr_len + data_len;
@@ -1001,7 +998,8 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
1001998
tusb_control_request_t const *request,
1002999
uint_fast8_t stm_idx) {
10031000
(void)rhport;
1004-
videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx];
1001+
videod_streaming_interface_t *stm = &_videod_streaming_itf[stm_idx];
1002+
videod_streaming_epbuf_t *stm_epbuf = &_videod_streaming_epbuf[stm_idx];
10051003

10061004
uint8_t const ctrl_sel = TU_U16_HIGH(request->wValue);
10071005
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));
@@ -1013,7 +1011,7 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
10131011
case VIDEO_REQUEST_GET_CUR:
10141012
if (stage == CONTROL_STAGE_SETUP) {
10151013
/* TODO */
1016-
TU_VERIFY(tud_control_xfer(rhport, request, &self->error_code, sizeof(uint8_t)), VIDEO_ERROR_UNKNOWN);
1014+
TU_VERIFY(tud_control_xfer(rhport, request, &stm->error_code, sizeof(uint8_t)), VIDEO_ERROR_UNKNOWN);
10171015
}
10181016
return VIDEO_ERROR_NONE;
10191017

@@ -1028,25 +1026,25 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
10281026
break;
10291027

10301028
case VIDEO_VS_CTL_PROBE:
1031-
if (self->state != VS_STATE_PROBING) {
1032-
self->state = VS_STATE_PROBING;
1029+
if (stm->state != VS_STATE_PROBING) {
1030+
stm->state = VS_STATE_PROBING;
10331031
}
10341032

10351033
switch (request->bRequest) {
10361034
case VIDEO_REQUEST_SET_CUR:
10371035
if (stage == CONTROL_STAGE_SETUP) {
1038-
TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)),
1036+
TU_VERIFY(tud_control_xfer(rhport, request, &stm->probe_commit_payload, sizeof(video_probe_and_commit_control_t)),
10391037
VIDEO_ERROR_UNKNOWN);
10401038
} else if (stage == CONTROL_STAGE_DATA) {
1041-
TU_VERIFY(_update_streaming_parameters(self, &self->probe_commit_payload),
1039+
TU_VERIFY(_update_streaming_parameters(stm, &stm->probe_commit_payload),
10421040
VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
10431041
}
10441042
return VIDEO_ERROR_NONE;
10451043

10461044
case VIDEO_REQUEST_GET_CUR:
10471045
if (stage == CONTROL_STAGE_SETUP) {
10481046
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
1049-
TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
1047+
TU_VERIFY(tud_control_xfer(rhport, request, &stm->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
10501048
}
10511049
return VIDEO_ERROR_NONE;
10521050

@@ -1056,8 +1054,8 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
10561054
case VIDEO_REQUEST_GET_DEF:
10571055
if (stage == CONTROL_STAGE_SETUP) {
10581056
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
1059-
video_probe_and_commit_control_t tmp = self->probe_commit_payload;
1060-
TU_VERIFY(_negotiate_streaming_parameters(self, request->bRequest, &tmp), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
1057+
video_probe_and_commit_control_t tmp = stm->probe_commit_payload;
1058+
TU_VERIFY(_negotiate_streaming_parameters(stm, request->bRequest, &tmp), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
10611059
TU_VERIFY(tud_control_xfer(rhport, request, &tmp, sizeof(tmp)), VIDEO_ERROR_UNKNOWN);
10621060
}
10631061
return VIDEO_ERROR_NONE;
@@ -1085,23 +1083,23 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
10851083
switch (request->bRequest) {
10861084
case VIDEO_REQUEST_SET_CUR:
10871085
if (stage == CONTROL_STAGE_SETUP) {
1088-
TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
1086+
TU_VERIFY(tud_control_xfer(rhport, request, &stm->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
10891087
} else if (stage == CONTROL_STAGE_DATA) {
1090-
video_probe_and_commit_control_t *param = &self->probe_commit_payload;
1091-
TU_VERIFY(_update_streaming_parameters(self, param), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
1088+
video_probe_and_commit_control_t *param = &stm->probe_commit_payload;
1089+
TU_VERIFY(_update_streaming_parameters(stm, param), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
10921090
/* Set the negotiated value */
1093-
self->max_payload_transfer_size = param->dwMaxPayloadTransferSize;
1091+
stm->max_payload_transfer_size = param->dwMaxPayloadTransferSize;
10941092
int ret = VIDEO_ERROR_NONE;
10951093
if (tud_video_commit_cb) {
1096-
ret = tud_video_commit_cb(self->index_vc, self->index_vs, param);
1094+
ret = tud_video_commit_cb(stm->index_vc, stm->index_vs, param);
10971095
}
10981096
if (VIDEO_ERROR_NONE == ret) {
1099-
self->state = VS_STATE_COMMITTED;
1100-
self->buffer = NULL;
1101-
self->bufsize = 0;
1102-
self->offset = 0;
1097+
stm->state = VS_STATE_COMMITTED;
1098+
stm->buffer = NULL;
1099+
stm->bufsize = 0;
1100+
stm->offset = 0;
11031101
/* initialize payload header */
1104-
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)self->ep_buf;
1102+
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm_epbuf->buf;
11051103
hdr->bHeaderLength = sizeof(*hdr);
11061104
hdr->bmHeaderInfo = 0;
11071105
}
@@ -1111,7 +1109,7 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
11111109
case VIDEO_REQUEST_GET_CUR:
11121110
if (stage == CONTROL_STAGE_SETUP) {
11131111
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
1114-
TU_VERIFY(tud_control_xfer(rhport, request, &self->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
1112+
TU_VERIFY(tud_control_xfer(rhport, request, &stm->probe_commit_payload, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
11151113
}
11161114
return VIDEO_ERROR_NONE;
11171115

@@ -1199,12 +1197,14 @@ bool tud_video_n_streaming(uint_fast8_t ctl_idx, uint_fast8_t stm_idx)
11991197
return true;
12001198
}
12011199

1202-
bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *buffer, size_t bufsize)
1203-
{
1200+
bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *buffer, size_t bufsize) {
12041201
TU_ASSERT(ctl_idx < CFG_TUD_VIDEO);
12051202
TU_ASSERT(stm_idx < CFG_TUD_VIDEO_STREAMING);
1203+
12061204
if (!buffer || !bufsize) return false;
12071205
videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, stm_idx);
1206+
videod_streaming_epbuf_t *stm_epbuf = &_videod_streaming_epbuf[ctl_idx];
1207+
12081208
if (!stm || !stm->desc.ep[0] || stm->buffer) return false;
12091209
if (stm->state == VS_STATE_PROBING) return false;
12101210

@@ -1221,14 +1221,14 @@ bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *bu
12211221

12221222
TU_VERIFY( usbd_edpt_claim(0, ep_addr) );
12231223
/* update the packet header */
1224-
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm->ep_buf;
1224+
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm_epbuf->buf;
12251225
hdr->FrameID ^= 1;
12261226
hdr->EndOfFrame = 0;
12271227
/* update the packet data */
12281228
stm->buffer = (uint8_t*)buffer;
12291229
stm->bufsize = bufsize;
1230-
uint_fast16_t pkt_len = _prepare_in_payload(stm);
1231-
TU_ASSERT( usbd_edpt_xfer(0, ep_addr, stm->ep_buf, (uint16_t) pkt_len), 0);
1230+
uint_fast16_t pkt_len = _prepare_in_payload(stm, stm_epbuf->buf);
1231+
TU_ASSERT( usbd_edpt_xfer(0, ep_addr, stm_epbuf->buf, (uint16_t) pkt_len), 0);
12321232
return true;
12331233
}
12341234

@@ -1242,7 +1242,7 @@ void videod_init(void) {
12421242
}
12431243
for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
12441244
videod_streaming_interface_t *stm = &_videod_streaming_itf[i];
1245-
tu_memclr(stm, ITF_STM_MEM_RESET_SIZE);
1245+
tu_memclr(stm, sizeof(videod_streaming_interface_t));
12461246
}
12471247
}
12481248

@@ -1258,7 +1258,7 @@ void videod_reset(uint8_t rhport) {
12581258
}
12591259
for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
12601260
videod_streaming_interface_t *stm = &_videod_streaming_itf[i];
1261-
tu_memclr(stm, ITF_STM_MEM_RESET_SIZE);
1261+
tu_memclr(stm, sizeof(videod_streaming_interface_t));
12621262
}
12631263
}
12641264

@@ -1392,13 +1392,14 @@ bool videod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint3
13921392
uint8_t const *desc = ctl->beg;
13931393
if (ep_addr == _desc_ep_addr(desc + ep_ofs)) break;
13941394
}
1395-
13961395
TU_ASSERT(itf < CFG_TUD_VIDEO_STREAMING);
1396+
videod_streaming_epbuf_t *stm_epbuf = &_videod_streaming_epbuf[itf];
1397+
13971398
if (stm->offset < stm->bufsize) {
13981399
/* Claim the endpoint */
13991400
TU_VERIFY( usbd_edpt_claim(rhport, ep_addr), 0);
1400-
uint_fast16_t pkt_len = _prepare_in_payload(stm);
1401-
TU_ASSERT( usbd_edpt_xfer(rhport, ep_addr, stm->ep_buf, (uint16_t) pkt_len), 0);
1401+
uint_fast16_t pkt_len = _prepare_in_payload(stm, stm_epbuf->buf);
1402+
TU_ASSERT( usbd_edpt_xfer(rhport, ep_addr, stm_epbuf->buf, (uint16_t) pkt_len), 0);
14021403
} else {
14031404
stm->buffer = NULL;
14041405
stm->bufsize = 0;

0 commit comments

Comments
 (0)