Subversion Repositories HomeAutomation

Rev

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

Rev 168 Rev 200
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
#define STATE_IDLE      0
24
#define STATE_IDLE      0
25
#define STATE_IR_REPEAT 1
25
#define STATE_IR_REPEAT 1
26
 
26
 
27
/*-----------------------------------------------------------------------------
27
/*-----------------------------------------------------------------------------
28
 * Main Program
28
 * Main Program
29
 *---------------------------------------------------------------------------*/
29
 *---------------------------------------------------------------------------*/
30
int main(void) {
30
int main(void) {
31
    Timebase_Init();
31
    Timebase_Init();
32
    Serial_Init();
32
    Serial_Init();
33
    sei();
33
    sei();
-
 
34
   
-
 
35
    #if defined(__AVR_ATmega88__)
-
 
36
    // set the clock speed to 8MHz
-
 
37
    // set the clock prescaler. First write CLKPCE to enable setting of clock the
-
 
38
    // next four instructions.
-
 
39
    CLKPR=(1<<CLKPCE);
-
 
40
    CLKPR=0; // 8 MHZ
-
 
41
    #endif
34
   
42
   
35
    IRDDR &= ~(1<<IRBIT);
43
    IRDDR &= ~(1<<IRBIT);
36
   
44
   
37
    printf("\n------------------------------------------------------------\n");
45
    printf("\n------------------------------------------------------------\n");
38
    printf(  "   CAN Test: Periodic Transmission\n");
46
    printf(  "   CAN Test: Periodic Transmission\n");
39
    printf(  "------------------------------------------------------------\n");
47
    printf(  "------------------------------------------------------------\n");
40
   
48
   
41
    printf("CanInit...");
49
    printf("CanInit...");
42
    if (Can_Init() != CAN_OK) {
50
    if (Can_Init() != CAN_OK) {
43
        printf("FAILED!\n");
51
        printf("FAILED!\n");
44
    }
52
    }
45
    else {
53
    else {
46
        printf("OK!\n");
54
        printf("OK!\n");
47
    }
55
    }
48
   
56
   
49
    uint32_t timeStamp = 0;
57
    uint32_t timeStamp = 0;
50
    uint32_t irTimeoutTimeStamp = 0;
58
    uint32_t irTimeoutTimeStamp = 0;
51
   
59
   
52
    Can_Message_t txMsg;
60
    Can_Message_t txMsg;
53
    Can_Message_t rxMsg;
61
    Can_Message_t rxMsg;
54
    txMsg.Id = 1;
62
    txMsg.Id = 1;
55
   
63
   
56
    uint8_t ir_protocol, ir_address, ir_command;
64
    uint8_t ir_protocol, ir_address, ir_command;
57
    uint8_t state = STATE_IDLE;
65
    uint8_t state = STATE_IDLE;
58
   
66
   
59
    /* main loop */
67
    /* main loop */
60
    while (1) {
68
    while (1) {
61
        /* service the CAN routines */
69
        /* service the CAN routines */
62
        Can_Service();
70
        Can_Service();
63
       
71
       
64
        if (state == STATE_IDLE) {
72
        if (state == STATE_IDLE) {
65
            if (checkIr(&ir_protocol, &ir_address, &ir_command) == IR_OK) {
73
            if (checkIr(&ir_protocol, &ir_address, &ir_command) == IR_OK) {
66
                txMsg.Data.bytes[0] = 0x00;
74
                txMsg.Data.bytes[0] = 0x00;
67
                txMsg.Data.bytes[1] = ir_protocol;
75
                txMsg.Data.bytes[1] = ir_protocol;
68
                txMsg.Data.bytes[2] = ir_address;
76
                txMsg.Data.bytes[2] = ir_address;
69
                txMsg.Data.bytes[3] = ir_command;
77
                txMsg.Data.bytes[3] = ir_command;
70
                txMsg.DataLength = 4;
78
                txMsg.DataLength = 4;
71
                Can_Send(&txMsg);
79
                Can_Send(&txMsg);
72
                irTimeoutTimeStamp = Timebase_CurrentTime();
80
                irTimeoutTimeStamp = Timebase_CurrentTime();
73
                state = STATE_IR_REPEAT;
81
                state = STATE_IR_REPEAT;
74
            }
82
            }
75
        } else if (state == STATE_IR_REPEAT) {
83
        } else if (state == STATE_IR_REPEAT) {
76
            //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE
84
            //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE
77
            if (Timebase_PassedTimeMillis(irTimeoutTimeStamp) >= 105) {     //getLastProtoTimeout()
85
            if (Timebase_PassedTimeMillis(irTimeoutTimeStamp) >= 105) {     //getLastProtoTimeout()
78
                state = STATE_IDLE;
86
                state = STATE_IDLE;
79
                txMsg.Data.bytes[0] = 0x0f;
87
                txMsg.Data.bytes[0] = 0x0f;
80
                Can_Send(&txMsg);
88
                Can_Send(&txMsg);
81
            }
89
            }
82
            if (!(IRPIN & (1<<IRBIT))) {        //funktion som borde finnas i irreceiver.c
90
            if (checkIrIdle() == IR_OK) {       //funktion som borde finnas i irreceiver.c
83
                irTimeoutTimeStamp = Timebase_CurrentTime();
91
                irTimeoutTimeStamp = Timebase_CurrentTime();
84
            }
92
            }
85
        }
93
        }
86
       
94
       
87
   
95
   
88
        /* check if any messages have been received */
96
        /* check if any messages have been received */
89
        while (Can_Receive(&rxMsg) == CAN_OK) {
97
        while (Can_Receive(&rxMsg) == CAN_OK) {
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));
98
            /*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));
91
            printf("data={ ");
99
            printf("data={ ");
92
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
100
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
93
                printf("%x ", rxMsg.Data.bytes[i]);
101
                printf("%x ", rxMsg.Data.bytes[i]);
94
            }
102
            }
95
            printf("}\n");*/
103
            printf("}\n");*/
96
        }
104
        }
97
    }
105
    }
98
   
106
   
99
    return 0;
107
    return 0;
100
}
108
}
101
 
109