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
39 /* Scheduler include files. */
\r
40 #include "FreeRTOS.h"
\r
44 /* Demo app include files. */
\r
47 /* Hardware definitions. */
\r
48 #define serNO_PARITY ( ( unsigned char ) 0x02 << 3 )
\r
49 #define ser8DATA_BITS ( ( unsigned char ) 0x03 )
\r
50 #define ser1STOP_BIT ( ( unsigned char ) 0x07 )
\r
51 #define serSYSTEM_CLOCK ( ( unsigned char ) 0xdd )
\r
52 #define serTX_ENABLE ( ( unsigned char ) 0x04 )
\r
53 #define serRX_ENABLE ( ( unsigned char ) 0x01 )
\r
54 #define serTX_INT ( ( unsigned char ) 0x01 )
\r
55 #define serRX_INT ( ( unsigned char ) 0x02 )
\r
58 /* The queues used to communicate between tasks and ISR's. */
\r
59 static QueueHandle_t xRxedChars;
\r
60 static QueueHandle_t xCharsForTx;
\r
62 /* Flag used to indicate the tx status. */
\r
63 static portBASE_TYPE xTxHasEnded = pdTRUE;
\r
65 /*-----------------------------------------------------------*/
\r
67 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
69 const unsigned long ulBaudRateDivisor = ( configCPU_CLOCK_HZ / ( 32UL * ulWantedBaud ) );
\r
71 /* Create the queues used by the com test task. */
\r
72 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
73 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
75 xTxHasEnded = pdTRUE;
\r
77 /* Set the pins to UART mode. */
\r
78 MCF_GPIO_PUAPAR |= MCF_GPIO_PUAPAR_UTXD0_UTXD0;
\r
79 MCF_GPIO_PUAPAR |= MCF_GPIO_PUAPAR_URXD0_URXD0;
\r
81 /* Reset the peripheral. */
\r
82 MCF_UART0_UCR = MCF_UART_UCR_RESET_RX;
\r
83 MCF_UART0_UCR = MCF_UART_UCR_RESET_TX;
\r
84 MCF_UART0_UCR = MCF_UART_UCR_RESET_ERROR;
\r
85 MCF_UART0_UCR = MCF_UART_UCR_RESET_BKCHGINT;
\r
86 MCF_UART0_UCR = MCF_UART_UCR_RESET_MR | MCF_UART_UCR_RX_DISABLED | MCF_UART_UCR_TX_DISABLED;
\r
88 /* Configure the UART. */
\r
89 MCF_UART0_UMR1 = serNO_PARITY | ser8DATA_BITS;
\r
90 MCF_UART0_UMR2 = ser1STOP_BIT;
\r
91 MCF_UART0_UCSR = serSYSTEM_CLOCK;
\r
93 MCF_UART0_UBG1 = ( unsigned char ) ( ( ulBaudRateDivisor >> 8UL ) & 0xffUL );
\r
94 MCF_UART0_UBG2 = ( unsigned char ) ( ulBaudRateDivisor & 0xffUL );
\r
97 MCF_UART0_UCR = serTX_ENABLE | serRX_ENABLE;
\r
99 /* Configure the interrupt controller. Run the UARTs above the kernel
\r
100 interrupt priority for demo purposes. */
\r
101 MCF_INTC0_ICR13 = ( ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 1 ) << 3 );
\r
102 MCF_INTC0_IMRL &= ~( MCF_INTC_IMRL_INT_MASK13 | 0x01 );
\r
104 /* The Tx interrupt is not enabled until there is data to send. */
\r
105 MCF_UART0_UIMR = serRX_INT;
\r
107 /* Only a single port is implemented so we don't need to return anything. */
\r
110 /*-----------------------------------------------------------*/
\r
112 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
114 /* Only one port is supported. */
\r
117 /* Get the next character from the buffer. Return false if no characters
\r
118 are available or arrive before xBlockTime expires. */
\r
119 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
128 /*-----------------------------------------------------------*/
\r
130 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
132 /* Only one port is supported. */
\r
135 /* Return false if after the block time there is no room on the Tx queue. */
\r
136 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
141 /* A critical section should not be required as xTxHasEnded will not be
\r
142 written to by the ISR if it is already 0 (is this correct?). */
\r
143 if( xTxHasEnded != pdFALSE )
\r
145 xTxHasEnded = pdFALSE;
\r
146 MCF_UART0_UIMR = serRX_INT | serTX_INT;
\r
151 /*-----------------------------------------------------------*/
\r
153 void vSerialClose( xComPortHandle xPort )
\r
157 /*-----------------------------------------------------------*/
\r
159 __declspec(interrupt:0) void vUART0InterruptHandler( void )
\r
161 unsigned char ucChar;
\r
162 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xDoneSomething = pdTRUE;
\r
164 while( xDoneSomething != pdFALSE )
\r
166 xDoneSomething = pdFALSE;
\r
168 /* Does the tx buffer contain space? */
\r
169 if( ( MCF_UART0_USR & MCF_UART_USR_TXRDY ) != 0x00 )
\r
171 /* Are there any characters queued to be sent? */
\r
172 if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
174 /* Send the next char. */
\r
175 MCF_UART0_UTB = ucChar;
\r
176 xDoneSomething = pdTRUE;
\r
180 /* Turn off the Tx interrupt until such time as another character
\r
181 is being transmitted. */
\r
182 MCF_UART0_UIMR = serRX_INT;
\r
183 xTxHasEnded = pdTRUE;
\r
187 if( MCF_UART0_USR & MCF_UART_USR_RXRDY )
\r
189 ucChar = MCF_UART0_URB;
\r
190 xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );
\r
191 xDoneSomething = pdTRUE;
\r
195 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r