]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PPC405_FPU_Xilinx_Virtex4_GCC/RTOSDemo/serial/serial.c
922cded4bbda6efec1cf7389d366b1eebfcbe2fa
[freertos] / FreeRTOS / Demo / PPC405_FPU_Xilinx_Virtex4_GCC / RTOSDemo / serial / serial.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 \r
70 /* \r
71         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART\r
72 */\r
73 \r
74 /* Scheduler includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "queue.h"\r
77 #include "task.h"\r
78 \r
79 /* Demo application includes. */\r
80 #include "serial.h"\r
81 \r
82 /* Library includes. */\r
83 #include "xparameters.h"\r
84 #include "xuartlite.h"\r
85 #include "xuartlite_l.h"\r
86 \r
87 /*-----------------------------------------------------------*/\r
88 \r
89 /* Queues used to hold received characters, and characters waiting to be\r
90 transmitted. */\r
91 static xQueueHandle xRxedChars; \r
92 static xQueueHandle xCharsForTx; \r
93 \r
94 /* Structure that maintains information on the UART being used. */\r
95 static XUartLite xUART;\r
96 \r
97 /*\r
98  * Sample UART interrupt handler.  Note this is used to demonstrate the kernel\r
99  * features and test the port - it is not intended to represent an efficient\r
100  * implementation.\r
101  */\r
102 static void vSerialISR( XUartLite *pxUART );\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
107 {\r
108         /* NOTE: The baud rate used by this driver is determined by the hardware\r
109         parameterization of the UART Lite peripheral, and the baud value passed to\r
110         this function has no effect. */\r
111         ( void ) ulWantedBaud;\r
112 \r
113         /* Create the queues used to hold Rx and Tx characters. */\r
114         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
115         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
116 \r
117         /* Only initialise the UART if the queues were created correctly. */\r
118         if( ( xRxedChars != NULL ) && ( xCharsForTx != NULL ) )\r
119         {\r
120 \r
121                 XUartLite_Initialize( &xUART, XPAR_RS232_UART_DEVICE_ID );\r
122                 XUartLite_ResetFifos( &xUART );\r
123                 XUartLite_DisableInterrupt( &xUART );\r
124 \r
125                 if( xPortInstallInterruptHandler( XPAR_XPS_INTC_0_RS232_UART_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART ) == pdPASS )\r
126                 {\r
127                         /* xPortInstallInterruptHandler() could fail if \r
128                         vPortSetupInterruptController() has not been called prior to this \r
129                         function. */\r
130                         XUartLite_EnableInterrupt( &xUART );\r
131                 }\r
132         }\r
133         \r
134         /* There is only one port so the handle is not used. */\r
135         return ( xComPortHandle ) 0;\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
140 {\r
141         /* The port handle is not required as this driver only supports one UART. */\r
142         ( void ) pxPort;\r
143 \r
144         /* Get the next character from the buffer.  Return false if no characters\r
145         are available, or arrive before xBlockTime expires. */\r
146         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
147         {\r
148                 return pdTRUE;\r
149         }\r
150         else\r
151         {\r
152                 return pdFALSE;\r
153         }\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
158 {\r
159 portBASE_TYPE xReturn = pdTRUE;\r
160 \r
161         /* Just to remove compiler warning. */\r
162         ( void ) pxPort;\r
163 \r
164         portENTER_CRITICAL();\r
165         {\r
166                 /* If the UART FIFO is full we can block posting the new data on the\r
167                 Tx queue. */\r
168                 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_BASEADDR ) )\r
169                 {\r
170                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
171                         {\r
172                                 xReturn = pdFAIL;\r
173                         }\r
174                 }\r
175                 /* Otherwise, if there is data already in the queue we should add the\r
176                 new data to the back of the queue to ensure the sequencing is \r
177                 maintained. */\r
178                 else if( uxQueueMessagesWaiting( xCharsForTx ) )\r
179                 {\r
180                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
181                         {\r
182                                 xReturn = pdFAIL;\r
183                         }                       \r
184                 }\r
185                 /* If the UART FIFO is not full and there is no data already in the\r
186                 queue we can write directly to the FIFO without disrupting the \r
187                 sequence. */\r
188                 else\r
189                 {\r
190                         XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );\r
191                 }\r
192         }\r
193         portEXIT_CRITICAL();\r
194 \r
195         return xReturn;\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void vSerialClose( xComPortHandle xPort )\r
200 {\r
201         /* Not supported as not required by the demo application. */\r
202         ( void ) xPort;\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 static void vSerialISR( XUartLite *pxUART )\r
207 {\r
208 unsigned portLONG ulISRStatus;\r
209 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, lDidSomething;\r
210 portCHAR cChar;\r
211 \r
212         /* Just to remove compiler warning. */\r
213         ( void ) pxUART;\r
214 \r
215         do\r
216         {\r
217                 lDidSomething = pdFALSE;\r
218 \r
219                 ulISRStatus = XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_STATUS_REG_OFFSET );\r
220 \r
221                 if( ( ulISRStatus & XUL_SR_RX_FIFO_VALID_DATA ) != 0 )\r
222                 {\r
223                         /* A character is available - place it in the queue of received\r
224                         characters.  This might wake a task that was blocked waiting for \r
225                         data. */\r
226                         cChar = ( portCHAR ) XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_RX_FIFO_OFFSET );\r
227                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
228                         lDidSomething = pdTRUE;\r
229                 }\r
230                 \r
231                 if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )\r
232                 {\r
233                         /* There is space in the FIFO - if there are any characters queue for\r
234                         transmission they can be sent to the UART now.  This might unblock a\r
235                         task that was waiting for space to become available on the Tx queue. */\r
236                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
237                         {\r
238                                 XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );\r
239                                 lDidSomething = pdTRUE;\r
240                         }                       \r
241                 }\r
242         } while( lDidSomething == pdTRUE );\r
243 \r
244         /* If we woke any tasks we may require a context switch. */\r
245         if( xHigherPriorityTaskWoken )\r
246         {\r
247                 portYIELD_FROM_ISR();\r
248         }\r
249 }\r
250 \r
251 \r
252 \r