]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2106_GCC/serial/serialISR.c
ab6c444ee4fabd1cd8a53c9946a7f771afe2bc06
[freertos] / FreeRTOS / Demo / ARM7_LPC2106_GCC / serial / serialISR.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 \r
29 /* \r
30         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. \r
31 \r
32         This file contains all the serial port components that must be compiled\r
33         to ARM mode.  The components that can be compiled to either ARM or THUMB\r
34         mode are contained in serial.c.\r
35 \r
36 */\r
37 \r
38 /* Standard includes. */\r
39 #include <stdlib.h>\r
40 \r
41 /* Scheduler includes. */\r
42 #include "FreeRTOS.h"\r
43 #include "queue.h"\r
44 #include "task.h"\r
45 \r
46 /* Demo application includes. */\r
47 #include "serial.h"\r
48 \r
49 /*-----------------------------------------------------------*/\r
50 \r
51 /* Constant to access the VIC. */\r
52 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )\r
53 \r
54 /* Constants to determine the ISR source. */\r
55 #define serSOURCE_THRE                                  ( ( unsigned char ) 0x02 )\r
56 #define serSOURCE_RX_TIMEOUT                    ( ( unsigned char ) 0x0c )\r
57 #define serSOURCE_ERROR                                 ( ( unsigned char ) 0x06 )\r
58 #define serSOURCE_RX                                    ( ( unsigned char ) 0x04 )\r
59 #define serINTERRUPT_SOURCE_MASK                ( ( unsigned char ) 0x0f )\r
60 \r
61 /* Queues used to hold received characters, and characters waiting to be\r
62 transmitted. */\r
63 static QueueHandle_t xRxedChars; \r
64 static QueueHandle_t xCharsForTx; \r
65 static volatile long lTHREEmpty;\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 /* \r
70  * The queues are created in serialISR.c as they are used from the ISR.\r
71  * Obtain references to the queues and THRE Empty flag. \r
72  */\r
73 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx, long volatile **pplTHREEmptyFlag );\r
74 \r
75 /* UART0 interrupt service routine entry point. */\r
76 void vUART_ISR_Wrapper( void ) __attribute__ ((naked));\r
77 \r
78 /* UART0 interrupt service routine handler. */\r
79 void vUART_ISR_Handler( void ) __attribute__ ((noinline));\r
80 \r
81 /*-----------------------------------------------------------*/\r
82 void vSerialISRCreateQueues(    unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, \r
83                                                                 QueueHandle_t *pxCharsForTx, long 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 char ) );\r
87         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\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 = ( long ) pdTRUE;\r
96         *pplTHREEmptyFlag = &lTHREEmpty;\r
97 }\r
98 /*-----------------------------------------------------------*/\r
99 \r
100 void vUART_ISR_Wrapper( void )\r
101 {\r
102         /* Save the context of the interrupted task. */\r
103         portSAVE_CONTEXT();\r
104 \r
105         /* Call the handler.  This must be a separate function from the wrapper\r
106         to ensure the correct stack frame is set up. */\r
107         __asm volatile ("bl vUART_ISR_Handler");\r
108 \r
109         /* Restore the context of whichever task is going to run next. */\r
110         portRESTORE_CONTEXT();\r
111 }\r
112 /*-----------------------------------------------------------*/\r
113 \r
114 void vUART_ISR_Handler( void )\r
115 {\r
116 signed char cChar;\r
117 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
118 \r
119         /* What caused the interrupt? */\r
120         switch( UART0_IIR & serINTERRUPT_SOURCE_MASK )\r
121         {\r
122                 case serSOURCE_ERROR :  /* Not handling this, but clear the interrupt. */\r
123                                                                 cChar = UART0_LSR;\r
124                                                                 break;\r
125 \r
126                 case serSOURCE_THRE     :       /* The THRE is empty.  If there is another\r
127                                                                 character in the Tx queue, send it now. */\r
128                                                                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
129                                                                 {\r
130                                                                         UART0_THR = cChar;\r
131                                                                 }\r
132                                                                 else\r
133                                                                 {\r
134                                                                         /* There are no further characters \r
135                                                                         queued to send so we can indicate \r
136                                                                         that the THRE is available. */\r
137                                                                         lTHREEmpty = pdTRUE;\r
138                                                                 }\r
139                                                                 break;\r
140 \r
141                 case serSOURCE_RX_TIMEOUT :\r
142                 case serSOURCE_RX       :       /* A character was received.  Place it in \r
143                                                                 the queue of received characters. */\r
144                                                                 cChar = UART0_RBR;\r
145                                                                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
146                                                                 break;\r
147 \r
148                 default                         :       /* There is nothing to do, leave the ISR. */\r
149                                                                 break;\r
150         }\r
151 \r
152         if( xHigherPriorityTaskWoken )\r
153         {\r
154                 portYIELD_FROM_ISR();\r
155         }\r
156 \r
157         /* Clear the ISR in the VIC. */\r
158         VICVectAddr = serCLEAR_VIC_INTERRUPT;\r
159 }\r
160 \r
161 \r
162 \r
163 \r
164 \r
165 \r
166         \r