Subversion Repositories HomeAutomation

Rev

Rev 280 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 280 Rev 281
1
/**
1
/**
2
 *
2
 *
3
 *
3
 *
4
 *
4
 *
5
 * @date    2006-11-21
5
 * @date    2006-11-21
6
 * @author  Jimmy Myhrman, Anders Runeson
6
 * @author  Jimmy Myhrman, Anders Runeson
7
 *  
7
 *  
8
 */
8
 */
9
 
9
 
10
/*-----------------------------------------------------------------------------
10
/*-----------------------------------------------------------------------------
11
 * Includes
11
 * Includes
12
 *---------------------------------------------------------------------------*/
12
 *---------------------------------------------------------------------------*/
13
/* system files */
13
/* system files */
14
#include <avr/io.h>
14
#include <avr/io.h>
15
#include <avr/interrupt.h>
15
#include <avr/interrupt.h>
16
#include <avr/wdt.h>
16
#include <avr/wdt.h>
17
#include <stdio.h>
17
#include <stdio.h>
18
/* lib files */
18
/* lib files */
19
#include <can.h>
19
#include <can.h>
20
//#include <serial.h>
20
//#include <serial.h>
21
#include <timebase.h>
21
#include <timebase.h>
22
#include <ip_arp_udp_tcp.h>
22
#include <ip_arp_udp_tcp.h>
23
#include <enc28j60.h>
23
#include <enc28j60.h>
24
#include <timeout.h>
24
#include <timeout.h>
25
#include <avr_compat.h>
25
#include <avr_compat.h>
26
#include <net.h>
26
#include <net.h>
27
 
27
 
28
// please modify the following two lines. mac and ip have to be unique
28
// please modify the following two lines. mac and ip have to be unique
29
// in your local area network. You can not have the same numbers in
29
// in your local area network. You can not have the same numbers in
30
// two devices:
30
// two devices:
31
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
31
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
32
static uint8_t myip[4] = {193,11,254,22};
32
static uint8_t myip[4] = {193,11,254,22};
33
static uint16_t rmDumperPrt =1200; // remote port for dumping data
33
static uint16_t rmDumperPrt =1200; // remote port for dumping data
34
static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data
34
static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data
35
static uint16_t locDumperPrt =1200; // listen port 
35
static uint16_t locDumperPrt =1200; // listen port 
36
static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data
36
static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data
37
static uint8_t gotserver = 0;
37
static uint8_t gotserver = 0;
38
 
38
 
39
// how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX
39
// how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX
40
 
40
 
41
static uint8_t remotemac[6];
41
static uint8_t remotemac[6];
42
static uint8_t remoteip[4];
42
static uint8_t remoteip[4];
43
 
43
 
44
#define BUFFER_SIZE 250
44
#define BUFFER_SIZE 250
45
static uint8_t buf[BUFFER_SIZE+1];
45
static uint8_t buf[BUFFER_SIZE+1];
46
 
46
 
47
#define SEND_BUFFER_SIZE 20
47
#define SEND_BUFFER_SIZE 20
48
static char sendbuf[SEND_BUFFER_SIZE+1];
48
static char sendbuf[SEND_BUFFER_SIZE+1];
49
static uint8_t buffpoint;
49
static uint8_t buffpoint;
-
 
50
 
-
 
51
#define C2S_START_BYTE 253
-
 
52
#define C2S_END_BYTE 250
50
 
53
 
51
/*----------------------------------------------------------------------------
54
/*----------------------------------------------------------------------------
52
 * Putchar for udp
55
 * Putchar for udp
53
 * Implements a small buffer, sends packet over udp if buffer is full or
56
 * Implements a small buffer, sends packet over udp if buffer is full or
54
 * \n-char is sent.
57
 * \n-char is sent.
55
 * Needs a timeout to force send.
58
 * Needs a timeout to force send.
56
 *--------------------------------------------------------------------------*/
59
 *--------------------------------------------------------------------------*/
57
int udp_putchar(char c, FILE* stream) {
60
int udp_putchar(char c, FILE* stream) {
58
    if (gotserver != 0) {
61
    if (gotserver != 0) {
59
        sendbuf[buffpoint] = c;
62
        sendbuf[buffpoint] = c;
60
        buffpoint++;
63
        buffpoint++;
61
       
64
       
62
        if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) {
65
        if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) {
63
            send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip);
66
            send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip);
64
            buffpoint = 0;
67
            buffpoint = 0;
65
        }
68
        }
66
    }
69
    }
67
    return 0;
70
    return 0;
68
}
71
}
69
 
