Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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