]> git.sur5r.net Git - freertos/blob - Demo/ARM7_AT91FR40008_GCC/serial/serialISR.c
Updated GCC/ARM7 ISR functions so they only use static variables.
[freertos] / Demo / ARM7_AT91FR40008_GCC / serial / serialISR.c
1 /*\r
2   FreeRTOS.org V4.5.0 - Copyright (C) 2003-2007 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.   These must be static. */\r
101         static signed portCHAR cChar;\r
102         static portBASE_TYPE xTaskWokenByTx, xTaskWokenByRx;\r
103         static unsigned portLONG ulStatus;\r
104 \r
105         /* These variables are static so need initialising manually here. */\r
106         xTaskWokenByTx = pdFALSE;\r
107         xTaskWokenByRx = pdFALSE;\r
108 \r
109         /* What caused the interrupt? */\r
110         ulStatus = AT91C_BASE_US0->US_CSR & AT91C_BASE_US0->US_IMR;\r
111 \r
112         if (ulStatus & US_TXRDY)\r
113         {\r
114                 /* The interrupt was caused by the THR becoming empty.  Are there any\r
115                 more characters to transmit? */\r
116                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWokenByTx ) == pdTRUE )\r
117                 {\r
118                         /* A character was retrieved from the queue so can be sent to the\r
119                         THR now. */\r
120                         AT91C_BASE_US0->US_THR = cChar;\r
121                 }\r
122                 else\r
123                 {\r
124                         /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
125                         AT91C_BASE_US0->US_IDR = US_TXRDY;\r
126                 }    \r
127         }\r
128 \r
129         if (ulStatus & US_RXRDY)\r
130         {\r
131                 /* The interrupt was caused by the receiver getting data. */\r
132                 cChar = AT91C_BASE_US0->US_RHR;\r
133 \r
134                 if (xQueueSendFromISR(xRxedChars, &cChar, pdFALSE))\r
135                 {\r
136                         xTaskWokenByRx = pdTRUE;\r
137                 }\r
138         }\r
139 \r
140         /* Acknowledge the interrupt at AIC level... */\r
141         AT91C_BASE_AIC->AIC_EOICR = serCLEAR_AIC_INTERRUPT;\r
142 \r
143         /* Exit the ISR.  If a task was woken by either a character being received\r
144         or transmitted then a context switch will occur. */\r
145         portEXIT_SWITCHING_ISR( ( xTaskWokenByTx || xTaskWokenByRx ) );\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r