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_SYSTEM.C
elessair 0:f269e3021894 5 * Purpose: System Task Manager
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_Task.h"
elessair 0:f269e3021894 38 #include "rt_System.h"
elessair 0:f269e3021894 39 #include "rt_Event.h"
elessair 0:f269e3021894 40 #include "rt_List.h"
elessair 0:f269e3021894 41 #include "rt_Mailbox.h"
elessair 0:f269e3021894 42 #include "rt_Semaphore.h"
elessair 0:f269e3021894 43 #include "rt_Time.h"
elessair 0:f269e3021894 44 #include "rt_Robin.h"
elessair 0:f269e3021894 45 #include "rt_HAL_CM.h"
elessair 0:f269e3021894 46
elessair 0:f269e3021894 47 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 48 * Global Variables
elessair 0:f269e3021894 49 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 50
elessair 0:f269e3021894 51 int os_tick_irqn;
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 54 * Local Variables
elessair 0:f269e3021894 55 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 56
elessair 0:f269e3021894 57 static volatile BIT os_lock;
elessair 0:f269e3021894 58 static volatile BIT os_psh_flag;
elessair 0:f269e3021894 59 static U8 pend_flags;
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 62 * Global Functions
elessair 0:f269e3021894 63 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 64
elessair 0:f269e3021894 65 #if defined (__CC_ARM)
elessair 0:f269e3021894 66 __asm void $$RTX$$version (void) {
elessair 0:f269e3021894 67 /* Export a version number symbol for a version control. */
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 EXPORT __RL_RTX_VER
elessair 0:f269e3021894 70
elessair 0:f269e3021894 71 __RL_RTX_VER EQU 0x450
elessair 0:f269e3021894 72 }
elessair 0:f269e3021894 73 #endif
elessair 0:f269e3021894 74
elessair 0:f269e3021894 75
elessair 0:f269e3021894 76 /*--------------------------- rt_suspend ------------------------------------*/
elessair 0:f269e3021894 77 U32 rt_suspend (void) {
elessair 0:f269e3021894 78 /* Suspend OS scheduler */
elessair 0:f269e3021894 79 U32 delta = 0xFFFF;
elessair 0:f269e3021894 80
elessair 0:f269e3021894 81 rt_tsk_lock();
elessair 0:f269e3021894 82
elessair 0:f269e3021894 83 if (os_dly.p_dlnk) {
elessair 0:f269e3021894 84 delta = os_dly.delta_time;
elessair 0:f269e3021894 85 }
elessair 0:f269e3021894 86 #ifndef __CMSIS_RTOS
elessair 0:f269e3021894 87 if (os_tmr.next) {
elessair 0:f269e3021894 88 if (os_tmr.tcnt < delta) delta = os_tmr.tcnt;
elessair 0:f269e3021894 89 }
elessair 0:f269e3021894 90 #endif
elessair 0:f269e3021894 91
elessair 0:f269e3021894 92 return (delta);
elessair 0:f269e3021894 93 }
elessair 0:f269e3021894 94
elessair 0:f269e3021894 95
elessair 0:f269e3021894 96 /*--------------------------- rt_resume -------------------------------------*/
elessair 0:f269e3021894 97 void rt_resume (U32 sleep_time) {
elessair 0:f269e3021894 98 /* Resume OS scheduler after suspend */
elessair 0:f269e3021894 99 P_TCB next;
elessair 0:f269e3021894 100 U32 delta;
elessair 0:f269e3021894 101
elessair 0:f269e3021894 102 os_tsk.run->state = READY;
elessair 0:f269e3021894 103 rt_put_rdy_first (os_tsk.run);
elessair 0:f269e3021894 104
elessair 0:f269e3021894 105 os_robin.task = NULL;
elessair 0:f269e3021894 106
elessair 0:f269e3021894 107 /* Update delays. */
elessair 0:f269e3021894 108 if (os_dly.p_dlnk) {
elessair 0:f269e3021894 109 delta = sleep_time;
elessair 0:f269e3021894 110 if (delta >= os_dly.delta_time) {
elessair 0:f269e3021894 111 delta -= os_dly.delta_time;
elessair 0:f269e3021894 112 os_time += os_dly.delta_time;
elessair 0:f269e3021894 113 os_dly.delta_time = 1;
elessair 0:f269e3021894 114 while (os_dly.p_dlnk) {
elessair 0:f269e3021894 115 rt_dec_dly();
elessair 0:f269e3021894 116 if (delta == 0) break;
elessair 0:f269e3021894 117 delta--;
elessair 0:f269e3021894 118 os_time++;
elessair 0:f269e3021894 119 }
elessair 0:f269e3021894 120 } else {
elessair 0:f269e3021894 121 os_time += delta;
elessair 0:f269e3021894 122 os_dly.delta_time -= delta;
elessair 0:f269e3021894 123 }
elessair 0:f269e3021894 124 } else {
elessair 0:f269e3021894 125 os_time += sleep_time;
elessair 0:f269e3021894 126 }
elessair 0:f269e3021894 127
elessair 0:f269e3021894 128 #ifndef __CMSIS_RTOS
elessair 0:f269e3021894 129 /* Check the user timers. */
elessair 0:f269e3021894 130 if (os_tmr.next) {
elessair 0:f269e3021894 131 delta = sleep_time;
elessair 0:f269e3021894 132 if (delta >= os_tmr.tcnt) {
elessair 0:f269e3021894 133 delta -= os_tmr.tcnt;
elessair 0:f269e3021894 134 os_tmr.tcnt = 1;
elessair 0:f269e3021894 135 while (os_tmr.next) {
elessair 0:f269e3021894 136 rt_tmr_tick();
elessair 0:f269e3021894 137 if (delta == 0) break;
elessair 0:f269e3021894 138 delta--;
elessair 0:f269e3021894 139 }
elessair 0:f269e3021894 140 } else {
elessair 0:f269e3021894 141 os_tmr.tcnt -= delta;
elessair 0:f269e3021894 142 }
elessair 0:f269e3021894 143 }
elessair 0:f269e3021894 144 #endif
elessair 0:f269e3021894 145
elessair 0:f269e3021894 146 /* Switch back to highest ready task */
elessair 0:f269e3021894 147 next = rt_get_first (&os_rdy);
elessair 0:f269e3021894 148 rt_switch_req (next);
elessair 0:f269e3021894 149
elessair 0:f269e3021894 150 rt_tsk_unlock();
elessair 0:f269e3021894 151 }
elessair 0:f269e3021894 152
elessair 0:f269e3021894 153
elessair 0:f269e3021894 154 /*--------------------------- rt_tsk_lock -----------------------------------*/
elessair 0:f269e3021894 155
elessair 0:f269e3021894 156 void rt_tsk_lock (void) {
elessair 0:f269e3021894 157 /* Prevent task switching by locking out scheduler */
elessair 0:f269e3021894 158 OS_X_LOCK(os_tick_irqn);
elessair 0:f269e3021894 159 os_lock = __TRUE;
elessair 0:f269e3021894 160 OS_X_UNPEND (&pend_flags);
elessair 0:f269e3021894 161 }
elessair 0:f269e3021894 162
elessair 0:f269e3021894 163
elessair 0:f269e3021894 164 /*--------------------------- rt_tsk_unlock ---------------------------------*/
elessair 0:f269e3021894 165
elessair 0:f269e3021894 166 void rt_tsk_unlock (void) {
elessair 0:f269e3021894 167 /* Unlock scheduler and re-enable task switching */
elessair 0:f269e3021894 168 OS_X_UNLOCK(os_tick_irqn);
elessair 0:f269e3021894 169 os_lock = __FALSE;
elessair 0:f269e3021894 170 OS_X_PEND (pend_flags, os_psh_flag);
elessair 0:f269e3021894 171 os_psh_flag = __FALSE;
elessair 0:f269e3021894 172 }
elessair 0:f269e3021894 173
elessair 0:f269e3021894 174
elessair 0:f269e3021894 175 /*--------------------------- rt_psh_req ------------------------------------*/
elessair 0:f269e3021894 176
elessair 0:f269e3021894 177 void rt_psh_req (void) {
elessair 0:f269e3021894 178 /* Initiate a post service handling request if required. */
elessair 0:f269e3021894 179 if (os_lock == __FALSE) {
elessair 0:f269e3021894 180 OS_PEND_IRQ ();
elessair 0:f269e3021894 181 }
elessair 0:f269e3021894 182 else {
elessair 0:f269e3021894 183 os_psh_flag = __TRUE;
elessair 0:f269e3021894 184 }
elessair 0:f269e3021894 185 }
elessair 0:f269e3021894 186
elessair 0:f269e3021894 187
elessair 0:f269e3021894 188 /*--------------------------- rt_pop_req ------------------------------------*/
elessair 0:f269e3021894 189
elessair 0:f269e3021894 190 void rt_pop_req (void) {
elessair 0:f269e3021894 191 /* Process an ISR post service requests. */
elessair 0:f269e3021894 192 struct OS_XCB *p_CB;
elessair 0:f269e3021894 193 P_TCB next;
elessair 0:f269e3021894 194 U32 idx;
elessair 0:f269e3021894 195
elessair 0:f269e3021894 196 os_tsk.run->state = READY;
elessair 0:f269e3021894 197 rt_put_rdy_first (os_tsk.run);
elessair 0:f269e3021894 198
elessair 0:f269e3021894 199 idx = os_psq->last;
elessair 0:f269e3021894 200 while (os_psq->count) {
elessair 0:f269e3021894 201 p_CB = os_psq->q[idx].id;
elessair 0:f269e3021894 202 if (p_CB->cb_type == TCB) {
elessair 0:f269e3021894 203 /* Is of TCB type */
elessair 0:f269e3021894 204 rt_evt_psh ((P_TCB)p_CB, (U16)os_psq->q[idx].arg);
elessair 0:f269e3021894 205 }
elessair 0:f269e3021894 206 else if (p_CB->cb_type == MCB) {
elessair 0:f269e3021894 207 /* Is of MCB type */
elessair 0:f269e3021894 208 rt_mbx_psh ((P_MCB)p_CB, (void *)os_psq->q[idx].arg);
elessair 0:f269e3021894 209 }
elessair 0:f269e3021894 210 else {
elessair 0:f269e3021894 211 /* Must be of SCB type */
elessair 0:f269e3021894 212 rt_sem_psh ((P_SCB)p_CB);
elessair 0:f269e3021894 213 }
elessair 0:f269e3021894 214 if (++idx == os_psq->size) idx = 0;
elessair 0:f269e3021894 215 rt_dec (&os_psq->count);
elessair 0:f269e3021894 216 }
elessair 0:f269e3021894 217 os_psq->last = idx;
elessair 0:f269e3021894 218
elessair 0:f269e3021894 219 next = rt_get_first (&os_rdy);
elessair 0:f269e3021894 220 rt_switch_req (next);
elessair 0:f269e3021894 221 }
elessair 0:f269e3021894 222
elessair 0:f269e3021894 223
elessair 0:f269e3021894 224 /*--------------------------- os_tick_init ----------------------------------*/
elessair 0:f269e3021894 225
elessair 0:f269e3021894 226 __weak int os_tick_init (void) {
elessair 0:f269e3021894 227 /* Initialize SysTick timer as system tick timer. */
elessair 0:f269e3021894 228 rt_systick_init ();
elessair 0:f269e3021894 229 return (SYS_TICK_IRQn); /* Return IRQ number of SysTick timer */
elessair 0:f269e3021894 230 }
elessair 0:f269e3021894 231
elessair 0:f269e3021894 232
elessair 0:f269e3021894 233 /*--------------------------- os_tick_irqack --------------------------------*/
elessair 0:f269e3021894 234
elessair 0:f269e3021894 235 __weak void os_tick_irqack (void) {
elessair 0:f269e3021894 236 /* Acknowledge timer interrupt. */
elessair 0:f269e3021894 237 }
elessair 0:f269e3021894 238
elessair 0:f269e3021894 239
elessair 0:f269e3021894 240 /*--------------------------- rt_systick ------------------------------------*/
elessair 0:f269e3021894 241
elessair 0:f269e3021894 242 extern void sysTimerTick(void);
elessair 0:f269e3021894 243
elessair 0:f269e3021894 244 void rt_systick (void) {
elessair 0:f269e3021894 245 if(NVIC_Pending(SYS_TICK_IRQn)){
elessair 0:f269e3021894 246 rt_pop_req();
elessair 0:f269e3021894 247 NVIC_UnpendIRQ(SYS_TICK_IRQn);
elessair 0:f269e3021894 248 SYS_TICK_TIMER->IR = 0xF; // clear timer interrupt
elessair 0:f269e3021894 249 return;
elessair 0:f269e3021894 250 }
elessair 0:f269e3021894 251 /* Check for system clock update, suspend running task. */
elessair 0:f269e3021894 252 P_TCB next;
elessair 0:f269e3021894 253
elessair 0:f269e3021894 254 os_tsk.run->state = READY;
elessair 0:f269e3021894 255 rt_put_rdy_first (os_tsk.run);
elessair 0:f269e3021894 256
elessair 0:f269e3021894 257 /* Check Round Robin timeout. */
elessair 0:f269e3021894 258 rt_chk_robin ();
elessair 0:f269e3021894 259
elessair 0:f269e3021894 260 /* Update delays. */
elessair 0:f269e3021894 261 os_time++;
elessair 0:f269e3021894 262 rt_dec_dly ();
elessair 0:f269e3021894 263
elessair 0:f269e3021894 264 /* Check the user timers. */
elessair 0:f269e3021894 265 #ifdef __CMSIS_RTOS
elessair 0:f269e3021894 266 sysTimerTick();
elessair 0:f269e3021894 267 #else
elessair 0:f269e3021894 268 rt_tmr_tick ();
elessair 0:f269e3021894 269 #endif
elessair 0:f269e3021894 270
elessair 0:f269e3021894 271 /* Switch back to highest ready task */
elessair 0:f269e3021894 272 next = rt_get_first (&os_rdy);
elessair 0:f269e3021894 273 rt_switch_req (next);
elessair 0:f269e3021894 274 SYS_TICK_TIMER->IR = 0xF; // clear timer interrupt
elessair 0:f269e3021894 275 }
elessair 0:f269e3021894 276
elessair 0:f269e3021894 277 /*--------------------------- rt_stk_check ----------------------------------*/
elessair 0:f269e3021894 278 __weak void rt_stk_check (void) {
elessair 0:f269e3021894 279 /* Check for stack overflow. */
elessair 0:f269e3021894 280 if (os_tsk.run->task_id == 0x01) {
elessair 0:f269e3021894 281 // TODO: For the main thread the check should be done against the main heap pointer
elessair 0:f269e3021894 282 } else {
elessair 0:f269e3021894 283 if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) ||
elessair 0:f269e3021894 284 (os_tsk.run->stack[0] != MAGIC_WORD)) {
elessair 0:f269e3021894 285 os_error (OS_ERR_STK_OVF);
elessair 0:f269e3021894 286 }
elessair 0:f269e3021894 287 }
elessair 0:f269e3021894 288 }
elessair 0:f269e3021894 289
elessair 0:f269e3021894 290 /*----------------------------------------------------------------------------
elessair 0:f269e3021894 291 * end of file
elessair 0:f269e3021894 292 *---------------------------------------------------------------------------*/
elessair 0:f269e3021894 293