2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
54 + ISRcode removed. Is now pulled inline to reduce stack-usage.
\r
59 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
61 /* Scheduler header files. */
\r
62 #include "FreeRTOS.h"
\r
68 /* Hardware pin definitions. */
\r
69 #define serTX_PIN bTRC6
\r
70 #define serRX_PIN bTRC7
\r
72 /* Bit/register definitions. */
\r
73 #define serINPUT ( 1 )
\r
74 #define serOUTPUT ( 0 )
\r
75 #define serINTERRUPT_ENABLED ( 1 )
\r
77 /* All ISR's use the PIC18 low priority interrupt. */
\r
78 #define serLOW_PRIORITY ( 0 )
\r
80 /*-----------------------------------------------------------*/
\r
82 /* Queues to interface between comms API and interrupt routines. */
\r
83 xQueueHandle xRxedChars;
\r
84 xQueueHandle xCharsForTx;
\r
85 portBASE_TYPE xHigherPriorityTaskWoken;
\r
87 /*-----------------------------------------------------------*/
\r
89 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portCHAR ucQueueLength )
\r
91 unsigned portSHORT usSPBRG;
\r
93 /* Create the queues used by the ISR's to interface to tasks. */
\r
94 xRxedChars = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
95 xCharsForTx = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
97 portENTER_CRITICAL();
\r
99 /* Setup the IO pins to enable the USART IO. */
\r
100 serTX_PIN = serINPUT; // YES really! See datasheet
\r
101 serRX_PIN = serINPUT;
\r
103 /* Set the TX config register. */
\r
104 TXSTA = 0b00100000;
\r
105 // ||||||||--bit0: TX9D = n/a
\r
106 // |||||||---bit1: TRMT = ReadOnly
\r
107 // ||||||----bit2: BRGH = High speed
\r
108 // |||||-----bit3: SENDB = n/a
\r
109 // ||||------bit4: SYNC = Asynchronous mode
\r
110 // |||-------bit5: TXEN = Transmit enable
\r
111 // ||--------bit6: TX9 = 8-bit transmission
\r
112 // |---------bit7: CSRC = n/a
\r
114 /* Set the Receive config register. */
\r
115 RCSTA = 0b10010000;
\r
116 // ||||||||--bit0: RX9D = ReadOnly
\r
117 // |||||||---bit1: OERR = ReadOnly
\r
118 // ||||||----bit2: FERR = ReadOnly
\r
119 // |||||-----bit3: ADDEN = n/a
\r
120 // ||||------bit4: CREN = Enable receiver
\r
121 // |||-------bit5: SREN = n/a
\r
122 // ||--------bit6: RX9 = 8-bit reception
\r
123 // |---------bit7: SPEN = Serial port enabled
\r
125 /* Calculate the baud rate generator value.
\r
126 We use low-speed (BRGH=0), the formula is
\r
127 SPBRG = ( ( FOSC / Desired Baud Rate ) / 64 ) - 1 */
\r
128 usSPBRG = ( ( APROCFREQ / ulWantedBaud ) / 64 ) - 1;
\r
129 if( usSPBRG > 255 )
\r
138 /* Set the serial interrupts to use the same priority as the tick. */
\r
139 bTXIP = serLOW_PRIORITY;
\r
140 bRCIP = serLOW_PRIORITY;
\r
142 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when
\r
143 we have data to send. */
\r
144 bRCIE = serINTERRUPT_ENABLED;
\r
146 portEXIT_CRITICAL();
\r
148 /* Unlike other ports, this serial code does not allow for more than one
\r
149 com port. We therefore don't return a pointer to a port structure and
\r
150 can instead just return NULL. */
\r
153 /*-----------------------------------------------------------*/
\r
155 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portCHAR ucBufferLength )
\r
157 /* This is not implemented in this port.
\r
158 Use xSerialPortInitMinimal() instead. */
\r
161 /*-----------------------------------------------------------*/
\r
163 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime )
\r
165 /* Get the next character from the buffer. Return false if no characters
\r
166 are available, or arrive before xBlockTime expires. */
\r
167 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
169 return ( portCHAR ) pdTRUE;
\r
172 return ( portCHAR ) pdFALSE;
\r
174 /*-----------------------------------------------------------*/
\r
176 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime )
\r
178 /* Return false if after the block time there is no room on the Tx queue. */
\r
179 if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != ( portCHAR ) pdPASS )
\r
184 /* Turn interrupt on - ensure the compiler only generates a single
\r
185 instruction for this. */
\r
186 bTXIE = serINTERRUPT_ENABLED;
\r
190 /*-----------------------------------------------------------*/
\r
192 void vSerialClose( xComPortHandle xPort )
\r
194 /* Not implemented for this port.
\r
195 To implement, turn off the interrupts and delete the memory
\r
196 allocated to the queues. */
\r