Subversion Repositories HomeAutomation

Rev

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

Rev 549 Rev 603
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/transceiver/irtransceiver.h>
9
#include <drivers/ir/receiver/irreceiver.h>
10
//#include <drivers/ir/receiver/irreceiver.h>
10
 
11
 
11
#define APP_TYPE    CAN_APPTYPES_IRRECEIVER
12
#define APP_TYPE    CAN_APPTYPES_IRRECEIVER
12
#define APP_VERSION 0x0003
13
#define APP_VERSION 0x0003
13
 
14
 
14
#define STATE_IDLE      0
15
#define STATE_IDLE      0
15
#define STATE_IR_REPEAT 1
16
#define STATE_IR_REPEAT 1
16
 
17
 
17
#define IR_ID_DATA  0
18
#define IR_ID_DATA  0
18
#define IR_ID_RAW   1
19
#define IR_ID_RAW   1
19
#define IR_ID_DEBUG 10
20
#define IR_ID_DEBUG 10
20
 
21
 
21
// A simple message "queue", with space for one message only.
22
// A simple message "queue", with space for one message only.
22
// These are declared volatile to tell the compiler not to optimize away accesses.
23
// These are declared volatile to tell the compiler not to optimize away accesses.
23
volatile Can_Message_t rxMsg; // Message storage
24
volatile Can_Message_t rxMsg; // Message storage
24
volatile uint8_t rxMsgFull;   // Synchronization flag
25
volatile uint8_t rxMsgFull;   // Synchronization flag
25
 
26
 
26
// CAN message reception callback.
27
// CAN message reception callback.
27
// This function runs with interrupts disabled, keep it as short as possible.
28
// This function runs with interrupts disabled, keep it as short as possible.
28
void can_receive(Can_Message_t *msg) {
29
void can_receive(Can_Message_t *msg) {
29
    if (!rxMsgFull) {
30
    if (!rxMsgFull) {
30
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
31
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
31
        rxMsgFull = 1;
32
        rxMsgFull = 1;
32
    }
33
    }
33
}
34
}
34
 
35
 
