2 FreeRTOS V7.1.1 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART1.
\r
71 /* Library includes. */
\r
72 #include "91x_lib.h"
\r
74 /* Scheduler includes. */
\r
75 #include "FreeRTOS.h"
\r
79 /* Demo application includes. */
\r
81 /*-----------------------------------------------------------*/
\r
84 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
85 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
86 #define serTX_BLOCK_TIME ( 40 / portTICK_RATE_MS )
\r
88 /* Interrupt and status bit definitions. */
\r
89 #define mainTXRIS 0x20
\r
90 #define mainRXRIS 0x50
\r
91 #define serTX_FIFO_FULL 0x20
\r
92 #define serCLEAR_ALL_INTERRUPTS 0x3ff
\r
93 /*-----------------------------------------------------------*/
\r
95 /* The queue used to hold received characters. */
\r
96 static xQueueHandle xRxedChars;
\r
98 /* The semaphore used to wake a task waiting for space to become available
\r
100 static xSemaphoreHandle xTxFIFOSemaphore;
\r
102 /*-----------------------------------------------------------*/
\r
104 /* UART interrupt handler. */
\r
105 void UART1_IRQHandler( void );
\r
107 /* The interrupt service routine - called from the assembly entry point. */
\r
108 __arm void UART1_IRQHandler( void );
\r
110 /*-----------------------------------------------------------*/
\r
112 /* Flag to indicate whether or not a task is blocked waiting for space on
\r
114 static long lTaskWaiting = pdFALSE;
\r
117 * See the serial2.h header file.
\r
119 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
121 xComPortHandle xReturn;
\r
122 UART_InitTypeDef xUART1_Init;
\r
123 GPIO_InitTypeDef GPIO_InitStructure;
\r
125 /* Create the queues used to hold Rx characters. */
\r
126 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
128 /* Create the semaphore used to wake a task waiting for space to become
\r
129 available in the FIFO. */
\r
130 vSemaphoreCreateBinary( xTxFIFOSemaphore );
\r
132 /* If the queue/semaphore was created correctly then setup the serial port
\r
134 if( ( xRxedChars != serINVALID_QUEUE ) && ( xTxFIFOSemaphore != serINVALID_QUEUE ) )
\r
136 /* Pre take the semaphore so a task will block if it tries to access
\r
138 xSemaphoreTake( xTxFIFOSemaphore, 0 );
\r
140 /* Configure the UART. */
\r
141 xUART1_Init.UART_WordLength = UART_WordLength_8D;
\r
142 xUART1_Init.UART_StopBits = UART_StopBits_1;
\r
143 xUART1_Init.UART_Parity = UART_Parity_No;
\r
144 xUART1_Init.UART_BaudRate = ulWantedBaud;
\r
145 xUART1_Init.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
\r
146 xUART1_Init.UART_Mode = UART_Mode_Tx_Rx;
\r
147 xUART1_Init.UART_FIFO = UART_FIFO_Enable;
\r
149 /* Enable the UART1 Clock */
\r
150 SCU_APBPeriphClockConfig( __UART1, ENABLE );
\r
152 /* Enable the GPIO3 Clock */
\r
153 SCU_APBPeriphClockConfig( __GPIO3, ENABLE );
\r
155 /* Configure UART1_Rx pin GPIO3.2 */
\r
156 GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
\r
157 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
\r
158 GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
\r
159 GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
\r
160 GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;
\r
161 GPIO_Init( GPIO3, &GPIO_InitStructure );
\r
163 /* Configure UART1_Tx pin GPIO3.3 */
\r
164 GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
\r
165 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
\r
166 GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
\r
167 GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
\r
168 GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2 ;
\r
169 GPIO_Init( GPIO3, &GPIO_InitStructure );
\r
172 portENTER_CRITICAL();
\r
174 /* Configure the UART itself. */
\r
175 UART_DeInit( UART1 );
\r
176 UART_Init( UART1, &xUART1_Init );
\r
177 UART_ITConfig( UART1, UART_IT_Receive | UART_IT_Transmit, ENABLE );
\r
178 UART1->ICR = serCLEAR_ALL_INTERRUPTS;
\r
179 UART_LoopBackConfig( UART1, DISABLE );
\r
180 UART_IrDACmd( IrDA1, DISABLE );
\r
182 /* Configure the VIC for the UART interrupts. */
\r
183 VIC_Config( UART1_ITLine, VIC_IRQ, 9 );
\r
184 VIC_ITCmd( UART1_ITLine, ENABLE );
\r
186 UART_Cmd( UART1, ENABLE );
\r
187 lTaskWaiting = pdFALSE;
\r
189 portEXIT_CRITICAL();
\r
193 xReturn = ( xComPortHandle ) 0;
\r
196 /* This demo file only supports a single port but we have to return
\r
197 something to comply with the standard demo header file. */
\r
200 /*-----------------------------------------------------------*/
\r
202 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
204 /* The port handle is not required as this driver only supports one port. */
\r
207 /* Get the next character from the buffer. Return false if no characters
\r
208 are available, or arrive before xBlockTime expires. */
\r
209 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
218 /*-----------------------------------------------------------*/
\r
220 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
222 signed char *pxNext;
\r
224 /* A couple of parameters that this port does not use. */
\r
225 ( void ) usStringLength;
\r
228 /* NOTE: This implementation does not handle the queue being full as no
\r
229 block time is used! */
\r
231 /* The port handle is not required as this driver only supports UART1. */
\r
234 /* Send each character in the string, one at a time. */
\r
235 pxNext = ( signed char * ) pcString;
\r
238 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
242 /*-----------------------------------------------------------*/
\r
244 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
246 portBASE_TYPE xReturn;
\r
248 portENTER_CRITICAL();
\r
250 /* Can we write to the FIFO? */
\r
251 if( UART1->FR & serTX_FIFO_FULL )
\r
253 /* Wait for the interrupt letting us know there is space on the
\r
254 FIFO. It is ok to block in a critical section, interrupts will be
\r
255 enabled for other tasks once we force a switch. */
\r
256 lTaskWaiting = pdTRUE;
\r
258 /* Just to be a bit different this driver uses a semaphore to
\r
259 block the sending task when the FIFO is full. The standard COMTest
\r
260 task assumes a queue of adequate length exists so does not use
\r
261 a block time. For this demo the block time is therefore hard
\r
263 xReturn = xSemaphoreTake( xTxFIFOSemaphore, serTX_BLOCK_TIME );
\r
266 UART1->DR = cOutChar;
\r
271 UART1->DR = cOutChar;
\r
275 portEXIT_CRITICAL();
\r
279 /*-----------------------------------------------------------*/
\r
281 void vSerialClose( xComPortHandle xPort )
\r
283 /* Not supported as not required by the demo application. */
\r
285 /*-----------------------------------------------------------*/
\r
287 void UART1_IRQHandler( void )
\r
290 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
292 while( UART1->RIS & mainRXRIS )
\r
294 /* The interrupt was caused by a character being received. Grab the
\r
295 character from the DR and place it in the queue of received
\r
298 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
301 if( UART1->RIS & mainTXRIS )
\r
303 if( lTaskWaiting == pdTRUE )
\r
305 /* This interrupt was caused by space becoming available on the Tx
\r
306 FIFO, wake any task that is waiting to post (if any). */
\r
307 xSemaphoreGiveFromISR( xTxFIFOSemaphore, &xHigherPriorityTaskWoken );
\r
308 lTaskWaiting = pdFALSE;
\r
311 UART1->ICR = mainTXRIS;
\r
314 /* If a task was woken by either a character being received or a character
\r
315 being transmitted then we may need to switch to another task. */
\r
316 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r