sk398

Fork of mbed-rtos by mbed official

Committer:
sk398
Date:
Wed Mar 30 02:21:27 2016 +0000
Revision:
107:5e95639cb109
Parent:
80:2dab120a94c2
Car Sim not functioning - overflow of Mailpool and Queue; Going to try adopting an array system with counter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 80:2dab120a94c2 1 /*----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 2 * RL-ARM - RTX
mbed_official 80:2dab120a94c2 3 *----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 4 * Name: RT_SEMAPHORE.C
mbed_official 80:2dab120a94c2 5 * Purpose: Implements binary and counting semaphores
mbed_official 80:2dab120a94c2 6 * Rev.: V4.60
mbed_official 80:2dab120a94c2 7 *----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 8 *
mbed_official 80:2dab120a94c2 9 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
mbed_official 80:2dab120a94c2 10 * All rights reserved.
mbed_official 80:2dab120a94c2 11 * Redistribution and use in source and binary forms, with or without
mbed_official 80:2dab120a94c2 12 * modification, are permitted provided that the following conditions are met:
mbed_official 80:2dab120a94c2 13 * - Redistributions of source code must retain the above copyright
mbed_official 80:2dab120a94c2 14 * notice, this list of conditions and the following disclaimer.
mbed_official 80:2dab120a94c2 15 * - Redistributions in binary form must reproduce the above copyright
mbed_official 80:2dab120a94c2 16 * notice, this list of conditions and the following disclaimer in the
mbed_official 80:2dab120a94c2 17 * documentation and/or other materials provided with the distribution.
mbed_official 80:2dab120a94c2 18 * - Neither the name of ARM nor the names of its contributors may be used
mbed_official 80:2dab120a94c2 19 * to endorse or promote products derived from this software without
mbed_official 80:2dab120a94c2 20 * specific prior written permission.
mbed_official 80:2dab120a94c2 21 *
mbed_official 80:2dab120a94c2 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 80:2dab120a94c2 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 80:2dab120a94c2 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
mbed_official 80:2dab120a94c2 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
mbed_official 80:2dab120a94c2 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
mbed_official 80:2dab120a94c2 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
mbed_official 80:2dab120a94c2 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbed_official 80:2dab120a94c2 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbed_official 80:2dab120a94c2 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
mbed_official 80:2dab120a94c2 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbed_official 80:2dab120a94c2 32 * POSSIBILITY OF SUCH DAMAGE.
mbed_official 80:2dab120a94c2 33 *---------------------------------------------------------------------------*/
mbed_official 80:2dab120a94c2 34
mbed_official 80:2dab120a94c2 35 #include "rt_TypeDef.h"
mbed_official 80:2dab120a94c2 36 #include "RTX_Conf.h"
mbed_official 80:2dab120a94c2 37 #include "rt_System.h"
mbed_official 80:2dab120a94c2 38 #include "rt_List.h"
mbed_official 80:2dab120a94c2 39 #include "rt_Task.h"
mbed_official 80:2dab120a94c2 40 #include "rt_Semaphore.h"
mbed_official 80:2dab120a94c2 41 #include "rt_HAL_CM.h"
mbed_official 80:2dab120a94c2 42
mbed_official 80:2dab120a94c2 43
mbed_official 80:2dab120a94c2 44 /*----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 45 * Functions
mbed_official 80:2dab120a94c2 46 *---------------------------------------------------------------------------*/
mbed_official 80:2dab120a94c2 47
mbed_official 80:2dab120a94c2 48
mbed_official 80:2dab120a94c2 49 /*--------------------------- rt_sem_init -----------------------------------*/
mbed_official 80:2dab120a94c2 50
mbed_official 80:2dab120a94c2 51 void rt_sem_init (OS_ID semaphore, U16 token_count) {
mbed_official 80:2dab120a94c2 52 /* Initialize a semaphore */
mbed_official 80:2dab120a94c2 53 P_SCB p_SCB = semaphore;
mbed_official 80:2dab120a94c2 54
mbed_official 80:2dab120a94c2 55 p_SCB->cb_type = SCB;
mbed_official 80:2dab120a94c2 56 p_SCB->p_lnk = NULL;
mbed_official 80:2dab120a94c2 57 p_SCB->tokens = token_count;
mbed_official 80:2dab120a94c2 58 }
mbed_official 80:2dab120a94c2 59
mbed_official 80:2dab120a94c2 60
mbed_official 80:2dab120a94c2 61 /*--------------------------- rt_sem_delete ---------------------------------*/
mbed_official 80:2dab120a94c2 62
mbed_official 80:2dab120a94c2 63 #ifdef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 64 OS_RESULT rt_sem_delete (OS_ID semaphore) {
mbed_official 80:2dab120a94c2 65 /* Delete semaphore */
mbed_official 80:2dab120a94c2 66 P_SCB p_SCB = semaphore;
mbed_official 80:2dab120a94c2 67 P_TCB p_TCB;
mbed_official 80:2dab120a94c2 68
mbed_official 80:2dab120a94c2 69 while (p_SCB->p_lnk != NULL) {
mbed_official 80:2dab120a94c2 70 /* A task is waiting for token */
mbed_official 80:2dab120a94c2 71 p_TCB = rt_get_first ((P_XCB)p_SCB);
mbed_official 80:2dab120a94c2 72 rt_ret_val(p_TCB, 0);
mbed_official 80:2dab120a94c2 73 rt_rmv_dly(p_TCB);
mbed_official 80:2dab120a94c2 74 p_TCB->state = READY;
mbed_official 80:2dab120a94c2 75 rt_put_prio (&os_rdy, p_TCB);
mbed_official 80:2dab120a94c2 76 }
mbed_official 80:2dab120a94c2 77
mbed_official 80:2dab120a94c2 78 if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
mbed_official 80:2dab120a94c2 79 /* preempt running task */
mbed_official 80:2dab120a94c2 80 rt_put_prio (&os_rdy, os_tsk.run);
mbed_official 80:2dab120a94c2 81 os_tsk.run->state = READY;
mbed_official 80:2dab120a94c2 82 rt_dispatch (NULL);
mbed_official 80:2dab120a94c2 83 }
mbed_official 80:2dab120a94c2 84
mbed_official 80:2dab120a94c2 85 p_SCB->cb_type = 0;
mbed_official 80:2dab120a94c2 86
mbed_official 80:2dab120a94c2 87 return (OS_R_OK);
mbed_official 80:2dab120a94c2 88 }
mbed_official 80:2dab120a94c2 89 #endif
mbed_official 80:2dab120a94c2 90
mbed_official 80:2dab120a94c2 91
mbed_official 80:2dab120a94c2 92 /*--------------------------- rt_sem_send -----------------------------------*/
mbed_official 80:2dab120a94c2 93
mbed_official 80:2dab120a94c2 94 OS_RESULT rt_sem_send (OS_ID semaphore) {
mbed_official 80:2dab120a94c2 95 /* Return a token to semaphore */
mbed_official 80:2dab120a94c2 96 P_SCB p_SCB = semaphore;
mbed_official 80:2dab120a94c2 97 P_TCB p_TCB;
mbed_official 80:2dab120a94c2 98
mbed_official 80:2dab120a94c2 99 if (p_SCB->p_lnk != NULL) {
mbed_official 80:2dab120a94c2 100 /* A task is waiting for token */
mbed_official 80:2dab120a94c2 101 p_TCB = rt_get_first ((P_XCB)p_SCB);
mbed_official 80:2dab120a94c2 102 #ifdef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 103 rt_ret_val(p_TCB, 1);
mbed_official 80:2dab120a94c2 104 #else
mbed_official 80:2dab120a94c2 105 rt_ret_val(p_TCB, OS_R_SEM);
mbed_official 80:2dab120a94c2 106 #endif
mbed_official 80:2dab120a94c2 107 rt_rmv_dly (p_TCB);
mbed_official 80:2dab120a94c2 108 rt_dispatch (p_TCB);
mbed_official 80:2dab120a94c2 109 }
mbed_official 80:2dab120a94c2 110 else {
mbed_official 80:2dab120a94c2 111 /* Store token. */
mbed_official 80:2dab120a94c2 112 p_SCB->tokens++;
mbed_official 80:2dab120a94c2 113 }
mbed_official 80:2dab120a94c2 114 return (OS_R_OK);
mbed_official 80:2dab120a94c2 115 }
mbed_official 80:2dab120a94c2 116
mbed_official 80:2dab120a94c2 117
mbed_official 80:2dab120a94c2 118 /*--------------------------- rt_sem_wait -----------------------------------*/
mbed_official 80:2dab120a94c2 119
mbed_official 80:2dab120a94c2 120 OS_RESULT rt_sem_wait (OS_ID semaphore, U16 timeout) {
mbed_official 80:2dab120a94c2 121 /* Obtain a token; possibly wait for it */
mbed_official 80:2dab120a94c2 122 P_SCB p_SCB = semaphore;
mbed_official 80:2dab120a94c2 123
mbed_official 80:2dab120a94c2 124 if (p_SCB->tokens) {
mbed_official 80:2dab120a94c2 125 p_SCB->tokens--;
mbed_official 80:2dab120a94c2 126 return (OS_R_OK);
mbed_official 80:2dab120a94c2 127 }
mbed_official 80:2dab120a94c2 128 /* No token available: wait for one */
mbed_official 80:2dab120a94c2 129 if (timeout == 0) {
mbed_official 80:2dab120a94c2 130 return (OS_R_TMO);
mbed_official 80:2dab120a94c2 131 }
mbed_official 80:2dab120a94c2 132 if (p_SCB->p_lnk != NULL) {
mbed_official 80:2dab120a94c2 133 rt_put_prio ((P_XCB)p_SCB, os_tsk.run);
mbed_official 80:2dab120a94c2 134 }
mbed_official 80:2dab120a94c2 135 else {
mbed_official 80:2dab120a94c2 136 p_SCB->p_lnk = os_tsk.run;
mbed_official 80:2dab120a94c2 137 os_tsk.run->p_lnk = NULL;
mbed_official 80:2dab120a94c2 138 os_tsk.run->p_rlnk = (P_TCB)p_SCB;
mbed_official 80:2dab120a94c2 139 }
mbed_official 80:2dab120a94c2 140 rt_block(timeout, WAIT_SEM);
mbed_official 80:2dab120a94c2 141 return (OS_R_TMO);
mbed_official 80:2dab120a94c2 142 }
mbed_official 80:2dab120a94c2 143
mbed_official 80:2dab120a94c2 144
mbed_official 80:2dab120a94c2 145 /*--------------------------- isr_sem_send ----------------------------------*/
mbed_official 80:2dab120a94c2 146
mbed_official 80:2dab120a94c2 147 void isr_sem_send (OS_ID semaphore) {
mbed_official 80:2dab120a94c2 148 /* Same function as "os_sem"send", but to be called by ISRs */
mbed_official 80:2dab120a94c2 149 P_SCB p_SCB = semaphore;
mbed_official 80:2dab120a94c2 150
mbed_official 80:2dab120a94c2 151 rt_psq_enq (p_SCB, 0);
mbed_official 80:2dab120a94c2 152 rt_psh_req ();
mbed_official 80:2dab120a94c2 153 }
mbed_official 80:2dab120a94c2 154
mbed_official 80:2dab120a94c2 155
mbed_official 80:2dab120a94c2 156 /*--------------------------- rt_sem_psh ------------------------------------*/
mbed_official 80:2dab120a94c2 157
mbed_official 80:2dab120a94c2 158 void rt_sem_psh (P_SCB p_CB) {
mbed_official 80:2dab120a94c2 159 /* Check if task has to be waken up */
mbed_official 80:2dab120a94c2 160 P_TCB p_TCB;
mbed_official 80:2dab120a94c2 161
mbed_official 80:2dab120a94c2 162 if (p_CB->p_lnk != NULL) {
mbed_official 80:2dab120a94c2 163 /* A task is waiting for token */
mbed_official 80:2dab120a94c2 164 p_TCB = rt_get_first ((P_XCB)p_CB);
mbed_official 80:2dab120a94c2 165 rt_rmv_dly (p_TCB);
mbed_official 80:2dab120a94c2 166 p_TCB->state = READY;
mbed_official 80:2dab120a94c2 167 #ifdef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 168 rt_ret_val(p_TCB, 1);
mbed_official 80:2dab120a94c2 169 #else
mbed_official 80:2dab120a94c2 170 rt_ret_val(p_TCB, OS_R_SEM);
mbed_official 80:2dab120a94c2 171 #endif
mbed_official 80:2dab120a94c2 172 rt_put_prio (&os_rdy, p_TCB);
mbed_official 80:2dab120a94c2 173 }
mbed_official 80:2dab120a94c2 174 else {
mbed_official 80:2dab120a94c2 175 /* Store token */
mbed_official 80:2dab120a94c2 176 p_CB->tokens++;
mbed_official 80:2dab120a94c2 177 }
mbed_official 80:2dab120a94c2 178 }
mbed_official 80:2dab120a94c2 179
mbed_official 80:2dab120a94c2 180 /*----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 181 * end of file
mbed_official 80:2dab120a94c2 182 *---------------------------------------------------------------------------*/
mbed_official 80:2dab120a94c2 183