]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2106_GCC/serial/serialISR.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / ARM7_LPC2106_GCC / serial / serialISR.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /* \r
72         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
73 \r
74         This file contains all the serial port components that must be compiled\r
75         to ARM mode.  The components that can be compiled to either ARM or THUMB\r
76         mode are contained in serial.c.\r
77 \r
78 */\r
79 \r
80 /* Standard includes. */\r
81 #include <stdlib.h>\r
82 \r
83 /* Scheduler includes. */\r
84 #include "FreeRTOS.h"\r
85 #include "queue.h"\r
86 #include "task.h"\r
87 \r
88 /* Demo application includes. */\r
89 #include "serial.h"\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* Constant to access the VIC. */\r
94 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )\r
95 \r
96 /* Constants to determine the ISR source. */\r
97 #define serSOURCE_THRE                                  ( ( unsigned char ) 0x02 )\r
98 #define serSOURCE_RX_TIMEOUT                    ( ( unsigned char ) 0x0c )\r
99 #define serSOURCE_ERROR                                 ( ( unsigned char ) 0x06 )\r
100 #define serSOURCE_RX                                    ( ( unsigned char ) 0x04 )\r
101 #define serINTERRUPT_SOURCE_MASK                ( ( unsigned char ) 0x0f )\r
102 \r
103 /* Queues used to hold received characters, and characters waiting to be\r
104 transmitted. */\r
105 static QueueHandle_t xRxedChars; \r
106 static QueueHandle_t xCharsForTx; \r
107 static volatile long lTHREEmpty;\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /* \r
112  * The queues are created in serialISR.c as they are used from the ISR.\r
113  * Obtain references to the queues and THRE Empty flag. \r
114  */\r
115 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx, long volatile **pplTHREEmptyFlag );\r
116 \r
117 /* UART0 interrupt service routine entry point. */\r
118 void vUART_ISR_Wrapper( void ) __attribute__ ((naked));\r
119 \r
120 /* UART0 interrupt service routine handler. */\r
121 void vUART_ISR_Handler( void ) __attribute__ ((noinline));\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 void vSerialISRCreateQueues(    unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, \r
125                                                                 QueueHandle_t *pxCharsForTx, long volatile **pplTHREEmptyFlag )\r
126 {\r
127         /* Create the queues used to hold Rx and Tx characters. */\r
128         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
129         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
130 \r
131         /* Pass back a reference to the queues so the serial API file can \r
132         post/receive characters. */\r
133         *pxRxedChars = xRxedChars;\r
134         *pxCharsForTx = xCharsForTx;\r
135 \r
136         /* Initialise the THRE empty flag - and pass back a reference. */\r
137         lTHREEmpty = ( long ) pdTRUE;\r
138         *pplTHREEmptyFlag = &lTHREEmpty;\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void vUART_ISR_Wrapper( void )\r
143 {\r
144         /* Save the context of the interrupted task. */\r
145         portSAVE_CONTEXT();\r
146 \r
147         /* Call the handler.  This must be a separate function from the wrapper\r
148         to ensure the correct stack frame is set up. */\r
149         __asm volatile ("bl vUART_ISR_Handler");\r
150 \r
151         /* Restore the context of whichever task is going to run next. */\r
152         portRESTORE_CONTEXT();\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 void vUART_ISR_Handler( void )\r
157 {\r
158 signed char cChar;\r
159 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
160 \r
161         /* What caused the interrupt? */\r
162         switch( UART0_IIR & serINTERRUPT_SOURCE_MASK )\r
163         {\r
164                 case serSOURCE_ERROR :  /* Not handling this, but clear the interrupt. */\r
165                                                                 cChar = UART0_LSR;\r
166                                                                 break;\r
167 \r
168                 case serSOURCE_THRE     :       /* The THRE is empty.  If there is another\r
169                                                                 character in the Tx queue, send it now. */\r
170                                                                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
171                                                                 {\r
172                                                                         UART0_THR = cChar;\r
173                                                                 }\r
174                                                                 else\r
175                                                                 {\r
176                                                                         /* There are no further characters \r
177                                                                         queued to send so we can indicate \r
178                                                                         that the THRE is available. */\r
179                                                                         lTHREEmpty = pdTRUE;\r
180                                                                 }\r
181                                                                 break;\r
182 \r
183                 case serSOURCE_RX_TIMEOUT :\r
184                 case serSOURCE_RX       :       /* A character was received.  Place it in \r
185                                                                 the queue of received characters. */\r
186                                                                 cChar = UART0_RBR;\r
187                                                                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
188                                                                 break;\r
189 \r
190                 default                         :       /* There is nothing to do, leave the ISR. */\r
191                                                                 break;\r
192         }\r
193 \r
194         if( xHigherPriorityTaskWoken )\r
195         {\r
196                 portYIELD_FROM_ISR();\r
197         }\r
198 \r
199         /* Clear the ISR in the VIC. */\r
200         VICVectAddr = serCLEAR_VIC_INTERRUPT;\r
201 }\r
202 \r
203 \r
204 \r
205 \r
206 \r
207 \r
208         \r