Subversion Repositories HomeAutomation

Rev

Rev 1931 | Rev 2040 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1931 Rev 1996
Line 2... Line 2...
2
#include "sns_rfTransceive.h"
2
#include "sns_rfTransceive.h"
3
 
3
 
-
 
4
#if IR_RX_ENABLE==1
4
StdCan_Msg_t        rfTxMsg;
5
StdCan_Msg_t        rfTxMsg;
-
 
6
#endif
5
 
7
 
6
#if IR_RX_ENABLE==1
8
#if IR_RX_ENABLE==1
7
uint8_t rfRxChannel_newData;
9
uint8_t rfRxChannel_newData;
8
uint8_t rfRxChannel_rxlen;
10
uint8_t rfRxChannel_rxlen;
9
uint8_t rfRxChannel_state;
11
uint8_t rfRxChannel_state;
10
Ir_Protocol_Data_t  rfRxChannel_proto;
12
Ir_Protocol_Data_t  rfRxChannel_proto;
11
uint16_t    rfRxChannel_buf[MAX_NR_TIMES];
13
uint16_t    rfRxChannel_buf[MAX_NR_TIMES];
12
 
14
 
13
void sns_rfTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
15
void sns_rfTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
14
{
16
{
15
    rfRxChannel_newData = TRUE;
17
    rfRxChannel_newData = TRUE;
16
    rfRxChannel_rxlen = len;
18
    rfRxChannel_rxlen = len;
17
}
19
}
18
#endif
20
#endif
19
 
21
 
20
 
22
 
21
#if IR_TX_ENABLE==1
23
#if IR_TX_ENABLE==1
22
uint8_t rfTxChannel_sendComplete;
24
uint8_t rfTxChannel_sendComplete;
23
uint8_t rfTxChannel_state;
25
uint8_t rfTxChannel_state;
24
uint8_t rfTxChannel_txlen;
26
uint8_t rfTxChannel_txlen;
25
Ir_Protocol_Data_t  rfTxChannel_proto;
27
Ir_Protocol_Data_t  rfTxChannel_proto;
26
uint16_t    rfTxChannel_buf[MAX_NR_TIMES];
28
uint16_t    rfTxChannel_buf[MAX_NR_TIMES];
27
uint8_t rfTxChannel_repeatCount;
29
uint8_t rfTxChannel_repeatCount;
28
uint8_t rfTxChannel_stopSend;
30
uint8_t rfTxChannel_stopSend;
29
 
31
 
30
void sns_rfTransceive_TX_done_callback(uint8_t channel)
32
void sns_rfTransceive_TX_done_callback(uint8_t channel)
31
{
33
{
32
    rfTxChannel_sendComplete = TRUE;
34
    rfTxChannel_sendComplete = TRUE;
33
}
35
}
34
#endif
36
#endif
35
 
37
 
