]> begriffs open source - cmsis-freertos/blob - Source/portable/GCC/ARM7_LPC23xx/portISR.c
Sources updated to FreeRTOS 10.2.0
[cmsis-freertos] / Source / portable / GCC / ARM7_LPC23xx / portISR.c
1 /*
2  * FreeRTOS Kernel V10.2.0
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28
29 /*-----------------------------------------------------------
30  * Components that can be compiled to either ARM or THUMB mode are
31  * contained in port.c  The ISR routines, which can only be compiled
32  * to ARM mode, are contained in this file.
33  *----------------------------------------------------------*/
34
35 /* Scheduler includes. */
36 #include "FreeRTOS.h"
37 #include "task.h"
38
39 /* Constants required to handle interrupts. */
40 #define portTIMER_MATCH_ISR_BIT         ( ( uint8_t ) 0x01 )
41 #define portCLEAR_VIC_INTERRUPT         ( ( uint32_t ) 0 )
42
43 /* Constants required to handle critical sections. */
44 #define portNO_CRITICAL_NESTING         ( ( uint32_t ) 0 )
45 volatile uint32_t ulCriticalNesting = 9999UL;
46
47 /*-----------------------------------------------------------*/
48
49 /* ISR to handle manual context switches (from a call to taskYIELD()). */
50 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));
51
52 /* 
53  * The scheduler can only be started from ARM mode, hence the inclusion of this
54  * function here.
55  */
56 void vPortISRStartFirstTask( void );
57 /*-----------------------------------------------------------*/
58
59 void vPortISRStartFirstTask( void )
60 {
61         /* Simply start the scheduler.  This is included here as it can only be
62         called from ARM mode. */
63         portRESTORE_CONTEXT();
64 }
65 /*-----------------------------------------------------------*/
66
67 /*
68  * Called by portYIELD() or taskYIELD() to manually force a context switch.
69  *
70  * When a context switch is performed from the task level the saved task 
71  * context is made to look as if it occurred from within the tick ISR.  This
72  * way the same restore context function can be used when restoring the context
73  * saved from the ISR or that saved from a call to vPortYieldProcessor.
74  */
75 void vPortYieldProcessor( void )
76 {
77         /* Within an IRQ ISR the link register has an offset from the true return 
78         address, but an SWI ISR does not.  Add the offset manually so the same 
79         ISR return code can be used in both cases. */
80         __asm volatile ( "ADD           LR, LR, #4" );
81
82         /* Perform the context switch.  First save the context of the current task. */
83         portSAVE_CONTEXT();
84
85         /* Find the highest priority task that is ready to run. */
86         __asm volatile( "bl                     vTaskSwitchContext" );
87
88         /* Restore the context of the new task. */
89         portRESTORE_CONTEXT();  
90 }
91 /*-----------------------------------------------------------*/
92
93 /* 
94  * The ISR used for the scheduler tick depends on whether the cooperative or
95  * the preemptive scheduler is being used.
96  */
97
98
99 #if configUSE_PREEMPTION == 0
100
101         /* The cooperative scheduler requires a normal IRQ service routine to 
102         simply increment the system tick. */
103         void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));
104         void vNonPreemptiveTick( void )
105         {       
106                 xTaskIncrementTick();
107                 T0IR = 2;
108                 VICVectAddr = portCLEAR_VIC_INTERRUPT;
109         }
110
111 #else
112
113         /* The preemptive scheduler is defined as "naked" as the full context is
114         saved on entry as part of the context switch. */
115         void vPreemptiveTick( void ) __attribute__((naked));
116         void vPreemptiveTick( void )
117         {
118                 /* Save the context of the interrupted task. */
119                 portSAVE_CONTEXT();     
120
121                 /* Increment the RTOS tick count, then look for the highest priority 
122                 task that is ready to run. */
123                 __asm volatile
124                 (
125                         "       bl xTaskIncrementTick   \t\n" \
126                         "       cmp r0, #0                              \t\n" \
127                         "       beq SkipContextSwitch   \t\n" \
128                         "       bl vTaskSwitchContext   \t\n" \
129                         "SkipContextSwitch:                     \t\n"
130                 );
131
132                 /* Ready for the next interrupt. */
133                 T0IR = 2;
134                 VICVectAddr = portCLEAR_VIC_INTERRUPT;
135                 
136                 /* Restore the context of the new task. */
137                 portRESTORE_CONTEXT();
138         }
139
140 #endif
141 /*-----------------------------------------------------------*/
142
143 /*
144  * The interrupt management utilities can only be called from ARM mode.  When
145  * THUMB_INTERWORK is defined the utilities are defined as functions here to
146  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then
147  * the utilities are defined as macros in portmacro.h - as per other ports.
148  */
149 #ifdef THUMB_INTERWORK
150
151         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));
152         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));
153
154         void vPortDisableInterruptsFromThumb( void )
155         {
156                 __asm volatile ( 
157                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */
158                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */
159                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */
160                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */
161                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */
162                         "BX             R14" );                                 /* Return back to thumb.                                        */
163         }
164                         
165         void vPortEnableInterruptsFromThumb( void )
166         {
167                 __asm volatile ( 
168                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      
169                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      
170                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      
171                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      
172                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */
173                         "BX             R14" );                                 /* Return back to thumb.                                        */
174         }
175
176 #endif /* THUMB_INTERWORK */
177
178 /* The code generated by the GCC compiler uses the stack in different ways at
179 different optimisation levels.  The interrupt flags can therefore not always
180 be saved to the stack.  Instead the critical section nesting level is stored
181 in a variable, which is then saved as part of the stack context. */
182 void vPortEnterCritical( void )
183 {
184         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */
185         __asm volatile ( 
186                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */
187                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */
188                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */
189                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */
190                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */
191
192         /* Now interrupts are disabled ulCriticalNesting can be accessed 
193         directly.  Increment ulCriticalNesting to keep a count of how many times
194         portENTER_CRITICAL() has been called. */
195         ulCriticalNesting++;
196 }
197
198 void vPortExitCritical( void )
199 {
200         if( ulCriticalNesting > portNO_CRITICAL_NESTING )
201         {
202                 /* Decrement the nesting count as we are leaving a critical section. */
203                 ulCriticalNesting--;
204
205                 /* If the nesting level has reached zero then interrupts should be
206                 re-enabled. */
207                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
208                 {
209                         /* Enable interrupts as per portEXIT_CRITICAL().                                        */
210                         __asm volatile ( 
211                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      
212                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      
213                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      
214                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      
215                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */
216                 }
217         }
218 }