]> git.sur5r.net Git - freertos/blob - Demo/ARM7_LPC2129_Keil/serial/serialISR.c
Update to V4.7.0.
[freertos] / Demo / ARM7_LPC2129_Keil / serial / serialISR.c
1 /*\r
2         FreeRTOS.org V4.7.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         Also see http://www.SafeRTOS.com a version that has been certified for use\r
32         in safety critical systems, plus commercial licensing, development and\r
33         support options.\r
34         ***************************************************************************\r
35 */\r
36 \r
37 \r
38 /* \r
39         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
40 \r
41         This file contains all the serial port components that must be compiled\r
42         to ARM mode.  The components that can be compiled to either ARM or THUMB\r
43         mode are contained in serial.c.\r
44 */\r
45 \r
46 /* This file must always be compiled to ARM mode as it contains ISR \r
47 definitions. */\r
48 #pragma ARM\r
49 \r
50 /* Standard includes. */\r
51 #include <stdlib.h>\r
52 \r
53 /* Scheduler includes. */\r
54 #include "FreeRTOS.h"\r
55 #include "queue.h"\r
56 #include "task.h"\r
57 \r
58 /* Demo application includes. */\r
59 #include "serial.h"\r
60 \r
61 /*-----------------------------------------------------------*/\r
62 \r
63 /* Constant to access the VIC. */\r
64 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned portLONG ) 0 )\r
65 \r
66 /* Constants to determine the ISR source. */\r
67 #define serSOURCE_THRE                                  ( ( unsigned portCHAR ) 0x02 )\r
68 #define serSOURCE_RX_TIMEOUT                    ( ( unsigned portCHAR ) 0x0c )\r
69 #define serSOURCE_ERROR                                 ( ( unsigned portCHAR ) 0x06 )\r
70 #define serSOURCE_RX                                    ( ( unsigned portCHAR ) 0x04 )\r
71 #define serINTERRUPT_SOURCE_MASK                ( ( unsigned portCHAR ) 0x0f )\r
72 \r
73 /* Queues used to hold received characters, and characters waiting to be\r
74 transmitted. */\r
75 static xQueueHandle xRxedChars; \r
76 static xQueueHandle xCharsForTx; \r
77 static volatile portLONG lTHREEmpty;\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* UART0 interrupt service routine.  This can cause a context switch so MUST\r
82 be declared "naked". */\r
83 void vUART_ISR( void );\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 void vSerialISRCreateQueues(    unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, \r
87                                                                 xQueueHandle *pxCharsForTx, portLONG volatile **pplTHREEmptyFlag )\r
88 {\r
89         /* Create the queues used to hold Rx and Tx characters. */\r
90         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
91         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
92 \r
93         /* Pass back a reference to the queues so the serial API file can \r
94         post/receive characters. */\r
95         *pxRxedChars = xRxedChars;\r
96         *pxCharsForTx = xCharsForTx;\r
97 \r
98         /* Initialise the THRE empty flag - and pass back a reference. */\r
99         lTHREEmpty = pdTRUE;\r
100         *pplTHREEmptyFlag = &lTHREEmpty;\r
101 }\r
102 /*-----------------------------------------------------------*/\r
103 \r
104 void vUART_ISR( void ) __task\r
105 {\r
106         portENTER_SWITCHING_ISR()\r
107 \r
108         /* Now we can declare the local variables. */\r
109         static signed portCHAR cChar;\r
110         static portBASE_TYPE xTaskWokenByRx, xTaskWokenByTx;\r
111 \r
112         xTaskWokenByTx = pdFALSE;\r
113         xTaskWokenByRx = pdFALSE;\r
114 \r
115         /* What caused the interrupt? */\r
116         switch( U0IIR & serINTERRUPT_SOURCE_MASK )\r
117         {\r
118                 case serSOURCE_ERROR :  /* Not handling this, but clear the interrupt. */\r
119                                                                 cChar = U0LSR;\r
120                                                                 break;\r
121 \r
122                 case serSOURCE_THRE     :       /* The THRE is empty.  If there is another\r
123                                                                 character in the Tx queue, send it now. */\r
124                                                                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWokenByTx ) == pdTRUE )\r
125                                                                 {\r
126                                                                         U0THR = cChar;\r
127                                                                 }\r
128                                                                 else\r
129                                                                 {\r
130                                                                         /* There are no further characters \r
131                                                                         queued to send so we can indicate \r
132                                                                         that the THRE is available. */\r
133                                                                         lTHREEmpty = pdTRUE;\r
134                                                                 }\r
135                                                                 break;\r
136 \r
137                 case serSOURCE_RX_TIMEOUT :\r
138                 case serSOURCE_RX       :       /* A character was received.  Place it in \r
139                                                                 the queue of received characters. */\r
140                                                                 cChar = U0RBR;\r
141                                                                 if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )\r
142                                                                 {\r
143                                                                         xTaskWokenByRx = pdTRUE;\r
144                                                                 }\r
145                                                                 break;\r
146 \r
147                 default                         :       /* There is nothing to do, leave the ISR. */\r
148                                                                 break;\r
149         }\r
150 \r
151         /* Clear the ISR in the VIC. */\r
152         VICVectAddr = serCLEAR_VIC_INTERRUPT;\r
153 \r
154         /* Exit the ISR.  If a task was woken by either a character being received\r
155         or transmitted then a context switch will occur. */\r
156         portEXIT_SWITCHING_ISR( ( xTaskWokenByTx || xTaskWokenByRx ) );\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 \r
161 \r
162 \r
163 \r
164         \r