Programme gyro_accelero avec acquisition accelero + calcul téta

Dependents:   Gyro_Accelerometre2

Committer:
SandrineO
Date:
Tue Feb 20 15:58:26 2018 +0000
Revision:
0:07281ea3b26b
temps r?el

Who changed what in which revision?

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