Subversion Repositories HomeAutomation

Rev

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

Rev 609 Rev 611
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/timer/timebase.h>
7
#include <drivers/timer/timebase.h>
8
#include <drivers/ir/transceiver/irtransceiver.h>
8
#include <drivers/ir/transceiver/irtransceiver.h>
9
#include <drivers/ir/protocols.h>
9
#include <drivers/ir/protocols.h>
10
 
10
 
11
#define APP_TYPE    CAN_APPTYPES_IRTRANSMITTER
11
#define APP_TYPE    CAN_APPTYPES_IRTRANSMITTER
12
#define APP_VERSION 0x0002
12
#define APP_VERSION 0x0002
13
 
13
 
14
#define STATE_IDLE              0
14
#define STATE_IDLE              0
15
#define STATE_START_TRANSMIT    1
15
#define STATE_START_TRANSMIT    1
16
#define STATE_TRANSMITTING      2
16
#define STATE_TRANSMITTING      2
17
#define STATE_START_PAUSE       3
17
#define STATE_START_PAUSE       3
18
#define STATE_PAUSING           4
18
#define STATE_PAUSING           4
19
#define STATE_STOP              5
19
#define STATE_STOP              5
20
 
20
 
21
// A simple message "queue", with space for one message only.
21
// A simple message "queue", with space for one message only.
22
// These are declared volatile to tell the compiler not to optimize away accesses.
22
// These are declared volatile to tell the compiler not to optimize away accesses.
23
volatile Can_Message_t rxMsg; // Message storage
23
volatile Can_Message_t rxMsg; // Message storage
24
volatile uint8_t rxMsgFull;   // Synchronization flag
24
volatile uint8_t rxMsgFull;   // Synchronization flag
25
 
25
 
26
// CAN message reception callback.
26
// CAN message reception callback.
27
// This function runs with interrupts disabled, keep it as short as possible.
27
// This function runs with interrupts disabled, keep it as short as possible.
28
void can_receive(Can_Message_t *msg) {
28
void can_receive(Can_Message_t *msg) {
29
    if (!rxMsgFull) {
29
    if (!rxMsgFull) {
30
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
30
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
31
        rxMsgFull = 1;
31
        rxMsgFull = 1;
32
    }
32
    }
33
}
33
}
34
 
34
 
