]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Kintex7_EthernetLite/RTOSDemo/src/serial.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS / Demo / MicroBlaze_Kintex7_EthernetLite / RTOSDemo / src / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR a UARTLite peripheral.\r
30 \r
31         NOTE:  This is not intended to represent an efficient driver.  It is\r
32         designed to test the FreeRTOS port.  Normally a UART driver would use a DMA,\r
33         or at least a circular RAM buffer rather than a queue.  A task notification\r
34         can then be used to unblock any task that is waiting for a complete message\r
35         once a complete message has been buffered.\r
36 */\r
37 \r
38 /* Scheduler includes. */\r
39 #include "FreeRTOS.h"\r
40 #include "task.h"\r
41 #include "queue.h"\r
42 #include "comtest_strings.h"\r
43 \r
44 /* Library includes. */\r
45 #include "xuartlite.h"\r
46 #include "xuartlite_l.h"\r
47 \r
48 /* Demo application includes. */\r
49 #include "serial.h"\r
50 \r
51 /*-----------------------------------------------------------*/\r
52 \r
53 /* Functions that are installed as the handler for interrupts that are caused by\r
54 Rx and Tx events respectively. */\r
55 static void prvRxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount );\r
56 static void prvTxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount );\r
57 \r
58 /* Structure that hold the state of the UARTLite peripheral used by this demo.\r
59 This is used by the Xilinx peripheral driver API functions. */\r
60 static XUartLite xUartLiteInstance;\r
61 \r
62 /* The queue used to hold received characters. */\r
63 static QueueHandle_t xRxedChars;\r
64 \r
65 /* Holds the handle of a task performing a Tx so it can be notified of when\r
66 the Tx has completed. */\r
67 static TaskHandle_t xUARTSendingTask = NULL;\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
72 {\r
73 BaseType_t xStatus;\r
74 \r
75         /* The standard demo header file requires a baud rate to be passed into this\r
76         function.  However, in this case the baud rate is configured when the\r
77         hardware is generated, leaving the ulWantedBaud parameter redundant. */\r
78         ( void ) ulWantedBaud;\r
79 \r
80         /* Create the queue used to hold Rx characters. */\r
81         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
82 \r
83         /* If the queue was created correctly, then setup the serial port\r
84         hardware. */\r
85         if( xRxedChars != NULL )\r
86         {\r
87                 xStatus = XUartLite_Initialize( &xUartLiteInstance, XPAR_UARTLITE_0_DEVICE_ID );\r
88 \r
89                 if( xStatus == XST_SUCCESS )\r
90                 {\r
91                         /* Complete initialisation of the UART and its associated\r
92                         interrupts. */\r
93                         XUartLite_ResetFifos( &xUartLiteInstance );\r
94 \r
95                         /* Install the handlers that the standard Xilinx library interrupt\r
96                         service routine will call when Rx and Tx events occur\r
97                         respectively. */\r
98                         XUartLite_SetRecvHandler( &xUartLiteInstance, ( XUartLite_Handler ) prvRxHandler, NULL );\r
99                         XUartLite_SetSendHandler( &xUartLiteInstance, ( XUartLite_Handler ) prvTxHandler, NULL );\r
100 \r
101                         /* Install the standard Xilinx library interrupt handler itself.\r
102                         *NOTE* The xPortInstallInterruptHandler() API function must be used\r
103                         for     this purpose. */\r
104                         xStatus = xPortInstallInterruptHandler( XPAR_INTC_0_UARTLITE_0_VEC_ID, ( XInterruptHandler ) XUartLite_InterruptHandler, &xUartLiteInstance );\r
105 \r
106                         /* Enable the interrupt in the peripheral. */\r
107                         XUartLite_EnableIntr( xUartLiteInstance.RegBaseAddress );\r
108 \r
109                         /* Enable the interrupt in the interrupt controller.\r
110                         *NOTE* The vPortEnableInterrupt() API function must be used for this\r
111                         purpose. */\r
112                         vPortEnableInterrupt( XPAR_INTC_0_UARTLITE_0_VEC_ID );\r
113                 }\r
114 \r
115                 configASSERT( xStatus == pdPASS );\r
116         }\r
117 \r
118         /* This demo file only supports a single port but something must be\r
119         returned to comply with the standard demo header file. */\r
120         return ( xComPortHandle ) 0;\r
121 }\r
122 /*-----------------------------------------------------------*/\r
123 \r
124 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
125 {\r
126 portBASE_TYPE xReturn;\r
127 \r
128         /* The port handle is not required as this driver only supports one port. */\r
129         ( void ) pxPort;\r
130 \r
131         /* Get the next character from the receive queue.  Return false if no\r
132         characters are available, or arrive before xBlockTime expires. */\r
133         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
134         {\r
135                 xReturn = pdTRUE;\r
136         }\r
137         else\r
138         {\r
139                 xReturn = pdFALSE;\r
140         }\r
141 \r
142         return xReturn;\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
147 {\r
148 const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 150UL );\r
149 portBASE_TYPE xReturn;\r
150 \r
151         ( void ) pxPort;\r
152         ( void ) xBlockTime;\r
153 \r
154         /* Note this is the currently sending task. */\r
155         xUARTSendingTask = xTaskGetCurrentTaskHandle();\r
156 \r
157         XUartLite_Send( &xUartLiteInstance, ( unsigned char * ) &cOutChar, sizeof( cOutChar ) );\r
158 \r
159         /* Wait in the Blocked state (so not using any CPU time) for the Tx to\r
160         complete. */\r
161         xReturn = ulTaskNotifyTake( pdTRUE, xMaxBlockTime );\r
162 \r
163         return xReturn;\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
168 {\r
169 const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 150UL );\r
170 \r
171         ( void ) pxPort;\r
172 \r
173         /* Note this is the currently sending task. */\r
174         xUARTSendingTask = xTaskGetCurrentTaskHandle();\r
175 \r
176         /* Output uxStringLength bytes starting from pcString. */\r
177         XUartLite_Send( &xUartLiteInstance, ( unsigned char * ) pcString, usStringLength );\r
178 \r
179         /* Wait in the Blocked state (so not using any CPU time) for the Tx to\r
180         complete. */\r
181         ulTaskNotifyTake( pdTRUE, xMaxBlockTime );\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 static void prvRxHandler( void *pvUnused, unsigned portBASE_TYPE uxByteCount )\r
186 {\r
187 signed char cRxedChar;\r
188 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
189 \r
190         ( void ) pvUnused;\r
191         ( void ) uxByteCount;\r
192 \r
193         /* Place any received characters into the receive queue. */\r
194         while( XUartLite_IsReceiveEmpty( xUartLiteInstance.RegBaseAddress ) == pdFALSE )\r
195         {\r
196                 cRxedChar = XUartLite_ReadReg( xUartLiteInstance.RegBaseAddress, XUL_RX_FIFO_OFFSET);\r
197                 xQueueSendFromISR( xRxedChars, &cRxedChar, &xHigherPriorityTaskWoken );\r
198         }\r
199 \r
200         /* If calling xQueueSendFromISR() caused a task to unblock, and the task\r
201         that unblocked has a priority equal to or greater than the task currently\r
202         in the Running state (the task that was interrupted), then\r
203         xHigherPriorityTaskWoken will have been set to pdTRUE internally within the\r
204         xQueueSendFromISR() API function.  If xHigherPriorityTaskWoken is equal to\r
205         pdTRUE then a context switch should be requested to ensure that the\r
206         interrupt returns to the highest priority task that is able     to run. */\r
207         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 static void prvTxHandler( void *pvUnused, unsigned portBASE_TYPE uxUnused )\r
212 {\r
213 BaseType_t xHigherPriorityTaskWoken = NULL;\r
214 \r
215         ( void ) pvUnused;\r
216         ( void ) uxUnused;\r
217 \r
218         /* Notify the sending that that the Tx has completed. */\r
219         if( xUARTSendingTask != NULL )\r
220         {\r
221                 vTaskNotifyGiveFromISR( xUARTSendingTask, &xHigherPriorityTaskWoken );\r
222                 xUARTSendingTask = NULL;\r
223         }\r
224 \r
225         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
226 }\r
227 \r
228 \r
229 \r
230 \r
231 \r
232 \r
233 \r
234 \r
235 \r