]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2106_GCC/serial/serialISR.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / ARM7_LPC2106_GCC / serial / serialISR.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /* \r
31         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
32 \r
33         This file contains all the serial port components that must be compiled\r
34         to ARM mode.  The components that can be compiled to either ARM or THUMB\r
35         mode are contained in serial.c.\r
36 \r
37 */\r
38 \r
39 /* Standard includes. */\r
40 #include <stdlib.h>\r
41 \r
42 /* Scheduler includes. */\r
43 #include "FreeRTOS.h"\r
44 #include "queue.h"\r
45 #include "task.h"\r
46 \r
47 /* Demo application includes. */\r
48 #include "serial.h"\r
49 \r
50 /*-----------------------------------------------------------*/\r
51 \r
52 /* Constant to access the VIC. */\r
53 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )\r
54 \r
55 /* Constants to determine the ISR source. */\r
56 #define serSOURCE_THRE                                  ( ( unsigned char ) 0x02 )\r
57 #define serSOURCE_RX_TIMEOUT                    ( ( unsigned char ) 0x0c )\r
58 #define serSOURCE_ERROR                                 ( ( unsigned char ) 0x06 )\r
59 #define serSOURCE_RX                                    ( ( unsigned char ) 0x04 )\r
60 #define serINTERRUPT_SOURCE_MASK                ( ( unsigned char ) 0x0f )\r
61 \r
62 /* Queues used to hold received characters, and characters waiting to be\r
63 transmitted. */\r
64 static QueueHandle_t xRxedChars; \r
65 static QueueHandle_t xCharsForTx; \r
66 static volatile long lTHREEmpty;\r
67 \r
68 /*-----------------------------------------------------------*/\r
69 \r
70 /* \r
71  * The queues are created in serialISR.c as they are used from the ISR.\r
72  * Obtain references to the queues and THRE Empty flag. \r
73  */\r
74 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx, long volatile **pplTHREEmptyFlag );\r
75 \r
76 /* UART0 interrupt service routine entry point. */\r
77 void vUART_ISR_Wrapper( void ) __attribute__ ((naked));\r
78 \r
79 /* UART0 interrupt service routine handler. */\r
80 void vUART_ISR_Handler( void ) __attribute__ ((noinline));\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 void vSerialISRCreateQueues(    unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, \r
84                                                                 QueueHandle_t *pxCharsForTx, long volatile **pplTHREEmptyFlag )\r
85 {\r
86         /* Create the queues used to hold Rx and Tx characters. */\r
87         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
88         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
89 \r
90         /* Pass back a reference to the queues so the serial API file can \r
91         post/receive characters. */\r
92         *pxRxedChars = xRxedChars;\r
93         *pxCharsForTx = xCharsForTx;\r
94 \r
95         /* Initialise the THRE empty flag - and pass back a reference. */\r
96         lTHREEmpty = ( long ) pdTRUE;\r
97         *pplTHREEmptyFlag = &lTHREEmpty;\r
98 }\r
99 /*-----------------------------------------------------------*/\r
100 \r
101 void vUART_ISR_Wrapper( void )\r
102 {\r
103         /* Save the context of the interrupted task. */\r
104         portSAVE_CONTEXT();\r
105 \r
106         /* Call the handler.  This must be a separate function from the wrapper\r
107         to ensure the correct stack frame is set up. */\r
108         __asm volatile ("bl vUART_ISR_Handler");\r
109 \r
110         /* Restore the context of whichever task is going to run next. */\r
111         portRESTORE_CONTEXT();\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 void vUART_ISR_Handler( void )\r
116 {\r
117 signed char cChar;\r
118 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
119 \r
120         /* What caused the interrupt? */\r
121         switch( UART0_IIR & serINTERRUPT_SOURCE_MASK )\r
122         {\r
123                 case serSOURCE_ERROR :  /* Not handling this, but clear the interrupt. */\r
124                                                                 cChar = UART0_LSR;\r
125                                                                 break;\r
126 \r
127                 case serSOURCE_THRE     :       /* The THRE is empty.  If there is another\r
128                                                                 character in the Tx queue, send it now. */\r
129                                                                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
130                                                                 {\r
131                                                                         UART0_THR = cChar;\r
132                                                                 }\r
133                                                                 else\r
134                                                                 {\r
135                                                                         /* There are no further characters \r
136                                                                         queued to send so we can indicate \r
137                                                                         that the THRE is available. */\r
138                                                                         lTHREEmpty = pdTRUE;\r
139                                                                 }\r
140                                                                 break;\r
141 \r
142                 case serSOURCE_RX_TIMEOUT :\r
143                 case serSOURCE_RX       :       /* A character was received.  Place it in \r
144                                                                 the queue of received characters. */\r
145                                                                 cChar = UART0_RBR;\r
146                                                                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
147                                                                 break;\r
148 \r
149                 default                         :       /* There is nothing to do, leave the ISR. */\r
150                                                                 break;\r
151         }\r
152 \r
153         if( xHigherPriorityTaskWoken )\r
154         {\r
155                 portYIELD_FROM_ISR();\r
156         }\r
157 \r
158         /* Clear the ISR in the VIC. */\r
159         VICVectAddr = serCLEAR_VIC_INTERRUPT;\r
160 }\r
161 \r
162 \r
163 \r
164 \r
165 \r
166 \r
167         \r