add mutex support (optional) for tu_fifo

This commit is contained in:
hathach
2018-11-02 17:28:07 +07:00
parent 2708632a6a
commit f6076b0e06
3 changed files with 68 additions and 63 deletions

View File

@@ -43,42 +43,17 @@
#ifndef _TUSB_FIFO_H_
#define _TUSB_FIFO_H_
#define CFG_FIFO_MUTEX 0
#define CFG_FIFO_MUTEX 1
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#if CFG_FIFO_MUTEX
#include "osal/osal.h"
#if CFG_TUSB_OS == OPT_OS_NONE
// Since all fifo read/write is done in thread mode, there should be
// no conflict except for osal queue which will be address seperatedly.
// Therefore there may be no need for mutex with internal use of fifo
#define _ff_mutex_def(mutex)
#else
#define tu_fifo_mutex_t struct os_mutex
#define tu_fifo_mutex_lock(m) os_mutex_pend(m, OS_TIMEOUT_NEVER)
#define tu_fifo_mutex_unlock(m) os_mutex_release(m)
/* Internal use only */
#define _mutex_declare(m) .mutex = m
#endif
#else
#define _mutex_declare(m)
#define tu_fifo_mutex_t osal_mutex_t
#endif
@@ -98,24 +73,29 @@ typedef struct
bool overwritable ;
#if CFG_FIFO_MUTEX
tu_fifo_mutex_t * const mutex;
tu_fifo_mutex_t mutex;
#endif
} tu_fifo_t;
#define TU_FIFO_DEF(_name, _depth, _type, _overwritable) /*, irq_mutex)*/ \
uint8_t _name##_buf[_depth*sizeof(_type)];\
tu_fifo_t _name = {\
.buffer = _name##_buf,\
.depth = _depth,\
.item_size = sizeof(_type),\
.overwritable = _overwritable,\
/*.irq = irq_mutex*/\
_mutex_declare(_mutex)\
#define TU_FIFO_DEF(_name, _depth, _type, _overwritable) \
uint8_t _name##_buf[_depth*sizeof(_type)]; \
tu_fifo_t _name = { \
.buffer = _name##_buf, \
.depth = _depth, \
.item_size = sizeof(_type), \
.overwritable = _overwritable, \
}
void tu_fifo_clear(tu_fifo_t *f);
void tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable);
bool tu_fifo_clear(tu_fifo_t *f);
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable);
#if CFG_FIFO_MUTEX
static inline void tu_fifo_config_mutex(tu_fifo_t *f, tu_fifo_mutex_t mutex_hdl)
{
f->mutex = mutex_hdl;
}
#endif
bool tu_fifo_write (tu_fifo_t* f, void const * p_data);
uint16_t tu_fifo_write_n (tu_fifo_t* f, void const * p_data, uint16_t count);