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