]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PPC440_Xilinx_Virtex5_GCC/RTOSDemo/serial/serial.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / PPC440_Xilinx_Virtex5_GCC / RTOSDemo / serial / 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 \r
30 /* \r
31         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART\r
32 */\r
33 \r
34 /* Scheduler includes. */\r
35 #include "FreeRTOS.h"\r
36 #include "queue.h"\r
37 #include "task.h"\r
38 \r
39 /* Demo application includes. */\r
40 #include "serial.h"\r
41 \r
42 /* Library includes. */\r
43 #include "xparameters.h"\r
44 #include "xuartlite.h"\r
45 #include "xuartlite_l.h"\r
46 \r
47 /*-----------------------------------------------------------*/\r
48 \r
49 /* Queues used to hold received characters, and characters waiting to be\r
50 transmitted. */\r
51 static QueueHandle_t xRxedChars; \r
52 static QueueHandle_t xCharsForTx; \r
53 \r
54 /* Structure that maintains information on the UART being used. */\r
55 static XUartLite xUART;\r
56 \r
57 /*\r
58  * Sample UART interrupt handler.  Note this is used to demonstrate the kernel\r
59  * features and test the port - it is not intended to represent an efficient\r
60  * implementation.\r
61  */\r
62 static void vSerialISR( XUartLite *pxUART );\r
63 \r
64 /*-----------------------------------------------------------*/\r
65 \r
66 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
67 {\r
68         /* NOTE: The baud rate used by this driver is determined by the hardware\r
69         parameterization of the UART Lite peripheral, and the baud value passed to\r
70         this function has no effect. */\r
71         ( void ) ulWantedBaud;\r
72 \r
73         /* Create the queues used to hold Rx and Tx characters. */\r
74         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
75         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
76 \r
77         /* Only initialise the UART if the queues were created correctly. */\r
78         if( ( xRxedChars != NULL ) && ( xCharsForTx != NULL ) )\r
79         {\r
80 \r
81                 XUartLite_Initialize( &xUART, XPAR_RS232_UART_1_DEVICE_ID );\r
82                 XUartLite_ResetFifos( &xUART );\r
83                 XUartLite_DisableInterrupt( &xUART );\r
84 \r
85                 if( xPortInstallInterruptHandler( XPAR_XPS_INTC_0_RS232_UART_1_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART ) == pdPASS )\r
86                 {\r
87                         /* xPortInstallInterruptHandler() could fail if \r
88                         vPortSetupInterruptController() has not been called prior to this \r
89                         function. */\r
90                         XUartLite_EnableInterrupt( &xUART );\r
91                 }\r
92         }\r
93         \r
94         /* There is only one port so the handle is not used. */\r
95         return ( xComPortHandle ) 0;\r
96 }\r
97 /*-----------------------------------------------------------*/\r
98 \r
99 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
100 {\r
101         /* The port handle is not required as this driver only supports one UART. */\r
102         ( void ) pxPort;\r
103 \r
104         /* Get the next character from the buffer.  Return false if no characters\r
105         are available, or arrive before xBlockTime expires. */\r
106         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
107         {\r
108                 return pdTRUE;\r
109         }\r
110         else\r
111         {\r
112                 return pdFALSE;\r
113         }\r
114 }\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
118 {\r
119 portBASE_TYPE xReturn = pdTRUE;\r
120 \r
121         /* Just to remove compiler warning. */\r
122         ( void ) pxPort;\r
123 \r
124         portENTER_CRITICAL();\r
125         {\r
126                 /* If the UART FIFO is full we can block posting the new data on the\r
127                 Tx queue. */\r
128                 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_1_BASEADDR ) )\r
129                 {\r
130                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
131                         {\r
132                                 xReturn = pdFAIL;\r
133                         }\r
134                 }\r
135                 /* Otherwise, if there is data already in the queue we should add the\r
136                 new data to the back of the queue to ensure the sequencing is \r
137                 maintained. */\r
138                 else if( uxQueueMessagesWaiting( xCharsForTx ) )\r
139                 {\r
140                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
141                         {\r
142                                 xReturn = pdFAIL;\r
143                         }                       \r
144                 }\r
145                 /* If the UART FIFO is not full and there is no data already in the\r
146                 queue we can write directly to the FIFO without disrupting the \r
147                 sequence. */\r
148                 else\r
149                 {\r
150                         XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );\r
151                 }\r
152         }\r
153         portEXIT_CRITICAL();\r
154 \r
155         return xReturn;\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 void vSerialClose( xComPortHandle xPort )\r
160 {\r
161         /* Not supported as not required by the demo application. */\r
162         ( void ) xPort;\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static void vSerialISR( XUartLite *pxUART )\r
167 {\r
168 unsigned long ulISRStatus;\r
169 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, lDidSomething;\r
170 char cChar;\r
171 \r
172         /* Just to remove compiler warning. */\r
173         ( void ) pxUART;\r
174 \r
175         do\r
176         {\r
177                 lDidSomething = pdFALSE;\r
178 \r
179                 ulISRStatus = XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_STATUS_REG_OFFSET );\r
180 \r
181                 if( ( ulISRStatus & XUL_SR_RX_FIFO_VALID_DATA ) != 0 )\r
182                 {\r
183                         /* A character is available - place it in the queue of received\r
184                         characters.  This might wake a task that was blocked waiting for \r
185                         data. */\r
186                         cChar = ( char ) XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_RX_FIFO_OFFSET );\r
187                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
188                         lDidSomething = pdTRUE;\r
189                 }\r
190                 \r
191                 if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )\r
192                 {\r
193                         /* There is space in the FIFO - if there are any characters queue for\r
194                         transmission they can be sent to the UART now.  This might unblock a\r
195                         task that was waiting for space to become available on the Tx queue. */\r
196                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
197                         {\r
198                                 XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );\r
199                                 lDidSomething = pdTRUE;\r
200                         }                       \r
201                 }\r
202         } while( lDidSomething == pdTRUE );\r
203 \r
204         /* If we woke any tasks we may require a context switch. */\r
205         if( xHigherPriorityTaskWoken )\r
206         {\r
207                 portYIELD_FROM_ISR();\r
208         }\r
209 }\r
210 \r
211 \r
212 \r