2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
68 * This is not a proper UART driver. It only supports one port, uses loopback
\r
69 * mode, and is used to test interrupts that use the FreeRTOS API as part of
\r
70 * a wider test suite. Nor is it intended to show an efficient implementation
\r
71 * of a UART interrupt service routine as queues are used to pass individual
\r
72 * characters one at a time!
\r
75 /* Standard includes. */
\r
78 /* Scheduler includes. */
\r
79 #include "FreeRTOS.h"
\r
83 /* Demo application includes. */
\r
86 /* Misc. constants. */
\r
87 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
89 /* The queue used to hold received characters. */
\r
90 static xQueueHandle xRxedChars;
\r
92 /* The queue used to hold characters waiting transmission. */
\r
93 static xQueueHandle xCharsForTx;
\r
95 /*-----------------------------------------------------------*/
\r
97 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
99 unsigned portLONG ulBaudRateCount;
\r
101 /* Initialise the hardware. */
\r
103 /* Generate the baud rate constants for the wanted baud rate. */
\r
104 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
\r
106 portENTER_CRITICAL();
\r
108 /* Create the queues used by the com test task. */
\r
109 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
110 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
113 UCA1CTL1 |= UCSWRST;
\r
116 UCA1CTL1 = UCSSEL0 | UCSSEL1;
\r
118 /* Setup baud rate low byte. */
\r
119 UCA1BR0 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
121 /* Setup baud rate high byte. */
\r
122 ulBaudRateCount >>= 8UL;
\r
123 UCA1BR1 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
125 /* UCLISTEN sets loopback mode! */
\r
126 UCA1STAT = UCLISTEN;
\r
128 /* Enable interrupts. */
\r
131 /* Take out of reset. */
\r
132 UCA1CTL1 &= ~UCSWRST;
\r
134 portEXIT_CRITICAL();
\r
136 /* Note the comments at the top of this file about this not being a generic
\r
140 /*-----------------------------------------------------------*/
\r
142 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
144 /* Get the next character from the buffer. Return false if no characters
\r
145 are available, or arrive before xBlockTime expires. */
\r
146 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
155 /*-----------------------------------------------------------*/
\r
157 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
159 signed portBASE_TYPE xReturn;
\r
161 /* Send the next character to the queue of characters waiting transmission,
\r
162 then enable the UART Tx interrupt, just in case UART transmission has already
\r
163 completed and switched itself off. */
\r
164 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
169 /*-----------------------------------------------------------*/
\r
171 /* The implementation of this interrupt is provided to demonstrate the use
\r
172 of queues from inside an interrupt service routine. It is *not* intended to
\r
173 be an efficient interrupt implementation. A real application should make use
\r
174 of the DMA. Or, as a minimum, transmission and reception could use a simple
\r
175 RAM ring buffer, and synchronise with a task using a semaphore when a complete
\r
176 message has been received or transmitted. */
\r
177 #pragma vector=USCI_A1_VECTOR
\r
178 static __interrupt void prvUSCI_A1_ISR( void )
\r
181 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
183 while( ( UCA1IFG & UCRXIFG ) != 0 )
\r
185 /* Get the character from the UART and post it on the queue of Rxed
\r
188 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
191 /* If there is a Tx interrupt pending and the tx interrupts are enabled. */
\r
192 if( ( UCA1IFG & UCTXIFG ) != 0 )
\r
194 /* The previous character has been transmitted. See if there are any
\r
195 further characters waiting transmission. */
\r
196 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
198 /* There was another character queued - transmit it now. */
\r
203 /* There were no other characters to transmit - disable the Tx
\r
209 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
\r
211 /* If writing to a queue caused a task to unblock, and the unblocked task
\r
212 has a priority equal to or above the task that this interrupt interrupted,
\r
213 then lHigherPriorityTaskWoken will have been set to pdTRUE internally within
\r
214 xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this
\r
215 interrupt returns directly to the higher priority unblocked task.
\r
217 THIS MUST BE THE LAST THING DONE IN THE ISR. */
\r
218 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r