]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R4F_RZ_T_GCC_IAR/src/FreeRTOS_tick_config.c
c250aa33a939444b1a022bae6b23eb9158b2dd34
[freertos] / FreeRTOS / Demo / CORTEX_R4F_RZ_T_GCC_IAR / src / FreeRTOS_tick_config.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 /* FreeRTOS includes. */\r
29 #include "FreeRTOS.h"\r
30 \r
31 /* Renesas includes. */\r
32 #include "r_cg_macrodriver.h"\r
33 #include "r_cg_cmt.h"\r
34 #include "r_reset.h"\r
35 \r
36 /*-----------------------------------------------------------*/\r
37 \r
38 /*\r
39  * Entry point for the FreeRTOS tick interrupt.  This sets the pxISRFunction\r
40  * variable to point to the RTOS tick handler, then branches to the FreeRTOS\r
41  * IRQ handler.\r
42  */\r
43 #ifdef __GNUC__\r
44         static void FreeRTOS_Tick_Handler_Entry( void ) __attribute__((naked));\r
45 #endif /* __GNUC__ */\r
46 #ifdef __ICCARM__\r
47         /* IAR requires the entry point to be in an assembly file.  The function is\r
48         implemented in $PROJ_DIR$/System/IAR/Interrupt_Entry_Stubs.asm. */\r
49         extern void FreeRTOS_Tick_Handler_Entry( void );\r
50 #endif /* __ICCARM__ */\r
51 \r
52 /*\r
53  * The FreeRTOS IRQ handler, which is implemented in the RTOS port layer.\r
54  */\r
55 extern void FreeRTOS_IRQ_Handler( void );\r
56 \r
57 /*\r
58  * The function called by the FreeRTOS_IRQ_Handler() to call the actual\r
59  * peripheral handler.\r
60  */\r
61 void vApplicationIRQHandler( void );\r
62 \r
63 /*-----------------------------------------------------------*/\r
64 \r
65 /*\r
66  * Variable used to hold the address of the interrupt handler the FreeRTOS IRQ\r
67  * handler will branch to.\r
68  */\r
69 ISRFunction_t pxISRFunction = NULL;\r
70 \r
71 /*-----------------------------------------------------------*/\r
72 \r
73 /*\r
74  * The application must provide a function that configures a peripheral to\r
75  * create the FreeRTOS tick interrupt, then define configSETUP_TICK_INTERRUPT()\r
76  * in FreeRTOSConfig.h to call the function.\r
77  */\r
78 void vConfigureTickInterrupt( void )\r
79 {\r
80 uint32_t ulCompareMatchValue;\r
81 const uint32_t ulPeripheralClockDivider = 6UL, ulCMTClockDivider = 8UL;\r
82 \r
83         /* Disable CMI5 interrupt. */\r
84         VIC.IEC9.LONG = 0x00001000UL;\r
85 \r
86         /* Cancel CMT stop state in LPC. */\r
87         r_rst_write_enable();\r
88         MSTP( CMT2 ) = 0U;\r
89         r_rst_write_disable();\r
90 \r
91         /* Interrupt on compare match. */\r
92         CMT5.CMCR.BIT.CMIE = 1;\r
93 \r
94         /* Calculate the compare match value. */\r
95         ulCompareMatchValue = configCPU_CLOCK_HZ / ulPeripheralClockDivider;\r
96         ulCompareMatchValue /= ulCMTClockDivider;\r
97         ulCompareMatchValue /= configTICK_RATE_HZ;\r
98         ulCompareMatchValue -= 1UL;\r
99 \r
100         /* Set the compare match value. */\r
101         CMT5.CMCOR = ( unsigned short ) ulCompareMatchValue;\r
102 \r
103         /* Divide the PCLK by 8. */\r
104         CMT5.CMCR.BIT.CKS = 0;\r
105 \r
106         CMT5.CMCNT = 0;\r
107 \r
108         /* Set CMI5 edge detection type. */\r
109         VIC.PLS9.LONG |= 0x00001000UL;\r
110 \r
111         /* Set CMI5 priority level to the lowest possible. */\r
112         VIC.PRL300.LONG = _CMT_PRIORITY_LEVEL31;\r
113 \r
114         /* Set CMI5 interrupt address */\r
115         VIC.VAD300.LONG = ( uint32_t ) FreeRTOS_Tick_Handler_Entry;\r
116 \r
117         /* Enable CMI5 interrupt in ICU. */\r
118         VIC.IEN9.LONG |= 0x00001000UL;\r
119 \r
120         /* Start CMT5 count. */\r
121         CMT.CMSTR2.BIT.STR5 = 1U;\r
122 }\r
123 /*-----------------------------------------------------------*/\r
124 \r
125 /*\r
126  * The function called by the FreeRTOS IRQ handler, after it has managed\r
127  * interrupt entry.  This function creates a local copy of pxISRFunction before\r
128  * re-enabling interrupts and actually calling the handler pointed to by\r
129  * pxISRFunction.\r
130  */\r
131 void vApplicationIRQHandler( void )\r
132 {\r
133 ISRFunction_t pxISRToCall = pxISRFunction;\r
134 \r
135         portENABLE_INTERRUPTS();\r
136 \r
137         /* Call the installed ISR. */\r
138         pxISRToCall();\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 /*\r
143  * The RZ/T vectors directly to a peripheral specific interrupt handler, rather\r
144  * than using the Cortex-R IRQ vector.  Therefore each interrupt handler\r
145  * installed by the application must follow the example below, which saves a\r
146  * pointer to a standard C function in the pxISRFunction variable, before\r
147  * branching to the FreeRTOS IRQ handler.  The FreeRTOS IRQ handler then manages\r
148  * interrupt entry (including interrupt nesting), before calling the C function\r
149  * saved in the pxISRFunction variable.  NOTE:  This entry point is a naked\r
150  * function - do not add C code to this function.\r
151  */\r
152 #ifdef __GNUC__\r
153         /* The IAR equivalent is implemented in\r
154         $PROJ_DIR$/System/IAR/Interrupt_Entry_Stubs.asm */\r
155         static void FreeRTOS_Tick_Handler_Entry( void )\r
156         {\r
157                 __asm volatile (\r
158                                                         "PUSH   {r0-r1}                                                         \t\n"\r
159                                                         "LDR    r0, =pxISRFunction                                      \t\n"\r
160                                                         "LDR    R1, =FreeRTOS_Tick_Handler                      \t\n"\r
161                                                         "STR    R1, [r0]                                                        \t\n"\r
162                                                         "POP    {r0-r1}                                                         \t\n"\r
163                                                         "B              FreeRTOS_IRQ_Handler                                    "\r
164                                                 );\r
165         }\r
166 #endif /* __GNUC__ */\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 \r
170 \r
171 \r