Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

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