Subversion Repositories HomeAutomation

Rev

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