]> git.sur5r.net Git - freertos/blob - Demo/ARM7_LPC2129_Keil/serial/serialISR.c
Update version numbers to V4.8.0
[freertos] / Demo / ARM7_LPC2129_Keil / serial / serialISR.c
1 /*\r
2         FreeRTOS.org V4.8.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         ***************************************************************************\r
28         *                                                                                                                                                 *\r
29         * SAVE TIME AND MONEY!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\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 UART0. \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 /* This file must always be compiled to ARM mode as it contains ISR \r
59 definitions. */\r
60 #pragma ARM\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 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /* Constant to access the VIC. */\r
76 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned portLONG ) 0 )\r
77 \r
78 /* Constants to determine the ISR source. */\r
79 #define serSOURCE_THRE                                  ( ( unsigned portCHAR ) 0x02 )\r
80 #define serSOURCE_RX_TIMEOUT                    ( ( unsigned portCHAR ) 0x0c )\r
81 #define serSOURCE_ERROR                                 ( ( unsigned portCHAR ) 0x06 )\r
82 #define serSOURCE_RX                                    ( ( unsigned portCHAR ) 0x04 )\r
83 #define serINTERRUPT_SOURCE_MASK                ( ( unsigned portCHAR ) 0x0f )\r
84 \r
85 /* Queues used to hold received characters, and characters waiting to be\r
86 transmitted. */\r
87 static xQueueHandle xRxedChars; \r
88 static xQueueHandle xCharsForTx; \r
89 static volatile portLONG lTHREEmpty;\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* UART0 interrupt service routine.  This can cause a context switch so MUST\r
94 be declared "naked". */\r
95 void vUART_ISR( void );\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 void vSerialISRCreateQueues(    unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, \r
99                                                                 xQueueHandle *pxCharsForTx, portLONG volatile **pplTHREEmptyFlag )\r
100 {\r
101         /* Create the queues used to hold Rx and Tx characters. */\r
102         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
103         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
104 \r
105         /* Pass back a reference to the queues so the serial API file can \r
106         post/receive characters. */\r
107         *pxRxedChars = xRxedChars;\r
108         *pxCharsForTx = xCharsForTx;\r
109 \r
110         /* Initialise the THRE empty flag - and pass back a reference. */\r
111         lTHREEmpty = pdTRUE;\r
112         *pplTHREEmptyFlag = &lTHREEmpty;\r
113 }\r
114 /*-----------------------------------------------------------*/\r
115 \r
116 void vUART_ISR( void ) __task\r
117 {\r
118         portENTER_SWITCHING_ISR()\r
119 \r
120         /* Now we can declare the local variables. */\r
121         static signed portCHAR cChar;\r
122         static portBASE_TYPE xTaskWokenByRx, xTaskWokenByTx;\r
123 \r
124         xTaskWokenByTx = pdFALSE;\r
125         xTaskWokenByRx = pdFALSE;\r
126 \r
127         /* What caused the interrupt? */\r
128         switch( U0IIR & serINTERRUPT_SOURCE_MASK )\r
129         {\r
130                 case serSOURCE_ERROR :  /* Not handling this, but clear the interrupt. */\r
131                                                                 cChar = U0LSR;\r
132                                                                 break;\r
133 \r
134                 case serSOURCE_THRE     :       /* The THRE is empty.  If there is another\r
135                                                                 character in the Tx queue, send it now. */\r
136                                                                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWokenByTx ) == pdTRUE )\r
137                                                                 {\r
138                                                                         U0THR = cChar;\r
139                                                                 }\r
140                                                                 else\r
141                                                                 {\r
142                                                                         /* There are no further characters \r
143                                                                         queued to send so we can indicate \r
144                                                                         that the THRE is available. */\r
145                                                                         lTHREEmpty = pdTRUE;\r
146                                                                 }\r
147                                                                 break;\r
148 \r
149                 case serSOURCE_RX_TIMEOUT :\r
150                 case serSOURCE_RX       :       /* A character was received.  Place it in \r
151                                                                 the queue of received characters. */\r
152                                                                 cChar = U0RBR;\r
153                                                                 if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )\r
154                                                                 {\r
155                                                                         xTaskWokenByRx = pdTRUE;\r
156                                                                 }\r
157                                                                 break;\r
158 \r
159                 default                         :       /* There is nothing to do, leave the ISR. */\r
160                                                                 break;\r
161         }\r
162 \r
163         /* Clear the ISR in the VIC. */\r
164         VICVectAddr = serCLEAR_VIC_INTERRUPT;\r
165 \r
166         /* Exit the ISR.  If a task was woken by either a character being received\r
167         or transmitted then a context switch will occur. */\r
168         portEXIT_SWITCHING_ISR( ( xTaskWokenByTx || xTaskWokenByRx ) );\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 \r
173 \r
174 \r
175 \r
176         \r