2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
55 + Clear overrun errors in the Rx ISR. Overrun errors prevent any further
\r
56 characters being received.
\r
60 + Use portTickType in place of unsigned pdLONG for delay periods.
\r
61 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
64 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
66 /* Scheduler header files. */
\r
67 #include "FreeRTOS.h"
\r
73 * Prototypes for ISR's. The PIC architecture means that these functions
\r
74 * have to be called from port.c. The prototypes are not however included
\r
75 * in the header as the header is common to all ports.
\r
77 void vSerialTxISR( void );
\r
78 void vSerialRxISR( void );
\r
80 /* Hardware pin definitions. */
\r
81 #define serTX_PIN TRISCbits.TRISC6
\r
82 #define serRX_PIN TRISCbits.TRISC7
\r
84 /* Bit/register definitions. */
\r
85 #define serINPUT ( 1 )
\r
86 #define serOUTPUT ( 0 )
\r
87 #define serTX_ENABLE ( ( unsigned portSHORT ) 1 )
\r
88 #define serRX_ENABLE ( ( unsigned portSHORT ) 1 )
\r
89 #define serHIGH_SPEED ( ( unsigned portSHORT ) 1 )
\r
90 #define serCONTINUOUS_RX ( ( unsigned portSHORT ) 1 )
\r
91 #define serCLEAR_OVERRUN ( ( unsigned portSHORT ) 0 )
\r
92 #define serINTERRUPT_ENABLED ( ( unsigned portSHORT ) 1 )
\r
93 #define serINTERRUPT_DISABLED ( ( unsigned portSHORT ) 0 )
\r
95 /* All ISR's use the PIC18 low priority interrupt. */
\r
96 #define serLOW_PRIORITY ( 0 )
\r
98 /*-----------------------------------------------------------*/
\r
100 /* Queues to interface between comms API and interrupt routines. */
\r
101 static xQueueHandle xRxedChars;
\r
102 static xQueueHandle xCharsForTx;
\r
104 /*-----------------------------------------------------------*/
\r
106 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
108 unsigned portLONG ulBaud;
\r
110 /* Calculate the baud rate generator constant.
\r
111 SPBRG = ( (FOSC / Desired Baud Rate) / 16 ) - 1 */
\r
112 ulBaud = configCPU_CLOCK_HZ / ulWantedBaud;
\r
113 ulBaud /= ( unsigned portLONG ) 16;
\r
114 ulBaud -= ( unsigned portLONG ) 1;
\r
116 /* Create the queues used by the ISR's to interface to tasks. */
\r
117 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
118 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
120 portENTER_CRITICAL();
\r
122 /* Start with config registers cleared, so we can just set the wanted
\r
124 TXSTA = ( unsigned portSHORT ) 0;
\r
125 RCSTA = ( unsigned portSHORT ) 0;
\r
127 /* Set the baud rate generator using the above calculated constant. */
\r
128 SPBRG = ( unsigned portCHAR ) ulBaud;
\r
130 /* Setup the IO pins to enable the USART IO. */
\r
131 serTX_PIN = serOUTPUT;
\r
132 serRX_PIN = serINPUT;
\r
134 /* Set the serial interrupts to use the same priority as the tick. */
\r
135 IPR1bits.TXIP = serLOW_PRIORITY;
\r
136 IPR1bits.RCIP = serLOW_PRIORITY;
\r
138 /* Setup Tx configuration. */
\r
139 TXSTAbits.BRGH = serHIGH_SPEED;
\r
140 TXSTAbits.TXEN = serTX_ENABLE;
\r
142 /* Setup Rx configuration. */
\r
143 RCSTAbits.SPEN = serRX_ENABLE;
\r
144 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
146 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
147 we have data to send. */
\r
148 PIE1bits.RCIE = serINTERRUPT_ENABLED;
\r
150 portEXIT_CRITICAL();
\r
152 /* Unlike other ports, this serial code does not allow for more than one
\r
153 com port. We therefore don't return a pointer to a port structure and
\r
154 can instead just return NULL. */
\r
157 /*-----------------------------------------------------------*/
\r
159 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
161 /* This is not implemented in this port.
\r
162 Use xSerialPortInitMinimal() instead. */
\r
164 /*-----------------------------------------------------------*/
\r
166 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
168 /* Get the next character from the buffer. Return false if no characters
\r
169 are available, or arrive before xBlockTime expires. */
\r
170 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
179 /*-----------------------------------------------------------*/
\r
181 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
183 /* Return false if after the block time there is no room on the Tx queue. */
\r
184 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != pdPASS )
\r
189 /* Turn interrupt on - ensure the compiler only generates a single
\r
190 instruction for this. */
\r
191 PIE1bits.TXIE = serINTERRUPT_ENABLED;
\r
195 /*-----------------------------------------------------------*/
\r
197 void vSerialClose( xComPortHandle xPort )
\r
199 /* Not implemented for this port.
\r
200 To implement, turn off the interrupts and delete the memory
\r
201 allocated to the queues. */
\r
203 /*-----------------------------------------------------------*/
\r
205 #pragma interruptlow vSerialRxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
206 void vSerialRxISR( void )
\r
209 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
211 /* Get the character and post it on the queue of Rxed characters.
\r
212 If the post causes a task to wake force a context switch as the woken task
\r
213 may have a higher priority than the task we have interrupted. */
\r
216 /* Clear any overrun errors. */
\r
217 if( RCSTAbits.OERR )
\r
219 RCSTAbits.CREN = serCLEAR_OVERRUN;
\r
220 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
223 xQueueSendFromISR( xRxedChars, ( const void * ) &cChar, &xHigherPriorityTaskWoken );
\r
225 if( xHigherPriorityTaskWoken )
\r
230 /*-----------------------------------------------------------*/
\r
232 #pragma interruptlow vSerialTxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
233 void vSerialTxISR( void )
\r
235 portCHAR cChar, cTaskWoken = pdFALSE;
\r
237 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
239 /* Send the next character queued for Tx. */
\r
244 /* Queue empty, nothing to send. */
\r
245 PIE1bits.TXIE = serINTERRUPT_DISABLED;
\r