Subversion Repositories HomeAutomation

Rev

Rev 1003 | Rev 1686 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1003 Rev 1115
Line 15... Line 15...
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
#include <string.h>
18
#include <string.h>
19
 
19
 
-
 
20
#include <canid.h>
-
 
21
#include <config.h>
20
/* lib files */
22
/* lib files */
-
 
23
#if USE_STDCAN == 1
-
 
24
#include <stdcan.h>
-
 
25
#else
21
#include <mcp2515.h>
26
#include <mcp2515.h>
-
 
27
#endif
22
//#include <serial.h>
28
//#include <serial.h>
23
#include <timebase.h>
29
#include <timebase.h>
24
#include <mcu.h>
30
#include <mcu.h>
25
#include <ip_arp_udp_tcp.h>
31
#include <ip_arp_udp_tcp.h>
26
#include <enc28j60.h>
32
#include <enc28j60.h>
Line 53... Line 59...
53
static uint8_t buffpoint;
59
static uint8_t buffpoint;
54
 
60
 
55
#define C2S_START_BYTE 253
61
#define C2S_START_BYTE 253
56
#define C2S_END_BYTE 250
62
#define C2S_END_BYTE 250
57
 
63
 
-
 
64
#if USE_STDCAN == 0
58
volatile Can_Message_t rxMsg; // Message storage
65
volatile Can_Message_t rxMsg; // Message storage
59
volatile uint8_t rxMsgFull;   // Synchronization flag
66
volatile uint8_t rxMsgFull;   // Synchronization flag
-
 
67
#endif
60
 
68
 
61
/*----------------------------------------------------------------------------
69
/*----------------------------------------------------------------------------
62
 * Putchar for udp
70
 * Putchar for udp
63
 * Implements a small buffer, sends packet over udp if buffer is full or
71
 * Implements a small buffer, sends packet over udp if buffer is full or
64
 * \n-char is sent.
72
 * \n-char is sent.
Line 78... Line 86...
78
}
86
}
79
 
87
 
80
static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE);
88
static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE);
81
*/
89
*/
82
 
90
 
-
 
91
#if USE_STDCAN == 0
83
void Can_Process(Can_Message_t* msg) {
92
void Can_Process(Can_Message_t* msg) {
84
    if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames.
93
    if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames.
85
 
94
 
86
    if (!rxMsgFull) {
95
    if (!rxMsgFull) {
87
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
96
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
88
        rxMsgFull = 1;
97
        rxMsgFull = 1;
89
    }
98
    }
90
}
99
}
-
 
100
#endif
91
 
101
 
92
/*-----------------------------------------------------------------------------
102
/*-----------------------------------------------------------------------------
93
 * Main Program
103
 * Main Program
94
 *---------------------------------------------------------------------------*/
104
 *---------------------------------------------------------------------------*/