72
 
70
static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE);
73
static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE);
71
 
74
 
72
/*-----------------------------------------------------------------------------
75
/*-----------------------------------------------------------------------------
73
 * Main Program
76
 * Main Program
74
 *---------------------------------------------------------------------------*/
77
 *---------------------------------------------------------------------------*/
75
int main(void) {
78
int main(void) {
76
 
79
 
77
    //ethernet vars
80
    //ethernet vars
78
    uint16_t plen = 0;
81
    uint16_t plen = 0;
79
    uint8_t ledi=0;
82
    uint8_t ledi=0;
80
    uint8_t payloadlen=0;
83
    uint8_t payloadlen=0;
81
    buffpoint = 0;
84
    buffpoint = 0;
82
   
85
   
83
    //can vars
86
    //can vars
84
    Can_Message_t txMsg;
87
    Can_Message_t txMsg;
85
    Can_Message_t rxMsg;
88
    Can_Message_t rxMsg;
86
    txMsg.Id = 3;
89
    txMsg.Id = 3;
87
    txMsg.DataLength = 0;
90
    txMsg.DataLength = 0;
88
    txMsg.RemoteFlag = 0;
91
    txMsg.RemoteFlag = 0;
89
    txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
92
    txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
90
 
93
 
91
 
94
 
92
    uint32_t timeStamp = 0;
95
    uint32_t timeStamp = 0;
93
   
96
   
94
    Mcu_Init();
97
    Mcu_Init();
95
    Timebase_Init();
98
    Timebase_Init();
96
    //Serial_Init();
99
    //Serial_Init();
97
 
100
 
98
    //alla printf ska skriva till udp
101
    //alla printf ska skriva till udp
99
    stdout = &mystdout;    //set the output stream 
102
    stdout = &mystdout;    //set the output stream 
100
   
103
   
101
    sei();
104
    sei();
102
 
105
 
103
//  printf("CanInit...");
106
//  printf("CanInit...");
104
    if (Can_Init() != CAN_OK) {
107
    if (Can_Init() != CAN_OK) {
105
//      printf("FAILED!\n");
108
//      printf("FAILED!\n");
106
    }
109
    }
107
    else {
110
    else {
108
//      printf("OK!\n");
111
//      printf("OK!\n");
109
    }
112
    }
110
 
113
 
111
    /* enable PB0, reset as output */
114
    /* enable PB0, reset as output */
112
    DDRB|= (1<<DDB0);
115
    DDRB|= (1<<DDB0);
113
    /* set output to gnd, reset the ethernet chip */
116
    /* set output to gnd, reset the ethernet chip */
114
    PORTB &= ~(1<<PB0);
117
    PORTB &= ~(1<<PB0);
115
    delay_ms(20);
118
    delay_ms(20);
116
    /* set output to Vcc, reset inactive */
119
    /* set output to Vcc, reset inactive */
117
    PORTB|= (1<<PB0);
120
    PORTB|= (1<<PB0);
118
    delay_ms(100);
121
    delay_ms(100);
119
   
122
   
120
    // LED
123
    // LED
121
    /* enable PB1, LED as output */
124
    /* enable PB1, LED as output */
122
    DDRD|= (1<<DDD6);
125
    DDRD|= (1<<DDD6);
123
    /* set output to Vcc, LED off */
126
    /* set output to Vcc, LED off */
124
    PORTD|= (1<<PD6);
127
    PORTD|= (1<<PD6);
125
 
128
 
126
    //printf("Init enc\n");
129
    //printf("Init enc\n");
127
    /*initialize enc28j60*/
130
    /*initialize enc28j60*/
128
    enc28j60Init(mymac);
131
    enc28j60Init(mymac);
129
    delay_ms(20);
132
    delay_ms(20);
130
   
133
   
131
    /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
134
    /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
132
    // LEDB=yellow LEDA=green
135
    // LEDB=yellow LEDA=green
133
    //
136
    //
134
    // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
137
    // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
135
    enc28j60PhyWrite(PHLCON,0x476);
138
    enc28j60PhyWrite(PHLCON,0x476);
136
    delay_ms(20);
139
    delay_ms(20);
137
   
140
   
138
    /* set output to GND, red LED on */
141
    /* set output to GND, red LED on */
139
    PORTD &= ~(1<<PD6);
142
    PORTD &= ~(1<<PD6);
140
    ledi=1;
143
    ledi=1;
141
   
144
   
142
    //init the ethernet/ip layer:
145
    //init the ethernet/ip layer:
143
    init_ip_arp_udp_tcp(mymac,myip,80);
146
    init_ip_arp_udp_tcp(mymac,myip,80);
144
 
147
 
145
    /* main loop */
148
    /* main loop */
146
    while (1) {
149
    while (1) {
147
        /* service the CAN routines */
150
        /* service the CAN routines */
148
        Can_Service();
151
        Can_Service();
149
       
152
       
150
        /* send CAN message and check for CAN errors once every second */
153
        /* send CAN message and check for CAN errors once every second */
151
        if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
154
        if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
152
            timeStamp = Timebase_CurrentTime();
155
            timeStamp = Timebase_CurrentTime();
153
            /* send txMsg */
156
            /* send txMsg */
154
            //txMsg.Id++;
157
            //txMsg.Id++;
155
            //txMsg.Id = 3;
158
            //txMsg.Id = 3;
156
            //Can_Send(&txMsg);
159
            //Can_Send(&txMsg);
157
           
160
           
158
        }
161
        }
159
       
162
       
160
        /* check if any messages have been received */
163
        /* check if any messages have been received */
161
        while (Can_Receive(&rxMsg) == CAN_OK) {
164
        while (Can_Receive(&rxMsg) == CAN_OK) {
-
 
165
            uint8_t i;
162
            printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
166
            printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
163
            printf("data={ ");
167
            printf("data={ ");
164
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
168
            for (i=0; i<rxMsg.DataLength; i++) {
165
                printf("%x ", rxMsg.Data.bytes[i]);
169
                printf("%x ", rxMsg.Data.bytes[i]);
166
            }
170
            }
167
            printf("}\n");
171
            printf("}\n");
168
           
172
           
-
 
173
            //skicka ocksÃ¥ som udppaket till rmCan2SerPrt
-
 
174
            char sendbuf2[17];
-
 
175
            sendbuf2[0] = C2S_START_BYTE;
-
 
176
            sendbuf2[1] = (uint8_t)rxMsg.Id;
-
 
177
            sendbuf2[2] = (uint8_t)(rxMsg.Id>>8);
-
 
178
            sendbuf2[3] = (uint8_t)(rxMsg.Id>>16);
-
 
179
            sendbuf2[4] = (uint8_t)(rxMsg.Id>>24);
-
 
180
            sendbuf2[5] = rxMsg.ExtendedFlag;
-
 
181
            sendbuf2[6] = rxMsg.RemoteFlag;
-
 
182
            sendbuf2[7] = rxMsg.DataLength;
-
 
183
            for (uint8_t i=0; i<8; i++) {
-
 
184
                sendbuf2[8+i] = rxMsg.Data.bytes[i];
-
 
185
            }
-
 
186
            sendbuf2[16] = C2S_END_BYTE;
-
 
187
            send_udp(buf, sendbuf2, 17, rmCan2SerPrt, remotemac, remoteip);
169
        }
188
        }
170
       
189
       
171
        //test-debug-kod
190
        //test-debug-kod
172
        if ((enc28j60Read(EIR) & EIR_TXERIF)) {
191
        if ((enc28j60Read(EIR) & EIR_TXERIF)) {
173
            //printf("EIR_TXERIF set!\n");
192
            //printf("EIR_TXERIF set!\n");
174
            if ((enc28j60Read(ECON1) & ECON1_TXRTS)) {
193
            if ((enc28j60Read(ECON1) & ECON1_TXRTS)) {
175
                //printf("ECON1_TXRTS set!\n");
194
                //printf("ECON1_TXRTS set!\n");
176
            }
195
            }
177
        }
196
        }
178
        if ((enc28j60Read(EIR) & EIR_TXIF)) {
197
        if ((enc28j60Read(EIR) & EIR_TXIF)) {
179
            //printf("EIR_TXIF set! ");
198
            //printf("EIR_TXIF set! ");
180
        }
199
        }
181
        if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) {
200
        if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) {
182
            //printf("ESTAT_TXABRT set!\n");
201
            //printf("ESTAT_TXABRT set!\n");
183
            //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1));
202
            //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1));
184
            enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT);
