2 FreeRTOS V9.0.1 - Copyright (C) 2017 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
73 + Clear overrun errors in the Rx ISR. Overrun errors prevent any further
\r
74 characters being received.
\r
78 + Use TickType_t in place of unsigned pdLONG for delay periods.
\r
79 + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.
\r
82 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
84 /* Scheduler header files. */
\r
85 #include "FreeRTOS.h"
\r
91 * Prototypes for ISR's. The PIC architecture means that these functions
\r
92 * have to be called from port.c. The prototypes are not however included
\r
93 * in the header as the header is common to all ports.
\r
95 void vSerialTxISR( void );
\r
96 void vSerialRxISR( void );
\r
98 /* Hardware pin definitions. */
\r
99 #define serTX_PIN TRISCbits.TRISC6
\r
100 #define serRX_PIN TRISCbits.TRISC7
\r
102 /* Bit/register definitions. */
\r
103 #define serINPUT ( 1 )
\r
104 #define serOUTPUT ( 0 )
\r
105 #define serTX_ENABLE ( ( unsigned short ) 1 )
\r
106 #define serRX_ENABLE ( ( unsigned short ) 1 )
\r
107 #define serHIGH_SPEED ( ( unsigned short ) 1 )
\r
108 #define serCONTINUOUS_RX ( ( unsigned short ) 1 )
\r
109 #define serCLEAR_OVERRUN ( ( unsigned short ) 0 )
\r
110 #define serINTERRUPT_ENABLED ( ( unsigned short ) 1 )
\r
111 #define serINTERRUPT_DISABLED ( ( unsigned short ) 0 )
\r
113 /* All ISR's use the PIC18 low priority interrupt. */
\r
114 #define serLOW_PRIORITY ( 0 )
\r
116 /*-----------------------------------------------------------*/
\r
118 /* Queues to interface between comms API and interrupt routines. */
\r
119 static QueueHandle_t xRxedChars;
\r
120 static QueueHandle_t xCharsForTx;
\r
122 /*-----------------------------------------------------------*/
\r
124 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
126 unsigned long ulBaud;
\r
128 /* Calculate the baud rate generator constant.
\r
129 SPBRG = ( (FOSC / Desired Baud Rate) / 16 ) - 1 */
\r
130 ulBaud = configCPU_CLOCK_HZ / ulWantedBaud;
\r
131 ulBaud /= ( unsigned long ) 16;
\r
132 ulBaud -= ( unsigned long ) 1;
\r
134 /* Create the queues used by the ISR's to interface to tasks. */
\r
135 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
136 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
138 portENTER_CRITICAL();
\r
140 /* Start with config registers cleared, so we can just set the wanted
\r
142 TXSTA = ( unsigned short ) 0;
\r
143 RCSTA = ( unsigned short ) 0;
\r
145 /* Set the baud rate generator using the above calculated constant. */
\r
146 SPBRG = ( unsigned char ) ulBaud;
\r
148 /* Setup the IO pins to enable the USART IO. */
\r
149 serTX_PIN = serOUTPUT;
\r
150 serRX_PIN = serINPUT;
\r
152 /* Set the serial interrupts to use the same priority as the tick. */
\r
153 IPR1bits.TXIP = serLOW_PRIORITY;
\r
154 IPR1bits.RCIP = serLOW_PRIORITY;
\r
156 /* Setup Tx configuration. */
\r
157 TXSTAbits.BRGH = serHIGH_SPEED;
\r
158 TXSTAbits.TXEN = serTX_ENABLE;
\r
160 /* Setup Rx configuration. */
\r
161 RCSTAbits.SPEN = serRX_ENABLE;
\r
162 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
164 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
165 we have data to send. */
\r
166 PIE1bits.RCIE = serINTERRUPT_ENABLED;
\r
168 portEXIT_CRITICAL();
\r
170 /* Unlike other ports, this serial code does not allow for more than one
\r
171 com port. We therefore don't return a pointer to a port structure and
\r
172 can instead just return NULL. */
\r
175 /*-----------------------------------------------------------*/
\r
177 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )
\r
179 /* This is not implemented in this port.
\r
180 Use xSerialPortInitMinimal() instead. */
\r
182 /*-----------------------------------------------------------*/
\r
184 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
186 /* Get the next character from the buffer. Return false if no characters
\r
187 are available, or arrive before xBlockTime expires. */
\r
188 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
197 /*-----------------------------------------------------------*/
\r
199 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
201 /* Return false if after the block time there is no room on the Tx queue. */
\r
202 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != pdPASS )
\r
207 /* Turn interrupt on - ensure the compiler only generates a single
\r
208 instruction for this. */
\r
209 PIE1bits.TXIE = serINTERRUPT_ENABLED;
\r
213 /*-----------------------------------------------------------*/
\r
215 void vSerialClose( xComPortHandle xPort )
\r
217 /* Not implemented for this port.
\r
218 To implement, turn off the interrupts and delete the memory
\r
219 allocated to the queues. */
\r
221 /*-----------------------------------------------------------*/
\r
223 #pragma interruptlow vSerialRxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
224 void vSerialRxISR( void )
\r
227 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
229 /* Get the character and post it on the queue of Rxed characters.
\r
230 If the post causes a task to wake force a context switch as the woken task
\r
231 may have a higher priority than the task we have interrupted. */
\r
234 /* Clear any overrun errors. */
\r
235 if( RCSTAbits.OERR )
\r
237 RCSTAbits.CREN = serCLEAR_OVERRUN;
\r
238 RCSTAbits.CREN = serCONTINUOUS_RX;
\r
241 xQueueSendFromISR( xRxedChars, ( const void * ) &cChar, &xHigherPriorityTaskWoken );
\r
243 if( xHigherPriorityTaskWoken )
\r
248 /*-----------------------------------------------------------*/
\r
250 #pragma interruptlow vSerialTxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")
\r
251 void vSerialTxISR( void )
\r
253 char cChar, cTaskWoken = pdFALSE;
\r
255 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
257 /* Send the next character queued for Tx. */
\r
262 /* Queue empty, nothing to send. */
\r
263 PIE1bits.TXIE = serINTERRUPT_DISABLED;
\r