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