]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Tensilica_Simulator_Xplorer_XCC/IntQueueTimer.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / Tensilica_Simulator_Xplorer_XCC / IntQueueTimer.c
1 /*\r
2  * FreeRTOS Kernel V10.0.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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Scheduler includes. */\r
30 #include "FreeRTOS.h"\r
31 #include "task.h"\r
32 \r
33 /* Demo includes. */\r
34 #include "IntQueueTimer.h"\r
35 #include "IntQueue.h"\r
36 \r
37 /* Xtensa includes. */\r
38 #include <xtensa/corebits.h>\r
39 #include <xtensa/config/system.h>\r
40 #include <xtensa_api.h>\r
41 #include <xtensa/hal.h>\r
42 /*-----------------------------------------------------------*/\r
43 \r
44 /* Check if Timer1 is available. */\r
45 #if XCHAL_TIMER1_INTERRUPT != XTHAL_TIMER_UNCONFIGURED\r
46         #if XCHAL_INT_LEVEL( XCHAL_TIMER1_INTERRUPT ) <= XCHAL_EXCM_LEVEL\r
47                 #define SECOND_TIMER_AVAILABLE          1\r
48         #endif\r
49 #endif\r
50 \r
51 #ifndef SECOND_TIMER_AVAILABLE\r
52         #define SECOND_TIMER_AVAILABLE                  0\r
53 #endif\r
54 \r
55 /**\r
56  * Timer0 is used to drive systick and therefore we use Timer1\r
57  * as second interrupt which runs on a higher priority than\r
58  * Timer0. This ensures that systick will get interrupted by\r
59  * this timer and hence we can test interrupt nesting.\r
60  */\r
61 #define SECOND_TIMER_INDEX                                      1\r
62 \r
63 /**\r
64  * Frequency of the second timer - This timer is configured at\r
65  * a frequency offset of 17 from the systick timer.\r
66  */\r
67 #define SECOND_TIMER_TICK_RATE_HZ                       ( configTICK_RATE_HZ + 17 )\r
68 #define SECOND_TIMER_TICK_DIVISOR                       ( configCPU_CLOCK_HZ / SECOND_TIMER_TICK_RATE_HZ )\r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* Defined in main_full.c. */\r
72 extern BaseType_t xTimerForQueueTestInitialized;\r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /**\r
76  * Interrupt handler for timer interrupt.\r
77  */\r
78 #if( SECOND_TIMER_AVAILABLE == 1 )\r
79         static void prvTimer2Handler( void *arg );\r
80 #endif /* SECOND_TIMER_AVAILABLE */\r
81 /*-----------------------------------------------------------*/\r
82 \r
83 void vInitialiseTimerForIntQueueTest( void )\r
84 {\r
85 unsigned currentCycleCount, firstComparatorValue;\r
86 \r
87         /* Inform the tick hook function that it can access queues now. */\r
88         xTimerForQueueTestInitialized = pdTRUE;\r
89 \r
90         #if( SECOND_TIMER_AVAILABLE == 1 )\r
91         {\r
92                 /* Install the interrupt handler for second timer. */\r
93                 xt_set_interrupt_handler( XCHAL_TIMER1_INTERRUPT, prvTimer2Handler, NULL );\r
94 \r
95                 /* Read the current cycle count. */\r
96                 currentCycleCount = xthal_get_ccount();\r
97 \r
98                 /* Calculate time of the first timer interrupt. */\r
99                 firstComparatorValue = currentCycleCount + SECOND_TIMER_TICK_DIVISOR;\r
100 \r
101                 /* Set the comparator. */\r
102                 xthal_set_ccompare( SECOND_TIMER_INDEX, firstComparatorValue );\r
103 \r
104                 /* Enable timer interrupt. */\r
105                 xt_ints_on( ( 1 << XCHAL_TIMER1_INTERRUPT ) );\r
106         }\r
107         #endif /* SECOND_TIMER_AVAILABLE */\r
108 }\r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /*\r
112  * Xtensa timers work by comparing a cycle counter with a preset value.\r
113  * Once the match occurs an interrupt is generated, and the handler has\r
114  * to set a new cycle count into the comparator. To avoid clock drift\r
115  * due to interrupt latency, the new cycle count is computed from the\r
116  * old, not the time the interrupt was serviced. However if a timer\r
117  * interrupt is ever serviced more than one tick late, it is necessary\r
118  * to process multiple ticks until the new cycle count is in the future,\r
119  * otherwise the next timer interrupt would not occur until after the\r
120  * cycle counter had wrapped (2^32 cycles later).\r
121 \r
122 do {\r
123     ticks++;\r
124     old_ccompare = read_ccompare_i();\r
125     write_ccompare_i( old_ccompare + divisor );\r
126     service one tick;\r
127     diff = read_ccount() - old_ccompare;\r
128 } while ( diff > divisor );\r
129 */\r
130 #if( SECOND_TIMER_AVAILABLE == 1 )\r
131 \r
132         static void prvTimer2Handler( void *arg )\r
133         {\r
134         unsigned oldComparatorValue, newComparatorValue, currentCycleCount;\r
135 \r
136                 /* Unused arguments. */\r
137                 ( void )arg;\r
138 \r
139                 do\r
140                 {\r
141                         /* Read old comparator value. */\r
142                         oldComparatorValue = xthal_get_ccompare( SECOND_TIMER_INDEX );\r
143 \r
144                         /* Calculate the new comparator value. */\r
145                         newComparatorValue = oldComparatorValue + SECOND_TIMER_TICK_DIVISOR;\r
146 \r
147                         /* Update comparator and clear interrupt. */\r
148                         xthal_set_ccompare( SECOND_TIMER_INDEX, newComparatorValue );\r
149 \r
150                         /* Process. */\r
151                         portYIELD_FROM_ISR( xSecondTimerHandler() );\r
152 \r
153                         /* Ensure comparator update is complete. */\r
154                         xthal_icache_sync();\r
155 \r
156                         /* Read current cycle count to check if we need to process more\r
157                          * ticks to catch up. */\r
158                         currentCycleCount = xthal_get_ccount();\r
159 \r
160                 } while( ( currentCycleCount - oldComparatorValue ) > SECOND_TIMER_TICK_DIVISOR );\r
161         }\r
162 \r
163 #endif /* SECOND_TIMER_AVAILABLE */\r
164 /*-----------------------------------------------------------*/\r