2 FreeRTOS.org V5.1.0 - Copyright (C) 2003-2008 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
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
53 + Clear overrun errors in the Rx ISR. Overrun errors prevent any further
\r
54 characters being received.
\r
58 + Use portTickType in place of unsigned pdLONG for delay periods.
\r
59 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
62 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
64 /* Scheduler header files. */
\r
65 #include "FreeRTOS.h"
\r
71 * Prototypes for ISR's. The PIC architecture means that these functions
\r
72 * have to be called from port.c. The prototypes are not however included
\r
73 * in the header as the header is common to all ports.
\r
75 void vSerialTxISR( void );
\r
76 void vSerialRxISR( void );
\r
78 /* Hardware pin definitions. */
\r
79 #define serTX_PIN TRISCbits.TRISC6
\r
80 #define serRX_PIN TRISCbits.TRISC7
\r
82 /* Bit/register definitions. */
\r
83 #define serINPUT ( 1 )
\r
84 #define serOUTPUT ( 0 )
\r
85 #define serTX_ENABLE ( ( unsigned portSHORT ) 1 )
\r
86 #define serRX_ENABLE ( ( unsigned portSHORT ) 1 )
\r
87 #define serHIGH_SPEED ( ( unsigned portSHORT ) 1 )
\r
88 #define serCONTINUOUS_RX ( ( unsigned portSHORT ) 1 )
\r
89 #define serCLEAR_OVERRUN ( ( unsigned portSHORT ) 0 )
\r
90 #define serINTERRUPT_ENABLED ( ( unsigned portSHORT ) 1 )
\r
91 #define serINTERRUPT_DISABLED ( ( unsigned portSHORT ) 0 )
\r
93 /* All ISR's use the PIC18 low priority interrupt. */
\r
94 #define serLOW_PRIORITY ( 0 )
\r
96 /*-----------------------------------------------------------*/
\r
98 /* Queues to interface between comms API and interrupt routines. */
\r
99 static xQueueHandle xRxedChars;
\r
100 static xQueueHandle xCharsForTx;
\r
102 /*-----------------------------------------------------------*/
\r
104 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
106 unsigned portLONG ulBaud;
\r
108 /* Calculate the baud rate generator constant.
\r
109 SPBRG = ( (FOSC / Desired Baud Rate) / 16 ) - 1 */
\r
110 ulBaud = configCPU_CLOCK_HZ / ulWantedBaud;
\r
111 ulBaud /= ( unsigned portLONG ) 16;
\r
112 ulBaud -= ( unsigned portLONG ) 1;
\r
114 /* Create the queues used by the ISR's to interface to tasks. */
\r
115 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
116 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
118 portENTER_CRITICAL();
\r
120 /* Start with config registers cleared, so we can just set the wanted
\r
122 TXSTA = ( unsigned portSHORT ) 0;
\r
123 RCSTA = ( unsigned portSHORT ) 0;
\r
125 /* Set the baud rate generator using the above calculated constant. */
\r
126 SPBRG = ( unsigned portCHAR ) ulBaud;
\r
128 /* Setup the IO pins to enable the USART IO. */
\r
129 serTX_PIN = serOUTPUT;
\r
130 serRX_PIN = serINPUT;
\r
132 /* Set the serial interrupts to use the same priority as the tick. */
\r
133 IPR1bits.TXIP = serLOW_PRIORITY;
\r
134 IPR1bits.RCIP = serLOW_PRIORITY;
\r
136 /* Setup Tx configuration. */
\r
137 TXSTAbits.BRGH = serHIGH_SPEED;
\r
138 TXSTAbits.TXEN = serTX_ENABLE;
\r
140 /* Setup Rx configuration. */
\r
141 RCSTAbits.SPEN = serRX_ENABLE;
\r
142 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
144 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
145 we have data to send. */
\r
146 PIE1bits.RCIE = serINTERRUPT_ENABLED;
\r
148 portEXIT_CRITICAL();
\r
150 /* Unlike other ports, this serial code does not allow for more than one
\r
151 com port. We therefore don't return a pointer to a port structure and
\r
152 can instead just return NULL. */
\r
155 /*-----------------------------------------------------------*/
\r
157 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
159 /* This is not implemented in this port.
\r
160 Use xSerialPortInitMinimal() instead. */
\r
162 /*-----------------------------------------------------------*/
\r
164 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
166 /* Get the next character from the buffer. Return false if no characters
\r
167 are available, or arrive before xBlockTime expires. */
\r
168 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
177 /*-----------------------------------------------------------*/
\r
179 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
181 /* Return false if after the block time there is no room on the Tx queue. */
\r
182 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != pdPASS )
\r
187 /* Turn interrupt on - ensure the compiler only generates a single
\r
188 instruction for this. */
\r
189 PIE1bits.TXIE = serINTERRUPT_ENABLED;
\r
193 /*-----------------------------------------------------------*/
\r
195 void vSerialClose( xComPortHandle xPort )
\r
197 /* Not implemented for this port.
\r
198 To implement, turn off the interrupts and delete the memory
\r
199 allocated to the queues. */
\r
201 /*-----------------------------------------------------------*/
\r
203 #pragma interruptlow vSerialRxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
204 void vSerialRxISR( void )
\r
207 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
209 /* Get the character and post it on the queue of Rxed characters.
\r
210 If the post causes a task to wake force a context switch as the woken task
\r
211 may have a higher priority than the task we have interrupted. */
\r
214 /* Clear any overrun errors. */
\r
215 if( RCSTAbits.OERR )
\r
217 RCSTAbits.CREN = serCLEAR_OVERRUN;
\r
218 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
221 xQueueSendFromISR( xRxedChars, ( const void * ) &cChar, &xHigherPriorityTaskWoken );
\r
223 if( xHigherPriorityTaskWoken )
\r
228 /*-----------------------------------------------------------*/
\r
230 #pragma interruptlow vSerialTxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
231 void vSerialTxISR( void )
\r
233 portCHAR cChar, cTaskWoken = pdFALSE;
\r
235 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
237 /* Send the next character queued for Tx. */
\r
242 /* Queue empty, nothing to send. */
\r
243 PIE1bits.TXIE = serINTERRUPT_DISABLED;
\r