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