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