1 |
589 |
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 IAR AVR PORT. */
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
#include <stdlib.h>
|
59 |
|
|
#include "FreeRTOS.h"
|
60 |
|
|
#include "queue.h"
|
61 |
|
|
#include "task.h"
|
62 |
|
|
#include "serial.h"
|
63 |
|
|
|
64 |
|
|
#define serBAUD_DIV_CONSTANT ( ( unsigned long ) 16 )
|
65 |
|
|
|
66 |
|
|
/* Constants for writing to UCSRB. */
|
67 |
|
|
#define serRX_INT_ENABLE ( ( unsigned char ) 0x80 )
|
68 |
|
|
#define serRX_ENABLE ( ( unsigned char ) 0x10 )
|
69 |
|
|
#define serTX_ENABLE ( ( unsigned char ) 0x08 )
|
70 |
|
|
#define serTX_INT_ENABLE ( ( unsigned char ) 0x20 )
|
71 |
|
|
|
72 |
|
|
/* Constants for writing to UCSRC. */
|
73 |
|
|
#define serUCSRC_SELECT ( ( unsigned char ) 0x80 )
|
74 |
|
|
#define serEIGHT_DATA_BITS ( ( unsigned char ) 0x06 )
|
75 |
|
|
|
76 |
|
|
static xQueueHandle xRxedChars;
|
77 |
|
|
static xQueueHandle xCharsForTx;
|
78 |
|
|
|
79 |
|
|
#define vInterruptOn() \
|
80 |
|
|
{ \
|
81 |
|
|
unsigned char ucByte; \
|
82 |
|
|
\
|
83 |
|
|
ucByte = UCSRB; \
|
84 |
|
|
ucByte |= serTX_INT_ENABLE; \
|
85 |
|
|
outb( UCSRB, ucByte ); \
|
86 |
|
|
}
|
87 |
|
|
/*-----------------------------------------------------------*/
|
88 |
|
|
|
89 |
|
|
#define vInterruptOff() \
|
90 |
|
|
{ \
|
91 |
|
|
unsigned char ucByte; \
|
92 |
|
|
\
|
93 |
|
|
ucByte = UCSRB; \
|
94 |
|
|
ucByte &= ~serTX_INT_ENABLE; \
|
95 |
|
|
outb( UCSRB, ucByte ); \
|
96 |
|
|
}
|
97 |
|
|
/*-----------------------------------------------------------*/
|
98 |
|
|
|
99 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
100 |
|
|
{
|
101 |
|
|
unsigned long ulBaudRateCounter;
|
102 |
|
|
unsigned char ucByte;
|
103 |
|
|
|
104 |
|
|
portENTER_CRITICAL();
|
105 |
|
|
{
|
106 |
|
|
/* Create the queues used by the com test task. */
|
107 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
|
108 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
|
109 |
|
|
|
110 |
|
|
/* Calculate the baud rate register value from the equation in the
|
111 |
|
|
data sheet. */
|
112 |
|
|
ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;
|
113 |
|
|
|
114 |
|
|
/* Set the baud rate. */
|
115 |
|
|
ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
|
116 |
|
|
outb( UBRRL, ucByte );
|
117 |
|
|
|
118 |
|
|
ulBaudRateCounter >>= ( unsigned long ) 8;
|
119 |
|
|
ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
|
120 |
|
|
outb( UBRRH, ucByte );
|
121 |
|
|
|
122 |
|
|
/* Enable the Rx interrupt. The Tx interrupt will get enabled
|
123 |
|
|
later. Also enable the Rx and Tx. */
|
124 |
|
|
outb( UCSRB, serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
|
125 |
|
|
|
126 |
|
|
/* Set the data bits to 8. */
|
127 |
|
|
outb( UCSRC, serUCSRC_SELECT | serEIGHT_DATA_BITS );
|
128 |
|
|
}
|
129 |
|
|
portEXIT_CRITICAL();
|
130 |
|
|
|
131 |
|
|
/* Unlike other ports, this serial code does not allow for more than one
|
132 |
|
|
com port. We therefore don't return a pointer to a port structure and can
|
133 |
|
|
instead just return NULL. */
|
134 |
|
|
return NULL;
|
135 |
|
|
}
|
136 |
|
|
/*-----------------------------------------------------------*/
|
137 |
|
|
|
138 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
|
139 |
|
|
{
|
140 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
141 |
|
|
are available, or arrive before xBlockTime expires. */
|
142 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
143 |
|
|
{
|
144 |
|
|
return pdTRUE;
|
145 |
|
|
}
|
146 |
|
|
else
|
147 |
|
|
{
|
148 |
|
|
return pdFALSE;
|
149 |
|
|
}
|
150 |
|
|
}
|
151 |
|
|
/*-----------------------------------------------------------*/
|
152 |
|
|
|
153 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
|
154 |
|
|
{
|
155 |
|
|
/* Return false if after the block time there is no room on the Tx queue. */
|
156 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
|
157 |
|
|
{
|
158 |
|
|
return pdFAIL;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
vInterruptOn();
|
162 |
|
|
|
163 |
|
|
return pdPASS;
|
164 |
|
|
}
|
165 |
|
|
/*-----------------------------------------------------------*/
|
166 |
|
|
|
167 |
|
|
void vSerialClose( xComPortHandle xPort )
|
168 |
|
|
{
|
169 |
|
|
unsigned char ucByte;
|
170 |
|
|
|
171 |
|
|
/* Turn off the interrupts. We may also want to delete the queues and/or
|
172 |
|
|
re-install the original ISR. */
|
173 |
|
|
|
174 |
|
|
portENTER_CRITICAL();
|
175 |
|
|
{
|
176 |
|
|
vInterruptOff();
|
177 |
|
|
ucByte = UCSRB;
|
178 |
|
|
ucByte &= ~serRX_INT_ENABLE;
|
179 |
|
|
outb( UCSRB, ucByte );
|
180 |
|
|
}
|
181 |
|
|
portEXIT_CRITICAL();
|
182 |
|
|
}
|
183 |
|
|
/*-----------------------------------------------------------*/
|
184 |
|
|
|
185 |
|
|
__interrupt void SIG_UART_RECV( void )
|
186 |
|
|
{
|
187 |
|
|
signed char ucChar, xHigherPriorityTaskWoken = pdFALSE;
|
188 |
|
|
|
189 |
|
|
/* Get the character and post it on the queue of Rxed characters.
|
190 |
|
|
If the post causes a task to wake force a context switch as the woken task
|
191 |
|
|
may have a higher priority than the task we have interrupted. */
|
192 |
|
|
ucChar = UDR;
|
193 |
|
|
|
194 |
|
|
xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );
|
195 |
|
|
|
196 |
|
|
if( xHigherPriorityTaskWoken != pdFALSE )
|
197 |
|
|
{
|
198 |
|
|
taskYIELD();
|
199 |
|
|
}
|
200 |
|
|
}
|
201 |
|
|
/*-----------------------------------------------------------*/
|
202 |
|
|
|
203 |
|
|
__interrupt void SIG_UART_DATA( void )
|
204 |
|
|
{
|
205 |
|
|
signed char cChar, cTaskWoken = pdFALSE;
|
206 |
|
|
|
207 |
|
|
if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
|
208 |
|
|
{
|
209 |
|
|
/* Send the next character queued for Tx. */
|
210 |
|
|
outb( UDR, cChar );
|
211 |
|
|
}
|
212 |
|
|
else
|
213 |
|
|
{
|
214 |
|
|
/* Queue empty, nothing to send. */
|
215 |
|
|
vInterruptOff();
|
216 |
|
|
}
|
217 |
|
|
}
|
218 |
|
|
|