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_MAILBOX.C
elessair 0:f269e3021894 5 * Purpose: Implements waits and wake-ups for mailbox messages
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_List.h"
elessair 0:f269e3021894 39 #include "rt_Mailbox.h"
elessair 0:f269e3021894 40 #include "rt_MemBox.h"
elessair 0:f269e3021894 41 #include "rt_Task.h"
elessair 0:f269e3021894 42 #include "rt_HAL_CM.h"
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 46 * Functions
elessair 0:f269e3021894 47 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49
elessair 0:f269e3021894 50 /*--------------------------- rt_mbx_init -----------------------------------*/
elessair 0:f269e3021894 51
elessair 0:f269e3021894 52 void rt_mbx_init (OS_ID mailbox, U16 mbx_size) {
elessair 0:f269e3021894 53 /* Initialize a mailbox */
elessair 0:f269e3021894 54 P_MCB p_MCB = mailbox;
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 p_MCB->cb_type = MCB;
elessair 0:f269e3021894 57 p_MCB->state = 0;
elessair 0:f269e3021894 58 p_MCB->isr_st = 0;
elessair 0:f269e3021894 59 p_MCB->p_lnk = NULL;
elessair 0:f269e3021894 60 p_MCB->first = 0;
elessair 0:f269e3021894 61 p_MCB->last = 0;
elessair 0:f269e3021894 62 p_MCB->count = 0;
elessair 0:f269e3021894 63 p_MCB->size = (mbx_size + sizeof(void *) - sizeof(struct OS_MCB)) /
elessair 0:f269e3021894 64 (U32)sizeof (void *);
elessair 0:f269e3021894 65 }
elessair 0:f269e3021894 66
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 /*--------------------------- rt_mbx_send -----------------------------------*/
elessair 0:f269e3021894 69
elessair 0:f269e3021894 70 OS_RESULT rt_mbx_send (OS_ID mailbox, void *p_msg, U16 timeout) {
elessair 0:f269e3021894 71 /* Send message to a mailbox */
elessair 0:f269e3021894 72 P_MCB p_MCB = mailbox;
elessair 0:f269e3021894 73 P_TCB p_TCB;
elessair 0:f269e3021894 74
elessair 0:f269e3021894 75 if ((p_MCB->p_lnk != NULL) && (p_MCB->state == 1)) {
elessair 0:f269e3021894 76 /* A task is waiting for message */
elessair 0:f269e3021894 77 p_TCB = rt_get_first ((P_XCB)p_MCB);
elessair 0:f269e3021894 78 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 79 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
elessair 0:f269e3021894 80 #else
elessair 0:f269e3021894 81 *p_TCB->msg = p_msg;
elessair 0:f269e3021894 82 rt_ret_val (p_TCB, OS_R_MBX);
elessair 0:f269e3021894 83 #endif
elessair 0:f269e3021894 84 rt_rmv_dly (p_TCB);
elessair 0:f269e3021894 85 rt_dispatch (p_TCB);
elessair 0:f269e3021894 86 }
elessair 0:f269e3021894 87 else {
elessair 0:f269e3021894 88 /* Store message in mailbox queue */
elessair 0:f269e3021894 89 if (p_MCB->count == p_MCB->size) {
elessair 0:f269e3021894 90 /* No free message entry, wait for one. If message queue is full, */
elessair 0:f269e3021894 91 /* then no task is waiting for message. The 'p_MCB->p_lnk' list */
elessair 0:f269e3021894 92 /* pointer can now be reused for send message waits task list. */
elessair 0:f269e3021894 93 if (timeout == 0) {
elessair 0:f269e3021894 94 return (OS_R_TMO);
elessair 0:f269e3021894 95 }
elessair 0:f269e3021894 96 if (p_MCB->p_lnk != NULL) {
elessair 0:f269e3021894 97 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
elessair 0:f269e3021894 98 }
elessair 0:f269e3021894 99 else {
elessair 0:f269e3021894 100 p_MCB->p_lnk = os_tsk.run;
elessair 0:f269e3021894 101 os_tsk.run->p_lnk = NULL;
elessair 0:f269e3021894 102 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
elessair 0:f269e3021894 103 /* Task is waiting to send a message */
elessair 0:f269e3021894 104 p_MCB->state = 2;
elessair 0:f269e3021894 105 }
elessair 0:f269e3021894 106 os_tsk.run->msg = p_msg;
elessair 0:f269e3021894 107 rt_block (timeout, WAIT_MBX);
elessair 0:f269e3021894 108 return (OS_R_TMO);
elessair 0:f269e3021894 109 }
elessair 0:f269e3021894 110 /* Yes, there is a free entry in a mailbox. */
elessair 0:f269e3021894 111 p_MCB->msg[p_MCB->first] = p_msg;
elessair 0:f269e3021894 112 rt_inc (&p_MCB->count);
elessair 0:f269e3021894 113 if (++p_MCB->first == p_MCB->size) {
elessair 0:f269e3021894 114 p_MCB->first = 0;
elessair 0:f269e3021894 115 }
elessair 0:f269e3021894 116 }
elessair 0:f269e3021894 117 return (OS_R_OK);
elessair 0:f269e3021894 118 }
elessair 0:f269e3021894 119
elessair 0:f269e3021894 120
elessair 0:f269e3021894 121 /*--------------------------- rt_mbx_wait -----------------------------------*/
elessair 0:f269e3021894 122
elessair 0:f269e3021894 123 OS_RESULT rt_mbx_wait (OS_ID mailbox, void **message, U16 timeout) {
elessair 0:f269e3021894 124 /* Receive a message; possibly wait for it */
elessair 0:f269e3021894 125 P_MCB p_MCB = mailbox;
elessair 0:f269e3021894 126 P_TCB p_TCB;
elessair 0:f269e3021894 127
elessair 0:f269e3021894 128 /* If a message is available in the fifo buffer */
elessair 0:f269e3021894 129 /* remove it from the fifo buffer and return. */
elessair 0:f269e3021894 130 if (p_MCB->count) {
elessair 0:f269e3021894 131 *message = p_MCB->msg[p_MCB->last];
elessair 0:f269e3021894 132 if (++p_MCB->last == p_MCB->size) {
elessair 0:f269e3021894 133 p_MCB->last = 0;
elessair 0:f269e3021894 134 }
elessair 0:f269e3021894 135 if ((p_MCB->p_lnk != NULL) && (p_MCB->state == 2)) {
elessair 0:f269e3021894 136 /* A task is waiting to send message */
elessair 0:f269e3021894 137 p_TCB = rt_get_first ((P_XCB)p_MCB);
elessair 0:f269e3021894 138 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 139 rt_ret_val(p_TCB, 0/*osOK*/);
elessair 0:f269e3021894 140 #else
elessair 0:f269e3021894 141 rt_ret_val(p_TCB, OS_R_OK);
elessair 0:f269e3021894 142 #endif
elessair 0:f269e3021894 143 p_MCB->msg[p_MCB->first] = p_TCB->msg;
elessair 0:f269e3021894 144 if (++p_MCB->first == p_MCB->size) {
elessair 0:f269e3021894 145 p_MCB->first = 0;
elessair 0:f269e3021894 146 }
elessair 0:f269e3021894 147 rt_rmv_dly (p_TCB);
elessair 0:f269e3021894 148 rt_dispatch (p_TCB);
elessair 0:f269e3021894 149 }
elessair 0:f269e3021894 150 else {
elessair 0:f269e3021894 151 rt_dec (&p_MCB->count);
elessair 0:f269e3021894 152 }
elessair 0:f269e3021894 153 return (OS_R_OK);
elessair 0:f269e3021894 154 }
elessair 0:f269e3021894 155 /* No message available: wait for one */
elessair 0:f269e3021894 156 if (timeout == 0) {
elessair 0:f269e3021894 157 return (OS_R_TMO);
elessair 0:f269e3021894 158 }
elessair 0:f269e3021894 159 if (p_MCB->p_lnk != NULL) {
elessair 0:f269e3021894 160 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
elessair 0:f269e3021894 161 }
elessair 0:f269e3021894 162 else {
elessair 0:f269e3021894 163 p_MCB->p_lnk = os_tsk.run;
elessair 0:f269e3021894 164 os_tsk.run->p_lnk = NULL;
elessair 0:f269e3021894 165 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
elessair 0:f269e3021894 166 /* Task is waiting to receive a message */
elessair 0:f269e3021894 167 p_MCB->state = 1;
elessair 0:f269e3021894 168 }
elessair 0:f269e3021894 169 rt_block(timeout, WAIT_MBX);
elessair 0:f269e3021894 170 #ifndef __CMSIS_RTOS
elessair 0:f269e3021894 171 os_tsk.run->msg = message;
elessair 0:f269e3021894 172 #endif
elessair 0:f269e3021894 173 return (OS_R_TMO);
elessair 0:f269e3021894 174 }
elessair 0:f269e3021894 175
elessair 0:f269e3021894 176
elessair 0:f269e3021894 177 /*--------------------------- rt_mbx_check ----------------------------------*/
elessair 0:f269e3021894 178
elessair 0:f269e3021894 179 OS_RESULT rt_mbx_check (OS_ID mailbox) {
elessair 0:f269e3021894 180 /* Check for free space in a mailbox. Returns the number of messages */
elessair 0:f269e3021894 181 /* that can be stored to a mailbox. It returns 0 when mailbox is full. */
elessair 0:f269e3021894 182 P_MCB p_MCB = mailbox;
elessair 0:f269e3021894 183
elessair 0:f269e3021894 184 return (p_MCB->size - p_MCB->count);
elessair 0:f269e3021894 185 }
elessair 0:f269e3021894 186
elessair 0:f269e3021894 187
elessair 0:f269e3021894 188 /*--------------------------- isr_mbx_send ----------------------------------*/
elessair 0:f269e3021894 189
elessair 0:f269e3021894 190 void isr_mbx_send (OS_ID mailbox, void *p_msg) {
elessair 0:f269e3021894 191 /* Same function as "os_mbx_send", but to be called by ISRs. */
elessair 0:f269e3021894 192 P_MCB p_MCB = mailbox;
elessair 0:f269e3021894 193
elessair 0:f269e3021894 194 rt_psq_enq (p_MCB, (U32)p_msg);
elessair 0:f269e3021894 195 rt_psh_req ();
elessair 0:f269e3021894 196 }
elessair 0:f269e3021894 197
elessair 0:f269e3021894 198
elessair 0:f269e3021894 199 /*--------------------------- isr_mbx_receive -------------------------------*/
elessair 0:f269e3021894 200
elessair 0:f269e3021894 201 OS_RESULT isr_mbx_receive (OS_ID mailbox, void **message) {
elessair 0:f269e3021894 202 /* Receive a message in the interrupt function. The interrupt function */
elessair 0:f269e3021894 203 /* should not wait for a message since this would block the rtx os. */
elessair 0:f269e3021894 204 P_MCB p_MCB = mailbox;
elessair 0:f269e3021894 205
elessair 0:f269e3021894 206 if (p_MCB->count) {
elessair 0:f269e3021894 207 /* A message is available in the fifo buffer. */
elessair 0:f269e3021894 208 *message = p_MCB->msg[p_MCB->last];
elessair 0:f269e3021894 209 if (p_MCB->state == 2) {
elessair 0:f269e3021894 210 /* A task is locked waiting to send message */
elessair 0:f269e3021894 211 rt_psq_enq (p_MCB, 0);
elessair 0:f269e3021894 212 rt_psh_req ();
elessair 0:f269e3021894 213 }
elessair 0:f269e3021894 214 rt_dec (&p_MCB->count);
elessair 0:f269e3021894 215 if (++p_MCB->last == p_MCB->size) {
elessair 0:f269e3021894 216 p_MCB->last = 0;
elessair 0:f269e3021894 217 }
elessair 0:f269e3021894 218 return (OS_R_MBX);
elessair 0:f269e3021894 219 }
elessair 0:f269e3021894 220 return (OS_R_OK);
elessair 0:f269e3021894 221 }
elessair 0:f269e3021894 222
elessair 0:f269e3021894 223
elessair 0:f269e3021894 224 /*--------------------------- rt_mbx_psh ------------------------------------*/
elessair 0:f269e3021894 225
elessair 0:f269e3021894 226 void rt_mbx_psh (P_MCB p_CB, void *p_msg) {
elessair 0:f269e3021894 227 /* Store the message to the mailbox queue or pass it to task directly. */
elessair 0:f269e3021894 228 P_TCB p_TCB;
elessair 0:f269e3021894 229 void *mem;
elessair 0:f269e3021894 230
elessair 0:f269e3021894 231 if (p_CB->p_lnk != NULL) switch (p_CB->state) {
elessair 0:f269e3021894 232 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 233 case 3:
elessair 0:f269e3021894 234 /* Task is waiting to allocate memory, remove it from the waiting list */
elessair 0:f269e3021894 235 mem = rt_alloc_box(p_msg);
elessair 0:f269e3021894 236 if (mem == NULL) break;
elessair 0:f269e3021894 237 p_TCB = rt_get_first ((P_XCB)p_CB);
elessair 0:f269e3021894 238 rt_ret_val(p_TCB, (U32)mem);
elessair 0:f269e3021894 239 p_TCB->state = READY;
elessair 0:f269e3021894 240 rt_rmv_dly (p_TCB);
elessair 0:f269e3021894 241 rt_put_prio (&os_rdy, p_TCB);
elessair 0:f269e3021894 242 break;
elessair 0:f269e3021894 243 #endif
elessair 0:f269e3021894 244 case 2:
elessair 0:f269e3021894 245 /* Task is waiting to send a message, remove it from the waiting list */
elessair 0:f269e3021894 246 p_TCB = rt_get_first ((P_XCB)p_CB);
elessair 0:f269e3021894 247 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 248 rt_ret_val(p_TCB, 0/*osOK*/);
elessair 0:f269e3021894 249 #else
elessair 0:f269e3021894 250 rt_ret_val(p_TCB, OS_R_OK);
elessair 0:f269e3021894 251 #endif
elessair 0:f269e3021894 252 p_CB->msg[p_CB->first] = p_TCB->msg;
elessair 0:f269e3021894 253 rt_inc (&p_CB->count);
elessair 0:f269e3021894 254 if (++p_CB->first == p_CB->size) {
elessair 0:f269e3021894 255 p_CB->first = 0;
elessair 0:f269e3021894 256 }
elessair 0:f269e3021894 257 p_TCB->state = READY;
elessair 0:f269e3021894 258 rt_rmv_dly (p_TCB);
elessair 0:f269e3021894 259 rt_put_prio (&os_rdy, p_TCB);
elessair 0:f269e3021894 260 break;
elessair 0:f269e3021894 261 case 1:
elessair 0:f269e3021894 262 /* Task is waiting for a message, pass the message to the task directly */
elessair 0:f269e3021894 263 p_TCB = rt_get_first ((P_XCB)p_CB);
elessair 0:f269e3021894 264 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 265 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
elessair 0:f269e3021894 266 #else
elessair 0:f269e3021894 267 *p_TCB->msg = p_msg;
elessair 0:f269e3021894 268 rt_ret_val (p_TCB, OS_R_MBX);
elessair 0:f269e3021894 269 #endif
elessair 0:f269e3021894 270 p_TCB->state = READY;
elessair 0:f269e3021894 271 rt_rmv_dly (p_TCB);
elessair 0:f269e3021894 272 rt_put_prio (&os_rdy, p_TCB);
elessair 0:f269e3021894 273 break;
elessair 0:f269e3021894 274 } else {
elessair 0:f269e3021894 275 /* No task is waiting for a message, store it to the mailbox queue */
elessair 0:f269e3021894 276 if (p_CB->count < p_CB->size) {
elessair 0:f269e3021894 277 p_CB->msg[p_CB->first] = p_msg;
elessair 0:f269e3021894 278 rt_inc (&p_CB->count);
elessair 0:f269e3021894 279 if (++p_CB->first == p_CB->size) {
elessair 0:f269e3021894 280 p_CB->first = 0;
elessair 0:f269e3021894 281 }
elessair 0:f269e3021894 282 }
elessair 0:f269e3021894 283 else {
elessair 0:f269e3021894 284 os_error (OS_ERR_MBX_OVF);
elessair 0:f269e3021894 285 }
elessair 0:f269e3021894 286 }
elessair 0:f269e3021894 287 }
elessair 0:f269e3021894 288
elessair 0:f269e3021894 289 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 290 * end of file
elessair 0:f269e3021894 291 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 292