Subversion Repositories HomeAutomation

Rev

Rev 614 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 614 Rev 616
Line 35... Line 35...
35
    if (!rxMsgFull) {
35
    if (!rxMsgFull) {
36
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
36
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
37
        rxMsgFull = 1;
37
        rxMsgFull = 1;
38
    }
38
    }
39
}
39
}
-
 
40
 
-
 
41
#if (SEND_DEBUG)
-
 
42
void send_debug(uint16_t *buffer, uint8_t len) {
-
 
43
    Can_Message_t txMsg;
-
 
44
   
-
 
45
    /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */
-
 
46
    txMsg.DataLength = 8;
-
 
47
    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));
-
 
48
    for (uint8_t i = 0; i < len>>2; i++) {
-
 
49
        uint8_t index = i<<2;
-
 
50
        txMsg.Data.bytes[0] = (buffer[index]>>8)&0xff;
-
 
51
        txMsg.Data.bytes[1] = (buffer[index]>>0)&0xff;
-
 
52
        txMsg.Data.bytes[2] = (buffer[index+1]>>8)&0xff;
-
 
53
        txMsg.Data.bytes[3] = (buffer[index+1]>>0)&0xff;
-
 
54
        txMsg.Data.bytes[4] = (buffer[index+2]>>8)&0xff;
-
 
55
        txMsg.Data.bytes[5] = (buffer[index+2]>>0)&0xff;
-
 
56
        txMsg.Data.bytes[6] = (buffer[index+3]>>8)&0xff;
-
 
57
        txMsg.Data.bytes[7] = (buffer[index+3]>>0)&0xff;
-
 
58
       
-
 
59
        /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
-
 
60
        //while (BIOS_CanSend(&txMsg) != CAN_OK) {}
-
 
61
        BIOS_CanSend(&txMsg);
-
 
62
       
-
 
63
        uint16_t dummycnt = 1;
-
 
64
        while (dummycnt > 0) {
-
 
65
            dummycnt++;
-
 
66
            asm("nop");
-
 
67
            asm("nop");
-
 
68
        }
-
 
69
    }
-
 
70
   
-
 
71
    uint8_t lastpacketcnt = len&0x03;
-
 
