Subversion Repositories HomeAutomation

Rev

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

Rev 212 Rev 280
1
/**
1
/**
2
 * CAN Test. This program sends a CAN message once every second. The ID of the
-
 
3
 * message is increased each time for testing purposes.
-
 
-
 
2
 *
-
 
3
 *
4
 *
4
 *
5
 * @date    2006-11-21
5
 * @date    2006-11-21
6
 * @author  Jimmy Myhrman
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
-
 
34
static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data
33
static uint16_t remoteport =1200; // listen port for udp
35
static uint16_t locDumperPrt =1200; // listen port 
-
 
36
static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data
34
static uint8_t gotserver = 0;
37
static uint8_t gotserver = 0;
35
 
38
 
36
// 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
37
 
40
 
38
static uint8_t remotemac[6];
41
static uint8_t remotemac[6];
39
static uint8_t remoteip[4];
42
static uint8_t remoteip[4];
40
 
43
 
41
#define BUFFER_SIZE 250
44
#define BUFFER_SIZE 250
42
static uint8_t buf[BUFFER_SIZE+1];
45
static uint8_t buf[BUFFER_SIZE+1];
43
 
46
 
44
#define SEND_BUFFER_SIZE 20
47
#define SEND_BUFFER_SIZE 20
45
static char sendbuf[SEND_BUFFER_SIZE+1];
48
static char sendbuf[SEND_BUFFER_SIZE+1];
46
static uint8_t buffpoint;
49
static uint8_t buffpoint;
47
 
50
 
48
/*----------------------------------------------------------------------------
51
/*----------------------------------------------------------------------------
49
 * Putchar for udp
52
 * Putchar for udp
50
 * Implements a small buffer, sends packet over udp if buffer is full or
53
 * Implements a small buffer, sends packet over udp if buffer is full or
51
 * \n-char is sent.
54
 * \n-char is sent.
52
 * Needs a timeout to force send.
55
 * Needs a timeout to force send.
53
 *--------------------------------------------------------------------------*/
56
 *--------------------------------------------------------------------------*/
54
int udp_putchar(char c, FILE* stream) {
57
int udp_putchar(char c, FILE* stream) {
55
    if (gotserver != 0) {
58
    if (gotserver != 0) {
56
        sendbuf[buffpoint] = c;
59
        sendbuf[buffpoint] = c;
57
        buffpoint++;
60
        buffpoint++;
58
       
61
       
59
        if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) {
62
        if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) {
60
            send_udp(buf, sendbuf, buffpoint, remoteport, remotemac, remoteip);
63
            send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip);
61
            buffpoint = 0;
64
            buffpoint = 0;
62
        }
65
        }
63
    }
66
    }
64
    return 0;
67
    return 0;
65
}
68
}
66
 
69
 
67
static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE);
70
static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE);
68
 
71
 
69
/*-----------------------------------------------------------------------------
72
/*-----------------------------------------------------------------------------
70
 * Main Program
73
 * Main Program
71
 *---------------------------------------------------------------------------*/
74
 *---------------------------------------------------------------------------*/
