]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Spartan-6_EthernetLite/SDKProjects/RTOSDemo/serial.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Demo / MicroBlaze_Spartan-6_EthernetLite / SDKProjects / RTOSDemo / serial.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR a UARTLite peripheral.\r
67 */\r
68 \r
69 /* Scheduler includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "queue.h"\r
72 #include "comtest_strings.h"\r
73 \r
74 /* Library includes. */\r
75 #include "xuartlite.h"\r
76 #include "xuartlite_l.h"\r
77 \r
78 /* Demo application includes. */\r
79 #include "serial.h"\r
80 \r
81 /*-----------------------------------------------------------*/\r
82 \r
83 /* Functions that are installed as the handler for interrupts that are caused by\r
84 Rx and Tx events respectively. */\r
85 static void prvRxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount );\r
86 static void prvTxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount );\r
87 \r
88 /* Structure that hold the state of the UARTLite peripheral used by this demo.\r
89 This is used by the Xilinx peripheral driver API functions. */\r
90 static XUartLite xUartLiteInstance;\r
91 \r
92 /* The queue used to hold received characters. */\r
93 static xQueueHandle xRxedChars;\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
98 {\r
99 portBASE_TYPE xStatus;\r
100 \r
101         /* The standard demo header file requires a baud rate to be passed into this\r
102         function.  However, in this case the baud rate is configured when the\r
103         hardware is generated, leaving the ulWantedBaud parameter redundant. */\r
104         ( void ) ulWantedBaud;\r
105 \r
106         /* Create the queue used to hold Rx characters. */\r
107         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
108 \r
109         /* If the queue was created correctly, then setup the serial port\r
110         hardware. */\r
111         if( xRxedChars != NULL )\r
112         {\r
113                 xStatus = XUartLite_Initialize( &xUartLiteInstance, XPAR_UARTLITE_1_DEVICE_ID );\r
114 \r
115                 if( xStatus == XST_SUCCESS )\r
116                 {\r
117                         /* Complete initialisation of the UART and its associated\r
118                         interrupts. */\r
119                         XUartLite_ResetFifos( &xUartLiteInstance );\r
120                         \r
121                         /* Install the handlers that the standard Xilinx library interrupt\r
122                         service routine will call when Rx and Tx events occur \r
123                         respectively. */\r
124                         XUartLite_SetRecvHandler( &xUartLiteInstance, ( XUartLite_Handler ) prvRxHandler, NULL );\r
125                         XUartLite_SetSendHandler( &xUartLiteInstance, ( XUartLite_Handler ) prvTxHandler, NULL );\r
126                         \r
127                         /* Install the standard Xilinx library interrupt handler itself.\r
128                         *NOTE* The xPortInstallInterruptHandler() API function must be used \r
129                         for     this purpose. */                        \r
130                         xStatus = xPortInstallInterruptHandler( XPAR_INTC_0_UARTLITE_1_VEC_ID, ( XInterruptHandler ) XUartLite_InterruptHandler, &xUartLiteInstance );\r
131                         \r
132                         /* Enable the interrupt in the peripheral. */\r
133                         XUartLite_EnableIntr( xUartLiteInstance.RegBaseAddress );\r
134                         \r
135                         /* Enable the interrupt in the interrupt controller.\r
136                         *NOTE* The vPortEnableInterrupt() API function must be used for this\r
137                         purpose. */\r
138                         vPortEnableInterrupt( XPAR_INTC_0_UARTLITE_1_VEC_ID );\r
139                 }\r
140 \r
141                 configASSERT( xStatus == pdPASS );\r
142         }\r
143 \r
144         /* This demo file only supports a single port but something must be\r
145         returned to comply with the standard demo header file. */\r
146         return ( xComPortHandle ) 0;\r
147 }\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
151 {\r
152         /* The port handle is not required as this driver only supports one port. */\r
153         ( void ) pxPort;\r
154 \r
155         /* Get the next character from the receive queue.  Return false if no \r
156         characters are available, or arrive before xBlockTime expires. */\r
157         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
158         {\r
159                 return pdTRUE;\r
160         }\r
161         else\r
162         {\r
163                 return pdFALSE;\r
164         }\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
169 {\r
170         ( void ) pxPort;\r
171 \r
172         /* Output uxStringLength bytes starting from pcString. */\r
173         XUartLite_Send( &xUartLiteInstance, ( unsigned char * ) pcString, usStringLength );\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 static void prvRxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount )\r
178 {\r
179 signed char cRxedChar;\r
180 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
181 \r
182         ( void ) pvUnused;\r
183         ( void ) uxByteCount;\r
184 \r
185         /* Place any received characters into the receive queue. */\r
186         while( XUartLite_IsReceiveEmpty( xUartLiteInstance.RegBaseAddress ) == pdFALSE )\r
187         {\r
188                 cRxedChar = XUartLite_ReadReg( xUartLiteInstance.RegBaseAddress, XUL_RX_FIFO_OFFSET);\r
189                 xQueueSendFromISR( xRxedChars, &cRxedChar, &xHigherPriorityTaskWoken );\r
190         }\r
191 \r
192         /* If calling xQueueSendFromISR() caused a task to unblock, and the task \r
193         that unblocked has a priority equal to or greater than the task currently\r
194         in the Running state (the task that was interrupted), then \r
195         xHigherPriorityTaskWoken will have been set to pdTRUE internally within the\r
196         xQueueSendFromISR() API function.  If xHigherPriorityTaskWoken is equal to\r
197         pdTRUE then a context switch should be requested to ensure that the \r
198         interrupt returns to the highest priority task that is able     to run. */\r
199         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 static void prvTxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount )\r
204 {\r
205         ( void ) pvUnused;\r
206         ( void ) uxByteCount;\r
207 \r
208         /* Nothing to do here.  The Xilinx library function takes care of the\r
209         transmission. */\r
210         portNOP();\r
211 }\r
212 \r
213 \r
214 \r
215 \r
216 \r
217 \r
218 \r
219 \r
220         \r