Control a robot over the internet using UDP and a Ethernet interface.

Dependencies:   EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rt_Semaphore.c Source File

rt_Semaphore.c

00001 /*----------------------------------------------------------------------------
00002  *      RL-ARM - RTX
00003  *----------------------------------------------------------------------------
00004  *      Name:    RT_SEMAPHORE.C
00005  *      Purpose: Implements binary and counting semaphores
00006  *      Rev.:    V4.60
00007  *----------------------------------------------------------------------------
00008  *
00009  * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
00010  * All rights reserved.
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are met:
00013  *  - Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  *  - Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  *  - Neither the name of ARM  nor the names of its contributors may be used 
00019  *    to endorse or promote products derived from this software without 
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
00026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  *---------------------------------------------------------------------------*/
00034 
00035 #include "rt_TypeDef.h"
00036 #include "RTX_Conf.h"
00037 #include "rt_System.h"
00038 #include "rt_List.h"
00039 #include "rt_Task.h"
00040 #include "rt_Semaphore.h"
00041 #include "rt_HAL_CM.h"
00042 
00043 
00044 /*----------------------------------------------------------------------------
00045  *      Functions
00046  *---------------------------------------------------------------------------*/
00047 
00048 
00049 /*--------------------------- rt_sem_init -----------------------------------*/
00050 
00051 void rt_sem_init (OS_ID semaphore, U16 token_count) {
00052   /* Initialize a semaphore */
00053   P_SCB p_SCB = semaphore;
00054 
00055   p_SCB->cb_type = SCB;
00056   p_SCB->p_lnk  = NULL;
00057   p_SCB->tokens = token_count;
00058 }
00059 
00060 
00061 /*--------------------------- rt_sem_delete ---------------------------------*/
00062 
00063 #ifdef __CMSIS_RTOS
00064 OS_RESULT rt_sem_delete (OS_ID semaphore) {
00065   /* Delete semaphore */
00066   P_SCB p_SCB = semaphore;
00067   P_TCB p_TCB;
00068 
00069   while (p_SCB->p_lnk != NULL) {
00070     /* A task is waiting for token */
00071     p_TCB = rt_get_first ((P_XCB)p_SCB);
00072     rt_ret_val(p_TCB, 0);
00073     rt_rmv_dly(p_TCB);
00074     p_TCB->state = READY;
00075     rt_put_prio (&os_rdy, p_TCB);
00076   }
00077 
00078   if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
00079     /* preempt running task */
00080     rt_put_prio (&os_rdy, os_tsk.run);
00081     os_tsk.run->state = READY;
00082     rt_dispatch (NULL);
00083   }
00084 
00085   p_SCB->cb_type = 0;
00086 
00087   return (OS_R_OK);
00088 }
00089 #endif
00090 
00091 
00092 /*--------------------------- rt_sem_send -----------------------------------*/
00093 
00094 OS_RESULT rt_sem_send (OS_ID semaphore) {
00095   /* Return a token to semaphore */
00096   P_SCB p_SCB = semaphore;
00097   P_TCB p_TCB;
00098 
00099   if (p_SCB->p_lnk != NULL) {
00100     /* A task is waiting for token */
00101     p_TCB = rt_get_first ((P_XCB)p_SCB);
00102 #ifdef __CMSIS_RTOS
00103     rt_ret_val(p_TCB, 1);
00104 #else
00105     rt_ret_val(p_TCB, OS_R_SEM);
00106 #endif
00107     rt_rmv_dly (p_TCB);
00108     rt_dispatch (p_TCB);
00109   }
00110   else {
00111     /* Store token. */
00112     p_SCB->tokens++;
00113   }
00114   return (OS_R_OK);
00115 }
00116 
00117 
00118 /*--------------------------- rt_sem_wait -----------------------------------*/
00119 
00120 OS_RESULT rt_sem_wait (OS_ID semaphore, U16 timeout) {
00121   /* Obtain a token; possibly wait for it */
00122   P_SCB p_SCB = semaphore;
00123 
00124   if (p_SCB->tokens) {
00125     p_SCB->tokens--;
00126     return (OS_R_OK);
00127   }
00128   /* No token available: wait for one */
00129   if (timeout == 0) {
00130     return (OS_R_TMO);
00131   }
00132   if (p_SCB->p_lnk != NULL) {
00133     rt_put_prio ((P_XCB)p_SCB, os_tsk.run);
00134   }
00135   else {
00136     p_SCB->p_lnk = os_tsk.run;
00137     os_tsk.run->p_lnk = NULL;
00138     os_tsk.run->p_rlnk = (P_TCB)p_SCB;
00139   }
00140   rt_block(timeout, WAIT_SEM);
00141   return (OS_R_TMO);
00142 }
00143 
00144 
00145 /*--------------------------- isr_sem_send ----------------------------------*/
00146 
00147 void isr_sem_send (OS_ID semaphore) {
00148   /* Same function as "os_sem"send", but to be called by ISRs */
00149   P_SCB p_SCB = semaphore;
00150 
00151   rt_psq_enq (p_SCB, 0);
00152   rt_psh_req ();
00153 }
00154 
00155 
00156 /*--------------------------- rt_sem_psh ------------------------------------*/
00157 
00158 void rt_sem_psh (P_SCB p_CB) {
00159   /* Check if task has to be waken up */
00160   P_TCB p_TCB;
00161 
00162   if (p_CB->p_lnk != NULL) {
00163     /* A task is waiting for token */
00164     p_TCB = rt_get_first ((P_XCB)p_CB);
00165     rt_rmv_dly (p_TCB);
00166     p_TCB->state   = READY;
00167 #ifdef __CMSIS_RTOS
00168     rt_ret_val(p_TCB, 1);
00169 #else
00170     rt_ret_val(p_TCB, OS_R_SEM);
00171 #endif
00172     rt_put_prio (&os_rdy, p_TCB);
00173   }
00174   else {
00175     /* Store token */
00176     p_CB->tokens++;
00177   }
00178 }
00179 
00180 /*----------------------------------------------------------------------------
00181  * end of file
00182  *---------------------------------------------------------------------------*/
00183