2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
71 + ISRcode removed. Is now pulled inline to reduce stack-usage.
\r
76 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
78 /* Scheduler header files. */
\r
79 #include "FreeRTOS.h"
\r
85 /* Hardware pin definitions. */
\r
86 #define serTX_PIN bTRC6
\r
87 #define serRX_PIN bTRC7
\r
89 /* Bit/register definitions. */
\r
90 #define serINPUT ( 1 )
\r
91 #define serOUTPUT ( 0 )
\r
92 #define serINTERRUPT_ENABLED ( 1 )
\r
94 /* All ISR's use the PIC18 low priority interrupt. */
\r
95 #define serLOW_PRIORITY ( 0 )
\r
97 /*-----------------------------------------------------------*/
\r
99 /* Queues to interface between comms API and interrupt routines. */
\r
100 xQueueHandle xRxedChars;
\r
101 xQueueHandle xCharsForTx;
\r
102 portBASE_TYPE xHigherPriorityTaskWoken;
\r
104 /*-----------------------------------------------------------*/
\r
106 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned char ucQueueLength )
\r
108 unsigned short usSPBRG;
\r
110 /* Create the queues used by the ISR's to interface to tasks. */
\r
111 xRxedChars = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
112 xCharsForTx = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
114 portENTER_CRITICAL();
\r
116 /* Setup the IO pins to enable the USART IO. */
\r
117 serTX_PIN = serINPUT; // YES really! See datasheet
\r
118 serRX_PIN = serINPUT;
\r
120 /* Set the TX config register. */
\r
121 TXSTA = 0b00100000;
\r
122 // ||||||||--bit0: TX9D = n/a
\r
123 // |||||||---bit1: TRMT = ReadOnly
\r
124 // ||||||----bit2: BRGH = High speed
\r
125 // |||||-----bit3: SENDB = n/a
\r
126 // ||||------bit4: SYNC = Asynchronous mode
\r
127 // |||-------bit5: TXEN = Transmit enable
\r
128 // ||--------bit6: TX9 = 8-bit transmission
\r
129 // |---------bit7: CSRC = n/a
\r
131 /* Set the Receive config register. */
\r
132 RCSTA = 0b10010000;
\r
133 // ||||||||--bit0: RX9D = ReadOnly
\r
134 // |||||||---bit1: OERR = ReadOnly
\r
135 // ||||||----bit2: FERR = ReadOnly
\r
136 // |||||-----bit3: ADDEN = n/a
\r
137 // ||||------bit4: CREN = Enable receiver
\r
138 // |||-------bit5: SREN = n/a
\r
139 // ||--------bit6: RX9 = 8-bit reception
\r
140 // |---------bit7: SPEN = Serial port enabled
\r
142 /* Calculate the baud rate generator value.
\r
143 We use low-speed (BRGH=0), the formula is
\r
144 SPBRG = ( ( FOSC / Desired Baud Rate ) / 64 ) - 1 */
\r
145 usSPBRG = ( ( APROCFREQ / ulWantedBaud ) / 64 ) - 1;
\r
146 if( usSPBRG > 255 )
\r
155 /* Set the serial interrupts to use the same priority as the tick. */
\r
156 bTXIP = serLOW_PRIORITY;
\r
157 bRCIP = serLOW_PRIORITY;
\r
159 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
160 we have data to send. */
\r
161 bRCIE = serINTERRUPT_ENABLED;
\r
163 portEXIT_CRITICAL();
\r
165 /* Unlike other ports, this serial code does not allow for more than one
\r
166 com port. We therefore don't return a pointer to a port structure and
\r
167 can instead just return NULL. */
\r
170 /*-----------------------------------------------------------*/
\r
172 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned char ucBufferLength )
\r
174 /* This is not implemented in this port.
\r
175 Use xSerialPortInitMinimal() instead. */
\r
178 /*-----------------------------------------------------------*/
\r
180 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, portTickType 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
186 return ( char ) pdTRUE;
\r
189 return ( char ) pdFALSE;
\r
191 /*-----------------------------------------------------------*/
\r
193 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, portTickType xBlockTime )
\r
195 /* Return false if after the block time there is no room on the Tx queue. */
\r
196 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != ( char ) pdPASS )
\r
201 /* Turn interrupt on - ensure the compiler only generates a single
\r
202 instruction for this. */
\r
203 bTXIE = serINTERRUPT_ENABLED;
\r
207 /*-----------------------------------------------------------*/
\r
209 void vSerialClose( xComPortHandle xPort )
\r
211 /* Not implemented for this port.
\r
212 To implement, turn off the interrupts and delete the memory
\r
213 allocated to the queues. */
\r