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_MUTEX.C
leo44 0:d984976f1f1c 5 * Purpose: Implements mutex synchronization objects
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_List.h"
leo44 0:d984976f1f1c 38 #include "rt_Task.h"
leo44 0:d984976f1f1c 39 #include "rt_Mutex.h"
leo44 0:d984976f1f1c 40 #include "rt_HAL_CM.h"
leo44 0:d984976f1f1c 41
leo44 0:d984976f1f1c 42
leo44 0:d984976f1f1c 43 /*----------------------------------------------------------------------------
leo44 0:d984976f1f1c 44 * Functions
leo44 0:d984976f1f1c 45 *---------------------------------------------------------------------------*/
leo44 0:d984976f1f1c 46
leo44 0:d984976f1f1c 47
leo44 0:d984976f1f1c 48 /*--------------------------- rt_mut_init -----------------------------------*/
leo44 0:d984976f1f1c 49
leo44 0:d984976f1f1c 50 void rt_mut_init (OS_ID mutex) {
leo44 0:d984976f1f1c 51 /* Initialize a mutex object */
leo44 0:d984976f1f1c 52 P_MUCB p_MCB = mutex;
leo44 0:d984976f1f1c 53
leo44 0:d984976f1f1c 54 p_MCB->cb_type = MUCB;
leo44 0:d984976f1f1c 55 p_MCB->prio = 0;
leo44 0:d984976f1f1c 56 p_MCB->level = 0;
leo44 0:d984976f1f1c 57 p_MCB->p_lnk = NULL;
leo44 0:d984976f1f1c 58 p_MCB->owner = NULL;
leo44 0:d984976f1f1c 59 }
leo44 0:d984976f1f1c 60
leo44 0:d984976f1f1c 61
leo44 0:d984976f1f1c 62 /*--------------------------- rt_mut_delete ---------------------------------*/
leo44 0:d984976f1f1c 63
leo44 0:d984976f1f1c 64 #ifdef __CMSIS_RTOS
leo44 0:d984976f1f1c 65 OS_RESULT rt_mut_delete (OS_ID mutex) {
leo44 0:d984976f1f1c 66 /* Delete a mutex object */
leo44 0:d984976f1f1c 67 P_MUCB p_MCB = mutex;
leo44 0:d984976f1f1c 68 P_TCB p_TCB;
leo44 0:d984976f1f1c 69
leo44 0:d984976f1f1c 70 /* Restore owner task's priority. */
leo44 0:d984976f1f1c 71 if (p_MCB->level != 0) {
leo44 0:d984976f1f1c 72 p_MCB->owner->prio = p_MCB->prio;
leo44 0:d984976f1f1c 73 if (p_MCB->owner != os_tsk.run) {
leo44 0:d984976f1f1c 74 rt_resort_prio (p_MCB->owner);
leo44 0:d984976f1f1c 75 }
leo44 0:d984976f1f1c 76 }
leo44 0:d984976f1f1c 77
leo44 0:d984976f1f1c 78 while (p_MCB->p_lnk != NULL) {
leo44 0:d984976f1f1c 79 /* A task is waiting for mutex. */
leo44 0:d984976f1f1c 80 p_TCB = rt_get_first ((P_XCB)p_MCB);
leo44 0:d984976f1f1c 81 rt_ret_val(p_TCB, 0/*osOK*/);
leo44 0:d984976f1f1c 82 rt_rmv_dly(p_TCB);
leo44 0:d984976f1f1c 83 p_TCB->state = READY;
leo44 0:d984976f1f1c 84 rt_put_prio (&os_rdy, p_TCB);
leo44 0:d984976f1f1c 85 }
leo44 0:d984976f1f1c 86
leo44 0:d984976f1f1c 87 if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
leo44 0:d984976f1f1c 88 /* preempt running task */
leo44 0:d984976f1f1c 89 rt_put_prio (&os_rdy, os_tsk.run);
leo44 0:d984976f1f1c 90 os_tsk.run->state = READY;
leo44 0:d984976f1f1c 91 rt_dispatch (NULL);
leo44 0:d984976f1f1c 92 }
leo44 0:d984976f1f1c 93
leo44 0:d984976f1f1c 94 p_MCB->cb_type = 0;
leo44 0:d984976f1f1c 95
leo44 0:d984976f1f1c 96 return (OS_R_OK);
leo44 0:d984976f1f1c 97 }
leo44 0:d984976f1f1c 98 #endif
leo44 0:d984976f1f1c 99
leo44 0:d984976f1f1c 100
leo44 0:d984976f1f1c 101 /*--------------------------- rt_mut_release --------------------------------*/
leo44 0:d984976f1f1c 102
leo44 0:d984976f1f1c 103 OS_RESULT rt_mut_release (OS_ID mutex) {
leo44 0:d984976f1f1c 104 /* Release a mutex object */
leo44 0:d984976f1f1c 105 P_MUCB p_MCB = mutex;
leo44 0:d984976f1f1c 106 P_TCB p_TCB;
leo44 0:d984976f1f1c 107
leo44 0:d984976f1f1c 108 if (p_MCB->level == 0 || p_MCB->owner != os_tsk.run) {
leo44 0:d984976f1f1c 109 /* Unbalanced mutex release or task is not the owner */
leo44 0:d984976f1f1c 110 return (OS_R_NOK);
leo44 0:d984976f1f1c 111 }
leo44 0:d984976f1f1c 112 if (--p_MCB->level != 0) {
leo44 0:d984976f1f1c 113 return (OS_R_OK);
leo44 0:d984976f1f1c 114 }
leo44 0:d984976f1f1c 115 /* Restore owner task's priority. */
leo44 0:d984976f1f1c 116 os_tsk.run->prio = p_MCB->prio;
leo44 0:d984976f1f1c 117 if (p_MCB->p_lnk != NULL) {
leo44 0:d984976f1f1c 118 /* A task is waiting for mutex. */
leo44 0:d984976f1f1c 119 p_TCB = rt_get_first ((P_XCB)p_MCB);
leo44 0:d984976f1f1c 120 #ifdef __CMSIS_RTOS
leo44 0:d984976f1f1c 121 rt_ret_val(p_TCB, 0/*osOK*/);
leo44 0:d984976f1f1c 122 #else
leo44 0:d984976f1f1c 123 rt_ret_val(p_TCB, OS_R_MUT);
leo44 0:d984976f1f1c 124 #endif
leo44 0:d984976f1f1c 125 rt_rmv_dly (p_TCB);
leo44 0:d984976f1f1c 126 /* A waiting task becomes the owner of this mutex. */
leo44 0:d984976f1f1c 127 p_MCB->level = 1;
leo44 0:d984976f1f1c 128 p_MCB->owner = p_TCB;
leo44 0:d984976f1f1c 129 p_MCB->prio = p_TCB->prio;
leo44 0:d984976f1f1c 130 /* Priority inversion, check which task continues. */
leo44 0:d984976f1f1c 131 if (os_tsk.run->prio >= rt_rdy_prio()) {
leo44 0:d984976f1f1c 132 rt_dispatch (p_TCB);
leo44 0:d984976f1f1c 133 }
leo44 0:d984976f1f1c 134 else {
leo44 0:d984976f1f1c 135 /* Ready task has higher priority than running task. */
leo44 0:d984976f1f1c 136 rt_put_prio (&os_rdy, os_tsk.run);
leo44 0:d984976f1f1c 137 rt_put_prio (&os_rdy, p_TCB);
leo44 0:d984976f1f1c 138 os_tsk.run->state = READY;
leo44 0:d984976f1f1c 139 p_TCB->state = READY;
leo44 0:d984976f1f1c 140 rt_dispatch (NULL);
leo44 0:d984976f1f1c 141 }
leo44 0:d984976f1f1c 142 }
leo44 0:d984976f1f1c 143 else {
leo44 0:d984976f1f1c 144 /* Check if own priority raised by priority inversion. */
leo44 0:d984976f1f1c 145 if (rt_rdy_prio() > os_tsk.run->prio) {
leo44 0:d984976f1f1c 146 rt_put_prio (&os_rdy, os_tsk.run);
leo44 0:d984976f1f1c 147 os_tsk.run->state = READY;
leo44 0:d984976f1f1c 148 rt_dispatch (NULL);
leo44 0:d984976f1f1c 149 }
leo44 0:d984976f1f1c 150 }
leo44 0:d984976f1f1c 151 return (OS_R_OK);
leo44 0:d984976f1f1c 152 }
leo44 0:d984976f1f1c 153
leo44 0:d984976f1f1c 154
leo44 0:d984976f1f1c 155 /*--------------------------- rt_mut_wait -----------------------------------*/
leo44 0:d984976f1f1c 156
leo44 0:d984976f1f1c 157 OS_RESULT rt_mut_wait (OS_ID mutex, U16 timeout) {
leo44 0:d984976f1f1c 158 /* Wait for a mutex, continue when mutex is free. */
leo44 0:d984976f1f1c 159 P_MUCB p_MCB = mutex;
leo44 0:d984976f1f1c 160
leo44 0:d984976f1f1c 161 if (p_MCB->level == 0) {
leo44 0:d984976f1f1c 162 p_MCB->owner = os_tsk.run;
leo44 0:d984976f1f1c 163 p_MCB->prio = os_tsk.run->prio;
leo44 0:d984976f1f1c 164 goto inc;
leo44 0:d984976f1f1c 165 }
leo44 0:d984976f1f1c 166 if (p_MCB->owner == os_tsk.run) {
leo44 0:d984976f1f1c 167 /* OK, running task is the owner of this mutex. */
leo44 0:d984976f1f1c 168 inc:p_MCB->level++;
leo44 0:d984976f1f1c 169 return (OS_R_OK);
leo44 0:d984976f1f1c 170 }
leo44 0:d984976f1f1c 171 /* Mutex owned by another task, wait until released. */
leo44 0:d984976f1f1c 172 if (timeout == 0) {
leo44 0:d984976f1f1c 173 return (OS_R_TMO);
leo44 0:d984976f1f1c 174 }
leo44 0:d984976f1f1c 175 /* Raise the owner task priority if lower than current priority. */
leo44 0:d984976f1f1c 176 /* This priority inversion is called priority inheritance. */
leo44 0:d984976f1f1c 177 if (p_MCB->prio < os_tsk.run->prio) {
leo44 0:d984976f1f1c 178 p_MCB->owner->prio = os_tsk.run->prio;
leo44 0:d984976f1f1c 179 rt_resort_prio (p_MCB->owner);
leo44 0:d984976f1f1c 180 }
leo44 0:d984976f1f1c 181 if (p_MCB->p_lnk != NULL) {
leo44 0:d984976f1f1c 182 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
leo44 0:d984976f1f1c 183 }
leo44 0:d984976f1f1c 184 else {
leo44 0:d984976f1f1c 185 p_MCB->p_lnk = os_tsk.run;
leo44 0:d984976f1f1c 186 os_tsk.run->p_lnk = NULL;
leo44 0:d984976f1f1c 187 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
leo44 0:d984976f1f1c 188 }
leo44 0:d984976f1f1c 189 rt_block(timeout, WAIT_MUT);
leo44 0:d984976f1f1c 190 return (OS_R_TMO);
leo44 0:d984976f1f1c 191 }
leo44 0:d984976f1f1c 192
leo44 0:d984976f1f1c 193
leo44 0:d984976f1f1c 194 /*----------------------------------------------------------------------------
leo44 0:d984976f1f1c 195 * end of file
leo44 0:d984976f1f1c 196 *---------------------------------------------------------------------------*/
leo44 0:d984976f1f1c 197