Subversion Repositories HomeAutomation

Rev

Rev 2040 | Rev 2199 | 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. uint16_t debugCnt1 = 0;
  16. uint16_t debugCnt2 = 0;
  17. uint16_t debugCnt3 = 5;
  18.  
  19. void sns_rfTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len, uint8_t index)
  20. {
  21. #if IR_RX_CONTINUOUS_MODE==0
  22.     rfRxChannel_newData = TRUE;
  23.     rfRxChannel_rxlen = len;
  24. #else
  25. //gpio_toggle_pin(EXP_B);
  26.     if (len > sns_rfTransceive_MIN_NUM_PULSES)
  27.     {
  28.         rfRxChannel_newData = TRUE;
  29.         rfRxChannel_rxlen = len;
  30.        
  31. ///////////// DEBUG!!!!
  32. //gpio_set_pin(EXP_B);
  33.  
  34. //debugCnt1 +=1;
  35.     }
  36. //debugCnt2 +=1;
  37.     //check if len is > something reasonable
  38.     //let process function parse protocols
  39. #endif
  40. }
  41. #endif
  42.  
  43.  
  44. #if IR_TX_ENABLE==1
  45. uint8_t rfTxChannel_sendComplete;
  46. uint8_t rfTxChannel_state;
  47. uint8_t rfTxChannel_txlen;
  48. Ir_Protocol_Data_t  rfTxChannel_proto;
  49. uint16_t    rfTxChannel_buf[MAX_NR_TIMES];
  50. uint8_t rfTxChannel_repeatCount;
  51. uint8_t rfTxChannel_stopSend;
  52.  
  53. void sns_rfTransceive_TX_done_callback(uint8_t channel)
  54. {
  55.     rfTxChannel_sendComplete = TRUE;
  56. }
  57. #endif
  58.  
  59. void sns_rfTransceive_Init(void)
  60. {
  61. #if IR_RX_ENABLE==1
  62.     StdCan_Set_class(rfTxMsg.Header, CAN_MODULE_CLASS_SNS);
  63.     StdCan_Set_direction(rfTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  64.     rfTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_RFTRANSCEIVE;
  65.     rfTxMsg.Header.ModuleId = sns_rfTransceive_ID;
  66.     rfTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
  67.     rfTxMsg.Length = 8;
  68.    
  69.     rfRxChannel_newData = FALSE;
  70.     rfRxChannel_rxlen = 0;
  71.     rfRxChannel_proto.timeout=0;
  72.     rfRxChannel_proto.data=0;
  73.     rfRxChannel_proto.repeats=0;
  74.     rfRxChannel_proto.protocol=0;
  75. #endif
  76.  
  77. #if IR_TX_ENABLE==1
  78.     rfTxChannel_sendComplete = FALSE;
  79.     rfTxChannel_txlen = 0;
  80.     rfTxChannel_proto.data=0;
  81.     rfTxChannel_proto.repeats=0;
  82.     rfTxChannel_proto.framecnt=0;
  83.     rfTxChannel_proto.protocol=0;
  84.     rfTxChannel_repeatCount = 0;
  85. #endif
  86.  
  87.     IrTransceiver_Init();
  88.     /* TX-pin must be set in case transmitter is nexa */
  89.     gpio_set_out(sns_rfTransceive_TX_PIN);
  90. #if IR_TX_ACTIVE_LOW==1
  91.     gpio_set_pin(sns_rfTransceive_TX_PIN);
  92. #else
  93.     gpio_clr_pin(sns_rfTransceive_TX_PIN);
  94. #endif
  95.  
  96. #if IR_RX_ENABLE==1
  97.     IrTransceiver_InitRxChannel(0, rfRxChannel_buf, sns_rfTransceive_RX_done_callback, sns_rfTransceive_RX_PCINT, sns_rfTransceive_RX_PIN);
  98.     rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
  99. #endif
  100.    
  101. #if IR_TX_ENABLE==1
  102.     IrTransceiver_InitTxChannel(0, sns_rfTransceive_TX_done_callback, sns_rfTransceive_TX_PIN);
  103.     rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
  104. #endif
  105.  
  106. ///////////// DEBUG!!!!
  107. gpio_set_out(EXP_A);
  108. }
  109.  
  110.  
  111. ///////////// DEBUG!!!!
  112. #if 0
  113. StdCan_Msg_t irTxMsg;
  114. void send_debug(uint16_t *buffer, uint8_t len) {
  115.  
  116.     /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */
  117.  
  118.     StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
  119.     StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  120.     irTxMsg.Length = 8;
  121.     irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRRECEIVE;
  122.     irTxMsg.Header.ModuleId = 0;
  123.     irTxMsg.Header.Command = CAN_MODULE_CMD_IRRECEIVE_IRRAW;
  124.     for (uint8_t i = 0; i < len>>2; i++) {
  125.         uint8_t index = i<<2;
  126.  
  127.         irTxMsg.Data[0] = (buffer[index]>>8)&0xff;
  128.         irTxMsg.Data[1] = (buffer[index]>>0)&0xff;
  129.         irTxMsg.Data[2] = (buffer[index+1]>>8)&0xff;
  130.         irTxMsg.Data[3] = (buffer[index+1]>>0)&0xff;
  131.         irTxMsg.Data[4] = (buffer[index+2]>>8)&0xff;
  132.         irTxMsg.Data[5] = (buffer[index+2]>>0)&0xff;
  133.         irTxMsg.Data[6] = (buffer[index+3]>>8)&0xff;
  134.         irTxMsg.Data[7] = (buffer[index+3]>>0)&0xff;
  135.                
  136.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  137.         while (StdCan_Put(&irTxMsg) != StdCan_Ret_OK) {}
  138.         _delay_ms(1);
  139.     }
  140.    
  141.     uint8_t lastpacketcnt = len&0x03;
  142.     if (lastpacketcnt > 0) {
  143.         irTxMsg.Length = lastpacketcnt<<1;
  144.         for (uint8_t i = 0; i < lastpacketcnt; i++) {
  145.             irTxMsg.Data[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff;
  146.             irTxMsg.Data[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff;
  147.         }
  148.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  149.         while (StdCan_Put(&irTxMsg) != StdCan_Ret_OK) {}
  150.         _delay_ms(1);
  151.     }
  152.  
  153. }
  154. #endif
  155.  
  156. void sns_rfTransceive_Process(void)
  157. {
  158. #if IR_RX_ENABLE==1
  159.         switch (rfRxChannel_state)
  160.         {
  161.         case sns_rfTransceive_STATE_IDLE:
  162.         {
  163.             /* If known protocol and timeout is not 0 (0 means burst) */
  164.             if (rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN && rfRxChannel_proto.timeout != 0) {
  165.                 /* Send button release command on CAN */
  166.                 rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  167.                 rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
  168.                 /* Data content is kept from last transmit (pressed) */
  169.  
  170.                 StdCan_Put(&rfTxMsg);
  171.             }
  172.             rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  173.             break;
  174.         }
  175.  
  176.         case sns_rfTransceive_STATE_START_RECEIVE:
  177.             cli();
  178.             rfRxChannel_newData = FALSE;
  179.             sei();
  180.             rfRxChannel_state = sns_rfTransceive_STATE_RECEIVING;
  181.            
  182.             break;
  183.  
  184.         case sns_rfTransceive_STATE_RECEIVING:
  185.             if (rfRxChannel_newData == TRUE) {
  186.                 cli();
  187.                 rfRxChannel_newData = FALSE;
  188.                 sei();
  189. /*
  190. debugCnt3 +=1;
  191. if (debugCnt3>5)
  192. {
  193. printf("1\n");
  194. debugCnt3=0;
  195. }
  196. */
  197.                 /* Let protocol driver parse and then send on CAN */
  198.                 uint8_t res2 = parseProtocol(rfRxChannel_buf, rfRxChannel_rxlen, &rfRxChannel_proto);
  199.                 if (res2 == IR_OK && rfRxChannel_proto.protocol != IR_PROTO_UNKNOWN) {
  200. gpio_clr_pin(EXP_B);
  201. //send_debug(rfRxChannel_buf, rfRxChannel_rxlen);
  202.                     /* If timeout is 0, protocol is burst protocol */
  203.                     if (rfRxChannel_proto.timeout > 0)
  204.                     {
  205.                         rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  206.                         rfRxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
  207.                     }
  208.                     else
  209.                     {
  210.                         rfTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST;
  211.                         rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  212.                     }
  213.                     rfTxMsg.Data[1] = rfRxChannel_proto.protocol;
  214.                     rfTxMsg.Data[2] = (rfRxChannel_proto.data>>40)&0xff;
  215.                     rfTxMsg.Data[3] = (rfRxChannel_proto.data>>32)&0xff;
  216.                     rfTxMsg.Data[4] = (rfRxChannel_proto.data>>24)&0xff;
  217.                     rfTxMsg.Data[5] = (rfRxChannel_proto.data>>16)&0xff;
  218.                     rfTxMsg.Data[6] = (rfRxChannel_proto.data>>8)&0xff;
  219.                     rfTxMsg.Data[7] = rfRxChannel_proto.data&0xff;
  220.  
  221.                     StdCan_Put(&rfTxMsg);
  222.                 }
  223.                 else if (rfRxChannel_proto.protocol == IR_PROTO_UNKNOWN)
  224.                 {
  225. #if (sns_rfTransceive_SEND_DEBUG==1)
  226.                     //send_debug(rfRxChannel_buf, rfRxChannel_rxlen);
  227.                     //rfRxChannel_proto.timeout=300;
  228. #endif
  229.                     rfRxChannel_state = sns_rfTransceive_STATE_START_RECEIVE;
  230.                 }
  231.             }
  232.             break;
  233.  
  234.         case sns_rfTransceive_STATE_START_PAUSE:
  235.             /* set a timer so we can send release button event when no new RF is arriving */
  236.             Timer_SetTimeout(sns_rfTransceive_RX_REPEATE_TIMER, rfRxChannel_proto.timeout, TimerTypeOneShot, 0);
  237.             rfRxChannel_state = sns_rfTransceive_STATE_PAUSING;
  238.             break;
  239.  
  240.         case sns_rfTransceive_STATE_PAUSING:
  241.             /* reset timer if new IR arrived */
  242.             if (rfRxChannel_newData == TRUE) {
  243.                 cli();
  244.                 IrTransceiver_ResetRx(0); //TODO??
  245.                 rfRxChannel_newData = FALSE;
  246.                 sei();
  247.  
  248.                 Ir_Protocol_Data_t  protoDummy;
  249.                 if (parseProtocol(rfRxChannel_buf, rfRxChannel_rxlen, &protoDummy) == IR_OK) {
  250.                     if (protoDummy.protocol == rfRxChannel_proto.protocol) {
  251.                         /* re-set timer so we can send release button event when no new RF is arriving */
  252.                         Timer_SetTimeout(sns_rfTransceive_RX_REPEATE_TIMER, rfRxChannel_proto.timeout, TimerTypeOneShot, 0);
  253.                     }
  254.                 }
  255.             }
  256.  
  257.             if (Timer_Expired(sns_rfTransceive_RX_REPEATE_TIMER)) {
  258.                 rfRxChannel_state = sns_rfTransceive_STATE_IDLE;
  259.             }
  260.             break;
  261.  
  262.         default:
  263.             break;
  264.         }
  265. #endif
  266.  
  267. #if IR_TX_ENABLE==1
  268.         switch (rfTxChannel_state)
  269.         {
  270.         case sns_rfTransceive_STATE_IDLE:
  271.         {
  272.             /* transmission is started when a command is received on can */
  273.             rfTxChannel_stopSend = FALSE;
  274.             rfTxChannel_repeatCount = 0;
  275.             rfTxChannel_proto.framecnt = 0;
  276.             break;
  277.         }
  278.  
  279.         case sns_rfTransceive_STATE_START_TRANSMIT:
  280.         {
  281.             /* Expand protocol. */
  282.             if (expandProtocol(rfTxChannel_buf, &rfTxChannel_txlen, &rfTxChannel_proto) != IR_OK) {
  283.                 /* Failed to expand protocol -> enter idle state. */
  284.                 rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
  285.                 break;
  286.             }
  287.  
  288.             /* Start RF transmission. */
  289.             IrTransceiver_Transmit(0, rfTxChannel_buf, 0, rfTxChannel_txlen, rfTxChannel_proto.modfreq);
  290.  
  291.             /* Enter transmitting state. */
  292.             rfTxChannel_state = sns_rfTransceive_STATE_TRANSMITTING;
  293.             break;
  294.         }
  295.  
  296.         case sns_rfTransceive_STATE_TRANSMITTING:
  297.         {
  298.             if (rfTxChannel_sendComplete == TRUE)
  299.             {
  300.                 cli();
  301.                 rfTxChannel_sendComplete = FALSE;
  302.                 sei();
  303.  
  304.                 rfTxChannel_state = sns_rfTransceive_STATE_START_PAUSE;
  305.             }
  306.             break;
  307.         }
  308.  
  309.         case sns_rfTransceive_STATE_START_PAUSE:
  310.         {
  311.             if (rfTxChannel_repeatCount < rfTxChannel_proto.repeats)
  312.             {
  313.                 rfTxChannel_repeatCount++;
  314.             }
  315.  
  316.             Timer_SetTimeout(sns_rfTransceive_TX_REPEATE_TIMER, rfTxChannel_proto.timeout, TimerTypeOneShot, 0);
  317.  
  318.             if (rfTxChannel_proto.framecnt != 255)
  319.             {
  320.                 rfTxChannel_proto.framecnt++;
  321.             }
  322.  
  323.             rfTxChannel_state = sns_rfTransceive_STATE_PAUSING;
  324.             break;
  325.         }
  326.  
  327.         case sns_rfTransceive_STATE_PAUSING:
  328.         {
  329.             if (Timer_Expired(sns_rfTransceive_TX_REPEATE_TIMER))
  330.             {
  331.                 rfTxChannel_state = sns_rfTransceive_STATE_START_TRANSMIT;
  332.             }
  333.  
  334.             /* Transmission is stopped when such command is recevied on can */
  335.             if (rfTxChannel_stopSend == TRUE && rfTxChannel_repeatCount >= rfTxChannel_proto.repeats)
  336.             {
  337.                 rfTxChannel_state = sns_rfTransceive_STATE_IDLE;
  338.             }
  339.             break;
  340.         }
  341.         default:
  342.             break;
  343.         }
  344. #endif
  345. }
  346.  
  347. void sns_rfTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  348. {
  349.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  350.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  351.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_RFTRANSCEIVE &&
  352.         rxMsg->Header.ModuleId == sns_rfTransceive_ID)
  353.     {
  354.         switch (rxMsg->Header.Command)
  355.         {
  356. #if IR_TX_ENABLE==1
  357.             case CAN_MODULE_CMD_PHYSICAL_IR:
  358.             {
  359.                 if (rfTxChannel_state == sns_rfTransceive_STATE_IDLE &&
  360.                     (rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED ||
  361.                     rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST))
  362.                 {
  363.                     rfTxChannel_stopSend = (uint8_t)(rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_BURST);
  364.  
  365.                     rfTxChannel_proto.protocol = rxMsg->Data[1];
  366.                     rfTxChannel_proto.data = rxMsg->Data[2];
  367.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  368.                     rfTxChannel_proto.data |= rxMsg->Data[3];
  369.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  370.                     rfTxChannel_proto.data |= rxMsg->Data[4];
  371.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  372.                     rfTxChannel_proto.data |= rxMsg->Data[5];
  373.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  374.                     rfTxChannel_proto.data |= rxMsg->Data[6];
  375.                     rfTxChannel_proto.data = rfTxChannel_proto.data<<8;
  376.                     rfTxChannel_proto.data |= rxMsg->Data[7];
  377.  
  378.                     rfTxChannel_state = sns_rfTransceive_STATE_START_TRANSMIT;
  379.                 }
  380.                 else if (rfTxChannel_state != sns_rfTransceive_STATE_IDLE && rxMsg->Data[0] == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED)
  381.                 {
  382.                     rfTxChannel_stopSend = TRUE;
  383.                 }
  384.             }
  385. #endif
  386.         }
  387.     }
  388. }
  389.  
  390. void sns_rfTransceive_List(uint8_t ModuleSequenceNumber)
  391. {
  392.     StdCan_Msg_t txMsg;
  393.  
  394.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  395.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  396.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_RFTRANSCEIVE;
  397.     txMsg.Header.ModuleId = sns_rfTransceive_ID;
  398.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  399.     txMsg.Length = 6;
  400.  
  401.     uint32_t HwId=BIOS_GetHwId();
  402.     txMsg.Data[0] = HwId&0xff;
  403.     txMsg.Data[1] = (HwId>>8)&0xff;
  404.     txMsg.Data[2] = (HwId>>16)&0xff;
  405.     txMsg.Data[3] = (HwId>>24)&0xff;
  406.  
  407.     txMsg.Data[4] = NUMBER_OF_MODULES;
  408.     txMsg.Data[5] = ModuleSequenceNumber;
  409.  
  410.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  411. }
  412.