72
int main(void) {
75
int main(void) {
73
 
76
 
74
    //ethernet vars
77
    //ethernet vars
75
    uint16_t plen = 0;
78
    uint16_t plen = 0;
76
    uint8_t ledi=0;
79
    uint8_t ledi=0;
77
    uint8_t payloadlen=0;
80
    uint8_t payloadlen=0;
78
    buffpoint = 0;
81
    buffpoint = 0;
79
   
82
   
80
    //can vars
83
    //can vars
81
    Can_Message_t txMsg;
84
    Can_Message_t txMsg;
82
    Can_Message_t rxMsg;
85
    Can_Message_t rxMsg;
83
    txMsg.Id = 3;
86
    txMsg.Id = 3;
84
    txMsg.DataLength = 0;
87
    txMsg.DataLength = 0;
85
    txMsg.RemoteFlag = 0;
88
    txMsg.RemoteFlag = 0;
86
    txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
89
    txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
87
 
90
 
88
 
91
 
89
    uint32_t timeStamp = 0;
92
    uint32_t timeStamp = 0;
90
   
93
   
91
    Mcu_Init();
94
    Mcu_Init();
92
    Timebase_Init();
95
    Timebase_Init();
93
    Serial_Init();
96
    //Serial_Init();
94
 
97
 
95
    //alla printf ska skriva till udp
98
    //alla printf ska skriva till udp
96
    stdout = &mystdout;    //set the output stream 
99
    stdout = &mystdout;    //set the output stream 
97
   
100
   
98
    sei();
101
    sei();
99
 
102
 
100
//  printf("CanInit...");
103
//  printf("CanInit...");
101
    if (Can_Init() != CAN_OK) {
104
    if (Can_Init() != CAN_OK) {
102
//      printf("FAILED!\n");
105
//      printf("FAILED!\n");
103
    }
106
    }
104
    else {
107
    else {
105
//      printf("OK!\n");
108
//      printf("OK!\n");
106
    }
109
    }
107
 
110
 
108
    /* enable PB0, reset as output */
111
    /* enable PB0, reset as output */
109
    DDRB|= (1<<DDB0);
112
    DDRB|= (1<<DDB0);
110
    /* set output to gnd, reset the ethernet chip */
113
    /* set output to gnd, reset the ethernet chip */
111
    PORTB &= ~(1<<PB0);
114
    PORTB &= ~(1<<PB0);
112
    delay_ms(20);
115
    delay_ms(20);
113
    /* set output to Vcc, reset inactive */
116
    /* set output to Vcc, reset inactive */
114
    PORTB|= (1<<PB0);
117
    PORTB|= (1<<PB0);
115
    delay_ms(100);
118
    delay_ms(100);
116
   
119
   
117
    // LED
120
    // LED
118
    /* enable PB1, LED as output */
121
    /* enable PB1, LED as output */
119
    DDRD|= (1<<DDD6);
122
    DDRD|= (1<<DDD6);
120
    /* set output to Vcc, LED off */
123
    /* set output to Vcc, LED off */
121
    PORTD|= (1<<PD6);
124
    PORTD|= (1<<PD6);
122
 
125
 
123
    //printf("Init enc\n");
126
    //printf("Init enc\n");
124
    /*initialize enc28j60*/
127
    /*initialize enc28j60*/
125
    enc28j60Init(mymac);
128
    enc28j60Init(mymac);
126
    delay_ms(20);
129
    delay_ms(20);
127
   
130
   
128
    /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
131
    /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
129
    // LEDB=yellow LEDA=green
132
    // LEDB=yellow LEDA=green
130
    //
133
    //
131
    // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
134
    // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
132
    enc28j60PhyWrite(PHLCON,0x476);
135
    enc28j60PhyWrite(PHLCON,0x476);
133
    delay_ms(20);
136
    delay_ms(20);
134
   
137
   
135
    /* set output to GND, red LED on */
138
    /* set output to GND, red LED on */
136
    PORTD &= ~(1<<PD6);
139
    PORTD &= ~(1<<PD6);
137
    ledi=1;
140
    ledi=1;
138
   
141
   
139
    //init the ethernet/ip layer:
142
    //init the ethernet/ip layer:
140
    init_ip_arp_udp_tcp(mymac,myip,80);
143
    init_ip_arp_udp_tcp(mymac,myip,80);
141
 
144
 
142
    /* main loop */
145
    /* main loop */
143
    while (1) {
146
    while (1) {
144
        /* service the CAN routines */
147
        /* service the CAN routines */
145
        Can_Service();
148
        Can_Service();
146
       
149
       
147
        /* send CAN message and check for CAN errors once every second */
150
        /* send CAN message and check for CAN errors once every second */
148
        if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
151
        if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
149
            timeStamp = Timebase_CurrentTime();
152
            timeStamp = Timebase_CurrentTime();
150
            /* send txMsg */
153
            /* send txMsg */
151
            //txMsg.Id++;
154
            //txMsg.Id++;
152
            //txMsg.Id = 3;
155
            //txMsg.Id = 3;
153
            //Can_Send(&txMsg);
156
            //Can_Send(&txMsg);
154
           
157
           
155
        }
158
        }
156
       
159
       
157
        /* check if any messages have been received */
160
        /* check if any messages have been received */
158
        while (Can_Receive(&rxMsg) == CAN_OK) {
161
        while (Can_Receive(&rxMsg) == CAN_OK) {
159
            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));
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));
160
            printf("data={ ");
163
            printf("data={ ");
161
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
164
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
162
                printf("%x ", rxMsg.Data.bytes[i]);
165
                printf("%x ", rxMsg.Data.bytes[i]);
163
            }
166
            }
164
            printf("}\n");
167
            printf("}\n");
165
           
168
           
166
        }
169
        }
167
       
170
       
168
        //test-debug-kod
171
        //test-debug-kod
