3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
29 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.
31 Note that this driver is written to test the RTOS port and is not intended
32 to represent an optimised solution. */
34 /* Processor Expert generated includes. */
37 /* Scheduler include files. */
42 /* Demo application include files. */
45 /* The queues used to communicate between the task code and the interrupt
47 static QueueHandle_t xRxedChars;
48 static QueueHandle_t xCharsForTx;
50 /* Interrupt identification bits. */
51 #define serOVERRUN_INTERRUPT ( 0x08 )
52 #define serRX_INTERRUPT ( 0x20 )
53 #define serTX_INTERRUPT ( 0x80 )
55 /*-----------------------------------------------------------*/
59 * Initialise port for interrupt driven communications.
61 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
63 /* Hardware setup is performed by the Processor Expert generated code.
64 This function just creates the queues used to communicate between the
65 interrupt code and the task code - then sets the required baud rate. */
67 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
68 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
70 COM0_SetBaudRateMode( ( char ) ulWantedBaud );
74 /*-----------------------------------------------------------*/
76 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
78 /* Get the next character from the buffer queue. Return false if no characters
79 are available, or arrive before xBlockTime expires. */
80 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
89 /*-----------------------------------------------------------*/
91 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
93 /* Place the character in the queue of characters to be transmitted. */
94 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
99 /* Turn on the Tx interrupt so the ISR will remove the character from the
100 queue and send it. This does not need to be in a critical section as
101 if the interrupt has already removed the character the next interrupt
102 will simply turn off the Tx interrupt again. */
107 /*-----------------------------------------------------------*/
109 void vSerialClose( xComPortHandle xPort )
114 /*-----------------------------------------------------------*/
118 * Interrupt service routine for the serial port. Must be in non-banked
122 #pragma CODE_SEG __NEAR_SEG NON_BANKED
124 __interrupt void vCOM0_ISR( void )
126 volatile unsigned char ucByte, ucStatus;
127 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
129 /* What caused the interrupt? */
132 if( ucStatus & serOVERRUN_INTERRUPT )
134 /* The interrupt was caused by an overrun. Clear the error by reading
135 the data register. */
139 if( ucStatus & serRX_INTERRUPT )
141 /* The interrupt was caused by a character being received.
142 Read the received byte. */
145 /* Post the character onto the queue of received characters - noting
146 whether or not this wakes a task. */
147 xQueueSendFromISR( xRxedChars, ( void * ) &ucByte, &xHigherPriorityTaskWoken );
150 if( ( ucStatus & serTX_INTERRUPT ) && ( SCI0CR2_SCTIE ) )
152 /* The interrupt was caused by a character being transmitted. */
153 if( xQueueReceiveFromISR( xCharsForTx, ( void * ) &ucByte, &xHigherPriorityTaskWoken ) == pdTRUE )
155 /* Clear the SCRF bit. */
160 /* Disable transmit interrupt */
165 if( xHigherPriorityTaskWoken )
171 #pragma CODE_SEG DEFAULT