36
void sns_rfTransceive_Init(void)
38
void sns_rfTransceive_Init(void)
37
{
39
{
-
 
40
#if IR_RX_ENABLE==1
38
    StdCan_Set_class(rfTxMsg.Header, CAN_MODULE_CLASS_SNS);
41
    StdCan_Set_class(rfTxMsg.Header, CAN_MODULE_CLASS_SNS);
39
    StdCan_Set_direction(rfTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
42
    StdCan_Set_direction(rfTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
40
    rfTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_RFTRANSCEIVE;
43
    rfTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_RFTRANSCEIVE;
41
    rfTxMsg.Header.ModuleId = sns_rfTransceive_ID;
44
    rfTxMsg.Header.ModuleId = sns_rfTransceive_ID;
-
 
45
    rfTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
42
    rfTxMsg.Length = 8;
46
    rfTxMsg.Length = 8;
43
   
47
   
44
#if IR_RX_ENABLE==1
-
 
45
    rfRxChannel_newData = FALSE;
48
    rfRxChannel_newData = FALSE;
46
    rfRxChannel_rxlen = 0;
49
    rfRxChannel_rxlen = 0;
47
    rfRxChannel_proto.timeout=0;
50
    rfRxChannel_proto.timeout=0;
48
    rfRxChannel_proto.data=0;
51
    rfRxChannel_proto.data=0;
49
    rfRxChannel_proto.repeats=0;
52
    rfRxChannel_proto.repeats=0;
50
    rfRxChannel_proto.protocol=0;
53
    rfRxChannel_proto.protocol=0;
51
#endif
54
#endif
52
 
55
 
53
#if IR_TX_ENABLE==1
56
#if IR_TX_ENABLE==1
54
    rfTxChannel_sendComplete = FALSE;
57
    rfTxChannel_sendComplete = FALSE;
55
    rfTxChannel_txlen = 0;
58
    rfTxChannel_txlen = 0;
56
    rfTxChannel_proto.data=0;
59
    rfTxChannel_proto.data=0;
57
    rfTxChannel_proto.repeats=0;
60
    rfTxChannel_proto.repeats=0;
58
    rfTxChannel_proto.framecnt=0;
61
    rfTxChannel_proto.framecnt=0;
59
    rfTxChannel_proto.protocol=0;
62
    rfTxChannel_proto.protocol=0;
60
    rfTxChannel_repeatCount = 0;
63
    rfTxChannel_repeatCount = 0;
61
#endif
64
#endif
62
 
65
 
63
    IrTransceiver_Init();
66
    IrTransceiver_Init();
64
    /* TX-pin must be set in case transmitter is nexa */
67
    /* TX-pin must be set in case transmitter is nexa */
65
    gpio_set_out(sns_rfTransceive_TX_PIN);
68
    gpio_set_out(sns_rfTransceive_TX_PIN);
66
#if IR_TX_ACTIVE_LOW==1
69
#if IR_TX_ACTIVE_LOW==1
67
    gpio_set_pin(sns_rfTransceive_TX_PIN);
70
    gpio_set_pin(sns_rfTransceive_TX_PIN);
68
#else
71
#else
69
    gpio_clr_pin(sns_rfTransceive_TX_PIN);
72
    gpio_clr_pin(sns_rfTransceive_TX_PIN);
70
#endif
73
#endif
71
 
74
 
72
#if IR_RX_ENABLE==1
75
#if IR_RX_ENABLE==1
73
    IrTransceiver_InitRxChannel(0, rfRxChannel_buf, sns_rfTransceive_RX_done_callback, sns_rfTransceive_RX_PCINT, sns_rfTransceive_RX_PIN);
76
    IrTransceiver_InitRxChannel(0, rfRxChannel_buf, sns_rfTransceive_RX_done_callback, sns_rfTransceive_RX_PCINT, sns_rfTransceive_RX_PIN);
74
    rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
77
    rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
75
#endif
78
#endif
76
   
79
   
77
#if IR_TX_ENABLE==1
80
#if IR_TX_ENABLE==1
78
    IrTransceiver_InitTxChannel(0, sns_rfTransceive_TX_done_callback, sns_rfTransceive_TX_PIN);
81
    IrTransceiver_InitTxChannel(0, sns_rfTransceive_TX_done_callback, sns_rfTransceive_TX_PIN);
79
    rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
82
    rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
80
#endif
83
#endif
81
}
84
}
82
 
85
 
83
void sns_rfTransceive_Process(void)
86
void sns_rfTransceive_Process(void)
84
{
87
{
85
#if IR_RX_ENABLE==1
88
#if IR_RX_ENABLE==1
86
        switch (rfRxChannel_state)
89
        switch (rfRxChannel_state)
Line 88... Line 91...
88
        case sns_rfTransceive_STATE_IDLE:
91
        case sns_rfTransceive_STATE_IDLE:
89
        {
92
        {
90
            /* If known protocol and timeout is not 0 (0 means burst) */
93
            /* If known protocol and timeout is not 0 (0 means burst) */
91
            if (rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN && rfRxChannel_proto.timeout != 0) {
94
            if (rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN && rfRxChannel_proto.timeout != 0) {
92
                /* Send button release command on CAN */
95
                /* Send button release command on CAN */
93
                rfTxMsg.Header.Command = CAN_MODULE_CMD_RFTRANSCEIVE_DATASTOP;
96
                rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
94
                rfTxMsg.Data[0] = 0;
-
 
95
                rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
97
                rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
96
                rfTxMsg.Data[2] = (rfRxChannel_proto.data>>40)&0xff;
98
                rfTxMsg.Data[2] = (rfRxChannel_proto.data>>40)&0xff;
97
                rfTxMsg.Data[3] = (rfRxChannel_proto.data>>32)&0xff;
99
                rfTxMsg.Data[3] = (rfRxChannel_proto.data>>32)&0xff;
98
                rfTxMsg.Data[4] = (rfRxChannel_proto.data>>24)&0xff;
100
                rfTxMsg.Data[4] = (rfRxChannel_proto.data>>24)&0xff;
99
                rfTxMsg.Data[5] = (rfRxChannel_proto.data>>16)&0xff;
101
                rfTxMsg.Data[5] = (rfRxChannel_proto.data>>16)&0xff;
100
                rfTxMsg.Data[6] = (rfRxChannel_proto.data>>8)&0xff;
102
                rfTxMsg.Data[6] = (rfRxChannel_proto.data>>8)&0xff;
101
                rfTxMsg.Data[7] = rfRxChannel_proto.data&0xff;
103
                rfTxMsg.Data[7] = rfRxChannel_proto.data&0xff;
102
 
104
 
103
                StdCan_Put(&rfTxMsg);
105
                StdCan_Put(&rfTxMsg);
104
            }
106
            }
105
            rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
107
            rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
106
            break;
108
            break;
107
        }
109
        }
Line 109... Line 111...
109
        case sns_rfTransceive_STATE_START_RECEIVE:
111
        case sns_rfTransceive_STATE_START_RECEIVE:
110
            cli();
112
            cli();
111
            rfRxChannel_newData = FALSE;
113
            rfRxChannel_newData = FALSE;
112
            sei();
114
            sei();
113
            rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
115
            rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
114
           
116
           
115
            break;
117
            break;
116
 
118
 
117
        case sns_rfTransceive_STATE_RECEIVING:
119
        case sns_rfTransceive_STATE_RECEIVING:
118
            if (rfRxChannel_newData == TRUE) {
120
            if (rfRxChannel_newData == TRUE) {
119
                cli();
121
                cli();
120
                rfRxChannel_newData = FALSE;
122
                rfRxChannel_newData = FALSE;
Line 124... Line 126...
124
                uint8_t res2 = parseProtocol(rfRxChannel_buf, rfRxChannel_rxlen, &rfRxChannel_proto);
126
                uint8_t res2 = parseProtocol(rfRxChannel_buf, rfRxChannel_rxlen, &rfRxChannel_proto);
125
                if (res2 == IR_OK && rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN) {
127
                if (res2 == IR_OK && rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN) {
126
                    /* If timeout is 0, protocol is burst protocol */
128
                    /* If timeout is 0, protocol is burst protocol */
127
                    if (rfRxChannel_proto.timeout > 0)
129
                    if (rfRxChannel_proto.timeout > 0)
128
                    {
130
                    {
129
                        rfTxMsg.Header.Command = CAN_MODULE_CMD_RFTRANSCEIVE_DATASTART;
131
                        rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
130
                        rfRxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
132
                        rfRxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
131
                    }
133
                    }
132
                    else
134
                    else
133
                    {
135
                    {
134
                        rfTxMsg.Header.Command = CAN_MODULE_CMD_RFTRANSCEIVE_DATABURST;
136
                        rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST;
135
                        rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
137
                        rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
136
                    }
138
                    }
137
                    rfTxMsg.Data[0] = 0;
-
 
138
                    rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
139
                    rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
139
                    rfTxMsg.Data[2] = (rfRxChannel_proto.data>>40)&0xff;
140
                    rfTxMsg.Data[2] = (rfRxChannel_proto.data>>40)&0xff;
140
                    rfTxMsg.Data[3] = (rfRxChannel_proto.data>>32)&0xff;
141
                    rfTxMsg.Data[3] = (rfRxChannel_proto.data>>32)&0xff;
141
                    rfTxMsg.Data[4] = (rfRxChannel_proto.data>>24)&0xff;
142
                    rfTxMsg.Data[4] = (rfRxChannel_proto.data>>24)&0xff;
142
                    rfTxMsg.Data[5] = (rfRxChannel_proto.data>>16)&0xff;
143
                    rfTxMsg.Data[5] = (rfRxChannel_proto.data>>16)&0xff;
Line 150... Line 151...
150
#if (sns_rfTransceive_SEND_DEBUG==1)
151
#if (sns_rfTransceive_SEND_DEBUG==1)
151
                    //send_debug(rfRxChannel_buf, rfRxChannel_rxlen);
152
                    //send_debug(rfRxChannel_buf, rfRxChannel_rxlen);
152
                    //rfRxChannel_proto.timeout=300;
153
                    //rfRxChannel_proto.timeout=300;
153
#endif
154
#endif
154
                    rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
155
                    rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
155
                }
156
                }
156
            }
157
            }
157
            break;
158
            break;
158
 
159
 
159
        case sns_rfTransceive_STATE_START_PAUSE:
160
        case sns_rfTransceive_STATE_START_PAUSE:
160
            /* set a timer so we can send release button event when no new RF is arriving */
161
            /* set a timer so we can send release button event when no new RF is arriving */
Line 177... Line 178...
177
                        Timer_SetTimeout(sns_rfTransceive_RX_REPEATE_TIMER, rfRxChannel_proto.timeout, TimerTypeOneShot, 0);
178
                        Timer_SetTimeout(sns_rfTransceive_RX_REPEATE_TIMER, rfRxChannel_proto.timeout, TimerTypeOneShot, 0);
178
                    }
179
                    }
179
                }
180
                }
