2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART1.
\r
58 /* Library includes. */
\r
59 #include "91x_lib.h"
\r
61 /* Scheduler includes. */
\r
62 #include "FreeRTOS.h"
\r
66 /* Demo application includes. */
\r
68 /*-----------------------------------------------------------*/
\r
71 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
72 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
73 #define serTX_BLOCK_TIME ( 40 / portTICK_RATE_MS )
\r
75 /* Interrupt and status bit definitions. */
\r
76 #define mainTXRIS 0x20
\r
77 #define mainRXRIS 0x50
\r
78 #define serTX_FIFO_FULL 0x20
\r
79 #define serCLEAR_ALL_INTERRUPTS 0x3ff
\r
80 /*-----------------------------------------------------------*/
\r
82 /* The queue used to hold received characters. */
\r
83 static xQueueHandle xRxedChars;
\r
85 /* The semaphore used to wake a task waiting for space to become available
\r
87 static xSemaphoreHandle xTxFIFOSemaphore;
\r
89 /*-----------------------------------------------------------*/
\r
91 /* UART interrupt handler. */
\r
92 void UART1_IRQHandler( void );
\r
94 /* The interrupt service routine - called from the assembly entry point. */
\r
95 __arm void UART1_IRQHandler( void );
\r
97 /*-----------------------------------------------------------*/
\r
99 /* Flag to indicate whether or not a task is blocked waiting for space on
\r
101 static long lTaskWaiting = pdFALSE;
\r
104 * See the serial2.h header file.
\r
106 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
108 xComPortHandle xReturn;
\r
109 UART_InitTypeDef xUART1_Init;
\r
110 GPIO_InitTypeDef GPIO_InitStructure;
\r
112 /* Create the queues used to hold Rx characters. */
\r
113 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
115 /* Create the semaphore used to wake a task waiting for space to become
\r
116 available in the FIFO. */
\r
117 vSemaphoreCreateBinary( xTxFIFOSemaphore );
\r
119 /* If the queue/semaphore was created correctly then setup the serial port
\r
121 if( ( xRxedChars != serINVALID_QUEUE ) && ( xTxFIFOSemaphore != serINVALID_QUEUE ) )
\r
123 /* Pre take the semaphore so a task will block if it tries to access
\r
125 xSemaphoreTake( xTxFIFOSemaphore, 0 );
\r
127 /* Configure the UART. */
\r
128 xUART1_Init.UART_WordLength = UART_WordLength_8D;
\r
129 xUART1_Init.UART_StopBits = UART_StopBits_1;
\r
130 xUART1_Init.UART_Parity = UART_Parity_No;
\r
131 xUART1_Init.UART_BaudRate = ulWantedBaud;
\r
132 xUART1_Init.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
\r
133 xUART1_Init.UART_Mode = UART_Mode_Tx_Rx;
\r
134 xUART1_Init.UART_FIFO = UART_FIFO_Enable;
\r
136 /* Enable the UART1 Clock */
\r
137 SCU_APBPeriphClockConfig( __UART1, ENABLE );
\r
139 /* Enable the GPIO3 Clock */
\r
140 SCU_APBPeriphClockConfig( __GPIO3, ENABLE );
\r
142 /* Configure UART1_Rx pin GPIO3.2 */
\r
143 GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
\r
144 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
\r
145 GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
\r
146 GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
\r
147 GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;
\r
148 GPIO_Init( GPIO3, &GPIO_InitStructure );
\r
150 /* Configure UART1_Tx pin GPIO3.3 */
\r
151 GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
\r
152 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
\r
153 GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
\r
154 GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
\r
155 GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2 ;
\r
156 GPIO_Init( GPIO3, &GPIO_InitStructure );
\r
159 portENTER_CRITICAL();
\r
161 /* Configure the UART itself. */
\r
162 UART_DeInit( UART1 );
\r
163 UART_Init( UART1, &xUART1_Init );
\r
164 UART_ITConfig( UART1, UART_IT_Receive | UART_IT_Transmit, ENABLE );
\r
165 UART1->ICR = serCLEAR_ALL_INTERRUPTS;
\r
166 UART_LoopBackConfig( UART1, DISABLE );
\r
167 UART_IrDACmd( IrDA1, DISABLE );
\r
169 /* Configure the VIC for the UART interrupts. */
\r
170 VIC_Config( UART1_ITLine, VIC_IRQ, 9 );
\r
171 VIC_ITCmd( UART1_ITLine, ENABLE );
\r
173 UART_Cmd( UART1, ENABLE );
\r
174 lTaskWaiting = pdFALSE;
\r
176 portEXIT_CRITICAL();
\r
180 xReturn = ( xComPortHandle ) 0;
\r
183 /* This demo file only supports a single port but we have to return
\r
184 something to comply with the standard demo header file. */
\r
187 /*-----------------------------------------------------------*/
\r
189 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
191 /* The port handle is not required as this driver only supports one port. */
\r
194 /* Get the next character from the buffer. Return false if no characters
\r
195 are available, or arrive before xBlockTime expires. */
\r
196 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
205 /*-----------------------------------------------------------*/
\r
207 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
209 signed char *pxNext;
\r
211 /* A couple of parameters that this port does not use. */
\r
212 ( void ) usStringLength;
\r
215 /* NOTE: This implementation does not handle the queue being full as no
\r
216 block time is used! */
\r
218 /* The port handle is not required as this driver only supports UART1. */
\r
221 /* Send each character in the string, one at a time. */
\r
222 pxNext = ( signed char * ) pcString;
\r
225 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
229 /*-----------------------------------------------------------*/
\r
231 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
233 portBASE_TYPE xReturn;
\r
235 portENTER_CRITICAL();
\r
237 /* Can we write to the FIFO? */
\r
238 if( UART1->FR & serTX_FIFO_FULL )
\r
240 /* Wait for the interrupt letting us know there is space on the
\r
241 FIFO. It is ok to block in a critical section, interrupts will be
\r
242 enabled for other tasks once we force a switch. */
\r
243 lTaskWaiting = pdTRUE;
\r
245 /* Just to be a bit different this driver uses a semaphore to
\r
246 block the sending task when the FIFO is full. The standard COMTest
\r
247 task assumes a queue of adequate length exists so does not use
\r
248 a block time. For this demo the block time is therefore hard
\r
250 xReturn = xSemaphoreTake( xTxFIFOSemaphore, serTX_BLOCK_TIME );
\r
253 UART1->DR = cOutChar;
\r
258 UART1->DR = cOutChar;
\r
262 portEXIT_CRITICAL();
\r
266 /*-----------------------------------------------------------*/
\r
268 void vSerialClose( xComPortHandle xPort )
\r
270 /* Not supported as not required by the demo application. */
\r
272 /*-----------------------------------------------------------*/
\r
274 void UART1_IRQHandler( void )
\r
277 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
279 while( UART1->RIS & mainRXRIS )
\r
281 /* The interrupt was caused by a character being received. Grab the
\r
282 character from the DR and place it in the queue of received
\r
285 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
288 if( UART1->RIS & mainTXRIS )
\r
290 if( lTaskWaiting == pdTRUE )
\r
292 /* This interrupt was caused by space becoming available on the Tx
\r
293 FIFO, wake any task that is waiting to post (if any). */
\r
294 xSemaphoreGiveFromISR( xTxFIFOSemaphore, &xHigherPriorityTaskWoken );
\r
295 lTaskWaiting = pdFALSE;
\r
298 UART1->ICR = mainTXRIS;
\r
301 /* If a task was woken by either a character being received or a character
\r
302 being transmitted then we may need to switch to another task. */
\r
303 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r