2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
56 /* Library includes. */
\r
57 #include "75x_uart.h"
\r
58 #include "75x_gpio.h"
\r
59 #include "75x_eic.h"
\r
60 #include "75x_mrcc.h"
\r
62 /* Scheduler includes. */
\r
63 #include "FreeRTOS.h"
\r
66 /* Demo application includes. */
\r
69 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
70 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
72 /*-----------------------------------------------------------*/
\r
74 /* Queues used to hold received characters, and characters waiting to be
\r
76 static xQueueHandle xRxedChars;
\r
77 static xQueueHandle xCharsForTx;
\r
79 static volatile portBASE_TYPE xQueueEmpty = pdTRUE;
\r
81 /*-----------------------------------------------------------*/
\r
83 /* The interrupt service routine - called from the assembly entry point. */
\r
84 __arm void vSerialISR( void );
\r
86 /*-----------------------------------------------------------*/
\r
89 * See the serial2.h header file.
\r
91 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
93 xComPortHandle xReturn;
\r
94 UART_InitTypeDef UART_InitStructure;
\r
95 GPIO_InitTypeDef GPIO_InitStructure;
\r
96 EIC_IRQInitTypeDef EIC_IRQInitStructure;
\r
98 /* Create the queues used to hold Rx and Tx characters. */
\r
99 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
100 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
102 /* If the queues were created correctly then setup the serial port
\r
104 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
106 portENTER_CRITICAL();
\r
108 /* Enable the UART0 Clock. */
\r
109 MRCC_PeripheralClockConfig( MRCC_Peripheral_UART0, ENABLE );
\r
111 /* Configure the UART0_Tx as alternate function */
\r
112 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
\r
113 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
\r
114 GPIO_Init(GPIO0, &GPIO_InitStructure);
\r
116 /* Configure the UART0_Rx as input floating */
\r
117 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
\r
118 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
\r
119 GPIO_Init(GPIO0, &GPIO_InitStructure);
\r
121 /* Configure UART0. */
\r
122 UART_InitStructure.UART_WordLength = UART_WordLength_8D;
\r
123 UART_InitStructure.UART_StopBits = UART_StopBits_1;
\r
124 UART_InitStructure.UART_Parity = UART_Parity_No;
\r
125 UART_InitStructure.UART_BaudRate = ulWantedBaud;
\r
126 UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
\r
127 UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;
\r
128 UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */
\r
129 UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */
\r
130 UART_Init(UART0, &UART_InitStructure);
\r
132 /* Enable the UART0 */
\r
133 UART_Cmd(UART0, ENABLE);
\r
135 /* Configure the IEC for the UART interrupts. */
\r
136 EIC_IRQInitStructure.EIC_IRQChannelCmd = ENABLE;
\r
137 EIC_IRQInitStructure.EIC_IRQChannel = UART0_IRQChannel;
\r
138 EIC_IRQInitStructure.EIC_IRQChannelPriority = 1;
\r
139 EIC_IRQInit(&EIC_IRQInitStructure);
\r
141 xQueueEmpty = pdTRUE;
\r
142 UART_ITConfig( UART0, UART_IT_Transmit | UART_IT_Receive, ENABLE );
\r
144 portEXIT_CRITICAL();
\r
148 xReturn = ( xComPortHandle ) 0;
\r
151 /* This demo file only supports a single port but we have to return
\r
152 something to comply with the standard demo header file. */
\r
155 /*-----------------------------------------------------------*/
\r
157 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
159 /* The port handle is not required as this driver only supports one port. */
\r
162 /* Get the next character from the buffer. Return false if no characters
\r
163 are available, or arrive before xBlockTime expires. */
\r
164 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
173 /*-----------------------------------------------------------*/
\r
175 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
177 signed portCHAR *pxNext;
\r
179 /* A couple of parameters that this port does not use. */
\r
180 ( void ) usStringLength;
\r
183 /* NOTE: This implementation does not handle the queue being full as no
\r
184 block time is used! */
\r
186 /* The port handle is not required as this driver only supports UART0. */
\r
189 /* Send each character in the string, one at a time. */
\r
190 pxNext = ( signed portCHAR * ) pcString;
\r
193 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
197 /*-----------------------------------------------------------*/
\r
199 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
201 portBASE_TYPE xReturn;
\r
203 /* Place the character in the queue of characters to be transmitted. */
\r
204 portENTER_CRITICAL();
\r
206 if( xQueueEmpty == pdTRUE )
\r
208 UART0->DR = cOutChar;
\r
213 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
223 xQueueEmpty = pdFALSE;
\r
225 portEXIT_CRITICAL();
\r
229 /*-----------------------------------------------------------*/
\r
231 void vSerialClose( xComPortHandle xPort )
\r
233 /* Not supported as not required by the demo application. */
\r
235 /*-----------------------------------------------------------*/
\r
237 __arm void vSerialISR( void )
\r
239 signed portCHAR cChar;
\r
240 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
244 if( UART0->MIS & UART_IT_Transmit )
\r
246 /* The interrupt was caused by the THR becoming empty. Are there any
\r
247 more characters to transmit? */
\r
248 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
250 /* A character was retrieved from the queue so can be sent to the
\r
256 xQueueEmpty = pdTRUE;
\r
259 UART_ClearITPendingBit( UART0, UART_IT_Transmit );
\r
262 if( UART0->MIS & UART_IT_Receive )
\r
264 /* The interrupt was caused by a character being received. Grab the
\r
265 character from the RHR and place it in the queue of received
\r
268 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
269 UART_ClearITPendingBit( UART0, UART_IT_Receive );
\r
271 } while( UART0->MIS );
\r
273 /* If a task was woken by either a character being received or a character
\r
274 being transmitted then we may need to switch to another task. */
\r
275 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r