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