180
            }
181
            }
181
 
182
 
182
            if (Timer_Expired(sns_rfTransceive_RX_REPEATE_TIMER)) {
183
            if (Timer_Expired(sns_rfTransceive_RX_REPEATE_TIMER)) {
183
                rfRxChannel_state = sns_rfTransceive_STATE_IDLE;
184
                rfRxChannel_state = sns_rfTransceive_STATE_IDLE;
184
            }
185
            }
185
            break;
186
            break;
186
 
187
 
187
        default:
188
        default:
188
            break;
189
            break;
189
        }
190
        }
190
#endif
191
#endif
191
 
192
 
192
#if IR_TX_ENABLE==1
193
#if IR_TX_ENABLE==1
193
        switch (rfTxChannel_state)
194
        switch (rfTxChannel_state)
194
        {
195
        {
195
        case sns_rfTransceive_STATE_IDLE:
196
        case sns_rfTransceive_STATE_IDLE:
196
        {
197
        {
197
            /* transmission is started when a command is received on can */
198
            /* transmission is started when a command is received on can */
Line 258... Line 259...
258
 
259
 
259
            /* Transmission is stopped when such command is recevied on can */
260
            /* Transmission is stopped when such command is recevied on can */
260
            if (rfTxChannel_stopSend == TRUE && rfTxChannel_repeatCount >= rfTxChannel_proto.repeats)
261
            if (rfTxChannel_stopSend == TRUE && rfTxChannel_repeatCount >= rfTxChannel_proto.repeats)
261
            {
262
            {
262
                rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
263
                rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
263
            }
264
            }
264
            break;
265
            break;
265
        }
266
        }
266
        default:
267
        default:
267
            break;
268
            break;
268
        }
269
        }
