Subversion Repositories HomeAutomation

Rev

Rev 1923 | 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. uint8_t rfRxChannel_newData;
  7. uint8_t rfRxChannel_rxlen;
  8. uint8_t rfRxChannel_state;
  9. Ir_Protocol_Data_t  rfRxChannel_proto;
  10. uint16_t    rfRxChannel_rxbuf[MAX_NR_TIMES];
  11.  
  12. uint8_t rfTxChannel_sendComplete;
  13. uint8_t rfTxChannel_state;
  14. uint8_t rfTxChannel_txlen;
  15. Ir_Protocol_Data_t  rfTxChannel_proto;
  16. uint16_t    rfTxChannel_rxbuf[MAX_NR_TIMES];
  17. uint8_t rfTxChannel_repeatCount;
  18.  
  19.  
  20. #if IR_RX_ENABLE==1
  21. void sns_rfTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
  22. {
  23.     rfRxChannel_newData = TRUE;
  24.     rfRxChannel_rxlen = len;
  25. }
  26. #endif
  27.  
  28.  
  29. #if IR_TX_ENABLE==1
  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.    
  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.    
  52.     rfTxChannel_sendComplete = FALSE;
  53.     rfTxChannel_txlen = 0;
  54.     rfTxChannel_proto.data=0;
  55.     rfTxChannel_proto.repeats=0;
  56.     rfTxChannel_proto.framecnt=0;
  57.     rfTxChannel_proto.protocol=0;
  58.     rfTxChannel_repeatCount = 0;
  59.  
  60.     IrTransceiver_Init();
  61.     /* TX-pin must be set in case transmitter is nexa */
  62.     gpio_set_out(sns_rfTransceive_TX_PIN);
  63. #if IR_TX_ACTIVE_LOW==1
  64.     gpio_set_pin(sns_rfTransceive_TX_PIN);
  65. #else
  66.     gpio_clr_pin(sns_rfTransceive_TX_PIN);
  67. #endif
  68.  
  69.     IrTransceiver_InitRxChannel(0, rfRxChannel_rxbuf, sns_rfTransceive_RX_done_callback, sns_rfTransceive_RX_PCINT, sns_rfTransceive_RX_PIN);
  70.     rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
  71.    
  72.     IrTransceiver_InitTxChannel(0, sns_rfTransceive_TX_done_callback, sns_rfTransceive_TX_PIN);
  73.     rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
  74. }
  75.  
  76. void sns_rfTransceive_Process(void)
  77. {
  78. #if IR_RX_ENABLE==1
  79.         switch (rfRxChannel_state)
  80.         {
  81.         case sns_rfTransceive_STATE_IDLE:
  82.         {
  83.             if (rfTxChannel_proto.protocol != IR_PROTO_UNKNOWN) {
  84.                 /* Send button release command on CAN */
  85.                 rfTxMsg.Header.Command = CAN_MODULE_CMD_RFTRANSCEIVE_DATASTOP;
  86.                 rfTxMsg.Data[0] = 0;
  87.                 rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
  88.                 rfTxMsg.Data[2] = (rfRxChannel_proto.data>>40)&0xff;
  89.                 rfTxMsg.Data[3] = (rfRxChannel_proto.data>>32)&0xff;
  90.                 rfTxMsg.Data[4] = (rfRxChannel_proto.data>>24)&0xff;
  91.                 rfTxMsg.Data[5] = (rfRxChannel_proto.data>>16)&0xff;
  92.                 rfTxMsg.Data[6] = (rfRxChannel_proto.data>>8)&0xff;
  93.                 rfTxMsg.Data[7] = rfRxChannel_proto.data&0xff;
  94.  
  95.                 StdCan_Put(&rfTxMsg);
  96.             }
  97.             rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  98.             break;
  99.         }
  100.  
  101.         case sns_rfTransceive_STATE_START_RECEIVE:
  102.             cli();
  103.             rfRxChannel_newData = FALSE;
  104.             sei();
  105.             rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
  106.            
  107.             break;
  108.  
  109.         case sns_rfTransceive_STATE_RECEIVING:
  110.             if (rfRxChannel_newData == TRUE) {
  111.                 cli();
  112.                 rfRxChannel_newData = FALSE;
  113.                 sei();
  114.                
  115.                 /* Let protocol driver parse and then send on CAN */
  116.                 uint8_t res2 = parseProtocol(rfRxChannel_rxbuf, rfRxChannel_rxlen, &rfRxChannel_proto);
  117.                 if (res2 == IR_OK && rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN) {
  118.                     rfTxMsg.Header.Command = CAN_MODULE_CMD_RFTRANSCEIVE_DATASTART;
  119.                     rfTxMsg.Data[0] = 0;
  120.                     rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
  121.                     rfTxMsg.Data[2] = (rfRxChannel_proto.data>>40)&0xff;
  122.                     rfTxMsg.Data[3] = (rfRxChannel_proto.data>>32)&0xff;
  123.                     rfTxMsg.Data[4] = (rfRxChannel_proto.data>>24)&0xff;
  124.                     rfTxMsg.Data[5] = (rfRxChannel_proto.data>>16)&0xff;
  125.                     rfTxMsg.Data[6] = (rfRxChannel_proto.data>>8)&0xff;
  126.                     rfTxMsg.Data[7] = rfRxChannel_proto.data&0xff;
  127.  
  128.                     StdCan_Put(&rfTxMsg);
  129.  
  130.                     rfRxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
  131.                 }
  132.                 else if (rfRxChannel_proto.protocol == IR_PROTO_UNKNOWN)
  133.                 {
  134. #if (sns_rfTransceive_SEND_DEBUG==1)
  135.                     //send_debug(rfRxChannel_rxbuf, rfRxChannel_rxlen);
  136.                     //rfRxChannel_proto.timeout=300;
  137. #endif
  138.                     rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  139.                 }
  140.             }
  141.             break;
  142.  
  143.         case sns_rfTransceive_STATE_START_PAUSE:
  144.             /* set a timer so we can send release button event when no new RF is arriving */
  145.             Timer_SetTimeout(sns_rfTransceive_RX_REPEATE_TIMER, rfRxChannel_proto.timeout, TimerTypeOneShot, 0);
  146.             rfRxChannel_state = sns_rfTransceive_STATE_PAUSING;
  147.             break;
  148.  
  149.         case sns_rfTransceive_STATE_PAUSING:
  150.             /* reset timer if new IR arrived */
  151.             if (rfRxChannel_newData == TRUE) {
  152.                 cli();
  153.                 IrTransceiver_ResetRx(0); //TODO??
  154.                 rfRxChannel_newData = FALSE;
  155.                 sei();
  156.  
  157.                 Ir_Protocol_Data_t  protoDummy;
  158.                 if (parseProtocol(rfRxChannel_rxbuf, rfRxChannel_rxlen, &protoDummy) == IR_OK) {
  159.                     if (protoDummy.protocol == rfRxChannel_proto.protocol) {
  160.                         /* re-set 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.                     }
  163.                 }
  164.             }
  165.  
  166.             if (Timer_Expired(sns_rfTransceive_RX_REPEATE_TIMER)) {
  167.                 rfRxChannel_state = sns_rfTransceive_STATE_IDLE;
  168.             }
  169.             break;
  170.  
  171.         default:
  172.             break;
  173.         }
  174. #endif
  175.  
  176. #if 0
  177. //#if IR_TX_ENABLE==1
  178.         switch (irTxChannel[channel].state)
  179.         {
  180.         case sns_irTransceive_STATE_IDLE:
  181.         {
  182.             /* transmission is started when a command is received on can */
  183.             irTxChannel[channel].stopSend = FALSE;
  184.             irTxChannel[channel].repeatCount = 0;
  185.             irTxChannel[channel].proto.framecnt = 0;
  186.             irTxChannel[channel].sendingPronto = FALSE;
  187.             break;
  188.         }
  189.  
  190.         case sns_irTransceive_STATE_START_TRANSMIT:
  191.         {
  192.             /* Expand protocol. */
  193.             if (expandProtocol(irTxChannel[channel].txbuf, &irTxChannel[channel].txlen, &irTxChannel[channel].proto) != IR_OK) {
  194.                 /* Failed to expand protocol -> enter idle state. */
  195.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  196.                 break;
  197.             }
  198.  
  199.             /* Start IR transmission. */
  200.             /* TODO Send respone. Function call may fail when different modulation frequencies are used simultaneously. */
  201.             IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].txlen, irTxChannel[channel].proto.modfreq);
  202.  
  203.             /* Enter transmitting state. */
  204.             irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  205.             break;
  206.         }
  207.  
  208.         case sns_irTransceive_STATE_START_TRANSMIT_PRONTO:
  209.         {
  210.             /* Start IR transmission. */
  211.             if(IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].onceSeqLen - 1, irTxChannel[channel].proto.modfreq) < 0) {
  212.                 /* Send error code. */
  213.                 pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ABORTED);
  214.  
  215.                 /* Enter idle state. */
  216.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  217.                 break;
  218.             }
  219.  
  220.             /* Enter transmitting state. */
  221.             irTxChannel[channel].sendingPronto = TRUE;
  222.             irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  223.             break;
  224.         }
  225.  
  226.         case sns_irTransceive_STATE_PRONTO_REPEAT:
  227.         {
  228.             /* Check if done. */
  229.             if ((irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats && irTxChannel[channel].proto.repeats != 0xFF) || irTxChannel[channel].stopSend) {
  230.                 /* Pronto transmission was stopped/completed */
  231.                 pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_STOPPED);
  232.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  233.                 break;
  234.             }
  235.  
  236.             if (irTxChannel[channel].repeatCount < 255) {
  237.                 irTxChannel[channel].repeatCount++;
  238.             }
  239.  
  240.             /* Repeat sequence exists? */
  241.             if (irTxChannel[channel].repSeqLen != 0) {
  242.                 /* Use repeat seq */
  243.                 uint16_t offset = ((uint16_t)irTxChannel[channel].onceSeqLen);
  244.                 /* Don't transmit last passive. last passive handled by timer */
  245.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, offset, ((uint16_t)irTxChannel[channel].repSeqLen) - 1, irTxChannel[channel].proto.modfreq);
  246.             }
  247.             else {
  248.                 /* Use once seq */
  249.                 /* Don't transmit last passive. last passive handled by timer */
  250.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].txlen - 1, irTxChannel[channel].proto.modfreq);
  251.             }
  252.             irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  253.         }
  254.  
  255.         case sns_irTransceive_STATE_TRANSMITTING:
  256.         {
  257.             if (irTxChannel[channel].sendComplete == TRUE)
  258.             {
  259.                 cli();
  260.                 irTxChannel[channel].sendComplete = FALSE;
  261.                 sei();
  262.  
  263.                 if (irTxChannel[channel].sendingPronto)
  264.                 {
  265.                     /* First repeat, or no repeat sequence defined => use last passive time in once seq */
  266.                     if(irTxChannel[channel].repeatCount == 0 || irTxChannel[channel].repSeqLen == 0)
  267.                     {
  268.                         /* Use last passive time as timeout */
  269.                         uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].onceSeqLen - 1] / 1000;
  270.                         Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
  271.                     }
  272.                     /* Second, or later repeat => use last passive time in repeat seq */
  273.                     else
  274.                     {
  275.                         /* Use last passive time as timeout */
  276.                         uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].txlen - 1]  / 1000;
  277.                         Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
  278.                     }
  279.                     irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  280.                 }
  281.                 else
  282.                 {
  283.                     irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  284.                 }
  285.             }
  286.             break;
  287.         }
  288.  
  289.         case sns_irTransceive_STATE_START_PAUSE:
  290.         {
  291.             if (irTxChannel[channel].repeatCount < irTxChannel[channel].proto.repeats)
  292.             {
  293.                 irTxChannel[channel].repeatCount++;
  294.             }
  295.  
  296.             Timer_SetTimeout(irTxChannel[channel].timerNum, irTxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  297.  
  298.             if (irTxChannel[channel].proto.framecnt != 255)
  299.             {
  300.                 irTxChannel[channel].proto.framecnt++;
  301.             }
  302.  
  303.             irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  304.             break;
  305.         }
  306.  
  307.         case sns_irTransceive_STATE_PAUSING:
  308.         {
  309.             if (irTxChannel[channel].sendingPronto)
  310.             {
  311.                 if (Timer_Expired(irTxChannel[channel].timerNum))
  312.                 {
  313.                     irTxChannel[channel].state = sns_irTransceive_STATE_PRONTO_REPEAT;
  314.                 }
  315.                 break;
  316.             }
  317.  
  318.             if (Timer_Expired(irTxChannel[channel].timerNum))
  319.             {
  320.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  321.             }
  322.  
  323.             /* Transmission is stopped when such command is recevied on can */
  324.             if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
  325.             {
  326.                 /* Non-pronto transmission was stopped/completed */
  327.                 pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_STOPPED);
  328.  
  329.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  330.             }
  331.             break;
  332.         }
  333.         default:
  334.             break;
  335.         }
  336. #endif
  337. }
  338.  
  339. void sns_rfTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  340. {
  341.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  342.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  343.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_RFTRANSCEIVE &&
  344.         rxMsg->Header.ModuleId == sns_rfTransceive_ID)
  345.     {
  346.         switch (rxMsg->Header.Command)
  347.         {
  348. #if IR_TX_ENABLE==1
  349.         case CAN_MODULE_CMD_RFTRANSCEIVE_DATASTART:
  350.         {
  351.         }
  352.         break;
  353.         case CAN_MODULE_CMD_RFTRANSCEIVE_DATASTOP:
  354.         {
  355.         }
  356.         break;
  357.         case CAN_MODULE_CMD_RFTRANSCEIVE_DATABURST:
  358.         {
  359.         }
  360.         break;
  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.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  378.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  379.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  380.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  381.  
  382.     txMsg.Data[4] = NUMBER_OF_MODULES;
  383.     txMsg.Data[5] = ModuleSequenceNumber;
  384.  
  385.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  386. }
  387.