151 |
kaklik |
1 |
/*
|
|
|
2 |
Copyright (C) 2004 John Orlando
|
|
|
3 |
|
|
|
4 |
AVRcam: a small real-time image processing engine.
|
|
|
5 |
|
|
|
6 |
This program is free software; you can redistribute it and/or
|
|
|
7 |
modify it under the terms of the GNU General Public
|
|
|
8 |
License as published by the Free Software Foundation; either
|
|
|
9 |
version 2 of the License, or (at your option) any later version.
|
|
|
10 |
|
|
|
11 |
This program is distributed in the hope that it will be useful,
|
|
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
14 |
General Public License for more details.
|
|
|
15 |
|
|
|
16 |
You should have received a copy of the GNU General Public
|
|
|
17 |
License along with this program; if not, write to the Free Software
|
|
|
18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
19 |
|
|
|
20 |
For more information on the AVRcam, please contact:
|
|
|
21 |
|
|
|
22 |
john@jrobot.net
|
|
|
23 |
|
|
|
24 |
or go to www.jrobot.net for more details regarding the system.
|
|
|
25 |
*/
|
|
|
26 |
/***********************************************************
|
|
|
27 |
Module Name: UartInterface.c
|
|
|
28 |
Module Date: 04/10/2004
|
|
|
29 |
Module Auth: John Orlando
|
|
|
30 |
|
|
|
31 |
Description: This module is responsible for providing an
|
|
|
32 |
interface to the UART hardware available on the mega8.
|
|
|
33 |
This interface is an interrupt-driven interface.
|
|
|
34 |
|
|
|
35 |
Revision History:
|
|
|
36 |
Date Rel Ver. Notes
|
|
|
37 |
4/10/2004 0.1 Module created
|
|
|
38 |
6/30/2004 1.0 Initial release for Circuit Cellar
|
|
|
39 |
contest.
|
|
|
40 |
11/15/2004 1.2 Updated UART baud rate regs so that
|
|
|
41 |
it runs at 115.2 kbps when the input
|
|
|
42 |
crystal is at 17.7 MHz (which is the
|
|
|
43 |
speed of the OV6620's crystal).
|
|
|
44 |
1/16/2005 1.4 Moved the serial received ISR to
|
|
|
45 |
this file, instead of having it
|
|
|
46 |
in its own UartInterfaceAsm.S file
|
|
|
47 |
written in assembly.
|
|
|
48 |
***********************************************************/
|
|
|
49 |
|
|
|
50 |
/* Includes */
|
|
|
51 |
#include <avr/io.h>
|
|
|
52 |
#include <avr/interrupt.h>
|
|
|
53 |
#include <avr/signal.h>
|
|
|
54 |
#include "CommonDefs.h"
|
|
|
55 |
#include "UartInterface.h"
|
|
|
56 |
#include "UIMgr.h"
|
|
|
57 |
#include "Executive.h"
|
|
|
58 |
|
|
|
59 |
/* Local Variables */
|
|
|
60 |
|
|
|
61 |
/* Local Structures and Typedefs */
|
|
|
62 |
|
|
|
63 |
/* Extern Variables */
|
|
|
64 |
|
|
|
65 |
/* Definitions */
|
|
|
66 |
|
|
|
67 |
/***********************************************************
|
|
|
68 |
Function Name: UartInt_init
|
|
|
69 |
Function Description: This function is responsible for
|
|
|
70 |
initializing the UART interface on the mega8. This
|
|
|
71 |
interface is set to communicate at 115.2 Kbps, with an
|
|
|
72 |
8N1 protocol.
|
|
|
73 |
Inputs: none
|
|
|
74 |
Outputs: none
|
|
|
75 |
***********************************************************/
|
|
|
76 |
void UartInt_init(void)
|
|
|
77 |
{
|
|
|
78 |
/* set up the baud rate registers so the UART will operate
|
|
|
79 |
at 115.2 Kbps */
|
|
|
80 |
UBRRH = 0x00;
|
|
|
81 |
|
|
|
82 |
#ifdef NO_CRYSTAL
|
|
|
83 |
UBRRL = 18; /* 18 for double clocking at 115.2 kbps */
|
|
|
84 |
#else
|
|
|
85 |
UBRRL = 0x08; /* for 16 MHz crystal at 115.2 kbps */
|
|
|
86 |
#endif
|
|
|
87 |
|
|
|
88 |
/* enable the tx and rx capabilities of the UART...as well
|
|
|
89 |
as the receive complete interrupt */
|
|
|
90 |
UCSRB = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
|
|
|
91 |
|
|
|
92 |
/* set up the control registers so the UART works at 8N1 */
|
|
|
93 |
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
|
|
|
94 |
|
|
|
95 |
#ifdef NO_CRYSTAL
|
|
|
96 |
/* set the baud rate to use the double-speed */
|
|
|
97 |
UCSRA = (1<<U2X);
|
|
|
98 |
#endif
|
|
|
99 |
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/***********************************************************
|
|
|
103 |
Function Name: UartInt_txByte
|
|
|
104 |
Function Description: This function is responsible for
|
|
|
105 |
transmitting a single byte on the uart.
|
|
|
106 |
Inputs: txByte - the byte to send
|
|
|
107 |
Outputs: none
|
|
|
108 |
NOTES: When the TX UDRE (data register empty) is set, there
|
|
|
109 |
is puposefully no interrupt...thus, to send a string of
|
|
|
110 |
data out, the calling routine needs to hold up the entire
|
|
|
111 |
application while this takes place (or just send one
|
|
|
112 |
byte at a time at strtegically timed intervals, like
|
|
|
113 |
the stats data is sent out :-)
|
|
|
114 |
***********************************************************/
|
|
|
115 |
void UartInt_txByte(unsigned char txByte)
|
|
|
116 |
{
|
|
|
117 |
/* Wait for empty transmit buffer */
|
|
|
118 |
while ( !( UCSRA & (1<<UDRE)) );
|
|
|
119 |
/* Put data into buffer, sends the data */
|
|
|
120 |
UDR = txByte;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/***********************************************************
|
|
|
124 |
Function Name: SIG_UART_RECV ISR
|
|
|
125 |
Function Description: This function is responsible for
|
|
|
126 |
handling the interrupt caused when a data byte is
|
|
|
127 |
received by the UART.
|
|
|
128 |
Inputs: none
|
|
|
129 |
Outputs: none
|
|
|
130 |
NOTES: This function was originally written in assembly,
|
|
|
131 |
but moved over to C when the setting of the "T" bit at
|
|
|
132 |
the end of the routine was no longer necessary (this
|
|
|
133 |
theoretically allowed the AVRcam to respond to serial
|
|
|
134 |
bytes in the middle of tracking or dumping a frame.
|
|
|
135 |
But it wasn't really needed, and understanding the C
|
|
|
136 |
is easier :-)
|
|
|
137 |
***********************************************************/
|
|
|
138 |
SIGNAL(SIG_UART_RECV)
|
|
|
139 |
{
|
|
|
140 |
unsigned char tmpHead;
|
|
|
141 |
/* read the data byte, put it in the serial queue, and
|
|
|
142 |
post the event */
|
|
|
143 |
|
|
|
144 |
UIMgr_rxFifo[UIMgr_rxFifoHead] = UDR;
|
|
|
145 |
|
|
|
146 |
/* now move the head up */
|
|
|
147 |
tmpHead = (UIMgr_rxFifoHead + 1) & (UI_MGR_RX_FIFO_MASK);
|
|
|
148 |
UIMgr_rxFifoHead = tmpHead;
|
|
|
149 |
|
|
|
150 |
/* write the serial received event to the event fifo */
|
|
|
151 |
Exec_eventFifo[Exec_eventFifoHead] = EV_SERIAL_DATA_RECEIVED;
|
|
|
152 |
|
|
|
153 |
/* now move the head up */
|
|
|
154 |
tmpHead = (Exec_eventFifoHead + 1) & (EXEC_EVENT_FIFO_MASK);
|
|
|
155 |
Exec_eventFifoHead = tmpHead;
|
|
|
156 |
}
|
|
|
157 |
|