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