Skip to content

Commit 159aa59

Browse files
authored
Merge pull request hathach#1799 from hathach/update-osal-mutex
Update osal mutex
2 parents ab18b87 + 1e99480 commit 159aa59

File tree

13 files changed

+73
-83
lines changed

13 files changed

+73
-83
lines changed

src/class/cdc/cdc_device.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ typedef struct
6262
uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
6363
uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
6464

65-
#if CFG_FIFO_MUTEX
66-
osal_mutex_def_t rx_ff_mutex;
67-
osal_mutex_def_t tx_ff_mutex;
68-
#endif
65+
OSAL_MUTEX_DEF(rx_ff_mutex);
66+
OSAL_MUTEX_DEF(tx_ff_mutex);
6967

7068
// Endpoint Transfer buffer
7169
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EP_BUFSIZE];
@@ -248,10 +246,8 @@ void cdcd_init(void)
248246
// In this way, the most current data is prioritized.
249247
tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, true);
250248

251-
#if CFG_FIFO_MUTEX
252249
tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, osal_mutex_create(&p_cdc->rx_ff_mutex));
253250
tu_fifo_config_mutex(&p_cdc->tx_ff, osal_mutex_create(&p_cdc->tx_ff_mutex), NULL);
254-
#endif
255251
}
256252
}
257253

