2 FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! Why not get us to quote to get FreeRTOS.org *
\r
30 * running on your hardware - or even write all or part of your application*
\r
31 * for you? See http://www.OpenRTOS.com for details. *
\r
33 ***************************************************************************
\r
34 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
51 + ISRcode removed. Is now pulled inline to reduce stack-usage.
\r
56 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
58 /* Scheduler header files. */
\r
59 #include "FreeRTOS.h"
\r
65 /* Hardware pin definitions. */
\r
66 #define serTX_PIN bTRC6
\r
67 #define serRX_PIN bTRC7
\r
69 /* Bit/register definitions. */
\r
70 #define serINPUT ( 1 )
\r
71 #define serOUTPUT ( 0 )
\r
72 #define serINTERRUPT_ENABLED ( 1 )
\r
74 /* All ISR's use the PIC18 low priority interrupt. */
\r
75 #define serLOW_PRIORITY ( 0 )
\r
77 /*-----------------------------------------------------------*/
\r
79 /* Queues to interface between comms API and interrupt routines. */
\r
80 xQueueHandle xRxedChars;
\r
81 xQueueHandle xCharsForTx;
\r
83 /*-----------------------------------------------------------*/
\r
85 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portCHAR ucQueueLength )
\r
87 unsigned portSHORT usSPBRG;
\r
89 /* Create the queues used by the ISR's to interface to tasks. */
\r
90 xRxedChars = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
91 xCharsForTx = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
93 portENTER_CRITICAL();
\r
95 /* Setup the IO pins to enable the USART IO. */
\r
96 serTX_PIN = serINPUT; // YES really! See datasheet
\r
97 serRX_PIN = serINPUT;
\r
99 /* Set the TX config register. */
\r
100 TXSTA = 0b00100000;
\r
101 // ||||||||--bit0: TX9D = n/a
\r
102 // |||||||---bit1: TRMT = ReadOnly
\r
103 // ||||||----bit2: BRGH = High speed
\r
104 // |||||-----bit3: SENDB = n/a
\r
105 // ||||------bit4: SYNC = Asynchronous mode
\r
106 // |||-------bit5: TXEN = Transmit enable
\r
107 // ||--------bit6: TX9 = 8-bit transmission
\r
108 // |---------bit7: CSRC = n/a
\r
110 /* Set the Receive config register. */
\r
111 RCSTA = 0b10010000;
\r
112 // ||||||||--bit0: RX9D = ReadOnly
\r
113 // |||||||---bit1: OERR = ReadOnly
\r
114 // ||||||----bit2: FERR = ReadOnly
\r
115 // |||||-----bit3: ADDEN = n/a
\r
116 // ||||------bit4: CREN = Enable receiver
\r
117 // |||-------bit5: SREN = n/a
\r
118 // ||--------bit6: RX9 = 8-bit reception
\r
119 // |---------bit7: SPEN = Serial port enabled
\r
121 /* Calculate the baud rate generator value.
\r
122 We use low-speed (BRGH=0), the formula is
\r
123 SPBRG = ( ( FOSC / Desired Baud Rate ) / 64 ) - 1 */
\r
124 usSPBRG = ( ( APROCFREQ / ulWantedBaud ) / 64 ) - 1;
\r
125 if( usSPBRG > 255 )
\r
134 /* Set the serial interrupts to use the same priority as the tick. */
\r
135 bTXIP = serLOW_PRIORITY;
\r
136 bRCIP = serLOW_PRIORITY;
\r
138 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
139 we have data to send. */
\r
140 bRCIE = serINTERRUPT_ENABLED;
\r
142 portEXIT_CRITICAL();
\r
144 /* Unlike other ports, this serial code does not allow for more than one
\r
145 com port. We therefore don't return a pointer to a port structure and
\r
146 can instead just return NULL. */
\r
149 /*-----------------------------------------------------------*/
\r
151 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portCHAR ucBufferLength )
\r
153 /* This is not implemented in this port.
\r
154 Use xSerialPortInitMinimal() instead. */
\r
157 /*-----------------------------------------------------------*/
\r
159 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime )
\r
161 /* Get the next character from the buffer. Return false if no characters
\r
162 are available, or arrive before xBlockTime expires. */
\r
163 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
165 return ( portCHAR ) pdTRUE;
\r
168 return ( portCHAR ) pdFALSE;
\r
170 /*-----------------------------------------------------------*/
\r
172 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime )
\r
174 /* Return false if after the block time there is no room on the Tx queue. */
\r
175 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != ( portCHAR ) pdPASS )
\r
180 /* Turn interrupt on - ensure the compiler only generates a single
\r
181 instruction for this. */
\r
182 bTXIE = serINTERRUPT_ENABLED;
\r
186 /*-----------------------------------------------------------*/
\r
188 void vSerialClose( xComPortHandle xPort )
\r
190 /* Not implemented for this port.
\r
191 To implement, turn off the interrupts and delete the memory
\r
192 allocated to the queues. */
\r