2 FreeRTOS V8.0.0:rc1 - 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 distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\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
68 + ISRcode removed. Is now pulled inline to reduce stack-usage.
\r
73 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
75 /* Scheduler header files. */
\r
76 #include "FreeRTOS.h"
\r
82 /* Hardware pin definitions. */
\r
83 #define serTX_PIN bTRC6
\r
84 #define serRX_PIN bTRC7
\r
86 /* Bit/register definitions. */
\r
87 #define serINPUT ( 1 )
\r
88 #define serOUTPUT ( 0 )
\r
89 #define serINTERRUPT_ENABLED ( 1 )
\r
91 /* All ISR's use the PIC18 low priority interrupt. */
\r
92 #define serLOW_PRIORITY ( 0 )
\r
94 /*-----------------------------------------------------------*/
\r
96 /* Queues to interface between comms API and interrupt routines. */
\r
97 QueueHandle_t xRxedChars;
\r
98 QueueHandle_t xCharsForTx;
\r
99 portBASE_TYPE xHigherPriorityTaskWoken;
\r
101 /*-----------------------------------------------------------*/
\r
103 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned char ucQueueLength )
\r
105 unsigned short usSPBRG;
\r
107 /* Create the queues used by the ISR's to interface to tasks. */
\r
108 xRxedChars = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
109 xCharsForTx = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
111 portENTER_CRITICAL();
\r
113 /* Setup the IO pins to enable the USART IO. */
\r
114 serTX_PIN = serINPUT; // YES really! See datasheet
\r
115 serRX_PIN = serINPUT;
\r
117 /* Set the TX config register. */
\r
118 TXSTA = 0b00100000;
\r
119 // ||||||||--bit0: TX9D = n/a
\r
120 // |||||||---bit1: TRMT = ReadOnly
\r
121 // ||||||----bit2: BRGH = High speed
\r
122 // |||||-----bit3: SENDB = n/a
\r
123 // ||||------bit4: SYNC = Asynchronous mode
\r
124 // |||-------bit5: TXEN = Transmit enable
\r
125 // ||--------bit6: TX9 = 8-bit transmission
\r
126 // |---------bit7: CSRC = n/a
\r
128 /* Set the Receive config register. */
\r
129 RCSTA = 0b10010000;
\r
130 // ||||||||--bit0: RX9D = ReadOnly
\r
131 // |||||||---bit1: OERR = ReadOnly
\r
132 // ||||||----bit2: FERR = ReadOnly
\r
133 // |||||-----bit3: ADDEN = n/a
\r
134 // ||||------bit4: CREN = Enable receiver
\r
135 // |||-------bit5: SREN = n/a
\r
136 // ||--------bit6: RX9 = 8-bit reception
\r
137 // |---------bit7: SPEN = Serial port enabled
\r
139 /* Calculate the baud rate generator value.
\r
140 We use low-speed (BRGH=0), the formula is
\r
141 SPBRG = ( ( FOSC / Desired Baud Rate ) / 64 ) - 1 */
\r
142 usSPBRG = ( ( APROCFREQ / ulWantedBaud ) / 64 ) - 1;
\r
143 if( usSPBRG > 255 )
\r
152 /* Set the serial interrupts to use the same priority as the tick. */
\r
153 bTXIP = serLOW_PRIORITY;
\r
154 bRCIP = serLOW_PRIORITY;
\r
156 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
157 we have data to send. */
\r
158 bRCIE = serINTERRUPT_ENABLED;
\r
160 portEXIT_CRITICAL();
\r
162 /* Unlike other ports, this serial code does not allow for more than one
\r
163 com port. We therefore don't return a pointer to a port structure and
\r
164 can instead just return NULL. */
\r
167 /*-----------------------------------------------------------*/
\r
169 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned char ucBufferLength )
\r
171 /* This is not implemented in this port.
\r
172 Use xSerialPortInitMinimal() instead. */
\r
175 /*-----------------------------------------------------------*/
\r
177 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, TickType_t xBlockTime )
\r
179 /* Get the next character from the buffer. Return false if no characters
\r
180 are available, or arrive before xBlockTime expires. */
\r
181 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
183 return ( char ) pdTRUE;
\r
186 return ( char ) pdFALSE;
\r
188 /*-----------------------------------------------------------*/
\r
190 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, TickType_t xBlockTime )
\r
192 /* Return false if after the block time there is no room on the Tx queue. */
\r
193 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != ( char ) pdPASS )
\r
198 /* Turn interrupt on - ensure the compiler only generates a single
\r
199 instruction for this. */
\r
200 bTXIE = serINTERRUPT_ENABLED;
\r
204 /*-----------------------------------------------------------*/
\r
206 void vSerialClose( xComPortHandle xPort )
\r
208 /* Not implemented for this port.
\r
209 To implement, turn off the interrupts and delete the memory
\r
210 allocated to the queues. */
\r