2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
57 + Clear overrun errors in the Rx ISR. Overrun errors prevent any further
\r
58 characters being received.
\r
62 + Use portTickType in place of unsigned pdLONG for delay periods.
\r
63 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
66 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
68 /* Scheduler header files. */
\r
69 #include "FreeRTOS.h"
\r
75 * Prototypes for ISR's. The PIC architecture means that these functions
\r
76 * have to be called from port.c. The prototypes are not however included
\r
77 * in the header as the header is common to all ports.
\r
79 void vSerialTxISR( void );
\r
80 void vSerialRxISR( void );
\r
82 /* Hardware pin definitions. */
\r
83 #define serTX_PIN TRISCbits.TRISC6
\r
84 #define serRX_PIN TRISCbits.TRISC7
\r
86 /* Bit/register definitions. */
\r
87 #define serINPUT ( 1 )
\r
88 #define serOUTPUT ( 0 )
\r
89 #define serTX_ENABLE ( ( unsigned short ) 1 )
\r
90 #define serRX_ENABLE ( ( unsigned short ) 1 )
\r
91 #define serHIGH_SPEED ( ( unsigned short ) 1 )
\r
92 #define serCONTINUOUS_RX ( ( unsigned short ) 1 )
\r
93 #define serCLEAR_OVERRUN ( ( unsigned short ) 0 )
\r
94 #define serINTERRUPT_ENABLED ( ( unsigned short ) 1 )
\r
95 #define serINTERRUPT_DISABLED ( ( unsigned short ) 0 )
\r
97 /* All ISR's use the PIC18 low priority interrupt. */
\r
98 #define serLOW_PRIORITY ( 0 )
\r
100 /*-----------------------------------------------------------*/
\r
102 /* Queues to interface between comms API and interrupt routines. */
\r
103 static xQueueHandle xRxedChars;
\r
104 static xQueueHandle xCharsForTx;
\r
106 /*-----------------------------------------------------------*/
\r
108 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
110 unsigned long ulBaud;
\r
112 /* Calculate the baud rate generator constant.
\r
113 SPBRG = ( (FOSC / Desired Baud Rate) / 16 ) - 1 */
\r
114 ulBaud = configCPU_CLOCK_HZ / ulWantedBaud;
\r
115 ulBaud /= ( unsigned long ) 16;
\r
116 ulBaud -= ( unsigned long ) 1;
\r
118 /* Create the queues used by the ISR's to interface to tasks. */
\r
119 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
120 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
122 portENTER_CRITICAL();
\r
124 /* Start with config registers cleared, so we can just set the wanted
\r
126 TXSTA = ( unsigned short ) 0;
\r
127 RCSTA = ( unsigned short ) 0;
\r
129 /* Set the baud rate generator using the above calculated constant. */
\r
130 SPBRG = ( unsigned char ) ulBaud;
\r
132 /* Setup the IO pins to enable the USART IO. */
\r
133 serTX_PIN = serOUTPUT;
\r
134 serRX_PIN = serINPUT;
\r
136 /* Set the serial interrupts to use the same priority as the tick. */
\r
137 IPR1bits.TXIP = serLOW_PRIORITY;
\r
138 IPR1bits.RCIP = serLOW_PRIORITY;
\r
140 /* Setup Tx configuration. */
\r
141 TXSTAbits.BRGH = serHIGH_SPEED;
\r
142 TXSTAbits.TXEN = serTX_ENABLE;
\r
144 /* Setup Rx configuration. */
\r
145 RCSTAbits.SPEN = serRX_ENABLE;
\r
146 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
148 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
149 we have data to send. */
\r
150 PIE1bits.RCIE = serINTERRUPT_ENABLED;
\r
152 portEXIT_CRITICAL();
\r
154 /* Unlike other ports, this serial code does not allow for more than one
\r
155 com port. We therefore don't return a pointer to a port structure and
\r
156 can instead just return NULL. */
\r
159 /*-----------------------------------------------------------*/
\r
161 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
163 /* This is not implemented in this port.
\r
164 Use xSerialPortInitMinimal() instead. */
\r
166 /*-----------------------------------------------------------*/
\r
168 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
170 /* Get the next character from the buffer. Return false if no characters
\r
171 are available, or arrive before xBlockTime expires. */
\r
172 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
181 /*-----------------------------------------------------------*/
\r
183 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
185 /* Return false if after the block time there is no room on the Tx queue. */
\r
186 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != pdPASS )
\r
191 /* Turn interrupt on - ensure the compiler only generates a single
\r
192 instruction for this. */
\r
193 PIE1bits.TXIE = serINTERRUPT_ENABLED;
\r
197 /*-----------------------------------------------------------*/
\r
199 void vSerialClose( xComPortHandle xPort )
\r
201 /* Not implemented for this port.
\r
202 To implement, turn off the interrupts and delete the memory
\r
203 allocated to the queues. */
\r
205 /*-----------------------------------------------------------*/
\r
207 #pragma interruptlow vSerialRxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
208 void vSerialRxISR( void )
\r
211 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
213 /* Get the character and post it on the queue of Rxed characters.
\r
214 If the post causes a task to wake force a context switch as the woken task
\r
215 may have a higher priority than the task we have interrupted. */
\r
218 /* Clear any overrun errors. */
\r
219 if( RCSTAbits.OERR )
\r
221 RCSTAbits.CREN = serCLEAR_OVERRUN;
\r
222 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
225 xQueueSendFromISR( xRxedChars, ( const void * ) &cChar, &xHigherPriorityTaskWoken );
\r
227 if( xHigherPriorityTaskWoken )
\r
232 /*-----------------------------------------------------------*/
\r
234 #pragma interruptlow vSerialTxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
235 void vSerialTxISR( void )
\r
237 char cChar, cTaskWoken = pdFALSE;
\r
239 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
241 /* Send the next character queued for Tx. */
\r
246 /* Queue empty, nothing to send. */
\r
247 PIE1bits.TXIE = serINTERRUPT_DISABLED;
\r