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
67 /* Basic interrupt driven serial port driver for uart1.
\r
70 /* Standard includes. */
\r
73 /* Kernel includes. */
\r
74 #include "FreeRTOS.h"
\r
78 /* Demo application includes. */
\r
79 #include <machine/uart.h>
\r
80 #include <machine/ic.h>
\r
82 /*-----------------------------------------------------------*/
\r
84 #define comBLOCK_RETRY_TIME 10
\r
85 /*-----------------------------------------------------------*/
\r
87 /* The interrupt handlers are naked functions that call C handlers. The C
\r
88 handlers are marked as noinline to ensure they work correctly when the
\r
90 void interrupt5_handler( void ) __attribute__((naked));
\r
91 static void prvTxHandler( void ) __attribute__((noinline));
\r
92 void interrupt6_handler( void ) __attribute__((naked));
\r
93 static void prvRxHandler( void ) __attribute__((noinline));
\r
95 /*-----------------------------------------------------------*/
\r
97 /* Queues used to hold received characters, and characters waiting to be
\r
99 static xQueueHandle xRxedChars;
\r
100 static xQueueHandle xCharsForTx;
\r
101 extern unsigned portBASE_TYPE *pxVectorTable;
\r
102 /*-----------------------------------------------------------*/
\r
104 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
106 /* Create the queues used to hold Rx and Tx characters. */
\r
107 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
108 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
110 if( ( xRxedChars ) && ( xCharsForTx ) )
\r
112 /* Set up interrupts */
\r
113 /* tx interrupt will be enabled when we need to send something */
\r
114 uart1->tx_mask = 0;
\r
115 uart1->rx_mask = 1;
\r
116 irq[IRQ_UART1_TX].ien = 1;
\r
117 irq[IRQ_UART1_TX].ipl = portSYSTEM_INTERRUPT_PRIORITY_LEVEL;
\r
118 irq[IRQ_UART1_RX].ien = 1;
\r
119 irq[IRQ_UART1_RX].ipl = portSYSTEM_INTERRUPT_PRIORITY_LEVEL;
\r
122 return ( xComPortHandle ) 0;
\r
124 /*-----------------------------------------------------------*/
\r
126 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
128 /* The port handle is not required as this driver only supports uart1. */
\r
131 /* Get the next character from the buffer. Return false if no characters
\r
132 are available, or arrive before xBlockTime expires. */
\r
133 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
142 /*-----------------------------------------------------------*/
\r
144 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
147 signed char *pChNext;
\r
149 /* Send each character in the string, one at a time. */
\r
150 pChNext = ( signed char * )pcString;
\r
151 for( i = 0; i < usStringLength; i++ )
\r
153 /* Block until character has been transmitted. */
\r
154 while( xSerialPutChar( pxPort, *pChNext, comBLOCK_RETRY_TIME ) != pdTRUE ); pChNext++;
\r
157 /*-----------------------------------------------------------*/
\r
159 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
163 /* Place the character in the queue of characters to be transmitted. */
\r
164 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
169 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
170 queue and send it. This does not need to be in a critical section as
\r
171 if the interrupt has already removed the character the next interrupt
\r
172 will simply turn off the Tx interrupt again. */
\r
173 uart1->tx_mask = 1;
\r
177 /*-----------------------------------------------------------*/
\r
179 void vSerialClose( xComPortHandle xPort )
\r
181 /* Not supported as not required by the demo application. */
\r
184 /*-----------------------------------------------------------*/
\r
186 /* UART Tx interrupt handler. */
\r
187 void interrupt5_handler( void )
\r
189 /* This is a naked function. */
\r
190 portSAVE_CONTEXT();
\r
192 portRESTORE_CONTEXT();
\r
194 /*-----------------------------------------------------------*/
\r
196 static void prvTxHandler( void )
\r
199 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
201 /* The interrupt was caused by the transmit fifo having space for at least one
\r
202 character. Are there any more characters to transmit? */
\r
203 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
205 /* A character was retrieved from the queue so can be sent to the uart now. */
\r
206 uart1->tx_data = cChar;
\r
210 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
211 uart1->tx_mask = 0;
\r
214 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
215 ensure that the unblocked task is the task that executes when the interrupt
\r
216 completes if the unblocked task has a priority higher than the interrupted
\r
218 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
220 /*-----------------------------------------------------------*/
\r
222 /* UART Rx interrupt. */
\r
223 void interrupt6_handler( void )
\r
225 portSAVE_CONTEXT();
\r
227 portRESTORE_CONTEXT();
\r
229 /*-----------------------------------------------------------*/
\r
231 static void prvRxHandler( void )
\r
234 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
236 /* The interrupt was caused by the receiver getting data. */
\r
237 cChar = uart1->rx_data;
\r
239 xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
241 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
242 ensure that the unblocked task is the task that executes when the interrupt
\r
243 completes if the unblocked task has a priority higher than the interrupted
\r
245 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
247 /*-----------------------------------------------------------*/
\r