]> git.sur5r.net Git - freertos/blob - Demo/PPC405_Xilinx_Virtex4_GCC/RTOSDemo/serial/serial.c
PPC405 work in progress.
[freertos] / Demo / PPC405_Xilinx_Virtex4_GCC / RTOSDemo / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.7.2 - 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         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 \r
44 /* \r
45         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART\r
46 */\r
47 \r
48 /* Scheduler includes. */\r
49 #include "FreeRTOS.h"\r
50 #include "queue.h"\r
51 #include "task.h"\r
52 \r
53 /* Demo application includes. */\r
54 #include "serial.h"\r
55 \r
56 /* Microblaze driver includes. */\r
57 #include "xparameters.h"\r
58 #include "xuartlite.h"\r
59 #include "xuartlite_l.h"\r
60 #include "xintc_l.h"\r
61 #include "xintc.h"\r
62 \r
63 /*-----------------------------------------------------------*/\r
64 \r
65 /* Queues used to hold received characters, and characters waiting to be\r
66 transmitted. */\r
67 static xQueueHandle xRxedChars; \r
68 static xQueueHandle xCharsForTx; \r
69 \r
70 static XUartLite xUART;\r
71 \r
72 static void vSerialISR( XUartLite *pxUART );\r
73 \r
74 /*-----------------------------------------------------------*/\r
75 \r
76 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
77 {\r
78 unsigned portLONG ulControlReg, ulMask;\r
79 extern XIntc xInterruptController;\r
80 \r
81         /* NOTE: The baud rate used by this driver is determined by the hardware\r
82         parameterization of the UART Lite peripheral, and the baud value passed to\r
83         this function has no effect. */\r
84 \r
85         /* Create the queues used to hold Rx and Tx characters. */\r
86         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
87         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
88 \r
89         if( ( xRxedChars ) && ( xCharsForTx ) )\r
90         {\r
91 \r
92                 XUartLite_Initialize( &xUART, XPAR_RS232_UART_DEVICE_ID );\r
93                 XUartLite_ResetFifos( &xUART );\r
94                 XUartLite_DisableInterrupt( &xUART );\r
95                 XIntc_Connect( &xInterruptController, XPAR_OPB_INTC_0_RS232_UART_INTERRUPT_INTR, ( XInterruptHandler )vSerialISR, (void *)&xUART );\r
96                 XIntc_Enable( &xInterruptController, XPAR_OPB_INTC_0_RS232_UART_INTERRUPT_INTR );\r
97                 \r
98                 XUartLite_EnableInterrupt( &xUART );\r
99         }\r
100         \r
101         return ( xComPortHandle ) 0;\r
102 }\r
103 /*-----------------------------------------------------------*/\r
104 \r
105 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
106 {\r
107         /* The port handle is not required as this driver only supports one UART. */\r
108         ( void ) pxPort;\r
109 \r
110         /* Get the next character from the buffer.  Return false if no characters\r
111         are available, or arrive before xBlockTime expires. */\r
112         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
113         {\r
114                 return pdTRUE;\r
115         }\r
116         else\r
117         {\r
118                 return pdFALSE;\r
119         }\r
120 }\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
124 {\r
125 portBASE_TYPE xReturn = pdTRUE;\r
126 \r
127         portENTER_CRITICAL();\r
128         {\r
129                 /* If the UART FIFO is full we can block posting the new data on the\r
130                 Tx queue. */\r
131                 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_BASEADDR ) )\r
132                 {\r
133                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
134                         {\r
135                                 xReturn = pdFAIL;\r
136                         }\r
137                 }\r
138                 /* Otherwise, if there is data already in the queue we should add the\r
139                 new data to the back of the queue to ensure the sequencing is \r
140                 maintained. */\r
141                 else if( uxQueueMessagesWaiting( xCharsForTx ) )\r
142                 {\r
143                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
144                         {\r
145                                 xReturn = pdFAIL;\r
146                         }                       \r
147                 }\r
148                 /* If the UART FIFO is not full and there is no data already in the\r
149                 queue we can write directly to the FIFO without disrupting the \r
150                 sequence. */\r
151                 else\r
152                 {\r
153                         XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );\r
154                 }\r
155         }\r
156         portEXIT_CRITICAL();\r
157 \r
158         return xReturn;\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 void vSerialClose( xComPortHandle xPort )\r
163 {\r
164         /* Not supported as not required by the demo application. */\r
165         ( void ) xPort;\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 static void vSerialISR( XUartLite *pxUART )\r
170 {\r
171 unsigned portLONG ulISRStatus;\r
172 portBASE_TYPE xTaskWokenByTx = pdFALSE, xTaskWokenByRx = pdFALSE;\r
173 portCHAR cChar;\r
174 \r
175     ulISRStatus = XIo_In32( pxUART->RegBaseAddress + XUL_STATUS_REG_OFFSET );\r
176 \r
177     if( ( ulISRStatus & (XUL_SR_RX_FIFO_FULL | XUL_SR_RX_FIFO_VALID_DATA ) ) != 0 )\r
178     {\r
179                 /* A character is available - place it in the queue of received\r
180                 characters.  This might wake a task that was blocked waiting for \r
181                 data. */\r
182                 cChar = ( portCHAR ) XIo_In32( pxUART->RegBaseAddress + XUL_RX_FIFO_OFFSET );\r
183                 xTaskWokenByRx = xQueueSendFromISR( xRxedChars, &cChar, xTaskWokenByRx );\r
184     }\r
185 \r
186     if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )\r
187     {\r
188                 /* There is space in the FIFO - if there are any characters queue for\r
189                 transmission they can be send to the UART now.  This might unblock a\r
190                 task that was waiting for space to become available on the Tx queue. */\r
191                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWokenByTx ) == pdTRUE )\r
192                 {\r
193                         XIo_Out32( pxUART->RegBaseAddress + XUL_TX_FIFO_OFFSET, cChar );\r
194                 }\r
195 \r
196     }\r
197 \r
198         /* If we woke any tasks we may require a context switch. */\r
199         if( xTaskWokenByTx || xTaskWokenByRx )\r
200         {\r
201                 portYIELD_FROM_ISR();\r
202         }\r
203 }\r
204 \r
205 \r
206 \r