2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
74 Note this driver is used to test the FreeRTOS port. It is NOT intended to
\r
75 be an example of an efficient implementation!
\r
78 /* Standard includes. */
\r
81 /* Scheduler includes. */
\r
82 #include "FreeRTOS.h"
\r
86 /* Demo application includes. */
\r
89 /*-----------------------------------------------------------*/
\r
91 /* Constants to setup and access the UART. */
\r
92 #define serDLAB ( ( unsigned char ) 0x80 )
\r
93 #define serENABLE_INTERRUPTS ( ( unsigned char ) 0x03 )
\r
94 #define serNO_PARITY ( ( unsigned char ) 0x00 )
\r
95 #define ser1_STOP_BIT ( ( unsigned char ) 0x00 )
\r
96 #define ser8_BIT_CHARS ( ( unsigned char ) 0x03 )
\r
97 #define serFIFO_ON ( ( unsigned char ) 0x01 )
\r
98 #define serCLEAR_FIFO ( ( unsigned char ) 0x06 )
\r
99 #define serWANTED_CLOCK_SCALING ( ( unsigned long ) 16 )
\r
101 /* Constants to setup and access the VIC. */
\r
102 #define serU1VIC_CHANNEL ( ( unsigned long ) 0x0007 )
\r
103 #define serU1VIC_CHANNEL_BIT ( ( unsigned long ) 0x0080 )
\r
104 #define serU1VIC_ENABLE ( ( unsigned long ) 0x0020 )
\r
107 #define serINVALID_QUEUE ( ( QueueHandle_t ) 0 )
\r
108 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
109 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
111 /* Constant to access the VIC. */
\r
112 #define serCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
\r
114 /* Constants to determine the ISR source. */
\r
115 #define serSOURCE_THRE ( ( unsigned char ) 0x02 )
\r
116 #define serSOURCE_RX_TIMEOUT ( ( unsigned char ) 0x0c )
\r
117 #define serSOURCE_ERROR ( ( unsigned char ) 0x06 )
\r
118 #define serSOURCE_RX ( ( unsigned char ) 0x04 )
\r
119 #define serINTERRUPT_SOURCE_MASK ( ( unsigned char ) 0x0f )
\r
120 #define serINTERRUPT_IS_PENDING ( ( unsigned char ) 0x01 )
\r
122 /*-----------------------------------------------------------*/
\r
125 * The asm wrapper for the interrupt service routine.
\r
127 extern void vUART_ISREntry( void );
\r
130 * The C function called from the asm wrapper.
\r
132 void vUART_ISRHandler( void );
\r
134 /*-----------------------------------------------------------*/
\r
136 /* Queues used to hold received characters, and characters waiting to be
\r
138 static QueueHandle_t xRxedChars;
\r
139 static QueueHandle_t xCharsForTx;
\r
141 /* Communication flag between the interrupt service routine and serial API. */
\r
142 static volatile long lTHREEmpty;
\r
144 /*-----------------------------------------------------------*/
\r
146 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
148 unsigned long ulDivisor, ulWantedClock;
\r
149 xComPortHandle xReturn = serHANDLE;
\r
151 /* Create the queues used to hold Rx and Tx characters. */
\r
152 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
153 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
155 /* Initialise the THRE empty flag. */
\r
156 lTHREEmpty = pdTRUE;
\r
159 ( xRxedChars != serINVALID_QUEUE ) &&
\r
160 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
161 ( ulWantedBaud != ( unsigned long ) 0 )
\r
164 portENTER_CRITICAL()
\r
166 /* Setup the baud rate: Calculate the divisor value. */
\r
167 ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
\r
168 ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
\r
170 /* Set the DLAB bit so we can access the divisor. */
\r
173 /* Setup the divisor. */
\r
174 U1DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
176 U1DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
178 /* Turn on the FIFO's and clear the buffers. */
\r
179 U1FCR = ( serFIFO_ON | serCLEAR_FIFO );
\r
181 /* Setup transmission format. */
\r
182 U1LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
\r
184 /* Setup the VIC for the UART. */
\r
185 VICIntSelect &= ~( serU1VIC_CHANNEL_BIT );
\r
186 VICIntEnable |= serU1VIC_CHANNEL_BIT;
\r
187 VICVectAddr1 = ( unsigned long ) vUART_ISREntry;
\r
188 VICVectCntl1 = serU1VIC_CHANNEL | serU1VIC_ENABLE;
\r
190 /* Enable UART0 interrupts. */
\r
191 U1IER |= serENABLE_INTERRUPTS;
\r
193 portEXIT_CRITICAL();
\r
197 xReturn = ( xComPortHandle ) 0;
\r
202 /*-----------------------------------------------------------*/
\r
204 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
206 /* The port handle is not required as this driver only supports UART0. */
\r
209 /* Get the next character from the buffer. Return false if no characters
\r
210 are available, or arrive before xBlockTime expires. */
\r
211 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
220 /*-----------------------------------------------------------*/
\r
222 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
224 signed char *pxNext;
\r
226 /* NOTE: This implementation does not handle the queue being full as no
\r
227 block time is used! */
\r
229 /* The port handle is not required as this driver only supports UART0. */
\r
231 ( void ) usStringLength;
\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, TickType_t xBlockTime )
\r
245 signed portBASE_TYPE xReturn;
\r
247 /* The port handle is not required as this driver only supports UART0. */
\r
250 portENTER_CRITICAL();
\r
252 /* Is there space to write directly to the UART? */
\r
253 if( lTHREEmpty == ( long ) pdTRUE )
\r
255 /* We wrote the character directly to the UART, so was
\r
257 lTHREEmpty = pdFALSE;
\r
263 /* We cannot write directly to the UART, so queue the character.
\r
264 Block for a maximum of xBlockTime if there is no space in the
\r
265 queue. It is ok to block within a critical section as each
\r
266 task has it's own critical section management. */
\r
267 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
269 /* Depending on queue sizing and task prioritisation: While we
\r
270 were blocked waiting to post interrupts were not disabled. It is
\r
271 possible that the serial ISR has emptied the Tx queue, in which
\r
272 case we need to start the Tx off again. */
\r
273 if( lTHREEmpty == ( long ) pdTRUE )
\r
275 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
276 lTHREEmpty = pdFALSE;
\r
281 portEXIT_CRITICAL();
\r
285 /*-----------------------------------------------------------*/
\r
287 void vUART_ISRHandler( void )
\r
290 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
291 unsigned char ucInterrupt;
\r
293 ucInterrupt = U1IIR;
\r
295 /* The interrupt pending bit is active low. */
\r
296 while( ( ucInterrupt & serINTERRUPT_IS_PENDING ) == 0 )
\r
298 /* What caused the interrupt? */
\r
299 switch( ucInterrupt & serINTERRUPT_SOURCE_MASK )
\r
301 case serSOURCE_ERROR : /* Not handling this, but clear the interrupt. */
\r
305 case serSOURCE_THRE : /* The THRE is empty. If there is another
\r
306 character in the Tx queue, send it now. */
\r
307 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
313 /* There are no further characters
\r
314 queued to send so we can indicate
\r
315 that the THRE is available. */
\r
316 lTHREEmpty = pdTRUE;
\r
320 case serSOURCE_RX_TIMEOUT :
\r
321 case serSOURCE_RX : /* A character was received. Place it in
\r
322 the queue of received characters. */
\r
324 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
327 default : /* There is nothing to do, leave the ISR. */
\r
331 ucInterrupt = U1IIR;
\r
334 /* Clear the ISR in the VIC. */
\r
335 VICVectAddr = serCLEAR_VIC_INTERRUPT;
\r
337 /* Exit the ISR. If a task was woken by either a character being received
\r
338 or transmitted then a context switch will occur. */
\r
339 portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
341 /*-----------------------------------------------------------*/
\r