Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /*----------------------------------------------------------------------------
switches 0:5c4d7b2438d3 2 * RL-ARM - RTX
switches 0:5c4d7b2438d3 3 *----------------------------------------------------------------------------
switches 0:5c4d7b2438d3 4 * Name: RT_SYSTEM.C
switches 0:5c4d7b2438d3 5 * Purpose: System Task Manager
switches 0:5c4d7b2438d3 6 * Rev.: V4.60
switches 0:5c4d7b2438d3 7 *----------------------------------------------------------------------------
switches 0:5c4d7b2438d3 8 *
switches 0:5c4d7b2438d3 9 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
switches 0:5c4d7b2438d3 10 * All rights reserved.
switches 0:5c4d7b2438d3 11 * Redistribution and use in source and binary forms, with or without
switches 0:5c4d7b2438d3 12 * modification, are permitted provided that the following conditions are met:
switches 0:5c4d7b2438d3 13 * - Redistributions of source code must retain the above copyright
switches 0:5c4d7b2438d3 14 * notice, this list of conditions and the following disclaimer.
switches 0:5c4d7b2438d3 15 * - Redistributions in binary form must reproduce the above copyright
switches 0:5c4d7b2438d3 16 * notice, this list of conditions and the following disclaimer in the
switches 0:5c4d7b2438d3 17 * documentation and/or other materials provided with the distribution.
switches 0:5c4d7b2438d3 18 * - Neither the name of ARM nor the names of its contributors may be used
switches 0:5c4d7b2438d3 19 * to endorse or promote products derived from this software without
switches 0:5c4d7b2438d3 20 * specific prior written permission.
switches 0:5c4d7b2438d3 21 *
switches 0:5c4d7b2438d3 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
switches 0:5c4d7b2438d3 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
switches 0:5c4d7b2438d3 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
switches 0:5c4d7b2438d3 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
switches 0:5c4d7b2438d3 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
switches 0:5c4d7b2438d3 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
switches 0:5c4d7b2438d3 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
switches 0:5c4d7b2438d3 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
switches 0:5c4d7b2438d3 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
switches 0:5c4d7b2438d3 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
switches 0:5c4d7b2438d3 32 * POSSIBILITY OF SUCH DAMAGE.
switches 0:5c4d7b2438d3 33 *---------------------------------------------------------------------------*/
switches 0:5c4d7b2438d3 34
switches 0:5c4d7b2438d3 35 #include "rt_TypeDef.h"
switches 0:5c4d7b2438d3 36 #include "RTX_Conf.h"
switches 0:5c4d7b2438d3 37 #include "rt_Task.h"
switches 0:5c4d7b2438d3 38 #include "rt_System.h"
switches 0:5c4d7b2438d3 39 #include "rt_Event.h"
switches 0:5c4d7b2438d3 40 #include "rt_List.h"
switches 0:5c4d7b2438d3 41 #include "rt_Mailbox.h"
switches 0:5c4d7b2438d3 42 #include "rt_Semaphore.h"
switches 0:5c4d7b2438d3 43 #include "rt_Time.h"
switches 0:5c4d7b2438d3 44 #include "rt_Robin.h"
switches 0:5c4d7b2438d3 45 #include "rt_HAL_CM.h"
switches 0:5c4d7b2438d3 46
switches 0:5c4d7b2438d3 47 /*----------------------------------------------------------------------------
switches 0:5c4d7b2438d3 48 * Global Variables
switches 0:5c4d7b2438d3 49 *---------------------------------------------------------------------------*/
switches 0:5c4d7b2438d3 50
switches 0:5c4d7b2438d3 51 int os_tick_irqn;
switches 0:5c4d7b2438d3 52
switches 0:5c4d7b2438d3 53 /*----------------------------------------------------------------------------
switches 0:5c4d7b2438d3 54 * Local Variables
switches 0:5c4d7b2438d3 55 *---------------------------------------------------------------------------*/
switches 0:5c4d7b2438d3 56
switches 0:5c4d7b2438d3 57 static volatile BIT os_lock;
switches 0:5c4d7b2438d3 58 static volatile BIT os_psh_flag;
switches 0:5c4d7b2438d3 59 static U8 pend_flags;
switches 0:5c4d7b2438d3 60
switches 0:5c4d7b2438d3 61 /*----------------------------------------------------------------------------
switches 0:5c4d7b2438d3 62 * Global Functions
switches 0:5c4d7b2438d3 63 *---------------------------------------------------------------------------*/
switches 0:5c4d7b2438d3 64
switches 0:5c4d7b2438d3 65 #if defined (__CC_ARM)
switches 0:5c4d7b2438d3 66 __asm void $$RTX$$version (void) {
switches 0:5c4d7b2438d3 67 /* Export a version number symbol for a version control. */
switches 0:5c4d7b2438d3 68
switches 0:5c4d7b2438d3 69 EXPORT __RL_RTX_VER
switches 0:5c4d7b2438d3 70
switches 0:5c4d7b2438d3 71 __RL_RTX_VER EQU 0x450
switches 0:5c4d7b2438d3 72 }
switches 0:5c4d7b2438d3 73 #endif
switches 0:5c4d7b2438d3 74
switches 0:5c4d7b2438d3 75
switches 0:5c4d7b2438d3 76 /*--------------------------- rt_suspend ------------------------------------*/
switches 0:5c4d7b2438d3 77 U32 rt_suspend (void) {
switches 0:5c4d7b2438d3 78 /* Suspend OS scheduler */
switches 0:5c4d7b2438d3 79 U32 delta = 0xFFFF;
switches 0:5c4d7b2438d3 80
switches 0:5c4d7b2438d3 81 rt_tsk_lock();
switches 0:5c4d7b2438d3 82
switches 0:5c4d7b2438d3 83 if (os_dly.p_dlnk) {
switches 0:5c4d7b2438d3 84 delta = os_dly.delta_time;
switches 0:5c4d7b2438d3 85 }
switches 0:5c4d7b2438d3 86 #ifndef __CMSIS_RTOS
switches 0:5c4d7b2438d3 87 if (os_tmr.next) {
switches 0:5c4d7b2438d3 88 if (os_tmr.tcnt < delta) delta = os_tmr.tcnt;
switches 0:5c4d7b2438d3 89 }
switches 0:5c4d7b2438d3 90 #endif
switches 0:5c4d7b2438d3 91
switches 0:5c4d7b2438d3 92 return (delta);
switches 0:5c4d7b2438d3 93 }
switches 0:5c4d7b2438d3 94
switches 0:5c4d7b2438d3 95
switches 0:5c4d7b2438d3 96 /*--------------------------- rt_resume -------------------------------------*/
switches 0:5c4d7b2438d3 97 void rt_resume (U32 sleep_time) {
switches 0:5c4d7b2438d3 98 /* Resume OS scheduler after suspend */
switches 0:5c4d7b2438d3 99 P_TCB next;
switches 0:5c4d7b2438d3 100 U32 delta;
switches 0:5c4d7b2438d3 101
switches 0:5c4d7b2438d3 102 os_tsk.run->state = READY;
switches 0:5c4d7b2438d3 103 rt_put_rdy_first (os_tsk.run);
switches 0:5c4d7b2438d3 104
switches 0:5c4d7b2438d3 105 os_robin.task = NULL;
switches 0:5c4d7b2438d3 106
switches 0:5c4d7b2438d3 107 /* Update delays. */
switches 0:5c4d7b2438d3 108 if (os_dly.p_dlnk) {
switches 0:5c4d7b2438d3 109 delta = sleep_time;
switches 0:5c4d7b2438d3 110 if (delta >= os_dly.delta_time) {
switches 0:5c4d7b2438d3 111 delta -= os_dly.delta_time;
switches 0:5c4d7b2438d3 112 os_time += os_dly.delta_time;
switches 0:5c4d7b2438d3 113 os_dly.delta_time = 1;
switches 0:5c4d7b2438d3 114 while (os_dly.p_dlnk) {
switches 0:5c4d7b2438d3 115 rt_dec_dly();
switches 0:5c4d7b2438d3 116 if (delta == 0) break;
switches 0:5c4d7b2438d3 117 delta--;
switches 0:5c4d7b2438d3 118 os_time++;
switches 0:5c4d7b2438d3 119 }
switches 0:5c4d7b2438d3 120 } else {
switches 0:5c4d7b2438d3 121 os_time += delta;
switches 0:5c4d7b2438d3 122 os_dly.delta_time -= delta;
switches 0:5c4d7b2438d3 123 }
switches 0:5c4d7b2438d3 124 } else {
switches 0:5c4d7b2438d3 125 os_time += sleep_time;
switches 0:5c4d7b2438d3 126 }
switches 0:5c4d7b2438d3 127
switches 0:5c4d7b2438d3 128 #ifndef __CMSIS_RTOS
switches 0:5c4d7b2438d3 129 /* Check the user timers. */
switches 0:5c4d7b2438d3 130 if (os_tmr.next) {
switches 0:5c4d7b2438d3 131 delta = sleep_time;
switches 0:5c4d7b2438d3 132 if (delta >= os_tmr.tcnt) {
switches 0:5c4d7b2438d3 133 delta -= os_tmr.tcnt;
switches 0:5c4d7b2438d3 134 os_tmr.tcnt = 1;
switches 0:5c4d7b2438d3 135 while (os_tmr.next) {
switches 0:5c4d7b2438d3 136 rt_tmr_tick();
switches 0:5c4d7b2438d3 137 if (delta == 0) break;
switches 0:5c4d7b2438d3 138 delta--;
switches 0:5c4d7b2438d3 139 }
switches 0:5c4d7b2438d3 140 } else {
switches 0:5c4d7b2438d3 141 os_tmr.tcnt -= delta;
switches 0:5c4d7b2438d3 142 }
switches 0:5c4d7b2438d3 143 }
switches 0:5c4d7b2438d3 144 #endif
switches 0:5c4d7b2438d3 145
switches 0:5c4d7b2438d3 146 /* Switch back to highest ready task */
switches 0:5c4d7b2438d3 147 next = rt_get_first (&os_rdy);
switches 0:5c4d7b2438d3 148 rt_switch_req (next);
switches 0:5c4d7b2438d3 149
switches 0:5c4d7b2438d3 150 rt_tsk_unlock();
switches 0:5c4d7b2438d3 151 }
switches 0:5c4d7b2438d3 152
switches 0:5c4d7b2438d3 153
switches 0:5c4d7b2438d3 154 /*--------------------------- rt_tsk_lock -----------------------------------*/
switches 0:5c4d7b2438d3 155
switches 0:5c4d7b2438d3 156 void rt_tsk_lock (void) {
switches 0:5c4d7b2438d3 157 /* Prevent task switching by locking out scheduler */
switches 0:5c4d7b2438d3 158 OS_X_LOCK(os_tick_irqn);
switches 0:5c4d7b2438d3 159 os_lock = __TRUE;
switches 0:5c4d7b2438d3 160 OS_X_UNPEND (&pend_flags);
switches 0:5c4d7b2438d3 161 }
switches 0:5c4d7b2438d3 162
switches 0:5c4d7b2438d3 163
switches 0:5c4d7b2438d3 164 /*--------------------------- rt_tsk_unlock ---------------------------------*/
switches 0:5c4d7b2438d3 165
switches 0:5c4d7b2438d3 166 void rt_tsk_unlock (void) {
switches 0:5c4d7b2438d3 167 /* Unlock scheduler and re-enable task switching */
switches 0:5c4d7b2438d3 168 OS_X_UNLOCK(os_tick_irqn);
switches 0:5c4d7b2438d3 169 os_lock = __FALSE;
switches 0:5c4d7b2438d3 170 OS_X_PEND (pend_flags, os_psh_flag);
switches 0:5c4d7b2438d3 171 os_psh_flag = __FALSE;
switches 0:5c4d7b2438d3 172 }
switches 0:5c4d7b2438d3 173
switches 0:5c4d7b2438d3 174
switches 0:5c4d7b2438d3 175 /*--------------------------- rt_psh_req ------------------------------------*/
switches 0:5c4d7b2438d3 176
switches 0:5c4d7b2438d3 177 void rt_psh_req (void) {
switches 0:5c4d7b2438d3 178 /* Initiate a post service handling request if required. */
switches 0:5c4d7b2438d3 179 if (os_lock == __FALSE) {
switches 0:5c4d7b2438d3 180 OS_PEND_IRQ ();
switches 0:5c4d7b2438d3 181 }
switches 0:5c4d7b2438d3 182 else {
switches 0:5c4d7b2438d3 183 os_psh_flag = __TRUE;
switches 0:5c4d7b2438d3 184 }
switches 0:5c4d7b2438d3 185 }
switches 0:5c4d7b2438d3 186
switches 0:5c4d7b2438d3 187
switches 0:5c4d7b2438d3 188 /*--------------------------- rt_pop_req ------------------------------------*/
switches 0:5c4d7b2438d3 189
switches 0:5c4d7b2438d3 190 void rt_pop_req (void) {
switches 0:5c4d7b2438d3 191 /* Process an ISR post service requests. */
switches 0:5c4d7b2438d3 192 struct OS_XCB *p_CB;
switches 0:5c4d7b2438d3 193 P_TCB next;
switches 0:5c4d7b2438d3 194 U32 idx;
switches 0:5c4d7b2438d3 195
switches 0:5c4d7b2438d3 196 os_tsk.run->state = READY;
switches 0:5c4d7b2438d3 197 rt_put_rdy_first (os_tsk.run);
switches 0:5c4d7b2438d3 198
switches 0:5c4d7b2438d3 199 idx = os_psq->last;
switches 0:5c4d7b2438d3 200 while (os_psq->count) {
switches 0:5c4d7b2438d3 201 p_CB = os_psq->q[idx].id;
switches 0:5c4d7b2438d3 202 if (p_CB->cb_type == TCB) {
switches 0:5c4d7b2438d3 203 /* Is of TCB type */
switches 0:5c4d7b2438d3 204 rt_evt_psh ((P_TCB)p_CB, (U16)os_psq->q[idx].arg);
switches 0:5c4d7b2438d3 205 }
switches 0:5c4d7b2438d3 206 else if (p_CB->cb_type == MCB) {
switches 0:5c4d7b2438d3 207 /* Is of MCB type */
switches 0:5c4d7b2438d3 208 rt_mbx_psh ((P_MCB)p_CB, (void *)os_psq->q[idx].arg);
switches 0:5c4d7b2438d3 209 }
switches 0:5c4d7b2438d3 210 else {
switches 0:5c4d7b2438d3 211 /* Must be of SCB type */
switches 0:5c4d7b2438d3 212 rt_sem_psh ((P_SCB)p_CB);
switches 0:5c4d7b2438d3 213 }
switches 0:5c4d7b2438d3 214 if (++idx == os_psq->size) idx = 0;
switches 0:5c4d7b2438d3 215 rt_dec (&os_psq->count);
switches 0:5c4d7b2438d3 216 }
switches 0:5c4d7b2438d3 217 os_psq->last = idx;
switches 0:5c4d7b2438d3 218
switches 0:5c4d7b2438d3 219 next = rt_get_first (&os_rdy);
switches 0:5c4d7b2438d3 220 rt_switch_req (next);
switches 0:5c4d7b2438d3 221 }
switches 0:5c4d7b2438d3 222
switches 0:5c4d7b2438d3 223
switches 0:5c4d7b2438d3 224 /*--------------------------- os_tick_init ----------------------------------*/
switches 0:5c4d7b2438d3 225
switches 0:5c4d7b2438d3 226 __weak int os_tick_init (void) {
switches 0:5c4d7b2438d3 227 /* Initialize SysTick timer as system tick timer. */
switches 0:5c4d7b2438d3 228 rt_systick_init ();
switches 0:5c4d7b2438d3 229 return (SYS_TICK_IRQn); /* Return IRQ number of SysTick timer */
switches 0:5c4d7b2438d3 230 }
switches 0:5c4d7b2438d3 231
switches 0:5c4d7b2438d3 232
switches 0:5c4d7b2438d3 233 /*--------------------------- os_tick_irqack --------------------------------*/
switches 0:5c4d7b2438d3 234
switches 0:5c4d7b2438d3 235 __weak void os_tick_irqack (void) {
switches 0:5c4d7b2438d3 236 /* Acknowledge timer interrupt. */
switches 0:5c4d7b2438d3 237 }
switches 0:5c4d7b2438d3 238
switches 0:5c4d7b2438d3 239
switches 0:5c4d7b2438d3 240 /*--------------------------- rt_systick ------------------------------------*/
switches 0:5c4d7b2438d3 241
switches 0:5c4d7b2438d3 242 extern void sysTimerTick(void);
switches 0:5c4d7b2438d3 243
switches 0:5c4d7b2438d3 244 void rt_systick (void) {
switches 0:5c4d7b2438d3 245 if(NVIC_Pending(SYS_TICK_IRQn)){
switches 0:5c4d7b2438d3 246 rt_pop_req();
switches 0:5c4d7b2438d3 247 NVIC_UnpendIRQ(SYS_TICK_IRQn);
switches 0:5c4d7b2438d3 248 SYS_TICK_TIMER->IR = 0xF; // clear timer interrupt
switches 0:5c4d7b2438d3 249 return;
switches 0:5c4d7b2438d3 250 }
switches 0:5c4d7b2438d3 251 /* Check for system clock update, suspend running task. */
switches 0:5c4d7b2438d3 252 P_TCB next;
switches 0:5c4d7b2438d3 253
switches 0:5c4d7b2438d3 254 os_tsk.run->state = READY;
switches 0:5c4d7b2438d3 255 rt_put_rdy_first (os_tsk.run);
switches 0:5c4d7b2438d3 256
switches 0:5c4d7b2438d3 257 /* Check Round Robin timeout. */
switches 0:5c4d7b2438d3 258 rt_chk_robin ();
switches 0:5c4d7b2438d3 259
switches 0:5c4d7b2438d3 260 /* Update delays. */
switches 0:5c4d7b2438d3 261 os_time++;
switches 0:5c4d7b2438d3 262 rt_dec_dly ();
switches 0:5c4d7b2438d3 263
switches 0:5c4d7b2438d3 264 /* Check the user timers. */
switches 0:5c4d7b2438d3 265 #ifdef __CMSIS_RTOS
switches 0:5c4d7b2438d3 266 sysTimerTick();
switches 0:5c4d7b2438d3 267 #else
switches 0:5c4d7b2438d3 268 rt_tmr_tick ();
switches 0:5c4d7b2438d3 269 #endif
switches 0:5c4d7b2438d3 270
switches 0:5c4d7b2438d3 271 /* Switch back to highest ready task */
switches 0:5c4d7b2438d3 272 next = rt_get_first (&os_rdy);
switches 0:5c4d7b2438d3 273 rt_switch_req (next);
switches 0:5c4d7b2438d3 274 SYS_TICK_TIMER->IR = 0xF; // clear timer interrupt
switches 0:5c4d7b2438d3 275 }
switches 0:5c4d7b2438d3 276
switches 0:5c4d7b2438d3 277 /*--------------------------- rt_stk_check ----------------------------------*/
switches 0:5c4d7b2438d3 278 __weak void rt_stk_check (void) {
switches 0:5c4d7b2438d3 279 /* Check for stack overflow. */
switches 0:5c4d7b2438d3 280 if (os_tsk.run->task_id == 0x01) {
switches 0:5c4d7b2438d3 281 // TODO: For the main thread the check should be done against the main heap pointer
switches 0:5c4d7b2438d3 282 } else {
switches 0:5c4d7b2438d3 283 if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) ||
switches 0:5c4d7b2438d3 284 (os_tsk.run->stack[0] != MAGIC_WORD)) {
switches 0:5c4d7b2438d3 285 os_error (OS_ERR_STK_OVF);
switches 0:5c4d7b2438d3 286 }
switches 0:5c4d7b2438d3 287 }
switches 0:5c4d7b2438d3 288 }
switches 0:5c4d7b2438d3 289
switches 0:5c4d7b2438d3 290 /*----------------------------------------------------------------------------
switches 0:5c4d7b2438d3 291 * end of file
switches 0:5c4d7b2438d3 292 *---------------------------------------------------------------------------*/
switches 0:5c4d7b2438d3 293