Subversion Repositories HomeAutomation

Rev

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