mikroSDK Reference Manual
tusb_private.h
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2022, Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27
28#ifndef _TUSB_PRIVATE_H_
29#define _TUSB_PRIVATE_H_
30
31// Internal Helper used by Host and Device Stack
32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37typedef struct TU_ATTR_PACKED
38{
39 volatile uint8_t busy : 1;
40 volatile uint8_t stalled : 1;
41 volatile uint8_t claimed : 1;
43
44typedef struct {
45 bool is_host; // host or device most
46 union {
47 uint8_t daddr;
48 uint8_t rhport;
49 uint8_t hwid;
50 };
51 uint8_t ep_addr;
52 uint8_t ep_speed;
53
54 uint16_t ep_packetsize;
55 uint16_t ep_bufsize;
56
57 // TODO xfer_fifo can skip this buffer
58 uint8_t* ep_buf;
59
60 tu_fifo_t ff;
61
62 // mutex: read if ep rx, write if e tx
63 OSAL_MUTEX_DEF(ff_mutex);
64
66
67//--------------------------------------------------------------------+
68// Endpoint
69//--------------------------------------------------------------------+
70
71// Check if endpoint descriptor is valid per USB specs
72bool tu_edpt_validate(tusb_desc_endpoint_t const * desc_ep, tusb_speed_t speed);
73
74// Bind all endpoint of a interface descriptor to class driver
75void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const* p_desc, uint16_t desc_len, uint8_t driver_id);
76
77// Calculate total length of n interfaces (depending on IAD)
78uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len);
79
80// Claim an endpoint with provided mutex
81bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex);
82
83// Release an endpoint with provided mutex
84bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex);
85
86//--------------------------------------------------------------------+
87// Endpoint Stream
88//--------------------------------------------------------------------+
89
90// Init an stream, should only be called once
91bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable,
92 void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize);
93
94// Open an stream for an endpoint
95// hwid is either device address (host mode) or rhport (device mode)
96TU_ATTR_ALWAYS_INLINE static inline
97void tu_edpt_stream_open(tu_edpt_stream_t* s, uint8_t hwid, tusb_desc_endpoint_t const *desc_ep)
98{
99 tu_fifo_clear(&s->ff);
100 s->hwid = hwid;
101 s->ep_addr = desc_ep->bEndpointAddress;
102 s->ep_packetsize = tu_edpt_packet_size(desc_ep);
103}
104
105TU_ATTR_ALWAYS_INLINE static inline
106void tu_edpt_stream_close(tu_edpt_stream_t* s)
107{
108 s->hwid = 0;
109 s->ep_addr = 0;
110}
111
112// Clear fifo
113TU_ATTR_ALWAYS_INLINE static inline
114bool tu_edpt_stream_clear(tu_edpt_stream_t* s)
115{
116 return tu_fifo_clear(&s->ff);
117}
118
119//--------------------------------------------------------------------+
120// Stream Write
121//--------------------------------------------------------------------+
122
123// Write to stream
124uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const *buffer, uint32_t bufsize);
125
126// Start an usb transfer if endpoint is not busy
127uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s);
128
129// Start an zero-length packet if needed
130bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes);
131
132// Get the number of bytes available for writing
133TU_ATTR_ALWAYS_INLINE static inline
134uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s)
135{
136 return (uint32_t) tu_fifo_remaining(&s->ff);
137}
138
139//--------------------------------------------------------------------+
140// Stream Read
141//--------------------------------------------------------------------+
142
143// Read from stream
144uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize);
145
146// Start an usb transfer if endpoint is not busy
147uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s);
148
149// Must be called in the transfer complete callback
150TU_ATTR_ALWAYS_INLINE static inline
151void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t* s, uint32_t xferred_bytes) {
152 tu_fifo_write_n(&s->ff, s->ep_buf, (uint16_t) xferred_bytes);
153}
154
155// Same as tu_edpt_stream_read_xfer_complete but skip the first n bytes
156TU_ATTR_ALWAYS_INLINE static inline
157void tu_edpt_stream_read_xfer_complete_offset(tu_edpt_stream_t* s, uint32_t xferred_bytes, uint32_t skip_offset) {
158 if (skip_offset < xferred_bytes) {
159 tu_fifo_write_n(&s->ff, s->ep_buf + skip_offset, (uint16_t) (xferred_bytes - skip_offset));
160 }
161}
162
163// Get the number of bytes available for reading
164TU_ATTR_ALWAYS_INLINE static inline
165uint32_t tu_edpt_stream_read_available(tu_edpt_stream_t* s) {
166 return (uint32_t) tu_fifo_count(&s->ff);
167}
168
169TU_ATTR_ALWAYS_INLINE static inline
170bool tu_edpt_stream_peek(tu_edpt_stream_t* s, uint8_t* ch) {
171 return tu_fifo_peek(&s->ff, ch);
172}
173
174#ifdef __cplusplus
175 }
176#endif
177
178#endif /* _TUSB_PRIVATE_H_ */
tusb_speed_t
defined base on EHCI specs value for Endpoint Speed
Definition tusb_types.h:48
AUDIO Channel Cluster Descriptor (4.1)
Definition audio.h:647
Definition tusb_private.h:44
Definition tusb_fifo.h:108