add timeout to queue_receive and update test code for it

This commit is contained in:
hathach 2013-02-01 12:39:39 +07:00
parent aab92b40d2
commit c24d461d19
4 changed files with 31 additions and 10 deletions

View File

@ -166,24 +166,30 @@ void test_task_with_semaphore(void)
void sample_task_with_queue(void)
{
uint32_t data;
tusb_error_t error;
OSAL_TASK_LOOP
{
OSAL_TASK_LOOP_BEGIN
statements[0]++;
osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER);
osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER, &error);
TEST_ASSERT_EQUAL(0x1111, data);
statements[1]++;
osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER);
osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER, &error);
TEST_ASSERT_EQUAL(0x2222, data);
statements[2]++;
osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER);
osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER, &error);
TEST_ASSERT_EQUAL(0x3333, data);
statements[3]++;
osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_NORMAL, &error);
statements[4]++;
TEST_ASSERT_EQUAL(TUSB_ERROR_OSAL_TIMEOUT, error);
OSAL_TASK_LOOP_END
}
}
@ -211,5 +217,16 @@ void test_task_with_queue(void)
TEST_ASSERT_EQUAL(1, statements[2]);
TEST_ASSERT_EQUAL(1, statements[3]);
// timeout
for(uint32_t i=0; i<(OSAL_TIMEOUT_NORMAL*TUSB_CFG_OS_TICKS_PER_SECOND)/1000 ; i++) // not enough time
osal_tick_tock();
sample_task_with_queue();
TEST_ASSERT_EQUAL(0, statements[4]);
osal_tick_tock();
sample_task_with_queue();
// reach end of task loop, back to beginning
sample_task_with_queue();
TEST_ASSERT_EQUAL(2, statements[0]);
}

View File

@ -61,8 +61,8 @@
enum
{
OSAL_TIMEOUT_NOTIMEOUT = 0, // for use within ISR, return immediately
OSAL_TIMEOUT_WAIT_FOREVER = 1,
OSAL_TIMEOUT_NORMAL = 10 // default is 10 msec
OSAL_TIMEOUT_NORMAL = 10, // default is 10 msec
OSAL_TIMEOUT_WAIT_FOREVER = 0xFFFF0000
};
typedef enum {

View File

@ -54,7 +54,7 @@ static volatile uint32_t osal_tick_current = 0;
//--------------------------------------------------------------------+
// IMPLEMENTATION
//--------------------------------------------------------------------+
volatile uint32_t osal_tick_get(void)
uint32_t osal_tick_get(void)
{
return osal_tick_current;
}

View File

@ -175,12 +175,16 @@ static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl,
return TUSB_ERROR_NONE;
}
#define osal_queue_receive(queue_hdl, p_data, msec) \
#define osal_queue_receive(queue_hdl, p_data, msec, p_error) \
do {\
timeout = osal_tick_get();\
state = __LINE__; case __LINE__:\
if( queue_hdl-> count == 0 ) \
return;\
else{\
if( queue_hdl-> count == 0 ) {\
if ( timeout + osal_tick_from_msec(msec) < osal_tick_get() ) /* time out */ \
*(p_error) = TUSB_ERROR_OSAL_TIMEOUT;\
else\
return;\
} else{\
/*TODO mutex lock hal_interrupt_disable */\
*p_data = queue_hdl->buffer[queue_hdl->rd_idx];\
queue_hdl->rd_idx = (queue_hdl->rd_idx + 1) % queue_hdl->depth;\