From 3fe7cd165902948240ecc40468b3610686f579c3 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 13 Nov 2018 15:34:50 +0700 Subject: added tud_cdc_write_str, tu_fifo only use mutex for RTOS config --- src/class/cdc/cdc_device.c | 13 +++++++++++-- src/class/cdc/cdc_device.h | 2 ++ src/common/tusb_fifo.c | 21 ++++++--------------- src/common/tusb_fifo.h | 4 +++- src/osal/osal_none.h | 13 ------------- 5 files changed, 22 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index a6f1f1fbb..392ad349d 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -71,8 +71,10 @@ typedef struct uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE]; uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE]; +#if CFG_FIFO_MUTEX osal_mutex_def_t rx_ff_mutex; osal_mutex_def_t tx_ff_mutex; +#endif // Endpoint Transfer buffer CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EPSIZE]; @@ -151,6 +153,11 @@ uint32_t tud_cdc_n_write_char(uint8_t itf, char ch) return tud_cdc_n_write(itf, &ch, 1); } +uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str) +{ + return tud_cdc_n_write(itf, str, strlen(str)); +} + uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize) { uint16_t ret = tu_fifo_write_n(&_cdcd_itf[itf].tx_ff, buffer, bufsize); @@ -203,10 +210,12 @@ void cdcd_init(void) // config fifo tu_fifo_config(&ser->rx_ff, ser->rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE, 1, true); - tu_fifo_config_mutex(&ser->rx_ff, osal_mutex_create(&ser->rx_ff_mutex)); - tu_fifo_config(&ser->tx_ff, ser->tx_ff_buf, CFG_TUD_CDC_TX_BUFSIZE, 1, false); + +#if CFG_FIFO_MUTEX + tu_fifo_config_mutex(&ser->rx_ff, osal_mutex_create(&ser->rx_ff_mutex)); tu_fifo_config_mutex(&ser->tx_ff, osal_mutex_create(&ser->tx_ff_mutex)); +#endif } } diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 2e03793a9..f1bd33953 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -77,6 +77,7 @@ char tud_cdc_n_peek (uint8_t itf, int pos); uint32_t tud_cdc_n_write_char (uint8_t itf, char ch); uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize); +uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str); bool tud_cdc_n_write_flush (uint8_t itf); //--------------------------------------------------------------------+ @@ -95,6 +96,7 @@ static inline char tud_cdc_peek (int pos) static inline uint32_t tud_cdc_write_char (char ch) { return tud_cdc_n_write_char(0, ch); } static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize) { return tud_cdc_n_write(0, buffer, bufsize); } +static inline uint32_t tud_cdc_write_str (char const* str) { return tud_cdc_n_write_str(0, str); } static inline bool tud_cdc_write_flush (void) { return tud_cdc_n_write_flush(0); } //--------------------------------------------------------------------+ diff --git a/src/common/tusb_fifo.c b/src/common/tusb_fifo.c index e0b3d0aa0..2acd4b696 100644 --- a/src/common/tusb_fifo.c +++ b/src/common/tusb_fifo.c @@ -42,25 +42,16 @@ #include "tusb_fifo.h" // implement mutex lock and unlock -// For OSAL_NONE: if mutex is locked by other, function return immediately (since there is no task context) -// For Real RTOS: fifo lock is a blocking API #if CFG_FIFO_MUTEX -static bool tu_fifo_lock(tu_fifo_t *f) +static void tu_fifo_lock(tu_fifo_t *f) { if (f->mutex) { -#if CFG_TUSB_OS == OPT_OS_NONE - // There is no subtask context for blocking mutex, we will check and return if cannot lock the mutex - if ( !osal_mutex_lock_notask(f->mutex) ) return false; -#else uint32_t err; (void) err; osal_mutex_lock(f->mutex, OSAL_TIMEOUT_WAIT_FOREVER, &err); -#endif } - - return true; } static void tu_fifo_unlock(tu_fifo_t *f) @@ -73,14 +64,14 @@ static void tu_fifo_unlock(tu_fifo_t *f) #else -#define tu_fifo_lock(_ff) true +#define tu_fifo_lock(_ff) #define tu_fifo_unlock(_ff) #endif bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable) { - if ( !tu_fifo_lock(f) ) return false; + tu_fifo_lock(f); f->buffer = (uint8_t*) buffer; f->depth = depth; @@ -115,7 +106,7 @@ bool tu_fifo_read(tu_fifo_t* f, void * p_buffer) { if( tu_fifo_empty(f) ) return false; - if ( !tu_fifo_lock(f) ) return false; + tu_fifo_lock(f); memcpy(p_buffer, f->buffer + (f->rd_idx * f->item_size), @@ -216,7 +207,7 @@ bool tu_fifo_write (tu_fifo_t* f, const void * p_data) { if ( tu_fifo_full(f) && !f->overwritable ) return false; - if ( !tu_fifo_lock(f) ) return false; + tu_fifo_lock(f); memcpy( f->buffer + (f->wr_idx * f->item_size), p_data, @@ -279,7 +270,7 @@ uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * p_data, uint16_t count) /******************************************************************************/ bool tu_fifo_clear(tu_fifo_t *f) { - if ( !tu_fifo_lock(f) ) return false; + tu_fifo_lock(f); f->rd_idx = f->wr_idx = f->count = 0; diff --git a/src/common/tusb_fifo.h b/src/common/tusb_fifo.h index 70dc087c2..61d92b602 100644 --- a/src/common/tusb_fifo.h +++ b/src/common/tusb_fifo.h @@ -43,7 +43,9 @@ #ifndef _TUSB_FIFO_H_ #define _TUSB_FIFO_H_ -#define CFG_FIFO_MUTEX 1 +// mutex is only needed for RTOS +// for OS None, we don't get preempted +#define CFG_FIFO_MUTEX (CFG_TUSB_OS != OPT_OS_NONE) #include #include diff --git a/src/osal/osal_none.h b/src/osal/osal_none.h index 796ddc15f..f70c99c7f 100644 --- a/src/osal/osal_none.h +++ b/src/osal/osal_none.h @@ -178,19 +178,6 @@ static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) #define osal_mutex_unlock(_mutex_hdl) osal_semaphore_post(_mutex_hdl, false) #define osal_mutex_lock osal_semaphore_wait -// check if mutex is available for non-thread/substask usage in some cases -static inline bool osal_mutex_lock_notask(osal_mutex_t mutex_hdl) -{ - if (mutex_hdl->count) - { - mutex_hdl->count--; - return true; - }else - { - return false; - } -} - //--------------------------------------------------------------------+ // QUEUE API //--------------------------------------------------------------------+ -- cgit v1.3.1