src/class/usbtmc/usbtmc_device.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ static bool handle_devMsgOut(uint8_t rhport, void *data, size_t len, size_t pack
157157
static uint8_t termChar;
158158
static uint8_t termCharRequested = false;
159159

160-
osal_mutex_def_t usbtmcLockBuffer;
160+
OSAL_MUTEX_DEF(usbtmcLockBuffer);
161161
static osal_mutex_t usbtmcLock;
162162

163163
// Our own private lock, mostly for the state variable.
164-
#define criticalEnter() do {osal_mutex_lock(usbtmcLock,OSAL_TIMEOUT_WAIT_FOREVER); } while (0)
165-
#define criticalLeave() do {osal_mutex_unlock(usbtmcLock); } while (0)
164+
#define criticalEnter() do { (void) osal_mutex_lock(usbtmcLock,OSAL_TIMEOUT_WAIT_FOREVER); } while (0)
165+
#define criticalLeave() do { (void) osal_mutex_unlock(usbtmcLock); } while (0)
166166

167167
bool atomicChangeState(usbtmcd_state_enum expectedState, usbtmcd_state_enum newState)
168168
{

src/common/tusb_fifo.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,13 @@ extern "C" {
4242
// within a certain number (see tu_fifo_overflow()).
4343

4444
#include "common/tusb_common.h"
45+
#include "osal/osal.h"
46+
47+
#define tu_fifo_mutex_t osal_mutex_t
4548

4649
// mutex is only needed for RTOS
4750
// for OS None, we don't get preempted
48-
#define CFG_FIFO_MUTEX (CFG_TUSB_OS != OPT_OS_NONE)
49-
50-
#if CFG_FIFO_MUTEX
51-
#include "osal/osal.h"
52-
#define tu_fifo_mutex_t osal_mutex_t
53-
#endif
51+
#define CFG_FIFO_MUTEX OSAL_MUTEX_REQUIRED
5452

5553
typedef struct
5654
{
@@ -65,7 +63,7 @@ typedef struct
6563
volatile uint16_t wr_idx ; ///< write pointer
6664
volatile uint16_t rd_idx ; ///< read pointer
6765

68-
#if CFG_FIFO_MUTEX
66+
#if OSAL_MUTEX_REQUIRED
6967
tu_fifo_mutex_t mutex_wr;
7068
tu_fifo_mutex_t mutex_rd;
7169
#endif
@@ -99,13 +97,18 @@ bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable);
9997
bool tu_fifo_clear(tu_fifo_t *f);
10098
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable);
10199

102-
#if CFG_FIFO_MUTEX
100+
#if OSAL_MUTEX_REQUIRED
103101
TU_ATTR_ALWAYS_INLINE static inline
104102
void tu_fifo_config_mutex(tu_fifo_t *f, tu_fifo_mutex_t write_mutex_hdl, tu_fifo_mutex_t read_mutex_hdl)
105103
{
106104
f->mutex_wr = write_mutex_hdl;
107105
f->mutex_rd = read_mutex_hdl;
108106
}
107+
108+
#else
109+
110+
#define tu_fifo_config_mutex(_f, _wr_mutex, _rd_mutex)
111+
109112
#endif
110113

111114
bool tu_fifo_write (tu_fifo_t* f, void const * p_data);

src/common/tusb_mcu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@
281281
// Default Values
282282
//--------------------------------------------------------------------+
283283

284+
#ifndef TUP_MCU_MULTIPLE_CORE
285+
#define TUP_MCU_MULTIPLE_CORE 0
286+
#endif
287+
284288
#ifndef TUP_DCD_ENDPOINT_MAX
285289
#warning "TUP_DCD_ENDPOINT_MAX is not defined for this MCU, default to 8"
286290
#define TUP_DCD_ENDPOINT_MAX 8

src/device/usbd.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,12 @@ static uint8_t _usbd_rhport = RHPORT_INVALID;
272272
OSAL_QUEUE_DEF(usbd_int_set, _usbd_qdef, CFG_TUD_TASK_QUEUE_SZ, dcd_event_t);
273273
static osal_queue_t _usbd_q;
274274

275-
// Mutex for claiming endpoint, only needed when using with preempted RTOS
276-
#if CFG_TUSB_OS != OPT_OS_NONE
277-
static osal_mutex_def_t _ubsd_mutexdef;
278-
static osal_mutex_t _usbd_mutex;
275+
// Mutex for claiming endpoint
276+
#if OSAL_MUTEX_REQUIRED
277+
static osal_mutex_def_t _ubsd_mutexdef;
278+
static osal_mutex_t _usbd_mutex;
279+
#else
280+
#define _usbd_mutex NULL
279281
#endif
280282

281283

@@ -389,7 +391,7 @@ bool tud_init (uint8_t rhport)
389391

390392
tu_varclr(&_usbd_dev);
391393

392-
#if CFG_TUSB_OS != OPT_OS_NONE
394+
#if OSAL_MUTEX_REQUIRED
393395
// Init device mutex
394396
_usbd_mutex = osal_mutex_create(&_ubsd_mutexdef);
395397
TU_ASSERT(_usbd_mutex);
@@ -1209,11 +1211,7 @@ bool usbd_edpt_claim(uint8_t rhport, uint8_t ep_addr)
12091211
uint8_t const dir = tu_edpt_dir(ep_addr);
12101212
tu_edpt_state_t* ep_state = &_usbd_dev.ep_status[epnum][dir];
12111213

1212-
#if TUSB_OPT_MUTEX
12131214
return tu_edpt_claim(ep_state, _usbd_mutex);
1214-
#else
1215-
return tu_edpt_claim(ep_state, NULL);
1216-
#endif
12171215
}
12181216

12191217
bool usbd_edpt_release(uint8_t rhport, uint8_t ep_addr)
@@ -1224,11 +1222,7 @@ bool usbd_edpt_release(uint8_t rhport, uint8_t ep_addr)
12241222
uint8_t const dir = tu_edpt_dir(ep_addr);
12251223
tu_edpt_state_t* ep_state = &_usbd_dev.ep_status[epnum][dir];
12261224

1227-
#if TUSB_OPT_MUTEX
12281225
return tu_edpt_release(ep_state, _usbd_mutex);
1229-
#else
1230-
return tu_edpt_release(ep_state, NULL);
1231-
#endif
12321226
}
12331227

12341228
bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)

src/host/usbh.c

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -212,28 +212,12 @@ static usbh_dev0_t _dev0;
212212
// TODO: hub can has its own simpler struct to save memory
213213
CFG_TUSB_MEM_SECTION usbh_device_t _usbh_devices[TOTAL_DEVICES];
214214

