Skip to content

Commit a265ec7

Browse files
committed
sync with tinyusb
1 parent eb16a0f commit a265ec7

File tree

11 files changed

+255
-243
lines changed

11 files changed

+255
-243
lines changed

cores/nRF5/usb/tinyusb/src/class/cdc/cdc_device.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
//--------------------------------------------------------------------+
4545
// INCLUDE
4646
//--------------------------------------------------------------------+
47-
#include "common/tusb_common.h"
4847
#include "cdc_device.h"
4948
#include "device/usbd_pvt.h"
5049

@@ -72,13 +71,16 @@ typedef struct
7271
uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
7372
uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
7473

74+
osal_mutex_def_t rx_ff_mutex;
75+
osal_mutex_def_t tx_ff_mutex;
76+
7577
// Endpoint Transfer buffer
7678
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EPSIZE];
7779
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_CDC_EPSIZE];
7880

7981
}cdcd_interface_t;
8082

81-
#define ITF_BUS_RESET_SZ offsetof(cdcd_interface_t, wanted_char)
83+
#define ITF_MEM_RESET_SIZE offsetof(cdcd_interface_t, wanted_char)
8284

8385
//--------------------------------------------------------------------+
8486
// INTERNAL OBJECT & FUNCTION DECLARATION
@@ -189,17 +191,22 @@ void cdcd_init(void)
189191

190192
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
191193
{
192-
_cdcd_itf[i].wanted_char = -1;
194+
cdcd_interface_t* ser = &_cdcd_itf[i];
195+
196+
ser->wanted_char = -1;
193197

194198
// default line coding is : stop bit = 1, parity = none, data bits = 8
195-
_cdcd_itf[i].line_coding.bit_rate = 115200;
196-
_cdcd_itf[i].line_coding.stop_bits = 0;
197-
_cdcd_itf[i].line_coding.parity = 0;
198-
_cdcd_itf[i].line_coding.data_bits = 8;
199+
ser->line_coding.bit_rate = 115200;
200+
ser->line_coding.stop_bits = 0;
201+
ser->line_coding.parity = 0;
202+
ser->line_coding.data_bits = 8;
199203

200204
// config fifo
201-
tu_fifo_config(&_cdcd_itf[i].rx_ff, _cdcd_itf[i].rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE, 1, true);
202-
tu_fifo_config(&_cdcd_itf[i].tx_ff, _cdcd_itf[i].tx_ff_buf, CFG_TUD_CDC_TX_BUFSIZE, 1, false);
205+
tu_fifo_config(&ser->rx_ff, ser->rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE, 1, true);
206+
tu_fifo_config_mutex(&ser->rx_ff, osal_mutex_create(&ser->rx_ff_mutex));
207+
208+
tu_fifo_config(&ser->tx_ff, ser->tx_ff_buf, CFG_TUD_CDC_TX_BUFSIZE, 1, false);
209+
tu_fifo_config_mutex(&ser->tx_ff, osal_mutex_create(&ser->tx_ff_mutex));
203210
}
204211
}
205212

@@ -209,7 +216,7 @@ void cdcd_reset(uint8_t rhport)
209216

210217
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
211218
{
212-
tu_memclr(&_cdcd_itf[i], ITF_BUS_RESET_SZ);
219+
tu_memclr(&_cdcd_itf[i], ITF_MEM_RESET_SIZE);
213220
tu_fifo_clear(&_cdcd_itf[i].rx_ff);
214221
tu_fifo_clear(&_cdcd_itf[i].tx_ff);
215222
}

cores/nRF5/usb/tinyusb/src/common/tusb_common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
#include "tusb_verify.h"
6767
#include "binary.h"
6868
#include "tusb_error.h"
69-
#include "tusb_fifo.h"
7069
#include "tusb_timeout.h"
7170
#include "tusb_types.h"
7271

cores/nRF5/usb/tinyusb/src/common/tusb_fifo.c

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,51 @@
3636
*/
3737
/**************************************************************************/
3838

39+
#include <string.h>
40+
41+
#include "osal/osal.h"
3942
#include "tusb_fifo.h"
40-
#include "common/tusb_verify.h" // for ASSERT
4143

