]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Kintex7_EthernetLite/RTOSDemo/src/serial.c
Add the beginnings of a Microblaze project for the KC705.
[freertos] / FreeRTOS / Demo / MicroBlaze_Kintex7_EthernetLite / RTOSDemo / src / 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         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR a UARTLite peripheral.\r
72 \r
73         NOTE:  This is not intended to represent an efficient driver.  It is\r
74         designed to test the FreeRTOS port.  Normally a UART driver would use a DMA,\r
75         or at least a circular RAM buffer rather than a queue.  A task notification\r
76         can then be used to unblock any task that is waiting for a complete message\r
77         once a complete message has been buffered.\r
78 */\r
79 \r
80 /* Scheduler includes. */\r
81 #include "FreeRTOS.h"\r
82 #include "queue.h"\r
83 #include "comtest_strings.h"\r
84 \r
85 /* Library includes. */\r
86 #include "xuartlite.h"\r
87 #include "xuartlite_l.h"\r
88 \r
89 /* Demo application includes. */\r
90 #include "serial.h"\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* Functions that are installed as the handler for interrupts that are caused by\r
95 Rx and Tx events respectively. */\r
96 static void prvRxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount );\r
97 static void prvTxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount );\r
98 \r
99 /* Structure that hold the state of the UARTLite peripheral used by this demo.\r
100 This is used by the Xilinx peripheral driver API functions. */\r
101 static XUartLite xUartLiteInstance;\r
102 \r
103 /* The queue used to hold received characters. */\r
104 static QueueHandle_t xRxedChars;\r
105 \r
106 /*-----------------------------------------------------------*/\r
107 \r
108 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
109 {\r
110 BaseType_t xStatus;\r
111 \r
112         /* The standard demo header file requires a baud rate to be passed into this\r
113         function.  However, in this case the baud rate is configured when the\r
114         hardware is generated, leaving the ulWantedBaud parameter redundant. */\r
115         ( void ) ulWantedBaud;\r
116 \r
117         /* Create the queue used to hold Rx characters. */\r
118         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
119 \r
120         /* If the queue was created correctly, then setup the serial port\r
121         hardware. */\r
122         if( xRxedChars != NULL )\r
123         {\r
124                 xStatus = XUartLite_Initialize( &xUartLiteInstance, XPAR_UARTLITE_0_DEVICE_ID );\r
125 \r
126                 if( xStatus == XST_SUCCESS )\r
127                 {\r
128                         /* Complete initialisation of the UART and its associated\r
129                         interrupts. */\r
130                         XUartLite_ResetFifos( &xUartLiteInstance );\r
131 \r
132                         /* Install the handlers that the standard Xilinx library interrupt\r
133                         service routine will call when Rx and Tx events occur\r
134                         respectively. */\r
135                         XUartLite_SetRecvHandler( &xUartLiteInstance, ( XUartLite_Handler ) prvRxHandler, NULL );\r
136                         XUartLite_SetSendHandler( &xUartLiteInstance, ( XUartLite_Handler ) prvTxHandler, NULL );\r
137 \r
138                         /* Install the standard Xilinx library interrupt handler itself.\r
139                         *NOTE* The xPortInstallInterruptHandler() API function must be used\r
140                         for     this purpose. */\r
141                         xStatus = xPortInstallInterruptHandler( XPAR_INTC_0_UARTLITE_0_VEC_ID, ( XInterruptHandler ) XUartLite_InterruptHandler, &xUartLiteInstance );\r
142 \r
143                         /* Enable the interrupt in the peripheral. */\r
144                         XUartLite_EnableIntr( xUartLiteInstance.RegBaseAddress );\r
145 \r
146                         /* Enable the interrupt in the interrupt controller.\r
147                         *NOTE* The vPortEnableInterrupt() API function must be used for this\r
148                         purpose. */\r
149                         vPortEnableInterrupt( XPAR_INTC_0_UARTLITE_0_VEC_ID );\r
150                 }\r
151 \r
152                 configASSERT( xStatus == pdPASS );\r
153         }\r
154 \r
155         /* This demo file only supports a single port but something must be\r
156         returned to comply with the standard demo header file. */\r
157         return ( xComPortHandle ) 0;\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
162 {\r
163 portBASE_TYPE xReturn;\r
164 \r
165         /* The port handle is not required as this driver only supports one port. */\r
166         ( void ) pxPort;\r
167 \r
168         /* Get the next character from the receive queue.  Return false if no\r
169         characters are available, or arrive before xBlockTime expires. */\r
170         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
171         {\r
172                 xReturn = pdTRUE;\r
173         }\r
174         else\r
175         {\r
176                 xReturn = pdFALSE;\r
177         }\r
178 \r
179         return xReturn;\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
184 {\r
185         ( void ) pxPort;\r
186         ( void ) xBlockTime;\r
187 \r
188         XUartLite_Send( &xUartLiteInstance, ( unsigned char * ) &cOutChar, sizeof( cOutChar ) );\r
189         return pdPASS;\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
194 {\r
195         ( void ) pxPort;\r
196 \r
197         /* Output uxStringLength bytes starting from pcString. */\r
198         XUartLite_Send( &xUartLiteInstance, ( unsigned char * ) pcString, usStringLength );\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 static void prvRxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount )\r
203 {\r
204 signed char cRxedChar;\r
205 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
206 \r
207         ( void ) pvUnused;\r
208         ( void ) uxByteCount;\r
209 \r
210         /* Place any received characters into the receive queue. */\r
211         while( XUartLite_IsReceiveEmpty( xUartLiteInstance.RegBaseAddress ) == pdFALSE )\r
212         {\r
213                 cRxedChar = XUartLite_ReadReg( xUartLiteInstance.RegBaseAddress, XUL_RX_FIFO_OFFSET);\r
214                 xQueueSendFromISR( xRxedChars, &cRxedChar, &xHigherPriorityTaskWoken );\r
215         }\r
216 \r
217         /* If calling xQueueSendFromISR() caused a task to unblock, and the task\r
218         that unblocked has a priority equal to or greater than the task currently\r
219         in the Running state (the task that was interrupted), then\r
220         xHigherPriorityTaskWoken will have been set to pdTRUE internally within the\r
221         xQueueSendFromISR() API function.  If xHigherPriorityTaskWoken is equal to\r
222         pdTRUE then a context switch should be requested to ensure that the\r
223         interrupt returns to the highest priority task that is able     to run. */\r
224         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 static void prvTxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount )\r
229 {\r
230         ( void ) pvUnused;\r
231         ( void ) uxByteCount;\r
232 \r
233         /* Nothing to do here.  The Xilinx library function takes care of the\r
234         transmission. */\r
235         portNOP();\r
236 }\r
237 \r
238 \r
239 \r
240 \r
241 \r
242 \r
243 \r
244 \r
245 \r