2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 #include "FreeRTOS.h"
\r
72 /*---------------------------------------------------------------------------*/
\r
74 #define serialSTRING_DELAY_TICKS ( portMAX_DELAY )
\r
75 /*---------------------------------------------------------------------------*/
\r
77 CY_ISR_PROTO( vUartRxISR );
\r
78 CY_ISR_PROTO( vUartTxISR );
\r
79 /*---------------------------------------------------------------------------*/
\r
81 static xQueueHandle xSerialTxQueue = NULL;
\r
82 static xQueueHandle xSerialRxQueue = NULL;
\r
83 /*---------------------------------------------------------------------------*/
\r
85 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
88 xSerialRxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
\r
89 isr_UART1_RX_BYTE_RECEIVED_ClearPending();
\r
90 isr_UART1_RX_BYTE_RECEIVED_StartEx(vUartRxISR);
\r
93 xSerialTxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
\r
94 isr_UART1_TX_BYTE_COMPLETE_ClearPending() ;
\r
95 isr_UART1_TX_BYTE_COMPLETE_StartEx(vUartTxISR);
\r
97 /* Clear the interrupt modes for the Tx for the time being. */
\r
98 UART_1_SetTxInterruptMode( 0 );
\r
100 /* Both configured successfully. */
\r
101 return ( xComPortHandle )( xSerialTxQueue && xSerialRxQueue );
\r
103 /*---------------------------------------------------------------------------*/
\r
105 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
107 unsigned short usIndex = 0;
\r
109 for( usIndex = 0; usIndex < usStringLength; usIndex++ )
\r
111 /* Check for pre-mature end of line. */
\r
112 if( '\0' == pcString[ usIndex ] )
\r
117 /* Send out, one character at a time. */
\r
118 if( pdTRUE != xSerialPutChar( NULL, pcString[ usIndex ], serialSTRING_DELAY_TICKS ) )
\r
120 /* Failed to send, this will be picked up in the receive comtest task. */
\r
124 /*---------------------------------------------------------------------------*/
\r
126 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
128 portBASE_TYPE xReturn = pdFALSE;
\r
130 if( pdTRUE == xQueueReceive( xSerialRxQueue, pcRxedChar, xBlockTime ) )
\r
132 /* Picked up a character. */
\r
137 /*---------------------------------------------------------------------------*/
\r
139 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
141 portBASE_TYPE xReturn = pdFALSE;
\r
143 /* The ISR is processing characters is so just add to the end of the queue. */
\r
144 if( pdTRUE == xQueueSend( xSerialTxQueue, &cOutChar, xBlockTime ) )
\r
150 /* The queue is probably full. */
\r
154 /* Make sure that the interrupt will fire in the case where:
\r
155 Currently sending so the Tx Complete will fire.
\r
156 Not sending so the Empty will fire. */
\r
157 taskENTER_CRITICAL();
\r
158 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE | UART_1_TX_STS_FIFO_EMPTY );
\r
159 taskEXIT_CRITICAL();
\r
163 /*---------------------------------------------------------------------------*/
\r
167 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
168 volatile unsigned char ucStatus = 0;
\r
169 signed char cInChar = 0;
\r
170 unsigned long ulMask = 0;
\r
172 /* Read the status to acknowledge. */
\r
173 ucStatus = UART_1_ReadRxStatus();
\r
175 /* Only interested in a character being received. */
\r
176 if( 0 != ( ucStatus & UART_1_RX_STS_FIFO_NOTEMPTY ) )
\r
178 /* Get the character. */
\r
179 cInChar = UART_1_GetChar();
\r
181 /* Mask off the other RTOS interrupts to interact with the queue. */
\r
182 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
184 /* Try to deliver the character. */
\r
185 if( pdTRUE != xQueueSendFromISR( xSerialRxQueue, &cInChar, &xHigherPriorityTaskWoken ) )
\r
187 /* Run out of space. */
\r
190 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
\r
193 /* If we delivered the character then a context switch might be required.
\r
194 xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
\r
195 xQueueSendFromISR() caused a task to unblock, and the unblocked task has
\r
196 a priority equal to or higher than the currently running task (the task this
\r
197 ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
\r
198 portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
\r
200 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
202 /*---------------------------------------------------------------------------*/
\r
206 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
207 volatile unsigned char ucStatus = 0;
\r
208 signed char cOutChar = 0;
\r
209 unsigned long ulMask = 0;
\r
211 /* Read the status to acknowledge. */
\r
212 ucStatus = UART_1_ReadTxStatus();
\r
214 /* Check to see whether this is a genuine interrupt. */
\r
215 if( ( 0 != ( ucStatus & UART_1_TX_STS_COMPLETE ) ) || ( 0 != ( ucStatus & UART_1_TX_STS_FIFO_EMPTY ) ) )
\r
217 /* Mask off the other RTOS interrupts to interact with the queue. */
\r
218 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
220 if( pdTRUE == xQueueReceiveFromISR( xSerialTxQueue, &cOutChar, &xHigherPriorityTaskWoken ) )
\r
222 /* Send the next character. */
\r
223 UART_1_PutChar( cOutChar );
\r
225 /* If we are firing, then the only interrupt we are interested in
\r
226 is the Complete. The application code will add the Empty interrupt
\r
227 when there is something else to be done. */
\r
228 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE );
\r
232 /* There is no work left so disable the interrupt until the application
\r
233 puts more into the queue. */
\r
234 UART_1_SetTxInterruptMode( 0 );
\r
237 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
\r
240 /* If we delivered the character then a context switch might be required.
\r
241 xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
\r
242 xQueueSendFromISR() caused a task to unblock, and the unblocked task has
\r
243 a priority equal to or higher than the currently running task (the task this
\r
244 ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
\r
245 portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
\r
247 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
249 /*---------------------------------------------------------------------------*/
\r