Subversion Repositories HomeAutomation

Rev

Rev 1931 | Rev 2198 | 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. #endif
  7.  
  8. #if IR_RX_ENABLE==1
  9. uint8_t rfRxChannel_newData;
  10. uint8_t rfRxChannel_rxlen;
  11. uint8_t rfRxChannel_state;
  12. Ir_Protocol_Data_t  rfRxChannel_proto;
  13. uint16_t    rfRxChannel_buf[MAX_NR_TIMES];
  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
  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.  
  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. {
  40. #if IR_RX_ENABLE==1
  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;
  45.     rfTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
  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;
  54. #endif
  55.  
  56. #if IR_TX_ENABLE==1
  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;
  64. #endif
  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.  
  75. #if IR_RX_ENABLE==1
  76.     IrTransceiver_InitRxChannel(0, rfRxChannel_buf, sns_rfTransceive_RX_done_callback, sns_rfTransceive_RX_PCINT, sns_rfTransceive_RX_PIN);
  77.     rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
  78. #endif
  79.    
  80. #if IR_TX_ENABLE==1
  81.     IrTransceiver_InitTxChannel(0, sns_rfTransceive_TX_done_callback, sns_rfTransceive_TX_PIN);
  82.     rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
  83. #endif
  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.         {
  93.             /* If known protocol and timeout is not 0 (0 means burst) */
  94.             if (rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN && rfRxChannel_proto.timeout != 0) {
  95.                 /* Send button release command on CAN */
  96.                 rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  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 */
  126.                 uint8_t res2 = parseProtocol(rfRxChannel_buf, rfRxChannel_rxlen, &rfRxChannel_proto);
  127.                 if (res2 == IR_OK && rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN) {
  128.                     /* If timeout is 0, protocol is burst protocol */
  129.                     if (rfRxChannel_proto.timeout > 0)
  130.                     {
  131.                         rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  132.                         rfRxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
  133.                     }
  134.                     else
  135.                     {
  136.                         rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST;
  137.                         rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  138.                     }
  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)
  152.                     //send_debug(rfRxChannel_buf, rfRxChannel_rxlen);
  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;
  175.                 if (parseProtocol(rfRxChannel_buf, rfRxChannel_rxlen, &protoDummy) == IR_OK) {
  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.  
  193. #if IR_TX_ENABLE==1
  194.         switch (rfTxChannel_state)
  195.         {
  196.         case sns_rfTransceive_STATE_IDLE:
  197.         {
  198.             /* transmission is started when a command is received on can */
  199.             rfTxChannel_stopSend = FALSE;
  200.             rfTxChannel_repeatCount = 0;
  201.             rfTxChannel_proto.framecnt = 0;
  202.             break;
  203.         }
  204.  
  205.         case sns_rfTransceive_STATE_START_TRANSMIT:
  206.         {
  207.             /* Expand protocol. */
  208.             if (expandProtocol(rfTxChannel_buf, &rfTxChannel_txlen, &rfTxChannel_proto) != IR_OK) {
  209.                 /* Failed to expand protocol -> enter idle state. */
  210.                 rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
  211.                 break;
  212.             }
  213.  
  214.             /* Start RF transmission. */
  215.             IrTransceiver_Transmit(0, rfTxChannel_buf, 0, rfTxChannel_txlen, rfTxChannel_proto.modfreq);
  216.  
  217.             /* Enter transmitting state. */
  218.             rfTxChannel_state = sns_rfTransceive_STATE_TRANSMITTING;
  219.             break;
  220.         }
  221.  
  222.         case sns_rfTransceive_STATE_TRANSMITTING:
  223.         {
  224.             if (rfTxChannel_sendComplete == TRUE)
  225.             {
  226.                 cli();
  227.                 rfTxChannel_sendComplete = FALSE;
  228.                 sei();
  229.  
  230.                 rfTxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
  231.             }
  232.             break;
  233.         }
  234.  
  235.         case sns_rfTransceive_STATE_START_PAUSE:
  236.         {
  237.             if (rfTxChannel_repeatCount < rfTxChannel_proto.repeats)
  238.             {
  239.                 rfTxChannel_repeatCount++;
  240.             }
  241.  
  242.             Timer_SetTimeout(sns_rfTransceive_TX_REPEATE_TIMER, rfTxChannel_proto.timeout, TimerTypeOneShot, 0);
  243.  
  244.             if (rfTxChannel_proto.framecnt != 255)
  245.             {
  246.                 rfTxChannel_proto.framecnt++;
  247.             }
  248.  
  249.             rfTxChannel_state = sns_rfTransceive_STATE_PAUSING;
  250.             break;
  251.         }
  252.  
  253.         case sns_rfTransceive_STATE_PAUSING:
  254.         {
  255.             if (Timer_Expired(sns_rfTransceive_TX_REPEATE_TIMER))
  256.             {
  257.                 rfTxChannel_state = sns_rfTransceive_STATE_START_TRANSMIT;
  258.             }
  259.  
  260.             /* Transmission is stopped when such command is recevied on can */
  261.             if (rfTxChannel_stopSend == TRUE && rfTxChannel_repeatCount >= rfTxChannel_proto.repeats)
  262.             {
  263.                 rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
  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
  283.             case CAN_MODULE_CMD_PHYSICAL_IR:
  284.             {
  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);
  290.  
  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.                 }
  310.             }
  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.  
  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;
  332.  
  333.     txMsg.Data[4] = NUMBER_OF_MODULES;
  334.     txMsg.Data[5] = ModuleSequenceNumber;
  335.  
  336.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  337. }
  338.