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_TASK.C
elessair 0:f269e3021894 5 * Purpose: Task functions and system start up.
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_Task.h"
elessair 0:f269e3021894 39 #include "rt_List.h"
elessair 0:f269e3021894 40 #include "rt_MemBox.h"
elessair 0:f269e3021894 41 #include "rt_Robin.h"
elessair 0:f269e3021894 42 #include "rt_HAL_CM.h"
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 45 * Global Variables
elessair 0:f269e3021894 46 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 /* Running and next task info. */
elessair 0:f269e3021894 49 struct OS_TSK os_tsk;
elessair 0:f269e3021894 50
elessair 0:f269e3021894 51 /* Task Control Blocks of idle demon */
elessair 0:f269e3021894 52 struct OS_TCB os_idle_TCB;
elessair 0:f269e3021894 53
elessair 0:f269e3021894 54
elessair 0:f269e3021894 55 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 56 * Local Functions
elessair 0:f269e3021894 57 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 58
elessair 0:f269e3021894 59 OS_TID rt_get_TID (void) {
elessair 0:f269e3021894 60 U32 tid;
elessair 0:f269e3021894 61
elessair 0:f269e3021894 62 for (tid = 1; tid <= os_maxtaskrun; tid++) {
elessair 0:f269e3021894 63 if (os_active_TCB[tid-1] == NULL) {
elessair 0:f269e3021894 64 return ((OS_TID)tid);
elessair 0:f269e3021894 65 }
elessair 0:f269e3021894 66 }
elessair 0:f269e3021894 67 return (0);
elessair 0:f269e3021894 68 }
elessair 0:f269e3021894 69
elessair 0:f269e3021894 70 #if defined (__CC_ARM) && !defined (__MICROLIB)
elessair 0:f269e3021894 71 /*--------------------------- __user_perthread_libspace ---------------------*/
elessair 0:f269e3021894 72 extern void *__libspace_start;
elessair 0:f269e3021894 73
elessair 0:f269e3021894 74 void *__user_perthread_libspace (void) {
elessair 0:f269e3021894 75 /* Provide a separate libspace for each task. */
elessair 0:f269e3021894 76 if (os_tsk.run == NULL) {
elessair 0:f269e3021894 77 /* RTX not running yet. */
elessair 0:f269e3021894 78 return (&__libspace_start);
elessair 0:f269e3021894 79 }
elessair 0:f269e3021894 80 return (void *)(os_tsk.run->std_libspace);
elessair 0:f269e3021894 81 }
elessair 0:f269e3021894 82 #endif
elessair 0:f269e3021894 83
elessair 0:f269e3021894 84 /*--------------------------- rt_init_context -------------------------------*/
elessair 0:f269e3021894 85
elessair 0:f269e3021894 86 void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body) {
elessair 0:f269e3021894 87 /* Initialize general part of the Task Control Block. */
elessair 0:f269e3021894 88 p_TCB->cb_type = TCB;
elessair 0:f269e3021894 89 p_TCB->state = READY;
elessair 0:f269e3021894 90 p_TCB->prio = priority;
elessair 0:f269e3021894 91 p_TCB->p_lnk = NULL;
elessair 0:f269e3021894 92 p_TCB->p_rlnk = NULL;
elessair 0:f269e3021894 93 p_TCB->p_dlnk = NULL;
elessair 0:f269e3021894 94 p_TCB->p_blnk = NULL;
elessair 0:f269e3021894 95 p_TCB->delta_time = 0;
elessair 0:f269e3021894 96 p_TCB->interval_time = 0;
elessair 0:f269e3021894 97 p_TCB->events = 0;
elessair 0:f269e3021894 98 p_TCB->waits = 0;
elessair 0:f269e3021894 99 p_TCB->stack_frame = 0;
elessair 0:f269e3021894 100
elessair 0:f269e3021894 101 rt_init_stack (p_TCB, task_body);
elessair 0:f269e3021894 102 }
elessair 0:f269e3021894 103
elessair 0:f269e3021894 104
elessair 0:f269e3021894 105 /*--------------------------- rt_switch_req ---------------------------------*/
elessair 0:f269e3021894 106
elessair 0:f269e3021894 107 void rt_switch_req (P_TCB p_new) {
elessair 0:f269e3021894 108 /* Switch to next task (identified by "p_new"). */
elessair 0:f269e3021894 109 os_tsk.new_tsk = p_new;
elessair 0:f269e3021894 110 p_new->state = RUNNING;
elessair 0:f269e3021894 111 DBG_TASK_SWITCH(p_new->task_id);
elessair 0:f269e3021894 112 }
elessair 0:f269e3021894 113
elessair 0:f269e3021894 114
elessair 0:f269e3021894 115 /*--------------------------- rt_dispatch -----------------------------------*/
elessair 0:f269e3021894 116
elessair 0:f269e3021894 117 void rt_dispatch (P_TCB next_TCB) {
elessair 0:f269e3021894 118 /* Dispatch next task if any identified or dispatch highest ready task */
elessair 0:f269e3021894 119 /* "next_TCB" identifies a task to run or has value NULL (=no next task) */
elessair 0:f269e3021894 120 if (next_TCB == NULL) {
elessair 0:f269e3021894 121 /* Running task was blocked: continue with highest ready task */
elessair 0:f269e3021894 122 next_TCB = rt_get_first (&os_rdy);
elessair 0:f269e3021894 123 rt_switch_req (next_TCB);
elessair 0:f269e3021894 124 }
elessair 0:f269e3021894 125 else {
elessair 0:f269e3021894 126 /* Check which task continues */
elessair 0:f269e3021894 127 if (next_TCB->prio > os_tsk.run->prio) {
elessair 0:f269e3021894 128 /* preempt running task */
elessair 0:f269e3021894 129 rt_put_rdy_first (os_tsk.run);
elessair 0:f269e3021894 130 os_tsk.run->state = READY;
elessair 0:f269e3021894 131 rt_switch_req (next_TCB);
elessair 0:f269e3021894 132 }
elessair 0:f269e3021894 133 else {
elessair 0:f269e3021894 134 /* put next task into ready list, no task switch takes place */
elessair 0:f269e3021894 135 next_TCB->state = READY;
elessair 0:f269e3021894 136 rt_put_prio (&os_rdy, next_TCB);
elessair 0:f269e3021894 137 }
elessair 0:f269e3021894 138 }
elessair 0:f269e3021894 139 }
elessair 0:f269e3021894 140
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 /*--------------------------- rt_block --------------------------------------*/
elessair 0:f269e3021894 143
elessair 0:f269e3021894 144 void rt_block (U16 timeout, U8 block_state) {
elessair 0:f269e3021894 145 /* Block running task and choose next ready task. */
elessair 0:f269e3021894 146 /* "timeout" sets a time-out value or is 0xffff (=no time-out). */
elessair 0:f269e3021894 147 /* "block_state" defines the appropriate task state */
elessair 0:f269e3021894 148 P_TCB next_TCB;
elessair 0:f269e3021894 149
elessair 0:f269e3021894 150 if (timeout) {
elessair 0:f269e3021894 151 if (timeout < 0xffff) {
elessair 0:f269e3021894 152 rt_put_dly (os_tsk.run, timeout);
elessair 0:f269e3021894 153 }
elessair 0:f269e3021894 154 os_tsk.run->state = block_state;
elessair 0:f269e3021894 155 next_TCB = rt_get_first (&os_rdy);
elessair 0:f269e3021894 156 rt_switch_req (next_TCB);
elessair 0:f269e3021894 157 }
elessair 0:f269e3021894 158 }
elessair 0:f269e3021894 159
elessair 0:f269e3021894 160
elessair 0:f269e3021894 161 /*--------------------------- rt_tsk_pass -----------------------------------*/
elessair 0:f269e3021894 162
elessair 0:f269e3021894 163 void rt_tsk_pass (void) {
elessair 0:f269e3021894 164 /* Allow tasks of same priority level to run cooperatively.*/
elessair 0:f269e3021894 165 P_TCB p_new;
elessair 0:f269e3021894 166
elessair 0:f269e3021894 167 p_new = rt_get_same_rdy_prio();
elessair 0:f269e3021894 168 if (p_new != NULL) {
elessair 0:f269e3021894 169 rt_put_prio ((P_XCB)&os_rdy, os_tsk.run);
elessair 0:f269e3021894 170 os_tsk.run->state = READY;
elessair 0:f269e3021894 171 rt_switch_req (p_new);
elessair 0:f269e3021894 172 }
elessair 0:f269e3021894 173 }
elessair 0:f269e3021894 174
elessair 0:f269e3021894 175
elessair 0:f269e3021894 176 /*--------------------------- rt_tsk_self -----------------------------------*/
elessair 0:f269e3021894 177
elessair 0:f269e3021894 178 OS_TID rt_tsk_self (void) {
elessair 0:f269e3021894 179 /* Return own task identifier value. */
elessair 0:f269e3021894 180 if (os_tsk.run == NULL) {
elessair 0:f269e3021894 181 return (0);
elessair 0:f269e3021894 182 }
elessair 0:f269e3021894 183 return (os_tsk.run->task_id);
elessair 0:f269e3021894 184 }
elessair 0:f269e3021894 185
elessair 0:f269e3021894 186
elessair 0:f269e3021894 187 /*--------------------------- rt_tsk_prio -----------------------------------*/
elessair 0:f269e3021894 188
elessair 0:f269e3021894 189 OS_RESULT rt_tsk_prio (OS_TID task_id, U8 new_prio) {
elessair 0:f269e3021894 190 /* Change execution priority of a task to "new_prio". */
elessair 0:f269e3021894 191 P_TCB p_task;
elessair 0:f269e3021894 192
elessair 0:f269e3021894 193 if (task_id == 0) {
elessair 0:f269e3021894 194 /* Change execution priority of calling task. */
elessair 0:f269e3021894 195 os_tsk.run->prio = new_prio;
elessair 0:f269e3021894 196 run:if (rt_rdy_prio() > new_prio) {
elessair 0:f269e3021894 197 rt_put_prio (&os_rdy, os_tsk.run);
elessair 0:f269e3021894 198 os_tsk.run->state = READY;
elessair 0:f269e3021894 199 rt_dispatch (NULL);
elessair 0:f269e3021894 200 }
elessair 0:f269e3021894 201 return (OS_R_OK);
elessair 0:f269e3021894 202 }
elessair 0:f269e3021894 203
elessair 0:f269e3021894 204 /* Find the task in the "os_active_TCB" array. */
elessair 0:f269e3021894 205 if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) {
elessair 0:f269e3021894 206 /* Task with "task_id" not found or not started. */
elessair 0:f269e3021894 207 return (OS_R_NOK);
elessair 0:f269e3021894 208 }
elessair 0:f269e3021894 209 p_task = os_active_TCB[task_id-1];
elessair 0:f269e3021894 210 p_task->prio = new_prio;
elessair 0:f269e3021894 211 if (p_task == os_tsk.run) {
elessair 0:f269e3021894 212 goto run;
elessair 0:f269e3021894 213 }
elessair 0:f269e3021894 214 rt_resort_prio (p_task);
elessair 0:f269e3021894 215 if (p_task->state == READY) {
elessair 0:f269e3021894 216 /* Task enqueued in a ready list. */
elessair 0:f269e3021894 217 p_task = rt_get_first (&os_rdy);
elessair 0:f269e3021894 218 rt_dispatch (p_task);
elessair 0:f269e3021894 219 }
elessair 0:f269e3021894 220 return (OS_R_OK);
elessair 0:f269e3021894 221 }
elessair 0:f269e3021894 222
elessair 0:f269e3021894 223 /*--------------------------- rt_tsk_delete ---------------------------------*/
elessair 0:f269e3021894 224
elessair 0:f269e3021894 225 OS_RESULT rt_tsk_delete (OS_TID task_id) {
elessair 0:f269e3021894 226 /* Terminate the task identified with "task_id". */
elessair 0:f269e3021894 227 P_TCB task_context;
elessair 0:f269e3021894 228
elessair 0:f269e3021894 229 if (task_id == 0 || task_id == os_tsk.run->task_id) {
elessair 0:f269e3021894 230 /* Terminate itself. */
elessair 0:f269e3021894 231 os_tsk.run->state = INACTIVE;
elessair 0:f269e3021894 232 os_tsk.run->tsk_stack = 0;
elessair 0:f269e3021894 233 rt_stk_check ();
elessair 0:f269e3021894 234 os_active_TCB[os_tsk.run->task_id-1] = NULL;
elessair 0:f269e3021894 235
elessair 0:f269e3021894 236 os_tsk.run->stack = NULL;
elessair 0:f269e3021894 237 DBG_TASK_NOTIFY(os_tsk.run, __FALSE);
elessair 0:f269e3021894 238 os_tsk.run = NULL;
elessair 0:f269e3021894 239 rt_dispatch (NULL);
elessair 0:f269e3021894 240 /* The program should never come to this point. */
elessair 0:f269e3021894 241 }
elessair 0:f269e3021894 242 else {
elessair 0:f269e3021894 243 /* Find the task in the "os_active_TCB" array. */
elessair 0:f269e3021894 244 if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) {
elessair 0:f269e3021894 245 /* Task with "task_id" not found or not started. */
elessair 0:f269e3021894 246 return (OS_R_NOK);
elessair 0:f269e3021894 247 }
elessair 0:f269e3021894 248 task_context = os_active_TCB[task_id-1];
elessair 0:f269e3021894 249 rt_rmv_list (task_context);
elessair 0:f269e3021894 250 rt_rmv_dly (task_context);
elessair 0:f269e3021894 251 os_active_TCB[task_id-1] = NULL;
elessair 0:f269e3021894 252
elessair 0:f269e3021894 253 task_context->stack = NULL;
elessair 0:f269e3021894 254 DBG_TASK_NOTIFY(task_context, __FALSE);
elessair 0:f269e3021894 255 }
elessair 0:f269e3021894 256 return (OS_R_OK);
elessair 0:f269e3021894 257 }
elessair 0:f269e3021894 258
elessair 0:f269e3021894 259
elessair 0:f269e3021894 260 /*--------------------------- rt_sys_init -----------------------------------*/
elessair 0:f269e3021894 261
elessair 0:f269e3021894 262 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 263 void rt_sys_init (void) {
elessair 0:f269e3021894 264 #else
elessair 0:f269e3021894 265 void rt_sys_init (FUNCP first_task, U32 prio_stksz, void *stk) {
elessair 0:f269e3021894 266 #endif
elessair 0:f269e3021894 267 /* Initialize system and start up task declared with "first_task". */
elessair 0:f269e3021894 268 U32 i;
elessair 0:f269e3021894 269
elessair 0:f269e3021894 270 DBG_INIT();
elessair 0:f269e3021894 271
elessair 0:f269e3021894 272 /* Initialize dynamic memory and task TCB pointers to NULL. */
elessair 0:f269e3021894 273 for (i = 0; i < os_maxtaskrun; i++) {
elessair 0:f269e3021894 274 os_active_TCB[i] = NULL;
elessair 0:f269e3021894 275 }
elessair 0:f269e3021894 276
elessair 0:f269e3021894 277 /* Set up TCB of idle demon */
elessair 0:f269e3021894 278 os_idle_TCB.task_id = 255;
elessair 0:f269e3021894 279 os_idle_TCB.priv_stack = idle_task_stack_size;
elessair 0:f269e3021894 280 os_idle_TCB.stack = idle_task_stack;
elessair 0:f269e3021894 281 rt_init_context (&os_idle_TCB, 0, os_idle_demon);
elessair 0:f269e3021894 282
elessair 0:f269e3021894 283 /* Set up ready list: initially empty */
elessair 0:f269e3021894 284 os_rdy.cb_type = HCB;
elessair 0:f269e3021894 285 os_rdy.p_lnk = NULL;
elessair 0:f269e3021894 286 /* Set up delay list: initially empty */
elessair 0:f269e3021894 287 os_dly.cb_type = HCB;
elessair 0:f269e3021894 288 os_dly.p_dlnk = NULL;
elessair 0:f269e3021894 289 os_dly.p_blnk = NULL;
elessair 0:f269e3021894 290 os_dly.delta_time = 0;
elessair 0:f269e3021894 291
elessair 0:f269e3021894 292 /* Fix SP and systemvariables to assume idle task is running */
elessair 0:f269e3021894 293 /* Transform main program into idle task by assuming idle TCB */
elessair 0:f269e3021894 294 #ifndef __CMSIS_RTOS
elessair 0:f269e3021894 295 rt_set_PSP (os_idle_TCB.tsk_stack);
elessair 0:f269e3021894 296 #endif
elessair 0:f269e3021894 297 os_tsk.run = &os_idle_TCB;
elessair 0:f269e3021894 298 os_tsk.run->state = RUNNING;
elessair 0:f269e3021894 299
elessair 0:f269e3021894 300 /* Initialize ps queue */
elessair 0:f269e3021894 301 os_psq->first = 0;
elessair 0:f269e3021894 302 os_psq->last = 0;
elessair 0:f269e3021894 303 os_psq->size = os_fifo_size;
elessair 0:f269e3021894 304
elessair 0:f269e3021894 305 rt_init_robin ();
elessair 0:f269e3021894 306
elessair 0:f269e3021894 307 /* Intitialize SVC and PendSV */
elessair 0:f269e3021894 308 rt_svc_init ();
elessair 0:f269e3021894 309
elessair 0:f269e3021894 310 #ifndef __CMSIS_RTOS
elessair 0:f269e3021894 311 /* Intitialize and start system clock timer */
elessair 0:f269e3021894 312 os_tick_irqn = os_tick_init ();
elessair 0:f269e3021894 313 if (os_tick_irqn >= 0) {
elessair 0:f269e3021894 314 OS_X_INIT(os_tick_irqn);
elessair 0:f269e3021894 315 }
elessair 0:f269e3021894 316
elessair 0:f269e3021894 317 /* Start up first user task before entering the endless loop */
elessair 0:f269e3021894 318 rt_tsk_create (first_task, prio_stksz, stk, NULL);
elessair 0:f269e3021894 319 #endif
elessair 0:f269e3021894 320 }
elessair 0:f269e3021894 321
elessair 0:f269e3021894 322
elessair 0:f269e3021894 323 /*--------------------------- rt_sys_start ----------------------------------*/
elessair 0:f269e3021894 324
elessair 0:f269e3021894 325 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 326 void rt_sys_start (void) {
elessair 0:f269e3021894 327 /* Start system */
elessair 0:f269e3021894 328
elessair 0:f269e3021894 329 /* Intitialize and start system clock timer */
elessair 0:f269e3021894 330 os_tick_irqn = os_tick_init ();
elessair 0:f269e3021894 331 if (os_tick_irqn >= 0) {
elessair 0:f269e3021894 332 OS_X_INIT(os_tick_irqn);
elessair 0:f269e3021894 333 }
elessair 0:f269e3021894 334 extern void RestoreContext();
elessair 0:f269e3021894 335 RestoreContext(); // Start the first task
elessair 0:f269e3021894 336 }
elessair 0:f269e3021894 337 #endif
elessair 0:f269e3021894 338
elessair 0:f269e3021894 339 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 340 * end of file
elessair 0:f269e3021894 341 *---------------------------------------------------------------------------*/