]> git.sur5r.net Git - freertos/blob - Demo/MicroBlaze/serial/serial.c
Update version numbers to V4.8.0
[freertos] / Demo / MicroBlaze / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.8.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!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\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 /* Microblaze driver includes. */\r
63 #include "xuartlite_l.h"\r
64 #include "xintc_l.h"\r
65 \r
66 /*-----------------------------------------------------------*/\r
67 \r
68 /* Queues used to hold received characters, and characters waiting to be\r
69 transmitted. */\r
70 static xQueueHandle xRxedChars; \r
71 static xQueueHandle xCharsForTx; \r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
76 {\r
77 unsigned portLONG ulControlReg, ulMask;\r
78 \r
79         /* NOTE: The baud rate used by this driver is determined by the hardware\r
80         parameterization of the UART Lite peripheral, and the baud value passed to\r
81         this function has no effect. */\r
82 \r
83         /* Create the queues used to hold Rx and Tx characters. */\r
84         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
85         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
86 \r
87         if( ( xRxedChars ) && ( xCharsForTx ) )\r
88         {\r
89                 /* Disable the interrupt. */\r
90                 XUartLite_mDisableIntr( XPAR_RS232_UART_BASEADDR );\r
91                 \r
92                 /* Flush the fifos. */\r
93                 ulControlReg = XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_STATUS_REG_OFFSET );\r
94                 XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_CONTROL_REG_OFFSET, ulControlReg | XUL_CR_FIFO_TX_RESET | XUL_CR_FIFO_RX_RESET );\r
95 \r
96                 /* Enable the interrupt again.  The interrupt controller has not yet been \r
97                 initialised so there is no chance of receiving an interrupt until the \r
98                 scheduler has been started. */\r
99                 XUartLite_mEnableIntr( XPAR_RS232_UART_BASEADDR );\r
100 \r
101                 /* Enable the interrupt in the interrupt controller while maintaining \r
102                 all the other bit settings. */\r
103                 ulMask = XIntc_In32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ) );\r
104                 ulMask |= XPAR_RS232_UART_INTERRUPT_MASK;\r
105                 XIntc_Out32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ), ( ulMask ) );\r
106                 XIntc_mAckIntr( XPAR_INTC_SINGLE_BASEADDR, 2 );\r
107         }\r
108         \r
109         return ( xComPortHandle ) 0;\r
110 }\r
111 /*-----------------------------------------------------------*/\r
112 \r
113 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
114 {\r
115         /* The port handle is not required as this driver only supports one UART. */\r
116         ( void ) pxPort;\r
117 \r
118         /* Get the next character from the buffer.  Return false if no characters\r
119         are available, or arrive before xBlockTime expires. */\r
120         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
121         {\r
122                 return pdTRUE;\r
123         }\r
124         else\r
125         {\r
126                 return pdFALSE;\r
127         }\r
128 }\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
132 {\r
133 portBASE_TYPE xReturn = pdTRUE;\r
134 \r
135         portENTER_CRITICAL();\r
136         {\r
137                 /* If the UART FIFO is full we can block posting the new data on the\r
138                 Tx queue. */\r
139                 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_BASEADDR ) )\r
140                 {\r
141                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
142                         {\r
143                                 xReturn = pdFAIL;\r
144                         }\r
145                 }\r
146                 /* Otherwise, if there is data already in the queue we should add the\r
147                 new data to the back of the queue to ensure the sequencing is \r
148                 maintained. */\r
149                 else if( uxQueueMessagesWaiting( xCharsForTx ) )\r
150                 {\r
151                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
152                         {\r
153                                 xReturn = pdFAIL;\r
154                         }                       \r
155                 }\r
156                 /* If the UART FIFO is not full and there is no data already in the\r
157                 queue we can write directly to the FIFO without disrupting the \r
158                 sequence. */\r
159                 else\r
160                 {\r
161                         XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );\r
162                 }\r
163         }\r
164         portEXIT_CRITICAL();\r
165 \r
166         return xReturn;\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 void vSerialClose( xComPortHandle xPort )\r
171 {\r
172         /* Not supported as not required by the demo application. */\r
173         ( void ) xPort;\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 void vSerialISR( void *pvBaseAddress )\r
178 {\r
179 unsigned portLONG ulISRStatus;\r
180 portBASE_TYPE xTaskWokenByTx = pdFALSE, xTaskWokenByRx = pdFALSE;\r
181 portCHAR cChar;\r
182 \r
183         /* Determine the cause of the interrupt. */\r
184     ulISRStatus = XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_STATUS_REG_OFFSET );\r
185 \r
186     if( ( ulISRStatus & ( XUL_SR_RX_FIFO_FULL | XUL_SR_RX_FIFO_VALID_DATA ) ) != 0 )\r
187         {\r
188                 /* A character is available - place it in the queue of received\r
189                 characters.  This might wake a task that was blocked waiting for \r
190                 data. */\r
191                 cChar = ( portCHAR )XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_RX_FIFO_OFFSET );\r
192                 xTaskWokenByRx = xQueueSendFromISR( xRxedChars, &cChar, xTaskWokenByRx );\r
193     }\r
194 \r
195     if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )\r
196     {\r
197                 /* There is space in the FIFO - if there are any characters queue for\r
198                 transmission they can be send to the UART now.  This might unblock a\r
199                 task that was waiting for space to become available on the Tx queue. */\r
200                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWokenByTx ) == pdTRUE )\r
201                 {\r
202                         XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );\r
203                 }\r
204     }\r
205 \r
206         /* If we woke any tasks we may require a context switch. */\r
207         if( xTaskWokenByTx || xTaskWokenByRx )\r
208         {\r
209                 portYIELD_FROM_ISR();\r
210         }\r
211 }\r