summaryrefslogtreecommitdiff
path: root/demos/host/src
diff options
context:
space:
mode:
authorhathach <[email protected]>2013-04-28 00:00:48 +0700
committerhathach <[email protected]>2013-04-28 00:00:48 +0700
commitfdd98e2fa954af916b2b61b4852d21af11477cd0 (patch)
treeb6622ec807663c1ab4d4c7a41f006bbd9b070f00 /demos/host/src
parentd68f882a251f3f3deb3fad38eab61cc717429a07 (diff)
clean up main
Diffstat (limited to 'demos/host/src')
-rw-r--r--demos/host/src/main.c82
1 files changed, 57 insertions, 25 deletions
diff --git a/demos/host/src/main.c b/demos/host/src/main.c
index a88573fb3..c4f318de1 100644
--- a/demos/host/src/main.c
+++ b/demos/host/src/main.c
@@ -64,19 +64,38 @@
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
-void print_greeting(void);
-
OSAL_TASK_FUNCTION( led_blinking_task ) (void* p_task_para);
OSAL_TASK_DEF(led_blinking_task_def, "led blinking", led_blinking_task, 128, LED_BLINKING_APP_TASK_PRIO);
+void print_greeting(void);
+static inline void wait_blocking_ms(uint32_t ms);
+
//--------------------------------------------------------------------+
// IMPLEMENTATION
//--------------------------------------------------------------------+
+#if TUSB_CFG_OS == TUSB_OS_NONE
+// like a real RTOS, this function is a main loop invoking each task in application and never return
+void os_none_start_scheduler(void)
+{
+ while (1)
+ {
+ tusb_task_runner();
+ keyboard_app_task(NULL);
+ mouse_app_task(NULL);
+ led_blinking_task(NULL);
+ }
+}
+#endif
+
+
int main(void)
{
board_init();
+ // TODO blocking wait --> systick handler --> ...... freeRTOS hardfault
+ //wait_blocking_ms(1000); // wait a bit for power stable
+
// print_greeting(); TODO uart output before freeRTOS scheduler start will lead to hardfault
// find a way to fix this as tusb_init can output to uart when an error occurred
@@ -98,15 +117,7 @@ int main(void)
vTaskStartScheduler();
#elif TUSB_CFG_OS == TUSB_OS_NONE
- print_greeting();
-
- while (1)
- {
- tusb_task_runner();
- keyboard_app_task(NULL);
- mouse_app_task(NULL);
- led_blinking_task(NULL);
- }
+ os_none_start_scheduler();
#else
#error need to start RTOS schduler
#endif
@@ -122,6 +133,33 @@ int main(void)
}
//--------------------------------------------------------------------+
+// BLINKING TASK
+//--------------------------------------------------------------------+
+OSAL_TASK_FUNCTION( led_blinking_task ) (void* p_task_para)
+{
+ // task init, only executed exactly one time, real RTOS does not need this but none OS does
+ {
+ static bool is_init = false;
+ if (!is_init)
+ {
+ is_init = true;
+ print_greeting();
+ }
+ }
+
+ static uint32_t led_on_mask = 0;
+
+ OSAL_TASK_LOOP_BEGIN
+
+ osal_task_delay(1000);
+
+ board_leds(led_on_mask, 1 - led_on_mask);
+ led_on_mask = 1 - led_on_mask; // toggle
+
+ OSAL_TASK_LOOP_END
+}
+
+//--------------------------------------------------------------------+
// HELPER FUNCTION
//--------------------------------------------------------------------+
void print_greeting(void)
@@ -135,20 +173,14 @@ void print_greeting(void)
);
}
-OSAL_TASK_FUNCTION( led_blinking_task ) (void* p_task_para)
+static inline void wait_blocking_us(volatile uint32_t us)
{
- static uint32_t led_on_mask = 0;
-
-#if TUSB_CFG_OS != TUSB_OS_NONE // TODO abstract to approriate place
- print_greeting();
-#endif
-
- OSAL_TASK_LOOP_BEGIN
-
- osal_task_delay(1000);
-
- board_leds(led_on_mask, 1 - led_on_mask);
- led_on_mask = 1 - led_on_mask; // toggle
+ us *= (SystemCoreClock / 1000000) / 3;
+ while(us--);
+}
- OSAL_TASK_LOOP_END
+static inline void wait_blocking_ms(uint32_t ms)
+{
+ wait_blocking_us(ms * 1000);
}
+