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
69 /* Basic interrupt driven serial port driver for uart1.
\r
72 /* Standard includes. */
\r
75 /* Kernel includes. */
\r
76 #include "FreeRTOS.h"
\r
80 /* Demo application includes. */
\r
81 #include <machine/uart.h>
\r
82 #include <machine/ic.h>
\r
84 /*-----------------------------------------------------------*/
\r
86 #define comBLOCK_RETRY_TIME 10
\r
87 /*-----------------------------------------------------------*/
\r
89 /* The interrupt handlers are naked functions that call C handlers. The C
\r
90 handlers are marked as noinline to ensure they work correctly when the
\r
92 void interrupt5_handler( void ) __attribute__((naked));
\r
93 static void prvTxHandler( void ) __attribute__((noinline));
\r
94 void interrupt6_handler( void ) __attribute__((naked));
\r
95 static void prvRxHandler( void ) __attribute__((noinline));
\r
97 /*-----------------------------------------------------------*/
\r
99 /* Queues used to hold received characters, and characters waiting to be
\r
101 static xQueueHandle xRxedChars;
\r
102 static xQueueHandle xCharsForTx;
\r
103 extern unsigned portBASE_TYPE *pxVectorTable;
\r
104 /*-----------------------------------------------------------*/
\r
106 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
108 /* Create the queues used to hold Rx and Tx characters. */
\r
109 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
110 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
112 if( ( xRxedChars ) && ( xCharsForTx ) )
\r
114 /* Set up interrupts */
\r
115 /* tx interrupt will be enabled when we need to send something */
\r
116 uart1->tx_mask = 0;
\r
117 uart1->rx_mask = 1;
\r
118 irq[IRQ_UART1_TX].ien = 1;
\r
119 irq[IRQ_UART1_TX].ipl = portSYSTEM_INTERRUPT_PRIORITY_LEVEL;
\r
120 irq[IRQ_UART1_RX].ien = 1;
\r
121 irq[IRQ_UART1_RX].ipl = portSYSTEM_INTERRUPT_PRIORITY_LEVEL;
\r
124 return ( xComPortHandle ) 0;
\r
126 /*-----------------------------------------------------------*/
\r
128 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
130 /* The port handle is not required as this driver only supports uart1. */
\r
133 /* Get the next character from the buffer. Return false if no characters
\r
134 are available, or arrive before xBlockTime expires. */
\r
135 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
144 /*-----------------------------------------------------------*/
\r
146 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
149 signed char *pChNext;
\r
151 /* Send each character in the string, one at a time. */
\r
152 pChNext = ( signed char * )pcString;
\r
153 for( i = 0; i < usStringLength; i++ )
\r
155 /* Block until character has been transmitted. */
\r
156 while( xSerialPutChar( pxPort, *pChNext, comBLOCK_RETRY_TIME ) != pdTRUE ); pChNext++;
\r
159 /*-----------------------------------------------------------*/
\r
161 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
165 /* Place the character in the queue of characters to be transmitted. */
\r
166 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
171 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
172 queue and send it. This does not need to be in a critical section as
\r
173 if the interrupt has already removed the character the next interrupt
\r
174 will simply turn off the Tx interrupt again. */
\r
175 uart1->tx_mask = 1;
\r
179 /*-----------------------------------------------------------*/
\r
181 void vSerialClose( xComPortHandle xPort )
\r
183 /* Not supported as not required by the demo application. */
\r
186 /*-----------------------------------------------------------*/
\r
188 /* UART Tx interrupt handler. */
\r
189 void interrupt5_handler( void )
\r
191 /* This is a naked function. */
\r
192 portSAVE_CONTEXT();
\r
194 portRESTORE_CONTEXT();
\r
196 /*-----------------------------------------------------------*/
\r
198 static void prvTxHandler( void )
\r
201 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
203 /* The interrupt was caused by the transmit fifo having space for at least one
\r
204 character. Are there any more characters to transmit? */
\r
205 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
207 /* A character was retrieved from the queue so can be sent to the uart now. */
\r
208 uart1->tx_data = cChar;
\r
212 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
213 uart1->tx_mask = 0;
\r
216 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
217 ensure that the unblocked task is the task that executes when the interrupt
\r
218 completes if the unblocked task has a priority higher than the interrupted
\r
220 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
222 /*-----------------------------------------------------------*/
\r
224 /* UART Rx interrupt. */
\r
225 void interrupt6_handler( void )
\r
227 portSAVE_CONTEXT();
\r
229 portRESTORE_CONTEXT();
\r
231 /*-----------------------------------------------------------*/
\r
233 static void prvRxHandler( void )
\r
236 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
238 /* The interrupt was caused by the receiver getting data. */
\r
239 cChar = uart1->rx_data;
\r
241 xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
243 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
244 ensure that the unblocked task is the task that executes when the interrupt
\r
245 completes if the unblocked task has a priority higher than the interrupted
\r
247 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
249 /*-----------------------------------------------------------*/
\r