2 FreeRTOS V7.2.0 - 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
68 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART1.
\r
70 ***Note*** This example uses queues to send each character into an interrupt
\r
71 service routine and out of an interrupt service routine individually. This
\r
72 is done to demonstrate queues being used in an interrupt, and to deliberately
\r
73 load the system to test the FreeRTOS port. It is *NOT* meant to be an
\r
74 example of an efficient implementation. An efficient implementation should
\r
75 use FIFO's or DMA if available, and only use FreeRTOS API functions when
\r
76 enough has been received to warrant a task being unblocked to process the
\r
80 /* Scheduler includes. */
\r
81 #include "FreeRTOS.h"
\r
84 #include "comtest2.h"
\r
86 /* Library includes. */
\r
89 /* Demo application includes. */
\r
90 #include "demo_serial.h"
\r
91 /*-----------------------------------------------------------*/
\r
94 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
95 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
96 #define serPMC_USART_ID ( BOARD_ID_USART )
\r
98 /* The USART supported by this file. */
\r
99 #define serUSART_PORT ( USART1 )
\r
100 #define serUSART_IRQ ( USART1_IRQn )
\r
102 /* Every bit in the interrupt mask. */
\r
103 #define serMASK_ALL_INTERRUPTS ( 0xffffffffUL )
\r
105 /*-----------------------------------------------------------*/
\r
107 /* The queue used to hold received characters. */
\r
108 static xQueueHandle xRxedChars;
\r
109 static xQueueHandle xCharsForTx;
\r
111 /*-----------------------------------------------------------*/
\r
115 * See the serial.h header file.
\r
117 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
120 xComPortHandle xReturn;
\r
121 const sam_usart_opt_t xUSARTSettings =
\r
126 US_MR_NBSTOP_1_BIT,
\r
127 US_MR_CHMODE_NORMAL,
\r
128 0 /* Only used in IrDA mode. */
\r
131 /* Create the queues used to hold Rx/Tx characters. */
\r
132 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
133 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
135 /* If the queues were created correctly then setup the serial port
\r
137 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
139 /* Enable the peripheral clock in the PMC. */
\r
140 pmc_enable_periph_clk( serPMC_USART_ID );
\r
142 /* Configure USART in serial mode. */
\r
143 usart_init_rs232( serUSART_PORT, &xUSARTSettings, sysclk_get_cpu_hz() );
\r
145 /* Disable all the interrupts. */
\r
146 usart_disable_interrupt( serUSART_PORT, serMASK_ALL_INTERRUPTS );
\r
148 /* Enable the receiver and transmitter. */
\r
149 usart_enable_tx( serUSART_PORT );
\r
150 usart_enable_rx( serUSART_PORT );
\r
152 /* Clear any characters before enabling interrupt. */
\r
153 usart_getchar( serUSART_PORT, &ulChar );
\r
155 /* Enable Rx end interrupt. */
\r
156 usart_enable_interrupt( serUSART_PORT, US_IER_RXRDY );
\r
158 /* Configure and enable interrupt of USART. */
\r
159 NVIC_SetPriority( serUSART_IRQ, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
160 NVIC_EnableIRQ( serUSART_IRQ );
\r
164 xReturn = ( xComPortHandle ) 0;
\r
167 /* This demo file only supports a single port but we have to return
\r
168 something to comply with the standard demo header file. */
\r
171 /*-----------------------------------------------------------*/
\r
173 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
175 /* The port handle is not required as this driver only supports one port. */
\r
178 /* Get the next character from the buffer. Return false if no characters
\r
179 are available, or arrive before xBlockTime expires. */
\r
180 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
189 /*-----------------------------------------------------------*/
\r
191 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
193 signed char *pxNext;
\r
195 /* A couple of parameters that this port does not use. */
\r
196 ( void ) usStringLength;
\r
199 /* NOTE: This implementation does not handle the queue being full as no
\r
200 block time is used! */
\r
202 /* The port handle is not required as this driver only supports USART1. */
\r
205 /* Send each character in the string, one at a time. */
\r
206 pxNext = ( signed char * ) pcString;
\r
209 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
213 /*-----------------------------------------------------------*/
\r
215 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
217 signed portBASE_TYPE xReturn;
\r
219 /* This simple example only supports one port. */
\r
222 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
\r
225 usart_enable_interrupt( serUSART_PORT, US_IER_TXRDY );
\r
234 /*-----------------------------------------------------------*/
\r
236 void vSerialClose( xComPortHandle xPort )
\r
238 /* Not supported as not required by the demo application. */
\r
241 /*-----------------------------------------------------------*/
\r
244 * It should be noted that the com test tasks (which use make use of this file)
\r
245 * are included to demonstrate queues being used to communicate between tasks
\r
246 * and interrupts, and to demonstrate a context switch being performed from
\r
247 * inside an interrupt service routine. The serial driver used here is *not*
\r
248 * intended to represent an efficient implementation. Real applications should
\r
249 * make use of the USARTS peripheral DMA channel (PDC).
\r
251 void USART1_Handler( void )
\r
253 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
256 uint32_t ulUSARTStatus, ulUSARTMask;
\r
258 ulUSARTStatus = usart_get_status( serUSART_PORT );
\r
259 ulUSARTMask = usart_get_interrupt_mask( serUSART_PORT );
\r
260 ulUSARTStatus &= ulUSARTMask;
\r
262 if( ( ulUSARTStatus & US_CSR_TXRDY ) != 0UL )
\r
264 /* The interrupt was caused by the TX register becoming empty. Are
\r
265 there any more characters to transmit? */
\r
266 if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
268 /* A character was retrieved from the queue so can be sent to the
\r
270 usart_putchar( serUSART_PORT, ( uint32_t ) ucChar );
\r
274 usart_disable_interrupt( serUSART_PORT, US_IER_TXRDY );
\r
278 if( ( ulUSARTStatus & US_CSR_RXRDY ) != 0UL )
\r
280 /* A character has been received on the USART, send it to the Rx
\r
282 usart_getchar( serUSART_PORT, &ulChar );
\r
283 ucChar = ( uint8_t ) ( ulChar & 0xffUL );
\r
284 xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );
\r
287 /* If sending or receiving from a queue has caused a task to unblock, and
\r
288 the unblocked task has a priority equal to or higher than the currently
\r
289 running task (the task this ISR interrupted), then xHigherPriorityTaskWoken
\r
290 will have automatically been set to pdTRUE within the queue send or receive
\r
291 function. portEND_SWITCHING_ISR() will then ensure that this ISR returns
\r
292 directly to the higher priority unblocked task. */
\r
293 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r