2 FreeRTOS V7.0.0 - Copyright (C) 2011 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
56 + ISRcode removed. Is now pulled inline to reduce stack-usage.
\r
61 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
63 /* Scheduler header files. */
\r
64 #include "FreeRTOS.h"
\r
70 /* Hardware pin definitions. */
\r
71 #define serTX_PIN bTRC6
\r
72 #define serRX_PIN bTRC7
\r
74 /* Bit/register definitions. */
\r
75 #define serINPUT ( 1 )
\r
76 #define serOUTPUT ( 0 )
\r
77 #define serINTERRUPT_ENABLED ( 1 )
\r
79 /* All ISR's use the PIC18 low priority interrupt. */
\r
80 #define serLOW_PRIORITY ( 0 )
\r
82 /*-----------------------------------------------------------*/
\r
84 /* Queues to interface between comms API and interrupt routines. */
\r
85 xQueueHandle xRxedChars;
\r
86 xQueueHandle xCharsForTx;
\r
87 portBASE_TYPE xHigherPriorityTaskWoken;
\r
89 /*-----------------------------------------------------------*/
\r
91 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned char ucQueueLength )
\r
93 unsigned short usSPBRG;
\r
95 /* Create the queues used by the ISR's to interface to tasks. */
\r
96 xRxedChars = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
97 xCharsForTx = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
99 portENTER_CRITICAL();
\r
101 /* Setup the IO pins to enable the USART IO. */
\r
102 serTX_PIN = serINPUT; // YES really! See datasheet
\r
103 serRX_PIN = serINPUT;
\r
105 /* Set the TX config register. */
\r
106 TXSTA = 0b00100000;
\r
107 // ||||||||--bit0: TX9D = n/a
\r
108 // |||||||---bit1: TRMT = ReadOnly
\r
109 // ||||||----bit2: BRGH = High speed
\r
110 // |||||-----bit3: SENDB = n/a
\r
111 // ||||------bit4: SYNC = Asynchronous mode
\r
112 // |||-------bit5: TXEN = Transmit enable
\r
113 // ||--------bit6: TX9 = 8-bit transmission
\r
114 // |---------bit7: CSRC = n/a
\r
116 /* Set the Receive config register. */
\r
117 RCSTA = 0b10010000;
\r
118 // ||||||||--bit0: RX9D = ReadOnly
\r
119 // |||||||---bit1: OERR = ReadOnly
\r
120 // ||||||----bit2: FERR = ReadOnly
\r
121 // |||||-----bit3: ADDEN = n/a
\r
122 // ||||------bit4: CREN = Enable receiver
\r
123 // |||-------bit5: SREN = n/a
\r
124 // ||--------bit6: RX9 = 8-bit reception
\r
125 // |---------bit7: SPEN = Serial port enabled
\r
127 /* Calculate the baud rate generator value.
\r
128 We use low-speed (BRGH=0), the formula is
\r
129 SPBRG = ( ( FOSC / Desired Baud Rate ) / 64 ) - 1 */
\r
130 usSPBRG = ( ( APROCFREQ / ulWantedBaud ) / 64 ) - 1;
\r
131 if( usSPBRG > 255 )
\r
140 /* Set the serial interrupts to use the same priority as the tick. */
\r
141 bTXIP = serLOW_PRIORITY;
\r
142 bRCIP = serLOW_PRIORITY;
\r
144 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
145 we have data to send. */
\r
146 bRCIE = serINTERRUPT_ENABLED;
\r
148 portEXIT_CRITICAL();
\r
150 /* Unlike other ports, this serial code does not allow for more than one
\r
151 com port. We therefore don't return a pointer to a port structure and
\r
152 can instead just return NULL. */
\r
155 /*-----------------------------------------------------------*/
\r
157 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned char ucBufferLength )
\r
159 /* This is not implemented in this port.
\r
160 Use xSerialPortInitMinimal() instead. */
\r
163 /*-----------------------------------------------------------*/
\r
165 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, portTickType xBlockTime )
\r
167 /* Get the next character from the buffer. Return false if no characters
\r
168 are available, or arrive before xBlockTime expires. */
\r
169 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
171 return ( char ) pdTRUE;
\r
174 return ( char ) pdFALSE;
\r
176 /*-----------------------------------------------------------*/
\r
178 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, portTickType xBlockTime )
\r
180 /* Return false if after the block time there is no room on the Tx queue. */
\r
181 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != ( char ) pdPASS )
\r
186 /* Turn interrupt on - ensure the compiler only generates a single
\r
187 instruction for this. */
\r
188 bTXIE = serINTERRUPT_ENABLED;
\r
192 /*-----------------------------------------------------------*/
\r
194 void vSerialClose( xComPortHandle xPort )
\r
196 /* Not implemented for this port.
\r
197 To implement, turn off the interrupts and delete the memory
\r
198 allocated to the queues. */
\r