2 FreeRTOS V7.1.1 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 + Clear overrun errors in the Rx ISR. Overrun errors prevent any further
\r
71 characters being received.
\r
75 + Use portTickType in place of unsigned pdLONG for delay periods.
\r
76 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
79 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
81 /* Scheduler header files. */
\r
82 #include "FreeRTOS.h"
\r
88 * Prototypes for ISR's. The PIC architecture means that these functions
\r
89 * have to be called from port.c. The prototypes are not however included
\r
90 * in the header as the header is common to all ports.
\r
92 void vSerialTxISR( void );
\r
93 void vSerialRxISR( void );
\r
95 /* Hardware pin definitions. */
\r
96 #define serTX_PIN TRISCbits.TRISC6
\r
97 #define serRX_PIN TRISCbits.TRISC7
\r
99 /* Bit/register definitions. */
\r
100 #define serINPUT ( 1 )
\r
101 #define serOUTPUT ( 0 )
\r
102 #define serTX_ENABLE ( ( unsigned short ) 1 )
\r
103 #define serRX_ENABLE ( ( unsigned short ) 1 )
\r
104 #define serHIGH_SPEED ( ( unsigned short ) 1 )
\r
105 #define serCONTINUOUS_RX ( ( unsigned short ) 1 )
\r
106 #define serCLEAR_OVERRUN ( ( unsigned short ) 0 )
\r
107 #define serINTERRUPT_ENABLED ( ( unsigned short ) 1 )
\r
108 #define serINTERRUPT_DISABLED ( ( unsigned short ) 0 )
\r
110 /* All ISR's use the PIC18 low priority interrupt. */
\r
111 #define serLOW_PRIORITY ( 0 )
\r
113 /*-----------------------------------------------------------*/
\r
115 /* Queues to interface between comms API and interrupt routines. */
\r
116 static xQueueHandle xRxedChars;
\r
117 static xQueueHandle xCharsForTx;
\r
119 /*-----------------------------------------------------------*/
\r
121 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
123 unsigned long ulBaud;
\r
125 /* Calculate the baud rate generator constant.
\r
126 SPBRG = ( (FOSC / Desired Baud Rate) / 16 ) - 1 */
\r
127 ulBaud = configCPU_CLOCK_HZ / ulWantedBaud;
\r
128 ulBaud /= ( unsigned long ) 16;
\r
129 ulBaud -= ( unsigned long ) 1;
\r
131 /* Create the queues used by the ISR's to interface to tasks. */
\r
132 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
133 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
135 portENTER_CRITICAL();
\r
137 /* Start with config registers cleared, so we can just set the wanted
\r
139 TXSTA = ( unsigned short ) 0;
\r
140 RCSTA = ( unsigned short ) 0;
\r
142 /* Set the baud rate generator using the above calculated constant. */
\r
143 SPBRG = ( unsigned char ) ulBaud;
\r
145 /* Setup the IO pins to enable the USART IO. */
\r
146 serTX_PIN = serOUTPUT;
\r
147 serRX_PIN = serINPUT;
\r
149 /* Set the serial interrupts to use the same priority as the tick. */
\r
150 IPR1bits.TXIP = serLOW_PRIORITY;
\r
151 IPR1bits.RCIP = serLOW_PRIORITY;
\r
153 /* Setup Tx configuration. */
\r
154 TXSTAbits.BRGH = serHIGH_SPEED;
\r
155 TXSTAbits.TXEN = serTX_ENABLE;
\r
157 /* Setup Rx configuration. */
\r
158 RCSTAbits.SPEN = serRX_ENABLE;
\r
159 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
161 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
162 we have data to send. */
\r
163 PIE1bits.RCIE = serINTERRUPT_ENABLED;
\r
165 portEXIT_CRITICAL();
\r
167 /* Unlike other ports, this serial code does not allow for more than one
\r
168 com port. We therefore don't return a pointer to a port structure and
\r
169 can instead just return NULL. */
\r
172 /*-----------------------------------------------------------*/
\r
174 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
176 /* This is not implemented in this port.
\r
177 Use xSerialPortInitMinimal() instead. */
\r
179 /*-----------------------------------------------------------*/
\r
181 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
183 /* Get the next character from the buffer. Return false if no characters
\r
184 are available, or arrive before xBlockTime expires. */
\r
185 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
194 /*-----------------------------------------------------------*/
\r
196 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
198 /* Return false if after the block time there is no room on the Tx queue. */
\r
199 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != pdPASS )
\r
204 /* Turn interrupt on - ensure the compiler only generates a single
\r
205 instruction for this. */
\r
206 PIE1bits.TXIE = serINTERRUPT_ENABLED;
\r
210 /*-----------------------------------------------------------*/
\r
212 void vSerialClose( xComPortHandle xPort )
\r
214 /* Not implemented for this port.
\r
215 To implement, turn off the interrupts and delete the memory
\r
216 allocated to the queues. */
\r
218 /*-----------------------------------------------------------*/
\r
220 #pragma interruptlow vSerialRxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
221 void vSerialRxISR( void )
\r
224 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
226 /* Get the character and post it on the queue of Rxed characters.
\r
227 If the post causes a task to wake force a context switch as the woken task
\r
228 may have a higher priority than the task we have interrupted. */
\r
231 /* Clear any overrun errors. */
\r
232 if( RCSTAbits.OERR )
\r
234 RCSTAbits.CREN = serCLEAR_OVERRUN;
\r
235 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
238 xQueueSendFromISR( xRxedChars, ( const void * ) &cChar, &xHigherPriorityTaskWoken );
\r
240 if( xHigherPriorityTaskWoken )
\r
245 /*-----------------------------------------------------------*/
\r
247 #pragma interruptlow vSerialTxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
248 void vSerialTxISR( void )
\r
250 char cChar, cTaskWoken = pdFALSE;
\r
252 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
254 /* Send the next character queued for Tx. */
\r
259 /* Queue empty, nothing to send. */
\r
260 PIE1bits.TXIE = serINTERRUPT_DISABLED;
\r