Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

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