215-
// Mutex for claiming endpoint, only needed when using with preempted RTOS
216-
#if TUSB_OPT_MUTEX
217-
static osal_mutex_def_t _usbh_mutexdef;
218-
static osal_mutex_t _usbh_mutex;
219-
220-
TU_ATTR_ALWAYS_INLINE static inline void usbh_lock(void)
221-
{
222-
osal_mutex_lock(_usbh_mutex, OSAL_TIMEOUT_WAIT_FOREVER);
223-
}
224-
225-
TU_ATTR_ALWAYS_INLINE static inline void usbh_unlock(void)
226-
{
227-
osal_mutex_unlock(_usbh_mutex);
228-
}
229-
215+
// Mutex for claiming endpoint
216+
#if OSAL_MUTEX_REQUIRED
217+
static osal_mutex_def_t _usbh_mutexdef;
218+
static osal_mutex_t _usbh_mutex;
230219
#else
231-
232-
#define _usbh_mutex NULL
233-
234-
#define usbh_lock()
235-
#define usbh_unlock()
236-
220+
#define _usbh_mutex NULL
237221
#endif
238222

239223
// Event queue
@@ -277,8 +261,6 @@ static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t
277261
// TODO rework time-related function later
278262
void osal_task_delay(uint32_t msec)
279263
{
280-
(void) msec;
281-
282264
const uint32_t start = hcd_frame_number(_usbh_controller);
283265
while ( ( hcd_frame_number(_usbh_controller) - start ) < msec ) {}
284266
}
@@ -352,8 +334,8 @@ bool tuh_init(uint8_t controller_id)
352334
_usbh_q = osal_queue_create( &_usbh_qdef );
353335
TU_ASSERT(_usbh_q != NULL);
354336

355-
#if TUSB_OPT_MUTEX
356-
// Mutex
337+
#if OSAL_MUTEX_REQUIRED
338+
// Init mutex
357339
_usbh_mutex = osal_mutex_create(&_usbh_mutexdef);
358340
TU_ASSERT(_usbh_mutex);
359341
#endif
@@ -537,8 +519,7 @@ bool tuh_control_xfer (tuh_xfer_t* xfer)
537519

538520
uint8_t const daddr = xfer->daddr;
539521

540-
// TODO probably better to use semaphore as resource management than mutex
541-
usbh_lock();
522+
(void) osal_mutex_lock(_usbh_mutex, OSAL_TIMEOUT_WAIT_FOREVER);
542523

543524
bool const is_idle = (_ctrl_xfer.stage == CONTROL_STAGE_IDLE);
544525
if (is_idle)
@@ -553,7 +534,7 @@ bool tuh_control_xfer (tuh_xfer_t* xfer)
553534
_ctrl_xfer.user_data = xfer->user_data;
554535
}
555536

556-
usbh_unlock();
537+
(void) osal_mutex_unlock(_usbh_mutex);
557538

558539
TU_VERIFY(is_idle);
559540
const uint8_t rhport = usbh_get_rhport(daddr);
@@ -597,9 +578,9 @@ bool tuh_control_xfer (tuh_xfer_t* xfer)
597578

598579
TU_ATTR_ALWAYS_INLINE static inline void _set_control_xfer_stage(uint8_t stage)
599580
{
600-
usbh_lock();
581+
(void) osal_mutex_lock(_usbh_mutex, OSAL_TIMEOUT_WAIT_FOREVER);
601582
_ctrl_xfer.stage = stage;
602-
usbh_unlock();
583+
(void) osal_mutex_unlock(_usbh_mutex);
603584
}
604585

605586
static void _xfer_complete(uint8_t daddr, xfer_result_t result)

src/osal/osal.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,24 @@
3333

3434
#include "common/tusb_common.h"
3535

36-
// Return immediately
37-
#define OSAL_TIMEOUT_NOTIMEOUT (0)
38-
// Default timeout
39-
#define OSAL_TIMEOUT_NORMAL (10)
40-
// Wait forever
41-
#define OSAL_TIMEOUT_WAIT_FOREVER (UINT32_MAX)
36+
typedef void (*osal_task_func_t)( void * );
4237