72
    if (lastpacketcnt > 0) {
-
 
73
        txMsg.DataLength = lastpacketcnt<<1;
-
 
74
        for (uint8_t i = 0; i < lastpacketcnt; i++) {
-
 
75
            txMsg.Data.bytes[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff;
-
 
76
            txMsg.Data.bytes[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff;
-
 
77
        }
-
 
78
        /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
-
 
79
        //while (BIOS_CanSend(&txMsg) != CAN_OK) {}
-
 
80
        BIOS_CanSend(&txMsg);
-
 
81
    }
-
 
82
 
-
 
83
}
-
 
84
#endif
40
 
85
 
41
int main(void)
86
int main(void)
42
{
87
{
43
    // Enable interrupts as early as possible
88
    // Enable interrupts as early as possible
44
    sei();
89
    sei();
45
   
90
   
46
    Timebase_Init();
91
    Timebase_Init();
47
    IrTransceiver_Init();
92
    IrTransceiver_Init();
48
   
93
   
49
    Can_Message_t txMsg;
94
    Can_Message_t txMsg;
50
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
95
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
51
    txMsg.DataLength = 4;
96
    txMsg.DataLength = 4;
52
    txMsg.RemoteFlag = 0;
97
    txMsg.RemoteFlag = 0;
53
    txMsg.ExtendedFlag = 1;
98
    txMsg.ExtendedFlag = 1;
54
    txMsg.Data.words[0] = APP_TYPE;
99
    txMsg.Data.words[0] = APP_TYPE;
55
    txMsg.Data.words[1] = APP_VERSION;
100
    txMsg.Data.words[1] = APP_VERSION;
56
   
101
   
57
    // Set up callback for CAN reception, this is optional if only sending is required.
102
    // Set up callback for CAN reception, this is optional if only sending is required.
58
    BIOS_CanCallback = &can_receive;
103
    BIOS_CanCallback = &can_receive;
59
    // Send CAN_NMT_APP_START
104
    // Send CAN_NMT_APP_START
60
    BIOS_CanSend(&txMsg);
105
    BIOS_CanSend(&txMsg);
61
   
106
   
62
    uint32_t time = Timebase_CurrentTime();
107
    uint32_t time = Timebase_CurrentTime();
63
    uint32_t timePauseStarted = 0;
108
    uint32_t timePauseStarted = 0;
64
   
109
   
65
    uint8_t state = STATE_IDLE;
110
    uint8_t state = STATE_IDLE;
66
   
111
   
67
    Ir_Protocol_Data_t proto;
112
    Ir_Protocol_Data_t proto;
68
    proto.timeout=0; proto.data=0; proto.repeats=0; proto.protocol=0;
113
    proto.timeout=0; proto.data=0; proto.repeats=0; proto.protocol=0;
69
    uint8_t len=0;
114
    uint8_t len=0;
Line 77... Line 122...
77
            state = STATE_RECEIVING;
122
            state = STATE_RECEIVING;
78
        } else if (state == STATE_RECEIVING) {
123
        } else if (state == STATE_RECEIVING) {
79
            uint8_t res = IrTransceiver_Receive_Poll(&len);
124
            uint8_t res = IrTransceiver_Receive_Poll(&len);
80
            if ((res == IR_OK) && (len > 0)) {
125
            if ((res == IR_OK) && (len > 0)) {
81
                //låt protocols parsa och skicka på can
126
                //låt protocols parsa och skicka på can
82
                if (parseProtocol(rxbuffer, len, &proto) == IR_OK) {
127
                uint8_t res2 = parseProtocol(rxbuffer, len, &proto);
83
                    if (proto.protocol != IR_PROTO_UNKNOWN) {
128
                if (res2 == IR_OK && proto.protocol != IR_PROTO_UNKNOWN) {
84
                        txMsg.Data.bytes[0] = IR_BUTTON_DOWN;
129
                    txMsg.Data.bytes[0] = IR_BUTTON_DOWN;
85
                        txMsg.Data.bytes[1] = proto.protocol;
130
                    txMsg.Data.bytes[1] = proto.protocol;
86
                        txMsg.Data.bytes[2] = (proto.data>>24)&0xff;
131
                    txMsg.Data.bytes[2] = (proto.data>>24)&0xff;
87
                        txMsg.Data.bytes[3] = (proto.data>>16)&0xff;
132
                    txMsg.Data.bytes[3] = (proto.data>>16)&0xff;
88
                        txMsg.Data.bytes[4] = (proto.data>>8)&0xff;
133
                    txMsg.Data.bytes[4] = (proto.data>>8)&0xff;
89
                        txMsg.Data.bytes[5] = proto.data&0xff;
134
                    txMsg.Data.bytes[5] = proto.data&0xff;
90
                        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));
135
                    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));
91
                        txMsg.DataLength = 6;
136
                    txMsg.DataLength = 6;
92
                        BIOS_CanSend(&txMsg);
137
                    BIOS_CanSend(&txMsg);
-
 
138
                } else if (proto.protocol == IR_PROTO_UNKNOWN) {
-
 
139
#if (SEND_DEBUG)
-
 
140
                        send_debug(rxbuffer, len);
-
 
141
                        proto.timeout=300;
-
 
142
#endif
93
                    }
143
                   
94
                }
144
                }
95
               
145
               
96
                state = STATE_START_PAUSE;
146
                state = STATE_START_PAUSE;
97
            }
147
            }
98
        } else if (state == STATE_START_PAUSE) {
148
        } else if (state == STATE_START_PAUSE) {
99
            IrTransceiver_Receive_Start(rxbuffer);
149
            IrTransceiver_Receive_Start(rxbuffer);
100
            timePauseStarted = Timebase_CurrentTime();
150
            timePauseStarted = Timebase_CurrentTime();
101
            state = STATE_PAUSING;
151
            state = STATE_PAUSING;
102
        } else if (state == STATE_PAUSING) {
152
        } else if (state == STATE_PAUSING) {
103
            //resetta timebase-var om ir finns, detta bör göras snyggare
153
            //resetta timebase-var om ir finns
104
            /*for (uint8_t i=0; i<10; i++) {
-
 
105
                if (IrTransceiver_Receive_Pause_Poll() == 0) {
-
 
106
                    break;
-
 
107
                }
-
 
108
                if (i==9) {
-
 
109
                    timePauseStarted = Timebase_CurrentTime();
-
 
110
                }
-
 
111
            }*/
-
 
112
            uint8_t res = IrTransceiver_Receive_Poll(&len);
154
            uint8_t res = IrTransceiver_Receive_Poll(&len);
113
            if (res == IR_NOT_FINISHED || res == IR_OK) {
155
            if (res == IR_NOT_FINISHED || res == IR_OK) {
114
                timePauseStarted = Timebase_CurrentTime();
156
                timePauseStarted = Timebase_CurrentTime();
115
            }
157
            }
116
            if (res == IR_OK) {
158
            if (res == IR_OK) {
-
 
159
                /* start a new reception */
117
                IrTransceiver_Receive_Start(rxbuffer);
160
                IrTransceiver_Receive_Start(rxbuffer);
118
            }
161
            }
119
           
162
           
120
            time = Timebase_CurrentTime();
163
            time = Timebase_CurrentTime();
121
            if (time - timePauseStarted >= proto.timeout) {
164
            if (time - timePauseStarted >= proto.timeout) {