1 |
583 |
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 |
|
|
/* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR DEMO PURPOSES */
|
56 |
|
|
#include <stdlib.h>
|
57 |
|
|
#include "FreeRTOS.h"
|
58 |
|
|
#include "queue.h"
|
59 |
|
|
#include "task.h"
|
60 |
|
|
#include "serial.h"
|
61 |
|
|
|
62 |
|
|
/* Constants required to setup the serial control register. */
|
63 |
|
|
#define ser8_BIT_MODE ( ( unsigned char ) 0x40 )
|
64 |
|
|
#define serRX_ENABLE ( ( unsigned char ) 0x10 )
|
65 |
|
|
|
66 |
|
|
/* Constants to setup the timer used to generate the baud rate. */
|
67 |
|
|
#define serCLOCK_DIV_48 ( ( unsigned char ) 0x03 )
|
68 |
|
|
#define serUSE_PRESCALED_CLOCK ( ( unsigned char ) 0x10 )
|
69 |
|
|
#define ser8BIT_WITH_RELOAD ( ( unsigned char ) 0x20 )
|
70 |
|
|
#define serSMOD ( ( unsigned char ) 0x10 )
|
71 |
|
|
|
72 |
|
|
static xQueueHandle xRxedChars;
|
73 |
|
|
static xQueueHandle xCharsForTx;
|
74 |
|
|
|
75 |
|
|
data static unsigned portBASE_TYPE uxTxEmpty;
|
76 |
|
|
|
77 |
|
|
/*-----------------------------------------------------------*/
|
78 |
|
|
|
79 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
80 |
|
|
{
|
81 |
|
|
unsigned long ulReloadValue;
|
82 |
|
|
const portFLOAT fBaudConst = ( portFLOAT ) configCPU_CLOCK_HZ * ( portFLOAT ) 2.0;
|
83 |
|
|
unsigned char ucOriginalSFRPage;
|
84 |
|
|
|
85 |
|
|
portENTER_CRITICAL();
|
86 |
|
|
{
|
87 |
|
|
ucOriginalSFRPage = SFRPAGE;
|
88 |
|
|
SFRPAGE = 0;
|
89 |
|
|
|
90 |
|
|
uxTxEmpty = pdTRUE;
|
91 |
|
|
|
92 |
|
|
/* Create the queues used by the com test task. */
|
93 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
|
94 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
|
95 |
|
|
|
96 |
|
|
/* Calculate the baud rate to use timer 1. */
|
97 |
|
|
ulReloadValue = ( unsigned long ) ( ( ( portFLOAT ) 256 - ( fBaudConst / ( portFLOAT ) ( 32 * ulWantedBaud ) ) ) + ( portFLOAT ) 0.5 );
|
98 |
|
|
|
99 |
|
|
/* Set timer one for desired mode of operation. */
|
100 |
|
|
TMOD &= 0x08;
|
101 |
|
|
TMOD |= ser8BIT_WITH_RELOAD;
|
102 |
|
|
SSTA0 |= serSMOD;
|
103 |
|
|
|
104 |
|
|
/* Set the reload and start values for the time. */
|
105 |
|
|
TL1 = ( unsigned char ) ulReloadValue;
|
106 |
|
|
TH1 = ( unsigned char ) ulReloadValue;
|
107 |
|
|
|
108 |
|
|
/* Setup the control register for standard n, 8, 1 - variable baud rate. */
|
109 |
|
|
SCON = ser8_BIT_MODE | serRX_ENABLE;
|
110 |
|
|
|
111 |
|
|
/* Enable the serial port interrupts */
|
112 |
|
|
ES = 1;
|
113 |
|
|
|
114 |
|
|
/* Start the timer. */
|
115 |
|
|
TR1 = 1;
|
116 |
|
|
|
117 |
|
|
SFRPAGE = ucOriginalSFRPage;
|
118 |
|
|
}
|
119 |
|
|
portEXIT_CRITICAL();
|
120 |
|
|
|
121 |
|
|
/* Unlike some ports, this serial code does not allow for more than one
|
122 |
|
|
com port. We therefore don't return a pointer to a port structure and can
|
123 |
|
|
instead just return NULL. */
|
124 |
|
|
return NULL;
|
125 |
|
|
}
|
126 |
|
|
/*-----------------------------------------------------------*/
|
127 |
|
|
|
128 |
|
|
void vSerialISR( void ) interrupt 4
|
129 |
|
|
{
|
130 |
|
|
char cChar;
|
131 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
132 |
|
|
|
133 |
|
|
/* 8051 port interrupt routines MUST be placed within a critical section
|
134 |
|
|
if taskYIELD() is used within the ISR! */
|
135 |
|
|
|
136 |
|
|
portENTER_CRITICAL();
|
137 |
|
|
{
|
138 |
|
|
if( RI )
|
139 |
|
|
{
|
140 |
|
|
/* Get the character and post it on the queue of Rxed characters.
|
141 |
|
|
If the post causes a task to wake force a context switch if the woken task
|
142 |
|
|
has a higher priority than the task we have interrupted. */
|
143 |
|
|
cChar = SBUF;
|
144 |
|
|
RI = 0;
|
145 |
|
|
|
146 |
|
|
xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
if( TI )
|
150 |
|
|
{
|
151 |
|
|
if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == ( portBASE_TYPE ) pdTRUE )
|
152 |
|
|
{
|
153 |
|
|
/* Send the next character queued for Tx. */
|
154 |
|
|
SBUF = cChar;
|
155 |
|
|
}
|
156 |
|
|
else
|
157 |
|
|
{
|
158 |
|
|
/* Queue empty, nothing to send. */
|
159 |
|
|
uxTxEmpty = pdTRUE;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
TI = 0;
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
if( xHigherPriorityTaskWoken )
|
166 |
|
|
{
|
167 |
|
|
portYIELD();
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
portEXIT_CRITICAL();
|
171 |
|
|
}
|
172 |
|
|
/*-----------------------------------------------------------*/
|
173 |
|
|
|
174 |
|
|
portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
|
175 |
|
|
{
|
176 |
|
|
/* There is only one port supported. */
|
177 |
|
|
( void ) pxPort;
|
178 |
|
|
|
179 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
180 |
|
|
are available, or arrive before xBlockTime expires. */
|
181 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
182 |
|
|
{
|
183 |
|
|
return ( portBASE_TYPE ) pdTRUE;
|
184 |
|
|
}
|
185 |
|
|
else
|
186 |
|
|
{
|
187 |
|
|
return ( portBASE_TYPE ) pdFALSE;
|
188 |
|
|
}
|
189 |
|
|
}
|
190 |
|
|
/*-----------------------------------------------------------*/
|
191 |
|
|
|
192 |
|
|
portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
|
193 |
|
|
{
|
194 |
|
|
portBASE_TYPE xReturn;
|
195 |
|
|
|
196 |
|
|
/* There is only one port supported. */
|
197 |
|
|
( void ) pxPort;
|
198 |
|
|
|
199 |
|
|
portENTER_CRITICAL();
|
200 |
|
|
{
|
201 |
|
|
if( uxTxEmpty == pdTRUE )
|
202 |
|
|
{
|
203 |
|
|
SBUF = cOutChar;
|
204 |
|
|
uxTxEmpty = pdFALSE;
|
205 |
|
|
xReturn = ( portBASE_TYPE ) pdTRUE;
|
206 |
|
|
}
|
207 |
|
|
else
|
208 |
|
|
{
|
209 |
|
|
xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
|
210 |
|
|
|
211 |
|
|
if( xReturn == ( portBASE_TYPE ) pdFALSE )
|
212 |
|
|
{
|
213 |
|
|
xReturn = ( portBASE_TYPE ) pdTRUE;
|
214 |
|
|
}
|
215 |
|
|
}
|
216 |
|
|
}
|
217 |
|
|
portEXIT_CRITICAL();
|
218 |
|
|
|
219 |
|
|
return xReturn;
|
220 |
|
|
}
|
221 |
|
|
/*-----------------------------------------------------------*/
|
222 |
|
|
|
223 |
|
|
void vSerialClose( xComPortHandle xPort )
|
224 |
|
|
{
|
225 |
|
|
/* Not implemented in this port. */
|
226 |
|
|
( void ) xPort;
|
227 |
|
|
}
|
228 |
|
|
/*-----------------------------------------------------------*/
|
229 |
|
|
|
230 |
|
|
|
231 |
|
|
|
232 |
|
|
|
233 |
|
|
|