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