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