]> git.sur5r.net Git - freertos/blob - Demo/ARM7_AT91FR40008_GCC/serial/serialISR.c
Remove unnecessary use of portLONG, portCHAR and portSHORT.
[freertos] / Demo / ARM7_AT91FR40008_GCC / serial / serialISR.c
1 /*\r
2     FreeRTOS V6.0.0 - 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     * The FreeRTOS eBook and reference manual are available to purchase for a *\r
29     * small fee. Help yourself get started quickly while also helping the     *\r
30     * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
31     *                                                                         *\r
32     ***************************************************************************\r
33 \r
34     1 tab == 4 spaces!\r
35 \r
36     Please ensure to read the configuration and relevant port sections of the\r
37     online documentation.\r
38 \r
39     http://www.FreeRTOS.org - Documentation, latest information, license and\r
40     contact details.\r
41 \r
42     http://www.SafeRTOS.com - A version that is certified for use in safety\r
43     critical systems.\r
44 \r
45     http://www.OpenRTOS.com - Commercial support, development, porting,\r
46     licensing and training services.\r
47 */\r
48 \r
49 \r
50 /* \r
51   BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0. \r
52 \r
53   This file contains all the serial port components that must be compiled\r
54   to ARM mode.  The components that can be compiled to either ARM or THUMB\r
55   mode are contained in serial.c.\r
56 \r
57 */\r
58 \r
59 /* Standard includes. */\r
60 #include <stdlib.h>\r
61 \r
62 /* Scheduler includes. */\r
63 #include "FreeRTOS.h"\r
64 #include "queue.h"\r
65 #include "task.h"\r
66 \r
67 /* Demo application includes. */\r
68 #include "serial.h"\r
69 #include "AT91R40008.h"\r
70 #include "usart.h"\r
71 \r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /* Constant to access the AIC. */\r
75 #define serCLEAR_AIC_INTERRUPT      ( ( unsigned long ) 0 )\r
76 \r
77 /* Constants to determine the ISR source. */\r
78 #define serSOURCE_THRE                          ( ( unsigned char ) 0x02 )\r
79 #define serSOURCE_RX_TIMEOUT            ( ( unsigned char ) 0x0c )\r
80 #define serSOURCE_ERROR                         ( ( unsigned char ) 0x06 )\r
81 #define serSOURCE_RX                            ( ( unsigned char ) 0x04 )\r
82 #define serINTERRUPT_SOURCE_MASK    ( ( unsigned long ) (US_RXRDY | US_TXRDY | US_RXBRK | US_OVRE | US_FRAME | US_PARE) )\r
83 \r
84 /* Queues used to hold received characters, and characters waiting to be\r
85 transmitted. */\r
86 static xQueueHandle xRxedChars; \r
87 static xQueueHandle xCharsForTx; \r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 /* UART0 interrupt service routine.  This can cause a context switch so MUST\r
92 be declared "naked". */\r
93 void vUART_ISR_Wrapper( void ) __attribute__ ((naked));\r
94 \r
95 /* The ISR function that actually performs the work.  This must be separate \r
96 from the wrapper to ensure the correct stack frame is set up. */\r
97 void vUART_ISR_Handler( void ) __attribute__ ((noinline));\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx )\r
101 {\r
102         /* Create the queues used to hold Rx and Tx characters. */\r
103         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
104         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
105 \r
106         /* Pass back a reference to the queues so the serial API file can \r
107         post/receive characters. */\r
108         *pxRxedChars = xRxedChars;\r
109         *pxCharsForTx = xCharsForTx;\r
110 }\r
111 /*-----------------------------------------------------------*/\r
112 \r
113 void vUART_ISR_Wrapper( void )\r
114 {\r
115         /* Save the context of the interrupted task. */\r
116         portSAVE_CONTEXT();\r
117 \r
118         /* Call the handler.  This must be a separate function to ensure the \r
119         stack frame is correctly set up. */\r
120         __asm volatile( "bl vUART_ISR_Handler" );\r
121 \r
122         /* Restore the context of whichever task will run next. */\r
123         portRESTORE_CONTEXT();\r
124 }\r
125 /*-----------------------------------------------------------*/\r
126 \r
127 void vUART_ISR_Handler( void )\r
128 {\r
129 /* Now we can declare the local variables.   These must be static. */\r
130 signed char cChar;\r
131 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
132 unsigned long ulStatus;\r
133 \r
134         /* What caused the interrupt? */\r
135         ulStatus = AT91C_BASE_US0->US_CSR & AT91C_BASE_US0->US_IMR;\r
136 \r
137         if (ulStatus & US_TXRDY)\r
138         {\r
139                 /* The interrupt was caused by the THR becoming empty.  Are there any\r
140                 more characters to transmit? */\r
141                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
142                 {\r
143                         /* A character was retrieved from the queue so can be sent to the\r
144                         THR now. */\r
145                         AT91C_BASE_US0->US_THR = cChar;\r
146                 }\r
147                 else\r
148                 {\r
149                         /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
150                         AT91C_BASE_US0->US_IDR = US_TXRDY;\r
151                 }    \r
152         }\r
153 \r
154         if (ulStatus & US_RXRDY)\r
155         {\r
156                 /* The interrupt was caused by the receiver getting data. */\r
157                 cChar = AT91C_BASE_US0->US_RHR;\r
158 \r
159                 xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken);\r
160         }\r
161 \r
162         /* Acknowledge the interrupt at AIC level... */\r
163         AT91C_BASE_AIC->AIC_EOICR = serCLEAR_AIC_INTERRUPT;\r
164 \r
165         /* If an event caused a task to unblock then we call "Yield from ISR" to\r
166         ensure that the unblocked task is the task that executes when the interrupt\r
167         completes if the unblocked task has a priority higher than the interrupted\r
168         task. */\r
169         if( xHigherPriorityTaskWoken )\r
170         {\r
171                 portYIELD_FROM_ISR();\r
172         }\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r