2 FreeRTOS V8.1.1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
69 + Clear overrun errors in the Rx ISR. Overrun errors prevent any further
\r
70 characters being received.
\r
74 + Use TickType_t in place of unsigned pdLONG for delay periods.
\r
75 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
78 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
80 /* Scheduler header files. */
\r
81 #include "FreeRTOS.h"
\r
87 * Prototypes for ISR's. The PIC architecture means that these functions
\r
88 * have to be called from port.c. The prototypes are not however included
\r
89 * in the header as the header is common to all ports.
\r
91 void vSerialTxISR( void );
\r
92 void vSerialRxISR( void );
\r
94 /* Hardware pin definitions. */
\r
95 #define serTX_PIN TRISCbits.TRISC6
\r
96 #define serRX_PIN TRISCbits.TRISC7
\r
98 /* Bit/register definitions. */
\r
99 #define serINPUT ( 1 )
\r
100 #define serOUTPUT ( 0 )
\r
101 #define serTX_ENABLE ( ( unsigned short ) 1 )
\r
102 #define serRX_ENABLE ( ( unsigned short ) 1 )
\r
103 #define serHIGH_SPEED ( ( unsigned short ) 1 )
\r
104 #define serCONTINUOUS_RX ( ( unsigned short ) 1 )
\r
105 #define serCLEAR_OVERRUN ( ( unsigned short ) 0 )
\r
106 #define serINTERRUPT_ENABLED ( ( unsigned short ) 1 )
\r
107 #define serINTERRUPT_DISABLED ( ( unsigned short ) 0 )
\r
109 /* All ISR's use the PIC18 low priority interrupt. */
\r
110 #define serLOW_PRIORITY ( 0 )
\r
112 /*-----------------------------------------------------------*/
\r
114 /* Queues to interface between comms API and interrupt routines. */
\r
115 static QueueHandle_t xRxedChars;
\r
116 static QueueHandle_t xCharsForTx;
\r
118 /*-----------------------------------------------------------*/
\r
120 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
122 unsigned long ulBaud;
\r
124 /* Calculate the baud rate generator constant.
\r
125 SPBRG = ( (FOSC / Desired Baud Rate) / 16 ) - 1 */
\r
126 ulBaud = configCPU_CLOCK_HZ / ulWantedBaud;
\r
127 ulBaud /= ( unsigned long ) 16;
\r
128 ulBaud -= ( unsigned long ) 1;
\r
130 /* Create the queues used by the ISR's to interface to tasks. */
\r
131 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
132 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
134 portENTER_CRITICAL();
\r
136 /* Start with config registers cleared, so we can just set the wanted
\r
138 TXSTA = ( unsigned short ) 0;
\r
139 RCSTA = ( unsigned short ) 0;
\r
141 /* Set the baud rate generator using the above calculated constant. */
\r
142 SPBRG = ( unsigned char ) ulBaud;
\r
144 /* Setup the IO pins to enable the USART IO. */
\r
145 serTX_PIN = serOUTPUT;
\r
146 serRX_PIN = serINPUT;
\r
148 /* Set the serial interrupts to use the same priority as the tick. */
\r
149 IPR1bits.TXIP = serLOW_PRIORITY;
\r
150 IPR1bits.RCIP = serLOW_PRIORITY;
\r
152 /* Setup Tx configuration. */
\r
153 TXSTAbits.BRGH = serHIGH_SPEED;
\r
154 TXSTAbits.TXEN = serTX_ENABLE;
\r
156 /* Setup Rx configuration. */
\r
157 RCSTAbits.SPEN = serRX_ENABLE;
\r
158 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
160 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
161 we have data to send. */
\r
162 PIE1bits.RCIE = serINTERRUPT_ENABLED;
\r
164 portEXIT_CRITICAL();
\r
166 /* Unlike other ports, this serial code does not allow for more than one
\r
167 com port. We therefore don't return a pointer to a port structure and
\r
168 can instead just return NULL. */
\r
171 /*-----------------------------------------------------------*/
\r
173 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
175 /* This is not implemented in this port.
\r
176 Use xSerialPortInitMinimal() instead. */
\r
178 /*-----------------------------------------------------------*/
\r
180 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
182 /* Get the next character from the buffer. Return false if no characters
\r
183 are available, or arrive before xBlockTime expires. */
\r
184 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
193 /*-----------------------------------------------------------*/
\r
195 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
197 /* Return false if after the block time there is no room on the Tx queue. */
\r
198 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != pdPASS )
\r
203 /* Turn interrupt on - ensure the compiler only generates a single
\r
204 instruction for this. */
\r
205 PIE1bits.TXIE = serINTERRUPT_ENABLED;
\r
209 /*-----------------------------------------------------------*/
\r
211 void vSerialClose( xComPortHandle xPort )
\r
213 /* Not implemented for this port.
\r
214 To implement, turn off the interrupts and delete the memory
\r
215 allocated to the queues. */
\r
217 /*-----------------------------------------------------------*/
\r
219 #pragma interruptlow vSerialRxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
220 void vSerialRxISR( void )
\r
223 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
225 /* Get the character and post it on the queue of Rxed characters.
\r
226 If the post causes a task to wake force a context switch as the woken task
\r
227 may have a higher priority than the task we have interrupted. */
\r
230 /* Clear any overrun errors. */
\r
231 if( RCSTAbits.OERR )
\r
233 RCSTAbits.CREN = serCLEAR_OVERRUN;
\r
234 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
237 xQueueSendFromISR( xRxedChars, ( const void * ) &cChar, &xHigherPriorityTaskWoken );
\r
239 if( xHigherPriorityTaskWoken )
\r
244 /*-----------------------------------------------------------*/
\r
246 #pragma interruptlow vSerialTxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
247 void vSerialTxISR( void )
\r
249 char cChar, cTaskWoken = pdFALSE;
\r
251 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
253 /* Send the next character queued for Tx. */
\r
258 /* Queue empty, nothing to send. */
\r
259 PIE1bits.TXIE = serINTERRUPT_DISABLED;
\r