NRF: receive, ntp, Data to Sd-card working

Dependencies:   F7_Ethernet mbed BSP_DISCO_F746NG Test_Mainboard SDFileSystem RF24

Committer:
lowlowry
Date:
Sun Jun 20 13:48:06 2021 +0000
Revision:
4:5ecb71f149bf
Parent:
0:d984976f1f1c
NRF: receive, ntp, Data to Sd-card working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leo44 0:d984976f1f1c 1 /*----------------------------------------------------------------------------
leo44 0:d984976f1f1c 2 * RL-ARM - RTX
leo44 0:d984976f1f1c 3 *----------------------------------------------------------------------------
leo44 0:d984976f1f1c 4 * Name: RT_SYSTEM.C
leo44 0:d984976f1f1c 5 * Purpose: System Task Manager
leo44 0:d984976f1f1c 6 * Rev.: V4.60
leo44 0:d984976f1f1c 7 *----------------------------------------------------------------------------
leo44 0:d984976f1f1c 8 *
leo44 0:d984976f1f1c 9 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
leo44 0:d984976f1f1c 10 * All rights reserved.
leo44 0:d984976f1f1c 11 * Redistribution and use in source and binary forms, with or without
leo44 0:d984976f1f1c 12 * modification, are permitted provided that the following conditions are met:
leo44 0:d984976f1f1c 13 * - Redistributions of source code must retain the above copyright
leo44 0:d984976f1f1c 14 * notice, this list of conditions and the following disclaimer.
leo44 0:d984976f1f1c 15 * - Redistributions in binary form must reproduce the above copyright
leo44 0:d984976f1f1c 16 * notice, this list of conditions and the following disclaimer in the
leo44 0:d984976f1f1c 17 * documentation and/or other materials provided with the distribution.
leo44 0:d984976f1f1c 18 * - Neither the name of ARM nor the names of its contributors may be used
leo44 0:d984976f1f1c 19 * to endorse or promote products derived from this software without
leo44 0:d984976f1f1c 20 * specific prior written permission.
leo44 0:d984976f1f1c 21 *
leo44 0:d984976f1f1c 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
leo44 0:d984976f1f1c 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
leo44 0:d984976f1f1c 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
leo44 0:d984976f1f1c 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
leo44 0:d984976f1f1c 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
leo44 0:d984976f1f1c 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
leo44 0:d984976f1f1c 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
leo44 0:d984976f1f1c 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
leo44 0:d984976f1f1c 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
leo44 0:d984976f1f1c 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
leo44 0:d984976f1f1c 32 * POSSIBILITY OF SUCH DAMAGE.
leo44 0:d984976f1f1c 33 *---------------------------------------------------------------------------*/
leo44 0:d984976f1f1c 34
leo44 0:d984976f1f1c 35 #include "rt_TypeDef.h"
leo44 0:d984976f1f1c 36 #include "RTX_Conf.h"
leo44 0:d984976f1f1c 37 #include "rt_Task.h"
leo44 0:d984976f1f1c 38 #include "rt_System.h"
leo44 0:d984976f1f1c 39 #include "rt_Event.h"
leo44 0:d984976f1f1c 40 #include "rt_List.h"
leo44 0:d984976f1f1c 41 #include "rt_Mailbox.h"
leo44 0:d984976f1f1c 42 #include "rt_Semaphore.h"
leo44 0:d984976f1f1c 43 #include "rt_Time.h"
leo44 0:d984976f1f1c 44 #include "rt_Robin.h"
leo44 0:d984976f1f1c 45 #include "rt_HAL_CM.h"
leo44 0:d984976f1f1c 46
leo44 0:d984976f1f1c 47 /*----------------------------------------------------------------------------
leo44 0:d984976f1f1c 48 * Global Variables
leo44 0:d984976f1f1c 49 *---------------------------------------------------------------------------*/
leo44 0:d984976f1f1c 50
leo44 0:d984976f1f1c 51 int os_tick_irqn;
leo44 0:d984976f1f1c 52
leo44 0:d984976f1f1c 53 /*----------------------------------------------------------------------------
leo44 0:d984976f1f1c 54 * Local Variables
leo44 0:d984976f1f1c 55 *---------------------------------------------------------------------------*/
leo44 0:d984976f1f1c 56
leo44 0:d984976f1f1c 57 static volatile BIT os_lock;
leo44 0:d984976f1f1c 58 static volatile BIT os_psh_flag;
leo44 0:d984976f1f1c 59 static U8 pend_flags;
leo44 0:d984976f1f1c 60
leo44 0:d984976f1f1c 61 /*----------------------------------------------------------------------------
leo44 0:d984976f1f1c 62 * Global Functions
leo44 0:d984976f1f1c 63 *---------------------------------------------------------------------------*/
leo44 0:d984976f1f1c 64
leo44 0:d984976f1f1c 65 #if defined (__CC_ARM)
leo44 0:d984976f1f1c 66 __asm void $$RTX$$version (void) {
leo44 0:d984976f1f1c 67 /* Export a version number symbol for a version control. */
leo44 0:d984976f1f1c 68
leo44 0:d984976f1f1c 69 EXPORT __RL_RTX_VER
leo44 0:d984976f1f1c 70
leo44 0:d984976f1f1c 71 __RL_RTX_VER EQU 0x450
leo44 0:d984976f1f1c 72 }
leo44 0:d984976f1f1c 73 #endif
leo44 0:d984976f1f1c 74
leo44 0:d984976f1f1c 75
leo44 0:d984976f1f1c 76 /*--------------------------- rt_suspend ------------------------------------*/
leo44 0:d984976f1f1c 77 U32 rt_suspend (void) {
leo44 0:d984976f1f1c 78 /* Suspend OS scheduler */
leo44 0:d984976f1f1c 79 U32 delta = 0xFFFF;
leo44 0:d984976f1f1c 80
leo44 0:d984976f1f1c 81 rt_tsk_lock();
leo44 0:d984976f1f1c 82
leo44 0:d984976f1f1c 83 if (os_dly.p_dlnk) {
leo44 0:d984976f1f1c 84 delta = os_dly.delta_time;
leo44 0:d984976f1f1c 85 }
leo44 0:d984976f1f1c 86 #ifndef __CMSIS_RTOS
leo44 0:d984976f1f1c 87 if (os_tmr.next) {
leo44 0:d984976f1f1c 88 if (os_tmr.tcnt < delta) delta = os_tmr.tcnt;
leo44 0:d984976f1f1c 89 }
leo44 0:d984976f1f1c 90 #endif
leo44 0:d984976f1f1c 91
leo44 0:d984976f1f1c 92 return (delta);
leo44 0:d984976f1f1c 93 }
leo44 0:d984976f1f1c 94
leo44 0:d984976f1f1c 95
leo44 0:d984976f1f1c 96 /*--------------------------- rt_resume -------------------------------------*/
leo44 0:d984976f1f1c 97 void rt_resume (U32 sleep_time) {
leo44 0:d984976f1f1c 98 /* Resume OS scheduler after suspend */
leo44 0:d984976f1f1c 99 P_TCB next;
leo44 0:d984976f1f1c 100 U32 delta;
leo44 0:d984976f1f1c 101
leo44 0:d984976f1f1c 102 os_tsk.run->state = READY;
leo44 0:d984976f1f1c 103 rt_put_rdy_first (os_tsk.run);
leo44 0:d984976f1f1c 104
leo44 0:d984976f1f1c 105 os_robin.task = NULL;
leo44 0:d984976f1f1c 106
leo44 0:d984976f1f1c 107 /* Update delays. */
leo44 0:d984976f1f1c 108 if (os_dly.p_dlnk) {
leo44 0:d984976f1f1c 109 delta = sleep_time;
leo44 0:d984976f1f1c 110 if (delta >= os_dly.delta_time) {
leo44 0:d984976f1f1c 111 delta -= os_dly.delta_time;
leo44 0:d984976f1f1c 112 os_time += os_dly.delta_time;
leo44 0:d984976f1f1c 113 os_dly.delta_time = 1;
leo44 0:d984976f1f1c 114 while (os_dly.p_dlnk) {
leo44 0:d984976f1f1c 115 rt_dec_dly();
leo44 0:d984976f1f1c 116 if (delta == 0) break;
leo44 0:d984976f1f1c 117 delta--;
leo44 0:d984976f1f1c 118 os_time++;
leo44 0:d984976f1f1c 119 }
leo44 0:d984976f1f1c 120 } else {
leo44 0:d984976f1f1c 121 os_time += delta;
leo44 0:d984976f1f1c 122 os_dly.delta_time -= delta;
leo44 0:d984976f1f1c 123 }
leo44 0:d984976f1f1c 124 } else {
leo44 0:d984976f1f1c 125 os_time += sleep_time;
leo44 0:d984976f1f1c 126 }
leo44 0:d984976f1f1c 127
leo44 0:d984976f1f1c 128 #ifndef __CMSIS_RTOS
leo44 0:d984976f1f1c 129 /* Check the user timers. */
leo44 0:d984976f1f1c 130 if (os_tmr.next) {
leo44 0:d984976f1f1c 131 delta = sleep_time;
leo44 0:d984976f1f1c 132 if (delta >= os_tmr.tcnt) {
leo44 0:d984976f1f1c 133 delta -= os_tmr.tcnt;
leo44 0:d984976f1f1c 134 os_tmr.tcnt = 1;
leo44 0:d984976f1f1c 135 while (os_tmr.next) {
leo44 0:d984976f1f1c 136 rt_tmr_tick();
leo44 0:d984976f1f1c 137 if (delta == 0) break;
leo44 0:d984976f1f1c 138 delta--;
leo44 0:d984976f1f1c 139 }
leo44 0:d984976f1f1c 140 } else {
leo44 0:d984976f1f1c 141 os_tmr.tcnt -= delta;
leo44 0:d984976f1f1c 142 }
leo44 0:d984976f1f1c 143 }
leo44 0:d984976f1f1c 144 #endif
leo44 0:d984976f1f1c 145
leo44 0:d984976f1f1c 146 /* Switch back to highest ready task */
leo44 0:d984976f1f1c 147 next = rt_get_first (&os_rdy);
leo44 0:d984976f1f1c 148 rt_switch_req (next);
leo44 0:d984976f1f1c 149
leo44 0:d984976f1f1c 150 rt_tsk_unlock();
leo44 0:d984976f1f1c 151 }
leo44 0:d984976f1f1c 152
leo44 0:d984976f1f1c 153
leo44 0:d984976f1f1c 154 /*--------------------------- rt_tsk_lock -----------------------------------*/
leo44 0:d984976f1f1c 155
leo44 0:d984976f1f1c 156 void rt_tsk_lock (void) {
leo44 0:d984976f1f1c 157 /* Prevent task switching by locking out scheduler */
leo44 0:d984976f1f1c 158 OS_X_LOCK(os_tick_irqn);
leo44 0:d984976f1f1c 159 os_lock = __TRUE;
leo44 0:d984976f1f1c 160 OS_X_UNPEND (&pend_flags);
leo44 0:d984976f1f1c 161 }
leo44 0:d984976f1f1c 162
leo44 0:d984976f1f1c 163
leo44 0:d984976f1f1c 164 /*--------------------------- rt_tsk_unlock ---------------------------------*/
leo44 0:d984976f1f1c 165
leo44 0:d984976f1f1c 166 void rt_tsk_unlock (void) {
leo44 0:d984976f1f1c 167 /* Unlock scheduler and re-enable task switching */
leo44 0:d984976f1f1c 168 OS_X_UNLOCK(os_tick_irqn);
leo44 0:d984976f1f1c 169 os_lock = __FALSE;
leo44 0:d984976f1f1c 170 OS_X_PEND (pend_flags, os_psh_flag);
leo44 0:d984976f1f1c 171 os_psh_flag = __FALSE;
leo44 0:d984976f1f1c 172 }
leo44 0:d984976f1f1c 173
leo44 0:d984976f1f1c 174
leo44 0:d984976f1f1c 175 /*--------------------------- rt_psh_req ------------------------------------*/
leo44 0:d984976f1f1c 176
leo44 0:d984976f1f1c 177 void rt_psh_req (void) {
leo44 0:d984976f1f1c 178 /* Initiate a post service handling request if required. */
leo44 0:d984976f1f1c 179 if (os_lock == __FALSE) {
leo44 0:d984976f1f1c 180 OS_PEND_IRQ ();
leo44 0:d984976f1f1c 181 }
leo44 0:d984976f1f1c 182 else {
leo44 0:d984976f1f1c 183 os_psh_flag = __TRUE;
leo44 0:d984976f1f1c 184 }
leo44 0:d984976f1f1c 185 }
leo44 0:d984976f1f1c 186
leo44 0:d984976f1f1c 187
leo44 0:d984976f1f1c 188 /*--------------------------- rt_pop_req ------------------------------------*/
leo44 0:d984976f1f1c 189
leo44 0:d984976f1f1c 190 void rt_pop_req (void) {
leo44 0:d984976f1f1c 191 /* Process an ISR post service requests. */
leo44 0:d984976f1f1c 192 struct OS_XCB *p_CB;
leo44 0:d984976f1f1c 193 P_TCB next;
leo44 0:d984976f1f1c 194 U32 idx;
leo44 0:d984976f1f1c 195
leo44 0:d984976f1f1c 196 os_tsk.run->state = READY;
leo44 0:d984976f1f1c 197 rt_put_rdy_first (os_tsk.run);
leo44 0:d984976f1f1c 198
leo44 0:d984976f1f1c 199 idx = os_psq->last;
leo44 0:d984976f1f1c 200 while (os_psq->count) {
leo44 0:d984976f1f1c 201 p_CB = os_psq->q[idx].id;
leo44 0:d984976f1f1c 202 if (p_CB->cb_type == TCB) {
leo44 0:d984976f1f1c 203 /* Is of TCB type */
leo44 0:d984976f1f1c 204 rt_evt_psh ((P_TCB)p_CB, (U16)os_psq->q[idx].arg);
leo44 0:d984976f1f1c 205 }
leo44 0:d984976f1f1c 206 else if (p_CB->cb_type == MCB) {
leo44 0:d984976f1f1c 207 /* Is of MCB type */
leo44 0:d984976f1f1c 208 rt_mbx_psh ((P_MCB)p_CB, (void *)os_psq->q[idx].arg);
leo44 0:d984976f1f1c 209 }
leo44 0:d984976f1f1c 210 else {
leo44 0:d984976f1f1c 211 /* Must be of SCB type */
leo44 0:d984976f1f1c 212 rt_sem_psh ((P_SCB)p_CB);
leo44 0:d984976f1f1c 213 }
leo44 0:d984976f1f1c 214 if (++idx == os_psq->size) idx = 0;
leo44 0:d984976f1f1c 215 rt_dec (&os_psq->count);
leo44 0:d984976f1f1c 216 }
leo44 0:d984976f1f1c 217 os_psq->last = idx;
leo44 0:d984976f1f1c 218
leo44 0:d984976f1f1c 219 next = rt_get_first (&os_rdy);
leo44 0:d984976f1f1c 220 rt_switch_req (next);
leo44 0:d984976f1f1c 221 }
leo44 0:d984976f1f1c 222
leo44 0:d984976f1f1c 223
leo44 0:d984976f1f1c 224 /*--------------------------- os_tick_init ----------------------------------*/
leo44 0:d984976f1f1c 225
leo44 0:d984976f1f1c 226 __weak int os_tick_init (void) {
leo44 0:d984976f1f1c 227 /* Initialize SysTick timer as system tick timer. */
leo44 0:d984976f1f1c 228 rt_systick_init ();
leo44 0:d984976f1f1c 229 return (SYS_TICK_IRQn); /* Return IRQ number of SysTick timer */
leo44 0:d984976f1f1c 230 }
leo44 0:d984976f1f1c 231
leo44 0:d984976f1f1c 232
leo44 0:d984976f1f1c 233 /*--------------------------- os_tick_irqack --------------------------------*/
leo44 0:d984976f1f1c 234
leo44 0:d984976f1f1c 235 __weak void os_tick_irqack (void) {
leo44 0:d984976f1f1c 236 /* Acknowledge timer interrupt. */
leo44 0:d984976f1f1c 237 }
leo44 0:d984976f1f1c 238
leo44 0:d984976f1f1c 239
leo44 0:d984976f1f1c 240 /*--------------------------- rt_systick ------------------------------------*/
leo44 0:d984976f1f1c 241
leo44 0:d984976f1f1c 242 extern void sysTimerTick(void);
leo44 0:d984976f1f1c 243
leo44 0:d984976f1f1c 244 void rt_systick (void) {
leo44 0:d984976f1f1c 245 if(NVIC_Pending(SYS_TICK_IRQn)){
leo44 0:d984976f1f1c 246 rt_pop_req();
leo44 0:d984976f1f1c 247 NVIC_UnpendIRQ(SYS_TICK_IRQn);
leo44 0:d984976f1f1c 248 SYS_TICK_TIMER->IR = 0xF; // clear timer interrupt
leo44 0:d984976f1f1c 249 return;
leo44 0:d984976f1f1c 250 }
leo44 0:d984976f1f1c 251 /* Check for system clock update, suspend running task. */
leo44 0:d984976f1f1c 252 P_TCB next;
leo44 0:d984976f1f1c 253
leo44 0:d984976f1f1c 254 os_tsk.run->state = READY;
leo44 0:d984976f1f1c 255 rt_put_rdy_first (os_tsk.run);
leo44 0:d984976f1f1c 256
leo44 0:d984976f1f1c 257 /* Check Round Robin timeout. */
leo44 0:d984976f1f1c 258 rt_chk_robin ();
leo44 0:d984976f1f1c 259
leo44 0:d984976f1f1c 260 /* Update delays. */
leo44 0:d984976f1f1c 261 os_time++;
leo44 0:d984976f1f1c 262 rt_dec_dly ();
leo44 0:d984976f1f1c 263
leo44 0:d984976f1f1c 264 /* Check the user timers. */
leo44 0:d984976f1f1c 265 #ifdef __CMSIS_RTOS
leo44 0:d984976f1f1c 266 sysTimerTick();
leo44 0:d984976f1f1c 267 #else
leo44 0:d984976f1f1c 268 rt_tmr_tick ();
leo44 0:d984976f1f1c 269 #endif
leo44 0:d984976f1f1c 270
leo44 0:d984976f1f1c 271 /* Switch back to highest ready task */
leo44 0:d984976f1f1c 272 next = rt_get_first (&os_rdy);
leo44 0:d984976f1f1c 273 rt_switch_req (next);
leo44 0:d984976f1f1c 274 SYS_TICK_TIMER->IR = 0xF; // clear timer interrupt
leo44 0:d984976f1f1c 275 }
leo44 0:d984976f1f1c 276
leo44 0:d984976f1f1c 277 /*--------------------------- rt_stk_check ----------------------------------*/
leo44 0:d984976f1f1c 278 __weak void rt_stk_check (void) {
leo44 0:d984976f1f1c 279 /* Check for stack overflow. */
leo44 0:d984976f1f1c 280 if (os_tsk.run->task_id == 0x01) {
leo44 0:d984976f1f1c 281 // TODO: For the main thread the check should be done against the main heap pointer
leo44 0:d984976f1f1c 282 } else {
leo44 0:d984976f1f1c 283 if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) ||
leo44 0:d984976f1f1c 284 (os_tsk.run->stack[0] != MAGIC_WORD)) {
leo44 0:d984976f1f1c 285 os_error (OS_ERR_STK_OVF);
leo44 0:d984976f1f1c 286 }
leo44 0:d984976f1f1c 287 }
leo44 0:d984976f1f1c 288 }
leo44 0:d984976f1f1c 289
leo44 0:d984976f1f1c 290 /*----------------------------------------------------------------------------
leo44 0:d984976f1f1c 291 * end of file
leo44 0:d984976f1f1c 292 *---------------------------------------------------------------------------*/
leo44 0:d984976f1f1c 293