This repository has been archived on 2023-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
FreeRTOS-Kernel/portable/IAR/78K0R/port.c

227 lines
8.5 KiB
C
Raw Normal View History

2009-01-30 18:47:35 +08:00
/*
* FreeRTOS Kernel V10.3.1
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
2020-07-08 08:42:07 +08:00
* 1 tab == 4 spaces!
*/
2009-01-30 18:47:35 +08:00
/* Standard includes. */
#include <stdlib.h>
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
2009-02-05 00:53:52 +08:00
/* The critical nesting value is initialised to a non zero value to ensure
2020-07-02 13:27:40 +08:00
* interrupts don't accidentally become enabled before the scheduler is started. */
#define portINITIAL_CRITICAL_NESTING ( ( uint16_t ) 10 )
2009-01-30 18:47:35 +08:00
2009-02-05 00:53:52 +08:00
/* Initial PSW value allocated to a newly created task.
2009-01-30 18:47:35 +08:00
* 1100011000000000
* ||||||||-------------- Fill byte
2009-02-05 00:53:52 +08:00
* |||||||--------------- Carry Flag cleared
2009-01-30 18:47:35 +08:00
* |||||----------------- In-service priority Flags set to low level
* ||||------------------ Register bank Select 0 Flag cleared
2009-02-05 00:53:52 +08:00
* |||------------------- Auxiliary Carry Flag cleared
2009-01-30 18:47:35 +08:00
* ||-------------------- Register bank Select 1 Flag cleared
* |--------------------- Zero Flag set
2009-02-03 23:20:05 +08:00
* ---------------------- Global Interrupt Flag set (enabled)
2009-01-30 18:47:35 +08:00
*/
2020-07-02 13:27:40 +08:00
#define portPSW ( 0xc6UL )
2009-01-30 18:47:35 +08:00
/* We require the address of the pxCurrentTCB variable, but don't want to know
2020-07-02 13:27:40 +08:00
* any details of its type. */
typedef void TCB_t;
extern volatile TCB_t * volatile pxCurrentTCB;
2009-01-30 18:47:35 +08:00
/* Most ports implement critical sections by placing the interrupt flags on
2020-07-02 13:27:40 +08:00
* the stack before disabling interrupts. Exiting the critical section is then
* simply a case of popping the flags from the stack. As 78K0 IAR does not use
* a frame pointer this cannot be done as modifying the stack will clobber all
* the stack variables. Instead each task maintains a count of the critical
* section nesting depth. Each time a critical section is entered the count is
* incremented. Each time a critical section is left the count is decremented -
* with interrupts only being re-enabled if the count is zero.
*
* usCriticalNesting will get set to zero when the scheduler starts, but must
* not be initialised to zero as this will cause problems during the startup
* sequence. */
volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
2009-01-30 18:47:35 +08:00
/*-----------------------------------------------------------*/
/*
2009-02-04 05:09:16 +08:00
* Sets up the periodic ISR used for the RTOS tick.
2009-01-30 18:47:35 +08:00
*/
static void prvSetupTimerInterrupt( void );
/*-----------------------------------------------------------*/
2009-02-04 05:09:16 +08:00
/*
* Initialise the stack of a task to look exactly as if a call to
2009-01-30 18:47:35 +08:00
* portSAVE_CONTEXT had been called.
2009-02-04 05:09:16 +08:00
*
2009-01-30 18:47:35 +08:00
* See the header file portable.h.
*/
2020-07-02 13:27:40 +08:00
StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
TaskFunction_t pxCode,
void * pvParameters )
2009-02-04 05:09:16 +08:00
{
2020-07-02 13:27:40 +08:00
uint32_t * pulLocal;
#if configMEMORY_MODE == 1
{
/* Parameters are passed in on the stack, and written using a 32bit value
* hence a space is left for the second two bytes. */
pxTopOfStack--;
/* Write in the parameter value. */
2020-07-08 08:42:07 +08:00
pulLocal = ( uint32_t * ) pxTopOfStack;
*pulLocal = ( uint32_t ) pvParameters;
2020-07-02 13:27:40 +08:00
pxTopOfStack--;
/* These values are just spacers. The return address of the function
* would normally be written here. */
*pxTopOfStack = ( StackType_t ) 0xcdcd;
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) 0xcdcd;
pxTopOfStack--;
/* The start address / PSW value is also written in as a 32bit value,
* so leave a space for the second two bytes. */
pxTopOfStack--;
/* Task function start address combined with the PSW. */
2020-07-08 08:42:07 +08:00
pulLocal = ( uint32_t * ) pxTopOfStack;
*pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
2020-07-02 13:27:40 +08:00
pxTopOfStack--;
/* An initial value for the AX register. */
*pxTopOfStack = ( StackType_t ) 0x1111;
pxTopOfStack--;
}
2020-07-02 13:55:52 +08:00
#else /* if configMEMORY_MODE == 1 */
2020-07-02 13:27:40 +08:00
{
/* Task function address is written to the stack first. As it is
* written as a 32bit value a space is left on the stack for the second
* two bytes. */
pxTopOfStack--;
/* Task function start address combined with the PSW. */
2020-07-08 08:42:07 +08:00
pulLocal = ( uint32_t * ) pxTopOfStack;
*pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
2020-07-02 13:27:40 +08:00
pxTopOfStack--;
/* The parameter is passed in AX. */
*pxTopOfStack = ( StackType_t ) pvParameters;
pxTopOfStack--;
}
#endif /* if configMEMORY_MODE == 1 */
/* An initial value for the HL register. */
*pxTopOfStack = ( StackType_t ) 0x2222;
pxTopOfStack--;
/* CS and ES registers. */
*pxTopOfStack = ( StackType_t ) 0x0F00;
pxTopOfStack--;
/* Finally the remaining general purpose registers DE and BC */
*pxTopOfStack = ( StackType_t ) 0xDEDE;
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) 0xBCBC;
pxTopOfStack--;
/* Finally the critical section nesting count is set to zero when the task
* first starts. */
*pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
/* Return a pointer to the top of the stack we have generated so this can
* be stored in the task control block for the task. */
return pxTopOfStack;
2009-01-30 18:47:35 +08:00
}
/*-----------------------------------------------------------*/
BaseType_t xPortStartScheduler( void )
2009-01-30 18:47:35 +08:00
{
2020-07-02 13:27:40 +08:00
/* Setup the hardware to generate the tick. Interrupts are disabled when
* this function is called. */
prvSetupTimerInterrupt();
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Restore the context of the first task that is going to run. */
vPortStart();
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Should not get here as the tasks are now running! */
return pdTRUE;
2009-01-30 18:47:35 +08:00
}
/*-----------------------------------------------------------*/
void vPortEndScheduler( void )
{
2020-07-02 13:27:40 +08:00
/* It is unlikely that the 78K0R port will get stopped. If required simply
* disable the tick interrupt here. */
2009-01-30 18:47:35 +08:00
}
/*-----------------------------------------------------------*/
static void prvSetupTimerInterrupt( void )
{
2020-07-02 13:27:40 +08:00
/* Setup channel 5 of the TAU to generate the tick interrupt. */
2009-02-05 00:53:52 +08:00
2020-07-02 13:27:40 +08:00
/* First the Timer Array Unit has to be enabled. */
2020-07-08 08:42:07 +08:00
TAU0EN = 1;
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* To configure the Timer Array Unit all Channels have to first be stopped. */
2020-07-08 08:42:07 +08:00
TT0 = 0xff;
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Interrupt of Timer Array Unit Channel 5 is disabled to set the interrupt
* priority. */
2020-07-08 08:42:07 +08:00
TMMK05 = 1;
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Clear Timer Array Unit Channel 5 interrupt flag. */
2020-07-08 08:42:07 +08:00
TMIF05 = 0;
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Set Timer Array Unit Channel 5 interrupt priority */
TMPR005 = 0;
TMPR105 = 0;
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Set Timer Array Unit Channel 5 Mode as interval timer. */
2020-07-08 08:42:07 +08:00
TMR05 = 0x0000;
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Set the compare match value according to the tick rate we want. */
2020-07-08 08:42:07 +08:00
TDR05 = ( TickType_t ) ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Set Timer Array Unit Channel 5 output mode */
2020-07-08 08:42:07 +08:00
TOM0 &= ~0x0020;
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Set Timer Array Unit Channel 5 output level */
2020-07-08 08:42:07 +08:00
TOL0 &= ~0x0020;
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Set Timer Array Unit Channel 5 output enable */
2020-07-08 08:42:07 +08:00
TOE0 &= ~0x0020;
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Interrupt of Timer Array Unit Channel 5 enabled */
2020-07-08 08:42:07 +08:00
TMMK05 = 0;
2009-01-30 18:47:35 +08:00
2020-07-02 13:27:40 +08:00
/* Start Timer Array Unit Channel 5.*/
2020-07-08 08:42:07 +08:00
TS0 |= 0x0020;
2009-01-30 18:47:35 +08:00
}
/*-----------------------------------------------------------*/