95
int main(void) {
105
int main(void) {
Line 97... Line 107...
97
#if MCP_CS_BIT != PB2
107
#if MCP_CS_BIT != PB2
98
    /* If slave select is not set as output it might change SPI hw to slave
108
    /* If slave select is not set as output it might change SPI hw to slave
99
     * See ch 18.3.2 (18.3 SS Pin Functionality) in ATmega48/88/168-datasheet */
109
     * See ch 18.3.2 (18.3 SS Pin Functionality) in ATmega48/88/168-datasheet */
100
    DDRB |= (1<<PB2);
110
    DDRB |= (1<<PB2);
101
#endif
111
#endif
102
 
112
 
103
 
113
 
104
    //ethernet vars
114
    //ethernet vars
105
    uint16_t plen = 0;
115
    uint16_t plen = 0;
106
    uint8_t payloadlen=0;
116
    uint8_t payloadlen=0;
107
    buffpoint = 0;
117
    buffpoint = 0;
108
   
118
   
109
    //can vars
119
    //can vars
110
    Can_Message_t txMsg;
-
 
111
    txMsg.Id = 3;
-
 
112
    txMsg.DataLength = 0;
-
 
113
    txMsg.RemoteFlag = 0;
-
 
114
    txMsg.ExtendedFlag = 1;
-
 
115
 
-
 
116
    rxMsgFull = 0;
-
 
117
   
120
   
118
    uint32_t timeStamp = 0;
121
    uint32_t timeStamp = 0;
119
 
122
 
120
    //uint8_t sw=0;
123
    //uint8_t sw=0;
121
    // LED
124
    // LED
Line 129... Line 132...
129
    Timebase_Init();
132
    Timebase_Init();
130
 
133
 
131
    //alla printf ska skriva till udp
134
    //alla printf ska skriva till udp
132
    //stdout = &mystdout;    //set the output stream 
135
    //stdout = &mystdout;    //set the output stream 
133
 
136
 
-
 
137
#if USE_STDCAN == 0
134
    if (Can_Init() != CAN_OK) {
138
    Can_Init();
-
 
139
    Can_Message_t txMsg;
-
 
140
    txMsg.RemoteFlag = 0;
-
 
141
    txMsg.ExtendedFlag = 1;
-
 
142
    rxMsgFull = 0;
135
    }
143
#else
-
 
144
    StdCan_Init(0);
-
 
145
    StdCan_Msg_t txMsg;
-
 
146
    StdCan_Msg_t rxMsg;
-
 
147
#endif
136
 
148
 
137
    sei();
149
    sei();
138
 
150
 
139
    /*initialize enc28j60*/
151
    /*initialize enc28j60*/
140
    if (enc28j60Init(mymac) != 1) {
152
    if (enc28j60Init(mymac) != 1) {
Line 159... Line 171...
159
       
171
       
160
#if SENDTIMESTAMP == 1
172
#if SENDTIMESTAMP == 1
161
        /* send CAN message and check for CAN errors once every second */
173
        /* send CAN message and check for CAN errors once every second */
162
        if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
174
        if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
163
            timeStamp = Timebase_CurrentTime();
175
            timeStamp = Timebase_CurrentTime();
-
 
176
 
164
            /* send timestamp, to keep network alive */
177
            /* send timestamp, to keep network alive */
-
 
178
            txMsg.Id = (CAN_NMT << CAN_SHIFT_CLASS) | (CAN_NMT_TIME << CAN_SHIFT_NMT_TYPE);
165
            txMsg.Id = 0;
179
#if USE_STDCAN == 0
166
            txMsg.DataLength = 0;
180
            txMsg.DataLength = 0;
167
            Can_Send(&txMsg);
181
            Can_Send(&txMsg);
-
 
182
#else
-
 
183
            txMsg.Length = 0;
-
 
184
            StdCan_Put(&txMsg);
-
 
185
#endif
168
           
186
           
169
        }
187
        }
170
#endif
188
#endif
171
       
189
       
172
        /* check if any messages have been received */
190
        /* check if any messages have been received */
-
 
191
#if USE_STDCAN == 0
-
 
192
        if (rxMsgFull) {
-
 
193
#else
173
        if (rxMsgFull) { //(Can_Receive(&rxMsg) == CAN_OK) {
194
        if (StdCan_Get(&rxMsg) == StdCan_Ret_OK) {
-
 
195
#endif
174
            uint8_t i;
196
            uint8_t i;
175
           
197
           
176
            /*if (gotdumpserver != 0) {
198
            /*if (gotdumpserver != 0) {
177
                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));
199
                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));
178
                printf("data={ ");
200
                printf("data={ ");
Line 189... Line 211...
189
                sendbuf2[0] = C2S_START_BYTE;
211
                sendbuf2[0] = C2S_START_BYTE;
190
                sendbuf2[1] = (uint8_t)rxMsg.Id;
212
                sendbuf2[1] = (uint8_t)rxMsg.Id;
191
                sendbuf2[2] = (uint8_t)(rxMsg.Id>>8);
213
                sendbuf2[2] = (uint8_t)(rxMsg.Id>>8);
192
                sendbuf2[3] = (uint8_t)(rxMsg.Id>>16);
214
                sendbuf2[3] = (uint8_t)(rxMsg.Id>>16);
193
                sendbuf2[4] = (uint8_t)(rxMsg.Id>>24);
215
                sendbuf2[4] = (uint8_t)(rxMsg.Id>>24);
-
 
216
#if USE_STDCAN == 0
194
                sendbuf2[5] = rxMsg.ExtendedFlag;
217
                sendbuf2[5] = rxMsg.ExtendedFlag;
195
                sendbuf2[6] = rxMsg.RemoteFlag;
218
                sendbuf2[6] = rxMsg.RemoteFlag;
196
                sendbuf2[7] = rxMsg.DataLength;
219
                sendbuf2[7] = rxMsg.DataLength;
197
                for (i=0; i<8; i++) {
220
                for (i=0; i<8; i++) {
198
                    sendbuf2[8+i] = rxMsg.Data.bytes[i];
221
                    sendbuf2[8+i] = rxMsg.Data.bytes[i];
199
                }
222
                }
-
 
223
#else
-
 
224
                sendbuf2[5] = 1;
-
 
225
                sendbuf2[6] = 0;
-
 
226
                sendbuf2[7] = rxMsg.Length;
-
 
227
                for (i=0; i<8; i++) {
-
 
228
                    sendbuf2[8+i] = rxMsg.Data[i];
-
 
229
                }
-
 
230
#endif
200
                sendbuf2[16] = C2S_END_BYTE;
231
                sendbuf2[16] = C2S_END_BYTE;
201
               
232
               
-
 
233
#ifndef ENC28J60_USART_SPI_MODE 
202
                MCP_INT_DISABLE();
234
                //MCP_INT_DISABLE();
-
 
235
                cli();
-
 
236
#endif
203
                send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip);
237
                send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip);
-
 
238
#ifndef ENC28J60_USART_SPI_MODE 
-
 
239
                sei();
204
                MCP_INT_ENABLE();
240
                //MCP_INT_ENABLE();
-
 
241
#endif
205
            }
242
            }
206
           
243
           
-
 
244
#if USE_STDCAN == 0
207
            rxMsgFull = 0;
245
            rxMsgFull = 0;
-
 
246
#endif
208
        }
247
        }
209
       
248
       
210
        //test-debug-kod
249
        //test-debug-kod
211
        if ((enc28j60Read(EIR) & EIR_TXERIF)) {
250
        if ((enc28j60Read(EIR) & EIR_TXERIF)) {
212
            //printf("EIR_TXERIF set!\n");
251
            //printf("EIR_TXERIF set!\n");
213
            if ((enc28j60Read(ECON1) & ECON1_TXRTS)) {
252
            if ((enc28j60Read(ECON1) & ECON1_TXRTS)) {
214
                //printf("ECON1_TXRTS set!\n");
253
                //printf("ECON1_TXRTS set!\n");
215
            }
254
            }
216
        }
255
        }
217
        if ((enc28j60Read(EIR) & EIR_TXIF)) {
256
        if ((enc28j60Read(EIR) & EIR_TXIF)) {
218
            //printf("EIR_TXIF set! ");
257
            //printf("EIR_TXIF set! ");
219
        }
258
        }
220
        if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) {
259
        if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) {
221
            //printf("ESTAT_TXABRT set!\n");
260
            //printf("ESTAT_TXABRT set!\n");
Line 223... Line 262...
223
            enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT);
262
            enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT);
224
        }
263
        }
225
       
264
       
226
        // get the next new packet:
265
        // get the next new packet:
227
        plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
266
        plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
228
 
267
 
229
        /*plen will ne unequal to zero if there is a valid
268
        /*plen will ne unequal to zero if there is a valid
230
         * packet (without crc error) */
269
         * packet (without crc error) */
231
        if(plen!=0){
270
        if(plen!=0){
232
            // arp is broadcast if unknown but a host may also
271
            // arp is broadcast if unknown but a host may also
233
            // verify the mac address by sending it to 
272
            // verify the mac address by sending it to 
Line 241... Line 280...
241
                    if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
280
                    if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
242
                            // a ping packet, let's send pong
281
                            // a ping packet, let's send pong
243
                            make_echo_reply_from_request(buf,plen);
282
                            make_echo_reply_from_request(buf,plen);
244
                            //txMsg.Id = 6;
283
                            //txMsg.Id = 6;
245
                            //Can_Send(&txMsg);
284
                            //Can_Send(&txMsg);
246
                           
285
                           
247
                    }
286
                    }
248
                    if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
287
                    if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
249
                        payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
288
                        payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
250
                        uint8_t i = 0;
289
                        uint8_t i = 0;
251
                       
290
                       
Line 257... Line 296...
257
                            }
296
                            }
258
                            i=0;
297
                            i=0;
259
                            while(i<4){
298
                            while(i<4){
260
                                prgremoteip[i]=buf[IP_SRC_P +i];
299
                                prgremoteip[i]=buf[IP_SRC_P +i];
261
                                i++;
300
                                i++;
262
                            }
301
                            }
263
                            gotcan2serserver = 1;
302
                            gotcan2serserver = 1;
264
 
303
 
265
                            if ((buf[UDP_DATA_P]==C2S_START_BYTE) && (buf[UDP_DATA_P+16]==C2S_END_BYTE) && (payloadlen==17)) {
304
                            if ((buf[UDP_DATA_P]==C2S_START_BYTE) && (buf[UDP_DATA_P+16]==C2S_END_BYTE) && (payloadlen==17)) {
-
 
305
#if USE_STDCAN == 0
266
                                Can_Message_t cm;
306
                                Can_Message_t cm;
267
                                //cm.Id = 0;
307
                                //cm.Id = 0;
268
                                cm.DataLength = buf[UDP_DATA_P+7];
308
                                cm.DataLength = buf[UDP_DATA_P+7];
269
                                cm.RemoteFlag = buf[UDP_DATA_P+6];
309
                                cm.RemoteFlag = buf[UDP_DATA_P+6];
270
                                cm.ExtendedFlag = buf[UDP_DATA_P+5];
310
                                cm.ExtendedFlag = buf[UDP_DATA_P+5];
Line 272... Line 312...
272
                                uint8_t i;
312
                                uint8_t i;
273
                                for (i = 0; i < cm.DataLength; i++) {
313
                                for (i = 0; i < cm.DataLength; i++) {
274
                                    cm.Data.bytes[i] = buf[UDP_DATA_P+8+i];
314
                                    cm.Data.bytes[i] = buf[UDP_DATA_P+8+i];
275
                                }
315
                                }
276
                                Can_Send(&cm);
316
                                Can_Send(&cm);
-
 
317
#else
-
 
318
                                static StdCan_Msg_t cm;
-
 
319
                                //cm.Id = 0;
-
 
320
                                cm.Length = buf[UDP_DATA_P+7];
-
 
321
                                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);
-
 
322
                                uint8_t i;
-
 
323
                                for (i = 0; i < cm.Length; i++) {
-
 
324
                                    cm.Data[i] = buf[UDP_DATA_P+8+i];
277
                               
325
                                }
-
 
326
                                StdCan_Put(&cm);
-
 
327
#endif
278
                                /*if (gotdumpserver != 0) {
328
                                /*if (gotdumpserver != 0) {
279
                                    printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", cm.Id, (uint16_t)(cm.DataLength), (uint16_t)(cm.ExtendedFlag), (uint16_t)(cm.RemoteFlag));
329
                                    printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", cm.Id, (uint16_t)(cm.DataLength), (uint16_t)(cm.ExtendedFlag), (uint16_t)(cm.RemoteFlag));
280
                                    printf("data={ ");
330
                                    printf("data={ ");
281
                                    for (i=0; i<cm.DataLength; i++) {
331
                                    for (i=0; i<cm.DataLength; i++) {
282
                                        printf("%x ", cm.Data.bytes[i]);
332
                                        printf("%x ", cm.Data.bytes[i]);