2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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