add osal queue service & its test code

- create, send
This commit is contained in:
hathach 2013-01-30 17:53:37 +07:00
parent 9c9fa182d4
commit c1bf187dad
3 changed files with 84 additions and 19 deletions

View File

@ -41,16 +41,20 @@
#include "unity.h"
#include "osal_none.h"
#define QUEUE_DEPTH 10
uint32_t statements[10];
osal_semaphore_t sem;
osal_semaphore_handle_t sem_hdl;
OSAL_DEF_QUEUE(queue, QUEUE_DEPTH, uin32_t);
osal_queue_handle_t queue_hdl;
void setUp(void)
{
memset(statements, 0, sizeof(statements));
sem = 0;
sem_hdl = osal_semaphore_create(&sem);
queue_hdl = osal_queue_create(&queue);
}
void tearDown(void)
@ -72,16 +76,30 @@ void test_semaphore_post(void)
osal_semaphore_post(sem_hdl);
TEST_ASSERT_EQUAL(1, sem);
}
// blocking service such as semaphore wait need to be invoked within a task's loop
//--------------------------------------------------------------------+
// Queue
//--------------------------------------------------------------------+
void test_queue_create(void)
{
TEST_IGNORE();
// osal_queue_put();
TEST_ASSERT_EQUAL_PTR(&queue, queue_hdl);
TEST_ASSERT_EQUAL(QUEUE_DEPTH, queue_hdl->depth);
TEST_ASSERT_EQUAL_PTR(queue_buffer, queue_hdl->buffer);
TEST_ASSERT_EQUAL(0, queue_hdl->count);
TEST_ASSERT_EQUAL(0, queue_hdl->wr_idx);
TEST_ASSERT_EQUAL(0, queue_hdl->rd_idx);
}
void test_queue_send(void)
{
uint32_t const item = 0x1234;
osal_queue_send(queue_hdl, item);
TEST_ASSERT_EQUAL(1, queue_hdl->count);
TEST_ASSERT_EQUAL(item, queue_hdl->buffer[queue_hdl->rd_idx]);
}
// blocking service such as semaphore wait need to be invoked within a task's loop
//--------------------------------------------------------------------+
// TASK
//--------------------------------------------------------------------+
@ -128,8 +146,26 @@ void test_task_with_semaphore(void)
// reach end of task loop, back to beginning
sample_task_semaphore();
TEST_ASSERT_EQUAL(2, statements[0]);
}
//void sample_task_with_queue(void)
//{
// OSAL_TASK_LOOP
// {
// OSAL_TASK_LOOP_BEGIN
//
// statements[0]++;
//
// osal_queue_receive(queue_hdl, OSAL_TIMEOUT_WAIT_FOREVER);
// statements[1]++;
//
// osal_queue_receive(queue_hdl, OSAL_TIMEOUT_WAIT_FOREVER);
// statements[2]++;
//
// osal_queue_receive(queue_hdl, OSAL_TIMEOUT_WAIT_FOREVER);
// statements[3]++;
//
// OSAL_TASK_LOOP_END
// }
//}

View File

@ -86,7 +86,7 @@ tusb_error_t osal_semaphore_post(osal_semaphore_handle_t const sem_hdl);
//------------- Queue -------------//
typedef uint32_t osal_queue_t;
typedef void* osal_queue_handle_t;
osal_queue_handle_t osal_queue_create(osal_queue_t *queue, uint8_t *buffer);
osal_queue_handle_t osal_queue_create(osal_queue_t *queue);
#endif

View File

@ -91,11 +91,11 @@ typedef uint32_t osal_timeout_t;
typedef uint8_t osal_semaphore_t;
typedef osal_semaphore_t * osal_semaphore_handle_t;
static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * const sem) ATTR_CONST ATTR_ALWAYS_INLINE;
static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * const sem)
static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * const p_sem) ATTR_ALWAYS_INLINE;
static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * const p_sem)
{
(*sem) = 0;
return (osal_semaphore_handle_t) sem;
(*p_sem) = 0;
return (osal_semaphore_handle_t) p_sem;
}
static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t const sem_hdl) ATTR_ALWAYS_INLINE;
@ -119,19 +119,48 @@ static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t const se
// QUEUE API
//--------------------------------------------------------------------+
typedef struct{
uint8_t *buf ; ///< buffer pointer
uint16_t size ; ///< buffer size
volatile uint16_t len ; ///< bytes in fifo
volatile uint16_t wr_ptr ; ///< write pointer
volatile uint16_t rd_ptr ; ///< read pointer
uint32_t * const buffer ; ///< buffer pointer
uint16_t const depth ; ///< buffer size
volatile uint16_t count ; ///< bytes in fifo
volatile uint16_t wr_idx ; ///< write pointer
volatile uint16_t rd_idx ; ///< read pointer
} osal_queue_t;
typedef osal_queue_t * osal_queue_handle_t;
// use to declare a queue, within the scope of tinyusb, should only use primitive type only
#define OSAL_DEF_QUEUE(name, queue_depth, type)\
uint32_t name##_buffer[queue_depth];\
osal_queue_t name = {\
.buffer = name##_buffer,\
.depth = queue_depth\
}
static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue) ATTR_ALWAYS_INLINE;
static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue)
{
p_queue->count = p_queue->wr_idx = p_queue->rd_idx = 0;
return (osal_queue_handle_t) p_queue;
}
static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, uint32_t data) ATTR_ALWAYS_INLINE;
static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, uint32_t data)
{
queue_hdl->buffer[queue_hdl->wr_idx] = data;
queue_hdl->wr_idx = (queue_hdl->wr_idx + 1) % queue_hdl->depth;
if (queue_hdl->depth == queue_hdl->count) // queue is full, 1st rd is overwritten
{
queue_hdl->rd_idx = queue_hdl->wr_idx; // keep full state
}else
{
queue_hdl->count++;
}
return TUSB_ERROR_NONE;
}
// queue_send, queue_receive
#define OSAL_DEF_QUEUE(name, size)\
osal_queue_t name;\
uint8_t buffer_##name[size]
#ifdef __cplusplus
}