269
#endif
270
#endif
270
}
271
}
271
 
272
 
272
void sns_rfTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
273
void sns_rfTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
273
{
274
{
274
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
275
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
275
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
276
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
276
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_RFTRANSCEIVE &&
277
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_RFTRANSCEIVE &&
277
        rxMsg->Header.ModuleId == sns_rfTransceive_ID)
278
        rxMsg->Header.ModuleId == sns_rfTransceive_ID)
278
    {
279
    {
279
        switch (rxMsg->Header.Command)
280
        switch (rxMsg->Header.Command)
280
        {
281
        {
281
#if IR_TX_ENABLE==1
282
#if IR_TX_ENABLE==1
282
        case CAN_MODULE_CMD_RFTRANSCEIVE_DATABURST:
283
            case CAN_MODULE_CMD_PHYSICAL_IR:
283
        {
284
            {
284
            if (rfTxChannel_state == sns_rfTransceive_STATE_IDLE)
285
                if (    rfTxChannel_state == sns_rfTransceive_STATE_IDLE &&
285
            {
-
 
286
                rfTxChannel_stopSend = TRUE;
-
 
287
            }
-
 
288
        }   /* Fall through, no break */
-
 
289
        case CAN_MODULE_CMD_RFTRANSCEIVE_DATASTART:
286
                    (rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED ||
290
        {
-
 
291
            if (rfTxChannel_state == sns_rfTransceive_STATE_IDLE)
287
                    rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST))
292
            {
288
                {
-
 
289
                    rfTxChannel_stopSend = (uint8_t)(rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST);
-
 
290
 
293
                rfTxChannel_proto.protocol = rxMsg->Data[1];
291
                    rfTxChannel_proto.protocol = rxMsg->Data[1];
294
                rfTxChannel_proto.data = rxMsg->Data[2];
292
                    rfTxChannel_proto.data = rxMsg->Data[2];
295
                rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
293
                    rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
296
                rfTxChannel_proto.data |= rxMsg->Data[3];
294
                    rfTxChannel_proto.data |= rxMsg->Data[3];
297
                rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
295
                    rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
Line 302... Line 300...
302
                rfTxChannel_proto.data |= rxMsg->Data[6];
300
                    rfTxChannel_proto.data |= rxMsg->Data[6];
303
                rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
301
                    rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
304
                rfTxChannel_proto.data |= rxMsg->Data[7];
302
                    rfTxChannel_proto.data |= rxMsg->Data[7];
305
 
303
 
306
                rfTxChannel_state = sns_rfTransceive_STATE_START_TRANSMIT;
304
                    rfTxChannel_state = sns_rfTransceive_STATE_START_TRANSMIT;
307
            }
305
                }
308
        }
-
 
309
        break;
-
 
310
        case CAN_MODULE_CMD_RFTRANSCEIVE_DATASTOP:
306
                else if (rfTxChannel_state != sns_rfTransceive_STATE_IDLE && rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED)
311
        {
307
                {
312
            rfTxChannel_stopSend = TRUE;
308
                    rfTxChannel_stopSend = TRUE;
313
        }
309
                }
314
        break;
310
            }
315
#endif
311
#endif
316
        }
312
        }
317
    }
313
    }
318
}
314
}
319
 
315