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_EVENT.C
elessair 0:f269e3021894 5 * Purpose: Implements waits and wake-ups for event flags
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_System.h"
elessair 0:f269e3021894 38 #include "rt_Event.h"
elessair 0:f269e3021894 39 #include "rt_List.h"
elessair 0:f269e3021894 40 #include "rt_Task.h"
elessair 0:f269e3021894 41 #include "rt_HAL_CM.h"
elessair 0:f269e3021894 42
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 45 * Functions
elessair 0:f269e3021894 46 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49 /*--------------------------- rt_evt_wait -----------------------------------*/
elessair 0:f269e3021894 50
elessair 0:f269e3021894 51 OS_RESULT rt_evt_wait (U16 wait_flags, U16 timeout, BOOL and_wait) {
elessair 0:f269e3021894 52 /* Wait for one or more event flags with optional time-out. */
elessair 0:f269e3021894 53 /* "wait_flags" identifies the flags to wait for. */
elessair 0:f269e3021894 54 /* "timeout" is the time-out limit in system ticks (0xffff if no time-out) */
elessair 0:f269e3021894 55 /* "and_wait" specifies the AND-ing of "wait_flags" as condition to be met */
elessair 0:f269e3021894 56 /* to complete the wait. (OR-ing if set to 0). */
elessair 0:f269e3021894 57 U32 block_state;
elessair 0:f269e3021894 58
elessair 0:f269e3021894 59 if (and_wait) {
elessair 0:f269e3021894 60 /* Check for AND-connected events */
elessair 0:f269e3021894 61 if ((os_tsk.run->events & wait_flags) == wait_flags) {
elessair 0:f269e3021894 62 os_tsk.run->events &= ~wait_flags;
elessair 0:f269e3021894 63 return (OS_R_EVT);
elessair 0:f269e3021894 64 }
elessair 0:f269e3021894 65 block_state = WAIT_AND;
elessair 0:f269e3021894 66 }
elessair 0:f269e3021894 67 else {
elessair 0:f269e3021894 68 /* Check for OR-connected events */
elessair 0:f269e3021894 69 if (os_tsk.run->events & wait_flags) {
elessair 0:f269e3021894 70 os_tsk.run->waits = os_tsk.run->events & wait_flags;
elessair 0:f269e3021894 71 os_tsk.run->events &= ~wait_flags;
elessair 0:f269e3021894 72 return (OS_R_EVT);
elessair 0:f269e3021894 73 }
elessair 0:f269e3021894 74 block_state = WAIT_OR;
elessair 0:f269e3021894 75 }
elessair 0:f269e3021894 76 /* Task has to wait */
elessair 0:f269e3021894 77 os_tsk.run->waits = wait_flags;
elessair 0:f269e3021894 78 rt_block (timeout, (U8)block_state);
elessair 0:f269e3021894 79 return (OS_R_TMO);
elessair 0:f269e3021894 80 }
elessair 0:f269e3021894 81
elessair 0:f269e3021894 82
elessair 0:f269e3021894 83 /*--------------------------- rt_evt_set ------------------------------------*/
elessair 0:f269e3021894 84
elessair 0:f269e3021894 85 void rt_evt_set (U16 event_flags, OS_TID task_id) {
elessair 0:f269e3021894 86 /* Set one or more event flags of a selectable task. */
elessair 0:f269e3021894 87 P_TCB p_tcb;
elessair 0:f269e3021894 88
elessair 0:f269e3021894 89 p_tcb = os_active_TCB[task_id-1];
elessair 0:f269e3021894 90 if (p_tcb == NULL) {
elessair 0:f269e3021894 91 return;
elessair 0:f269e3021894 92 }
elessair 0:f269e3021894 93 p_tcb->events |= event_flags;
elessair 0:f269e3021894 94 event_flags = p_tcb->waits;
elessair 0:f269e3021894 95 /* If the task is not waiting for an event, it should not be put */
elessair 0:f269e3021894 96 /* to ready state. */
elessair 0:f269e3021894 97 if (p_tcb->state == WAIT_AND) {
elessair 0:f269e3021894 98 /* Check for AND-connected events */
elessair 0:f269e3021894 99 if ((p_tcb->events & event_flags) == event_flags) {
elessair 0:f269e3021894 100 goto wkup;
elessair 0:f269e3021894 101 }
elessair 0:f269e3021894 102 }
elessair 0:f269e3021894 103 if (p_tcb->state == WAIT_OR) {
elessair 0:f269e3021894 104 /* Check for OR-connected events */
elessair 0:f269e3021894 105 if (p_tcb->events & event_flags) {
elessair 0:f269e3021894 106 p_tcb->waits &= p_tcb->events;
elessair 0:f269e3021894 107 wkup: p_tcb->events &= ~event_flags;
elessair 0:f269e3021894 108 rt_rmv_dly (p_tcb);
elessair 0:f269e3021894 109 p_tcb->state = READY;
elessair 0:f269e3021894 110 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 111 rt_ret_val2(p_tcb, 0x08/*osEventSignal*/, p_tcb->waits);
elessair 0:f269e3021894 112 #else
elessair 0:f269e3021894 113 rt_ret_val (p_tcb, OS_R_EVT);
elessair 0:f269e3021894 114 #endif
elessair 0:f269e3021894 115 rt_dispatch (p_tcb);
elessair 0:f269e3021894 116 }
elessair 0:f269e3021894 117 }
elessair 0:f269e3021894 118 }
elessair 0:f269e3021894 119
elessair 0:f269e3021894 120
elessair 0:f269e3021894 121 /*--------------------------- rt_evt_clr ------------------------------------*/
elessair 0:f269e3021894 122
elessair 0:f269e3021894 123 void rt_evt_clr (U16 clear_flags, OS_TID task_id) {
elessair 0:f269e3021894 124 /* Clear one or more event flags (identified by "clear_flags") of a */
elessair 0:f269e3021894 125 /* selectable task (identified by "task"). */
elessair 0:f269e3021894 126 P_TCB task = os_active_TCB[task_id-1];
elessair 0:f269e3021894 127
elessair 0:f269e3021894 128 if (task == NULL) {
elessair 0:f269e3021894 129 return;
elessair 0:f269e3021894 130 }
elessair 0:f269e3021894 131 task->events &= ~clear_flags;
elessair 0:f269e3021894 132 }
elessair 0:f269e3021894 133
elessair 0:f269e3021894 134
elessair 0:f269e3021894 135 /*--------------------------- isr_evt_set -----------------------------------*/
elessair 0:f269e3021894 136
elessair 0:f269e3021894 137 void isr_evt_set (U16 event_flags, OS_TID task_id) {
elessair 0:f269e3021894 138 /* Same function as "os_evt_set", but to be called by ISRs. */
elessair 0:f269e3021894 139 P_TCB p_tcb = os_active_TCB[task_id-1];
elessair 0:f269e3021894 140
elessair 0:f269e3021894 141 if (p_tcb == NULL) {
elessair 0:f269e3021894 142 return;
elessair 0:f269e3021894 143 }
elessair 0:f269e3021894 144 rt_psq_enq (p_tcb, event_flags);
elessair 0:f269e3021894 145 rt_psh_req ();
elessair 0:f269e3021894 146 }
elessair 0:f269e3021894 147
elessair 0:f269e3021894 148
elessair 0:f269e3021894 149 /*--------------------------- rt_evt_get ------------------------------------*/
elessair 0:f269e3021894 150
elessair 0:f269e3021894 151 U16 rt_evt_get (void) {
elessair 0:f269e3021894 152 /* Get events of a running task after waiting for OR connected events. */
elessair 0:f269e3021894 153 return (os_tsk.run->waits);
elessair 0:f269e3021894 154 }
elessair 0:f269e3021894 155
elessair 0:f269e3021894 156
elessair 0:f269e3021894 157 /*--------------------------- rt_evt_psh ------------------------------------*/
elessair 0:f269e3021894 158
elessair 0:f269e3021894 159 void rt_evt_psh (P_TCB p_CB, U16 set_flags) {
elessair 0:f269e3021894 160 /* Check if task has to be waken up */
elessair 0:f269e3021894 161 U16 event_flags;
elessair 0:f269e3021894 162
elessair 0:f269e3021894 163 p_CB->events |= set_flags;
elessair 0:f269e3021894 164 event_flags = p_CB->waits;
elessair 0:f269e3021894 165 if (p_CB->state == WAIT_AND) {
elessair 0:f269e3021894 166 /* Check for AND-connected events */
elessair 0:f269e3021894 167 if ((p_CB->events & event_flags) == event_flags) {
elessair 0:f269e3021894 168 goto rdy;
elessair 0:f269e3021894 169 }
elessair 0:f269e3021894 170 }
elessair 0:f269e3021894 171 if (p_CB->state == WAIT_OR) {
elessair 0:f269e3021894 172 /* Check for OR-connected events */
elessair 0:f269e3021894 173 if (p_CB->events & event_flags) {
elessair 0:f269e3021894 174 p_CB->waits &= p_CB->events;
elessair 0:f269e3021894 175 rdy: p_CB->events &= ~event_flags;
elessair 0:f269e3021894 176 rt_rmv_dly (p_CB);
elessair 0:f269e3021894 177 p_CB->state = READY;
elessair 0:f269e3021894 178 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 179 rt_ret_val2(p_CB, 0x08/*osEventSignal*/, p_CB->waits);
elessair 0:f269e3021894 180 #else
elessair 0:f269e3021894 181 rt_ret_val (p_CB, OS_R_EVT);
elessair 0:f269e3021894 182 #endif
elessair 0:f269e3021894 183 rt_put_prio (&os_rdy, p_CB);
elessair 0:f269e3021894 184 }
elessair 0:f269e3021894 185 }
elessair 0:f269e3021894 186 }
elessair 0:f269e3021894 187
elessair 0:f269e3021894 188 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 189 * end of file
elessair 0:f269e3021894 190 *---------------------------------------------------------------------------*/