Subversion Repositories HomeAutomation

Rev

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

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