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 "CommonDefs.h"
|
|
|
54 |
#include "UartInterface.h"
|
|
|
55 |
#include "UIMgr.h"
|
|
|
56 |
#include "Executive.h"
|
|
|
57 |
|
|
|
58 |
/* Local Variables */
|
|
|
59 |
|
|
|
60 |
/* Local Structures and Typedefs */
|
|
|
61 |
|
|
|
62 |
/* Extern Variables */
|
|
|
63 |
|
|
|
64 |
/* Definitions */
|
|
|
65 |
|
|
|
66 |
/***********************************************************
|
|
|
67 |
Function Name: UartInt_init
|
|
|
68 |
Function Description: This function is responsible for
|
|
|
69 |
initializing the UART interface on the mega8. This
|
|
|
70 |
interface is set to communicate at 115.2 Kbps, with an
|
|
|
71 |
8N1 protocol.
|
|
|
72 |
Inputs: none
|
|
|
73 |
Outputs: none
|
|
|
74 |
***********************************************************/
|
|
|
75 |
void UartInt_init(void)
|
|
|
76 |
{
|
|
|
77 |
/* set up the baud rate registers so the UART will operate
|
|
|
78 |
at 115.2 Kbps */
|
|
|
79 |
UBRRH = 0x00;
|
|
|
80 |
|
|
|
81 |
#ifdef NO_CRYSTAL
|
|
|
82 |
UBRRL = 18; /* 18 for double clocking at 115.2 kbps */
|
|
|
83 |
#else
|
|
|
84 |
UBRRL = 0x08; /* for 16 MHz crystal at 115.2 kbps */
|
|
|
85 |
#endif
|
|
|
86 |
|
|
|
87 |
/* enable the tx and rx capabilities of the UART...as well
|
|
|
88 |
as the receive complete interrupt */
|
|
|
89 |
UCSRB = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
|
|
|
90 |
|
|
|
91 |
/* set up the control registers so the UART works at 8N1 */
|
|
|
92 |
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
|
|
|
93 |
|
|
|
94 |
#ifdef NO_CRYSTAL
|
|
|
95 |
/* set the baud rate to use the double-speed */
|
|
|
96 |
UCSRA = (1<<U2X);
|
|
|
97 |
#endif
|
|
|
98 |
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/***********************************************************
|
|
|
102 |
Function Name: UartInt_txByte
|
|
|
103 |
Function Description: This function is responsible for
|
|
|
104 |
transmitting a single byte on the uart.
|
|
|
105 |
Inputs: txByte - the byte to send
|
|
|
106 |
Outputs: none
|
|
|
107 |
NOTES: When the TX UDRE (data register empty) is set, there
|
|
|
108 |
is puposefully no interrupt...thus, to send a string of
|
|
|
109 |
data out, the calling routine needs to hold up the entire
|
|
|
110 |
application while this takes place (or just send one
|
|
|
111 |
byte at a time at strtegically timed intervals, like
|
|
|
112 |
the stats data is sent out :-)
|
|
|
113 |
***********************************************************/
|
|
|
114 |
void UartInt_txByte(unsigned char txByte)
|
|
|
115 |
{
|
|
|
116 |
/* Wait for empty transmit buffer */
|
|
|
117 |
while ( !( UCSRA & (1<<UDRE)) );
|
|
|
118 |
/* Put data into buffer, sends the data */
|
|
|
119 |
UDR = txByte;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/***********************************************************
|
|
|
123 |
Function Name: SIG_UART_RECV ISR
|
|
|
124 |
Function Description: This function is responsible for
|
|
|
125 |
handling the interrupt caused when a data byte is
|
|
|
126 |
received by the UART.
|
|
|
127 |
Inputs: none
|
|
|
128 |
Outputs: none
|
|
|
129 |
NOTES: This function was originally written in assembly,
|
|
|
130 |
but moved over to C when the setting of the "T" bit at
|
|
|
131 |
the end of the routine was no longer necessary (this
|
|
|
132 |
theoretically allowed the AVRcam to respond to serial
|
|
|
133 |
bytes in the middle of tracking or dumping a frame.
|
|
|
134 |
But it wasn't really needed, and understanding the C
|
|
|
135 |
is easier :-)
|
|
|
136 |
***********************************************************/
|
|
|
137 |
SIGNAL(SIG_UART_RECV)
|
|
|
138 |
{
|
|
|
139 |
unsigned char tmpHead;
|
|
|
140 |
/* read the data byte, put it in the serial queue, and
|
|
|
141 |
post the event */
|
|
|
142 |
|
|
|
143 |
UIMgr_rxFifo[UIMgr_rxFifoHead] = UDR;
|
|
|
144 |
|
|
|
145 |
/* now move the head up */
|
|
|
146 |
tmpHead = (UIMgr_rxFifoHead + 1) & (UI_MGR_RX_FIFO_MASK);
|
|
|
147 |
UIMgr_rxFifoHead = tmpHead;
|
|
|
148 |
|
|
|
149 |
/* write the serial received event to the event fifo */
|
|
|
150 |
Exec_eventFifo[Exec_eventFifoHead] = EV_SERIAL_DATA_RECEIVED;
|
|
|
151 |
|
|
|
152 |
/* now move the head up */
|
|
|
153 |
tmpHead = (Exec_eventFifoHead + 1) & (EXEC_EVENT_FIFO_MASK);
|
|
|
154 |
Exec_eventFifoHead = tmpHead;
|
|
|
155 |
}
|
|
|
156 |
|