Subversion Repositories HomeAutomation

Rev

Rev 1931 | Rev 2040 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1922 arune 1
 
2
#include "sns_rfTransceive.h"
3
 
1996 arune 4
#if IR_RX_ENABLE==1
1922 arune 5
StdCan_Msg_t        rfTxMsg;
1996 arune 6
#endif
1922 arune 7
 
1927 arune 8
#if IR_RX_ENABLE==1
1922 arune 9
uint8_t rfRxChannel_newData;
10
uint8_t rfRxChannel_rxlen;
11
uint8_t rfRxChannel_state;
12
Ir_Protocol_Data_t  rfRxChannel_proto;
1927 arune 13
uint16_t    rfRxChannel_buf[MAX_NR_TIMES];
1922 arune 14
 
15
void sns_rfTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
16
{
17
    rfRxChannel_newData = TRUE;
18
    rfRxChannel_rxlen = len;
19
}
20
#endif
21
 
22
 
23
#if IR_TX_ENABLE==1
1927 arune 24
uint8_t rfTxChannel_sendComplete;
25
uint8_t rfTxChannel_state;
26
uint8_t rfTxChannel_txlen;
27
Ir_Protocol_Data_t  rfTxChannel_proto;
28
uint16_t    rfTxChannel_buf[MAX_NR_TIMES];
29
uint8_t rfTxChannel_repeatCount;
30
uint8_t rfTxChannel_stopSend;
31
 
1922 arune 32
void sns_rfTransceive_TX_done_callback(uint8_t channel)
33
{
34
    rfTxChannel_sendComplete = TRUE;
35
}
36
#endif
37
 
