Subversion Repositories HomeAutomation

Rev

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

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