]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R5_UltraScale_MPSoC/RTOSDemo_R5/src/FreeRTOS_tick_config.c
148ccf3334ce4b8c09d1ad873b273a1386974517
[freertos] / FreeRTOS / Demo / CORTEX_R5_UltraScale_MPSoC / RTOSDemo_R5 / src / FreeRTOS_tick_config.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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 #include "Task.h"\r
31 \r
32 /* Xilinx includes. */\r
33 #include "xscugic.h"\r
34 #include "xttcps.h"\r
35 \r
36 /* Settings to generate the tick from channel 0 of TTC 0. */\r
37 #define tickTTC_ID                      XPAR_PSU_TTC_0_DEVICE_ID\r
38 #define tickINTERRUPT_ID        XPAR_XTTCPS_0_INTR\r
39 \r
40 /* The timer used to generate the tick interrupt. */\r
41 static XTtcPs xTimerInstance;\r
42 \r
43 /*\r
44  * The application must provide a function that configures a peripheral to\r
45  * create the FreeRTOS tick interrupt, then define configSETUP_TICK_INTERRUPT()\r
46  * in FreeRTOSConfig.h to call the function.  This file contains a function\r
47  * that is suitable for use on the Zynq SoC.\r
48  */\r
49 void vConfigureTickInterrupt( void )\r
50 {\r
51 XTtcPs_Config *pxTimerConfig;\r
52 BaseType_t xStatus;\r
53 XScuGic_Config *pxGICConfig;\r
54 static XScuGic xInterruptController;    /* Interrupt controller instance */\r
55 uint16_t usInterval;\r
56 uint8_t ucPrescale = 0;\r
57 const uint8_t ucRisingEdge = 3;\r
58 \r
59         /* This function is called with the IRQ interrupt disabled, and the IRQ\r
60         interrupt should be left disabled.  It is enabled automatically when the\r
61         scheduler is started. */\r
62 \r
63         /* Ensure XScuGic_CfgInitialize() has been called.  In this demo it has\r
64         already been called from prvSetupHardware() in main(). */\r
65         pxGICConfig = XScuGic_LookupConfig( XPAR_SCUGIC_SINGLE_DEVICE_ID );\r
66         xStatus = XScuGic_CfgInitialize( &xInterruptController, pxGICConfig, pxGICConfig->CpuBaseAddress );\r
67         configASSERT( xStatus == XST_SUCCESS );\r
68         ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */\r
69 \r
70         /* The interrupt priority must be the lowest possible. */\r
71         XScuGic_SetPriorityTriggerType( &xInterruptController, tickINTERRUPT_ID, portLOWEST_USABLE_INTERRUPT_PRIORITY << portPRIORITY_SHIFT, ucRisingEdge );\r
72 \r
73         /* Install the FreeRTOS tick handler. */\r
74         xStatus = XScuGic_Connect( &xInterruptController, tickINTERRUPT_ID, (Xil_ExceptionHandler) FreeRTOS_Tick_Handler, NULL );\r
75         configASSERT( xStatus == XST_SUCCESS );\r
76         ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */\r
77 \r
78         /* Initialise the Triple Timer Counter (TTC) that is going to be used to\r
79         generate the tick interrupt. */\r
80         pxTimerConfig = XTtcPs_LookupConfig( tickTTC_ID );\r
81         xStatus = XTtcPs_CfgInitialize( &xTimerInstance, pxTimerConfig, pxTimerConfig->BaseAddress );\r
82         configASSERT( xStatus == XST_SUCCESS );\r
83         ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */\r
84 \r
85         /* Configure the interval to be the require tick rate. */\r
86         XTtcPs_CalcIntervalFromFreq( &xTimerInstance, configTICK_RATE_HZ, &usInterval, &ucPrescale );\r
87         XTtcPs_SetInterval( &xTimerInstance, usInterval );\r
88         XTtcPs_SetPrescaler( &xTimerInstance, ucPrescale );\r
89 \r
90         /* Interval mode used. */\r
91         XTtcPs_SetOptions( &xTimerInstance, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE );\r
92 \r
93         /* Start the timer. */\r
94         XTtcPs_Start( &xTimerInstance );\r
95 \r
96         /* Enable the interrupt in the interrupt controller. */\r
97         XScuGic_Enable( &xInterruptController, tickINTERRUPT_ID );\r
98 \r
99         /* Enable the interrupt in the timer itself. */\r
100         XTtcPs_EnableInterrupts( &xTimerInstance, XTTCPS_IXR_INTERVAL_MASK );\r
101 }\r
102 /*-----------------------------------------------------------*/\r
103 \r
104 void vClearTickInterrupt( void )\r
105 {\r
106 volatile uint32_t ulStatus;\r
107 \r
108         ulStatus = XTtcPs_GetInterruptStatus( &xTimerInstance );\r
109         ( void ) ulStatus;\r
110 }\r
111 /*-----------------------------------------------------------*/\r
112 \r
113 void vApplicationIRQHandler( uint32_t ulICCIAR )\r
114 {\r
115 extern const XScuGic_Config XScuGic_ConfigTable[];\r
116 static const XScuGic_VectorTableEntry *pxVectorTable = XScuGic_ConfigTable[ XPAR_SCUGIC_SINGLE_DEVICE_ID ].HandlerTable;\r
117 uint32_t ulInterruptID;\r
118 const XScuGic_VectorTableEntry *pxVectorEntry;\r
119 \r
120         /* Re-enable interrupts. */\r
121     __asm ( "cpsie i" );\r
122 \r
123         /* The ID of the interrupt is obtained by bitwise anding the ICCIAR value\r
124         with 0x3FF. */\r
125         ulInterruptID = ulICCIAR & 0x3FFUL;\r
126         if( ulInterruptID < XSCUGIC_MAX_NUM_INTR_INPUTS )\r
127         {\r
128                 /* Call the function installed in the array of installed handler\r
129                 functions. */\r
130                 pxVectorEntry = &( pxVectorTable[ ulInterruptID ] );\r
131                 pxVectorEntry->Handler( pxVectorEntry->CallBackRef );\r
132         }\r
133 }\r
134 \r
135 \r