35
int main(void)
36
int main(void)
36
{
37
{
37
    // Enable interrupts as early as possible
38
    // Enable interrupts as early as possible
38
    sei();
39
    sei();
39
   
40
   
40
    Timebase_Init();
41
    Timebase_Init();
41
    //Serial_Init();
42
    //Serial_Init();
42
    IrReceive_Init();
43
    IrReceive_Init();
43
   
44
   
44
    Can_Message_t txMsg;
45
    Can_Message_t txMsg;
45
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
46
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
46
    txMsg.DataLength = 4;
47
    txMsg.DataLength = 4;
47
    txMsg.RemoteFlag = 0;
48
    txMsg.RemoteFlag = 0;
48
    txMsg.ExtendedFlag = 1;
49
    txMsg.ExtendedFlag = 1;
49
    txMsg.Data.words[0] = APP_TYPE;
50
    txMsg.Data.words[0] = APP_TYPE;
50
    txMsg.Data.words[1] = APP_VERSION;
51
    txMsg.Data.words[1] = APP_VERSION;
51
   
52
   
52
    // Set up callback for CAN reception, this is optional if only sending is required.
53
    // Set up callback for CAN reception, this is optional if only sending is required.
53
    BIOS_CanCallback = &can_receive;
54
    BIOS_CanCallback = &can_receive;
54
    // Send CAN_NMT_APP_START
55
    // Send CAN_NMT_APP_START
55
    BIOS_CanSend(&txMsg);
56
    BIOS_CanSend(&txMsg);
56
 
57
 
57
    //txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
58
    //txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
58
   
59
   
59
    unsigned long time = Timebase_CurrentTime();
60
    unsigned long time = Timebase_CurrentTime();
60
    unsigned long irTimeoutTime = 100;
61
    unsigned long irTimeoutTime = 100;
61
   
62
   
-
 
63
    uint8_t ir_protocol;
62
    uint8_t ir_protocol, ir_address, ir_command;
64
    //uint8_t ir_address, ir_command;
-
 
65
    uint32_t ir_data;
63
    uint16_t ir_timeout;
66
    uint16_t ir_timeout;
64
    uint8_t state = STATE_IDLE;
67
    uint8_t state = STATE_IDLE;
65
 
68
 
66
    while (1) {
69
    while (1) {
67
        time = Timebase_CurrentTime();
70
        time = Timebase_CurrentTime();
68
       
71
       
69
        if (state == STATE_IDLE) {
72
        if (state == STATE_IDLE) {
70
            /* test for irdata */
73
            /* test for irdata */
71
            uint8_t returnval = IrReceive_CheckIR(&ir_protocol, &ir_address, &ir_command, &ir_timeout);
74
            //uint8_t returnval = IrReceive_CheckIR(&ir_protocol, &ir_address, &ir_command, &ir_timeout);
-
 
75
            uint8_t returnval = IrReceive_CheckIR(&ir_protocol, &ir_data, &ir_timeout);
72
            if (returnval == IR_OK) {
76
            if (returnval == IR_OK) {
73
                /* a protocol was found for irdata, send on can */
77
                /* a protocol was found for irdata, send on can */
74
                txMsg.Data.bytes[0] = IR_BUTTON_DOWN;
78
                txMsg.Data.bytes[0] = IR_BUTTON_DOWN;
75
                txMsg.Data.bytes[1] = ir_protocol;
79
                txMsg.Data.bytes[1] = ir_protocol;
76
                txMsg.Data.bytes[2] = ir_address;
80
                txMsg.Data.bytes[2] = (ir_data>>24)&0xff;
-
 
81
                txMsg.Data.bytes[3] = (ir_data>>16)&0xff;
-
 
82
                txMsg.Data.bytes[4] = (ir_data>>8)&0xff;
77
                txMsg.Data.bytes[3] = ir_command;
83
                txMsg.Data.bytes[5] = ir_data&0xff;
78
                txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
84
                txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
79
                txMsg.DataLength = 4;
85
                txMsg.DataLength = 6;
80
                BIOS_CanSend(&txMsg);
86
                BIOS_CanSend(&txMsg);
81
               
87
               
82
                /* set up timeout */
88
                /* set up timeout */
83
                irTimeoutTime = Timebase_CurrentTime();
89
                irTimeoutTime = Timebase_CurrentTime();
84
                state = STATE_IR_REPEAT;
90
                state = STATE_IR_REPEAT;
85
               
91
               
86
            } else if (returnval == IR_NO_PROTOCOL) {
92
            } else if (returnval == IR_NO_PROTOCOL) {
87
                /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */
93
                /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */
88
                ir_protocol = IR_PROTO_UNKNOWN;     //used in STATE_IR_REPEATE, to know if to send a release
94
                ir_protocol = IR_PROTO_UNKNOWN;     //used in STATE_IR_REPEATE, to know if to send a release
89
                txMsg.DataLength = 8;
95
                txMsg.DataLength = 8;
90
                txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_RAW<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
96
                txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_RAW<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
91
                uint8_t datacnt = getRawDataCnt();
97
                uint8_t datacnt = getRawDataCnt();
92
                for (uint8_t i = 0; i < datacnt>>2; i++) {
98
                for (uint8_t i = 0; i < datacnt>>2; i++) {
93
                    uint8_t index = i<<2;
99
                    uint8_t index = i<<2;
94
                    txMsg.Data.words[0] = getRawData(index);
100
                    txMsg.Data.words[0] = getRawData(index);
95
                    txMsg.Data.words[1] = getRawData(index+1);
101
                    txMsg.Data.words[1] = getRawData(index+1);
96
                    txMsg.Data.words[2] = getRawData(index+2);
102
                    txMsg.Data.words[2] = getRawData(index+2);
97
                    txMsg.Data.words[3] = getRawData(index+3);
103
                    txMsg.Data.words[3] = getRawData(index+3);
98
                   
104
                   
99
                    /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
105
                    /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
100
                    //while (BIOS_CanSend(&txMsg) != CAN_OK) {}
106
                    //while (BIOS_CanSend(&txMsg) != CAN_OK) {}
101
                    BIOS_CanSend(&txMsg);
107
                    BIOS_CanSend(&txMsg);
102
                   
108
                   
103
                    uint16_t dummycnt = 1;
109
                    uint16_t dummycnt = 1;
104
                    while (dummycnt > 0) {
110
                    while (dummycnt > 0) {
105
                        dummycnt++;
111
                        dummycnt++;
106
                        asm("nop");
112
                        asm("nop");
107
                        asm("nop");
113
                        asm("nop");
108
                    }
114
                    }
109
                }
115
                }
110
               
116
               
111
                uint8_t lastpacketcnt = datacnt&0x03;
117
                uint8_t lastpacketcnt = datacnt&0x03;
112
                if (lastpacketcnt > 0) {
118
                if (lastpacketcnt > 0) {
113
                    txMsg.DataLength = lastpacketcnt<<1;
119
                    txMsg.DataLength = lastpacketcnt<<1;
114
                    for (uint8_t i = 0; i < lastpacketcnt; i++) {
120
                    for (uint8_t i = 0; i < lastpacketcnt; i++) {
115
                        txMsg.Data.words[i] = getRawData((datacnt&0xfc)|i);
121
                        txMsg.Data.words[i] = getRawData((datacnt&0xfc)|i);
116
                    }
122
                    }
117
                    /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
123
                    /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
118
                    //while (BIOS_CanSend(&txMsg) != CAN_OK) {}
124
                    //while (BIOS_CanSend(&txMsg) != CAN_OK) {}
119
                    BIOS_CanSend(&txMsg);
125
                    BIOS_CanSend(&txMsg);
120
                }
126
                }
121
 
127
 
122
                irTimeoutTime = Timebase_CurrentTime();
128
                irTimeoutTime = Timebase_CurrentTime();
123
                state = STATE_IR_REPEAT;
129
                state = STATE_IR_REPEAT;
124
                ir_timeout = 200;           /* Set a default timeout */
130
                ir_timeout = 200;           /* Set a default timeout */
125
            } else if (returnval != IR_NO_DATA) {
131
            } else if (returnval != IR_NO_DATA) {
126
                /* debug, send the returned message on errors (except for IR_NO_DATA-errors) */
132
                /* debug, send the returned message on errors (except for IR_NO_DATA-errors) */
127
                txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DEBUG<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
133
                txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DEBUG<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
128
                txMsg.DataLength = 1;
134
                txMsg.DataLength = 1;
129
                txMsg.Data.bytes[0] = returnval;
135
                txMsg.Data.bytes[0] = returnval;
130
                BIOS_CanSend(&txMsg);
136
                BIOS_CanSend(&txMsg);
131
            }
137
            }
132
 
138
 
133
        } else if (state == STATE_IR_REPEAT) {
139
        } else if (state == STATE_IR_REPEAT) {
134
            /* check for new ir-data, if no new data during the timout-time then set state to IDLE */
140
            /* check for new ir-data, if no new data during the timout-time then set state to IDLE */
135
            if (time - irTimeoutTime >= ir_timeout) {      
141
            if (time - irTimeoutTime >= ir_timeout) {      
136
                state = STATE_IDLE;
142
                state = STATE_IDLE;
137
                /* if the protocol was known then send a release-message */
143
                /* if the protocol was known then send a release-message */
138
                if (ir_protocol != IR_PROTO_UNKNOWN) {
144
                if (ir_protocol != IR_PROTO_UNKNOWN) {
139
                    txMsg.Data.bytes[0] = IR_BUTTON_UP;
145
                    txMsg.Data.bytes[0] = IR_BUTTON_UP;
140
                    BIOS_CanSend(&txMsg);
146
                    BIOS_CanSend(&txMsg);
141
                }
147
                }
142
            }
148
            }
143
            if (IrReceive_CheckIdle() == IR_OK) {
149
            if (IrReceive_CheckIdle() == IR_OK) {
144
                irTimeoutTime = Timebase_CurrentTime();
150
                irTimeoutTime = Timebase_CurrentTime();
145
            }
151
            }
146
        }
152
        }
147
 
153
 
148
       
154
       
149
        if (rxMsgFull) {
155
        if (rxMsgFull) {
150
            /* No message reception functionality implemented */
156
            /* No message reception functionality implemented */
151
           
157
           
152
            /* Flush the received message */
158
            /* Flush the received message */
153
            rxMsgFull = 0; //  
159
            rxMsgFull = 0; //  
154
        }
160
        }
155
    }
161
    }
156
   
162
   
157
    return 0;
163
    return 0;
158
}
164
}
159
 
165