]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze/serial/serial.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / MicroBlaze / serial / serial.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 \r
70 /* \r
71         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART\r
72 */\r
73 \r
74 /* Scheduler includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "queue.h"\r
77 #include "task.h"\r
78 \r
79 /* Demo application includes. */\r
80 #include "serial.h"\r
81 \r
82 /* Microblaze driver includes. */\r
83 #include "xuartlite_l.h"\r
84 #include "xintc_l.h"\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* Queues used to hold received characters, and characters waiting to be\r
89 transmitted. */\r
90 static xQueueHandle xRxedChars; \r
91 static xQueueHandle xCharsForTx; \r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
96 {\r
97 unsigned long ulControlReg, ulMask;\r
98 \r
99         /* NOTE: The baud rate used by this driver is determined by the hardware\r
100         parameterization of the UART Lite peripheral, and the baud value passed to\r
101         this function has no effect. */\r
102 \r
103         /* Create the queues used to hold Rx and Tx characters. */\r
104         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
105         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
106 \r
107         if( ( xRxedChars ) && ( xCharsForTx ) )\r
108         {\r
109                 /* Disable the interrupt. */\r
110                 XUartLite_mDisableIntr( XPAR_RS232_UART_BASEADDR );\r
111                 \r
112                 /* Flush the fifos. */\r
113                 ulControlReg = XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_STATUS_REG_OFFSET );\r
114                 XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_CONTROL_REG_OFFSET, ulControlReg | XUL_CR_FIFO_TX_RESET | XUL_CR_FIFO_RX_RESET );\r
115 \r
116                 /* Enable the interrupt again.  The interrupt controller has not yet been \r
117                 initialised so there is no chance of receiving an interrupt until the \r
118                 scheduler has been started. */\r
119                 XUartLite_mEnableIntr( XPAR_RS232_UART_BASEADDR );\r
120 \r
121                 /* Enable the interrupt in the interrupt controller while maintaining \r
122                 all the other bit settings. */\r
123                 ulMask = XIntc_In32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ) );\r
124                 ulMask |= XPAR_RS232_UART_INTERRUPT_MASK;\r
125                 XIntc_Out32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ), ( ulMask ) );\r
126                 XIntc_mAckIntr( XPAR_INTC_SINGLE_BASEADDR, 2 );\r
127         }\r
128         \r
129         return ( xComPortHandle ) 0;\r
130 }\r
131 /*-----------------------------------------------------------*/\r
132 \r
133 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
134 {\r
135         /* The port handle is not required as this driver only supports one UART. */\r
136         ( void ) pxPort;\r
137 \r
138         /* Get the next character from the buffer.  Return false if no characters\r
139         are available, or arrive before xBlockTime expires. */\r
140         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
141         {\r
142                 return pdTRUE;\r
143         }\r
144         else\r
145         {\r
146                 return pdFALSE;\r
147         }\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
152 {\r
153 portBASE_TYPE xReturn = pdTRUE;\r
154 \r
155         portENTER_CRITICAL();\r
156         {\r
157                 /* If the UART FIFO is full we can block posting the new data on the\r
158                 Tx queue. */\r
159                 if( XUartLite_mIsTransmitFull( XPAR_RS232_UART_BASEADDR ) )\r
160                 {\r
161                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
162                         {\r
163                                 xReturn = pdFAIL;\r
164                         }\r
165                 }\r
166                 /* Otherwise, if there is data already in the queue we should add the\r
167                 new data to the back of the queue to ensure the sequencing is \r
168                 maintained. */\r
169                 else if( uxQueueMessagesWaiting( xCharsForTx ) )\r
170                 {\r
171                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
172                         {\r
173                                 xReturn = pdFAIL;\r
174                         }                       \r
175                 }\r
176                 /* If the UART FIFO is not full and there is no data already in the\r
177                 queue we can write directly to the FIFO without disrupting the \r
178                 sequence. */\r
179                 else\r
180                 {\r
181                         XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_TX_FIFO_OFFSET, cOutChar );\r
182                 }\r
183         }\r
184         portEXIT_CRITICAL();\r
185 \r
186         return xReturn;\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void vSerialClose( xComPortHandle xPort )\r
191 {\r
192         /* Not supported as not required by the demo application. */\r
193         ( void ) xPort;\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 void vSerialISR( void *pvBaseAddress )\r
198 {\r
199 unsigned long ulISRStatus;\r
200 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
201 char cChar;\r
202 \r
203         /* Determine the cause of the interrupt. */\r
204     ulISRStatus = XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_STATUS_REG_OFFSET );\r
205 \r
206     if( ( ulISRStatus & ( XUL_SR_RX_FIFO_FULL | XUL_SR_RX_FIFO_VALID_DATA ) ) != 0 )\r
207         {\r
208                 /* A character is available - place it in the queue of received\r
209                 characters.  This might wake a task that was blocked waiting for \r
210                 data. */\r
211                 cChar = ( char )XIo_In32( XPAR_RS232_UART_BASEADDR + XUL_RX_FIFO_OFFSET );\r
212                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
213     }\r
214 \r
215     if( ( ulISRStatus & XUL_SR_TX_FIFO_EMPTY ) != 0 )\r
216     {\r
217                 /* There is space in the FIFO - if there are any characters queue for\r
218                 transmission they can be send to the UART now.  This might unblock a\r
219                 task that was waiting for space to become available on the Tx queue. */\r
220                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
221                 {\r
222                         XIo_Out32( XPAR_RS232_UART_BASEADDR + XUL_TX_FIFO_OFFSET, cChar );\r
223                 }\r
224     }\r
225 \r
226         /* If we woke any tasks we may require a context switch. */\r
227         if( xHigherPriorityTaskWoken )\r
228         {\r
229                 portYIELD_FROM_ISR();\r
230         }\r
231 }\r