2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART1.
\r
32 ***Note*** This example uses queues to send each character into an interrupt
\r
33 service routine and out of an interrupt service routine individually. This
\r
34 is done to demonstrate queues being used in an interrupt, and to deliberately
\r
35 load the system to test the FreeRTOS port. It is *NOT* meant to be an
\r
36 example of an efficient implementation. An efficient implementation should
\r
37 use FIFO's or DMA if available, and only use FreeRTOS API functions when
\r
38 enough has been received to warrant a task being unblocked to process the
\r
42 /* Scheduler includes. */
\r
43 #include "FreeRTOS.h"
\r
46 #include "comtest2.h"
\r
48 /* Library includes. */
\r
51 /* Demo application includes. */
\r
52 #include "demo_serial.h"
\r
53 /*-----------------------------------------------------------*/
\r
56 #define serINVALID_QUEUE ( ( QueueHandle_t ) 0 )
\r
57 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
58 #define serPMC_USART_ID ( BOARD_ID_USART )
\r
60 /* The USART supported by this file. */
\r
61 #define serUSART_PORT ( USART1 )
\r
62 #define serUSART_IRQ ( USART1_IRQn )
\r
64 /* Every bit in the interrupt mask. */
\r
65 #define serMASK_ALL_INTERRUPTS ( 0xffffffffUL )
\r
67 /*-----------------------------------------------------------*/
\r
69 /* The queue used to hold received characters. */
\r
70 static QueueHandle_t xRxedChars;
\r
71 static QueueHandle_t xCharsForTx;
\r
73 /*-----------------------------------------------------------*/
\r
77 * See the serial.h header file.
\r
79 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
82 xComPortHandle xReturn;
\r
83 const sam_usart_opt_t xUSARTSettings =
\r
89 US_MR_CHMODE_NORMAL,
\r
90 0 /* Only used in IrDA mode. */
\r
93 /* Create the queues used to hold Rx/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 the queues were created correctly then setup the serial port
\r
99 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
101 /* Enable the peripheral clock in the PMC. */
\r
102 pmc_enable_periph_clk( serPMC_USART_ID );
\r
104 /* Configure USART in serial mode. */
\r
105 usart_init_rs232( serUSART_PORT, &xUSARTSettings, sysclk_get_cpu_hz() );
\r
107 /* Disable all the interrupts. */
\r
108 usart_disable_interrupt( serUSART_PORT, serMASK_ALL_INTERRUPTS );
\r
110 /* Enable the receiver and transmitter. */
\r
111 usart_enable_tx( serUSART_PORT );
\r
112 usart_enable_rx( serUSART_PORT );
\r
114 /* Clear any characters before enabling interrupt. */
\r
115 usart_getchar( serUSART_PORT, &ulChar );
\r
117 /* Enable Rx end interrupt. */
\r
118 usart_enable_interrupt( serUSART_PORT, US_IER_RXRDY );
\r
120 /* Configure and enable interrupt of USART. */
\r
121 NVIC_SetPriority( serUSART_IRQ, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
122 NVIC_EnableIRQ( serUSART_IRQ );
\r
126 xReturn = ( xComPortHandle ) 0;
\r
129 /* This demo file only supports a single port but we have to return
\r
130 something to comply with the standard demo header file. */
\r
133 /*-----------------------------------------------------------*/
\r
135 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
137 /* The port handle is not required as this driver only supports one port. */
\r
140 /* Get the next character from the buffer. Return false if no characters
\r
141 are available, or arrive before xBlockTime expires. */
\r
142 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
151 /*-----------------------------------------------------------*/
\r
153 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
155 signed char *pxNext;
\r
157 /* A couple of parameters that this port does not use. */
\r
158 ( void ) usStringLength;
\r
161 /* NOTE: This implementation does not handle the queue being full as no
\r
162 block time is used! */
\r
164 /* The port handle is not required as this driver only supports USART1. */
\r
167 /* Send each character in the string, one at a time. */
\r
168 pxNext = ( signed char * ) pcString;
\r
171 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
175 /*-----------------------------------------------------------*/
\r
177 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
179 signed portBASE_TYPE xReturn;
\r
181 /* This simple example only supports one port. */
\r
184 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
\r
187 usart_enable_interrupt( serUSART_PORT, US_IER_TXRDY );
\r
196 /*-----------------------------------------------------------*/
\r
198 void vSerialClose( xComPortHandle xPort )
\r
200 /* Not supported as not required by the demo application. */
\r
203 /*-----------------------------------------------------------*/
\r
206 * It should be noted that the com test tasks (which use make use of this file)
\r
207 * are included to demonstrate queues being used to communicate between tasks
\r
208 * and interrupts, and to demonstrate a context switch being performed from
\r
209 * inside an interrupt service routine. The serial driver used here is *not*
\r
210 * intended to represent an efficient implementation. Real applications should
\r
211 * make use of the USARTS peripheral DMA channel (PDC).
\r
213 void USART1_Handler( void )
\r
215 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
218 uint32_t ulUSARTStatus, ulUSARTMask;
\r
220 ulUSARTStatus = usart_get_status( serUSART_PORT );
\r
221 ulUSARTMask = usart_get_interrupt_mask( serUSART_PORT );
\r
222 ulUSARTStatus &= ulUSARTMask;
\r
224 if( ( ulUSARTStatus & US_CSR_TXRDY ) != 0UL )
\r
226 /* The interrupt was caused by the TX register becoming empty. Are
\r
227 there any more characters to transmit? */
\r
228 if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
230 /* A character was retrieved from the queue so can be sent to the
\r
232 usart_putchar( serUSART_PORT, ( uint32_t ) ucChar );
\r
236 usart_disable_interrupt( serUSART_PORT, US_IER_TXRDY );
\r
240 if( ( ulUSARTStatus & US_CSR_RXRDY ) != 0UL )
\r
242 /* A character has been received on the USART, send it to the Rx
\r
244 usart_getchar( serUSART_PORT, &ulChar );
\r
245 ucChar = ( uint8_t ) ( ulChar & 0xffUL );
\r
246 xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );
\r
249 /* If sending or receiving from a queue has caused a task to unblock, and
\r
250 the unblocked task has a priority equal to or higher than the currently
\r
251 running task (the task this ISR interrupted), then xHigherPriorityTaskWoken
\r
252 will have automatically been set to pdTRUE within the queue send or receive
\r
253 function. portEND_SWITCHING_ISR() will then ensure that this ISR returns
\r
254 directly to the higher priority unblocked task. */
\r
255 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r