Subversion Repositories HomeAutomation

Rev

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

Rev 427 Rev 451
Line 24... Line 24...
24
#include <stdio.h>
24
#include <stdio.h>
25
/* lib files */
25
/* lib files */
26
#include <can.h>
26
#include <can.h>
27
#include <serial.h>
27
#include <serial.h>
28
#include <timebase.h>
28
#include <timebase.h>
-
 
29
 
-
 
30
//#define UART_OUTPUT /* If you got the node connected to a computer enable this */
-
 
31
//#define LED_OUTPUT /* If you havent got any connection to a computer but still got some IOs free */
29
 
32
 
30
#define SENDING_ID  0x1600000UL // FIXME vilka idn kan vara lämpliga?
33
#define SENDING_ID  0x1600000UL // FIXME vilka idn kan vara lämpliga?
31
#define ECHO_RECEIVE_ID     0x1700000UL
34
#define ECHO_RECEIVE_ID     0x1700000UL
32
#define ECHO_SENDING_ID     0x17000FFUL
35
#define ECHO_SENDING_ID     0x17000FFUL
-
 
36
 
-
 
37
/* LED 1 blinks
-
 
38
#define LED1_PORT   PORTC
-
 
39
#define LED1_DDR    DDRC
-
 
40
#define LED2_PORT   PORTC
-
 
41
#define LED2_DDR    PORTC
33
 
42
 
34
 
43
 
35
 
44
 
36
/*-----------------------------------------------------------------------------
45
/*-----------------------------------------------------------------------------
37
 * Main Program
46
 * Main Program
38
 *---------------------------------------------------------------------------*/
47
 *---------------------------------------------------------------------------*/
39
int main(void) {
48
int main(void) {
40
    Mcu_Init();
49
    Mcu_Init();
41
    Timebase_Init();
50
    Timebase_Init();
-
 
51
    sei();
-
 
52
#if defined(UART_OUTPUT)
42
    Serial_Init();
53
    Serial_Init();
43
 
-
 
44
    sei();
-
 
45
 
54
 
46
    printf("\n------------------------------------------------------------\n");
55
    printf("\n------------------------------------------------------------\n");
47
    printf(  "   CAN Test:\n");
56
    printf(  "   CAN Test:\n");
48
    printf(  "   Periodic Transmission on CAN\n");
57
    printf(  "   Periodic Transmission on CAN\n");
49
    printf(  "   CAN Dump\n");
58
    printf(  "   CAN Dump\n");
Line 56... Line 65...
56
        printf("Check wires between AVR and MCP2515 and the MCP speed (xtal).");
65
        printf("Check wires between AVR and MCP2515 and the MCP speed (xtal).");
57
    }
66
    }
58
    else {
67
    else {
59
        printf("OK!\n");
68
        printf("OK!\n");
60
        printf("MCP2515 working fine\n");
69
        printf("MCP2515 working fine\n");
61
    }
70
    }
-
 
71
#elif defined(LED_OUTPUT)
-
 
72
    Can_Init();
-
 
73
#else
-
 
74
    Can_Init();
-
 
75
#endif
62
   
76
   
63
    uint32_t timeStamp = 0;
77
    uint32_t timeStamp = 0;
64
   
78
   
65
    Can_Message_t txMsg;
79
    Can_Message_t txMsg;
66
    Can_Message_t rxMsg;
80
    Can_Message_t rxMsg;
67
    txMsg.RemoteFlag = 0;
81
    txMsg.RemoteFlag = 0;
Line 73... Line 87...
73
 
87
 
74
    /* main loop */
88
    /* main loop */
75
    while (1) {
89
    while (1) {
76
        /* service the CAN routines */
90
        /* service the CAN routines */
77
        Can_Service();
91
        Can_Service();
78
       
92
       
79
        /* send CAN message and check for CAN errors once every second */
93
        /* send CAN message and check for CAN errors once every second */
80
        if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
94
        if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
81
            timeStamp = Timebase_CurrentTime();
95
            timeStamp = Timebase_CurrentTime();
82
            /* send txMsg */
96
            /* send txMsg */
83
            txMsg.Id = SENDING_ID;
97
            txMsg.Id = SENDING_ID;
84
            Can_Send(&txMsg);
98
            Can_Send(&txMsg);
85
        }
99
        }
86
       
100
       
87
        /* check if any messages have been received */
101
        /* check if any messages have been received */
88
        while (Can_Receive(&rxMsg) == CAN_OK) {
102
        while (Can_Receive(&rxMsg) == CAN_OK) {
-
 
103
#if defined(UART_OUTPUT)
89
            /* Dump message data on uart */
104
            /* Dump message data on uart */
90
            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));
105
            printf("\nPKT %#lx %u %u", rxMsg.Id, (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
91
            printf("data={ ");
-
 
92
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
106
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
93
                printf("%x ", rxMsg.Data.bytes[i]);
107
                printf(" %#x", rxMsg.Data.bytes[i]);
94
            }
108
            }
95
            printf("}\n");
109
#endif
96
 
-
 
97
            /* Echo function */
110
            /* Echo function */
98
            if(rxMsg.Id == ECHO_RECEIVE_ID){
111
            if(rxMsg.Id == ECHO_RECEIVE_ID){
-
 
112
#if defined(UART_OUTPUT)
99
                printf("\"ping\" received\n");
113
                printf("\n\"ping\" received");
-
 
114
                txMsg.Id = ECHO_SENDING_ID;
-
 
115
                /* Send reply */
-
 
116
                Can_Send(&txMsg);
-
 
117
                printf("\nreply sent");
-
 
118
#else
100
                txMsg.Id = ECHO_SENDING_ID;
119
                txMsg.Id = ECHO_SENDING_ID;
101
                /* Send reply */
120
                /* Send reply */
102
                Can_Send(&txMsg);
121
                Can_Send(&txMsg);
103
                printf("reply sent\n");
122
#endif
104
            }
123
            }
105
        }
124
        }
106
    }
125
    }
107
   
126
   
108
    return 0;
127
    return 0;