Subversion Repositories HomeAutomation

Rev

Rev 2199 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

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