2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 #include "FreeRTOS.h"
\r
59 /*---------------------------------------------------------------------------*/
\r
61 #define serialSTRING_DELAY_TICKS ( portMAX_DELAY )
\r
62 /*---------------------------------------------------------------------------*/
\r
64 CY_ISR_PROTO( vUartRxISR );
\r
65 CY_ISR_PROTO( vUartTxISR );
\r
66 /*---------------------------------------------------------------------------*/
\r
68 static xQueueHandle xSerialTxQueue = NULL;
\r
69 static xQueueHandle xSerialRxQueue = NULL;
\r
70 /*---------------------------------------------------------------------------*/
\r
72 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
75 xSerialRxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
\r
76 isr_UART1_RX_BYTE_RECEIVED_ClearPending();
\r
77 isr_UART1_RX_BYTE_RECEIVED_StartEx(vUartRxISR);
\r
80 xSerialTxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
\r
81 isr_UART1_TX_BYTE_COMPLETE_ClearPending() ;
\r
82 isr_UART1_TX_BYTE_COMPLETE_StartEx(vUartTxISR);
\r
84 /* Clear the interrupt modes for the Tx for the time being. */
\r
85 UART_1_SetTxInterruptMode( 0 );
\r
87 /* Both configured successfully. */
\r
88 return ( xComPortHandle )( xSerialTxQueue && xSerialRxQueue );
\r
90 /*---------------------------------------------------------------------------*/
\r
92 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
94 unsigned short usIndex = 0;
\r
96 for( usIndex = 0; usIndex < usStringLength; usIndex++ )
\r
98 /* Check for pre-mature end of line. */
\r
99 if( '\0' == pcString[ usIndex ] )
\r
104 /* Send out, one character at a time. */
\r
105 if( pdTRUE != xSerialPutChar( NULL, pcString[ usIndex ], serialSTRING_DELAY_TICKS ) )
\r
107 /* Failed to send, this will be picked up in the receive comtest task. */
\r
111 /*---------------------------------------------------------------------------*/
\r
113 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
115 portBASE_TYPE xReturn = pdFALSE;
\r
117 if( pdTRUE == xQueueReceive( xSerialRxQueue, pcRxedChar, xBlockTime ) )
\r
119 /* Picked up a character. */
\r
124 /*---------------------------------------------------------------------------*/
\r
126 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
128 portBASE_TYPE xReturn = pdFALSE;
\r
130 /* The ISR is processing characters is so just add to the end of the queue. */
\r
131 if( pdTRUE == xQueueSend( xSerialTxQueue, &cOutChar, xBlockTime ) )
\r
137 /* The queue is probably full. */
\r
141 /* Make sure that the interrupt will fire in the case where:
\r
142 Currently sending so the Tx Complete will fire.
\r
143 Not sending so the Empty will fire. */
\r
144 taskENTER_CRITICAL();
\r
145 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE | UART_1_TX_STS_FIFO_EMPTY );
\r
146 taskEXIT_CRITICAL();
\r
150 /*---------------------------------------------------------------------------*/
\r
154 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
155 volatile unsigned char ucStatus = 0;
\r
156 signed char cInChar = 0;
\r
157 unsigned long ulMask = 0;
\r
159 /* Read the status to acknowledge. */
\r
160 ucStatus = UART_1_ReadRxStatus();
\r
162 /* Only interested in a character being received. */
\r
163 if( 0 != ( ucStatus & UART_1_RX_STS_FIFO_NOTEMPTY ) )
\r
165 /* Get the character. */
\r
166 cInChar = UART_1_GetChar();
\r
168 /* Mask off the other RTOS interrupts to interact with the queue. */
\r
169 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
171 /* Try to deliver the character. */
\r
172 if( pdTRUE != xQueueSendFromISR( xSerialRxQueue, &cInChar, &xHigherPriorityTaskWoken ) )
\r
174 /* Run out of space. */
\r
177 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
\r
180 /* If we delivered the character then a context switch might be required.
\r
181 xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
\r
182 xQueueSendFromISR() caused a task to unblock, and the unblocked task has
\r
183 a priority equal to or higher than the currently running task (the task this
\r
184 ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
\r
185 portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
\r
187 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
189 /*---------------------------------------------------------------------------*/
\r
193 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
194 volatile unsigned char ucStatus = 0;
\r
195 signed char cOutChar = 0;
\r
196 unsigned long ulMask = 0;
\r
198 /* Read the status to acknowledge. */
\r
199 ucStatus = UART_1_ReadTxStatus();
\r
201 /* Check to see whether this is a genuine interrupt. */
\r
202 if( ( 0 != ( ucStatus & UART_1_TX_STS_COMPLETE ) ) || ( 0 != ( ucStatus & UART_1_TX_STS_FIFO_EMPTY ) ) )
\r
204 /* Mask off the other RTOS interrupts to interact with the queue. */
\r
205 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
207 if( pdTRUE == xQueueReceiveFromISR( xSerialTxQueue, &cOutChar, &xHigherPriorityTaskWoken ) )
\r
209 /* Send the next character. */
\r
210 UART_1_PutChar( cOutChar );
\r
212 /* If we are firing, then the only interrupt we are interested in
\r
213 is the Complete. The application code will add the Empty interrupt
\r
214 when there is something else to be done. */
\r
215 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE );
\r
219 /* There is no work left so disable the interrupt until the application
\r
220 puts more into the queue. */
\r
221 UART_1_SetTxInterruptMode( 0 );
\r
224 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
\r
227 /* If we delivered the character then a context switch might be required.
\r
228 xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
\r
229 xQueueSendFromISR() caused a task to unblock, and the unblocked task has
\r
230 a priority equal to or higher than the currently running task (the task this
\r
231 ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
\r
232 portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
\r
234 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
236 /*---------------------------------------------------------------------------*/
\r