2 FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART1.
\r
70 /* Library includes. */
\r
71 #include "91x_lib.h"
\r
73 /* Scheduler includes. */
\r
74 #include "FreeRTOS.h"
\r
78 /* Demo application includes. */
\r
80 /*-----------------------------------------------------------*/
\r
83 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
84 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
85 #define serTX_BLOCK_TIME ( 40 / portTICK_RATE_MS )
\r
87 /* Interrupt and status bit definitions. */
\r
88 #define mainTXRIS 0x20
\r
89 #define mainRXRIS 0x50
\r
90 #define serTX_FIFO_FULL 0x20
\r
91 #define serCLEAR_ALL_INTERRUPTS 0x3ff
\r
92 /*-----------------------------------------------------------*/
\r
94 /* The queue used to hold received characters. */
\r
95 static xQueueHandle xRxedChars;
\r
97 /* The semaphore used to wake a task waiting for space to become available
\r
99 static xSemaphoreHandle xTxFIFOSemaphore;
\r
101 /*-----------------------------------------------------------*/
\r
103 /* UART interrupt handler. */
\r
104 void UART1_IRQHandler( void );
\r
106 /* The interrupt service routine - called from the assembly entry point. */
\r
107 __arm void UART1_IRQHandler( void );
\r
109 /*-----------------------------------------------------------*/
\r
111 /* Flag to indicate whether or not a task is blocked waiting for space on
\r
113 static long lTaskWaiting = pdFALSE;
\r
116 * See the serial2.h header file.
\r
118 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
120 xComPortHandle xReturn;
\r
121 UART_InitTypeDef xUART1_Init;
\r
122 GPIO_InitTypeDef GPIO_InitStructure;
\r
124 /* Create the queues used to hold Rx characters. */
\r
125 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
127 /* Create the semaphore used to wake a task waiting for space to become
\r
128 available in the FIFO. */
\r
129 vSemaphoreCreateBinary( xTxFIFOSemaphore );
\r
131 /* If the queue/semaphore was created correctly then setup the serial port
\r
133 if( ( xRxedChars != serINVALID_QUEUE ) && ( xTxFIFOSemaphore != serINVALID_QUEUE ) )
\r
135 /* Pre take the semaphore so a task will block if it tries to access
\r
137 xSemaphoreTake( xTxFIFOSemaphore, 0 );
\r
139 /* Configure the UART. */
\r
140 xUART1_Init.UART_WordLength = UART_WordLength_8D;
\r
141 xUART1_Init.UART_StopBits = UART_StopBits_1;
\r
142 xUART1_Init.UART_Parity = UART_Parity_No;
\r
143 xUART1_Init.UART_BaudRate = ulWantedBaud;
\r
144 xUART1_Init.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
\r
145 xUART1_Init.UART_Mode = UART_Mode_Tx_Rx;
\r
146 xUART1_Init.UART_FIFO = UART_FIFO_Enable;
\r
148 /* Enable the UART1 Clock */
\r
149 SCU_APBPeriphClockConfig( __UART1, ENABLE );
\r
151 /* Enable the GPIO3 Clock */
\r
152 SCU_APBPeriphClockConfig( __GPIO3, ENABLE );
\r
154 /* Configure UART1_Rx pin GPIO3.2 */
\r
155 GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
\r
156 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
\r
157 GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
\r
158 GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
\r
159 GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;
\r
160 GPIO_Init( GPIO3, &GPIO_InitStructure );
\r
162 /* Configure UART1_Tx pin GPIO3.3 */
\r
163 GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
\r
164 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
\r
165 GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
\r
166 GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
\r
167 GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2 ;
\r
168 GPIO_Init( GPIO3, &GPIO_InitStructure );
\r
171 portENTER_CRITICAL();
\r
173 /* Configure the UART itself. */
\r
174 UART_DeInit( UART1 );
\r
175 UART_Init( UART1, &xUART1_Init );
\r
176 UART_ITConfig( UART1, UART_IT_Receive | UART_IT_Transmit, ENABLE );
\r
177 UART1->ICR = serCLEAR_ALL_INTERRUPTS;
\r
178 UART_LoopBackConfig( UART1, DISABLE );
\r
179 UART_IrDACmd( IrDA1, DISABLE );
\r
181 /* Configure the VIC for the UART interrupts. */
\r
182 VIC_Config( UART1_ITLine, VIC_IRQ, 9 );
\r
183 VIC_ITCmd( UART1_ITLine, ENABLE );
\r
185 UART_Cmd( UART1, ENABLE );
\r
186 lTaskWaiting = pdFALSE;
\r
188 portEXIT_CRITICAL();
\r
192 xReturn = ( xComPortHandle ) 0;
\r
195 /* This demo file only supports a single port but we have to return
\r
196 something to comply with the standard demo header file. */
\r
199 /*-----------------------------------------------------------*/
\r
201 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
203 /* The port handle is not required as this driver only supports one port. */
\r
206 /* Get the next character from the buffer. Return false if no characters
\r
207 are available, or arrive before xBlockTime expires. */
\r
208 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
217 /*-----------------------------------------------------------*/
\r
219 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
221 signed char *pxNext;
\r
223 /* A couple of parameters that this port does not use. */
\r
224 ( void ) usStringLength;
\r
227 /* NOTE: This implementation does not handle the queue being full as no
\r
228 block time is used! */
\r
230 /* The port handle is not required as this driver only supports UART1. */
\r
233 /* Send each character in the string, one at a time. */
\r
234 pxNext = ( signed char * ) pcString;
\r
237 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
241 /*-----------------------------------------------------------*/
\r
243 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
245 portBASE_TYPE xReturn;
\r
247 portENTER_CRITICAL();
\r
249 /* Can we write to the FIFO? */
\r
250 if( UART1->FR & serTX_FIFO_FULL )
\r
252 /* Wait for the interrupt letting us know there is space on the
\r
253 FIFO. It is ok to block in a critical section, interrupts will be
\r
254 enabled for other tasks once we force a switch. */
\r
255 lTaskWaiting = pdTRUE;
\r
257 /* Just to be a bit different this driver uses a semaphore to
\r
258 block the sending task when the FIFO is full. The standard COMTest
\r
259 task assumes a queue of adequate length exists so does not use
\r
260 a block time. For this demo the block time is therefore hard
\r
262 xReturn = xSemaphoreTake( xTxFIFOSemaphore, serTX_BLOCK_TIME );
\r
265 UART1->DR = cOutChar;
\r
270 UART1->DR = cOutChar;
\r
274 portEXIT_CRITICAL();
\r
278 /*-----------------------------------------------------------*/
\r
280 void vSerialClose( xComPortHandle xPort )
\r
282 /* Not supported as not required by the demo application. */
\r
284 /*-----------------------------------------------------------*/
\r
286 void UART1_IRQHandler( void )
\r
289 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
291 while( UART1->RIS & mainRXRIS )
\r
293 /* The interrupt was caused by a character being received. Grab the
\r
294 character from the DR and place it in the queue of received
\r
297 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
300 if( UART1->RIS & mainTXRIS )
\r
302 if( lTaskWaiting == pdTRUE )
\r
304 /* This interrupt was caused by space becoming available on the Tx
\r
305 FIFO, wake any task that is waiting to post (if any). */
\r
306 xSemaphoreGiveFromISR( xTxFIFOSemaphore, &xHigherPriorityTaskWoken );
\r
307 lTaskWaiting = pdFALSE;
\r
310 UART1->ICR = mainTXRIS;
\r
313 /* If a task was woken by either a character being received or a character
\r
314 being transmitted then we may need to switch to another task. */
\r
315 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r