Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
source/rtos/rt_Mutex.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_Mutex.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_List.h" |
Pawel Zarembski |
0:01f31e923fe2 | 25 | #include "rt_Task.h" |
Pawel Zarembski |
0:01f31e923fe2 | 26 | #include "rt_Mutex.h" |
Pawel Zarembski |
0:01f31e923fe2 | 27 | |
Pawel Zarembski |
0:01f31e923fe2 | 28 | |
Pawel Zarembski |
0:01f31e923fe2 | 29 | /*---------------------------------------------------------------------------- |
Pawel Zarembski |
0:01f31e923fe2 | 30 | * Functions |
Pawel Zarembski |
0:01f31e923fe2 | 31 | *---------------------------------------------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 32 | |
Pawel Zarembski |
0:01f31e923fe2 | 33 | |
Pawel Zarembski |
0:01f31e923fe2 | 34 | /*--------------------------- rt_mut_init -----------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 35 | |
Pawel Zarembski |
0:01f31e923fe2 | 36 | void rt_mut_init (OS_ID mutex) { |
Pawel Zarembski |
0:01f31e923fe2 | 37 | /* Initialize a mutex object */ |
Pawel Zarembski |
0:01f31e923fe2 | 38 | P_MUCB p_MCB = mutex; |
Pawel Zarembski |
0:01f31e923fe2 | 39 | |
Pawel Zarembski |
0:01f31e923fe2 | 40 | p_MCB->cb_type = MUCB; |
Pawel Zarembski |
0:01f31e923fe2 | 41 | p_MCB->prio = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 42 | p_MCB->level = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 43 | p_MCB->p_lnk = NULL; |
Pawel Zarembski |
0:01f31e923fe2 | 44 | p_MCB->owner = NULL; |
Pawel Zarembski |
0:01f31e923fe2 | 45 | } |
Pawel Zarembski |
0:01f31e923fe2 | 46 | |
Pawel Zarembski |
0:01f31e923fe2 | 47 | |
Pawel Zarembski |
0:01f31e923fe2 | 48 | /*--------------------------- rt_mut_release --------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 49 | |
Pawel Zarembski |
0:01f31e923fe2 | 50 | OS_RESULT rt_mut_release (OS_ID mutex) { |
Pawel Zarembski |
0:01f31e923fe2 | 51 | /* Release a mutex object */ |
Pawel Zarembski |
0:01f31e923fe2 | 52 | P_MUCB p_MCB = mutex; |
Pawel Zarembski |
0:01f31e923fe2 | 53 | P_TCB p_TCB; |
Pawel Zarembski |
0:01f31e923fe2 | 54 | |
Pawel Zarembski |
0:01f31e923fe2 | 55 | if (p_MCB->level == 0 || p_MCB->owner != os_tsk.run) { |
Pawel Zarembski |
0:01f31e923fe2 | 56 | /* Unbalanced mutex release or task is not the owner */ |
Pawel Zarembski |
0:01f31e923fe2 | 57 | return (OS_R_NOK); |
Pawel Zarembski |
0:01f31e923fe2 | 58 | } |
Pawel Zarembski |
0:01f31e923fe2 | 59 | if (--p_MCB->level != 0) { |
Pawel Zarembski |
0:01f31e923fe2 | 60 | return (OS_R_OK); |
Pawel Zarembski |
0:01f31e923fe2 | 61 | } |
Pawel Zarembski |
0:01f31e923fe2 | 62 | /* Restore owner task's priority. */ |
Pawel Zarembski |
0:01f31e923fe2 | 63 | os_tsk.run->prio = p_MCB->prio; |
Pawel Zarembski |
0:01f31e923fe2 | 64 | if (p_MCB->p_lnk != NULL) { |
Pawel Zarembski |
0:01f31e923fe2 | 65 | /* A task is waiting for mutex. */ |
Pawel Zarembski |
0:01f31e923fe2 | 66 | p_TCB = rt_get_first ((P_XCB)p_MCB); |
Pawel Zarembski |
0:01f31e923fe2 | 67 | p_TCB->ret_val = OS_R_MUT; |
Pawel Zarembski |
0:01f31e923fe2 | 68 | rt_rmv_dly (p_TCB); |
Pawel Zarembski |
0:01f31e923fe2 | 69 | /* A waiting task becomes the owner of this mutex. */ |
Pawel Zarembski |
0:01f31e923fe2 | 70 | p_MCB->level = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 71 | p_MCB->owner = p_TCB; |
Pawel Zarembski |
0:01f31e923fe2 | 72 | p_MCB->prio = p_TCB->prio; |
Pawel Zarembski |
0:01f31e923fe2 | 73 | /* Priority inversion, check which task continues. */ |
Pawel Zarembski |
0:01f31e923fe2 | 74 | if (os_tsk.run->prio >= rt_rdy_prio()) { |
Pawel Zarembski |
0:01f31e923fe2 | 75 | rt_dispatch (p_TCB); |
Pawel Zarembski |
0:01f31e923fe2 | 76 | } |
Pawel Zarembski |
0:01f31e923fe2 | 77 | else { |
Pawel Zarembski |
0:01f31e923fe2 | 78 | /* Ready task has higher priority than running task. */ |
Pawel Zarembski |
0:01f31e923fe2 | 79 | rt_put_prio (&os_rdy, os_tsk.run); |
Pawel Zarembski |
0:01f31e923fe2 | 80 | rt_put_prio (&os_rdy, p_TCB); |
Pawel Zarembski |
0:01f31e923fe2 | 81 | os_tsk.run->state = READY; |
Pawel Zarembski |
0:01f31e923fe2 | 82 | p_TCB->state = READY; |
Pawel Zarembski |
0:01f31e923fe2 | 83 | rt_dispatch (NULL); |
Pawel Zarembski |
0:01f31e923fe2 | 84 | } |
Pawel Zarembski |
0:01f31e923fe2 | 85 | os_tsk.run->ret_val = OS_R_OK; |
Pawel Zarembski |
0:01f31e923fe2 | 86 | } |
Pawel Zarembski |
0:01f31e923fe2 | 87 | else { |
Pawel Zarembski |
0:01f31e923fe2 | 88 | /* Check if own priority raised by priority inversion. */ |
Pawel Zarembski |
0:01f31e923fe2 | 89 | if (rt_rdy_prio() > os_tsk.run->prio) { |
Pawel Zarembski |
0:01f31e923fe2 | 90 | rt_put_prio (&os_rdy, os_tsk.run); |
Pawel Zarembski |
0:01f31e923fe2 | 91 | os_tsk.run->state = READY; |
Pawel Zarembski |
0:01f31e923fe2 | 92 | rt_dispatch (NULL); |
Pawel Zarembski |
0:01f31e923fe2 | 93 | os_tsk.run->ret_val = OS_R_OK; |
Pawel Zarembski |
0:01f31e923fe2 | 94 | } |
Pawel Zarembski |
0:01f31e923fe2 | 95 | } |
Pawel Zarembski |
0:01f31e923fe2 | 96 | return (OS_R_OK); |
Pawel Zarembski |
0:01f31e923fe2 | 97 | } |
Pawel Zarembski |
0:01f31e923fe2 | 98 | |
Pawel Zarembski |
0:01f31e923fe2 | 99 | |
Pawel Zarembski |
0:01f31e923fe2 | 100 | /*--------------------------- rt_mut_wait -----------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 101 | |
Pawel Zarembski |
0:01f31e923fe2 | 102 | OS_RESULT rt_mut_wait (OS_ID mutex, U16 timeout) { |
Pawel Zarembski |
0:01f31e923fe2 | 103 | /* Wait for a mutex, continue when mutex is free. */ |
Pawel Zarembski |
0:01f31e923fe2 | 104 | P_MUCB p_MCB = mutex; |
Pawel Zarembski |
0:01f31e923fe2 | 105 | |
Pawel Zarembski |
0:01f31e923fe2 | 106 | if (p_MCB->level == 0) { |
Pawel Zarembski |
0:01f31e923fe2 | 107 | p_MCB->owner = os_tsk.run; |
Pawel Zarembski |
0:01f31e923fe2 | 108 | p_MCB->prio = os_tsk.run->prio; |
Pawel Zarembski |
0:01f31e923fe2 | 109 | goto inc; |
Pawel Zarembski |
0:01f31e923fe2 | 110 | } |
Pawel Zarembski |
0:01f31e923fe2 | 111 | if (p_MCB->owner == os_tsk.run) { |
Pawel Zarembski |
0:01f31e923fe2 | 112 | /* OK, running task is the owner of this mutex. */ |
Pawel Zarembski |
0:01f31e923fe2 | 113 | inc:p_MCB->level++; |
Pawel Zarembski |
0:01f31e923fe2 | 114 | return (OS_R_OK); |
Pawel Zarembski |
0:01f31e923fe2 | 115 | } |
Pawel Zarembski |
0:01f31e923fe2 | 116 | /* Mutex owned by another task, wait until released. */ |
Pawel Zarembski |
0:01f31e923fe2 | 117 | if (timeout == 0) { |
Pawel Zarembski |
0:01f31e923fe2 | 118 | return (OS_R_TMO); |
Pawel Zarembski |
0:01f31e923fe2 | 119 | } |
Pawel Zarembski |
0:01f31e923fe2 | 120 | /* Raise the owner task priority if lower than current priority. */ |
Pawel Zarembski |
0:01f31e923fe2 | 121 | /* This priority inversion is called priority inheritance. */ |
Pawel Zarembski |
0:01f31e923fe2 | 122 | if (p_MCB->prio < os_tsk.run->prio) { |
Pawel Zarembski |
0:01f31e923fe2 | 123 | p_MCB->owner->prio = os_tsk.run->prio; |
Pawel Zarembski |
0:01f31e923fe2 | 124 | rt_resort_prio (p_MCB->owner); |
Pawel Zarembski |
0:01f31e923fe2 | 125 | } |
Pawel Zarembski |
0:01f31e923fe2 | 126 | if (p_MCB->p_lnk != NULL) { |
Pawel Zarembski |
0:01f31e923fe2 | 127 | rt_put_prio ((P_XCB)p_MCB, os_tsk.run); |
Pawel Zarembski |
0:01f31e923fe2 | 128 | } |
Pawel Zarembski |
0:01f31e923fe2 | 129 | else { |
Pawel Zarembski |
0:01f31e923fe2 | 130 | p_MCB->p_lnk = os_tsk.run; |
Pawel Zarembski |
0:01f31e923fe2 | 131 | os_tsk.run->p_lnk = NULL; |
Pawel Zarembski |
0:01f31e923fe2 | 132 | os_tsk.run->p_rlnk = (P_TCB)p_MCB; |
Pawel Zarembski |
0:01f31e923fe2 | 133 | } |
Pawel Zarembski |
0:01f31e923fe2 | 134 | rt_block(timeout, WAIT_MUT); |
Pawel Zarembski |
0:01f31e923fe2 | 135 | return (OS_R_TMO); |
Pawel Zarembski |
0:01f31e923fe2 | 136 | } |
Pawel Zarembski |
0:01f31e923fe2 | 137 | |
Pawel Zarembski |
0:01f31e923fe2 | 138 | |
Pawel Zarembski |
0:01f31e923fe2 | 139 | /*---------------------------------------------------------------------------- |
Pawel Zarembski |
0:01f31e923fe2 | 140 | * end of file |
Pawel Zarembski |
0:01f31e923fe2 | 141 | *---------------------------------------------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 142 |