2 FreeRTOS V7.0.1 - Copyright (C) 2011 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 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
54 /* Basic interrupt driven serial port driver for uart1.
\r
57 /* Standard includes. */
\r
60 /* Kernel includes. */
\r
61 #include "FreeRTOS.h"
\r
65 /* Demo application includes. */
\r
66 #include <machine/uart.h>
\r
67 #include <machine/ic.h>
\r
69 /*-----------------------------------------------------------*/
\r
71 #define comBLOCK_RETRY_TIME 10
\r
72 /*-----------------------------------------------------------*/
\r
74 /* The interrupt handlers are naked functions that call C handlers. The C
\r
75 handlers are marked as noinline to ensure they work correctly when the
\r
77 void interrupt5_handler( void ) __attribute__((naked));
\r
78 static void prvTxHandler( void ) __attribute__((noinline));
\r
79 void interrupt6_handler( void ) __attribute__((naked));
\r
80 static void prvRxHandler( void ) __attribute__((noinline));
\r
82 /*-----------------------------------------------------------*/
\r
84 /* Queues used to hold received characters, and characters waiting to be
\r
86 static xQueueHandle xRxedChars;
\r
87 static xQueueHandle xCharsForTx;
\r
88 extern unsigned portBASE_TYPE *pxVectorTable;
\r
89 /*-----------------------------------------------------------*/
\r
91 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
93 /* Create the queues used to hold Rx and Tx characters. */
\r
94 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
95 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
97 if( ( xRxedChars ) && ( xCharsForTx ) )
\r
99 /* Set up interrupts */
\r
100 /* tx interrupt will be enabled when we need to send something */
\r
101 uart1->tx_mask = 0;
\r
102 uart1->rx_mask = 1;
\r
103 irq[IRQ_UART1_TX].ien = 1;
\r
104 irq[IRQ_UART1_TX].ipl = portSYSTEM_INTERRUPT_PRIORITY_LEVEL;
\r
105 irq[IRQ_UART1_RX].ien = 1;
\r
106 irq[IRQ_UART1_RX].ipl = portSYSTEM_INTERRUPT_PRIORITY_LEVEL;
\r
109 return ( xComPortHandle ) 0;
\r
111 /*-----------------------------------------------------------*/
\r
113 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
115 /* The port handle is not required as this driver only supports uart1. */
\r
118 /* Get the next character from the buffer. Return false if no characters
\r
119 are available, or arrive before xBlockTime expires. */
\r
120 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
129 /*-----------------------------------------------------------*/
\r
131 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
134 signed char *pChNext;
\r
136 /* Send each character in the string, one at a time. */
\r
137 pChNext = ( signed char * )pcString;
\r
138 for( i = 0; i < usStringLength; i++ )
\r
140 /* Block until character has been transmitted. */
\r
141 while( xSerialPutChar( pxPort, *pChNext, comBLOCK_RETRY_TIME ) != pdTRUE ); pChNext++;
\r
144 /*-----------------------------------------------------------*/
\r
146 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
150 /* Place the character in the queue of characters to be transmitted. */
\r
151 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
156 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
157 queue and send it. This does not need to be in a critical section as
\r
158 if the interrupt has already removed the character the next interrupt
\r
159 will simply turn off the Tx interrupt again. */
\r
160 uart1->tx_mask = 1;
\r
164 /*-----------------------------------------------------------*/
\r
166 void vSerialClose( xComPortHandle xPort )
\r
168 /* Not supported as not required by the demo application. */
\r
171 /*-----------------------------------------------------------*/
\r
173 /* UART Tx interrupt handler. */
\r
174 void interrupt5_handler( void )
\r
176 /* This is a naked function. */
\r
177 portSAVE_CONTEXT();
\r
179 portRESTORE_CONTEXT();
\r
181 /*-----------------------------------------------------------*/
\r
183 static void prvTxHandler( void )
\r
186 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
188 /* The interrupt was caused by the transmit fifo having space for at least one
\r
189 character. Are there any more characters to transmit? */
\r
190 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
192 /* A character was retrieved from the queue so can be sent to the uart now. */
\r
193 uart1->tx_data = cChar;
\r
197 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
198 uart1->tx_mask = 0;
\r
201 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
202 ensure that the unblocked task is the task that executes when the interrupt
\r
203 completes if the unblocked task has a priority higher than the interrupted
\r
205 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
207 /*-----------------------------------------------------------*/
\r
209 /* UART Rx interrupt. */
\r
210 void interrupt6_handler( void )
\r
212 portSAVE_CONTEXT();
\r
214 portRESTORE_CONTEXT();
\r
216 /*-----------------------------------------------------------*/
\r
218 static void prvRxHandler( void )
\r
221 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
223 /* The interrupt was caused by the receiver getting data. */
\r
224 cChar = uart1->rx_data;
\r
226 xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
228 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
229 ensure that the unblocked task is the task that executes when the interrupt
\r
230 completes if the unblocked task has a priority higher than the interrupted
\r
232 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
234 /*-----------------------------------------------------------*/
\r