38+
// Timeout
39+
#define OSAL_TIMEOUT_NOTIMEOUT (0) // Return immediately
40+
#define OSAL_TIMEOUT_NORMAL (10) // Default timeout
41+
#define OSAL_TIMEOUT_WAIT_FOREVER (UINT32_MAX) // Wait forever
4342
#define OSAL_TIMEOUT_CONTROL_XFER OSAL_TIMEOUT_WAIT_FOREVER
4443

45-
typedef void (*osal_task_func_t)( void * );
44+
// Mutex is required when using a preempted RTOS or MCU has multiple cores
45+
#if (CFG_TUSB_OS == OPT_OS_NONE) && !TUP_MCU_MULTIPLE_CORE
46+
#define OSAL_MUTEX_REQUIRED 0
47+
#define OSAL_MUTEX_DEF(_name)
48+
#else
49+
#define OSAL_MUTEX_REQUIRED 1
50+
#define OSAL_MUTEX_DEF(_name) osal_mutex_def_t _name
51+
#endif
4652

53+
// OS thin implementation
4754
#if CFG_TUSB_OS == OPT_OS_NONE
4855
#include "osal_none.h"
4956
#elif CFG_TUSB_OS == OPT_OS_FREERTOS

src/osal/osal_none.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t s
8282
typedef osal_semaphore_def_t osal_mutex_def_t;
8383
typedef osal_semaphore_t osal_mutex_t;
8484

85+
#if OSAL_MUTEX_REQUIRED
86+
// Note: multiple cores MCUs usually do provide IPC API for mutex
87+
// or we can use std atomic function
88+
8589
TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
8690
{
8791
mdef->count = 1;
@@ -98,6 +102,14 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hd
98102
return osal_semaphore_post(mutex_hdl, false);
99103
}
100104

105+
#else
106+
107+
#define osal_mutex_create(_mdef) (NULL)
108+
#define osal_mutex_lock(_mutex_hdl, _ms) (true)
109+
#define osal_mutex_unlock(_mutex_hdl) (true)
110+
111+
#endif
112+
101113
//--------------------------------------------------------------------+
102114
// QUEUE API
103115
//--------------------------------------------------------------------+

src/tusb.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex)
7474
{
7575
(void) mutex;
7676

77-
#if TUSB_OPT_MUTEX
7877
// pre-check to help reducing mutex lock
7978
TU_VERIFY((ep_state->busy == 0) && (ep_state->claimed == 0));
80-
osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
81-
#endif
79+
(void) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
8280

8381
// can only claim the endpoint if it is not busy and not claimed yet.
8482
bool const available = (ep_state->busy == 0) && (ep_state->claimed == 0);
@@ -87,9 +85,7 @@ bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex)
8785
ep_state->claimed = 1;
8886
}
8987

90-
#if TUSB_OPT_MUTEX
91-
osal_mutex_unlock(mutex);
92-
#endif
88+
(void) osal_mutex_unlock(mutex);
9389

9490
return available;
9591
}
@@ -98,9 +94,7 @@ bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex)
9894
{
9995
(void) mutex;
10096

101-
#if TUSB_OPT_MUTEX
102-
osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
103-
#endif
97+
(void) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
10498

10599
// can only release the endpoint if it is claimed and not busy
106100
bool const ret = (ep_state->claimed == 1) && (ep_state->busy == 0);
@@ -109,9 +103,7 @@ bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex)
109103
ep_state->claimed = 0;
110104
}
111105

112-
#if TUSB_OPT_MUTEX
113-
osal_mutex_unlock(mutex);
114-
#endif
106+
(void) osal_mutex_unlock(mutex);
115107

116108
return ret;
117109
}

src/tusb_option.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,6 @@ typedef int make_iso_compilers_happy;
299299
#define CFG_TUSB_OS_INC_PATH
300300
#endif
301301

302-
// mutex is only needed for RTOS TODO also required with multiple core MCUs
303-
#define TUSB_OPT_MUTEX (CFG_TUSB_OS != OPT_OS_NONE)
304-
305302
//--------------------------------------------------------------------
306303
// Device Options (Default)
307304
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)