Open FFBoard
Open source force feedback firmware
tusb_private.h
Go to the documentation of this file.
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#ifndef TUSB_PRIVATE_H_
28#define TUSB_PRIVATE_H_
29
30// Internal Helper used by Host and Device Stack
31
32#ifdef __cplusplus
33 extern "C" {
34#endif
35
36#define TUP_USBIP_CONTROLLER_NUM 2
37extern tusb_role_t _tusb_rhport_role[TUP_USBIP_CONTROLLER_NUM];
38
39//--------------------------------------------------------------------+
40// Endpoint
41//--------------------------------------------------------------------+
42
43typedef struct TU_ATTR_PACKED {
44 volatile uint8_t busy : 1;
45 volatile uint8_t stalled : 1;
46 volatile uint8_t claimed : 1;
48
49typedef struct {
51 uint8_t is_host : 1; // 1: host, 0: device
52 uint8_t is_mps512 : 1; // 1: 512, 0: 64 since stream is used for Bulk only
53 };
54 uint8_t ep_addr;
55 uint16_t ep_bufsize;
56
57 uint8_t* ep_buf; // TODO xfer_fifo can skip this buffer
59
60 // mutex: read if rx, otherwise write
61 OSAL_MUTEX_DEF(ff_mutexdef);
62
64
65//--------------------------------------------------------------------+
66// Endpoint
67//--------------------------------------------------------------------+
68
69// Check if endpoint descriptor is valid per USB specs
70bool tu_edpt_validate(tusb_desc_endpoint_t const * desc_ep, tusb_speed_t speed);
71
72// Bind all endpoint of a interface descriptor to class driver
73void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const* p_desc, uint16_t desc_len, uint8_t driver_id);
74
75// Calculate total length of n interfaces (depending on IAD)
76uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len);
77
78// Claim an endpoint with provided mutex
79bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex);
80
81// Release an endpoint with provided mutex
82bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex);
83
84//--------------------------------------------------------------------+
85// Endpoint Stream
86//--------------------------------------------------------------------+
87
88// Init an endpoint stream
89bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable,
90 void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize);
91
92// Deinit an endpoint stream
94
95// Open an stream for an endpoint
96TU_ATTR_ALWAYS_INLINE static inline
98 tu_fifo_clear(&s->ff);
99 s->ep_addr = desc_ep->bEndpointAddress;
100 s->is_mps512 = (tu_edpt_packet_size(desc_ep) == 512) ? 1 : 0;
101}
102
103TU_ATTR_ALWAYS_INLINE static inline
105 s->ep_addr = 0;
106}
107
108// Clear fifo
109TU_ATTR_ALWAYS_INLINE static inline
111 return tu_fifo_clear(&s->ff);
112}
113
114//--------------------------------------------------------------------+
115// Stream Write
116//--------------------------------------------------------------------+
117
118// Write to stream
119uint32_t tu_edpt_stream_write(uint8_t hwid, tu_edpt_stream_t* s, void const *buffer, uint32_t bufsize);
120
121// Start an usb transfer if endpoint is not busy
122uint32_t tu_edpt_stream_write_xfer(uint8_t hwid, tu_edpt_stream_t* s);
123
124// Start an zero-length packet if needed
125bool tu_edpt_stream_write_zlp_if_needed(uint8_t hwid, tu_edpt_stream_t* s, uint32_t last_xferred_bytes);
126
127// Get the number of bytes available for writing to FIFO
128// Note: if no fifo, return endpoint size if not busy, 0 otherwise
129uint32_t tu_edpt_stream_write_available(uint8_t hwid, tu_edpt_stream_t* s);
130
131//--------------------------------------------------------------------+
132// Stream Read
133//--------------------------------------------------------------------+
134
135// Read from stream
136uint32_t tu_edpt_stream_read(uint8_t hwid, tu_edpt_stream_t* s, void* buffer, uint32_t bufsize);
137
138// Start an usb transfer if endpoint is not busy
139uint32_t tu_edpt_stream_read_xfer(uint8_t hwid, tu_edpt_stream_t* s);
140
141// Must be called in the transfer complete callback
142TU_ATTR_ALWAYS_INLINE static inline
143void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t* s, uint32_t xferred_bytes) {
144 if (tu_fifo_depth(&s->ff)) {
145 tu_fifo_write_n(&s->ff, s->ep_buf, (uint16_t) xferred_bytes);
146 }
147}
148
149// Same as tu_edpt_stream_read_xfer_complete but skip the first n bytes
150TU_ATTR_ALWAYS_INLINE static inline
151void tu_edpt_stream_read_xfer_complete_offset(tu_edpt_stream_t* s, uint32_t xferred_bytes, uint32_t skip_offset) {
152 if (tu_fifo_depth(&s->ff) && (skip_offset < xferred_bytes)) {
153 tu_fifo_write_n(&s->ff, s->ep_buf + skip_offset, (uint16_t) (xferred_bytes - skip_offset));
154 }
155}
156
157// Get the number of bytes available for reading
158TU_ATTR_ALWAYS_INLINE static inline
160 return (uint32_t) tu_fifo_count(&s->ff);
161}
162
163TU_ATTR_ALWAYS_INLINE static inline
165 return tu_fifo_peek(&s->ff, ch);
166}
167
168#ifdef __cplusplus
169 }
170#endif
171
172#endif
uint8_t const * buffer
Definition: midi_device.h:100
uint32_t bufsize
Definition: midi_device.h:95
SemaphoreHandle_t osal_mutex_t
Definition: osal_freertos.h:54
AUDIO Channel Cluster Descriptor (4.1)
Definition: audio.h:647
volatile uint8_t busy
Definition: tusb_private.h:44
volatile uint8_t claimed
Definition: tusb_private.h:46
volatile uint8_t stalled
Definition: tusb_private.h:45
uint8_t bEndpointAddress
Definition: video.h:306
uint8_t * ep_buf
Definition: tusb_private.h:57
OSAL_MUTEX_DEF(ff_mutexdef)
uint16_t ep_bufsize
Definition: tusb_private.h:55
tusb_role_t _tusb_rhport_role[TUP_USBIP_CONTROLLER_NUM]
Definition: tusb.c:42
uint16_t tu_fifo_count(tu_fifo_t *f)
Get number of items in FIFO.
Definition: tusb_fifo.c:590
uint16_t tu_fifo_write_n(tu_fifo_t *f, const void *data, uint16_t n)
This function will write n elements into the array index specified by the write pointer and increment...
Definition: tusb_fifo.c:861
bool tu_fifo_peek(tu_fifo_t *f, void *p_buffer)
Read one item without removing it from the FIFO. This function checks for an overflow and corrects re...
Definition: tusb_fifo.c:772
bool tu_fifo_clear(tu_fifo_t *f)
Clear the fifo read and write pointers.
Definition: tusb_fifo.c:896
static TU_ATTR_ALWAYS_INLINE uint16_t tu_fifo_depth(tu_fifo_t *f)
Definition: tusb_fifo.h:180
bool tu_edpt_validate(tusb_desc_endpoint_t const *desc_ep, tusb_speed_t speed)
Definition: tusb.c:202
bool tu_edpt_stream_write_zlp_if_needed(uint8_t hwid, tu_edpt_stream_t *s, uint32_t last_xferred_bytes)
Definition: tusb.c:353
bool tu_edpt_claim(tu_edpt_state_t *ep_state, osal_mutex_t mutex)
Definition: tusb.c:171
static TU_ATTR_ALWAYS_INLINE bool tu_edpt_stream_clear(tu_edpt_stream_t *s)
Definition: tusb_private.h:110
static TU_ATTR_ALWAYS_INLINE void tu_edpt_stream_read_xfer_complete_offset(tu_edpt_stream_t *s, uint32_t xferred_bytes, uint32_t skip_offset)
Definition: tusb_private.h:151
static TU_ATTR_ALWAYS_INLINE void tu_edpt_stream_close(tu_edpt_stream_t *s)
Definition: tusb_private.h:104
static TU_ATTR_ALWAYS_INLINE bool tu_edpt_stream_peek(tu_edpt_stream_t *s, uint8_t *ch)
Definition: tusb_private.h:164
static TU_ATTR_ALWAYS_INLINE uint32_t tu_edpt_stream_read_available(tu_edpt_stream_t *s)
Definition: tusb_private.h:159
uint32_t tu_edpt_stream_read_xfer(uint8_t hwid, tu_edpt_stream_t *s)
Definition: tusb.c:426
uint32_t tu_edpt_stream_read(uint8_t hwid, tu_edpt_stream_t *s, void *buffer, uint32_t bufsize)
Definition: tusb.c:461
uint32_t tu_edpt_stream_write_available(uint8_t hwid, tu_edpt_stream_t *s)
Definition: tusb.c:405
uint32_t tu_edpt_stream_write_xfer(uint8_t hwid, tu_edpt_stream_t *s)
Definition: tusb.c:362
uint32_t tu_edpt_stream_write(uint8_t hwid, tu_edpt_stream_t *s, void const *buffer, uint32_t bufsize)
Definition: tusb.c:382
struct TU_ATTR_PACKED tu_edpt_state_t
static TU_ATTR_ALWAYS_INLINE void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t *s, uint32_t xferred_bytes)
Definition: tusb_private.h:143
static TU_ATTR_ALWAYS_INLINE void tu_edpt_stream_open(tu_edpt_stream_t *s, tusb_desc_endpoint_t const *desc_ep)
Definition: tusb_private.h:97
bool tu_edpt_release(tu_edpt_state_t *ep_state, osal_mutex_t mutex)
Definition: tusb.c:188
uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const *desc_itf, uint8_t itf_count, uint16_t max_len)
Definition: tusb.c:251
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)
Definition: tusb.c:282
void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const *p_desc, uint16_t desc_len, uint8_t driver_id)
Definition: tusb.c:236
bool tu_edpt_stream_deinit(tu_edpt_stream_t *s)
Definition: tusb.c:302
tusb_speed_t
defined base on EHCI specs value for Endpoint Speed
Definition: tusb_types.h:49
static TU_ATTR_ALWAYS_INLINE uint16_t tu_edpt_packet_size(tusb_desc_endpoint_t const *desc_ep)
Definition: tusb_types.h:515