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