Subversion Repositories HomeAutomation

Rev

Rev 205 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
205 arune 1
/*********************************************
2
 * vim:sw=8:ts=8:si:et
3
 * To use the above modeline in vim you must have "set modeline" in your .vimrc
4
 * Author: Guido Socher
5
 * Copyright: GPL V2
6
 *
7
 * Ethernet remote device and sensor: test program 1
8
 *
9
 * Title: Microchip ENC28J60 Ethernet Interface Driver
10
 * Chip type           : ATMEGA88 with ENC28J60
11
 *********************************************/
12
/*-----------------------------------------------------------------------------
13
 * Includes
14
 *---------------------------------------------------------------------------*/
15
/* system files */
16
#include <avr/io.h>
17
 
18
/* lib files */
19
#include <ip_arp_udp_tcp.h>
20
#include <enc28j60.h>
21
#include <timeout.h>
22
#include <avr_compat.h>
23
#include <net.h>
24
 
25
// please modify the following two lines. mac and ip have to be unique
26
// in your local area network. You can not have the same numbers in
27
// two devices:
28
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
29
static uint8_t myip[4] = {193,11,254,22};
30
// how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX
31
 
32
#define BUFFER_SIZE 250
33
static uint8_t buf[BUFFER_SIZE+1];
34
 
35
int main(void){
36
 
37
 
38
        uint16_t plen;
39
        uint8_t i=0;
40
 
41
        // set the clock speed to 8MHz
42
        // set the clock prescaler. First write CLKPCE to enable setting of clock the
43
        // next four instructions.
44
        CLKPR=(1<<CLKPCE);
45
        CLKPR=0; // 8 MHZ
46
 
47
        /* enable PB0, reset as output */
48
        DDRB|= (1<<DDB0);
49
 
50
        /* set output to gnd, reset the ethernet chip */
51
        PORTB &= ~(1<<PB0);
52
        delay_ms(20);
53
        /* set output to Vcc, reset inactive */
54
        PORTB|= (1<<PB0);
55
        delay_ms(100);
56
 
57
        // LED
58
        /* enable PB1, LED as output */
59
        DDRD|= (1<<DDD6);
60
 
61
        /* set output to Vcc, LED off */
62
        PORTD|= (1<<PD6);
63
 
64
        /*initialize enc28j60*/
65
        enc28j60Init(mymac);
66
 
67
    /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
68
    // LEDA=greed LEDB=yellow
69
    //
70
    // 0x880 is PHLCON LEDB=on, LEDA=on
71
    // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
72
    enc28j60PhyWrite(PHLCON,0x880);
73
    delay_ms(500);
74
    //
75
    // 0x990 is PHLCON LEDB=off, LEDA=off
76
    // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
77
    enc28j60PhyWrite(PHLCON,0x990);
78
    delay_ms(500);
79
    //
80
    // 0x880 is PHLCON LEDB=on, LEDA=on
81
    // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
82
    enc28j60PhyWrite(PHLCON,0x880);
83
    delay_ms(500);
84
    //
85
    // 0x990 is PHLCON LEDB=off, LEDA=off
86
    // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
87
    enc28j60PhyWrite(PHLCON,0x990);
88
    delay_ms(500);
89
    //
90
        // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
91
        // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);
92
        enc28j60PhyWrite(PHLCON,0x476);
93
    delay_ms(100);
94
 
95
        //init the ethernet/ip layer:
96
        init_ip_arp_udp_tcp(mymac,myip,80);
97
 
98
        while(1){
99
                // get the next new packet:
100
                plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
101
 
102
                /*plen will ne unequal to zero if there is a valid
103
                 * packet (without crc error) */
104
                if(plen==0){
105
                        continue;
106
                }
107
                // arp is broadcast if unknown but a host may also
108
                // verify the mac address by sending it to 
109
                // a unicast address.
110
                if(eth_type_is_arp_and_my_ip(buf,plen)){
111
                        make_arp_answer_from_request(buf);
112
                        continue;
113
                }
114
                // check if ip packets (icmp or udp) are for us:
115
                if(eth_type_is_ip_and_my_ip(buf,plen)==0){
116
                        continue;
117
                }
118
 
119
                if (i){
120
                        /* set output to Vcc, LED off */
121
                        PORTD|= (1<<PD6);
122
                        i=0;
123
                }else{
124
                        /* set output to GND, LED on */
125
                        PORTD &= ~(1<<PD6);
126
                        i=1;
127
                }
128
 
129
 
130
                if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
131
                        // a ping packet, let's send pong
132
                        make_echo_reply_from_request(buf,plen);
133
                        continue;
134
                }
135
        }
136
        return (0);
137
}