summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhathach <[email protected]>2013-01-30 23:29:27 +0700
committerhathach <[email protected]>2013-01-30 23:29:27 +0700
commit658b2b970c23c5f88b369869d947e047f96e2f72 (patch)
treea01638c1db85a8e10f3b23938b3880d98fa4b85e
parentc1bf187dad2b81e15ac810aa756a68f10541a47f (diff)
change semaphore to volatile uint8_t
add osal_queue_receive code and its test *NOTE* - consideration for semaphore & queue with hal_interrupt_disable/hal_interrupt_enable as safe guard
-rw-r--r--tests/test/test_osal_none.c73
-rw-r--r--tinyusb/osal/osal_none.h24
2 files changed, 74 insertions, 23 deletions
diff --git a/tests/test/test_osal_none.c b/tests/test/test_osal_none.c
index 0417c9d66..8ae0bfd51 100644
--- a/tests/test/test_osal_none.c
+++ b/tests/test/test_osal_none.c
@@ -41,7 +41,9 @@
#include "unity.h"
#include "osal_none.h"
+
#define QUEUE_DEPTH 10
+
uint32_t statements[10];
osal_semaphore_t sem;
@@ -132,7 +134,7 @@ void test_task_with_semaphore(void)
sample_task_semaphore();
TEST_ASSERT_EQUAL(1, statements[0]);
- // several invoke after posting semaphore
+ // invoke after posting semaphore
osal_semaphore_post(sem_hdl);
sample_task_semaphore();
TEST_ASSERT_EQUAL(1, statements[1]);
@@ -148,24 +150,53 @@ void test_task_with_semaphore(void)
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
-// }
-//}
+void sample_task_with_queue(void)
+{
+ uint32_t data;
+ OSAL_TASK_LOOP
+ {
+ OSAL_TASK_LOOP_BEGIN
+
+ statements[0]++;
+
+ osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER);
+ TEST_ASSERT_EQUAL(0x1111, data);
+ statements[1]++;
+
+ osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER);
+ TEST_ASSERT_EQUAL(0x2222, data);
+ statements[2]++;
+
+ osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER);
+ TEST_ASSERT_EQUAL(0x3333, data);
+ statements[3]++;
+
+ OSAL_TASK_LOOP_END
+ }
+}
+
+void test_task_with_queue(void)
+{
+ uint32_t i = 0;
+
+ sample_task_with_queue();
+ // several invoke before queue is available
+ for(i=0; i<10; i++)
+ sample_task_with_queue();
+ TEST_ASSERT_EQUAL(1, statements[0]);
+
+ // invoke after posting semaphore
+ osal_queue_send(queue_hdl, 0x1111);
+ sample_task_with_queue();
+ TEST_ASSERT_EQUAL(1, statements[1]);
+ sample_task_with_queue();
+ TEST_ASSERT_EQUAL(1, statements[1]);
+
+ osal_queue_send(queue_hdl, 0x2222);
+ osal_queue_send(queue_hdl, 0x3333);
+ sample_task_with_queue();
+ TEST_ASSERT_EQUAL(1, statements[2]);
+ TEST_ASSERT_EQUAL(1, statements[3]);
+
+}
diff --git a/tinyusb/osal/osal_none.h b/tinyusb/osal/osal_none.h
index f089294e4..99f145a37 100644
--- a/tinyusb/osal/osal_none.h
+++ b/tinyusb/osal/osal_none.h
@@ -88,7 +88,7 @@ typedef uint32_t osal_timeout_t;
//--------------------------------------------------------------------+
// Semaphore API
//--------------------------------------------------------------------+
-typedef uint8_t osal_semaphore_t;
+typedef volatile 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 p_sem) ATTR_ALWAYS_INLINE;
@@ -112,7 +112,7 @@ static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t const se
if( (*sem_hdl) == 0 ) \
return;\
else\
- (*sem_hdl)--;\
+ (*sem_hdl)--; /*TODO mutex hal_interrupt_disable consideration*/\
}while(0)
//--------------------------------------------------------------------+
@@ -143,9 +143,12 @@ static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue
return (osal_queue_handle_t) p_queue;
}
+// when queue is full, it will overwrite the oldest data in the 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)
{
+ //TODO mutex lock hal_interrupt_disable
+
queue_hdl->buffer[queue_hdl->wr_idx] = data;
queue_hdl->wr_idx = (queue_hdl->wr_idx + 1) % queue_hdl->depth;
@@ -157,9 +160,26 @@ static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl,
queue_hdl->count++;
}
+ //TODO mutex unlock hal_interrupt_enable
+
return TUSB_ERROR_NONE;
}
+#define osal_queue_receive(queue_hdl, p_data, msec) \
+ do {\
+ state = __LINE__; case __LINE__:\
+ if( queue_hdl-> count == 0 ) \
+ 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;\
+ queue_hdl->count--;\
+ /*TODO mutex unlock hal_interrupt_enable */\
+ }\
+ }while(0)
+
+
// queue_send, queue_receive
#ifdef __cplusplus