Subversion Repositories HomeAutomation

Rev

Rev 2200 | 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 (sns_rfTransceive_SEND_DEBUG==1)
  97. StdCan_Msg_t irTxMsg;
  98. void send_debug(uint16_t *buffer, uint8_t startindex, uint8_t endindex) {
  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_RFTRANSCEIVE;
  106.     irTxMsg.Header.ModuleId = 0;
  107.     irTxMsg.Header.Command = CAN_MODULE_CMD_IRRECEIVE_IRRAW;
  108.     while (StdCan_Put(&irTxMsg) != StdCan_Ret_OK) {}
  109.       _delay_ms(1);
  110.     //_------------------
  111.     uint8_t i= startindex;
  112.     while(i != endindex) {
  113.       irTxMsg.Length = 2;
  114.       irTxMsg.Data[0] = (buffer[i]>>8)&0xff;
  115.       irTxMsg.Data[1] = (buffer[i]>>0)&0xff;
  116.       i++;
  117.       if (i == MAX_NR_TIMES) {
  118.         i=0;
  119.       }
  120.       if(i != endindex) {
  121.         irTxMsg.Length = 4;
  122.         irTxMsg.Data[2] = (buffer[i]>>8)&0xff;
  123.         irTxMsg.Data[3] = (buffer[i]>>0)&0xff;
  124.         i++;
  125.         if (i == MAX_NR_TIMES) {
  126.           i=0;
  127.         }
  128.         if(i != endindex) {
  129.           irTxMsg.Length = 6;
  130.           irTxMsg.Data[4] = (buffer[i]>>8)&0xff;
  131.           irTxMsg.Data[5] = (buffer[i]>>0)&0xff;
  132.           i++;
  133.           if (i == MAX_NR_TIMES) {
  134.         i=0;
  135.           }
  136.           if(i != endindex) {
  137.         irTxMsg.Length = 8;
  138.         irTxMsg.Data[6] = (buffer[i]>>8)&0xff;
  139.         irTxMsg.Data[7] = (buffer[i]>>0)&0xff;
  140.         i++;
  141.         if (i == MAX_NR_TIMES) {
  142.           i=0;
  143.         }
  144.           }
  145.         }
  146.       }
  147.       /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  148.       while (StdCan_Put(&irTxMsg) != StdCan_Ret_OK) {}
  149.       _delay_ms(1);
  150.     }
  151. }
  152. #endif
  153.  
  154. void sns_rfTransceive_Process(void)
  155. {
  156. #if IR_RX_ENABLE==1
  157.         switch (rfRxChannel_state)
  158.         {
  159.         case sns_rfTransceive_STATE_IDLE:
  160.         {
  161.             /* If known protocol and timeout is not 0 (0 means burst) */
  162.             if (rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN && rfRxChannel_proto.timeout != 0) {
  163.                 /* Send button release command on CAN */
  164.                 rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  165.                 rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
  166.                 /* Data content is kept from last transmit (pressed) */
  167.  
  168.                 StdCan_Put(&rfTxMsg);
  169.             }
  170.             rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  171.             break;
  172.         }
  173.  
  174.         case sns_rfTransceive_STATE_START_RECEIVE:
  175.             cli();
  176.             rfRxChannel_newData = FALSE;
  177.             sei();
  178.             rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
  179.            
  180.             break;
  181.  
  182.         case sns_rfTransceive_STATE_RECEIVING:
  183.             if (rfRxChannel_newData == TRUE) {
  184.                 cli();
  185.                 rfRxChannel_newData = FALSE;
  186.                 sei();
  187.                 /* Let protocol driver parse and then send on CAN */
  188.                 uint8_t res2 = parseProtocol(rfRxChannel_buf, rfRxChannel_len, rfRxChannel_index, &rfRxChannel_proto);
  189.                 if (res2 == IR_OK && rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN)
  190.                 {
  191. //gpio_clr_pin(EXP_B);
  192.                     //send_debug(rfRxChannel_buf, rfRxChannel_len);
  193.                     /* If timeout is 0, protocol is burst protocol */
  194.                     if (rfRxChannel_proto.timeout > 0)
  195.                     {
  196.                         rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  197.                         rfRxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
  198.                     }
  199.                     else
  200.                     {
  201.                         rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST;
  202.                         rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  203.                     }
  204.                     rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
  205.                     rfTxMsg.Data[2] = (rfRxChannel_proto.data>>40)&0xff;
  206.                     rfTxMsg.Data[3] = (rfRxChannel_proto.data>>32)&0xff;
  207.                     rfTxMsg.Data[4] = (rfRxChannel_proto.data>>24)&0xff;
  208.                     rfTxMsg.Data[5] = (rfRxChannel_proto.data>>16)&0xff;
  209.                     rfTxMsg.Data[6] = (rfRxChannel_proto.data>>8)&0xff;
  210.                     rfTxMsg.Data[7] = rfRxChannel_proto.data&0xff;
  211.  
  212.                     StdCan_Put(&rfTxMsg);
  213.                 }
  214.                 else if (rfRxChannel_proto.protocol == IR_PROTO_UNKNOWN)
  215.                 {
  216. #if (sns_rfTransceive_SEND_DEBUG==1)
  217.                     //send_debug(rfRxChannel_buf, rfRxChannel_len);
  218.                     //rfRxChannel_proto.timeout=300;
  219. #endif
  220.                     rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  221.                 }
  222. #if (sns_rfTransceive_SEND_DEBUG==1)
  223.                 else if (res2 == IR_SEND_DEBUG)
  224.                 {
  225.                     send_debug(rfRxChannel_buf, rfRxChannel_proto.data & 0xFFu, rfRxChannel_index);
  226.                     rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  227.                 }
  228. #endif
  229.             }
  230.             break;
  231.  
  232.         case sns_rfTransceive_STATE_START_PAUSE:
  233.             /* set a timer so we can send release button event when no new RF is arriving */
  234.             Timer_SetTimeout(sns_rfTransceive_RX_REPEATE_TIMER, rfRxChannel_proto.timeout, TimerTypeOneShot, 0);
  235.             rfRxChannel_state = sns_rfTransceive_STATE_PAUSING;
  236.             break;
  237.  
  238.         case sns_rfTransceive_STATE_PAUSING:
  239.             /* reset timer if new IR arrived */
  240.             if (rfRxChannel_newData == TRUE) {
  241.                 cli();
  242.                 rfRxChannel_newData = FALSE;
  243.                 sei();
  244.  
  245.                 Ir_Protocol_Data_t  protoDummy;
  246.                 if (parseProtocol(rfRxChannel_buf, rfRxChannel_len, rfRxChannel_index, &protoDummy) == IR_OK) {
  247.                     if (protoDummy.protocol == rfRxChannel_proto.protocol) {
  248.                         /* re-set timer so we can send release button event when no new RF is arriving */
  249.                         Timer_SetTimeout(sns_rfTransceive_RX_REPEATE_TIMER, rfRxChannel_proto.timeout, TimerTypeOneShot, 0);
  250.                     }
  251.                 }
  252.             }
  253.  
  254.             if (Timer_Expired(sns_rfTransceive_RX_REPEATE_TIMER)) {
  255.                 rfRxChannel_state = sns_rfTransceive_STATE_IDLE;
  256.             }
  257.             break;
  258.  
  259.         default:
  260.             break;
  261.         }
  262. #endif
  263.  
  264. #if IR_TX_ENABLE==1
  265.         switch (rfTxChannel_state)
  266.         {
  267.         case sns_rfTransceive_STATE_IDLE:
  268.         {
  269.             /* transmission is started when a command is received on can */
  270.             rfTxChannel_stopSend = FALSE;
  271.             rfTxChannel_repeatCount = 0;
  272.             rfTxChannel_proto.framecnt = 0;
  273.             break;
  274.         }
  275.  
  276.         case sns_rfTransceive_STATE_START_TRANSMIT:
  277.         {
  278.             /* Expand protocol. */
  279.             if (expandProtocol(rfTxChannel_buf, &rfTxChannel_len, &rfTxChannel_proto) != IR_OK) {
  280.                 /* Failed to expand protocol -> enter idle state. */
  281.                 rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
  282.                 break;
  283.             }
  284.  
  285.             /* Start RF transmission. */
  286.             IrTransceiver_Transmit(0, rfTxChannel_buf, 0, rfTxChannel_len, rfTxChannel_proto.modfreq);
  287.  
  288.             /* Enter transmitting state. */
  289.             rfTxChannel_state = sns_rfTransceive_STATE_TRANSMITTING;
  290.             break;
  291.         }
  292.  
  293.         case sns_rfTransceive_STATE_TRANSMITTING:
  294.         {
  295.             if (rfTxChannel_sendComplete == TRUE)
  296.             {
  297.                 cli();
  298.                 rfTxChannel_sendComplete = FALSE;
  299.                 sei();
  300.  
  301.                 rfTxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
  302.             }
  303.             break;
  304.         }
  305.  
  306.         case sns_rfTransceive_STATE_START_PAUSE:
  307.         {
  308.             if (rfTxChannel_repeatCount < rfTxChannel_proto.repeats)
  309.             {
  310.                 rfTxChannel_repeatCount++;
  311.             }
  312.  
  313.             Timer_SetTimeout(sns_rfTransceive_TX_REPEATE_TIMER, rfTxChannel_proto.timeout, TimerTypeOneShot, 0);
  314.  
  315.             if (rfTxChannel_proto.framecnt != 255)
  316.             {
  317.                 rfTxChannel_proto.framecnt++;
  318.             }
  319.  
  320.             rfTxChannel_state = sns_rfTransceive_STATE_PAUSING;
  321.             break;
  322.         }
  323.  
  324.         case sns_rfTransceive_STATE_PAUSING:
  325.         {
  326.             if (Timer_Expired(sns_rfTransceive_TX_REPEATE_TIMER))
  327.             {
  328.                 rfTxChannel_state = sns_rfTransceive_STATE_START_TRANSMIT;
  329.             }
  330.  
  331.             /* Transmission is stopped when such command is recevied on can */
  332.             if (rfTxChannel_stopSend == TRUE && rfTxChannel_repeatCount >= rfTxChannel_proto.repeats)
  333.             {
  334.                 rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
  335.             }
  336.             break;
  337.         }
  338.         default:
  339.             break;
  340.         }
  341. #endif
  342. }
  343.  
  344. void sns_rfTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  345. {
  346.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  347.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  348.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_RFTRANSCEIVE &&
  349.         rxMsg->Header.ModuleId == sns_rfTransceive_ID)
  350.     {
  351.         switch (rxMsg->Header.Command)
  352.         {
  353. #if IR_TX_ENABLE==1
  354.             case CAN_MODULE_CMD_PHYSICAL_IR:
  355.             {
  356.                 if (rfTxChannel_state == sns_rfTransceive_STATE_IDLE &&
  357.                     (rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED ||
  358.                     rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST))
  359.                 {
  360.                     rfTxChannel_stopSend = (uint8_t)(rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST);
  361.  
  362.                     rfTxChannel_proto.protocol = rxMsg->Data[1];
  363.                     rfTxChannel_proto.data = rxMsg->Data[2];
  364.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  365.                     rfTxChannel_proto.data |= rxMsg->Data[3];
  366.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  367.                     rfTxChannel_proto.data |= rxMsg->Data[4];
  368.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  369.                     rfTxChannel_proto.data |= rxMsg->Data[5];
  370.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  371.                     rfTxChannel_proto.data |= rxMsg->Data[6];
  372.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  373.                     rfTxChannel_proto.data |= rxMsg->Data[7];
  374.  
  375.                     rfTxChannel_state = sns_rfTransceive_STATE_START_TRANSMIT;
  376.                 }
  377.                 else if (rfTxChannel_state != sns_rfTransceive_STATE_IDLE && rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED)
  378.                 {
  379.                     rfTxChannel_stopSend = TRUE;
  380.                 }
  381.             }
  382. #endif
  383.         }
  384.     }
  385. }
  386.  
  387. void sns_rfTransceive_List(uint8_t ModuleSequenceNumber)
  388. {
  389.     StdCan_Msg_t txMsg;
  390.  
  391.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  392.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  393.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_RFTRANSCEIVE;
  394.     txMsg.Header.ModuleId = sns_rfTransceive_ID;
  395.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  396.     txMsg.Length = 6;
  397.  
  398.     uint32_t HwId=BIOS_GetHwId();
  399.     txMsg.Data[0] = HwId&0xff;
  400.     txMsg.Data[1] = (HwId>>8)&0xff;
  401.     txMsg.Data[2] = (HwId>>16)&0xff;
  402.     txMsg.Data[3] = (HwId>>24)&0xff;
  403.  
  404.     txMsg.Data[4] = NUMBER_OF_MODULES;
  405.     txMsg.Data[5] = ModuleSequenceNumber;
  406.  
  407.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  408. }
  409.