]> git.sur5r.net Git - freertos/blob - Demo/ARM7_AT91FR40008_GCC/serial/serialISR.c
Update to V5.4.2. See http://www.freertos.org/History.txt .
[freertos] / Demo / ARM7_AT91FR40008_GCC / serial / serialISR.c
1 /*\r
2         FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 \r
49 /* \r
50   BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0. \r
51 \r
52   This file contains all the serial port components that must be compiled\r
53   to ARM mode.  The components that can be compiled to either ARM or THUMB\r
54   mode are contained in serial.c.\r
55 \r
56 */\r
57 \r
58 /* Standard includes. */\r
59 #include <stdlib.h>\r
60 \r
61 /* Scheduler includes. */\r
62 #include "FreeRTOS.h"\r
63 #include "queue.h"\r
64 #include "task.h"\r
65 \r
66 /* Demo application includes. */\r
67 #include "serial.h"\r
68 #include "AT91R40008.h"\r
69 #include "usart.h"\r
70 \r
71 /*-----------------------------------------------------------*/\r
72 \r
73 /* Constant to access the AIC. */\r
74 #define serCLEAR_AIC_INTERRUPT      ( ( unsigned portLONG ) 0 )\r
75 \r
76 /* Constants to determine the ISR source. */\r
77 #define serSOURCE_THRE                          ( ( unsigned portCHAR ) 0x02 )\r
78 #define serSOURCE_RX_TIMEOUT            ( ( unsigned portCHAR ) 0x0c )\r
79 #define serSOURCE_ERROR                         ( ( unsigned portCHAR ) 0x06 )\r
80 #define serSOURCE_RX                            ( ( unsigned portCHAR ) 0x04 )\r
81 #define serINTERRUPT_SOURCE_MASK    ( ( unsigned portLONG ) (US_RXRDY | US_TXRDY | US_RXBRK | US_OVRE | US_FRAME | US_PARE) )\r
82 \r
83 /* Queues used to hold received characters, and characters waiting to be\r
84 transmitted. */\r
85 static xQueueHandle xRxedChars; \r
86 static xQueueHandle xCharsForTx; \r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 /* UART0 interrupt service routine.  This can cause a context switch so MUST\r
91 be declared "naked". */\r
92 void vUART_ISR_Wrapper( void ) __attribute__ ((naked));\r
93 \r
94 /* The ISR function that actually performs the work.  This must be separate \r
95 from the wrapper to ensure the correct stack frame is set up. */\r
96 void vUART_ISR_Handler( void );\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx )\r
100 {\r
101         /* Create the queues used to hold Rx and Tx characters. */\r
102         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
103         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
104 \r
105         /* Pass back a reference to the queues so the serial API file can \r
106         post/receive characters. */\r
107         *pxRxedChars = xRxedChars;\r
108         *pxCharsForTx = xCharsForTx;\r
109 }\r
110 /*-----------------------------------------------------------*/\r
111 \r
112 void vUART_ISR_Wrapper( void )\r
113 {\r
114         /* Save the context of the interrupted task. */\r
115         portSAVE_CONTEXT();\r
116 \r
117         /* Call the handler.  This must be a separate function to ensure the \r
118         stack frame is correctly set up. */\r
119         vUART_ISR_Handler();\r
120 \r
121         /* Restore the context of whichever task will run next. */\r
122         portRESTORE_CONTEXT();\r
123 }\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 void vUART_ISR_Handler( void )\r
127 {\r
128 /* Now we can declare the local variables.   These must be static. */\r
129 signed portCHAR cChar;\r
130 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
131 unsigned portLONG ulStatus;\r
132 \r
133         /* What caused the interrupt? */\r
134         ulStatus = AT91C_BASE_US0->US_CSR & AT91C_BASE_US0->US_IMR;\r
135 \r
136         if (ulStatus & US_TXRDY)\r
137         {\r
138                 /* The interrupt was caused by the THR becoming empty.  Are there any\r
139                 more characters to transmit? */\r
140                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
141                 {\r
142                         /* A character was retrieved from the queue so can be sent to the\r
143                         THR now. */\r
144                         AT91C_BASE_US0->US_THR = cChar;\r
145                 }\r
146                 else\r
147                 {\r
148                         /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
149                         AT91C_BASE_US0->US_IDR = US_TXRDY;\r
150                 }    \r
151         }\r
152 \r
153         if (ulStatus & US_RXRDY)\r
154         {\r
155                 /* The interrupt was caused by the receiver getting data. */\r
156                 cChar = AT91C_BASE_US0->US_RHR;\r
157 \r
158                 xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken);\r
159         }\r
160 \r
161         /* Acknowledge the interrupt at AIC level... */\r
162         AT91C_BASE_AIC->AIC_EOICR = serCLEAR_AIC_INTERRUPT;\r
163 \r
164         /* If an event caused a task to unblock then we call "Yield from ISR" to\r
165         ensure that the unblocked task is the task that executes when the interrupt\r
166         completes if the unblocked task has a priority higher than the interrupted\r
167         task. */\r
168         if( xHigherPriorityTaskWoken )\r
169         {\r
170                 portYIELD_FROM_ISR();\r
171         }\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r