44+
// implement mutex lock and unlock
45+
// For OSAL_NONE: if mutex is locked by other, function return immediately (since there is no task context)
46+
// For Real RTOS: fifo lock is a blocking API
4247
#if CFG_FIFO_MUTEX
4348

44-
#define mutex_lock_if_needed(_ff) if (_ff->mutex) tu_fifo_mutex_lock(_ff->mutex)
45-
#define mutex_unlock_if_needed(_ff) if (_ff->mutex) tu_fifo_mutex_unlock(_ff->mutex)
49+
static bool tu_fifo_lock(tu_fifo_t *f)
50+
{
51+
if (f->mutex)
52+
{
53+
#if CFG_TUSB_OS == OPT_OS_NONE
54+
// There is no subtask context for blocking mutex, we will check and return if cannot lock the mutex
55+
if ( !osal_mutex_lock_notask(f->mutex) ) return false;
56+
#else
57+
uint32_t err;
58+
(void) err;
59+
osal_mutex_lock(f->mutex, OSAL_TIMEOUT_WAIT_FOREVER, &err);
60+
#endif
61+
}
62+
63+
return true;
64+
}
65+
66+
static void tu_fifo_unlock(tu_fifo_t *f)
67+
{
68+
if (f->mutex)
69+
{
70+
osal_mutex_unlock(f->mutex);
71+
}
72+
}
4673

4774
#else
4875

49-
#define mutex_lock_if_needed(_ff)
50-
#define mutex_unlock_if_needed(_ff)
76+
#define tu_fifo_lock(_ff) true
77+
#define tu_fifo_unlock(_ff)
5178

5279
#endif
5380

54-
void tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
81+
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
5582
{
56-
mutex_lock_if_needed(f);
83+
if ( !tu_fifo_lock(f) ) return false;
5784

5885
f->buffer = (uint8_t*) buffer;
5986
f->depth = depth;
@@ -62,7 +89,9 @@ void tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
6289

6390
f->rd_idx = f->wr_idx = f->count = 0;
6491

65-
mutex_unlock_if_needed(f);
92+
tu_fifo_unlock(f);
93+
94+
return true;
6695
}
6796

6897

@@ -86,15 +115,15 @@ bool tu_fifo_read(tu_fifo_t* f, void * p_buffer)
86115
{
87116
if( tu_fifo_empty(f) ) return false;
88117

89-
mutex_lock_if_needed(f);
118+
if ( !tu_fifo_lock(f) ) return false;
90119

91120
memcpy(p_buffer,
92121
f->buffer + (f->rd_idx * f->item_size),
93122
f->item_size);
94123
f->rd_idx = (f->rd_idx + 1) % f->depth;
95124
f->count--;
96125

97-
mutex_unlock_if_needed(f);
126+
tu_fifo_unlock(f);
98127

99128
return true;
100129
}
@@ -122,8 +151,6 @@ uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t count)
122151
/* Limit up to fifo's count */
123152
if ( count > f->count ) count = f->count;
124153

125-
mutex_lock_if_needed(f);
126-
127154
/* Could copy up to 2 portions marked as 'x' if queue is wrapped around
128155
* case 1: ....RxxxxW.......
129156
* case 2: xxxxxW....Rxxxxxx
@@ -138,8 +165,6 @@ uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t count)
138165
p_buf += f->item_size;
139166
}
140167

141-
mutex_unlock_if_needed(f);
142-
143168
return len;
144169
}
145170

@@ -191,7 +216,7 @@ bool tu_fifo_write (tu_fifo_t* f, const void * p_data)
191216
{
192217
if ( tu_fifo_full(f) && !f->overwritable ) return false;
193218

194-
mutex_lock_if_needed(f);
219+
if ( !tu_fifo_lock(f) ) return false;
195220

196221
memcpy( f->buffer + (f->wr_idx * f->item_size),
197222
p_data,
@@ -208,7 +233,7 @@ bool tu_fifo_write (tu_fifo_t* f, const void * p_data)
208233
f->count++;
209234
}
210235

211-
mutex_unlock_if_needed(f);
236+
tu_fifo_unlock(f);
212237

213238
return true;
214239
}
@@ -232,7 +257,7 @@ uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * p_data, uint16_t count)
232257
{
233258
if ( count == 0 ) return 0;
234259

235-
uint8_t* p_buf = (uint8_t*) p_data;
260+
uint8_t const* p_buf = (uint8_t const*) p_data;
236261

237262
uint16_t len = 0;
238263
while( (len < count) && tu_fifo_write(f, p_buf) )
@@ -252,11 +277,13 @@ uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * p_data, uint16_t count)
252277
Pointer to the FIFO buffer to manipulate
253278
*/
254279
/******************************************************************************/
255-
void tu_fifo_clear(tu_fifo_t *f)
280+
bool tu_fifo_clear(tu_fifo_t *f)
256281
{
257-
mutex_lock_if_needed(f);
282+
if ( !tu_fifo_lock(f) ) return false;
258283

259284
f->rd_idx = f->wr_idx = f->count = 0;
260285

261-
mutex_unlock_if_needed(f);
286+
tu_fifo_unlock(f);
287+
288+
return true;
262289
}