203
            enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT);
185
        }
204
        }
186
       
205
       
187
        // get the next new packet:
206
        // get the next new packet:
188
        plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
207
        plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
189
 
208
 
190
        /*plen will ne unequal to zero if there is a valid
209
        /*plen will ne unequal to zero if there is a valid
191
         * packet (without crc error) */
210
         * packet (without crc error) */
192
        if(plen!=0){
211
        if(plen!=0){
193
            // arp is broadcast if unknown but a host may also
212
            // arp is broadcast if unknown but a host may also
194
            // verify the mac address by sending it to 
213
            // verify the mac address by sending it to 
195
            // a unicast address.
214
            // a unicast address.
196
            if(eth_type_is_arp_and_my_ip(buf,plen)){
215
            if(eth_type_is_arp_and_my_ip(buf,plen)){
197
                    make_arp_answer_from_request(buf);
216
                    make_arp_answer_from_request(buf);
198
            } else {
217
            } else {
199
                // check if ip packets (icmp or udp) are for us:
218
                // check if ip packets (icmp or udp) are for us:
200
                if(eth_type_is_ip_and_my_ip(buf,plen)!=0){
219
                if(eth_type_is_ip_and_my_ip(buf,plen)!=0){
201
                   
220
                   
202
                    if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
221
                    if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
203
                            // a ping packet, let's send pong
222
                            // a ping packet, let's send pong
204
                            make_echo_reply_from_request(buf,plen);
223
                            make_echo_reply_from_request(buf,plen);
205
                            //txMsg.Id = 6;
224
                            //txMsg.Id = 6;
206
                            //Can_Send(&txMsg);
225
                            //Can_Send(&txMsg);
207
                           
226
                           
208
                    }
227
                    }
209
                    if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
228
                    if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
210
                        payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
229
                        payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
211
                            printf("hej %x %x\n", buf[UDP_DST_PORT_H_P], buf[UDP_DST_PORT_L_P]);
230
                        //printf("hej %x %x\n", buf[UDP_DST_PORT_H_P], buf[UDP_DST_PORT_L_P]);
212
                        if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) {
231
                        if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) {
-
 
232
                            if (payloadlen==17) {   //ifsats ska in i ovan eller nedan ifsats kanske? nedan
-
 
233
                                if (buf[UDP_DATA_P]==C2S_START_BYTE && buf[UDP_DATA_P+16]==C2S_END_BYTE) {
-
 
234
                                    Can_Message_t cm;
-
 
235
                                    //cm.Id = 0;
-
 
236
                                    cm.DataLength = buf[UDP_DATA_P+7];
-
 
237
                                    cm.RemoteFlag = buf[UDP_DATA_P+6];
-
 
238
                                    cm.ExtendedFlag = buf[UDP_DATA_P+5];
-
 
239
                                    cm.Id = (uint32_t)buf[UDP_DATA_P+1] + ((uint32_t)buf[UDP_DATA_P+2] << 8) + ((uint32_t)buf[UDP_DATA_P+3] << 16) + ((uint32_t)buf[UDP_DATA_P+4] << 24);
-
 
240
                                    uint8_t i;
-
 
241
                                    for (i = 0; i < cm.DataLength; i++) {
-
 
242
                                        cm.Data.bytes[i] = buf[UDP_DATA_P+8+i];
-
 
243
                                    }
-
 
244
                                    Can_Send(&cm);
-
 
245
                                }
213
                            printf("hoj\n");
246
                                printf("hoj\n");
-
 
247
                            }
214
                        } else if (buf[UDP_DATA_P]=='t' && payloadlen==5){
248
                        } else if (buf[UDP_DATA_P]=='t' && payloadlen==5){
215
                            uint8_t i=0;
249
                            uint8_t i=0;
216
                            if (gotserver == 0) {
250
                            if (gotserver == 0) {
217
                                while(i<6){
251
                                while(i<6){
218
                                    remotemac[i]=buf[ETH_SRC_MAC +i];
252
                                    remotemac[i]=buf[ETH_SRC_MAC +i];
219
                                    i++;
253
                                    i++;
220
                                }
254
                                }
221
                                i=0;
255
                                i=0;
222
                                while(i<4){
256
                                while(i<4){
223
                                    remoteip[i]=buf[IP_SRC_P +i];
257
                                    remoteip[i]=buf[IP_SRC_P +i];
224
                                    i++;
258
                                    i++;
225
                                }
259
                                }
226
                                i=0;
260
                                i=0;
227
                               
261
                               
228
                                gotserver = 1;
262
                                gotserver = 1;
229
                                make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt);
263
                                make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt);
230
                            } else {
264
                            } else {
231
                                make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt);
265
                                make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt);
232
                            }
266
                            }
233
                        }
267
                        }
234
                    }                      
268
                    }                      
235
                }
269
                }
236
            }
270
            }
237
        }
271
        }
238
       
272
       
239
 
273
 
240
               
274
               
241
       
275
       
242
    }
276
    }
243
   
277
   
244
    return 0;
278
    return 0;
245
}
279
}
246
 
280