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