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