Lab Checkoff

Dependencies:   SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem

Committer:
doubster
Date:
Wed Nov 13 20:00:28 2013 +0000
Revision:
0:67dbd54e60d4
Lab Checkoff

Who changed what in which revision?

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