]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Kintex7_EthernetLite/RTOSDemo/src/serial.c
0c9cbce02d695cbb9102c64661f0e7e8a69848a3
[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 "task.h"\r
83 #include "queue.h"\r
84 #include "comtest_strings.h"\r
85 \r
86 /* Library includes. */\r
87 #include "xuartlite.h"\r
88 #include "xuartlite_l.h"\r
89 \r
90 /* Demo application includes. */\r
91 #include "serial.h"\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /* Functions that are installed as the handler for interrupts that are caused by\r
96 Rx and Tx events respectively. */\r
97 static void prvRxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount );\r
98 static void prvTxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount );\r
99 \r
100 /* Structure that hold the state of the UARTLite peripheral used by this demo.\r
101 This is used by the Xilinx peripheral driver API functions. */\r
102 static XUartLite xUartLiteInstance;\r
103 \r
104 /* The queue used to hold received characters. */\r
105 static QueueHandle_t xRxedChars;\r
106 \r
107 /* Holds the handle of a task performing a Tx so it can be notified of when\r
108 the Tx has completed. */\r
109 static TaskHandle_t xUARTSendingTask = NULL;\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
114 {\r
115 BaseType_t xStatus;\r
116 \r
117         /* The standard demo header file requires a baud rate to be passed into this\r
118         function.  However, in this case the baud rate is configured when the\r
119         hardware is generated, leaving the ulWantedBaud parameter redundant. */\r
120         ( void ) ulWantedBaud;\r
121 \r
122         /* Create the queue used to hold Rx characters. */\r
123         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
124 \r
125         /* If the queue was created correctly, then setup the serial port\r
126         hardware. */\r
127         if( xRxedChars != NULL )\r
128         {\r
129                 xStatus = XUartLite_Initialize( &xUartLiteInstance, XPAR_UARTLITE_0_DEVICE_ID );\r
130 \r
131                 if( xStatus == XST_SUCCESS )\r
132                 {\r
133                         /* Complete initialisation of the UART and its associated\r
134                         interrupts. */\r
135                         XUartLite_ResetFifos( &xUartLiteInstance );\r
136 \r
137                         /* Install the handlers that the standard Xilinx library interrupt\r
138                         service routine will call when Rx and Tx events occur\r
139                         respectively. */\r
140                         XUartLite_SetRecvHandler( &xUartLiteInstance, ( XUartLite_Handler ) prvRxHandler, NULL );\r
141                         XUartLite_SetSendHandler( &xUartLiteInstance, ( XUartLite_Handler ) prvTxHandler, NULL );\r
142 \r
143                         /* Install the standard Xilinx library interrupt handler itself.\r
144                         *NOTE* The xPortInstallInterruptHandler() API function must be used\r
145                         for     this purpose. */\r
146                         xStatus = xPortInstallInterruptHandler( XPAR_INTC_0_UARTLITE_0_VEC_ID, ( XInterruptHandler ) XUartLite_InterruptHandler, &xUartLiteInstance );\r
147 \r
148                         /* Enable the interrupt in the peripheral. */\r
149                         XUartLite_EnableIntr( xUartLiteInstance.RegBaseAddress );\r
150 \r
151                         /* Enable the interrupt in the interrupt controller.\r
152                         *NOTE* The vPortEnableInterrupt() API function must be used for this\r
153                         purpose. */\r
154                         vPortEnableInterrupt( XPAR_INTC_0_UARTLITE_0_VEC_ID );\r
155                 }\r
156 \r
157                 configASSERT( xStatus == pdPASS );\r
158         }\r
159 \r
160         /* This demo file only supports a single port but something must be\r
161         returned to comply with the standard demo header file. */\r
162         return ( xComPortHandle ) 0;\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
167 {\r
168 portBASE_TYPE xReturn;\r
169 \r
170         /* The port handle is not required as this driver only supports one port. */\r
171         ( void ) pxPort;\r
172 \r
173         /* Get the next character from the receive queue.  Return false if no\r
174         characters are available, or arrive before xBlockTime expires. */\r
175         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
176         {\r
177                 xReturn = pdTRUE;\r
178         }\r
179         else\r
180         {\r
181                 xReturn = pdFALSE;\r
182         }\r
183 \r
184         return xReturn;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
189 {\r
190 const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 150UL );\r
191 portBASE_TYPE xReturn;\r
192 \r
193         ( void ) pxPort;\r
194         ( void ) xBlockTime;\r
195 \r
196         /* Note this is the currently sending task. */\r
197         xUARTSendingTask = xTaskGetCurrentTaskHandle();\r
198 \r
199         XUartLite_Send( &xUartLiteInstance, ( unsigned char * ) &cOutChar, sizeof( cOutChar ) );\r
200 \r
201         /* Wait in the Blocked state (so not using any CPU time) for the Tx to\r
202         complete. */\r
203         xReturn = ulTaskNotifyTake( pdTRUE, xMaxBlockTime );\r
204 \r
205         return xReturn;\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
210 {\r
211 const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 150UL );\r
212 \r
213         ( void ) pxPort;\r
214 \r
215         /* Note this is the currently sending task. */\r
216         xUARTSendingTask = xTaskGetCurrentTaskHandle();\r
217 \r
218         /* Output uxStringLength bytes starting from pcString. */\r
219         XUartLite_Send( &xUartLiteInstance, ( unsigned char * ) pcString, usStringLength );\r
220 \r
221         /* Wait in the Blocked state (so not using any CPU time) for the Tx to\r
222         complete. */\r
223         ulTaskNotifyTake( pdTRUE, xMaxBlockTime );\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 static void prvRxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount )\r
228 {\r
229 signed char cRxedChar;\r
230 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
231 \r
232         ( void ) pvUnused;\r
233         ( void ) uxByteCount;\r
234 \r
235         /* Place any received characters into the receive queue. */\r
236         while( XUartLite_IsReceiveEmpty( xUartLiteInstance.RegBaseAddress ) == pdFALSE )\r
237         {\r
238                 cRxedChar = XUartLite_ReadReg( xUartLiteInstance.RegBaseAddress, XUL_RX_FIFO_OFFSET);\r
239                 xQueueSendFromISR( xRxedChars, &cRxedChar, &xHigherPriorityTaskWoken );\r
240         }\r
241 \r
242         /* If calling xQueueSendFromISR() caused a task to unblock, and the task\r
243         that unblocked has a priority equal to or greater than the task currently\r
244         in the Running state (the task that was interrupted), then\r
245         xHigherPriorityTaskWoken will have been set to pdTRUE internally within the\r
246         xQueueSendFromISR() API function.  If xHigherPriorityTaskWoken is equal to\r
247         pdTRUE then a context switch should be requested to ensure that the\r
248         interrupt returns to the highest priority task that is able     to run. */\r
249         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 static void prvTxHandler( void *pvUnused, unsigned portBASE_TYPE uxUnused )\r
254 {\r
255 BaseType_t xHigherPriorityTaskWoken = NULL;\r
256 \r
257         ( void ) pvUnused;\r
258         ( void ) uxUnused;\r
259 \r
260         /* Notify the sending that that the Tx has completed. */\r
261         if( xUARTSendingTask != NULL )\r
262         {\r
263                 vTaskNotifyGiveFromISR( xUARTSendingTask, &xHigherPriorityTaskWoken );\r
264                 xUARTSendingTask = NULL;\r
265         }\r
266 \r
267         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
268 }\r
269 \r
270 \r
271 \r
272 \r
273 \r
274 \r
275 \r
276 \r
277 \r