Subversion Repositories HomeAutomation

Rev

Rev 1996 | 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.                 /* Data content is kept from last transmit (pressed) */
  99.  
  100.                 StdCan_Put(&rfTxMsg);
  101.             }
  102.             rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  103.             break;
  104.         }
  105.  
  106.         case sns_rfTransceive_STATE_START_RECEIVE:
  107.             cli();
  108.             rfRxChannel_newData = FALSE;
  109.             sei();
  110.             rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
  111.            
  112.             break;
  113.  
  114.         case sns_rfTransceive_STATE_RECEIVING:
  115.             if (rfRxChannel_newData == TRUE) {
  116.                 cli();
  117.                 rfRxChannel_newData = FALSE;
  118.                 sei();
  119.                
  120.                 /* Let protocol driver parse and then send on CAN */
  121.                 uint8_t res2 = parseProtocol(rfRxChannel_buf, rfRxChannel_rxlen, &rfRxChannel_proto);
  122.                 if (res2 == IR_OK && rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN) {
  123.                     /* If timeout is 0, protocol is burst protocol */
  124.                     if (rfRxChannel_proto.timeout > 0)
  125.                     {
  126.                         rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  127.                         rfRxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
  128.                     }
  129.                     else
  130.                     {
  131.                         rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST;
  132.                         rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  133.                     }
  134.                     rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
  135.                     rfTxMsg.Data[2] = (rfRxChannel_proto.data>>40)&0xff;
  136.                     rfTxMsg.Data[3] = (rfRxChannel_proto.data>>32)&0xff;
  137.                     rfTxMsg.Data[4] = (rfRxChannel_proto.data>>24)&0xff;
  138.                     rfTxMsg.Data[5] = (rfRxChannel_proto.data>>16)&0xff;
  139.                     rfTxMsg.Data[6] = (rfRxChannel_proto.data>>8)&0xff;
  140.                     rfTxMsg.Data[7] = rfRxChannel_proto.data&0xff;
  141.  
  142.                     StdCan_Put(&rfTxMsg);
  143.                 }
  144.                 else if (rfRxChannel_proto.protocol == IR_PROTO_UNKNOWN)
  145.                 {
  146. #if (sns_rfTransceive_SEND_DEBUG==1)
  147.                     //send_debug(rfRxChannel_buf, rfRxChannel_rxlen);
  148.                     //rfRxChannel_proto.timeout=300;
  149. #endif
  150.                     rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  151.                 }
  152.             }
  153.             break;
  154.  
  155.         case sns_rfTransceive_STATE_START_PAUSE:
  156.             /* set a timer so we can send release button event when no new RF is arriving */
  157.             Timer_SetTimeout(sns_rfTransceive_RX_REPEATE_TIMER, rfRxChannel_proto.timeout, TimerTypeOneShot, 0);
  158.             rfRxChannel_state = sns_rfTransceive_STATE_PAUSING;
  159.             break;
  160.  
  161.         case sns_rfTransceive_STATE_PAUSING:
  162.             /* reset timer if new IR arrived */
  163.             if (rfRxChannel_newData == TRUE) {
  164.                 cli();
  165.                 IrTransceiver_ResetRx(0); //TODO??
  166.                 rfRxChannel_newData = FALSE;
  167.                 sei();
  168.  
  169.                 Ir_Protocol_Data_t  protoDummy;
  170.                 if (parseProtocol(rfRxChannel_buf, rfRxChannel_rxlen, &protoDummy) == IR_OK) {
  171.                     if (protoDummy.protocol == rfRxChannel_proto.protocol) {
  172.                         /* re-set timer so we can send release button event when no new RF is arriving */
  173.                         Timer_SetTimeout(sns_rfTransceive_RX_REPEATE_TIMER, rfRxChannel_proto.timeout, TimerTypeOneShot, 0);
  174.                     }
  175.                 }
  176.             }
  177.  
  178.             if (Timer_Expired(sns_rfTransceive_RX_REPEATE_TIMER)) {
  179.                 rfRxChannel_state = sns_rfTransceive_STATE_IDLE;
  180.             }
  181.             break;
  182.  
  183.         default:
  184.             break;
  185.         }
  186. #endif
  187.  
  188. #if IR_TX_ENABLE==1
  189.         switch (rfTxChannel_state)
  190.         {
  191.         case sns_rfTransceive_STATE_IDLE:
  192.         {
  193.             /* transmission is started when a command is received on can */
  194.             rfTxChannel_stopSend = FALSE;
  195.             rfTxChannel_repeatCount = 0;
  196.             rfTxChannel_proto.framecnt = 0;
  197.             break;
  198.         }
  199.  
  200.         case sns_rfTransceive_STATE_START_TRANSMIT:
  201.         {
  202.             /* Expand protocol. */
  203.             if (expandProtocol(rfTxChannel_buf, &rfTxChannel_txlen, &rfTxChannel_proto) != IR_OK) {
  204.                 /* Failed to expand protocol -> enter idle state. */
  205.                 rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
  206.                 break;
  207.             }
  208.  
  209.             /* Start RF transmission. */
  210.             IrTransceiver_Transmit(0, rfTxChannel_buf, 0, rfTxChannel_txlen, rfTxChannel_proto.modfreq);
  211.  
  212.             /* Enter transmitting state. */
  213.             rfTxChannel_state = sns_rfTransceive_STATE_TRANSMITTING;
  214.             break;
  215.         }
  216.  
  217.         case sns_rfTransceive_STATE_TRANSMITTING:
  218.         {
  219.             if (rfTxChannel_sendComplete == TRUE)
  220.             {
  221.                 cli();
  222.                 rfTxChannel_sendComplete = FALSE;
  223.                 sei();
  224.  
  225.                 rfTxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
  226.             }
  227.             break;
  228.         }
  229.  
  230.         case sns_rfTransceive_STATE_START_PAUSE:
  231.         {
  232.             if (rfTxChannel_repeatCount < rfTxChannel_proto.repeats)
  233.             {
  234.                 rfTxChannel_repeatCount++;
  235.             }
  236.  
  237.             Timer_SetTimeout(sns_rfTransceive_TX_REPEATE_TIMER, rfTxChannel_proto.timeout, TimerTypeOneShot, 0);
  238.  
  239.             if (rfTxChannel_proto.framecnt != 255)
  240.             {
  241.                 rfTxChannel_proto.framecnt++;
  242.             }
  243.  
  244.             rfTxChannel_state = sns_rfTransceive_STATE_PAUSING;
  245.             break;
  246.         }
  247.  
  248.         case sns_rfTransceive_STATE_PAUSING:
  249.         {
  250.             if (Timer_Expired(sns_rfTransceive_TX_REPEATE_TIMER))
  251.             {
  252.                 rfTxChannel_state = sns_rfTransceive_STATE_START_TRANSMIT;
  253.             }
  254.  
  255.             /* Transmission is stopped when such command is recevied on can */
  256.             if (rfTxChannel_stopSend == TRUE && rfTxChannel_repeatCount >= rfTxChannel_proto.repeats)
  257.             {
  258.                 rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
  259.             }
  260.             break;
  261.         }
  262.         default:
  263.             break;
  264.         }
  265. #endif
  266. }
  267.  
  268. void sns_rfTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  269. {
  270.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  271.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  272.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_RFTRANSCEIVE &&
  273.         rxMsg->Header.ModuleId == sns_rfTransceive_ID)
  274.     {
  275.         switch (rxMsg->Header.Command)
  276.         {
  277. #if IR_TX_ENABLE==1
  278.             case CAN_MODULE_CMD_PHYSICAL_IR:
  279.             {
  280.                 if (rfTxChannel_state == sns_rfTransceive_STATE_IDLE &&
  281.                     (rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED ||
  282.                     rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST))
  283.                 {
  284.                     rfTxChannel_stopSend = (uint8_t)(rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST);
  285.  
  286.                     rfTxChannel_proto.protocol = rxMsg->Data[1];
  287.                     rfTxChannel_proto.data = rxMsg->Data[2];
  288.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  289.                     rfTxChannel_proto.data |= rxMsg->Data[3];
  290.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  291.                     rfTxChannel_proto.data |= rxMsg->Data[4];
  292.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  293.                     rfTxChannel_proto.data |= rxMsg->Data[5];
  294.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  295.                     rfTxChannel_proto.data |= rxMsg->Data[6];
  296.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  297.                     rfTxChannel_proto.data |= rxMsg->Data[7];
  298.  
  299.                     rfTxChannel_state = sns_rfTransceive_STATE_START_TRANSMIT;
  300.                 }
  301.                 else if (rfTxChannel_state != sns_rfTransceive_STATE_IDLE && rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED)
  302.                 {
  303.                     rfTxChannel_stopSend = TRUE;
  304.                 }
  305.             }
  306. #endif
  307.         }
  308.     }
  309. }
  310.  
  311. void sns_rfTransceive_List(uint8_t ModuleSequenceNumber)
  312. {
  313.     StdCan_Msg_t txMsg;
  314.  
  315.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  316.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  317.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_RFTRANSCEIVE;
  318.     txMsg.Header.ModuleId = sns_rfTransceive_ID;
  319.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  320.     txMsg.Length = 6;
  321.  
  322.     uint32_t HwId=BIOS_GetHwId();
  323.     txMsg.Data[0] = HwId&0xff;
  324.     txMsg.Data[1] = (HwId>>8)&0xff;
  325.     txMsg.Data[2] = (HwId>>16)&0xff;
  326.     txMsg.Data[3] = (HwId>>24)&0xff;
  327.  
  328.     txMsg.Data[4] = NUMBER_OF_MODULES;
  329.     txMsg.Data[5] = ModuleSequenceNumber;
  330.  
  331.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  332. }
  333.