]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PPC440_SP_FPU_Xilinx_Virtex5_GCC/RTOSDemo/serial/serial.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / PPC440_SP_FPU_Xilinx_Virtex5_GCC / RTOSDemo / serial / serial.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /* \r
72         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART\r
73 */\r
74 \r
75 /* Scheduler includes. */\r
76 #include "FreeRTOS.h"\r
77 #include "queue.h"\r
78 #include "task.h"\r
79 \r
80 /* Demo application includes. */\r
81 #include "serial.h"\r
82 \r
83 /* Library includes. */\r
84 #include "xparameters.h"\r
85 #include "xuartlite.h"\r
86 #include "xuartlite_l.h"\r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 /* Queues used to hold received characters, and characters waiting to be\r
91 transmitted. */\r
92 static QueueHandle_t xRxedChars; \r
93 static QueueHandle_t xCharsForTx; \r
94 \r
95 /* Structure that maintains information on the UART being used. */\r
96 static XUartLite xUART;\r
97 \r
98 /*\r
99  * Sample UART interrupt handler.  Note this is used to demonstrate the kernel\r
100  * features and test the port - it is not intended to represent an efficient\r
101  * implementation.\r
102  */\r
103 static void vSerialISR( XUartLite *pxUART );\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
108 {\r
109         /* NOTE: The baud rate used by this driver is determined by the hardware\r
110         parameterization of the UART Lite peripheral, and the baud value passed to\r
111         this function has no effect. */\r
112         ( void ) ulWantedBaud;\r
113 \r
114         /* Create the queues used to hold Rx and Tx characters. */\r
115         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
116         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
117 \r
118         /* Only initialise the UART if the queues were created correctly. */\r
119         if( ( xRxedChars != NULL ) && ( xCharsForTx != NULL ) )\r
120         {\r
121 \r
122                 XUartLite_Initialize( &xUART, XPAR_RS232_UART_1_DEVICE_ID );\r
123                 XUartLite_ResetFifos( &xUART );\r
124                 XUartLite_DisableInterrupt( &xUART );\r
125 \r
126                 if( xPortInstallInterruptHandler( XPAR_XPS_INTC_0_RS232_UART_1_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART ) == pdPASS )\r
127                 {\r
128                         /* xPortInstallInterruptHandler() could fail if \r
129                         vPortSetupInterruptController() has not been called prior to this \r
130                         function. */\r
131                         XUartLite_EnableInterrupt( &xUART );\r
132                 }\r
133         }\r
134         \r
135         /* There is only one port so the handle is not used. */\r
136         return ( xComPortHandle ) 0;\r
137 }\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
141 {\r
142         /* The port handle is not required as this driver only supports one UART. */\r
143         ( void ) pxPort;\r
144 \r
145         /* Get the next character from the buffer.  Return false if no characters\r
146         are available, or arrive before xBlockTime expires. */\r
147         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
148         {\r
149                 return pdTRUE;\r
150         }\r
151         else\r
152         {\r
153                 return pdFALSE;\r
154         }\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
159 {\r
160 portBASE_TYPE xReturn = pdTRUE;\r
161 \r
162         /* Just to remove compiler warning. */\r
163         ( void ) pxPort;\r
164 \r
165         portENTER_CRITICAL();\r
166         {\r
167                 /* If the UART FIFO is full we can block posting the new data on the\r
168                 Tx queue. */\r
169                 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_1_BASEADDR ) )\r
170                 {\r
171                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
172                         {\r
173                                 xReturn = pdFAIL;\r
174                         }\r
175                 }\r
176                 /* Otherwise, if there is data already in the queue we should add the\r
177                 new data to the back of the queue to ensure the sequencing is \r
178                 maintained. */\r
179                 else if( uxQueueMessagesWaiting( xCharsForTx ) )\r
180                 {\r
181                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
182                         {\r
183                                 xReturn = pdFAIL;\r
184                         }                       \r
185                 }\r
186                 /* If the UART FIFO is not full and there is no data already in the\r
187                 queue we can write directly to the FIFO without disrupting the \r
188                 sequence. */\r
189                 else\r
190                 {\r
191                         XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );\r
192                 }\r
193         }\r
194         portEXIT_CRITICAL();\r
195 \r
196         return xReturn;\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 void vSerialClose( xComPortHandle xPort )\r
201 {\r
202         /* Not supported as not required by the demo application. */\r
203         ( void ) xPort;\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 static void vSerialISR( XUartLite *pxUART )\r
208 {\r
209 unsigned long ulISRStatus;\r
210 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, lDidSomething;\r
211 char cChar;\r
212 \r
213         /* Just to remove compiler warning. */\r
214         ( void ) pxUART;\r
215 \r
216         do\r
217         {\r
218                 lDidSomething = pdFALSE;\r
219 \r
220                 ulISRStatus = XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_STATUS_REG_OFFSET );\r
221 \r
222                 if( ( ulISRStatus & XUL_SR_RX_FIFO_VALID_DATA ) != 0 )\r
223                 {\r
224                         /* A character is available - place it in the queue of received\r
225                         characters.  This might wake a task that was blocked waiting for \r
226                         data. */\r
227                         cChar = ( char ) XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_RX_FIFO_OFFSET );\r
228                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
229                         lDidSomething = pdTRUE;\r
230                 }\r
231                 \r
232                 if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )\r
233                 {\r
234                         /* There is space in the FIFO - if there are any characters queue for\r
235                         transmission they can be sent to the UART now.  This might unblock a\r
236                         task that was waiting for space to become available on the Tx queue. */\r
237                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
238                         {\r
239                                 XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );\r
240                                 lDidSomething = pdTRUE;\r
241                         }                       \r
242                 }\r
243         } while( lDidSomething == pdTRUE );\r
244 \r
245         /* If we woke any tasks we may require a context switch. */\r
246         if( xHigherPriorityTaskWoken )\r
247         {\r
248                 portYIELD_FROM_ISR();\r
249         }\r
250 }\r
251 \r
252 \r
253 \r