-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
200 lines (163 loc) · 4.29 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*****************************************************************************
* Module Name: uIP-AVR Port - main control loop shell
*
* Created By: Louis Beaudoin (www.embedded-creations.com)
*
* Original Release: September 21, 2002
*
* Modifications from Sven Sperner <cethss@gmail.com>
*
* Module Description:
* This main control loop shell provides everything required for a basic uIP
* application using the RTL8019AS NIC
*
*****************************************************************************/
#include <avr/pgmspace.h>
#include "uip.h"
#include "nic.h"
#include "uip_arp.h"
#include "compiler.h"
#include "services.h"
#include "usart.h"
#include "mca25.h"
#include "servo.h"
//#include "clock.h"
#define BUF ((struct uip_eth_hdr *)&uip_buf[0])
//#define LED_RED (PIND & (1 <<7))
volatile unsigned char usart_timeout;
mca25_t mca25_stat;
/*****************************************************************************
* Periodic Timout Functions and variables
*
* The periodic timeout rate can be changed depeding on your application
* Modify these functions and variables based on your AVR device and clock
* rate
* The current setup will interrupt every 256 timer ticks when the timer
* counter overflows. timerCounter must count until 0.5 seconds have
* alapsed
*****************************************************************************/
#define TIMER_PRESCALE 1024
#define TIMERCOUNTER_PERIODIC_TIMEOUT (F_CPU / TIMER_PRESCALE / 2 / 256)
static unsigned char timerCounter;
void initTimer(void)
{
TCCR0=5; //outp( 5, TCCR0 ) ; // timer0 prescale 1/1024
// interrupt on overflow
TIMSK|=(1<<TOIE0); //sbi( TIMSK, TOIE0 ) ;
timerCounter = 0;
}
#ifdef __IMAGECRAFT__
#pragma interrupt_handler SIG_OVERFLOW0:iv_TIMER0_OVF
#endif
SIGNAL(SIG_OVERFLOW0)
{
timerCounter++;
if((unsigned char)!(timerCounter&3)) if (usart_timeout) usart_timeout--;
}
/*****************************************************************************
* Main Control Loop
*
*
*****************************************************************************/
int main(void)
{
//led PB4
// Pins als Ausgänge definieren:
DDRB |= (1 << 4);
PORTB |= (0 << 4);
//DDRD |= (1 << 7);
//PORTD |= (1 << 7);
unsigned char i;
unsigned char arptimer=0;
// init NIC device driver
nic_init();
// init uIP
uip_init();
// init app
services_init();
// init ARP cache
uip_arp_init();
// init periodic timer
initTimer();
SET_USART_9600();
//PORTB |= (1 << 4); //LED PB4 ein
sei();
// if((mca25_stat.init=mca25_init())==0) mca25_stat.init=mca25_configure();
// if(mca25_stat.init) {
// print error message?;
// }
#if USE_MCA25_CAM
//DEBUG:
MCA25_ERROR_LED_OFF();
MCA25_CLOCK_LED_OFF();
//DDRB = 0xFF;
MCA25_STATUS_LED_ON();
mca25_init();
mca25_configure();
MCA25_STATUS_LED_OFF();
MCA25_ERROR_LED_ON();
MCA25_CLOCK_LED_ON();
#endif
// #if USE_CLOCK
// Start_Clock();
//#endif
#if USE_SERVO
servo_init();
#endif
//PORTD |= (1 << 7);
while(1)
{
// look for a packet
uip_len = nic_poll();
if(uip_len == 0)
{
// if timed out, call periodic function for each connection
if(timerCounter > TIMERCOUNTER_PERIODIC_TIMEOUT)
{
timerCounter = 0;
for(i = 0; i < UIP_CONNS; i++)
{
uip_periodic(i);
// transmit a packet, if one is ready
if(uip_len > 0)
{
uip_arp_out();
nic_send();
}
}
/* Call the ARP timer function every 10 seconds. */
if(++arptimer == 20)
{
uip_arp_timer();
arptimer = 0;
}
}
}
else // packet received
{
// process an IP packet
if(BUF->type == htons(UIP_ETHTYPE_IP))
{
// add the source to the ARP cache
// also correctly set the ethernet packet length before processing
uip_arp_ipin();
uip_input();
// transmit a packet, if one is ready
if(uip_len > 0)
{
uip_arp_out();
nic_send();
}
}
// process an ARP packet
else if(BUF->type == htons(UIP_ETHTYPE_ARP))
{
uip_arp_arpin();
// transmit a packet, if one is ready
if(uip_len > 0)
nic_send();
}
}
}
return 1;
}