Subversion Repositories HomeAutomation

Rev

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

Rev 159 Rev 168
1
/**
1
/**
2
 * CAN Test. This program sends a CAN message once every second. The ID of the
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.
3
 * message is increased each time for testing purposes.
4
 *
4
 *
5
 * @date    2006-11-21
5
 * @date    2006-11-21
6
 * @author  Jimmy Myhrman
6
 * @author  Jimmy Myhrman
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 <irreceiver.h>
22
#include <irreceiver.h>
23
 
23
 
24
 
-
 
-
 
24
#define STATE_IDLE      0
-
 
25
#define STATE_IR_REPEAT 1
25
 
26
 
26
/*-----------------------------------------------------------------------------
27
/*-----------------------------------------------------------------------------
27
 * Main Program
28
 * Main Program
28
 *---------------------------------------------------------------------------*/
29
 *---------------------------------------------------------------------------*/
29
int main(void) {
30
int main(void) {
30
    Timebase_Init();
31
    Timebase_Init();
31
    Serial_Init();
32
    Serial_Init();
32
    sei();
33
    sei();
33
   
-
 
34
    DDRD |= (1<<PD5);
-
 
35
    PORTD &= ~(1<<PD5);
-
 
36
 
34
   
37
    IRDDR &= ~(1<<IRBIT);
35
    IRDDR &= ~(1<<IRBIT);
38
   
36
   
39
    printf("\n------------------------------------------------------------\n");
37
    printf("\n------------------------------------------------------------\n");
40
    printf(  "   CAN Test: Periodic Transmission\n");
38
    printf(  "   CAN Test: Periodic Transmission\n");
41
    printf(  "------------------------------------------------------------\n");
39
    printf(  "------------------------------------------------------------\n");
42
   
40
   
43
    printf("CanInit...");
41
    printf("CanInit...");
44
    if (Can_Init() != CAN_OK) {
42
    if (Can_Init() != CAN_OK) {
45
        printf("FAILED!\n");
43
        printf("FAILED!\n");
46
    }
44
    }
47
    else {
45
    else {
48
        printf("OK!\n");
46
        printf("OK!\n");
49
    }
47
    }
50
   
48
   
51
    uint32_t timeStamp = 0;
49
    uint32_t timeStamp = 0;
-
 
50
    uint32_t irTimeoutTimeStamp = 0;
52
   
51
   
53
    Can_Message_t txMsg;
52
    Can_Message_t txMsg;
54
    Can_Message_t rxMsg;
53
    Can_Message_t rxMsg;
55
    txMsg.Id = 2;
54
    txMsg.Id = 1;
56
   
55
   
57
    uint8_t ir_protocol, ir_address, ir_command;
56
    uint8_t ir_protocol, ir_address, ir_command;
-
 
57
    uint8_t state = STATE_IDLE;
58
   
58
   
59
    /* main loop */
59
    /* main loop */
60
    while (1) {
60
    while (1) {
61
        /* service the CAN routines */
61
        /* service the CAN routines */
62
        Can_Service();
62
        Can_Service();
63
       
63
       
64
        if (IRPIN & (1<<IRBIT)) {
64
        if (state == STATE_IDLE) {
65
            PORTD |= (1<<PD5);
-
 
66
        } else {
-
 
67
            PORTD &= ~(1<<PD5);
-
 
68
        }
-
 
69
       
-
 
70
        if (checkIr(&ir_protocol, &ir_address, &ir_command) == IR_OK) {
65
            if (checkIr(&ir_protocol, &ir_address, &ir_command) == IR_OK) {
71
            txMsg.Data.bytes[0] = 0x00;
66
                txMsg.Data.bytes[0] = 0x00;
72
            txMsg.Data.bytes[1] = ir_protocol;
67
                txMsg.Data.bytes[1] = ir_protocol;
73
            txMsg.Data.bytes[2] = ir_address;
68
                txMsg.Data.bytes[2] = ir_address;
74
            txMsg.Data.bytes[3] = ir_command;
69
                txMsg.Data.bytes[3] = ir_command;
75
            txMsg.DataLength = 4;
70
                txMsg.DataLength = 4;
-
 
71
                Can_Send(&txMsg);
-
 
72
                irTimeoutTimeStamp = Timebase_CurrentTime();
-
 
73
                state = STATE_IR_REPEAT;
-
 
74
            }
-
 
75
        } else if (state == STATE_IR_REPEAT) {
-
 
76
            //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE
-
 
77
            if (Timebase_PassedTimeMillis(irTimeoutTimeStamp) >= 105) {     //getLastProtoTimeout()
-
 
78
                state = STATE_IDLE;
-
 
79
                txMsg.Data.bytes[0] = 0x0f;
76
            Can_Send(&txMsg);
80
                Can_Send(&txMsg);
77
        }
81
            }
78
       
-
 
79
        /* send CAN message and check for CAN errors once every second */
82
            if (!(IRPIN & (1<<IRBIT))) {        //funktion som borde finnas i irreceiver.c
80
        if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
-
 
81
            timeStamp = Timebase_CurrentTime();
83
                irTimeoutTimeStamp = Timebase_CurrentTime();
82
            txMsg.DataLength = 0;
-
 
83
            /* send txMsg */
-
 
84
            Can_Send(&txMsg);
-
 
85
        }
84
            }
-
 
85
        }
-
 
86
       
86
       
87
   
87
        /* check if any messages have been received */
88
        /* check if any messages have been received */
88
        while (Can_Receive(&rxMsg) == CAN_OK) {
89
        while (Can_Receive(&rxMsg) == CAN_OK) {
89
            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));
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));
90
            printf("data={ ");
91
            printf("data={ ");
91
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
92
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
92
                printf("%x ", rxMsg.Data.bytes[i]);
93
                printf("%x ", rxMsg.Data.bytes[i]);
93
            }
94
            }
94
            printf("}\n");
95
            printf("}\n");*/
95
        }
96
        }
96
    }
97
    }
97
   
98
   
98
    return 0;
99
    return 0;
99
}
100
}
100
 
101