mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 2 * RL-ARM - RTX
elessair 0:f269e3021894 3 *----------------------------------------------------------------------------
elessair 0:f269e3021894 4 * Name: RT_MUTEX.C
elessair 0:f269e3021894 5 * Purpose: Implements mutex synchronization objects
elessair 0:f269e3021894 6 * Rev.: V4.60
elessair 0:f269e3021894 7 *----------------------------------------------------------------------------
elessair 0:f269e3021894 8 *
elessair 0:f269e3021894 9 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
elessair 0:f269e3021894 10 * All rights reserved.
elessair 0:f269e3021894 11 * Redistribution and use in source and binary forms, with or without
elessair 0:f269e3021894 12 * modification, are permitted provided that the following conditions are met:
elessair 0:f269e3021894 13 * - Redistributions of source code must retain the above copyright
elessair 0:f269e3021894 14 * notice, this list of conditions and the following disclaimer.
elessair 0:f269e3021894 15 * - Redistributions in binary form must reproduce the above copyright
elessair 0:f269e3021894 16 * notice, this list of conditions and the following disclaimer in the
elessair 0:f269e3021894 17 * documentation and/or other materials provided with the distribution.
elessair 0:f269e3021894 18 * - Neither the name of ARM nor the names of its contributors may be used
elessair 0:f269e3021894 19 * to endorse or promote products derived from this software without
elessair 0:f269e3021894 20 * specific prior written permission.
elessair 0:f269e3021894 21 *
elessair 0:f269e3021894 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
elessair 0:f269e3021894 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
elessair 0:f269e3021894 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
elessair 0:f269e3021894 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
elessair 0:f269e3021894 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
elessair 0:f269e3021894 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
elessair 0:f269e3021894 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
elessair 0:f269e3021894 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
elessair 0:f269e3021894 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
elessair 0:f269e3021894 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
elessair 0:f269e3021894 32 * POSSIBILITY OF SUCH DAMAGE.
elessair 0:f269e3021894 33 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 #include "rt_TypeDef.h"
elessair 0:f269e3021894 36 #include "RTX_Conf.h"
elessair 0:f269e3021894 37 #include "rt_List.h"
elessair 0:f269e3021894 38 #include "rt_Task.h"
elessair 0:f269e3021894 39 #include "rt_Mutex.h"
elessair 0:f269e3021894 40 #include "rt_HAL_CM.h"
elessair 0:f269e3021894 41
elessair 0:f269e3021894 42
elessair 0:f269e3021894 43 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 44 * Functions
elessair 0:f269e3021894 45 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 46
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 /*--------------------------- rt_mut_init -----------------------------------*/
elessair 0:f269e3021894 49
elessair 0:f269e3021894 50 void rt_mut_init (OS_ID mutex) {
elessair 0:f269e3021894 51 /* Initialize a mutex object */
elessair 0:f269e3021894 52 P_MUCB p_MCB = mutex;
elessair 0:f269e3021894 53
elessair 0:f269e3021894 54 p_MCB->cb_type = MUCB;
elessair 0:f269e3021894 55 p_MCB->prio = 0;
elessair 0:f269e3021894 56 p_MCB->level = 0;
elessair 0:f269e3021894 57 p_MCB->p_lnk = NULL;
elessair 0:f269e3021894 58 p_MCB->owner = NULL;
elessair 0:f269e3021894 59 }
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61
elessair 0:f269e3021894 62 /*--------------------------- rt_mut_delete ---------------------------------*/
elessair 0:f269e3021894 63
elessair 0:f269e3021894 64 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 65 OS_RESULT rt_mut_delete (OS_ID mutex) {
elessair 0:f269e3021894 66 /* Delete a mutex object */
elessair 0:f269e3021894 67 P_MUCB p_MCB = mutex;
elessair 0:f269e3021894 68 P_TCB p_TCB;
elessair 0:f269e3021894 69
elessair 0:f269e3021894 70 /* Restore owner task's priority. */
elessair 0:f269e3021894 71 if (p_MCB->level != 0) {
elessair 0:f269e3021894 72 p_MCB->owner->prio = p_MCB->prio;
elessair 0:f269e3021894 73 if (p_MCB->owner != os_tsk.run) {
elessair 0:f269e3021894 74 rt_resort_prio (p_MCB->owner);
elessair 0:f269e3021894 75 }
elessair 0:f269e3021894 76 }
elessair 0:f269e3021894 77
elessair 0:f269e3021894 78 while (p_MCB->p_lnk != NULL) {
elessair 0:f269e3021894 79 /* A task is waiting for mutex. */
elessair 0:f269e3021894 80 p_TCB = rt_get_first ((P_XCB)p_MCB);
elessair 0:f269e3021894 81 rt_ret_val(p_TCB, 0/*osOK*/);
elessair 0:f269e3021894 82 rt_rmv_dly(p_TCB);
elessair 0:f269e3021894 83 p_TCB->state = READY;
elessair 0:f269e3021894 84 rt_put_prio (&os_rdy, p_TCB);
elessair 0:f269e3021894 85 }
elessair 0:f269e3021894 86
elessair 0:f269e3021894 87 if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
elessair 0:f269e3021894 88 /* preempt running task */
elessair 0:f269e3021894 89 rt_put_prio (&os_rdy, os_tsk.run);
elessair 0:f269e3021894 90 os_tsk.run->state = READY;
elessair 0:f269e3021894 91 rt_dispatch (NULL);
elessair 0:f269e3021894 92 }
elessair 0:f269e3021894 93
elessair 0:f269e3021894 94 p_MCB->cb_type = 0;
elessair 0:f269e3021894 95
elessair 0:f269e3021894 96 return (OS_R_OK);
elessair 0:f269e3021894 97 }
elessair 0:f269e3021894 98 #endif
elessair 0:f269e3021894 99
elessair 0:f269e3021894 100
elessair 0:f269e3021894 101 /*--------------------------- rt_mut_release --------------------------------*/
elessair 0:f269e3021894 102
elessair 0:f269e3021894 103 OS_RESULT rt_mut_release (OS_ID mutex) {
elessair 0:f269e3021894 104 /* Release a mutex object */
elessair 0:f269e3021894 105 P_MUCB p_MCB = mutex;
elessair 0:f269e3021894 106 P_TCB p_TCB;
elessair 0:f269e3021894 107
elessair 0:f269e3021894 108 if (p_MCB->level == 0 || p_MCB->owner != os_tsk.run) {
elessair 0:f269e3021894 109 /* Unbalanced mutex release or task is not the owner */
elessair 0:f269e3021894 110 return (OS_R_NOK);
elessair 0:f269e3021894 111 }
elessair 0:f269e3021894 112 if (--p_MCB->level != 0) {
elessair 0:f269e3021894 113 return (OS_R_OK);
elessair 0:f269e3021894 114 }
elessair 0:f269e3021894 115 /* Restore owner task's priority. */
elessair 0:f269e3021894 116 os_tsk.run->prio = p_MCB->prio;
elessair 0:f269e3021894 117 if (p_MCB->p_lnk != NULL) {
elessair 0:f269e3021894 118 /* A task is waiting for mutex. */
elessair 0:f269e3021894 119 p_TCB = rt_get_first ((P_XCB)p_MCB);
elessair 0:f269e3021894 120 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 121 rt_ret_val(p_TCB, 0/*osOK*/);
elessair 0:f269e3021894 122 #else
elessair 0:f269e3021894 123 rt_ret_val(p_TCB, OS_R_MUT);
elessair 0:f269e3021894 124 #endif
elessair 0:f269e3021894 125 rt_rmv_dly (p_TCB);
elessair 0:f269e3021894 126 /* A waiting task becomes the owner of this mutex. */
elessair 0:f269e3021894 127 p_MCB->level = 1;
elessair 0:f269e3021894 128 p_MCB->owner = p_TCB;
elessair 0:f269e3021894 129 p_MCB->prio = p_TCB->prio;
elessair 0:f269e3021894 130 /* Priority inversion, check which task continues. */
elessair 0:f269e3021894 131 if (os_tsk.run->prio >= rt_rdy_prio()) {
elessair 0:f269e3021894 132 rt_dispatch (p_TCB);
elessair 0:f269e3021894 133 }
elessair 0:f269e3021894 134 else {
elessair 0:f269e3021894 135 /* Ready task has higher priority than running task. */
elessair 0:f269e3021894 136 rt_put_prio (&os_rdy, os_tsk.run);
elessair 0:f269e3021894 137 rt_put_prio (&os_rdy, p_TCB);
elessair 0:f269e3021894 138 os_tsk.run->state = READY;
elessair 0:f269e3021894 139 p_TCB->state = READY;
elessair 0:f269e3021894 140 rt_dispatch (NULL);
elessair 0:f269e3021894 141 }
elessair 0:f269e3021894 142 }
elessair 0:f269e3021894 143 else {
elessair 0:f269e3021894 144 /* Check if own priority raised by priority inversion. */
elessair 0:f269e3021894 145 if (rt_rdy_prio() > os_tsk.run->prio) {
elessair 0:f269e3021894 146 rt_put_prio (&os_rdy, os_tsk.run);
elessair 0:f269e3021894 147 os_tsk.run->state = READY;
elessair 0:f269e3021894 148 rt_dispatch (NULL);
elessair 0:f269e3021894 149 }
elessair 0:f269e3021894 150 }
elessair 0:f269e3021894 151 return (OS_R_OK);
elessair 0:f269e3021894 152 }
elessair 0:f269e3021894 153
elessair 0:f269e3021894 154
elessair 0:f269e3021894 155 /*--------------------------- rt_mut_wait -----------------------------------*/
elessair 0:f269e3021894 156
elessair 0:f269e3021894 157 OS_RESULT rt_mut_wait (OS_ID mutex, U16 timeout) {
elessair 0:f269e3021894 158 /* Wait for a mutex, continue when mutex is free. */
elessair 0:f269e3021894 159 P_MUCB p_MCB = mutex;
elessair 0:f269e3021894 160
elessair 0:f269e3021894 161 if (p_MCB->level == 0) {
elessair 0:f269e3021894 162 p_MCB->owner = os_tsk.run;
elessair 0:f269e3021894 163 p_MCB->prio = os_tsk.run->prio;
elessair 0:f269e3021894 164 goto inc;
elessair 0:f269e3021894 165 }
elessair 0:f269e3021894 166 if (p_MCB->owner == os_tsk.run) {
elessair 0:f269e3021894 167 /* OK, running task is the owner of this mutex. */
elessair 0:f269e3021894 168 inc:p_MCB->level++;
elessair 0:f269e3021894 169 return (OS_R_OK);
elessair 0:f269e3021894 170 }
elessair 0:f269e3021894 171 /* Mutex owned by another task, wait until released. */
elessair 0:f269e3021894 172 if (timeout == 0) {
elessair 0:f269e3021894 173 return (OS_R_TMO);
elessair 0:f269e3021894 174 }
elessair 0:f269e3021894 175 /* Raise the owner task priority if lower than current priority. */
elessair 0:f269e3021894 176 /* This priority inversion is called priority inheritance. */
elessair 0:f269e3021894 177 if (p_MCB->prio < os_tsk.run->prio) {
elessair 0:f269e3021894 178 p_MCB->owner->prio = os_tsk.run->prio;
elessair 0:f269e3021894 179 rt_resort_prio (p_MCB->owner);
elessair 0:f269e3021894 180 }
elessair 0:f269e3021894 181 if (p_MCB->p_lnk != NULL) {
elessair 0:f269e3021894 182 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
elessair 0:f269e3021894 183 }
elessair 0:f269e3021894 184 else {
elessair 0:f269e3021894 185 p_MCB->p_lnk = os_tsk.run;
elessair 0:f269e3021894 186 os_tsk.run->p_lnk = NULL;
elessair 0:f269e3021894 187 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
elessair 0:f269e3021894 188 }
elessair 0:f269e3021894 189 rt_block(timeout, WAIT_MUT);
elessair 0:f269e3021894 190 return (OS_R_TMO);
elessair 0:f269e3021894 191 }
elessair 0:f269e3021894 192
elessair 0:f269e3021894 193
elessair 0:f269e3021894 194 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 195 * end of file
elessair 0:f269e3021894 196 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 197