sync with tinyusb
This commit is contained in:
1 parent
eb16a0f535
commit
a265ec7b81
11 files changed
+271
-259
No files matched your search
@@ -44,7 +44,6 @@
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
#include "common/tusb_common.h"
|
||||
#include "cdc_device.h"
|
||||
#include "device/usbd_pvt.h"
|
||||
|
||||
@@ -72,13 +71,16 @@ typedef struct
|
||||
uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
|
||||
uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
|
||||
|
||||
osal_mutex_def_t rx_ff_mutex;
|
||||
osal_mutex_def_t tx_ff_mutex;
|
||||
|
||||
// Endpoint Transfer buffer
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EPSIZE];
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_CDC_EPSIZE];
|
||||
|
||||
}cdcd_interface_t;
|
||||
|
||||
#define ITF_BUS_RESET_SZ offsetof(cdcd_interface_t, wanted_char)
|
||||
#define ITF_MEM_RESET_SIZE offsetof(cdcd_interface_t, wanted_char)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
@@ -189,17 +191,22 @@ void cdcd_init(void)
|
||||
|
||||
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
|
||||
{
|
||||
_cdcd_itf[i].wanted_char = -1;
|
||||
cdcd_interface_t* ser = &_cdcd_itf[i];
|
||||
|
||||
ser->wanted_char = -1;
|
||||
|
||||
// default line coding is : stop bit = 1, parity = none, data bits = 8
|
||||
_cdcd_itf[i].line_coding.bit_rate = 115200;
|
||||
_cdcd_itf[i].line_coding.stop_bits = 0;
|
||||
_cdcd_itf[i].line_coding.parity = 0;
|
||||
_cdcd_itf[i].line_coding.data_bits = 8;
|
||||
ser->line_coding.bit_rate = 115200;
|
||||
ser->line_coding.stop_bits = 0;
|
||||
ser->line_coding.parity = 0;
|
||||
ser->line_coding.data_bits = 8;
|
||||
|
||||
// config fifo
|
||||
tu_fifo_config(&_cdcd_itf[i].rx_ff, _cdcd_itf[i].rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE, 1, true);
|
||||
tu_fifo_config(&_cdcd_itf[i].tx_ff, _cdcd_itf[i].tx_ff_buf, CFG_TUD_CDC_TX_BUFSIZE, 1, false);
|
||||
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);
|
||||
tu_fifo_config_mutex(&ser->tx_ff, osal_mutex_create(&ser->tx_ff_mutex));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,7 +216,7 @@ void cdcd_reset(uint8_t rhport)
|
||||
|
||||
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
|
||||
{
|
||||
tu_memclr(&_cdcd_itf[i], ITF_BUS_RESET_SZ);
|
||||
tu_memclr(&_cdcd_itf[i], ITF_MEM_RESET_SIZE);
|
||||
tu_fifo_clear(&_cdcd_itf[i].rx_ff);
|
||||
tu_fifo_clear(&_cdcd_itf[i].tx_ff);
|
||||
}
|
||||
|
||||
@@ -66,7 +66,6 @@
|
||||
#include "tusb_verify.h"
|
||||
#include "binary.h"
|
||||
#include "tusb_error.h"
|
||||
#include "tusb_fifo.h"
|
||||
#include "tusb_timeout.h"
|
||||
#include "tusb_types.h"
|
||||
|
||||
|
||||
@@ -36,24 +36,51 @@
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#include "tusb_fifo.h"
|
||||
#include "common/tusb_verify.h" // for ASSERT
|
||||
#include <string.h>
|
||||
|
||||
#include "osal/osal.h"
|
||||
#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
|
||||
|
||||
#define mutex_lock_if_needed(_ff) if (_ff->mutex) tu_fifo_mutex_lock(_ff->mutex)
|
||||
#define mutex_unlock_if_needed(_ff) if (_ff->mutex) tu_fifo_mutex_unlock(_ff->mutex)
|
||||
static bool 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)
|
||||
{
|
||||
if (f->mutex)
|
||||
{
|
||||
osal_mutex_unlock(f->mutex);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define mutex_lock_if_needed(_ff)
|
||||
#define mutex_unlock_if_needed(_ff)
|
||||
#define tu_fifo_lock(_ff) true
|
||||
#define tu_fifo_unlock(_ff)
|
||||
|
||||
#endif
|
||||
|
||||
void tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
|
||||
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
|
||||
{
|
||||
mutex_lock_if_needed(f);
|
||||
if ( !tu_fifo_lock(f) ) return false;
|
||||
|
||||
f->buffer = (uint8_t*) buffer;
|
||||
f->depth = depth;
|
||||
@@ -62,7 +89,9 @@ void tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
|
||||
|
||||
f->rd_idx = f->wr_idx = f->count = 0;
|
||||
|
||||
mutex_unlock_if_needed(f);
|
||||
tu_fifo_unlock(f);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -86,7 +115,7 @@ bool tu_fifo_read(tu_fifo_t* f, void * p_buffer)
|
||||
{
|
||||
if( tu_fifo_empty(f) ) return false;
|
||||
|
||||
mutex_lock_if_needed(f);
|
||||
if ( !tu_fifo_lock(f) ) return false;
|
||||
|
||||
memcpy(p_buffer,
|
||||
f->buffer + (f->rd_idx * f->item_size),
|
||||
@@ -94,7 +123,7 @@ bool tu_fifo_read(tu_fifo_t* f, void * p_buffer)
|
||||
f->rd_idx = (f->rd_idx + 1) % f->depth;
|
||||
f->count--;
|
||||
|
||||
mutex_unlock_if_needed(f);
|
||||
tu_fifo_unlock(f);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -122,8 +151,6 @@ uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t count)
|
||||
/* Limit up to fifo's count */
|
||||
if ( count > f->count ) count = f->count;
|
||||
|
||||
mutex_lock_if_needed(f);
|
||||
|
||||
/* Could copy up to 2 portions marked as 'x' if queue is wrapped around
|
||||
* case 1: ....RxxxxW.......
|
||||
* case 2: xxxxxW....Rxxxxxx
|
||||
@@ -138,8 +165,6 @@ uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t count)
|
||||
p_buf += f->item_size;
|
||||
}
|
||||
|
||||
mutex_unlock_if_needed(f);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -191,7 +216,7 @@ bool tu_fifo_write (tu_fifo_t* f, const void * p_data)
|
||||
{
|
||||
if ( tu_fifo_full(f) && !f->overwritable ) return false;
|
||||
|
||||
mutex_lock_if_needed(f);
|
||||
if ( !tu_fifo_lock(f) ) return false;
|
||||
|
||||
memcpy( f->buffer + (f->wr_idx * f->item_size),
|
||||
p_data,
|
||||
@@ -208,7 +233,7 @@ bool tu_fifo_write (tu_fifo_t* f, const void * p_data)
|
||||
f->count++;
|
||||
}
|
||||
|
||||
mutex_unlock_if_needed(f);
|
||||
tu_fifo_unlock(f);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -232,7 +257,7 @@ uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * p_data, uint16_t count)
|
||||
{
|
||||
if ( count == 0 ) return 0;
|
||||
|
||||
uint8_t* p_buf = (uint8_t*) p_data;
|
||||
uint8_t const* p_buf = (uint8_t const*) p_data;
|
||||
|
||||
uint16_t len = 0;
|
||||
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)
|
||||
Pointer to the FIFO buffer to manipulate
|
||||
*/
|
||||
/******************************************************************************/
|
||||
void tu_fifo_clear(tu_fifo_t *f)
|
||||
bool tu_fifo_clear(tu_fifo_t *f)
|
||||
{
|
||||
mutex_lock_if_needed(f);
|
||||
if ( !tu_fifo_lock(f) ) return false;
|
||||
|
||||
f->rd_idx = f->wr_idx = f->count = 0;
|
||||
|
||||
mutex_unlock_if_needed(f);
|
||||
tu_fifo_unlock(f);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -212,6 +212,18 @@ tusb_error_t usbd_init (void)
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
static void usbd_reset(uint8_t rhport)
|
||||
{
|
||||
tu_varclr(&_usbd_dev);
|
||||
memset(_usbd_dev.itf2drv, 0xff, sizeof(_usbd_dev.itf2drv)); // invalid mapping
|
||||
memset(_usbd_dev.ep2drv , 0xff, sizeof(_usbd_dev.ep2drv )); // invalid mapping
|
||||
|
||||
for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
|
||||
{
|
||||
if ( usbd_class_drivers[i].reset ) usbd_class_drivers[i].reset( rhport );
|
||||
}
|
||||
}
|
||||
|
||||
// To enable the TASK_ASSERT style (quick return on false condition) in a real RTOS, a task must act as a wrapper
|
||||
// and is used mainly to call subtasks. Within a subtask return statement can be called freely, the task with
|
||||
// forever loop cannot have any return at all.
|
||||
@@ -236,9 +248,9 @@ static tusb_error_t usbd_main_st(void)
|
||||
// Loop until there is no more events in the queue
|
||||
while (1)
|
||||
{
|
||||
tusb_error_t err;
|
||||
err = TUSB_ERROR_NONE;
|
||||
uint32_t err;
|
||||
|
||||
err = TUSB_ERROR_NONE;
|
||||
tu_memclr(&event, sizeof(dcd_event_t));
|
||||
|
||||
osal_queue_receive(_usbd_q, &event, OSAL_TIMEOUT_WAIT_FOREVER, &err);
|
||||
@@ -258,6 +270,20 @@ static tusb_error_t usbd_main_st(void)
|
||||
usbd_class_drivers[drv_id].xfer_cb( event.rhport, ep_addr, (tusb_event_t) event.xfer_complete.result, event.xfer_complete.len);
|
||||
}
|
||||
}
|
||||
else if (DCD_EVENT_BUS_RESET == event.event_id)
|
||||
{
|
||||
usbd_reset(event.rhport);
|
||||
osal_queue_reset(_usbd_q);
|
||||
osal_semaphore_reset(_usbd_ctrl_sem);
|
||||
}
|
||||
else if (DCD_EVENT_UNPLUGGED == event.event_id)
|
||||
{
|
||||
usbd_reset(event.rhport);
|
||||
osal_queue_reset(_usbd_q);
|
||||
osal_semaphore_reset(_usbd_ctrl_sem);
|
||||
|
||||
tud_umount_cb(); // invoke callback
|
||||
}
|
||||
else if (DCD_EVENT_SOF == event.event_id)
|
||||
{
|
||||
for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
|
||||
@@ -281,18 +307,6 @@ static tusb_error_t usbd_main_st(void)
|
||||
OSAL_SUBTASK_END
|
||||
}
|
||||
|
||||
static void usbd_reset(uint8_t rhport)
|
||||
{
|
||||
tu_varclr(&_usbd_dev);
|
||||
memset(_usbd_dev.itf2drv, 0xff, sizeof(_usbd_dev.itf2drv)); // invalid mapping
|
||||
memset(_usbd_dev.ep2drv , 0xff, sizeof(_usbd_dev.ep2drv )); // invalid mapping
|
||||
|
||||
for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
|
||||
{
|
||||
if ( usbd_class_drivers[i].reset ) usbd_class_drivers[i].reset( rhport );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// CONTROL REQUEST
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -539,28 +553,9 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr)
|
||||
switch (event->event_id)
|
||||
{
|
||||
case DCD_EVENT_BUS_RESET:
|
||||
usbd_reset(rhport);
|
||||
|
||||
osal_queue_flush(_usbd_q);
|
||||
osal_semaphore_reset_isr(_usbd_ctrl_sem);
|
||||
break;
|
||||
|
||||
case DCD_EVENT_SOF:
|
||||
{
|
||||
#if 0
|
||||
dcd_event_t task_event =
|
||||
{
|
||||
.rhport = rhport,
|
||||
.event_id = DCD_EVENT_SOF,
|
||||
};
|
||||
osal_queue_send(_usbd_q, &task_event, in_isr);
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
|
||||
case DCD_EVENT_UNPLUGGED:
|
||||
usbd_reset(rhport);
|
||||
tud_umount_cb(); // invoke callback
|
||||
case DCD_EVENT_SOF:
|
||||
osal_queue_send(_usbd_q, event, in_isr);
|
||||
break;
|
||||
|
||||
case DCD_EVENT_SUSPENDED:
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#define USBD_PVT_H_
|
||||
|
||||
#include "osal/osal.h"
|
||||
#include "common/tusb_fifo.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -68,7 +69,7 @@ tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* p_d
|
||||
#define usbd_control_xfer_st(_rhport, _dir, _buffer, _len) \
|
||||
do { \
|
||||
if (_len) { \
|
||||
tusb_error_t err; \
|
||||
uint32_t err; \
|
||||
dcd_control_xfer(_rhport, _dir, (uint8_t*) _buffer, _len); \
|
||||
osal_semaphore_wait( _usbd_ctrl_sem, OSAL_TIMEOUT_CONTROL_XFER, &err ); \
|
||||
STASK_ASSERT_ERR( err ); \
|
||||
|
||||
@@ -39,7 +39,6 @@
|
||||
#include "tusb_option.h"
|
||||
#include "osal.h"
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TICK API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
@@ -79,22 +79,22 @@ typedef void (*osal_task_func_t)( void * );
|
||||
* Queue
|
||||
* osal_queue_def_t, osal_queue_t
|
||||
* osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
|
||||
* osal_queue_receive (osal_queue_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error)
|
||||
* osal_queue_receive (osal_queue_t const queue_hdl, void *p_data, uint32_t msec, uint32_t *p_error)
|
||||
* bool osal_queue_send(osal_queue_t const queue_hdl, void const * data, bool in_isr)
|
||||
* osal_queue_flush() TODO remove
|
||||
* osal_queue_reset()
|
||||
*
|
||||
* Semaphore
|
||||
* osal_semaphore_def_t, osal_semaphore_t
|
||||
* osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
|
||||
* bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
|
||||
* void osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec, tusb_error_t *p_error)
|
||||
* void osal_semaphore_reset_isr(osal_semaphore_t const sem_hdl)
|
||||
* void osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec, uint32_t *p_error)
|
||||
* void osal_semaphore_reset(osal_semaphore_t const sem_hdl)
|
||||
*
|
||||
* Mutex
|
||||
* osal_mutex_t
|
||||
* osal_mutex_create()
|
||||
* bool osal_mutex_release(osal_mutex_t mutex_hdl)
|
||||
* void osal_mutex_wait(osal_mutex_t mutex_hdl, uint32_t msec, tusb_error_t *p_error)
|
||||
* osal_mutex_create(osal_mutex_def_t* mdef)
|
||||
* bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
|
||||
* void osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec, uint32_t *p_error)
|
||||
*/
|
||||
|
||||
#if CFG_TUSB_OS == OPT_OS_FREERTOS
|
||||
|
||||
@@ -91,6 +91,51 @@ static inline void osal_task_delay(uint32_t msec)
|
||||
vTaskDelay( pdMS_TO_TICKS(msec) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Semaphore API
|
||||
//--------------------------------------------------------------------+
|
||||
typedef StaticSemaphore_t osal_semaphore_def_t;
|
||||
typedef SemaphoreHandle_t osal_semaphore_t;
|
||||
|
||||
static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
|
||||
{
|
||||
return xSemaphoreCreateBinaryStatic(semdef);
|
||||
}
|
||||
|
||||
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
|
||||
{
|
||||
return in_isr ? xSemaphoreGiveFromISR(sem_hdl, NULL) : xSemaphoreGive(sem_hdl);
|
||||
}
|
||||
|
||||
static inline void osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec, uint32_t *err)
|
||||
{
|
||||
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec);
|
||||
(*err) = (xSemaphoreTake(sem_hdl, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
|
||||
}
|
||||
|
||||
static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl)
|
||||
{
|
||||
xQueueReset(sem_hdl);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API (priority inheritance)
|
||||
//--------------------------------------------------------------------+
|
||||
typedef StaticSemaphore_t osal_mutex_def_t;
|
||||
typedef SemaphoreHandle_t osal_mutex_t;
|
||||
|
||||
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
|
||||
{
|
||||
return xSemaphoreCreateMutexStatic(mdef);
|
||||
}
|
||||
|
||||
#define osal_mutex_lock osal_semaphore_wait
|
||||
|
||||
static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
|
||||
{
|
||||
return xSemaphoreGive(mutex_hdl);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// QUEUE API
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -114,10 +159,10 @@ static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
|
||||
return xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq);
|
||||
}
|
||||
|
||||
static inline void osal_queue_receive (osal_queue_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error)
|
||||
static inline void osal_queue_receive (osal_queue_t const queue_hdl, void *p_data, uint32_t msec, uint32_t *err)
|
||||
{
|
||||
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec);
|
||||
(*p_error) = ( xQueueReceive(queue_hdl, p_data, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
|
||||
(*err) = ( xQueueReceive(queue_hdl, p_data, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
|
||||
}
|
||||
|
||||
static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data, bool in_isr)
|
||||
@@ -125,58 +170,11 @@ static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * da
|
||||
return in_isr ? xQueueSendToBackFromISR(queue_hdl, data, NULL) : xQueueSendToBack(queue_hdl, data, OSAL_TIMEOUT_WAIT_FOREVER);
|
||||
}
|
||||
|
||||
static inline void osal_queue_flush(osal_queue_t const queue_hdl)
|
||||
static inline void osal_queue_reset(osal_queue_t const queue_hdl)
|
||||
{
|
||||
// TODO move to thread context
|
||||
// xQueueReset(queue_hdl);
|
||||
xQueueReset(queue_hdl);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Semaphore API
|
||||
//--------------------------------------------------------------------+
|
||||
typedef StaticSemaphore_t osal_semaphore_def_t;
|
||||
typedef SemaphoreHandle_t osal_semaphore_t;
|
||||
|
||||
static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
|
||||
{
|
||||
return xSemaphoreCreateBinaryStatic(semdef);
|
||||
}
|
||||
|
||||
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
|
||||
{
|
||||
return in_isr ? xSemaphoreGiveFromISR(sem_hdl, NULL) : xSemaphoreGive(sem_hdl);
|
||||
}
|
||||
|
||||
static inline void osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec, tusb_error_t *p_error)
|
||||
{
|
||||
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec);
|
||||
(*p_error) = (xSemaphoreTake(sem_hdl, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
|
||||
}
|
||||
|
||||
static inline void osal_semaphore_reset_isr(osal_semaphore_t const sem_hdl)
|
||||
{
|
||||
xSemaphoreTakeFromISR(sem_hdl, NULL);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API (priority inheritance)
|
||||
//--------------------------------------------------------------------+
|
||||
typedef SemaphoreHandle_t osal_mutex_t;
|
||||
|
||||
#define osal_mutex_create(x) xSemaphoreCreateMutex()
|
||||
|
||||
static inline bool osal_mutex_release(osal_mutex_t mutex_hdl)
|
||||
{
|
||||
return xSemaphoreGive(mutex_hdl);
|
||||
}
|
||||
|
||||
static inline void osal_mutex_wait(osal_mutex_t mutex_hdl, uint32_t msec, tusb_error_t *p_error)
|
||||
{
|
||||
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec);
|
||||
(*p_error) = (xSemaphoreTake(mutex_hdl, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT);
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -43,8 +43,6 @@
|
||||
#ifndef _TUSB_OSAL_NONE_H_
|
||||
#define _TUSB_OSAL_NONE_H_
|
||||
|
||||
#include "common/tusb_fifo.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -78,12 +76,12 @@ static inline bool osal_task_create(osal_task_def_t* taskdef)
|
||||
#define TASK_RESTART \
|
||||
_state = 0
|
||||
|
||||
#define osal_task_delay(msec) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if ( _timeout + msec > tusb_hal_millis() ) \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
#define osal_task_delay(_msec) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if ( _timeout + (_msec) > tusb_hal_millis() ) \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
}while(0)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -119,9 +117,85 @@ static inline bool osal_task_create(osal_task_def_t* taskdef)
|
||||
#define STASK_ASSERT(_cond) TU_VERIFY_HDLR(_cond, TU_BREAKPOINT(); TASK_RESTART, TUSB_ERROR_FAILED)
|
||||
#define STASK_ASSERT_HDLR(_cond, _func) TU_VERIFY_HDLR(_cond, TU_BREAKPOINT(); _func; TASK_RESTART, TUSB_ERROR_FAILED)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Semaphore API
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct
|
||||
{
|
||||
volatile uint16_t count;
|
||||
}osal_semaphore_def_t;
|
||||
|
||||
typedef osal_semaphore_def_t* osal_semaphore_t;
|
||||
|
||||
static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
|
||||
{
|
||||
semdef->count = 0;
|
||||
return semdef;
|
||||
}
|
||||
|
||||
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
|
||||
{
|
||||
(void) in_isr;
|
||||
sem_hdl->count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
|
||||
{
|
||||
sem_hdl->count = 0;
|
||||
}
|
||||
|
||||
#define osal_semaphore_wait(_sem_hdl, _msec, _err) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if( (_sem_hdl)->count == 0 ) { \
|
||||
if ( ((_msec) != OSAL_TIMEOUT_WAIT_FOREVER) && (_timeout + (_msec) <= tusb_hal_millis()) ) \
|
||||
*(_err) = TUSB_ERROR_OSAL_TIMEOUT; \
|
||||
else \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
} else{ \
|
||||
/* Enter critical ? */ \
|
||||
(_sem_hdl)->count--; \
|
||||
/* Exit critical ? */ \
|
||||
*(_err) = TUSB_ERROR_NONE; \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API
|
||||
// Within tinyusb, mutex is never used in ISR context
|
||||
//--------------------------------------------------------------------+
|
||||
typedef osal_semaphore_def_t osal_mutex_def_t;
|
||||
typedef osal_semaphore_t osal_mutex_t;
|
||||
|
||||
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
|
||||
{
|
||||
mdef->count = 1;
|
||||
return 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
|
||||
//--------------------------------------------------------------------+
|
||||
#include "common/tusb_fifo.h"
|
||||
|
||||
#define OSAL_QUEUE_DEF(_name, _depth, _type) TU_FIFO_DEF(_name, _depth, _type, false)
|
||||
|
||||
typedef tu_fifo_t osal_queue_def_t;
|
||||
@@ -139,97 +213,28 @@ static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * da
|
||||
return tu_fifo_write( (tu_fifo_t*) queue_hdl, data);
|
||||
}
|
||||
|
||||
static inline void osal_queue_flush(osal_queue_t const queue_hdl)
|
||||
static inline void osal_queue_reset(osal_queue_t const queue_hdl)
|
||||
{
|
||||
queue_hdl->count = queue_hdl->rd_idx = queue_hdl->wr_idx = 0;
|
||||
}
|
||||
|
||||
#define osal_queue_receive(queue_hdl, p_data, msec, p_error) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if( queue_hdl->count == 0 ) { \
|
||||
if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && ( _timeout + msec <= tusb_hal_millis()) ) \
|
||||
*(p_error) = TUSB_ERROR_OSAL_TIMEOUT; \
|
||||
else \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
} else{ \
|
||||
/*tusb_hal_int_disable_all();*/ \
|
||||
tu_fifo_read(queue_hdl, p_data); \
|
||||
/*tusb_hal_int_enable_all();*/ \
|
||||
*(p_error) = TUSB_ERROR_NONE; \
|
||||
} \
|
||||
#define osal_queue_receive(_q_hdl, p_data, _msec, _err) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if( (_q_hdl)->count == 0 ) { \
|
||||
if ( ((_msec) != OSAL_TIMEOUT_WAIT_FOREVER) && ( _timeout + (_msec) <= tusb_hal_millis()) ) \
|
||||
*(_err) = TUSB_ERROR_OSAL_TIMEOUT; \
|
||||
else \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
} else{ \
|
||||
/* Enter critical ? */ \
|
||||
tu_fifo_read(_q_hdl, p_data); \
|
||||
/* Exit critical ? */ \
|
||||
*(_err) = TUSB_ERROR_NONE; \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Semaphore API
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct
|
||||
{
|
||||
volatile uint16_t count;
|
||||
uint16_t max_count;
|
||||
}osal_semaphore_def_t;
|
||||
|
||||
typedef osal_semaphore_def_t* osal_semaphore_t;
|
||||
|
||||
|
||||
static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
|
||||
{
|
||||
semdef->count = 0;
|
||||
semdef->max_count = 1;
|
||||
return semdef;
|
||||
}
|
||||
|
||||
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
|
||||
{
|
||||
(void) in_isr;
|
||||
if (sem_hdl->count < sem_hdl->max_count ) sem_hdl->count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void osal_semaphore_reset_isr(osal_semaphore_t sem_hdl)
|
||||
{
|
||||
sem_hdl->count = 0;
|
||||
}
|
||||
|
||||
#define osal_semaphore_wait(sem_hdl, msec, p_error) \
|
||||
do { \
|
||||
_timeout = tusb_hal_millis(); \
|
||||
_state = __LINE__; case __LINE__: \
|
||||
if( sem_hdl->count == 0 ) { \
|
||||
if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && (_timeout + msec <= tusb_hal_millis()) ) \
|
||||
*(p_error) = TUSB_ERROR_OSAL_TIMEOUT; \
|
||||
else \
|
||||
return TUSB_ERROR_OSAL_WAITING; \
|
||||
} else{ \
|
||||
/*tusb_hal_int_disable_all();*/ \
|
||||
sem_hdl->count--; \
|
||||
/*tusb_hal_int_enable_all();*/ \
|
||||
*(p_error) = TUSB_ERROR_NONE; \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API (priority inheritance)
|
||||
//--------------------------------------------------------------------+
|
||||
#if 0
|
||||
typedef osal_semaphore_t osal_mutex_t;
|
||||
|
||||
static inline osal_mutex_t osal_mutex_create(void)
|
||||
{
|
||||
return osal_semaphore_create(1, 0);
|
||||
}
|
||||
|
||||
static inline bool osal_mutex_release(osal_mutex_t mutex_hdl)
|
||||
{
|
||||
return osal_semaphore_post(mutex_hdl);
|
||||
}
|
||||
|
||||
#define osal_mutex_wait osal_semaphore_wait
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
#include "common/tusb_common.h"
|
||||
#include "tusb_hal.h"
|
||||
#include "osal/osal.h"
|
||||
#include "common/tusb_fifo.h"
|
||||
|
||||
//------------- HOST -------------//
|
||||
#if MODE_HOST_SUPPORTED
|
||||
|
||||
Reference in new issue
Block a user