35
int main(void)
35
int main(void)
36
{
36
{
37
    // Enable interrupts as early as possible
37
    // Enable interrupts as early as possible
38
    sei();
38
    sei();
39
   
39
   
40
    Timebase_Init();
40
    Timebase_Init();
41
    IrTransceiver_Init();
41
    IrTransceiver_Init();
42
   
42
   
43
    Can_Message_t txMsg;
43
    Can_Message_t txMsg;
44
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
44
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
45
    txMsg.DataLength = 4;
45
    txMsg.DataLength = 4;
46
    txMsg.RemoteFlag = 0;
46
    txMsg.RemoteFlag = 0;
47
    txMsg.ExtendedFlag = 1;
47
    txMsg.ExtendedFlag = 1;
48
    txMsg.Data.words[0] = APP_TYPE;
48
    txMsg.Data.words[0] = APP_TYPE;
49
    txMsg.Data.words[1] = APP_VERSION;
49
    txMsg.Data.words[1] = APP_VERSION;
50
   
50
   
51
    // Set up callback for CAN reception, this is optional if only sending is required.
51
    // Set up callback for CAN reception, this is optional if only sending is required.
52
    BIOS_CanCallback = &can_receive;
52
    BIOS_CanCallback = &can_receive;
53
    // Send CAN_NMT_APP_START
53
    // Send CAN_NMT_APP_START
54
    BIOS_CanSend(&txMsg);
54
    BIOS_CanSend(&txMsg);
55
 
55
 
56
    uint32_t time = Timebase_CurrentTime();
56
    uint32_t time = Timebase_CurrentTime();
57
    uint32_t timePauseStarted = 0;
57
    uint32_t timePauseStarted = 0;
58
   
58
   
59
    uint8_t state=STATE_IDLE;
59
    uint8_t state=STATE_IDLE;
60
    uint8_t repeatcnt=0;
60
    uint8_t repeatcnt=0;
61
    uint8_t stop=0;
61
    uint8_t stop=0;
62
    Ir_Protocol_Data_t proto;
62
    Ir_Protocol_Data_t proto;
63
    proto.timeout=0; proto.data=0; proto.repeats=0; proto.protocol=0;
63
    proto.timeout=0; proto.data=0; proto.repeats=0; proto.protocol=0;
64
    uint8_t len=0;
64
    uint8_t len=0;
65
   
65
   
66
    uint16_t txbuffer[MAX_NR_TIMES];
66
    uint16_t txbuffer[MAX_NR_TIMES];
67
   
67
   
68
    while (1) {
68
    while (1) {
69
        time = Timebase_CurrentTime();
-
 
70
        if (state == STATE_IDLE) {
69
        if (state == STATE_IDLE) {
71
        } else if (state == STATE_START_TRANSMIT) {
70
        } else if (state == STATE_START_TRANSMIT) {
72
            if (expandProtocol(txbuffer, &len, &proto) == IR_OK) {
71
            if (expandProtocol(txbuffer, &len, &proto) == IR_OK) {
73
                if (IrTransceiver_Transmit(txbuffer, len, proto.modfreq) == IR_OK) {
72
                if (IrTransceiver_Transmit(txbuffer, len, proto.modfreq) == IR_OK) {
74
                    state = STATE_TRANSMITTING;
73
                    state = STATE_TRANSMITTING;
75
                } else {
74
                } else {
76
                    state = STATE_IDLE;
75
                    state = STATE_IDLE;
77
                }
76
                }
78
            } else {
77
            } else {
79
                state = STATE_IDLE;
78
                state = STATE_IDLE;
80
            }
79
            }
81
        } else if (state == STATE_TRANSMITTING) {
80
        } else if (state == STATE_TRANSMITTING) {
82
            //polla om den är klar, om klar gå till STATE_START_PAUSE
81
            //polla om den är klar, om klar gå till STATE_START_PAUSE
83
            if (IrTransceiver_Transmit_Poll() != IR_NOT_FINISHED) {
82
            if (IrTransceiver_Transmit_Poll() != IR_NOT_FINISHED) {
84
                state = STATE_START_PAUSE;
83
                state = STATE_START_PAUSE;
85
            }
84
            }
86
        } else if (state == STATE_START_PAUSE) {
85
        } else if (state == STATE_START_PAUSE) {
87
            if (repeatcnt<proto.repeats) {
86
            if (repeatcnt<proto.repeats) {
88
                repeatcnt++;
87
                repeatcnt++;
89
            }
88
            }
90
            timePauseStarted = Timebase_CurrentTime();
89
            timePauseStarted = Timebase_CurrentTime();
91
            state = STATE_PAUSING;
90
            state = STATE_PAUSING;
92
        } else if (state == STATE_PAUSING) {
91
        } else if (state == STATE_PAUSING) {
93
            //när timeout har gått (timebase) så gå till STATE_START_TRANSMIT
92
            //när timeout har gått (timebase) så gå till STATE_START_TRANSMIT
-
 
93
            time = Timebase_CurrentTime();
94
            if (time - timePauseStarted >= proto.timeout) {
94
            if (time - timePauseStarted >= proto.timeout) {
95
                state = STATE_START_TRANSMIT;
95
                state = STATE_START_TRANSMIT;
96
            }
96
            }
97
            if (stop == 1 && repeatcnt >= proto.repeats) {
97
            if (stop == 1 && repeatcnt >= proto.repeats) {
98
                state = STATE_STOP;
98
                state = STATE_STOP;
99
            }
99
            }
100
        } else if (state == STATE_STOP) {
100
        } else if (state == STATE_STOP) {
101
            stop = 0;
101
            stop = 0;
102
            proto.timeout = 0;
102
            proto.timeout = 0;
103
            repeatcnt = 0;
103
            repeatcnt = 0;
104
            state = STATE_IDLE;
104
            state = STATE_IDLE;
105
        }
105
        }
106
 
106
 
107
       
107
       
108
        if (rxMsgFull) {
108
        if (rxMsgFull) {
109
            if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT) {
109
            if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT) {
110
                uint16_t acttype =(uint16_t)((rxMsg.Id & CAN_MASK_ACT_TYPE) >> CAN_SHIFT_ACT_TYPE);
110
                uint16_t acttype =(uint16_t)((rxMsg.Id & CAN_MASK_ACT_TYPE) >> CAN_SHIFT_ACT_TYPE);
111
                uint8_t actid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID);
111
                uint8_t actid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID);
112
               
112
               
113
                if (rxMsg.DataLength==6) {
113
                if (rxMsg.DataLength==6) {
114
                    if (acttype == ACT_TYPE_IR) {
114
                    if (acttype == ACT_TYPE_IR) {
115
                        if (state == STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_DOWN) {
115
                        if (state == STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_DOWN) {
116
                            proto.protocol = rxMsg.Data.bytes[1];
116
                            proto.protocol = rxMsg.Data.bytes[1];
117
                            proto.data = rxMsg.Data.bytes[5];
117
                            proto.data = rxMsg.Data.bytes[5];
118
                            proto.data |= (rxMsg.Data.bytes[4]<<8);
118
                            proto.data |= (rxMsg.Data.bytes[4]<<8);
119
                            proto.data |= ((uint32_t)rxMsg.Data.bytes[3]<<16);
119
                            proto.data |= ((uint32_t)rxMsg.Data.bytes[3]<<16);
120
                            proto.data |= ((uint32_t)rxMsg.Data.bytes[2]<<24);
120
                            proto.data |= ((uint32_t)rxMsg.Data.bytes[2]<<24);
121
 
121
 
122
                            state = STATE_START_TRANSMIT;
122
                            state = STATE_START_TRANSMIT;
123
                        } else if (state != STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_UP) {
123
                        } else if (state != STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_UP) {
124
                            stop = 1;
124
                            stop = 1;
125
                        }
125
                        }
126
                    }
126
                    }
127
                }
127
                }
128
            }
128
            }
129
           
129
           
130
            // Flush the received message
130
            // Flush the received message
131
            rxMsgFull = 0; //  
131
            rxMsgFull = 0; //  
132
        }
132
        }
133
    }
133
    }
134
   
134
   
135
    return 0;
135
    return 0;
136
}
136
}
137
 
137