2 * FreeRTOS Kernel V10.2.0
\r
3 * Copyright (C) 2019 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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART1.
\r
31 ***Note*** This example uses queues to send each character into an interrupt
\r
32 service routine and out of an interrupt service routine individually. This
\r
33 is done to demonstrate queues being used in an interrupt, and to deliberately
\r
34 load the system to test the FreeRTOS port. It is *NOT* meant to be an
\r
35 example of an efficient implementation. An efficient implementation should
\r
36 use FIFO's or DMA if available, and only use FreeRTOS API functions when
\r
37 enough has been received to warrant a task being unblocked to process the
\r
41 /* Scheduler includes. */
\r
42 #include "FreeRTOS.h"
\r
45 #include "comtest2.h"
\r
47 /* Library includes. */
\r
50 /* Demo application includes. */
\r
51 #include "demo_serial.h"
\r
52 /*-----------------------------------------------------------*/
\r
55 #define serINVALID_QUEUE ( ( QueueHandle_t ) 0 )
\r
56 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
57 #define serPMC_USART_ID ( BOARD_ID_USART )
\r
59 /* The USART supported by this file. */
\r
60 #define serUSART_PORT ( USART1 )
\r
61 #define serUSART_IRQ ( USART1_IRQn )
\r
63 /* Every bit in the interrupt mask. */
\r
64 #define serMASK_ALL_INTERRUPTS ( 0xffffffffUL )
\r
66 /*-----------------------------------------------------------*/
\r
68 /* The queue used to hold received characters. */
\r
69 static QueueHandle_t xRxedChars;
\r
70 static QueueHandle_t xCharsForTx;
\r
72 /*-----------------------------------------------------------*/
\r
76 * See the serial.h header file.
\r
78 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
81 xComPortHandle xReturn;
\r
82 const sam_usart_opt_t xUSARTSettings =
\r
88 US_MR_CHMODE_NORMAL,
\r
89 0 /* Only used in IrDA mode. */
\r
92 /* Create the queues used to hold Rx/Tx characters. */
\r
93 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
94 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
96 /* If the queues were created correctly then setup the serial port
\r
98 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
100 /* Enable the peripheral clock in the PMC. */
\r
101 pmc_enable_periph_clk( serPMC_USART_ID );
\r
103 /* Configure USART in serial mode. */
\r
104 usart_init_rs232( serUSART_PORT, &xUSARTSettings, sysclk_get_cpu_hz() );
\r
106 /* Disable all the interrupts. */
\r
107 usart_disable_interrupt( serUSART_PORT, serMASK_ALL_INTERRUPTS );
\r
109 /* Enable the receiver and transmitter. */
\r
110 usart_enable_tx( serUSART_PORT );
\r
111 usart_enable_rx( serUSART_PORT );
\r
113 /* Clear any characters before enabling interrupt. */
\r
114 usart_getchar( serUSART_PORT, &ulChar );
\r
116 /* Enable Rx end interrupt. */
\r
117 usart_enable_interrupt( serUSART_PORT, US_IER_RXRDY );
\r
119 /* Configure and enable interrupt of USART. */
\r
120 NVIC_SetPriority( serUSART_IRQ, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
121 NVIC_EnableIRQ( serUSART_IRQ );
\r
125 xReturn = ( xComPortHandle ) 0;
\r
128 /* This demo file only supports a single port but we have to return
\r
129 something to comply with the standard demo header file. */
\r
132 /*-----------------------------------------------------------*/
\r
134 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
136 /* The port handle is not required as this driver only supports one port. */
\r
139 /* Get the next character from the buffer. Return false if no characters
\r
140 are available, or arrive before xBlockTime expires. */
\r
141 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
150 /*-----------------------------------------------------------*/
\r
152 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
154 signed char *pxNext;
\r
156 /* A couple of parameters that this port does not use. */
\r
157 ( void ) usStringLength;
\r
160 /* NOTE: This implementation does not handle the queue being full as no
\r
161 block time is used! */
\r
163 /* The port handle is not required as this driver only supports USART1. */
\r
166 /* Send each character in the string, one at a time. */
\r
167 pxNext = ( signed char * ) pcString;
\r
170 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
174 /*-----------------------------------------------------------*/
\r
176 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
178 signed portBASE_TYPE xReturn;
\r
180 /* This simple example only supports one port. */
\r
183 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
\r
186 usart_enable_interrupt( serUSART_PORT, US_IER_TXRDY );
\r
195 /*-----------------------------------------------------------*/
\r
197 void vSerialClose( xComPortHandle xPort )
\r
199 /* Not supported as not required by the demo application. */
\r
202 /*-----------------------------------------------------------*/
\r
205 * It should be noted that the com test tasks (which use make use of this file)
\r
206 * are included to demonstrate queues being used to communicate between tasks
\r
207 * and interrupts, and to demonstrate a context switch being performed from
\r
208 * inside an interrupt service routine. The serial driver used here is *not*
\r
209 * intended to represent an efficient implementation. Real applications should
\r
210 * make use of the USARTS peripheral DMA channel (PDC).
\r
212 void USART1_Handler( void )
\r
214 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
217 uint32_t ulUSARTStatus, ulUSARTMask;
\r
219 ulUSARTStatus = usart_get_status( serUSART_PORT );
\r
220 ulUSARTMask = usart_get_interrupt_mask( serUSART_PORT );
\r
221 ulUSARTStatus &= ulUSARTMask;
\r
223 if( ( ulUSARTStatus & US_CSR_TXRDY ) != 0UL )
\r
225 /* The interrupt was caused by the TX register becoming empty. Are
\r
226 there any more characters to transmit? */
\r
227 if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
229 /* A character was retrieved from the queue so can be sent to the
\r
231 usart_putchar( serUSART_PORT, ( uint32_t ) ucChar );
\r
235 usart_disable_interrupt( serUSART_PORT, US_IER_TXRDY );
\r
239 if( ( ulUSARTStatus & US_CSR_RXRDY ) != 0UL )
\r
241 /* A character has been received on the USART, send it to the Rx
\r
243 usart_getchar( serUSART_PORT, &ulChar );
\r
244 ucChar = ( uint8_t ) ( ulChar & 0xffUL );
\r
245 xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );
\r
248 /* If sending or receiving from a queue has caused a task to unblock, and
\r
249 the unblocked task has a priority equal to or higher than the currently
\r
250 running task (the task this ISR interrupted), then xHigherPriorityTaskWoken
\r
251 will have automatically been set to pdTRUE within the queue send or receive
\r
252 function. portEND_SWITCHING_ISR() will then ensure that this ISR returns
\r
253 directly to the higher priority unblocked task. */
\r
254 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r