Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
source/rtos/rt_Semaphore.c@0:01f31e923fe2, 2020-04-07 (annotated)
- Committer:
- Pawel Zarembski
- Date:
- Tue Apr 07 12:55:42 2020 +0200
- Revision:
- 0:01f31e923fe2
hani: DAPLink with reset workaround
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:01f31e923fe2 | 1 | /** |
Pawel Zarembski |
0:01f31e923fe2 | 2 | * @file rt_Semaphore.c |
Pawel Zarembski |
0:01f31e923fe2 | 3 | * @brief |
Pawel Zarembski |
0:01f31e923fe2 | 4 | * |
Pawel Zarembski |
0:01f31e923fe2 | 5 | * DAPLink Interface Firmware |
Pawel Zarembski |
0:01f31e923fe2 | 6 | * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved |
Pawel Zarembski |
0:01f31e923fe2 | 7 | * SPDX-License-Identifier: Apache-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 8 | * |
Pawel Zarembski |
0:01f31e923fe2 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Pawel Zarembski |
0:01f31e923fe2 | 10 | * not use this file except in compliance with the License. |
Pawel Zarembski |
0:01f31e923fe2 | 11 | * You may obtain a copy of the License at |
Pawel Zarembski |
0:01f31e923fe2 | 12 | * |
Pawel Zarembski |
0:01f31e923fe2 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 14 | * |
Pawel Zarembski |
0:01f31e923fe2 | 15 | * Unless required by applicable law or agreed to in writing, software |
Pawel Zarembski |
0:01f31e923fe2 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Pawel Zarembski |
0:01f31e923fe2 | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pawel Zarembski |
0:01f31e923fe2 | 18 | * See the License for the specific language governing permissions and |
Pawel Zarembski |
0:01f31e923fe2 | 19 | * limitations under the License. |
Pawel Zarembski |
0:01f31e923fe2 | 20 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 21 | |
Pawel Zarembski |
0:01f31e923fe2 | 22 | #include "rt_TypeDef.h" |
Pawel Zarembski |
0:01f31e923fe2 | 23 | #include "RTX_Config.h" |
Pawel Zarembski |
0:01f31e923fe2 | 24 | #include "rt_System.h" |
Pawel Zarembski |
0:01f31e923fe2 | 25 | #include "rt_List.h" |
Pawel Zarembski |
0:01f31e923fe2 | 26 | #include "rt_Task.h" |
Pawel Zarembski |
0:01f31e923fe2 | 27 | #include "rt_Semaphore.h" |
Pawel Zarembski |
0:01f31e923fe2 | 28 | |
Pawel Zarembski |
0:01f31e923fe2 | 29 | |
Pawel Zarembski |
0:01f31e923fe2 | 30 | /*---------------------------------------------------------------------------- |
Pawel Zarembski |
0:01f31e923fe2 | 31 | * Functions |
Pawel Zarembski |
0:01f31e923fe2 | 32 | *---------------------------------------------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 33 | |
Pawel Zarembski |
0:01f31e923fe2 | 34 | |
Pawel Zarembski |
0:01f31e923fe2 | 35 | /*--------------------------- rt_sem_init -----------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 36 | |
Pawel Zarembski |
0:01f31e923fe2 | 37 | void rt_sem_init (OS_ID semaphore, U16 token_count) { |
Pawel Zarembski |
0:01f31e923fe2 | 38 | /* Initialize a semaphore */ |
Pawel Zarembski |
0:01f31e923fe2 | 39 | P_SCB p_SCB = semaphore; |
Pawel Zarembski |
0:01f31e923fe2 | 40 | |
Pawel Zarembski |
0:01f31e923fe2 | 41 | p_SCB->cb_type = SCB; |
Pawel Zarembski |
0:01f31e923fe2 | 42 | p_SCB->p_lnk = NULL; |
Pawel Zarembski |
0:01f31e923fe2 | 43 | p_SCB->tokens = token_count; |
Pawel Zarembski |
0:01f31e923fe2 | 44 | } |
Pawel Zarembski |
0:01f31e923fe2 | 45 | |
Pawel Zarembski |
0:01f31e923fe2 | 46 | |
Pawel Zarembski |
0:01f31e923fe2 | 47 | /*--------------------------- rt_sem_send -----------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 48 | |
Pawel Zarembski |
0:01f31e923fe2 | 49 | OS_RESULT rt_sem_send (OS_ID semaphore) { |
Pawel Zarembski |
0:01f31e923fe2 | 50 | /* Return a token to semaphore */ |
Pawel Zarembski |
0:01f31e923fe2 | 51 | P_SCB p_SCB = semaphore; |
Pawel Zarembski |
0:01f31e923fe2 | 52 | P_TCB p_TCB; |
Pawel Zarembski |
0:01f31e923fe2 | 53 | |
Pawel Zarembski |
0:01f31e923fe2 | 54 | if (p_SCB->p_lnk != NULL) { |
Pawel Zarembski |
0:01f31e923fe2 | 55 | /* A task is waiting for token */ |
Pawel Zarembski |
0:01f31e923fe2 | 56 | p_TCB = rt_get_first ((P_XCB)p_SCB); |
Pawel Zarembski |
0:01f31e923fe2 | 57 | p_TCB->ret_val = OS_R_SEM; |
Pawel Zarembski |
0:01f31e923fe2 | 58 | rt_rmv_dly (p_TCB); |
Pawel Zarembski |
0:01f31e923fe2 | 59 | rt_dispatch (p_TCB); |
Pawel Zarembski |
0:01f31e923fe2 | 60 | os_tsk.run->ret_val = OS_R_OK; |
Pawel Zarembski |
0:01f31e923fe2 | 61 | } |
Pawel Zarembski |
0:01f31e923fe2 | 62 | else { |
Pawel Zarembski |
0:01f31e923fe2 | 63 | /* Store token. */ |
Pawel Zarembski |
0:01f31e923fe2 | 64 | p_SCB->tokens++; |
Pawel Zarembski |
0:01f31e923fe2 | 65 | } |
Pawel Zarembski |
0:01f31e923fe2 | 66 | return (OS_R_OK); |
Pawel Zarembski |
0:01f31e923fe2 | 67 | } |
Pawel Zarembski |
0:01f31e923fe2 | 68 | |
Pawel Zarembski |
0:01f31e923fe2 | 69 | |
Pawel Zarembski |
0:01f31e923fe2 | 70 | /*--------------------------- rt_sem_wait -----------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 71 | |
Pawel Zarembski |
0:01f31e923fe2 | 72 | OS_RESULT rt_sem_wait (OS_ID semaphore, U16 timeout) { |
Pawel Zarembski |
0:01f31e923fe2 | 73 | /* Obtain a token; possibly wait for it */ |
Pawel Zarembski |
0:01f31e923fe2 | 74 | P_SCB p_SCB = semaphore; |
Pawel Zarembski |
0:01f31e923fe2 | 75 | |
Pawel Zarembski |
0:01f31e923fe2 | 76 | if (p_SCB->tokens) { |
Pawel Zarembski |
0:01f31e923fe2 | 77 | p_SCB->tokens--; |
Pawel Zarembski |
0:01f31e923fe2 | 78 | return (OS_R_OK); |
Pawel Zarembski |
0:01f31e923fe2 | 79 | } |
Pawel Zarembski |
0:01f31e923fe2 | 80 | /* No token available: wait for one */ |
Pawel Zarembski |
0:01f31e923fe2 | 81 | if (timeout == 0) { |
Pawel Zarembski |
0:01f31e923fe2 | 82 | return (OS_R_TMO); |
Pawel Zarembski |
0:01f31e923fe2 | 83 | } |
Pawel Zarembski |
0:01f31e923fe2 | 84 | if (p_SCB->p_lnk != NULL) { |
Pawel Zarembski |
0:01f31e923fe2 | 85 | rt_put_prio ((P_XCB)p_SCB, os_tsk.run); |
Pawel Zarembski |
0:01f31e923fe2 | 86 | } |
Pawel Zarembski |
0:01f31e923fe2 | 87 | else { |
Pawel Zarembski |
0:01f31e923fe2 | 88 | p_SCB->p_lnk = os_tsk.run; |
Pawel Zarembski |
0:01f31e923fe2 | 89 | os_tsk.run->p_lnk = NULL; |
Pawel Zarembski |
0:01f31e923fe2 | 90 | os_tsk.run->p_rlnk = (P_TCB)p_SCB; |
Pawel Zarembski |
0:01f31e923fe2 | 91 | } |
Pawel Zarembski |
0:01f31e923fe2 | 92 | rt_block(timeout, WAIT_SEM); |
Pawel Zarembski |
0:01f31e923fe2 | 93 | return (OS_R_TMO); |
Pawel Zarembski |
0:01f31e923fe2 | 94 | } |
Pawel Zarembski |
0:01f31e923fe2 | 95 | |
Pawel Zarembski |
0:01f31e923fe2 | 96 | |
Pawel Zarembski |
0:01f31e923fe2 | 97 | /*--------------------------- isr_sem_send ----------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 98 | |
Pawel Zarembski |
0:01f31e923fe2 | 99 | void isr_sem_send (OS_ID semaphore) { |
Pawel Zarembski |
0:01f31e923fe2 | 100 | /* Same function as "os_sem"send", but to be called by ISRs */ |
Pawel Zarembski |
0:01f31e923fe2 | 101 | P_SCB p_SCB = semaphore; |
Pawel Zarembski |
0:01f31e923fe2 | 102 | |
Pawel Zarembski |
0:01f31e923fe2 | 103 | rt_psq_enq (p_SCB, 0); |
Pawel Zarembski |
0:01f31e923fe2 | 104 | rt_psh_req (); |
Pawel Zarembski |
0:01f31e923fe2 | 105 | } |
Pawel Zarembski |
0:01f31e923fe2 | 106 | |
Pawel Zarembski |
0:01f31e923fe2 | 107 | |
Pawel Zarembski |
0:01f31e923fe2 | 108 | /*--------------------------- rt_sem_psh ------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 109 | |
Pawel Zarembski |
0:01f31e923fe2 | 110 | void rt_sem_psh (P_SCB p_CB) { |
Pawel Zarembski |
0:01f31e923fe2 | 111 | /* Check if task has to be waken up */ |
Pawel Zarembski |
0:01f31e923fe2 | 112 | P_TCB p_TCB; |
Pawel Zarembski |
0:01f31e923fe2 | 113 | |
Pawel Zarembski |
0:01f31e923fe2 | 114 | if (p_CB->p_lnk != NULL) { |
Pawel Zarembski |
0:01f31e923fe2 | 115 | /* A task is waiting for token */ |
Pawel Zarembski |
0:01f31e923fe2 | 116 | p_TCB = rt_get_first ((P_XCB)p_CB); |
Pawel Zarembski |
0:01f31e923fe2 | 117 | rt_rmv_dly (p_TCB); |
Pawel Zarembski |
0:01f31e923fe2 | 118 | p_TCB->state = READY; |
Pawel Zarembski |
0:01f31e923fe2 | 119 | p_TCB->ret_val = OS_R_SEM; |
Pawel Zarembski |
0:01f31e923fe2 | 120 | rt_put_prio (&os_rdy, p_TCB); |
Pawel Zarembski |
0:01f31e923fe2 | 121 | } |
Pawel Zarembski |
0:01f31e923fe2 | 122 | else { |
Pawel Zarembski |
0:01f31e923fe2 | 123 | /* Store token */ |
Pawel Zarembski |
0:01f31e923fe2 | 124 | p_CB->tokens++; |
Pawel Zarembski |
0:01f31e923fe2 | 125 | } |
Pawel Zarembski |
0:01f31e923fe2 | 126 | } |
Pawel Zarembski |
0:01f31e923fe2 | 127 | |
Pawel Zarembski |
0:01f31e923fe2 | 128 | /*---------------------------------------------------------------------------- |
Pawel Zarembski |
0:01f31e923fe2 | 129 | * end of file |
Pawel Zarembski |
0:01f31e923fe2 | 130 | *---------------------------------------------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 131 |