1 |
587 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
/*
|
55 |
|
|
Changes from V3.0.0
|
56 |
|
|
+ ISRcode removed. Is now pulled inline to reduce stack-usage.
|
57 |
|
|
|
58 |
|
|
Changes from V3.0.1
|
59 |
|
|
*/
|
60 |
|
|
|
61 |
|
|
/* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
|
62 |
|
|
|
63 |
|
|
/* Scheduler header files. */
|
64 |
|
|
#include "FreeRTOS.h"
|
65 |
|
|
#include "task.h"
|
66 |
|
|
#include "queue.h"
|
67 |
|
|
|
68 |
|
|
#include "serial.h"
|
69 |
|
|
|
70 |
|
|
/* Hardware pin definitions. */
|
71 |
|
|
#define serTX_PIN bTRC6
|
72 |
|
|
#define serRX_PIN bTRC7
|
73 |
|
|
|
74 |
|
|
/* Bit/register definitions. */
|
75 |
|
|
#define serINPUT ( 1 )
|
76 |
|
|
#define serOUTPUT ( 0 )
|
77 |
|
|
#define serINTERRUPT_ENABLED ( 1 )
|
78 |
|
|
|
79 |
|
|
/* All ISR's use the PIC18 low priority interrupt. */
|
80 |
|
|
#define serLOW_PRIORITY ( 0 )
|
81 |
|
|
|
82 |
|
|
/*-----------------------------------------------------------*/
|
83 |
|
|
|
84 |
|
|
/* Queues to interface between comms API and interrupt routines. */
|
85 |
|
|
xQueueHandle xRxedChars;
|
86 |
|
|
xQueueHandle xCharsForTx;
|
87 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken;
|
88 |
|
|
|
89 |
|
|
/*-----------------------------------------------------------*/
|
90 |
|
|
|
91 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned char ucQueueLength )
|
92 |
|
|
{
|
93 |
|
|
unsigned short usSPBRG;
|
94 |
|
|
|
95 |
|
|
/* Create the queues used by the ISR's to interface to tasks. */
|
96 |
|
|
xRxedChars = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
|
97 |
|
|
xCharsForTx = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
|
98 |
|
|
|
99 |
|
|
portENTER_CRITICAL();
|
100 |
|
|
|
101 |
|
|
/* Setup the IO pins to enable the USART IO. */
|
102 |
|
|
serTX_PIN = serINPUT; // YES really! See datasheet
|
103 |
|
|
serRX_PIN = serINPUT;
|
104 |
|
|
|
105 |
|
|
/* Set the TX config register. */
|
106 |
|
|
TXSTA = 0b00100000;
|
107 |
|
|
// ||||||||--bit0: TX9D = n/a
|
108 |
|
|
// |||||||---bit1: TRMT = ReadOnly
|
109 |
|
|
// ||||||----bit2: BRGH = High speed
|
110 |
|
|
// |||||-----bit3: SENDB = n/a
|
111 |
|
|
// ||||------bit4: SYNC = Asynchronous mode
|
112 |
|
|
// |||-------bit5: TXEN = Transmit enable
|
113 |
|
|
// ||--------bit6: TX9 = 8-bit transmission
|
114 |
|
|
// |---------bit7: CSRC = n/a
|
115 |
|
|
|
116 |
|
|
/* Set the Receive config register. */
|
117 |
|
|
RCSTA = 0b10010000;
|
118 |
|
|
// ||||||||--bit0: RX9D = ReadOnly
|
119 |
|
|
// |||||||---bit1: OERR = ReadOnly
|
120 |
|
|
// ||||||----bit2: FERR = ReadOnly
|
121 |
|
|
// |||||-----bit3: ADDEN = n/a
|
122 |
|
|
// ||||------bit4: CREN = Enable receiver
|
123 |
|
|
// |||-------bit5: SREN = n/a
|
124 |
|
|
// ||--------bit6: RX9 = 8-bit reception
|
125 |
|
|
// |---------bit7: SPEN = Serial port enabled
|
126 |
|
|
|
127 |
|
|
/* Calculate the baud rate generator value.
|
128 |
|
|
We use low-speed (BRGH=0), the formula is
|
129 |
|
|
SPBRG = ( ( FOSC / Desired Baud Rate ) / 64 ) - 1 */
|
130 |
|
|
usSPBRG = ( ( APROCFREQ / ulWantedBaud ) / 64 ) - 1;
|
131 |
|
|
if( usSPBRG > 255 )
|
132 |
|
|
{
|
133 |
|
|
SPBRG = 255;
|
134 |
|
|
}
|
135 |
|
|
else
|
136 |
|
|
{
|
137 |
|
|
SPBRG = usSPBRG;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
/* Set the serial interrupts to use the same priority as the tick. */
|
141 |
|
|
bTXIP = serLOW_PRIORITY;
|
142 |
|
|
bRCIP = serLOW_PRIORITY;
|
143 |
|
|
|
144 |
|
|
/* Enable the Rx interrupt now, the Tx interrupt will get enabled when
|
145 |
|
|
we have data to send. */
|
146 |
|
|
bRCIE = serINTERRUPT_ENABLED;
|
147 |
|
|
|
148 |
|
|
portEXIT_CRITICAL();
|
149 |
|
|
|
150 |
|
|
/* Unlike other ports, this serial code does not allow for more than one
|
151 |
|
|
com port. We therefore don't return a pointer to a port structure and
|
152 |
|
|
can instead just return NULL. */
|
153 |
|
|
return NULL;
|
154 |
|
|
}
|
155 |
|
|
/*-----------------------------------------------------------*/
|
156 |
|
|
|
157 |
|
|
xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned char ucBufferLength )
|
158 |
|
|
{
|
159 |
|
|
/* This is not implemented in this port.
|
160 |
|
|
Use xSerialPortInitMinimal() instead. */
|
161 |
|
|
return NULL;
|
162 |
|
|
}
|
163 |
|
|
/*-----------------------------------------------------------*/
|
164 |
|
|
|
165 |
|
|
portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, portTickType xBlockTime )
|
166 |
|
|
{
|
167 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
168 |
|
|
are available, or arrive before xBlockTime expires. */
|
169 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
170 |
|
|
{
|
171 |
|
|
return ( char ) pdTRUE;
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
return ( char ) pdFALSE;
|
175 |
|
|
}
|
176 |
|
|
/*-----------------------------------------------------------*/
|
177 |
|
|
|
178 |
|
|
portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, portTickType xBlockTime )
|
179 |
|
|
{
|
180 |
|
|
/* Return false if after the block time there is no room on the Tx queue. */
|
181 |
|
|
if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != ( char ) pdPASS )
|
182 |
|
|
{
|
183 |
|
|
return pdFAIL;
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
/* Turn interrupt on - ensure the compiler only generates a single
|
187 |
|
|
instruction for this. */
|
188 |
|
|
bTXIE = serINTERRUPT_ENABLED;
|
189 |
|
|
|
190 |
|
|
return pdPASS;
|
191 |
|
|
}
|
192 |
|
|
/*-----------------------------------------------------------*/
|
193 |
|
|
|
194 |
|
|
void vSerialClose( xComPortHandle xPort )
|
195 |
|
|
{
|
196 |
|
|
/* Not implemented for this port.
|
197 |
|
|
To implement, turn off the interrupts and delete the memory
|
198 |
|
|
allocated to the queues. */
|
199 |
|
|
}
|