2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
68 + Clear overrun errors in the Rx ISR. Overrun errors prevent any further
\r
69 characters being received.
\r
73 + Use portTickType in place of unsigned pdLONG for delay periods.
\r
74 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
77 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
79 /* Scheduler header files. */
\r
80 #include "FreeRTOS.h"
\r
86 * Prototypes for ISR's. The PIC architecture means that these functions
\r
87 * have to be called from port.c. The prototypes are not however included
\r
88 * in the header as the header is common to all ports.
\r
90 void vSerialTxISR( void );
\r
91 void vSerialRxISR( void );
\r
93 /* Hardware pin definitions. */
\r
94 #define serTX_PIN TRISCbits.TRISC6
\r
95 #define serRX_PIN TRISCbits.TRISC7
\r
97 /* Bit/register definitions. */
\r
98 #define serINPUT ( 1 )
\r
99 #define serOUTPUT ( 0 )
\r
100 #define serTX_ENABLE ( ( unsigned short ) 1 )
\r
101 #define serRX_ENABLE ( ( unsigned short ) 1 )
\r
102 #define serHIGH_SPEED ( ( unsigned short ) 1 )
\r
103 #define serCONTINUOUS_RX ( ( unsigned short ) 1 )
\r
104 #define serCLEAR_OVERRUN ( ( unsigned short ) 0 )
\r
105 #define serINTERRUPT_ENABLED ( ( unsigned short ) 1 )
\r
106 #define serINTERRUPT_DISABLED ( ( unsigned short ) 0 )
\r
108 /* All ISR's use the PIC18 low priority interrupt. */
\r
109 #define serLOW_PRIORITY ( 0 )
\r
111 /*-----------------------------------------------------------*/
\r
113 /* Queues to interface between comms API and interrupt routines. */
\r
114 static xQueueHandle xRxedChars;
\r
115 static xQueueHandle xCharsForTx;
\r
117 /*-----------------------------------------------------------*/
\r
119 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
121 unsigned long ulBaud;
\r
123 /* Calculate the baud rate generator constant.
\r
124 SPBRG = ( (FOSC / Desired Baud Rate) / 16 ) - 1 */
\r
125 ulBaud = configCPU_CLOCK_HZ / ulWantedBaud;
\r
126 ulBaud /= ( unsigned long ) 16;
\r
127 ulBaud -= ( unsigned long ) 1;
\r
129 /* Create the queues used by the ISR's to interface to tasks. */
\r
130 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
131 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
133 portENTER_CRITICAL();
\r
135 /* Start with config registers cleared, so we can just set the wanted
\r
137 TXSTA = ( unsigned short ) 0;
\r
138 RCSTA = ( unsigned short ) 0;
\r
140 /* Set the baud rate generator using the above calculated constant. */
\r
141 SPBRG = ( unsigned char ) ulBaud;
\r
143 /* Setup the IO pins to enable the USART IO. */
\r
144 serTX_PIN = serOUTPUT;
\r
145 serRX_PIN = serINPUT;
\r
147 /* Set the serial interrupts to use the same priority as the tick. */
\r
148 IPR1bits.TXIP = serLOW_PRIORITY;
\r
149 IPR1bits.RCIP = serLOW_PRIORITY;
\r
151 /* Setup Tx configuration. */
\r
152 TXSTAbits.BRGH = serHIGH_SPEED;
\r
153 TXSTAbits.TXEN = serTX_ENABLE;
\r
155 /* Setup Rx configuration. */
\r
156 RCSTAbits.SPEN = serRX_ENABLE;
\r
157 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
159 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
160 we have data to send. */
\r
161 PIE1bits.RCIE = serINTERRUPT_ENABLED;
\r
163 portEXIT_CRITICAL();
\r
165 /* Unlike other ports, this serial code does not allow for more than one
\r
166 com port. We therefore don't return a pointer to a port structure and
\r
167 can instead just return NULL. */
\r
170 /*-----------------------------------------------------------*/
\r
172 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
174 /* This is not implemented in this port.
\r
175 Use xSerialPortInitMinimal() instead. */
\r
177 /*-----------------------------------------------------------*/
\r
179 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
181 /* Get the next character from the buffer. Return false if no characters
\r
182 are available, or arrive before xBlockTime expires. */
\r
183 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
192 /*-----------------------------------------------------------*/
\r
194 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
196 /* Return false if after the block time there is no room on the Tx queue. */
\r
197 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != pdPASS )
\r
202 /* Turn interrupt on - ensure the compiler only generates a single
\r
203 instruction for this. */
\r
204 PIE1bits.TXIE = serINTERRUPT_ENABLED;
\r
208 /*-----------------------------------------------------------*/
\r
210 void vSerialClose( xComPortHandle xPort )
\r
212 /* Not implemented for this port.
\r
213 To implement, turn off the interrupts and delete the memory
\r
214 allocated to the queues. */
\r
216 /*-----------------------------------------------------------*/
\r
218 #pragma interruptlow vSerialRxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
219 void vSerialRxISR( void )
\r
222 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
224 /* Get the character and post it on the queue of Rxed characters.
\r
225 If the post causes a task to wake force a context switch as the woken task
\r
226 may have a higher priority than the task we have interrupted. */
\r
229 /* Clear any overrun errors. */
\r
230 if( RCSTAbits.OERR )
\r
232 RCSTAbits.CREN = serCLEAR_OVERRUN;
\r
233 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
236 xQueueSendFromISR( xRxedChars, ( const void * ) &cChar, &xHigherPriorityTaskWoken );
\r
238 if( xHigherPriorityTaskWoken )
\r
243 /*-----------------------------------------------------------*/
\r
245 #pragma interruptlow vSerialTxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
246 void vSerialTxISR( void )
\r
248 char cChar, cTaskWoken = pdFALSE;
\r
250 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
252 /* Send the next character queued for Tx. */
\r
257 /* Queue empty, nothing to send. */
\r
258 PIE1bits.TXIE = serINTERRUPT_DISABLED;
\r