]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/FreeRTOS_tick_config.c
d2f405bfc7bd4e321c263989becc7831b78e92d9
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / FreeRTOS_tick_config.c
1 /*\r
2  * FreeRTOS Kernel V10.1.1\r
3  * Copyright (C) 2018 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 "xscutimer.h"\r
34 #include "xscugic.h"\r
35 \r
36 #define XSCUTIMER_CLOCK_HZ ( XPAR_CPU_CORTEXA9_0_CPU_CLK_FREQ_HZ / 2UL )\r
37 \r
38 static XScuTimer xTimer;\r
39 XScuGic xInterruptController;   /* Interrupt controller instance */\r
40 \r
41 /*\r
42  * The application must provide a function that configures a peripheral to\r
43  * create the FreeRTOS tick interrupt, then define configSETUP_TICK_INTERRUPT()\r
44  * in FreeRTOSConfig.h to call the function.  This file contains a function\r
45  * that is suitable for use on the Zynq SoC.\r
46  */\r
47 void vConfigureTickInterrupt( void )\r
48 {\r
49 BaseType_t xStatus;\r
50 extern void FreeRTOS_Tick_Handler( void );\r
51 XScuTimer_Config *pxTimerConfig;\r
52 XScuGic_Config *pxGICConfig;\r
53 const uint8_t ucRisingEdge = 3;\r
54 \r
55         /* This function is called with the IRQ interrupt disabled, and the IRQ\r
56         interrupt should be left disabled.  It is enabled automatically when the\r
57         scheduler is started. */\r
58 \r
59         /* Ensure XScuGic_CfgInitialize() has been called.  In this demo it has\r
60         already been called from prvSetupHardware() in main(). */\r
61         pxGICConfig = XScuGic_LookupConfig( XPAR_SCUGIC_SINGLE_DEVICE_ID );\r
62         xStatus = XScuGic_CfgInitialize( &xInterruptController, pxGICConfig, pxGICConfig->CpuBaseAddress );\r
63         configASSERT( xStatus == XST_SUCCESS );\r
64         ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */\r
65 \r
66         /* The priority must be the lowest possible. */\r
67         XScuGic_SetPriorityTriggerType( &xInterruptController, XPAR_SCUTIMER_INTR, portLOWEST_USABLE_INTERRUPT_PRIORITY << portPRIORITY_SHIFT, ucRisingEdge );\r
68 \r
69         /* Install the FreeRTOS tick handler. */\r
70         xStatus = XScuGic_Connect( &xInterruptController, XPAR_SCUTIMER_INTR, (Xil_ExceptionHandler) FreeRTOS_Tick_Handler, ( void * ) &xTimer );\r
71         configASSERT( xStatus == XST_SUCCESS );\r
72         ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */\r
73 \r
74         /* Initialise the timer. */\r
75         pxTimerConfig = XScuTimer_LookupConfig( XPAR_SCUTIMER_DEVICE_ID );\r
76         xStatus = XScuTimer_CfgInitialize( &xTimer, pxTimerConfig, pxTimerConfig->BaseAddr );\r
77         configASSERT( xStatus == XST_SUCCESS );\r
78         ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */\r
79 \r
80         /* Enable Auto reload mode. */\r
81         XScuTimer_EnableAutoReload( &xTimer );\r
82 \r
83         /* Ensure there is no prescale. */\r
84         XScuTimer_SetPrescaler( &xTimer, 0 );\r
85 \r
86         /* Load the timer counter register. */\r
87         XScuTimer_LoadTimer( &xTimer, XSCUTIMER_CLOCK_HZ / configTICK_RATE_HZ );\r
88 \r
89         /* Start the timer counter and then wait for it to timeout a number of\r
90         times. */\r
91         XScuTimer_Start( &xTimer );\r
92 \r
93         /* Enable the interrupt for the xTimer in the interrupt controller. */\r
94         XScuGic_Enable( &xInterruptController, XPAR_SCUTIMER_INTR );\r
95 \r
96         /* Enable the interrupt in the xTimer itself. */\r
97         vClearTickInterrupt();\r
98         XScuTimer_EnableInterrupt( &xTimer );\r
99 }\r
100 /*-----------------------------------------------------------*/\r
101 \r
102 void vClearTickInterrupt( void )\r
103 {\r
104         XScuTimer_ClearInterruptStatus( &xTimer );\r
105 }\r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /* This is the callback function which is called by the FreeRTOS Cortex-A port\r
109 layer in response to an interrupt.  If the function is called\r
110 vApplicationFPUSafeIRQHandler() then it is called after the floating point\r
111 registers have been saved.  If the function is called vApplicationIRQHandler()\r
112 then it will be called without first having saved the FPU registers.  See\r
113 http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html for\r
114 more information */\r
115 void vApplicationFPUSafeIRQHandler( uint32_t ulICCIAR )\r
116 {\r
117 extern const XScuGic_Config XScuGic_ConfigTable[];\r
118 static const XScuGic_VectorTableEntry *pxVectorTable = XScuGic_ConfigTable[ XPAR_SCUGIC_SINGLE_DEVICE_ID ].HandlerTable;\r
119 uint32_t ulInterruptID;\r
120 const XScuGic_VectorTableEntry *pxVectorEntry;\r
121 \r
122         /* Re-enable interrupts. */\r
123         __asm ( "cpsie i" );\r
124 \r
125         /* The ID of the interrupt is obtained by bitwise anding the ICCIAR value\r
126         with 0x3FF. */\r
127         ulInterruptID = ulICCIAR & 0x3FFUL;\r
128         if( ulInterruptID < XSCUGIC_MAX_NUM_INTR_INPUTS )\r
129         {\r
130                 /* Call the function installed in the array of installed handler functions. */\r
131                 pxVectorEntry = &( pxVectorTable[ ulInterruptID ] );\r
132                 pxVectorEntry->Handler( pxVectorEntry->CallBackRef );\r
133         }\r
134 }\r
135 \r
136 \r
137 \r