Subversion Repositories HomeAutomation

Rev

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

Rev 439 Rev 484
1
#include <inttypes.h>
1
#include <inttypes.h>
2
#include <avr/interrupt.h>
2
#include <avr/interrupt.h>
3
#include <stdio.h>
3
#include <stdio.h>
4
#include <string.h>
4
#include <string.h>
5
#include <config.h> // All configuration parameters
5
#include <config.h> // All configuration parameters
6
#include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
6
#include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
7
//#include <drivers/uart/serial.h>
7
//#include <drivers/uart/serial.h>
8
#include <drivers/timer/timebase.h>
8
#include <drivers/timer/timebase.h>
9
#include <drivers/ir/receiver/irreceiver.h>
9
#include <drivers/ir/receiver/irreceiver.h>
10
 
10
 
11
#define APP_TYPE    0xf010
11
#define APP_TYPE    0xf010
12
#define APP_VERSION 0x0001
12
#define APP_VERSION 0x0001
13
 
13
 
14
#define CAN_IR 0x12
14
#define CAN_IR 0x12
15
 
15
 
16
#define STATE_IDLE      0
16
#define STATE_IDLE      0
17
#define STATE_IR_REPEAT 1
17
#define STATE_IR_REPEAT 1
18
 
18
 
19
 
19
 
20
// A simple message "queue", with space for one message only.
20
// A simple message "queue", with space for one message only.
21
// These are declared volatile to tell the compiler not to optimize away accesses.
21
// These are declared volatile to tell the compiler not to optimize away accesses.
22
volatile Can_Message_t rxMsg; // Message storage
22
volatile Can_Message_t rxMsg; // Message storage
23
volatile uint8_t rxMsgFull;   // Synchronization flag
23
volatile uint8_t rxMsgFull;   // Synchronization flag
24
 
24
 
25
// CAN message reception callback.
25
// CAN message reception callback.
26
// This function runs with interrupts disabled, keep it as short as possible.
26
// This function runs with interrupts disabled, keep it as short as possible.
27
void can_receive(Can_Message_t *msg) {
27
void can_receive(Can_Message_t *msg) {
28
    if (!rxMsgFull) {
28
    if (!rxMsgFull) {
29
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
29
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
30
        rxMsgFull = 1;
30
        rxMsgFull = 1;
31
    }
31
    }
32
}
32
}
33
 
33
 
34
int main(void)
34
int main(void)
35
{
35
{
36
    // Enable interrupts as early as possible
36
    // Enable interrupts as early as possible
37
    sei();
37
    sei();
38
   
38
   
39
    Timebase_Init();
39
    Timebase_Init();
40
    //Serial_Init();
40
    //Serial_Init();
41
   
41
   
42
    Can_Message_t txMsg;
42
    Can_Message_t txMsg;
43
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
43
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
44
    txMsg.DataLength = 4;
44
    txMsg.DataLength = 4;
45
    txMsg.RemoteFlag = 0;
45
    txMsg.RemoteFlag = 0;
46
    txMsg.ExtendedFlag = 1;
46
    txMsg.ExtendedFlag = 1;
47
    txMsg.Data.words[0] = APP_TYPE;
47
    txMsg.Data.words[0] = APP_TYPE;
48
    txMsg.Data.words[1] = APP_VERSION;
48
    txMsg.Data.words[1] = APP_VERSION;
49
   
49
   
50
    // Set up callback for CAN reception, this is optional if only sending is required.
50
    // Set up callback for CAN reception, this is optional if only sending is required.
51
    BIOS_CanCallback = &can_receive;
51
    BIOS_CanCallback = &can_receive;
52
    // Send CAN_NMT_APP_START
52
    // Send CAN_NMT_APP_START
53
    BIOS_CanSend(&txMsg);
53
    BIOS_CanSend(&txMsg);
54
 
54
 
55
    txMsg.Id = CAN_ID_SNS_IRDATA;   // ((CAN_SNS << CAN_SHIFT_CLASS) | (NODE_ID << CAN_SHIFT_SNS_SID))
55
    txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_FUNCC_CAN_IR << CAN_SHIFT_SNS_FUNCC) | (NODE_ID << CAN_SHIFT_SNS_SID));
56
   
56
       
57
    //printf("AVR Test Application\n");
57
    //printf("AVR Test Application\n");
58
    //printf("Using AVR BIOS version %x\n", BIOS_VERSION);
