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_MUTEX.C
shoaib_ahmed 0:791a779d6220 5 * Purpose: Implements mutex synchronization objects
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_List.h"
shoaib_ahmed 0:791a779d6220 38 #include "rt_Task.h"
shoaib_ahmed 0:791a779d6220 39 #include "rt_Mutex.h"
shoaib_ahmed 0:791a779d6220 40 #include "rt_HAL_CM.h"
shoaib_ahmed 0:791a779d6220 41
shoaib_ahmed 0:791a779d6220 42
shoaib_ahmed 0:791a779d6220 43 /*----------------------------------------------------------------------------
shoaib_ahmed 0:791a779d6220 44 * Functions
shoaib_ahmed 0:791a779d6220 45 *---------------------------------------------------------------------------*/
shoaib_ahmed 0:791a779d6220 46
shoaib_ahmed 0:791a779d6220 47
shoaib_ahmed 0:791a779d6220 48 /*--------------------------- rt_mut_init -----------------------------------*/
shoaib_ahmed 0:791a779d6220 49
shoaib_ahmed 0:791a779d6220 50 void rt_mut_init (OS_ID mutex) {
shoaib_ahmed 0:791a779d6220 51 /* Initialize a mutex object */
shoaib_ahmed 0:791a779d6220 52 P_MUCB p_MCB = mutex;
shoaib_ahmed 0:791a779d6220 53
shoaib_ahmed 0:791a779d6220 54 p_MCB->cb_type = MUCB;
shoaib_ahmed 0:791a779d6220 55 p_MCB->prio = 0;
shoaib_ahmed 0:791a779d6220 56 p_MCB->level = 0;
shoaib_ahmed 0:791a779d6220 57 p_MCB->p_lnk = NULL;
shoaib_ahmed 0:791a779d6220 58 p_MCB->owner = NULL;
shoaib_ahmed 0:791a779d6220 59 }
shoaib_ahmed 0:791a779d6220 60
shoaib_ahmed 0:791a779d6220 61
shoaib_ahmed 0:791a779d6220 62 /*--------------------------- rt_mut_delete ---------------------------------*/
shoaib_ahmed 0:791a779d6220 63
shoaib_ahmed 0:791a779d6220 64 #ifdef __CMSIS_RTOS
shoaib_ahmed 0:791a779d6220 65 OS_RESULT rt_mut_delete (OS_ID mutex) {
shoaib_ahmed 0:791a779d6220 66 /* Delete a mutex object */
shoaib_ahmed 0:791a779d6220 67 P_MUCB p_MCB = mutex;
shoaib_ahmed 0:791a779d6220 68 P_TCB p_TCB;
shoaib_ahmed 0:791a779d6220 69
shoaib_ahmed 0:791a779d6220 70 /* Restore owner task's priority. */
shoaib_ahmed 0:791a779d6220 71 if (p_MCB->level != 0) {
shoaib_ahmed 0:791a779d6220 72 p_MCB->owner->prio = p_MCB->prio;
shoaib_ahmed 0:791a779d6220 73 if (p_MCB->owner != os_tsk.run) {
shoaib_ahmed 0:791a779d6220 74 rt_resort_prio (p_MCB->owner);
shoaib_ahmed 0:791a779d6220 75 }
shoaib_ahmed 0:791a779d6220 76 }
shoaib_ahmed 0:791a779d6220 77
shoaib_ahmed 0:791a779d6220 78 while (p_MCB->p_lnk != NULL) {
shoaib_ahmed 0:791a779d6220 79 /* A task is waiting for mutex. */
shoaib_ahmed 0:791a779d6220 80 p_TCB = rt_get_first ((P_XCB)p_MCB);
shoaib_ahmed 0:791a779d6220 81 rt_ret_val(p_TCB, 0/*osOK*/);
shoaib_ahmed 0:791a779d6220 82 rt_rmv_dly(p_TCB);
shoaib_ahmed 0:791a779d6220 83 p_TCB->state = READY;
shoaib_ahmed 0:791a779d6220 84 rt_put_prio (&os_rdy, p_TCB);
shoaib_ahmed 0:791a779d6220 85 }
shoaib_ahmed 0:791a779d6220 86
shoaib_ahmed 0:791a779d6220 87 if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
shoaib_ahmed 0:791a779d6220 88 /* preempt running task */
shoaib_ahmed 0:791a779d6220 89 rt_put_prio (&os_rdy, os_tsk.run);
shoaib_ahmed 0:791a779d6220 90 os_tsk.run->state = READY;
shoaib_ahmed 0:791a779d6220 91 rt_dispatch (NULL);
shoaib_ahmed 0:791a779d6220 92 }
shoaib_ahmed 0:791a779d6220 93
shoaib_ahmed 0:791a779d6220 94 p_MCB->cb_type = 0;
shoaib_ahmed 0:791a779d6220 95
shoaib_ahmed 0:791a779d6220 96 return (OS_R_OK);
shoaib_ahmed 0:791a779d6220 97 }
shoaib_ahmed 0:791a779d6220 98 #endif
shoaib_ahmed 0:791a779d6220 99
shoaib_ahmed 0:791a779d6220 100
shoaib_ahmed 0:791a779d6220 101 /*--------------------------- rt_mut_release --------------------------------*/
shoaib_ahmed 0:791a779d6220 102
shoaib_ahmed 0:791a779d6220 103 OS_RESULT rt_mut_release (OS_ID mutex) {
shoaib_ahmed 0:791a779d6220 104 /* Release a mutex object */
shoaib_ahmed 0:791a779d6220 105 P_MUCB p_MCB = mutex;
shoaib_ahmed 0:791a779d6220 106 P_TCB p_TCB;
shoaib_ahmed 0:791a779d6220 107
shoaib_ahmed 0:791a779d6220 108 if (p_MCB->level == 0 || p_MCB->owner != os_tsk.run) {
shoaib_ahmed 0:791a779d6220 109 /* Unbalanced mutex release or task is not the owner */
shoaib_ahmed 0:791a779d6220 110 return (OS_R_NOK);
shoaib_ahmed 0:791a779d6220 111 }
shoaib_ahmed 0:791a779d6220 112 if (--p_MCB->level != 0) {
shoaib_ahmed 0:791a779d6220 113 return (OS_R_OK);
shoaib_ahmed 0:791a779d6220 114 }
shoaib_ahmed 0:791a779d6220 115 /* Restore owner task's priority. */
shoaib_ahmed 0:791a779d6220 116 os_tsk.run->prio = p_MCB->prio;
shoaib_ahmed 0:791a779d6220 117 if (p_MCB->p_lnk != NULL) {
shoaib_ahmed 0:791a779d6220 118 /* A task is waiting for mutex. */
shoaib_ahmed 0:791a779d6220 119 p_TCB = rt_get_first ((P_XCB)p_MCB);
shoaib_ahmed 0:791a779d6220 120 #ifdef __CMSIS_RTOS
shoaib_ahmed 0:791a779d6220 121 rt_ret_val(p_TCB, 0/*osOK*/);
shoaib_ahmed 0:791a779d6220 122 #else
shoaib_ahmed 0:791a779d6220 123 rt_ret_val(p_TCB, OS_R_MUT);
shoaib_ahmed 0:791a779d6220 124 #endif
shoaib_ahmed 0:791a779d6220 125 rt_rmv_dly (p_TCB);
shoaib_ahmed 0:791a779d6220 126 /* A waiting task becomes the owner of this mutex. */
shoaib_ahmed 0:791a779d6220 127 p_MCB->level = 1;
shoaib_ahmed 0:791a779d6220 128 p_MCB->owner = p_TCB;
shoaib_ahmed 0:791a779d6220 129 p_MCB->prio = p_TCB->prio;
shoaib_ahmed 0:791a779d6220 130 /* Priority inversion, check which task continues. */
shoaib_ahmed 0:791a779d6220 131 if (os_tsk.run->prio >= rt_rdy_prio()) {
shoaib_ahmed 0:791a779d6220 132 rt_dispatch (p_TCB);
shoaib_ahmed 0:791a779d6220 133 }
shoaib_ahmed 0:791a779d6220 134 else {
shoaib_ahmed 0:791a779d6220 135 /* Ready task has higher priority than running task. */
shoaib_ahmed 0:791a779d6220 136 rt_put_prio (&os_rdy, os_tsk.run);
shoaib_ahmed 0:791a779d6220 137 rt_put_prio (&os_rdy, p_TCB);
shoaib_ahmed 0:791a779d6220 138 os_tsk.run->state = READY;
shoaib_ahmed 0:791a779d6220 139 p_TCB->state = READY;
shoaib_ahmed 0:791a779d6220 140 rt_dispatch (NULL);
shoaib_ahmed 0:791a779d6220 141 }
shoaib_ahmed 0:791a779d6220 142 }
shoaib_ahmed 0:791a779d6220 143 else {
shoaib_ahmed 0:791a779d6220 144 /* Check if own priority raised by priority inversion. */
shoaib_ahmed 0:791a779d6220 145 if (rt_rdy_prio() > os_tsk.run->prio) {
shoaib_ahmed 0:791a779d6220 146 rt_put_prio (&os_rdy, os_tsk.run);
shoaib_ahmed 0:791a779d6220 147 os_tsk.run->state = READY;
shoaib_ahmed 0:791a779d6220 148 rt_dispatch (NULL);
shoaib_ahmed 0:791a779d6220 149 }
shoaib_ahmed 0:791a779d6220 150 }
shoaib_ahmed 0:791a779d6220 151 return (OS_R_OK);
shoaib_ahmed 0:791a779d6220 152 }
shoaib_ahmed 0:791a779d6220 153
shoaib_ahmed 0:791a779d6220 154
shoaib_ahmed 0:791a779d6220 155 /*--------------------------- rt_mut_wait -----------------------------------*/
shoaib_ahmed 0:791a779d6220 156
shoaib_ahmed 0:791a779d6220 157 OS_RESULT rt_mut_wait (OS_ID mutex, U16 timeout) {
shoaib_ahmed 0:791a779d6220 158 /* Wait for a mutex, continue when mutex is free. */
shoaib_ahmed 0:791a779d6220 159 P_MUCB p_MCB = mutex;
shoaib_ahmed 0:791a779d6220 160
shoaib_ahmed 0:791a779d6220 161 if (p_MCB->level == 0) {
shoaib_ahmed 0:791a779d6220 162 p_MCB->owner = os_tsk.run;
shoaib_ahmed 0:791a779d6220 163 p_MCB->prio = os_tsk.run->prio;
shoaib_ahmed 0:791a779d6220 164 goto inc;
shoaib_ahmed 0:791a779d6220 165 }
shoaib_ahmed 0:791a779d6220 166 if (p_MCB->owner == os_tsk.run) {
shoaib_ahmed 0:791a779d6220 167 /* OK, running task is the owner of this mutex. */
shoaib_ahmed 0:791a779d6220 168 inc:p_MCB->level++;
shoaib_ahmed 0:791a779d6220 169 return (OS_R_OK);
shoaib_ahmed 0:791a779d6220 170 }
shoaib_ahmed 0:791a779d6220 171 /* Mutex owned by another task, wait until released. */
shoaib_ahmed 0:791a779d6220 172 if (timeout == 0) {
shoaib_ahmed 0:791a779d6220 173 return (OS_R_TMO);
shoaib_ahmed 0:791a779d6220 174 }
shoaib_ahmed 0:791a779d6220 175 /* Raise the owner task priority if lower than current priority. */
shoaib_ahmed 0:791a779d6220 176 /* This priority inversion is called priority inheritance. */
shoaib_ahmed 0:791a779d6220 177 if (p_MCB->prio < os_tsk.run->prio) {
shoaib_ahmed 0:791a779d6220 178 p_MCB->owner->prio = os_tsk.run->prio;
shoaib_ahmed 0:791a779d6220 179 rt_resort_prio (p_MCB->owner);
shoaib_ahmed 0:791a779d6220 180 }
shoaib_ahmed 0:791a779d6220 181 if (p_MCB->p_lnk != NULL) {
shoaib_ahmed 0:791a779d6220 182 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
shoaib_ahmed 0:791a779d6220 183 }
shoaib_ahmed 0:791a779d6220 184 else {
shoaib_ahmed 0:791a779d6220 185 p_MCB->p_lnk = os_tsk.run;
shoaib_ahmed 0:791a779d6220 186 os_tsk.run->p_lnk = NULL;
shoaib_ahmed 0:791a779d6220 187 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
shoaib_ahmed 0:791a779d6220 188 }
shoaib_ahmed 0:791a779d6220 189 rt_block(timeout, WAIT_MUT);
shoaib_ahmed 0:791a779d6220 190 return (OS_R_TMO);
shoaib_ahmed 0:791a779d6220 191 }
shoaib_ahmed 0:791a779d6220 192
shoaib_ahmed 0:791a779d6220 193
shoaib_ahmed 0:791a779d6220 194 /*----------------------------------------------------------------------------
shoaib_ahmed 0:791a779d6220 195 * end of file
shoaib_ahmed 0:791a779d6220 196 *---------------------------------------------------------------------------*/
shoaib_ahmed 0:791a779d6220 197