169
        if ((enc28j60Read(EIR) & EIR_TXERIF)) {
172
        if ((enc28j60Read(EIR) & EIR_TXERIF)) {
170
            //printf("EIR_TXERIF set!\n");
173
            //printf("EIR_TXERIF set!\n");
171
            if ((enc28j60Read(ECON1) & ECON1_TXRTS)) {
174
            if ((enc28j60Read(ECON1) & ECON1_TXRTS)) {
172
                //printf("ECON1_TXRTS set!\n");
175
                //printf("ECON1_TXRTS set!\n");
173
            }
176
            }
174
        }
177
        }
175
        if ((enc28j60Read(EIR) & EIR_TXIF)) {
178
        if ((enc28j60Read(EIR) & EIR_TXIF)) {
176
            //printf("EIR_TXIF set! ");
179
            //printf("EIR_TXIF set! ");
177
        }
180
        }
178
        if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) {
181
        if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) {
179
            //printf("ESTAT_TXABRT set!\n");
182
            //printf("ESTAT_TXABRT set!\n");
180
            //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1));
183
            //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1));
181
            enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT);
184
            enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT);
182
        }
185
        }
183
       
186
       
184
        // get the next new packet:
187
        // get the next new packet:
185
        plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
188
        plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
186
 
189
 
187
        /*plen will ne unequal to zero if there is a valid
190
        /*plen will ne unequal to zero if there is a valid
188
         * packet (without crc error) */
191
         * packet (without crc error) */
189
        if(plen!=0){
192
        if(plen!=0){
190
            // arp is broadcast if unknown but a host may also
193
            // arp is broadcast if unknown but a host may also
191
            // verify the mac address by sending it to 
194
            // verify the mac address by sending it to 
192
            // a unicast address.
195
            // a unicast address.
193
            if(eth_type_is_arp_and_my_ip(buf,plen)){
196
            if(eth_type_is_arp_and_my_ip(buf,plen)){
194
                    make_arp_answer_from_request(buf);
197
                    make_arp_answer_from_request(buf);
195
            } else {
198
            } else {
196
                // check if ip packets (icmp or udp) are for us:
199
                // check if ip packets (icmp or udp) are for us:
197
                if(eth_type_is_ip_and_my_ip(buf,plen)!=0){
200
                if(eth_type_is_ip_and_my_ip(buf,plen)!=0){
198
                   
201
                   
199
                    if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
202
                    if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
200
                            // a ping packet, let's send pong
203
                            // a ping packet, let's send pong
201
                            make_echo_reply_from_request(buf,plen);
204
                            make_echo_reply_from_request(buf,plen);
202
                            //txMsg.Id = 6;
205
                            //txMsg.Id = 6;
203
                            //Can_Send(&txMsg);
206
                            //Can_Send(&txMsg);
204
                           
207
                           
205
                    }
208
                    }
206
                    if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
209
                    if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
207
                        payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
210
                        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]);
-
 
212
                        if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) {
-
 
213
                            printf("hoj\n");
208
                        if (buf[UDP_DATA_P]=='t' && payloadlen==5){
214
                        } else if (buf[UDP_DATA_P]=='t' && payloadlen==5){
209
                            uint8_t i=0;
215
                            uint8_t i=0;
210
                            if (gotserver == 0) {
216
                            if (gotserver == 0) {
211
                                while(i<6){
217
                                while(i<6){
212
                                    remotemac[i]=buf[ETH_SRC_MAC +i];
218
                                    remotemac[i]=buf[ETH_SRC_MAC +i];
213
                                    i++;
219
                                    i++;
214
                                }
220
                                }
215
                                i=0;
221
                                i=0;
216
                                while(i<4){
222
                                while(i<4){
217
                                    remoteip[i]=buf[IP_SRC_P +i];
223
                                    remoteip[i]=buf[IP_SRC_P +i];
218
                                    i++;
224
                                    i++;
219
                                }
225
                                }
220
                                i=0;
226
                                i=0;
221
                               
227
                               
222
                                gotserver = 1;
228
                                gotserver = 1;
223
                                make_udp_reply_from_request(buf,"Got server",10,remoteport);
229
                                make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt);
224
                            } else {
230
                            } else {
225
                                make_udp_reply_from_request(buf,"Already got server",18,remoteport);
231
                                make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt);
226
                            }
232
                            }
227
                        }
233
                        }
228
                    }                      
234
                    }                      
229
                }
235
                }
230
            }
236
            }
231
        }
237
        }
232
       
238
       
233
 
239
 
234
               
240
               
235
       
241
       
236
    }
242
    }
237
   
243
   
238
    return 0;
244
    return 0;
239
}
245
}
240
 
246