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