cores/nRF5/usb/tinyusb/src/common/tusb_fifo.h

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,17 @@
4343
#ifndef _TUSB_FIFO_H_
4444
#define _TUSB_FIFO_H_
4545

46-
#define CFG_FIFO_MUTEX 0
46+
#define CFG_FIFO_MUTEX 1
4747

4848
#include <stdint.h>
4949
#include <stdbool.h>
50-
#include <string.h>
5150

5251
#ifdef __cplusplus
5352
extern "C" {
5453
#endif
5554

5655
#if CFG_FIFO_MUTEX
57-
58-
#include "osal/osal.h"
59-
60-
#if CFG_TUSB_OS == OPT_OS_NONE
61-
// Since all fifo read/write is done in thread mode, there should be
62-
// no conflict except for osal queue which will be address seperatedly.
63-
// Therefore there may be no need for mutex with internal use of fifo
64-
65-
#define _ff_mutex_def(mutex)
66-
67-
#else
68-
#define tu_fifo_mutex_t struct os_mutex
69-
70-
#define tu_fifo_mutex_lock(m) os_mutex_pend(m, OS_TIMEOUT_NEVER)
71-
#define tu_fifo_mutex_unlock(m) os_mutex_release(m)
72-
73-
/* Internal use only */
74-
#define _mutex_declare(m) .mutex = m
75-
76-
#endif
77-
78-
#else
79-
80-
#define _mutex_declare(m)
81-
56+
#define tu_fifo_mutex_t osal_mutex_t
8257
#endif
8358

8459

@@ -98,24 +73,29 @@ typedef struct
9873
bool overwritable ;
9974

10075
#if CFG_FIFO_MUTEX
101-
tu_fifo_mutex_t * const mutex;
76+
tu_fifo_mutex_t mutex;
10277
#endif
10378

10479
} tu_fifo_t;
10580

106-
#define TU_FIFO_DEF(_name, _depth, _type, _overwritable) /*, irq_mutex)*/ \
107-
uint8_t _name##_buf[_depth*sizeof(_type)];\
108-
tu_fifo_t _name = {\
109-
.buffer = _name##_buf,\
110-
.depth = _depth,\
111-
.item_size = sizeof(_type),\
112-
.overwritable = _overwritable,\
113-
/*.irq = irq_mutex*/\
114-
_mutex_declare(_mutex)\
81+
#define TU_FIFO_DEF(_name, _depth, _type, _overwritable) \
82+
uint8_t _name##_buf[_depth*sizeof(_type)]; \
83+
tu_fifo_t _name = { \
84+
.buffer = _name##_buf, \
85+
.depth = _depth, \
86+
.item_size = sizeof(_type), \
87+
.overwritable = _overwritable, \
11588
}
11689

117-
void tu_fifo_clear(tu_fifo_t *f);
118-
void tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable);
90+
bool tu_fifo_clear(tu_fifo_t *f);
91+
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable);
92+
93+
#if CFG_FIFO_MUTEX
94+
static inline void tu_fifo_config_mutex(tu_fifo_t *f, tu_fifo_mutex_t mutex_hdl)
95+
{
96+
f->mutex = mutex_hdl;
97+
}
98+
#endif
11999

120100
bool tu_fifo_write (tu_fifo_t* f, void const * p_data);
121101
uint16_t tu_fifo_write_n (tu_fifo_t* f, void const * p_data, uint16_t count);

0 commit comments

Comments
 (0)