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