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