pinebuds/rtos/rtx/TARGET_CORTEX_A/rt_System.c

295 lines
7.9 KiB
C
Raw Permalink Normal View History

2022-08-15 04:20:27 -05:00
/*----------------------------------------------------------------------------
* RL-ARM - RTX
*----------------------------------------------------------------------------
* Name: RT_SYSTEM.C
* Purpose: System Task Manager
* Rev.: V4.60
*----------------------------------------------------------------------------
*
* Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of ARM nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*---------------------------------------------------------------------------*/
#include "rt_System.h"
#include "RTX_Config.h"
2022-08-15 04:20:27 -05:00
#include "rt_Event.h"
#include "rt_List.h"
#include "rt_Mailbox.h"
#include "rt_Robin.h"
2022-08-15 04:20:27 -05:00
#include "rt_Semaphore.h"
#include "rt_Task.h"
2022-08-15 04:20:27 -05:00
#include "rt_Time.h"
#include "rt_Timer.h"
#include "rt_TypeDef.h"
2022-08-15 04:20:27 -05:00
#ifdef __CORTEX_A9
#include "rt_HAL_CA.h"
#else
#include "rt_HAL_CM.h"
#endif
/*----------------------------------------------------------------------------
* Global Variables
*---------------------------------------------------------------------------*/
int os_tick_irqn;
/*----------------------------------------------------------------------------
* Local Variables
*---------------------------------------------------------------------------*/
static volatile BIT os_lock;
static volatile BIT os_psh_flag;
#ifndef __CORTEX_A9
static U8 pend_flags;
2022-08-15 04:20:27 -05:00
#endif
/*----------------------------------------------------------------------------
* Global Functions
*---------------------------------------------------------------------------*/
#if defined(__CC_ARM)
__asm void $$RTX$$version(void) {
/* Export a version number symbol for a version control. */
2022-08-15 04:20:27 -05:00
EXPORT __RL_RTX_VER
2022-08-15 04:20:27 -05:00
__RL_RTX_VER EQU 0x450
2022-08-15 04:20:27 -05:00
}
#endif
/*--------------------------- rt_suspend ------------------------------------*/
U32 rt_suspend(void) {
2022-08-15 04:20:27 -05:00
/* Suspend OS scheduler */
U32 delta = 0xFFFF;
rt_tsk_lock();
if (os_dly.p_dlnk) {
delta = os_dly.delta_time;
}
#ifndef __CMSIS_RTOS
if (os_tmr.next) {
if (os_tmr.tcnt < delta)
delta = os_tmr.tcnt;
2022-08-15 04:20:27 -05:00
}
#endif
return (delta);
}
/*--------------------------- rt_resume -------------------------------------*/
void rt_resume(U32 sleep_time) {
2022-08-15 04:20:27 -05:00
/* Resume OS scheduler after suspend */
P_TCB next;
U32 delta;
2022-08-15 04:20:27 -05:00
os_tsk.run->state = READY;
rt_put_rdy_first(os_tsk.run);
2022-08-15 04:20:27 -05:00
os_robin.task = NULL;
/* Update delays. */
if (os_dly.p_dlnk) {
delta = sleep_time;
if (delta >= os_dly.delta_time) {
delta -= os_dly.delta_time;
2022-08-15 04:20:27 -05:00
os_time += os_dly.delta_time;
os_dly.delta_time = 1;
while (os_dly.p_dlnk) {
rt_dec_dly();
if (delta == 0)
break;
2022-08-15 04:20:27 -05:00
delta--;
os_time++;
}
} else {
os_time += delta;
2022-08-15 04:20:27 -05:00
os_dly.delta_time -= delta;
}
} else {
os_time += sleep_time;
}
#ifndef __CMSIS_RTOS
/* Check the user timers. */
if (os_tmr.next) {
delta = sleep_time;
if (delta >= os_tmr.tcnt) {
delta -= os_tmr.tcnt;
2022-08-15 04:20:27 -05:00
os_tmr.tcnt = 1;
while (os_tmr.next) {
rt_tmr_tick();
if (delta == 0)
break;
2022-08-15 04:20:27 -05:00
delta--;
}
} else {
os_tmr.tcnt -= delta;
}
}
#endif
/* Switch back to highest ready task */
next = rt_get_first(&os_rdy);
rt_switch_req(next);
2022-08-15 04:20:27 -05:00
rt_tsk_unlock();
}
/*--------------------------- rt_tsk_lock -----------------------------------*/
void rt_tsk_lock(void) {
2022-08-15 04:20:27 -05:00
/* Prevent task switching by locking out scheduler */
if (os_tick_irqn < 0) {
OS_LOCK();
os_lock = __TRUE;
OS_UNPEND(&pend_flags);
2022-08-15 04:20:27 -05:00
} else {
OS_X_LOCK(os_tick_irqn);
os_lock = __TRUE;
OS_X_UNPEND(&pend_flags);
2022-08-15 04:20:27 -05:00
}
}
/*--------------------------- rt_tsk_unlock ---------------------------------*/
void rt_tsk_unlock(void) {
2022-08-15 04:20:27 -05:00
/* Unlock scheduler and re-enable task switching */
if (os_tick_irqn < 0) {
OS_UNLOCK();
os_lock = __FALSE;
OS_PEND(pend_flags, os_psh_flag);
2022-08-15 04:20:27 -05:00
os_psh_flag = __FALSE;
} else {
OS_X_UNLOCK(os_tick_irqn);
os_lock = __FALSE;
OS_X_PEND(pend_flags, os_psh_flag);
2022-08-15 04:20:27 -05:00
os_psh_flag = __FALSE;
}
}
/*--------------------------- rt_psh_req ------------------------------------*/
void rt_psh_req(void) {
2022-08-15 04:20:27 -05:00
/* Initiate a post service handling request if required. */
if (os_lock == __FALSE) {
OS_PEND_IRQ();
} else {
2022-08-15 04:20:27 -05:00
os_psh_flag = __TRUE;
}
}
/*--------------------------- rt_pop_req ------------------------------------*/
void rt_pop_req(void) {
2022-08-15 04:20:27 -05:00
/* Process an ISR post service requests. */
struct OS_XCB *p_CB;
P_TCB next;
U32 idx;
2022-08-15 04:20:27 -05:00
os_tsk.run->state = READY;
rt_put_rdy_first(os_tsk.run);
2022-08-15 04:20:27 -05:00
idx = os_psq->last;
while (os_psq->count) {
p_CB = os_psq->q[idx].id;
if (p_CB->cb_type == TCB) {
/* Is of TCB type */
rt_evt_psh((P_TCB)p_CB, (U16)os_psq->q[idx].arg);
} else if (p_CB->cb_type == MCB) {
2022-08-15 04:20:27 -05:00
/* Is of MCB type */
rt_mbx_psh((P_MCB)p_CB, (void *)os_psq->q[idx].arg);
} else {
2022-08-15 04:20:27 -05:00
/* Must be of SCB type */
rt_sem_psh((P_SCB)p_CB);
2022-08-15 04:20:27 -05:00
}
if (++idx == os_psq->size)
idx = 0;
rt_dec(&os_psq->count);
2022-08-15 04:20:27 -05:00
}
os_psq->last = idx;
next = rt_get_first(&os_rdy);
rt_switch_req(next);
2022-08-15 04:20:27 -05:00
}
/*--------------------------- os_tick_init ----------------------------------*/
__weak int os_tick_init(void) {
2022-08-15 04:20:27 -05:00
/* Initialize SysTick timer as system tick timer. */
rt_systick_init();
return (-1); /* Return IRQ number of SysTick timer */
2022-08-15 04:20:27 -05:00
}
/*--------------------------- os_tick_irqack --------------------------------*/
__weak void os_tick_irqack(void) { /* Acknowledge timer interrupt. */
2022-08-15 04:20:27 -05:00
}
/*--------------------------- rt_systick ------------------------------------*/
extern void sysTimerTick(void);
void rt_systick(void) {
2022-08-15 04:20:27 -05:00
/* Check for system clock update, suspend running task. */
P_TCB next;
os_tsk.run->state = READY;
rt_put_rdy_first(os_tsk.run);
2022-08-15 04:20:27 -05:00
/* Check Round Robin timeout. */
rt_chk_robin();
2022-08-15 04:20:27 -05:00
/* Update delays. */
os_time++;
rt_dec_dly();
2022-08-15 04:20:27 -05:00
/* Check the user timers. */
#ifdef __CMSIS_RTOS
sysTimerTick();
#else
rt_tmr_tick();
2022-08-15 04:20:27 -05:00
#endif
/* Switch back to highest ready task */
next = rt_get_first(&os_rdy);
rt_switch_req(next);
2022-08-15 04:20:27 -05:00
}
/*--------------------------- rt_stk_check ----------------------------------*/
__weak void rt_stk_check(void) {
2022-08-15 04:20:27 -05:00
/* Check for stack overflow. */
if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) ||
(os_tsk.run->stack[0] != MAGIC_WORD)) {
os_error(OS_ERR_STK_OVF);
2022-08-15 04:20:27 -05:00
}
}
/*----------------------------------------------------------------------------
* end of file
*---------------------------------------------------------------------------*/