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