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