Eric Ebert
/
CoOS
CoOS Demonstrator adapted to mbed Hardware.
main.cpp@0:57690853989a, 2010-12-03 (annotated)
- Committer:
- ericebert
- Date:
- Fri Dec 03 19:45:30 2010 +0000
- Revision:
- 0:57690853989a
Some basic LED-Flashing works in the CoOS-RTOS using Tasks
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ericebert | 0:57690853989a | 1 | #include "mbed.h" |
ericebert | 0:57690853989a | 2 | #include <CoOS.h> |
ericebert | 0:57690853989a | 3 | |
ericebert | 0:57690853989a | 4 | Serial pc(USBTX, USBRX); |
ericebert | 0:57690853989a | 5 | |
ericebert | 0:57690853989a | 6 | /*---------------------------------- Define ---------------------------------*/ |
ericebert | 0:57690853989a | 7 | #define STK_SIZE 128 /*!< Stack size. */ |
ericebert | 0:57690853989a | 8 | #define INIT_TASK_PRIO 11 /*!< Priority of "init_task" task. */ |
ericebert | 0:57690853989a | 9 | #define A_TASK_PRIO 10 /*!< Priority of "A" task. */ |
ericebert | 0:57690853989a | 10 | #define B_TASK_PRIO 10 /*!< Priority of "B" task. */ |
ericebert | 0:57690853989a | 11 | #define CLK_TASK_PRIO 10 /*!< Priority of "clock" task. */ |
ericebert | 0:57690853989a | 12 | |
ericebert | 0:57690853989a | 13 | /*---------------------------------- Variable Define -------------------------*/ |
ericebert | 0:57690853989a | 14 | OS_STK init_task_stk[STK_SIZE]; /*!< The stack of "init_task" task. */ |
ericebert | 0:57690853989a | 15 | OS_STK a_task_stk[STK_SIZE]; /*!< The stack of "a" task. */ |
ericebert | 0:57690853989a | 16 | OS_STK b_task_stk[STK_SIZE]; /*!< The stack of "b" task. */ |
ericebert | 0:57690853989a | 17 | OS_STK clk_task_stk[STK_SIZE]; /*!< The stack of "clcok" task. */ |
ericebert | 0:57690853989a | 18 | |
ericebert | 0:57690853989a | 19 | OS_MutexID mut_LED; /*!< Mutex id related to LED. */ |
ericebert | 0:57690853989a | 20 | |
ericebert | 0:57690853989a | 21 | OS_FlagID a_flg; |
ericebert | 0:57690853989a | 22 | OS_FlagID b_flg; |
ericebert | 0:57690853989a | 23 | U8 errInfo[32]; /*!< Save error code. */ |
ericebert | 0:57690853989a | 24 | |
ericebert | 0:57690853989a | 25 | |
ericebert | 0:57690853989a | 26 | DigitalOut myled(LED1); |
ericebert | 0:57690853989a | 27 | |
ericebert | 0:57690853989a | 28 | /** |
ericebert | 0:57690853989a | 29 | ******************************************************************************* |
ericebert | 0:57690853989a | 30 | * @brief Switch on LED |
ericebert | 0:57690853989a | 31 | * @param[in] None |
ericebert | 0:57690853989a | 32 | * @param[out] None |
ericebert | 0:57690853989a | 33 | * @retval None |
ericebert | 0:57690853989a | 34 | * @par Description |
ericebert | 0:57690853989a | 35 | * @details This function use to Switch on specify led in LCD. |
ericebert | 0:57690853989a | 36 | ******************************************************************************* |
ericebert | 0:57690853989a | 37 | */ |
ericebert | 0:57690853989a | 38 | void LED_On () |
ericebert | 0:57690853989a | 39 | { |
ericebert | 0:57690853989a | 40 | CoEnterMutexSection(mut_LED); |
ericebert | 0:57690853989a | 41 | myled =1; |
ericebert | 0:57690853989a | 42 | CoLeaveMutexSection(mut_LED); |
ericebert | 0:57690853989a | 43 | } |
ericebert | 0:57690853989a | 44 | |
ericebert | 0:57690853989a | 45 | /** |
ericebert | 0:57690853989a | 46 | ******************************************************************************* |
ericebert | 0:57690853989a | 47 | * @brief Switch off LED |
ericebert | 0:57690853989a | 48 | * @param[in] None |
ericebert | 0:57690853989a | 49 | * @param[out] None |
ericebert | 0:57690853989a | 50 | * @retval None |
ericebert | 0:57690853989a | 51 | * @par Description |
ericebert | 0:57690853989a | 52 | * @details This function use to Switch off specify led in LCD. |
ericebert | 0:57690853989a | 53 | ******************************************************************************* |
ericebert | 0:57690853989a | 54 | */ |
ericebert | 0:57690853989a | 55 | void LED_Off () { |
ericebert | 0:57690853989a | 56 | |
ericebert | 0:57690853989a | 57 | CoEnterMutexSection(mut_LED); |
ericebert | 0:57690853989a | 58 | myled = 0; |
ericebert | 0:57690853989a | 59 | CoLeaveMutexSection(mut_LED); |
ericebert | 0:57690853989a | 60 | } |
ericebert | 0:57690853989a | 61 | |
ericebert | 0:57690853989a | 62 | |
ericebert | 0:57690853989a | 63 | /** |
ericebert | 0:57690853989a | 64 | ******************************************************************************* |
ericebert | 0:57690853989a | 65 | * @brief Function 'signal_func' called from multiple tasks |
ericebert | 0:57690853989a | 66 | * @param[in] None |
ericebert | 0:57690853989a | 67 | * @param[out] None |
ericebert | 0:57690853989a | 68 | * @retval None |
ericebert | 0:57690853989a | 69 | * @par Description |
ericebert | 0:57690853989a | 70 | * @details This function use to Set flag for multiple tasks. |
ericebert | 0:57690853989a | 71 | ******************************************************************************* |
ericebert | 0:57690853989a | 72 | */ |
ericebert | 0:57690853989a | 73 | void signal_func (const char* fromString) |
ericebert | 0:57690853989a | 74 | { |
ericebert | 0:57690853989a | 75 | |
ericebert | 0:57690853989a | 76 | pc.printf(fromString); |
ericebert | 0:57690853989a | 77 | CoSetFlag(b_flg); |
ericebert | 0:57690853989a | 78 | CoTickDelay(50); |
ericebert | 0:57690853989a | 79 | CoSetFlag(b_flg); |
ericebert | 0:57690853989a | 80 | CoTickDelay(50); |
ericebert | 0:57690853989a | 81 | CoSetFlag(a_flg); |
ericebert | 0:57690853989a | 82 | CoTickDelay(50); |
ericebert | 0:57690853989a | 83 | } |
ericebert | 0:57690853989a | 84 | |
ericebert | 0:57690853989a | 85 | |
ericebert | 0:57690853989a | 86 | |
ericebert | 0:57690853989a | 87 | /** |
ericebert | 0:57690853989a | 88 | ******************************************************************************* |
ericebert | 0:57690853989a | 89 | * @brief Task 1 'phaseA': Phase A output |
ericebert | 0:57690853989a | 90 | * @param[in] pdata A pointer to parameter passed to task. |
ericebert | 0:57690853989a | 91 | * @param[out] None |
ericebert | 0:57690853989a | 92 | * @retval None |
ericebert | 0:57690853989a | 93 | ******************************************************************************* |
ericebert | 0:57690853989a | 94 | */ |
ericebert | 0:57690853989a | 95 | void taskA (void *pdata) { |
ericebert | 0:57690853989a | 96 | for (;;) { |
ericebert | 0:57690853989a | 97 | CoWaitForSingleFlag(a_flg,0); |
ericebert | 0:57690853989a | 98 | LED_On(); |
ericebert | 0:57690853989a | 99 | signal_func("\r\nSignal from Task A"); /*!< call common signal function */ |
ericebert | 0:57690853989a | 100 | LED_Off(); |
ericebert | 0:57690853989a | 101 | } |
ericebert | 0:57690853989a | 102 | } |
ericebert | 0:57690853989a | 103 | |
ericebert | 0:57690853989a | 104 | |
ericebert | 0:57690853989a | 105 | /** |
ericebert | 0:57690853989a | 106 | ******************************************************************************* |
ericebert | 0:57690853989a | 107 | * @brief Task 2 'phaseB': Phase B output |
ericebert | 0:57690853989a | 108 | * @param[in] pdata A pointer to parameter passed to task. |
ericebert | 0:57690853989a | 109 | * @param[out] None |
ericebert | 0:57690853989a | 110 | * @retval None |
ericebert | 0:57690853989a | 111 | ******************************************************************************* |
ericebert | 0:57690853989a | 112 | */ |
ericebert | 0:57690853989a | 113 | void taskB (void *pdata) { |
ericebert | 0:57690853989a | 114 | for (;;) { |
ericebert | 0:57690853989a | 115 | CoWaitForSingleFlag(a_flg,0); |
ericebert | 0:57690853989a | 116 | LED_On (); |
ericebert | 0:57690853989a | 117 | signal_func("\r\nSignal from Task B"); /*!< call common signal function */ |
ericebert | 0:57690853989a | 118 | LED_Off(); |
ericebert | 0:57690853989a | 119 | } |
ericebert | 0:57690853989a | 120 | } |
ericebert | 0:57690853989a | 121 | |
ericebert | 0:57690853989a | 122 | |
ericebert | 0:57690853989a | 123 | |
ericebert | 0:57690853989a | 124 | /** |
ericebert | 0:57690853989a | 125 | ******************************************************************************* |
ericebert | 0:57690853989a | 126 | * @brief Task 5 'clock': Signal Clock |
ericebert | 0:57690853989a | 127 | * @param[in] pdata A pointer to parameter passed to task. |
ericebert | 0:57690853989a | 128 | * @param[out] None |
ericebert | 0:57690853989a | 129 | * @retval None |
ericebert | 0:57690853989a | 130 | ******************************************************************************* |
ericebert | 0:57690853989a | 131 | */ |
ericebert | 0:57690853989a | 132 | void clock (void *pdata) { |
ericebert | 0:57690853989a | 133 | for (;;) { |
ericebert | 0:57690853989a | 134 | CoWaitForSingleFlag(b_flg,0); |
ericebert | 0:57690853989a | 135 | LED_On (); |
ericebert | 0:57690853989a | 136 | CoTickDelay(10); |
ericebert | 0:57690853989a | 137 | LED_Off(); |
ericebert | 0:57690853989a | 138 | } |
ericebert | 0:57690853989a | 139 | } |
ericebert | 0:57690853989a | 140 | |
ericebert | 0:57690853989a | 141 | |
ericebert | 0:57690853989a | 142 | /** |
ericebert | 0:57690853989a | 143 | ******************************************************************************* |
ericebert | 0:57690853989a | 144 | * @brief "init_task" task code |
ericebert | 0:57690853989a | 145 | * @param[in] pdata A pointer to parameter passed to task. |
ericebert | 0:57690853989a | 146 | * @param[out] None |
ericebert | 0:57690853989a | 147 | * @retval None |
ericebert | 0:57690853989a | 148 | * @par Description |
ericebert | 0:57690853989a | 149 | * @details This task use to create other tasks, all mutexs and flags,and so on. |
ericebert | 0:57690853989a | 150 | ******************************************************************************* |
ericebert | 0:57690853989a | 151 | */ |
ericebert | 0:57690853989a | 152 | void init_task (void *pdata) |
ericebert | 0:57690853989a | 153 | { |
ericebert | 0:57690853989a | 154 | pdata = pdata; |
ericebert | 0:57690853989a | 155 | |
ericebert | 0:57690853989a | 156 | mut_LED = CoCreateMutex(); |
ericebert | 0:57690853989a | 157 | a_flg = CoCreateFlag(TRUE,0); |
ericebert | 0:57690853989a | 158 | b_flg = CoCreateFlag(TRUE,0); |
ericebert | 0:57690853989a | 159 | |
ericebert | 0:57690853989a | 160 | CoCreateTask(taskA,0,A_TASK_PRIO,&a_task_stk[STK_SIZE-1],STK_SIZE); |
ericebert | 0:57690853989a | 161 | CoCreateTask(taskB,0,B_TASK_PRIO,&b_task_stk[STK_SIZE-1],STK_SIZE); |
ericebert | 0:57690853989a | 162 | CoCreateTask(clock,0,CLK_TASK_PRIO,&clk_task_stk[STK_SIZE-1],STK_SIZE); |
ericebert | 0:57690853989a | 163 | CoSetFlag(a_flg); |
ericebert | 0:57690853989a | 164 | CoExitTask(); /*!< Delete "init_task" task. */ |
ericebert | 0:57690853989a | 165 | } |
ericebert | 0:57690853989a | 166 | |
ericebert | 0:57690853989a | 167 | |
ericebert | 0:57690853989a | 168 | /** |
ericebert | 0:57690853989a | 169 | ******************************************************************************* |
ericebert | 0:57690853989a | 170 | * @brief main function |
ericebert | 0:57690853989a | 171 | * @param[in] None |
ericebert | 0:57690853989a | 172 | * @param[out] None |
ericebert | 0:57690853989a | 173 | * @retval None |
ericebert | 0:57690853989a | 174 | ******************************************************************************* |
ericebert | 0:57690853989a | 175 | */ |
ericebert | 0:57690853989a | 176 | int main() |
ericebert | 0:57690853989a | 177 | { |
ericebert | 0:57690853989a | 178 | //SystemInit(); /*!< Initialize the MCU clocks */ |
ericebert | 0:57690853989a | 179 | |
ericebert | 0:57690853989a | 180 | pc.printf ("\r\n"); |
ericebert | 0:57690853989a | 181 | pc.printf ("\r\nCooCox RTOS Demo\n"); |
ericebert | 0:57690853989a | 182 | CoInitOS(); /*!< Initial CooCox RTOS. */ |
ericebert | 0:57690853989a | 183 | pc.printf ("\r\n CoInitOS done."); |
ericebert | 0:57690853989a | 184 | CoCreateTask( init_task,0,INIT_TASK_PRIO,&init_task_stk[STK_SIZE-1],STK_SIZE); |
ericebert | 0:57690853989a | 185 | pc.printf ("\r\n CoCreateTask init_task done."); |
ericebert | 0:57690853989a | 186 | CoStartOS(); |
ericebert | 0:57690853989a | 187 | pc.printf ("\r\n CoStartOS done."); |
ericebert | 0:57690853989a | 188 | |
ericebert | 0:57690853989a | 189 | while (1); |
ericebert | 0:57690853989a | 190 | } |