test

Dependencies:   F7_Ethernet mbed BSP_DISCO_F746NG SDFileSystem RF24

Committer:
lowlowry
Date:
Tue Jun 08 16:21:16 2021 +0000
Revision:
3:1c6da30ca347
Parent:
0:d984976f1f1c
test final

Who changed what in which revision?

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