2 FreeRTOS V7.4.2 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 #include "FreeRTOS.h"
\r
80 /*---------------------------------------------------------------------------*/
\r
82 #define serialSTRING_DELAY_TICKS ( portMAX_DELAY )
\r
83 /*---------------------------------------------------------------------------*/
\r
85 CY_ISR_PROTO( vUartRxISR );
\r
86 CY_ISR_PROTO( vUartTxISR );
\r
87 /*---------------------------------------------------------------------------*/
\r
89 static xQueueHandle xSerialTxQueue = NULL;
\r
90 static xQueueHandle xSerialRxQueue = NULL;
\r
91 /*---------------------------------------------------------------------------*/
\r
93 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
96 xSerialRxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
\r
97 isr_UART1_RX_BYTE_RECEIVED_ClearPending();
\r
98 isr_UART1_RX_BYTE_RECEIVED_StartEx(vUartRxISR);
\r
101 xSerialTxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
\r
102 isr_UART1_TX_BYTE_COMPLETE_ClearPending() ;
\r
103 isr_UART1_TX_BYTE_COMPLETE_StartEx(vUartTxISR);
\r
105 /* Clear the interrupt modes for the Tx for the time being. */
\r
106 UART_1_SetTxInterruptMode( 0 );
\r
108 /* Both configured successfully. */
\r
109 return ( xComPortHandle )( xSerialTxQueue && xSerialRxQueue );
\r
111 /*---------------------------------------------------------------------------*/
\r
113 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
115 unsigned short usIndex = 0;
\r
117 for( usIndex = 0; usIndex < usStringLength; usIndex++ )
\r
119 /* Check for pre-mature end of line. */
\r
120 if( '\0' == pcString[ usIndex ] )
\r
125 /* Send out, one character at a time. */
\r
126 if( pdTRUE != xSerialPutChar( NULL, pcString[ usIndex ], serialSTRING_DELAY_TICKS ) )
\r
128 /* Failed to send, this will be picked up in the receive comtest task. */
\r
132 /*---------------------------------------------------------------------------*/
\r
134 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
136 portBASE_TYPE xReturn = pdFALSE;
\r
138 if( pdTRUE == xQueueReceive( xSerialRxQueue, pcRxedChar, xBlockTime ) )
\r
140 /* Picked up a character. */
\r
145 /*---------------------------------------------------------------------------*/
\r
147 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
149 portBASE_TYPE xReturn = pdFALSE;
\r
151 /* The ISR is processing characters is so just add to the end of the queue. */
\r
152 if( pdTRUE == xQueueSend( xSerialTxQueue, &cOutChar, xBlockTime ) )
\r
158 /* The queue is probably full. */
\r
162 /* Make sure that the interrupt will fire in the case where:
\r
163 Currently sending so the Tx Complete will fire.
\r
164 Not sending so the Empty will fire. */
\r
165 taskENTER_CRITICAL();
\r
166 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE | UART_1_TX_STS_FIFO_EMPTY );
\r
167 taskEXIT_CRITICAL();
\r
171 /*---------------------------------------------------------------------------*/
\r
175 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
176 volatile unsigned char ucStatus = 0;
\r
177 signed char cInChar = 0;
\r
178 unsigned long ulMask = 0;
\r
180 /* Read the status to acknowledge. */
\r
181 ucStatus = UART_1_ReadRxStatus();
\r
183 /* Only interested in a character being received. */
\r
184 if( 0 != ( ucStatus & UART_1_RX_STS_FIFO_NOTEMPTY ) )
\r
186 /* Get the character. */
\r
187 cInChar = UART_1_GetChar();
\r
189 /* Mask off the other RTOS interrupts to interact with the queue. */
\r
190 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
192 /* Try to deliver the character. */
\r
193 if( pdTRUE != xQueueSendFromISR( xSerialRxQueue, &cInChar, &xHigherPriorityTaskWoken ) )
\r
195 /* Run out of space. */
\r
198 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
\r
201 /* If we delivered the character then a context switch might be required.
\r
202 xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
\r
203 xQueueSendFromISR() caused a task to unblock, and the unblocked task has
\r
204 a priority equal to or higher than the currently running task (the task this
\r
205 ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
\r
206 portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
\r
208 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
210 /*---------------------------------------------------------------------------*/
\r
214 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
215 volatile unsigned char ucStatus = 0;
\r
216 signed char cOutChar = 0;
\r
217 unsigned long ulMask = 0;
\r
219 /* Read the status to acknowledge. */
\r
220 ucStatus = UART_1_ReadTxStatus();
\r
222 /* Check to see whether this is a genuine interrupt. */
\r
223 if( ( 0 != ( ucStatus & UART_1_TX_STS_COMPLETE ) ) || ( 0 != ( ucStatus & UART_1_TX_STS_FIFO_EMPTY ) ) )
\r
225 /* Mask off the other RTOS interrupts to interact with the queue. */
\r
226 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
228 if( pdTRUE == xQueueReceiveFromISR( xSerialTxQueue, &cOutChar, &xHigherPriorityTaskWoken ) )
\r
230 /* Send the next character. */
\r
231 UART_1_PutChar( cOutChar );
\r
233 /* If we are firing, then the only interrupt we are interested in
\r
234 is the Complete. The application code will add the Empty interrupt
\r
235 when there is something else to be done. */
\r
236 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE );
\r
240 /* There is no work left so disable the interrupt until the application
\r
241 puts more into the queue. */
\r
242 UART_1_SetTxInterruptMode( 0 );
\r
245 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
\r
248 /* If we delivered the character then a context switch might be required.
\r
249 xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
\r
250 xQueueSendFromISR() caused a task to unblock, and the unblocked task has
\r
251 a priority equal to or higher than the currently running task (the task this
\r
252 ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
\r
253 portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
\r
255 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
257 /*---------------------------------------------------------------------------*/
\r