]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTUS_APS3_GCC/Demo/serial.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTUS_APS3_GCC / Demo / serial.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 /*  Basic interrupt driven serial port driver for uart1.\r
30 */\r
31 \r
32 /* Standard includes. */\r
33 #include <stdlib.h>\r
34 \r
35 /* Kernel includes. */\r
36 #include "FreeRTOS.h"\r
37 #include "queue.h"\r
38 #include "task.h"\r
39 \r
40 /* Demo application includes. */\r
41 #include <machine/uart.h>\r
42 #include <machine/ic.h>\r
43 #include "serial.h"\r
44 /*-----------------------------------------------------------*/\r
45 \r
46 #define comBLOCK_RETRY_TIME                             10\r
47 /*-----------------------------------------------------------*/\r
48 \r
49 /* The interrupt handlers are naked functions that call C handlers.  The C\r
50 handlers are marked as noinline to ensure they work correctly when the\r
51 optimiser is on. */\r
52 void interrupt5_handler( void ) __attribute__((naked));\r
53 static void prvTxHandler( void ) __attribute__((noinline));\r
54 void interrupt6_handler( void ) __attribute__((naked));\r
55 static void prvRxHandler( void ) __attribute__((noinline));\r
56 \r
57 /*-----------------------------------------------------------*/\r
58 \r
59 /* Queues used to hold received characters, and characters waiting to be\r
60 transmitted. */\r
61 static QueueHandle_t xRxedChars;\r
62 static QueueHandle_t xCharsForTx;\r
63 /*-----------------------------------------------------------*/\r
64 \r
65 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
66 {\r
67         /* Create the queues used to hold Rx and Tx characters. */\r
68         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
69         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
70 \r
71         if( ( xRxedChars ) && ( xCharsForTx ) )\r
72         {\r
73                 /* Set up interrupts */\r
74                 /* tx interrupt will be enabled when we need to send something */\r
75                 uart1->tx_mask = 0;\r
76                 uart1->rx_mask = 1;\r
77                 irq[IRQ_UART1_TX].ien = 1;\r
78                 irq[IRQ_UART1_RX].ien = 1;\r
79         }\r
80 \r
81         return ( xComPortHandle ) 0;\r
82 }\r
83 /*-----------------------------------------------------------*/\r
84 \r
85 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
86 {\r
87         /* The port handle is not required as this driver only supports uart1. */\r
88         (void) pxPort;\r
89 \r
90         /* Get the next character from the buffer.      Return false if no characters\r
91            are available, or arrive before xBlockTime expires. */\r
92         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
93         {\r
94                 return pdTRUE;\r
95         }\r
96         else\r
97         {\r
98                 return pdFALSE;\r
99         }\r
100 }\r
101 /*-----------------------------------------------------------*/\r
102 \r
103 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
104 {\r
105         int i;\r
106         signed char *pChNext;\r
107 \r
108         /* Send each character in the string, one at a time. */\r
109         pChNext = ( signed char * )pcString;\r
110         for( i = 0; i < usStringLength; i++ )\r
111         {\r
112                 /* Block until character has been transmitted. */\r
113                 while( xSerialPutChar( pxPort, *pChNext, comBLOCK_RETRY_TIME ) != pdTRUE ); pChNext++;\r
114         }\r
115 }\r
116 /*-----------------------------------------------------------*/\r
117 \r
118 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
119 {\r
120         ( void ) pxPort;\r
121 \r
122         /* Place the character in the queue of characters to be transmitted. */\r
123         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
124         {\r
125                 return pdFAIL;\r
126         }\r
127 \r
128         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
129            queue and send it. This does not need to be in a critical section as\r
130            if the interrupt has already removed the character the next interrupt\r
131            will simply turn off the Tx interrupt again. */\r
132         uart1->tx_mask = 1;\r
133 \r
134         return pdPASS;\r
135 }\r
136 /*-----------------------------------------------------------*/\r
137 \r
138 void vSerialClose( xComPortHandle xPort )\r
139 {\r
140         /* Not supported as not required by the demo application. */\r
141         ( void ) xPort;\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 /* UART Tx interrupt handler. */\r
146 void interrupt5_handler( void )\r
147 {\r
148         /* This is a naked function. */\r
149         portSAVE_CONTEXT();\r
150         prvTxHandler();\r
151         portRESTORE_CONTEXT();\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 static void prvTxHandler( void )\r
156 {\r
157 signed char cChar;\r
158 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
159 \r
160         /* The interrupt was caused by the transmit fifo having space for at least one\r
161         character. Are there any more characters to transmit? */\r
162         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
163         {\r
164                 /* A character was retrieved from the queue so can be sent to the uart now. */\r
165                 uart1->tx_data = cChar;\r
166         }\r
167         else\r
168         {\r
169                 /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
170                 uart1->tx_mask = 0;\r
171         }\r
172 \r
173         /* If an event caused a task to unblock then we call "Yield from ISR" to\r
174         ensure that the unblocked task is the task that executes when the interrupt\r
175         completes if the unblocked task has a priority higher than the interrupted\r
176         task. */\r
177         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 /* UART Rx interrupt. */\r
182 void interrupt6_handler( void )\r
183 {\r
184         portSAVE_CONTEXT();\r
185         prvRxHandler();\r
186         portRESTORE_CONTEXT();\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 static void prvRxHandler( void )\r
191 {\r
192 signed char cChar;\r
193 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
194 \r
195         /* The interrupt was caused by the receiver getting data. */\r
196         cChar = uart1->rx_data;\r
197 \r
198         xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
199 \r
200         /* If an event caused a task to unblock then we call "Yield from ISR" to\r
201         ensure that the unblocked task is the task that executes when the interrupt\r
202         completes if the unblocked task has a priority higher than the interrupted\r
203         task. */\r
204         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 \r