]> git.sur5r.net Git - freertos/blob - Demo/ARM7_AT91FR40008_GCC/serial/serialISR.c
e337813076507c9a6635c9cb8d9402da671cada1
[freertos] / Demo / ARM7_AT91FR40008_GCC / serial / serialISR.c
1 /*\r
2   FreeRTOS.org V4.0.2 - Copyright (C) 2003-2006 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( void ) __attribute__ ((naked));\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx )\r
81 {\r
82         /* Create the queues used to hold Rx and Tx characters. */\r
83         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
84         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
85 \r
86         /* Pass back a reference to the queues so the serial API file can \r
87         post/receive characters. */\r
88         *pxRxedChars = xRxedChars;\r
89         *pxCharsForTx = xCharsForTx;\r
90 }\r
91 /*-----------------------------------------------------------*/\r
92 \r
93 void vUART_ISR( void )\r
94 {\r
95         /* This ISR can cause a context switch, so the first statement must be a\r
96         call to the portENTER_SWITCHING_ISR() macro.  This must be BEFORE any\r
97         variable declarations. */\r
98         portENTER_SWITCHING_ISR();\r
99 \r
100         /* Now we can declare the local variables. */\r
101         signed portCHAR cChar;\r
102         portBASE_TYPE xTaskWokenByTx = pdFALSE, xTaskWokenByRx = pdFALSE;\r
103         unsigned portLONG ulStatus;\r
104 \r
105         /* What caused the interrupt? */\r
106         ulStatus = AT91C_BASE_US0->US_CSR & AT91C_BASE_US0->US_IMR;\r
107 \r
108         if (ulStatus & US_TXRDY)\r
109         {\r
110                 /* The interrupt was caused by the THR becoming empty.  Are there any\r
111                 more characters to transmit? */\r
112                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWokenByTx ) == pdTRUE )\r
113                 {\r
114                         /* A character was retrieved from the queue so can be sent to the\r
115                         THR now. */\r
116                         AT91C_BASE_US0->US_THR = cChar;\r
117                 }\r
118                 else\r
119                 {\r
120                         /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
121                         AT91C_BASE_US0->US_IDR = US_TXRDY;\r
122                 }    \r
123         }\r
124 \r
125         if (ulStatus & US_RXRDY)\r
126         {\r
127                 /* The interrupt was caused by the receiver getting data. */\r
128                 cChar = AT91C_BASE_US0->US_RHR;\r
129 \r
130                 if (xQueueSendFromISR(xRxedChars, &cChar, pdFALSE))\r
131                 {\r
132                         xTaskWokenByRx = pdTRUE;\r
133                 }\r
134         }\r
135 \r
136         // Acknowledge the interrupt at AIC level...\r
137         AT91C_BASE_AIC->AIC_EOICR = serCLEAR_AIC_INTERRUPT;\r
138 \r
139         /* Exit the ISR.  If a task was woken by either a character being received\r
140         or transmitted then a context switch will occur. */\r
141         portEXIT_SWITCHING_ISR( ( xTaskWokenByTx || xTaskWokenByRx ) );\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r