2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 #include "FreeRTOS.h"
\r
74 /*---------------------------------------------------------------------------*/
\r
76 #define serialSTRING_DELAY_TICKS ( portMAX_DELAY )
\r
77 /*---------------------------------------------------------------------------*/
\r
79 CY_ISR_PROTO( vUartRxISR );
\r
80 CY_ISR_PROTO( vUartTxISR );
\r
81 /*---------------------------------------------------------------------------*/
\r
83 static xQueueHandle xSerialTxQueue = NULL;
\r
84 static xQueueHandle xSerialRxQueue = NULL;
\r
85 /*---------------------------------------------------------------------------*/
\r
87 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
90 xSerialRxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
\r
91 isr_UART1_RX_BYTE_RECEIVED_ClearPending();
\r
92 isr_UART1_RX_BYTE_RECEIVED_StartEx(vUartRxISR);
\r
95 xSerialTxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
\r
96 isr_UART1_TX_BYTE_COMPLETE_ClearPending() ;
\r
97 isr_UART1_TX_BYTE_COMPLETE_StartEx(vUartTxISR);
\r
99 /* Clear the interrupt modes for the Tx for the time being. */
\r
100 UART_1_SetTxInterruptMode( 0 );
\r
102 /* Both configured successfully. */
\r
103 return ( xComPortHandle )( xSerialTxQueue && xSerialRxQueue );
\r
105 /*---------------------------------------------------------------------------*/
\r
107 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
109 unsigned short usIndex = 0;
\r
111 for( usIndex = 0; usIndex < usStringLength; usIndex++ )
\r
113 /* Check for pre-mature end of line. */
\r
114 if( '\0' == pcString[ usIndex ] )
\r
119 /* Send out, one character at a time. */
\r
120 if( pdTRUE != xSerialPutChar( NULL, pcString[ usIndex ], serialSTRING_DELAY_TICKS ) )
\r
122 /* Failed to send, this will be picked up in the receive comtest task. */
\r
126 /*---------------------------------------------------------------------------*/
\r
128 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
130 portBASE_TYPE xReturn = pdFALSE;
\r
132 if( pdTRUE == xQueueReceive( xSerialRxQueue, pcRxedChar, xBlockTime ) )
\r
134 /* Picked up a character. */
\r
139 /*---------------------------------------------------------------------------*/
\r
141 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
143 portBASE_TYPE xReturn = pdFALSE;
\r
145 /* The ISR is processing characters is so just add to the end of the queue. */
\r
146 if( pdTRUE == xQueueSend( xSerialTxQueue, &cOutChar, xBlockTime ) )
\r
152 /* The queue is probably full. */
\r
156 /* Make sure that the interrupt will fire in the case where:
\r
157 Currently sending so the Tx Complete will fire.
\r
158 Not sending so the Empty will fire. */
\r
159 taskENTER_CRITICAL();
\r
160 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE | UART_1_TX_STS_FIFO_EMPTY );
\r
161 taskEXIT_CRITICAL();
\r
165 /*---------------------------------------------------------------------------*/
\r
169 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
170 volatile unsigned char ucStatus = 0;
\r
171 signed char cInChar = 0;
\r
172 unsigned long ulMask = 0;
\r
174 /* Read the status to acknowledge. */
\r
175 ucStatus = UART_1_ReadRxStatus();
\r
177 /* Only interested in a character being received. */
\r
178 if( 0 != ( ucStatus & UART_1_RX_STS_FIFO_NOTEMPTY ) )
\r
180 /* Get the character. */
\r
181 cInChar = UART_1_GetChar();
\r
183 /* Mask off the other RTOS interrupts to interact with the queue. */
\r
184 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
186 /* Try to deliver the character. */
\r
187 if( pdTRUE != xQueueSendFromISR( xSerialRxQueue, &cInChar, &xHigherPriorityTaskWoken ) )
\r
189 /* Run out of space. */
\r
192 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
\r
195 /* If we delivered the character then a context switch might be required.
\r
196 xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
\r
197 xQueueSendFromISR() caused a task to unblock, and the unblocked task has
\r
198 a priority equal to or higher than the currently running task (the task this
\r
199 ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
\r
200 portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
\r
202 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
204 /*---------------------------------------------------------------------------*/
\r
208 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
209 volatile unsigned char ucStatus = 0;
\r
210 signed char cOutChar = 0;
\r
211 unsigned long ulMask = 0;
\r
213 /* Read the status to acknowledge. */
\r
214 ucStatus = UART_1_ReadTxStatus();
\r
216 /* Check to see whether this is a genuine interrupt. */
\r
217 if( ( 0 != ( ucStatus & UART_1_TX_STS_COMPLETE ) ) || ( 0 != ( ucStatus & UART_1_TX_STS_FIFO_EMPTY ) ) )
\r
219 /* Mask off the other RTOS interrupts to interact with the queue. */
\r
220 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
222 if( pdTRUE == xQueueReceiveFromISR( xSerialTxQueue, &cOutChar, &xHigherPriorityTaskWoken ) )
\r
224 /* Send the next character. */
\r
225 UART_1_PutChar( cOutChar );
\r
227 /* If we are firing, then the only interrupt we are interested in
\r
228 is the Complete. The application code will add the Empty interrupt
\r
229 when there is something else to be done. */
\r
230 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE );
\r
234 /* There is no work left so disable the interrupt until the application
\r
235 puts more into the queue. */
\r
236 UART_1_SetTxInterruptMode( 0 );
\r
239 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
\r
242 /* If we delivered the character then a context switch might be required.
\r
243 xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
\r
244 xQueueSendFromISR() caused a task to unblock, and the unblocked task has
\r
245 a priority equal to or higher than the currently running task (the task this
\r
246 ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
\r
247 portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
\r
249 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
251 /*---------------------------------------------------------------------------*/
\r