]> git.sur5r.net Git - freertos/blob - Demo/PPC440_DP_FPU_Xilinx_Virtex5_GCC/RTOSDemo/serial/serial.c
Update version number.
[freertos] / Demo / PPC440_DP_FPU_Xilinx_Virtex5_GCC / RTOSDemo / serial / serial.c
1 /*\r
2         FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 \r
53 /* \r
54         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART\r
55 */\r
56 \r
57 /* Scheduler includes. */\r
58 #include "FreeRTOS.h"\r
59 #include "queue.h"\r
60 #include "task.h"\r
61 \r
62 /* Demo application includes. */\r
63 #include "serial.h"\r
64 \r
65 /* Library includes. */\r
66 #include "xparameters.h"\r
67 #include "xuartlite.h"\r
68 #include "xuartlite_l.h"\r
69 \r
70 /*-----------------------------------------------------------*/\r
71 \r
72 /* Queues used to hold received characters, and characters waiting to be\r
73 transmitted. */\r
74 static xQueueHandle xRxedChars; \r
75 static xQueueHandle xCharsForTx; \r
76 \r
77 /* Structure that maintains information on the UART being used. */\r
78 static XUartLite xUART;\r
79 \r
80 /*\r
81  * Sample UART interrupt handler.  Note this is used to demonstrate the kernel\r
82  * features and test the port - it is not intended to represent an efficient\r
83  * implementation.\r
84  */\r
85 static void vSerialISR( XUartLite *pxUART );\r
86 \r
87 /*-----------------------------------------------------------*/\r
88 \r
89 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
90 {\r
91         /* NOTE: The baud rate used by this driver is determined by the hardware\r
92         parameterization of the UART Lite peripheral, and the baud value passed to\r
93         this function has no effect. */\r
94         ( void ) ulWantedBaud;\r
95 \r
96         /* Create the queues used to hold Rx and Tx characters. */\r
97         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
98         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
99 \r
100         /* Only initialise the UART if the queues were created correctly. */\r
101         if( ( xRxedChars != NULL ) && ( xCharsForTx != NULL ) )\r
102         {\r
103 \r
104                 XUartLite_Initialize( &xUART, XPAR_RS232_UART_1_DEVICE_ID );\r
105                 XUartLite_ResetFifos( &xUART );\r
106                 XUartLite_DisableInterrupt( &xUART );\r
107 \r
108                 if( xPortInstallInterruptHandler( XPAR_XPS_INTC_0_RS232_UART_1_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART ) == pdPASS )\r
109                 {\r
110                         /* xPortInstallInterruptHandler() could fail if \r
111                         vPortSetupInterruptController() has not been called prior to this \r
112                         function. */\r
113                         XUartLite_EnableInterrupt( &xUART );\r
114                 }\r
115         }\r
116         \r
117         /* There is only one port so the handle is not used. */\r
118         return ( xComPortHandle ) 0;\r
119 }\r
120 /*-----------------------------------------------------------*/\r
121 \r
122 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
123 {\r
124         /* The port handle is not required as this driver only supports one UART. */\r
125         ( void ) pxPort;\r
126 \r
127         /* Get the next character from the buffer.  Return false if no characters\r
128         are available, or arrive before xBlockTime expires. */\r
129         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
130         {\r
131                 return pdTRUE;\r
132         }\r
133         else\r
134         {\r
135                 return pdFALSE;\r
136         }\r
137 }\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
141 {\r
142 portBASE_TYPE xReturn = pdTRUE;\r
143 \r
144         /* Just to remove compiler warning. */\r
145         ( void ) pxPort;\r
146 \r
147         portENTER_CRITICAL();\r
148         {\r
149                 /* If the UART FIFO is full we can block posting the new data on the\r
150                 Tx queue. */\r
151                 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_1_BASEADDR ) )\r
152                 {\r
153                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
154                         {\r
155                                 xReturn = pdFAIL;\r
156                         }\r
157                 }\r
158                 /* Otherwise, if there is data already in the queue we should add the\r
159                 new data to the back of the queue to ensure the sequencing is \r
160                 maintained. */\r
161                 else if( uxQueueMessagesWaiting( xCharsForTx ) )\r
162                 {\r
163                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
164                         {\r
165                                 xReturn = pdFAIL;\r
166                         }                       \r
167                 }\r
168                 /* If the UART FIFO is not full and there is no data already in the\r
169                 queue we can write directly to the FIFO without disrupting the \r
170                 sequence. */\r
171                 else\r
172                 {\r
173                         XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );\r
174                 }\r
175         }\r
176         portEXIT_CRITICAL();\r
177 \r
178         return xReturn;\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void vSerialClose( xComPortHandle xPort )\r
183 {\r
184         /* Not supported as not required by the demo application. */\r
185         ( void ) xPort;\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 static void vSerialISR( XUartLite *pxUART )\r
190 {\r
191 unsigned portLONG ulISRStatus;\r
192 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, lDidSomething;\r
193 portCHAR cChar;\r
194 \r
195         /* Just to remove compiler warning. */\r
196         ( void ) pxUART;\r
197 \r
198         do\r
199         {\r
200                 lDidSomething = pdFALSE;\r
201 \r
202                 ulISRStatus = XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_STATUS_REG_OFFSET );\r
203 \r
204                 if( ( ulISRStatus & XUL_SR_RX_FIFO_VALID_DATA ) != 0 )\r
205                 {\r
206                         /* A character is available - place it in the queue of received\r
207                         characters.  This might wake a task that was blocked waiting for \r
208                         data. */\r
209                         cChar = ( portCHAR ) XIo_In32( XPAR_RS232_UART_1_BASEADDR + XUL_RX_FIFO_OFFSET );\r
210                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
211                         lDidSomething = pdTRUE;\r
212                 }\r
213                 \r
214                 if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )\r
215                 {\r
216                         /* There is space in the FIFO - if there are any characters queue for\r
217                         transmission they can be sent to the UART now.  This might unblock a\r
218                         task that was waiting for space to become available on the Tx queue. */\r
219                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
220                         {\r
221                                 XIo_Out32( XPAR_RS232_UART_1_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );\r
222                                 lDidSomething = pdTRUE;\r
223                         }                       \r
224                 }\r
225         } while( lDidSomething == pdTRUE );\r
226 \r
227         /* If we woke any tasks we may require a context switch. */\r
228         if( xHigherPriorityTaskWoken )\r
229         {\r
230                 portYIELD_FROM_ISR();\r
231         }\r
232 }\r
233 \r
234 \r
235 \r