-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
126 lines (103 loc) · 2.85 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
#include "spi.h"
#include "twi_master.h"
#include "net.h"
#include "ethernet.h"
#include "arp.h"
#include "ipv4.h"
#include "bootp.h"
#include "stats.h"
#include "eeprom.h"
#include "enc.h"
#include "application.h"
#include "util.h"
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <string.h>
#include <stdint.h>
enum
{
max_frame_size = 384,
};
static ipv4_addr_t my_ipv4_address;
static mac_addr_t my_mac_address;
ISR(WDT_vect)
{
wd_interrupts++;
}
int main(void)
{
uint8_t rx_frame[max_frame_size];
uint8_t tx_frame[max_frame_size];
uint16_t rx_frame_length;
uint16_t tx_frame_length;
cli();
wdt_reset();
MCUSR = 0;
wdt_disable();
PRR = 0xff;
#if 0
b0 /cs b0 O square green spi ss
b1 oc1a O small red light
b2 (/ss) oc1b O small green ir
b3 mosi O square green spi mosi
b4 miso I square red spi miso
b5 sck O square red spi sck
b6 b6 O small orange led spi
b7 b7 I button light down
c0 adc0 I tmp36
c1 c1 O *
c2 c2 O *
c3 c3 O *
c4 sda O i2c sda
c5 scl O i2c scl
c6 RESET O
d0 d0 O small transparent green led heartbeat
d1 d1 O small transparent red led command
d2 int0 I enc int
d3 d3 O rectangular green DEBUG 1
d4 d4 O small red led i2c
d5 d5 O large green DEBUG 2
d6 d6 O large red DEBUG 3
d7 d7 I button light up
#endif
MCUCR |= _BV(PUD); // disable pullups
DDRB = _BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(5) | _BV(6);
DDRC = _BV(1) | _BV(2) | _BV(3) | _BV(4) | _BV(5) | _BV(6);
DDRD = _BV(0) | _BV(1) | _BV(3) | _BV(4) | _BV(5) | _BV(6);
eeprom_read_mac_address(&my_mac_address);
my_ipv4_address.byte[0] = 0;
my_ipv4_address.byte[1] = 0;
my_ipv4_address.byte[2] = 0;
my_ipv4_address.byte[3] = 0;
PORTD = 0;
sleep(200);
PORTD = _BV(0);
sleep(200);
PORTD = _BV(1);
sleep(200);
PORTD = 0;
wdt_enable(WDTO_2S);
spi_init();
twi_master_init();
enc_init(max_frame_size, &my_mac_address);
enc_set_led(PHLCON_LED_RCV, PHLCON_LED_XMIT);
application_init();
sei();
for(;;)
{
pause_idle(); // gets woken by the 122 Hz timer1 interrupt or packet arrival or watchdog interrupt
WDTCSR |= _BV(WDIE); // enable wdt interrupt, reset
application_periodic(); // run periodic tasks
if(ipv4_address_match(&my_ipv4_address, &ipv4_addr_zero))
{
tx_frame_length = bootp_create_request(tx_frame, &my_mac_address);
enc_send_frame(tx_frame, tx_frame_length);
}
if((rx_frame_length = enc_receive_frame(rx_frame, sizeof(rx_frame))) == 0)
continue;
if((tx_frame_length = ethernet_process_frame(rx_frame, rx_frame_length, tx_frame, sizeof(tx_frame), &my_mac_address, &my_ipv4_address)) == 0)
continue;
enc_send_frame(tx_frame, tx_frame_length);
}
}