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.
\r
32 NOTE: This driver is primarily to test the scheduler functionality. It does
\r
33 not effectively use the buffers or DMA and is therefore not intended to be
\r
34 an example of an efficient driver. */
\r
36 /* Standard include file. */
\r
40 /* Scheduler include files. */
\r
41 #include "FreeRTOS.h"
\r
45 /* Demo app include files. */
\r
48 /* Hardware setup. */
\r
49 #define serSET_FLAG ( 1 )
\r
51 /* The queues used to communicate between tasks and ISR's. */
\r
52 static QueueHandle_t xRxedChars;
\r
53 static QueueHandle_t xCharsForTx;
\r
55 /* Flag used to indicate the tx status. */
\r
56 static volatile portBASE_TYPE xTxHasEnded;
\r
58 /*-----------------------------------------------------------*/
\r
60 /* The UART interrupt handler. As this uses the FreeRTOS assembly interrupt
\r
61 entry point the IPL setting in the following prototype has no effect. The
\r
62 interrupt priority is set by the call to ConfigIntUART2() in
\r
63 xSerialPortInitMinimal(). */
\r
64 void __attribute__( (interrupt(IPL0AUTO), vector(_UART2_VECTOR))) vU2InterruptWrapper( void );
\r
66 /*-----------------------------------------------------------*/
\r
68 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
70 unsigned short usBRG;
\r
72 /* Create the queues used by the com test task. */
\r
73 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
74 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
76 /* Configure the UART and interrupts. */
\r
77 usBRG = (unsigned short)(( (float)configPERIPHERAL_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
\r
78 OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );
\r
79 ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_TX_INT_EN | UART_RX_INT_EN );
\r
81 xTxHasEnded = pdTRUE;
\r
83 /* Only a single port is implemented so we don't need to return anything. */
\r
86 /*-----------------------------------------------------------*/
\r
88 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
90 /* Only one port is supported. */
\r
93 /* Get the next character from the buffer. Return false if no characters
\r
94 are available or arrive before xBlockTime expires. */
\r
95 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
104 /*-----------------------------------------------------------*/
\r
106 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
108 signed portBASE_TYPE xReturn;
\r
110 /* Only one port is supported. */
\r
113 /* Return false if after the block time there is no room on the Tx queue. */
\r
114 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
123 if( xReturn != pdFAIL )
\r
125 /* A critical section should not be required as xTxHasEnded will not be
\r
126 written to by the ISR if it is already 0. */
\r
127 if( xTxHasEnded == pdTRUE )
\r
129 xTxHasEnded = pdFALSE;
\r
130 IFS1SET = _IFS1_U2TXIF_MASK;
\r
136 /*-----------------------------------------------------------*/
\r
138 void vSerialClose( xComPortHandle xPort )
\r
141 /*-----------------------------------------------------------*/
\r
143 void vU2InterruptHandler( void )
\r
145 /* Declared static to minimise stack use. */
\r
147 static portBASE_TYPE xHigherPriorityTaskWoken;
\r
149 xHigherPriorityTaskWoken = pdFALSE;
\r
151 /* Are any Rx interrupts pending? */
\r
152 if( IFS1bits.U2RXIF == 1)
\r
154 while( U2STAbits.URXDA )
\r
156 /* Retrieve the received character and place it in the queue of
\r
157 received characters. */
\r
159 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
161 IFS1CLR = _IFS1_U2RXIF_MASK;
\r
164 /* Are any Tx interrupts pending? */
\r
165 if( IFS1bits.U2TXIF == 1 )
\r
167 while( ( U2STAbits.UTXBF ) == 0 )
\r
169 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
171 /* Send the next character queued for Tx. */
\r
176 /* Queue empty, nothing to send. */
\r
177 xTxHasEnded = pdTRUE;
\r
182 IFS1CLR = _IFS1_U2TXIF_MASK;
\r
185 /* If sending or receiving necessitates a context switch, then switch now. */
\r
186 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r