xref: /Kernel-v10.6.2/portable/GCC/RL78/port.c (revision ef7b253b56c9788077f5ecd6c9deb4021923d646)
1 /*
2  * FreeRTOS Kernel V10.6.2
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28 
29 /* Scheduler includes. */
30 #include "FreeRTOS.h"
31 #include "task.h"
32 
33 /* The critical nesting value is initialised to a non zero value to ensure
34 interrupts don't accidentally become enabled before the scheduler is started. */
35 #define portINITIAL_CRITICAL_NESTING  ( ( uint16_t ) 10 )
36 
37 /* Initial PSW value allocated to a newly created task.
38  *   11000110
39  *   ||||||||-------------- Fill byte
40  *   |||||||--------------- Carry Flag cleared
41  *   |||||----------------- In-service priority Flags set to low level
42  *   ||||------------------ Register bank Select 0 Flag cleared
43  *   |||------------------- Auxiliary Carry Flag cleared
44  *   ||-------------------- Register bank Select 1 Flag cleared
45  *   |--------------------- Zero Flag set
46  *   ---------------------- Global Interrupt Flag set (enabled)
47  */
48 #define portPSW       ( 0xc6UL )
49 
50 /* Each task maintains a count of the critical section nesting depth.  Each time
51 a critical section is entered the count is incremented.  Each time a critical
52 section is exited the count is decremented - with interrupts only being
53 re-enabled if the count is zero.
54 
55 usCriticalNesting will get set to zero when the scheduler starts, but must
56 not be initialised to zero as that could cause problems during the startup
57 sequence. */
58 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
59 
60 /*-----------------------------------------------------------*/
61 
62 /*
63  * Sets up the periodic ISR used for the RTOS tick.
64  */
65 __attribute__((weak)) void vApplicationSetupTimerInterrupt( void );
66 
67 /*
68  * Starts the scheduler by loading the context of the first task to run.
69  * (defined in portasm.S).
70  */
71 extern void vPortStartFirstTask( void );
72 
73 /*-----------------------------------------------------------*/
74 
75 /*
76  * Initialise the stack of a task to look exactly as if a call to
77  * portSAVE_CONTEXT had been called.
78  *
79  * See the header file portable.h.
80  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)81 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
82 {
83 uint32_t *pulLocal;
84 
85     /* Stack type and pointers to the stack type are both 2 bytes. */
86 
87     /* Parameters are passed in on the stack, and written using a 32bit value
88     hence a space is left for the second two bytes. */
89     pxTopOfStack--;
90 
91     /* Write in the parameter value. */
92     pulLocal =  ( uint32_t * ) pxTopOfStack;
93     *pulLocal = ( StackType_t ) pvParameters;
94     pxTopOfStack--;
95 
96     /* The return address, leaving space for the first two bytes of the
97     32-bit value. */
98     pxTopOfStack--;
99     pulLocal = ( uint32_t * ) pxTopOfStack;
100     *pulLocal = ( uint32_t ) 0;
101     pxTopOfStack--;
102 
103     /* The start address / PSW value is also written in as a 32bit value,
104     so leave a space for the second two bytes. */
105     pxTopOfStack--;
106 
107     /* Task function start address combined with the PSW. */
108     pulLocal = ( uint32_t * ) pxTopOfStack;
109     *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
110     pxTopOfStack--;
111 
112     /* An initial value for the AX register. */
113     *pxTopOfStack = ( StackType_t ) 0x1111;
114     pxTopOfStack--;
115 
116     /* An initial value for the HL register. */
117     *pxTopOfStack = ( StackType_t ) 0x2222;
118     pxTopOfStack--;
119 
120     /* CS and ES registers. */
121     *pxTopOfStack = ( StackType_t ) 0x0F00;
122     pxTopOfStack--;
123 
124     /* The remaining general purpose registers bank 0 (DE and BC) and the other
125     two register banks...register bank 3 is dedicated for use by interrupts so
126     is not saved as part of the task context. */
127     pxTopOfStack -= 10;
128 
129     /* Finally the critical section nesting count is set to zero when the task
130     first starts. */
131     *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
132 
133     /* Return a pointer to the top of the stack that has beene generated so it
134     can be stored in the task control block for the task. */
135     return pxTopOfStack;
136 }
137 /*-----------------------------------------------------------*/
138 
xPortStartScheduler(void)139 portBASE_TYPE xPortStartScheduler( void )
140 {
141     /* Setup the hardware to generate the tick.  Interrupts are disabled when
142     this function is called. */
143     vApplicationSetupTimerInterrupt();
144 
145     /* Restore the context of the first task that is going to run. */
146     vPortStartFirstTask();
147 
148     /* Execution should not reach here as the tasks are now running! */
149     return pdTRUE;
150 }
151 /*-----------------------------------------------------------*/
152 
vPortEndScheduler(void)153 void vPortEndScheduler( void )
154 {
155     /* It is unlikely that the RL78 port will get stopped. */
156 }
157 /*-----------------------------------------------------------*/
158 
vApplicationSetupTimerInterrupt(void)159 __attribute__((weak)) void vApplicationSetupTimerInterrupt( void )
160 {
161 const uint16_t usClockHz = 15000UL; /* Internal clock. */
162 const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;
163 
164     /* Use the internal 15K clock. */
165     OSMC = ( unsigned char ) 0x16;
166 
167     #ifdef RTCEN
168     {
169         /* Supply the interval timer clock. */
170         RTCEN = ( unsigned char ) 1U;
171 
172         /* Disable INTIT interrupt. */
173         ITMK = ( unsigned char ) 1;
174 
175         /* Disable ITMC operation. */
176         ITMC = ( unsigned char ) 0x0000;
177 
178         /* Clear INIT interrupt. */
179         ITIF = ( unsigned char ) 0;
180 
181         /* Set interval and enable interrupt operation. */
182         ITMC = usCompareMatch | 0x8000U;
183 
184         /* Enable INTIT interrupt. */
185         ITMK = ( unsigned char ) 0;
186     }
187     #endif
188 
189     #ifdef TMKAEN
190     {
191         /* Supply the interval timer clock. */
192         TMKAEN = ( unsigned char ) 1U;
193 
194         /* Disable INTIT interrupt. */
195         TMKAMK = ( unsigned char ) 1;
196 
197         /* Disable ITMC operation. */
198         ITMC = ( unsigned char ) 0x0000;
199 
200         /* Clear INIT interrupt. */
201         TMKAIF = ( unsigned char ) 0;
202 
203         /* Set interval and enable interrupt operation. */
204         ITMC = usCompareMatch | 0x8000U;
205 
206         /* Enable INTIT interrupt. */
207         TMKAMK = ( unsigned char ) 0;
208     }
209     #endif
210 }
211 /*-----------------------------------------------------------*/
212