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