38
void sns_rfTransceive_Init(void)
39
{
1996 arune 40
#if IR_RX_ENABLE==1
1922 arune 41
    StdCan_Set_class(rfTxMsg.Header, CAN_MODULE_CLASS_SNS);
42
    StdCan_Set_direction(rfTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
43
    rfTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_RFTRANSCEIVE;
44
    rfTxMsg.Header.ModuleId = sns_rfTransceive_ID;
1996 arune 45
    rfTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
1922 arune 46
    rfTxMsg.Length = 8;
47
 
48
    rfRxChannel_newData = FALSE;
49
    rfRxChannel_rxlen = 0;
50
    rfRxChannel_proto.timeout=0;
51
    rfRxChannel_proto.data=0;
52
    rfRxChannel_proto.repeats=0;
53
    rfRxChannel_proto.protocol=0;
1927 arune 54
#endif
55
 
56
#if IR_TX_ENABLE==1
1922 arune 57
    rfTxChannel_sendComplete = FALSE;
58
    rfTxChannel_txlen = 0;
59
    rfTxChannel_proto.data=0;
60
    rfTxChannel_proto.repeats=0;
61
    rfTxChannel_proto.framecnt=0;
62
    rfTxChannel_proto.protocol=0;
63
    rfTxChannel_repeatCount = 0;
1927 arune 64
#endif
1922 arune 65
 
66
    IrTransceiver_Init();
67
    /* TX-pin must be set in case transmitter is nexa */
68
    gpio_set_out(sns_rfTransceive_TX_PIN);
69
#if IR_TX_ACTIVE_LOW==1
70
    gpio_set_pin(sns_rfTransceive_TX_PIN);
71
#else
72
    gpio_clr_pin(sns_rfTransceive_TX_PIN);
73
#endif
74
 
1923 arune 75
#if IR_RX_ENABLE==1
1927 arune 76
    IrTransceiver_InitRxChannel(0, rfRxChannel_buf, sns_rfTransceive_RX_done_callback, sns_rfTransceive_RX_PCINT, sns_rfTransceive_RX_PIN);
1922 arune 77
    rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
1923 arune 78
#endif
1922 arune 79
 
1923 arune 80
#if IR_TX_ENABLE==1
1922 arune 81
    IrTransceiver_InitTxChannel(0, sns_rfTransceive_TX_done_callback, sns_rfTransceive_TX_PIN);
82
    rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
1923 arune 83
#endif
1922 arune 84
}
85
 
86
void sns_rfTransceive_Process(void)
87
{
88
#if IR_RX_ENABLE==1
89
        switch (rfRxChannel_state)
90
        {
91
        case sns_rfTransceive_STATE_IDLE:
92
        {
1931 arune 93
            /* If known protocol and timeout is not 0 (0 means burst) */
94
            if (rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN && rfRxChannel_proto.timeout != 0) {
1922 arune 95
                /* Send button release command on CAN */
1996 arune 96
                rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
1922 arune 97
                rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
98
                rfTxMsg.Data[2] = (rfRxChannel_proto.data>>40)&0xff;
99
                rfTxMsg.Data[3] = (rfRxChannel_proto.data>>32)&0xff;
100
                rfTxMsg.Data[4] = (rfRxChannel_proto.data>>24)&0xff;
101
                rfTxMsg.Data[5] = (rfRxChannel_proto.data>>16)&0xff;
102
                rfTxMsg.Data[6] = (rfRxChannel_proto.data>>8)&0xff;
103
                rfTxMsg.Data[7] = rfRxChannel_proto.data&0xff;
104
 
105
                StdCan_Put(&rfTxMsg);
106
            }
107
            rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
108
            break;
109
        }
110
 
111
        case sns_rfTransceive_STATE_START_RECEIVE:
112
            cli();
113
            rfRxChannel_newData = FALSE;
114
            sei();
115
            rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
116
 
117
            break;
118
 
119
        case sns_rfTransceive_STATE_RECEIVING:
120
            if (rfRxChannel_newData == TRUE) {
121
                cli();
122
                rfRxChannel_newData = FALSE;
123
                sei();
124
 
125
                /* Let protocol driver parse and then send on CAN */
1927 arune 126
                uint8_t res2 = parseProtocol(rfRxChannel_buf, rfRxChannel_rxlen, &rfRxChannel_proto);
1922 arune 127
                if (res2 == IR_OK && rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN) {
1931 arune 128
                    /* If timeout is 0, protocol is burst protocol */
129
                    if (rfRxChannel_proto.timeout > 0)
130
                    {
1996 arune 131
                        rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
1931 arune 132
                        rfRxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
133
                    }
134
                    else
135
                    {
1996 arune 136
                        rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST;
1931 arune 137
                        rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
138
                    }
1922 arune 139
                    rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
140
                    rfTxMsg.Data[2] = (rfRxChannel_proto.data>>40)&0xff;
141
                    rfTxMsg.Data[3] = (rfRxChannel_proto.data>>32)&0xff;
142
                    rfTxMsg.Data[4] = (rfRxChannel_proto.data>>24)&0xff;
143
                    rfTxMsg.Data[5] = (rfRxChannel_proto.data>>16)&0xff;
144
                    rfTxMsg.Data[6] = (rfRxChannel_proto.data>>8)&0xff;
145
                    rfTxMsg.Data[7] = rfRxChannel_proto.data&0xff;
146
 
147
                    StdCan_Put(&rfTxMsg);
148
                }
149
                else if (rfRxChannel_proto.protocol == IR_PROTO_UNKNOWN)
150
                {
151
#if (sns_rfTransceive_SEND_DEBUG==1)
1927 arune 152
                    //send_debug(rfRxChannel_buf, rfRxChannel_rxlen);
1922 arune 153
                    //rfRxChannel_proto.timeout=300;
154
#endif
155
                    rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
156
                }
157
            }
158
            break;
159
 
160
        case sns_rfTransceive_STATE_START_PAUSE:
161
            /* set a timer so we can send release button event when no new RF is arriving */
162
            Timer_SetTimeout(sns_rfTransceive_RX_REPEATE_TIMER, rfRxChannel_proto.timeout, TimerTypeOneShot, 0);
163
            rfRxChannel_state = sns_rfTransceive_STATE_PAUSING;
164
            break;
165
 
166
        case sns_rfTransceive_STATE_PAUSING:
167
            /* reset timer if new IR arrived */
168
            if (rfRxChannel_newData == TRUE) {
169
                cli();
170
                IrTransceiver_ResetRx(0); //TODO??
171
                rfRxChannel_newData = FALSE;
172
                sei();
173
 
174
                Ir_Protocol_Data_t  protoDummy;
1927 arune 175
                if (parseProtocol(rfRxChannel_buf, rfRxChannel_rxlen, &protoDummy) == IR_OK) {
1922 arune 176
                    if (protoDummy.protocol == rfRxChannel_proto.protocol) {
177
                        /* re-set timer so we can send release button event when no new RF is arriving */
178
                        Timer_SetTimeout(sns_rfTransceive_RX_REPEATE_TIMER, rfRxChannel_proto.timeout, TimerTypeOneShot, 0);
179
                    }
180
                }
181
            }
182
 
183
            if (Timer_Expired(sns_rfTransceive_RX_REPEATE_TIMER)) {
184
                rfRxChannel_state = sns_rfTransceive_STATE_IDLE;
185
            }
186
            break;
187
 
188
        default:
189
            break;
190
        }
191
#endif
192
 
1927 arune 193
#if IR_TX_ENABLE==1
194
        switch (rfTxChannel_state)
1922 arune 195
        {
1927 arune 196
        case sns_rfTransceive_STATE_IDLE:
1922 arune 197
        {
198
            /* transmission is started when a command is received on can */
1927 arune 199
            rfTxChannel_stopSend = FALSE;
200
            rfTxChannel_repeatCount = 0;
201
            rfTxChannel_proto.framecnt = 0;
1922 arune 202
            break;
203
        }
204
 
1927 arune 205
        case sns_rfTransceive_STATE_START_TRANSMIT:
1922 arune 206
        {
207
            /* Expand protocol. */
1927 arune 208
            if (expandProtocol(rfTxChannel_buf, &rfTxChannel_txlen, &rfTxChannel_proto) != IR_OK) {
1922 arune 209
                /* Failed to expand protocol -> enter idle state. */
1927 arune 210
                rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
1922 arune 211
                break;
212
            }
213
 
1927 arune 214
            /* Start RF transmission. */
215
            IrTransceiver_Transmit(0, rfTxChannel_buf, 0, rfTxChannel_txlen, rfTxChannel_proto.modfreq);
1922 arune 216
 
217
            /* Enter transmitting state. */
1927 arune 218
            rfTxChannel_state = sns_rfTransceive_STATE_TRANSMITTING;
1922 arune 219
            break;
220
        }
221
 
1927 arune 222
        case sns_rfTransceive_STATE_TRANSMITTING:
1922 arune 223
        {
1927 arune 224
            if (rfTxChannel_sendComplete == TRUE)
1922 arune 225
            {
226
                cli();
1927 arune 227
                rfTxChannel_sendComplete = FALSE;
1922 arune 228
                sei();
229
 
1927 arune 230
                rfTxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
1922 arune 231
            }
232
            break;
233
        }
234
 
1927 arune 235
        case sns_rfTransceive_STATE_START_PAUSE:
1922 arune 236
        {
1927 arune 237
            if (rfTxChannel_repeatCount < rfTxChannel_proto.repeats)
1922 arune 238
            {
1927 arune 239
                rfTxChannel_repeatCount++;
1922 arune 240
            }
241
 
1927 arune 242
            Timer_SetTimeout(sns_rfTransceive_TX_REPEATE_TIMER, rfTxChannel_proto.timeout, TimerTypeOneShot, 0);
1922 arune 243
 
1927 arune 244
            if (rfTxChannel_proto.framecnt != 255)
1922 arune 245
            {
1927 arune 246
                rfTxChannel_proto.framecnt++;
1922 arune 247
            }
248
 
1927 arune 249
            rfTxChannel_state = sns_rfTransceive_STATE_PAUSING;
1922 arune 250
            break;
251
        }
252
 
1927 arune 253
        case sns_rfTransceive_STATE_PAUSING:
1922 arune 254
        {
1927 arune 255
            if (Timer_Expired(sns_rfTransceive_TX_REPEATE_TIMER))
1922 arune 256
            {
1927 arune 257
                rfTxChannel_state = sns_rfTransceive_STATE_START_TRANSMIT;
1922 arune 258
            }
259
 
260
            /* Transmission is stopped when such command is recevied on can */
1927 arune 261
            if (rfTxChannel_stopSend == TRUE && rfTxChannel_repeatCount >= rfTxChannel_proto.repeats)
1922 arune 262
            {
1927 arune 263
                rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
1922 arune 264
            }
265
            break;
266
        }
267
        default:
268
            break;
269
        }
270
#endif
271
}
272
 
273
void sns_rfTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
274
{
275
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
276
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
277
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_RFTRANSCEIVE &&
278
        rxMsg->Header.ModuleId == sns_rfTransceive_ID)
279
    {
280
        switch (rxMsg->Header.Command)
281
        {
282
#if IR_TX_ENABLE==1
1996 arune 283
            case CAN_MODULE_CMD_PHYSICAL_IR:
1927 arune 284
            {
1996 arune 285
                if (    rfTxChannel_state == sns_rfTransceive_STATE_IDLE &&
286
                    (rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED ||
287
                    rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST))
288
                {
289
                    rfTxChannel_stopSend = (uint8_t)(rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST);
1927 arune 290
 
1996 arune 291
                    rfTxChannel_proto.protocol = rxMsg->Data[1];
292
                    rfTxChannel_proto.data = rxMsg->Data[2];
293
                    rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
294
                    rfTxChannel_proto.data |= rxMsg->Data[3];
295
                    rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
296
                    rfTxChannel_proto.data |= rxMsg->Data[4];
297
                    rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
298
                    rfTxChannel_proto.data |= rxMsg->Data[5];
299
                    rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
300
                    rfTxChannel_proto.data |= rxMsg->Data[6];
301
                    rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
302
                    rfTxChannel_proto.data |= rxMsg->Data[7];
303
 
304
                    rfTxChannel_state = sns_rfTransceive_STATE_START_TRANSMIT;
305
                }
306
                else if (rfTxChannel_state != sns_rfTransceive_STATE_IDLE && rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED)
307
                {
308
                    rfTxChannel_stopSend = TRUE;
309
                }
1927 arune 310
            }
1922 arune 311
#endif
312
        }
313
    }
314
}
315
 
316
void sns_rfTransceive_List(uint8_t ModuleSequenceNumber)
317
{
318
    StdCan_Msg_t txMsg;
319
 
320
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
321
    StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
322
    txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_RFTRANSCEIVE;
323
    txMsg.Header.ModuleId = sns_rfTransceive_ID;
324
    txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
325
    txMsg.Length = 6;
326
 
1927 arune 327
    uint32_t HwId=BIOS_GetHwId();
328
    txMsg.Data[0] = HwId&0xff;
329
    txMsg.Data[1] = (HwId>>8)&0xff;
330
    txMsg.Data[2] = (HwId>>16)&0xff;
331
    txMsg.Data[3] = (HwId>>24)&0xff;
1922 arune 332
 
333
    txMsg.Data[4] = NUMBER_OF_MODULES;
334
    txMsg.Data[5] = ModuleSequenceNumber;
335
 
336
    while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
337
}