58
    //printf("Using AVR BIOS version %x\n", BIOS_VERSION);
59
 
59
 
60
    //txMsg.Id = (CAN_SNS << CAN_SHIFT_CLASS) | ((CAN_IR & CAN_MASK_SNS_FUNCC) << CAN_SHIFT_SNS_FUNCC) | (NODE_ID << CAN_SHIFT_SNS_SID);
60
    //txMsg.Id = (CAN_SNS << CAN_SHIFT_CLASS) | ((CAN_IR & CAN_MASK_SNS_FUNCC) << CAN_SHIFT_SNS_FUNCC) | (NODE_ID << CAN_SHIFT_SNS_SID);
61
    //txMsg.Data.dwords[0] = 0x01020304;
61
    //txMsg.Data.dwords[0] = 0x01020304;
62
    //txMsg.DataLength = 4;
62
    //txMsg.DataLength = 4;
63
 
63
 
64
    IRDDR &= ~(1<<IRBIT);
64
    IRDDR &= ~(1<<IRBIT);
65
 
65
 
66
    unsigned long time = Timebase_CurrentTime();
66
    unsigned long time = Timebase_CurrentTime();
67
    unsigned long irTimeoutTime;
67
    unsigned long irTimeoutTime;
68
   
68
   
69
    uint8_t ir_protocol, ir_address, ir_command;
69
    uint8_t ir_protocol, ir_address, ir_command;
70
    uint8_t state = STATE_IDLE;
70
    uint8_t state = STATE_IDLE;
71
 
71
 
72
    while (1) {
72
    while (1) {
73
        time = Timebase_CurrentTime();
73
        time = Timebase_CurrentTime();
74
        if (state == STATE_IDLE) {
74
        if (state == STATE_IDLE) {
75
            if (checkIr(&ir_protocol, &ir_address, &ir_command) == IR_OK) {
75
            if (checkIr(&ir_protocol, &ir_address, &ir_command) == IR_OK) {
76
                txMsg.Data.bytes[0] = 0x00;
76
                txMsg.Data.bytes[0] = 0x00;
77
                txMsg.Data.bytes[1] = ir_protocol;
77
                txMsg.Data.bytes[1] = ir_protocol;
78
                txMsg.Data.bytes[2] = ir_address;
78
                txMsg.Data.bytes[2] = ir_address;
79
                txMsg.Data.bytes[3] = ir_command;
79
                txMsg.Data.bytes[3] = ir_command;
80
                txMsg.DataLength = 4;
80
                txMsg.DataLength = 4;
81
                BIOS_CanSend(&txMsg);
81
                BIOS_CanSend(&txMsg);
82
                irTimeoutTime = Timebase_CurrentTime();
82
                irTimeoutTime = Timebase_CurrentTime();
83
                state = STATE_IR_REPEAT;
83
                state = STATE_IR_REPEAT;
84
            }
84
            }
85
        } else if (state == STATE_IR_REPEAT) {
85
        } else if (state == STATE_IR_REPEAT) {
86
            //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE
86
            //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE
87
            if (time - irTimeoutTime >= 105) {      //getLastProtoTimeout()
87
            if (time - irTimeoutTime >= 105) {      //getLastProtoTimeout()
88
                state = STATE_IDLE;
88
                state = STATE_IDLE;
89
                txMsg.Data.bytes[0] = 0x0f;
89
                txMsg.Data.bytes[0] = 0x0f;
90
                BIOS_CanSend(&txMsg);
90
                BIOS_CanSend(&txMsg);
91
            }
91
            }
92
            if (checkIrIdle() == IR_OK) {       //funktion som borde finnas i irreceiver.c
92
            if (checkIrIdle() == IR_OK) {       //funktion som borde finnas i irreceiver.c
93
                irTimeoutTime = Timebase_CurrentTime();
93
                irTimeoutTime = Timebase_CurrentTime();
94
            }
94
            }
95
        }
95
        }
96
 
96
 
97
       
97
       
98
        if (rxMsgFull) {
98
        if (rxMsgFull) {
99
            // Flush the received message
99
            // Flush the received message
100
            rxMsgFull = 0; //  
100
            rxMsgFull = 0; //  
101
        }
101
        }
102
    }
102
    }
103
   
103
   
104
    return 0;
104
    return 0;
105
}
105
}
106
 
106