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 * This file only supports UART 1
\r
35 /* Standard includes. */
\r
39 /* Scheduler includes. */
\r
40 #include "FreeRTOS.h"
\r
44 /* Demo application includes. */
\r
47 /* Constants required to setup the hardware. */
\r
48 #define serTX_AND_RX ( ( unsigned char ) 0x03 )
\r
50 /* Misc. constants. */
\r
51 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
53 /* Enable the UART Tx interrupt. */
\r
54 #define vInterruptOn() IFG2 |= UTXIFG1
\r
56 /* The queue used to hold received characters. */
\r
57 static QueueHandle_t xRxedChars;
\r
59 /* The queue used to hold characters waiting transmission. */
\r
60 static QueueHandle_t xCharsForTx;
\r
62 static volatile short sTHREEmpty;
\r
64 /* Interrupt service routines. */
\r
65 interrupt (UART1RX_VECTOR) wakeup vRxISR( void );
\r
66 interrupt (UART1TX_VECTOR) wakeup vTxISR( void );
\r
68 /*-----------------------------------------------------------*/
\r
70 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
72 unsigned long ulBaudRateCount;
\r
74 /* Initialise the hardware. */
\r
76 /* Generate the baud rate constants for the wanted baud rate. */
\r
77 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
\r
79 portENTER_CRITICAL();
\r
81 /* Create the queues used by the com test task. */
\r
82 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
83 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
88 /* Set pin function. */
\r
89 P4SEL |= serTX_AND_RX;
\r
91 /* All other bits remain at zero for n, 8, 1 interrupt driven operation.
\r
93 U1CTL |= CHAR + LISTEN;
\r
96 /* Setup baud rate low byte. */
\r
97 U1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
\r
99 /* Setup baud rate high byte. */
\r
100 ulBaudRateCount >>= 8UL;
\r
101 U1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
\r
103 /* Enable ports. */
\r
104 ME2 |= UTXE1 + URXE1;
\r
109 /* Nothing in the buffer yet. */
\r
110 sTHREEmpty = pdTRUE;
\r
112 /* Enable interrupts. */
\r
113 IE2 |= URXIE1 + UTXIE1;
\r
115 portEXIT_CRITICAL();
\r
117 /* Unlike other ports, this serial code does not allow for more than one
\r
118 com port. We therefore don't return a pointer to a port structure and can
\r
119 instead just return NULL. */
\r
122 /*-----------------------------------------------------------*/
\r
124 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
126 /* Get the next character from the buffer. Return false if no characters
\r
127 are available, or arrive before xBlockTime expires. */
\r
128 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
137 /*-----------------------------------------------------------*/
\r
139 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
141 signed portBASE_TYPE xReturn;
\r
143 /* Transmit a character. */
\r
145 portENTER_CRITICAL();
\r
147 if( sTHREEmpty == pdTRUE )
\r
149 /* If sTHREEmpty is true then the UART Tx ISR has indicated that
\r
150 there are no characters queued to be transmitted - so we can
\r
151 write the character directly to the shift Tx register. */
\r
152 sTHREEmpty = pdFALSE;
\r
153 U1TXBUF = cOutChar;
\r
158 /* sTHREEmpty is false, so there are still characters waiting to be
\r
159 transmitted. We have to queue this character so it gets
\r
160 transmitted in turn. */
\r
162 /* Return false if after the block time there is no room on the Tx
\r
163 queue. It is ok to block inside a critical section as each task
\r
164 maintains it's own critical section status. */
\r
165 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
167 /* Depending on queue sizing and task prioritisation: While we
\r
168 were blocked waiting to post on the queue interrupts were not
\r
169 disabled. It is possible that the serial ISR has emptied the
\r
170 Tx queue, in which case we need to start the Tx off again
\r
171 writing directly to the Tx register. */
\r
172 if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )
\r
174 /* Get back the character we just posted. */
\r
175 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
176 sTHREEmpty = pdFALSE;
\r
177 U1TXBUF = cOutChar;
\r
181 portEXIT_CRITICAL();
\r
185 /*-----------------------------------------------------------*/
\r
188 * UART RX interrupt service routine.
\r
190 interrupt (UART1RX_VECTOR) wakeup vRxISR( void )
\r
193 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
195 /* Get the character from the UART and post it on the queue of Rxed
\r
199 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
201 if( xHigherPriorityTaskWoken )
\r
203 /*If the post causes a task to wake force a context switch
\r
204 as the woken task may have a higher priority than the task we have
\r
209 /*-----------------------------------------------------------*/
\r
212 * UART Tx interrupt service routine.
\r
214 interrupt (UART1TX_VECTOR) wakeup vTxISR( void )
\r
217 portBASE_TYPE xTaskWoken = pdFALSE;
\r
219 /* The previous character has been transmitted. See if there are any
\r
220 further characters waiting transmission. */
\r
222 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
224 /* There was another character queued - transmit it now. */
\r
229 /* There were no other characters to transmit. */
\r
230 sTHREEmpty = pdTRUE;
\r