Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "sns_irTransceive.h"
  3.  
  4. #ifdef sns_irTransceive_USEEEPROM
  5. #include "sns_irTransceive_eeprom.h"
  6. struct eeprom_sns_irTransceive EEMEM eeprom_sns_irTransceive =
  7. {
  8.     {
  9.         /* Define initialization values on the EEPROM variables here.
  10.         This will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips.
  11.         Write the values in the exact same order as the struct is defined in the *.h file. */
  12.         CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO,
  13.         0,
  14.         CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO,
  15.         0,
  16.         CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO,
  17.         0,
  18.     },
  19.     0   /* crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts */
  20. };
  21. #endif
  22.  
  23. #if IR_RX_ENABLE==1
  24. struct {
  25.     uint8_t             state;
  26.     uint16_t            *rxbuf;
  27.     uint8_t             rxlen;
  28.     uint8_t             timerNum;
  29.     Ir_Protocol_Data_t  proto;
  30.     uint8_t             newData;
  31. } irRxChannel[IR_SUPPORTED_NUM_CHANNELS];
  32. #endif
  33.  
  34. #if IR_TX_ENABLE==1
  35. struct {
  36.     uint8_t             state;
  37.     uint16_t            *txbuf;
  38.     uint8_t             txlen;
  39.     uint8_t             timerNum;
  40.     uint8_t             repeatCount;
  41.     Ir_Protocol_Data_t  proto;
  42.     uint8_t             stopSend;
  43.     uint8_t             sendComplete;
  44. } irTxChannel[IR_SUPPORTED_NUM_CHANNELS];
  45. #endif
  46.  
  47. uint16_t    buf[IR_SUPPORTED_NUM_CHANNELS][MAX_NR_TIMES];
  48.  
  49. StdCan_Msg_t        irTxMsg;
  50.  
  51. #if (sns_irTransceive_SEND_DEBUG==1)
  52. void send_debug(uint16_t *buffer, uint8_t len) {
  53.     StdCan_Msg_t dbgIrTxMsg;
  54.     /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */
  55.  
  56.     StdCan_Set_class(dbgIrTxMsg.Header, CAN_MODULE_CLASS_SNS);
  57.     StdCan_Set_direction(dbgIrTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  58.     dbgIrTxMsg.Length = 8;
  59.     dbgIrTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  60.     dbgIrTxMsg.Header.ModuleId = sns_irTransceive_ID;
  61.     dbgIrTxMsg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRRAW;
  62.     for (uint8_t i = 0; i < len>>2; i++) {
  63.         uint8_t index = i<<2;
  64.  
  65.         dbgIrTxMsg.Data[0] = (buffer[index]>>8)&0xff;
  66.         dbgIrTxMsg.Data[1] = (buffer[index]>>0)&0xff;
  67.         dbgIrTxMsg.Data[2] = (buffer[index+1]>>8)&0xff;
  68.         dbgIrTxMsg.Data[3] = (buffer[index+1]>>0)&0xff;
  69.         dbgIrTxMsg.Data[4] = (buffer[index+2]>>8)&0xff;
  70.         dbgIrTxMsg.Data[5] = (buffer[index+2]>>0)&0xff;
  71.         dbgIrTxMsg.Data[6] = (buffer[index+3]>>8)&0xff;
  72.         dbgIrTxMsg.Data[7] = (buffer[index+3]>>0)&0xff;
  73.                
  74.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  75.         while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
  76.         _delay_ms(1);
  77.     }
  78.    
  79.     uint8_t lastpacketcnt = len&0x03;
  80.     if (lastpacketcnt > 0) {
  81.         dbgIrTxMsg.Length = lastpacketcnt<<1;
  82.         for (uint8_t i = 0; i < lastpacketcnt; i++) {
  83.             dbgIrTxMsg.Data[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff;
  84.             dbgIrTxMsg.Data[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff;
  85.         }
  86.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  87.         while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
  88.         _delay_ms(1);
  89.     }
  90.  
  91. }
  92. #endif
  93.  
  94. #ifndef sns_irTransceive_PRONTO_SUPPORT
  95. #define sns_irTransceive_PRONTO_SUPPORT 0
  96. #endif
  97.  
  98. #if sns_irTransceive_PRONTO_SUPPORT==1
  99. #define BaseFrq (4145146ULL)
  100. /* TODO configurable divider parameter */
  101. #define wFrqDiv (0x6d)
  102. #define divider (1000000ULL*wFrqDiv/BaseFrq)
  103.  
  104. void send_pronto(uint16_t *buffer, uint8_t len) {
  105.     StdCan_Msg_t msg;
  106.  
  107.     StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
  108.     StdCan_Set_direction(msg.Header, DIRECTIONFLAG_FROM_OWNER);
  109.     msg.Length = 8;
  110.     msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  111.     msg.Header.ModuleId = sns_irTransceive_ID;
  112.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTART;
  113.    
  114.     msg.Data[0] = 0x00; /* Prontoformat 0x0000 */
  115.     msg.Data[1] = 0x00;
  116.     msg.Data[2] = 0x00; /* Freq divider, 38kHz => 0x006d TODO: configurable */
  117.     msg.Data[3] = wFrqDiv;
  118.     msg.Data[4] = 0x00; /* Once seq length, first byte always 0 */
  119.     msg.Data[5] = len/2; /* TODO not correct if any byte overflows */
  120.     msg.Data[6] = 0x00; /* Repeat seq length, always 0 for ir receive */
  121.     msg.Data[7] = 0x00;
  122.    
  123.     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  124.     _delay_ms(1);
  125.    
  126.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1;
  127.     for (uint8_t i = 0; i < len>>3; i++) {
  128.         uint8_t index = i<<3;
  129.  
  130.         for (uint8_t j = 0; j < 8; j += 2)
  131.         {
  132.             msg.Data[j] = (buffer[index+j]/divider)&0xff;
  133.             msg.Data[j+1] = (buffer[index+j+1]/divider)&0xff;
  134.         }
  135.  
  136.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  137.         while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  138.         _delay_ms(1);
  139.        
  140.         msg.Header.Command++;
  141.     }
  142.    
  143.     uint8_t lastpacketcnt = len&0x03;
  144.     /* msg command kept from for loop, but increase 16 */
  145.     msg.Header.Command+=16;
  146.     msg.Length = lastpacketcnt<<1;
  147.     for (uint8_t i = 0; i < lastpacketcnt; i++) {
  148.         msg.Data[i<<1] = ((buffer[(len&0xfc)|i]/divider)>>8)&0xff;
  149.         msg.Data[(i<<1)+1] = ((buffer[(len&0xfc)|i]/divider)>>0)&0xff;
  150.     }
  151.     /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  152.     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  153.     _delay_ms(1);
  154. }
  155. #endif
  156.  
  157.  
  158. #if IR_RX_ENABLE==1
  159. void sns_irTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
  160. {
  161.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  162.     {
  163.         irRxChannel[channel].newData = TRUE;
  164.         irRxChannel[channel].rxlen = len;
  165.     }
  166. }
  167. #endif
  168.  
  169.  
  170. #if IR_TX_ENABLE==1
  171. void sns_irTransceive_TX_done_callback(uint8_t channel)
  172. {
  173.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  174.     {
  175.         irTxChannel[channel].sendComplete = TRUE;
  176.     }
  177. }
  178. #endif
  179.  
  180.  
  181. void sns_irTransceive_setConfig(uint8_t channel, uint8_t config, uint8_t power)
  182. {
  183.     if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  184.     {
  185. #if IR_RX_ENABLE==1
  186.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE)
  187.         {
  188. #if IR_TX_ENABLE==1
  189.             irTxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  190. #endif
  191.             irRxChannel[channel].rxbuf = buf[channel];
  192.             switch (channel)
  193.             {
  194.                 case 0:
  195.                     gpio_clr_pin(sns_irTransceive_VCC_EN0_PIN);
  196.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  197.                     break;
  198.                 case 1:
  199.                     gpio_clr_pin(sns_irTransceive_VCC_EN1_PIN);
  200.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  201.                     break;
  202.                 case 2:
  203.                     gpio_clr_pin(sns_irTransceive_VCC_EN2_PIN);
  204.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  205.                     break;
  206.             }
  207.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  208.         }
  209. #endif
  210.  
  211. #if IR_TX_ENABLE==1
  212.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_TRANSMIT)
  213.         {
  214. #if IR_RX_ENABLE==1
  215.             irRxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  216. #endif
  217.            
  218.             irTxChannel[channel].txbuf = buf[channel];
  219.             switch (channel)
  220.             {
  221.                 case 0:
  222. #if IR_RX_ENABLE==1
  223.                     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  224.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  225. #endif
  226.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX0_PIN);
  227.  
  228. #if sns_irTransceive_ENABLE_PCA95xx==1
  229.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  230.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  231.                     if (power&0x1)
  232.                     {
  233.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRl);
  234.                     }
  235.                     if (power&0x2)
  236.                     {
  237.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRh);
  238.                     }
  239. #endif
  240.                     break;
  241.                 case 1:
  242. #if IR_RX_ENABLE==1
  243.                     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  244.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  245. #endif
  246.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX1_PIN);
  247.                    
  248. #if sns_irTransceive_ENABLE_PCA95xx==1
  249.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  250.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  251.                     if (power&0x1)
  252.                     {
  253.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRl);
  254.                     }
  255.                     if (power&0x2)
  256.                     {
  257.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRh);
  258.                     }
  259. #endif
  260.                     break;
  261.                 case 2:
  262. #if IR_RX_ENABLE==1
  263.                     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  264.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  265. #endif
  266.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX2_PIN);
  267.                    
  268. #if sns_irTransceive_ENABLE_PCA95xx==1
  269.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  270.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  271.                     if (power&0x1)
  272.                     {
  273.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRl);
  274.                     }
  275.                     if (power&0x2)
  276.                     {
  277.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRh);
  278.                     }
  279. #endif
  280.                     break;
  281.             }
  282.             irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  283.         }
  284. #endif
  285.     }
  286. }
  287.  
  288.  
  289. void sns_irTransceive_Init(void)
  290. {
  291.     StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
  292.     StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  293.     irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  294.     irTxMsg.Header.ModuleId = sns_irTransceive_ID;
  295.     irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
  296.     irTxMsg.Length = 6;
  297.  
  298.     for (uint8_t i = 0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  299.     {
  300. #if IR_RX_ENABLE==1
  301.         irRxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  302.         irRxChannel[i].newData = FALSE;
  303.         irRxChannel[i].rxlen = 0;
  304.         irRxChannel[i].proto.timeout=0;
  305.         irRxChannel[i].proto.data=0;
  306.         irRxChannel[i].proto.repeats=0;
  307.         irRxChannel[i].proto.protocol=0;
  308. #endif
  309.  
  310. #if IR_TX_ENABLE==1
  311.         irTxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  312.         irTxChannel[i].sendComplete = FALSE;
  313.         irTxChannel[i].repeatCount = 0;
  314.         irTxChannel[i].txlen = 0;
  315.         irTxChannel[i].proto.data=0;
  316.         irTxChannel[i].proto.repeats=0;
  317.         irTxChannel[i].proto.framecnt=0;
  318.         irTxChannel[i].proto.protocol=0;
  319. #endif
  320.     }
  321.    
  322. #if IR_RX_ENABLE==1
  323.     // TODO: hardcoded to 3 channels?? /jm
  324.     irRxChannel[0].timerNum=sns_irTransceive_RX0_REPEATE_TIMER;
  325.     irRxChannel[1].timerNum=sns_irTransceive_RX1_REPEATE_TIMER;
  326.     irRxChannel[2].timerNum=sns_irTransceive_RX2_REPEATE_TIMER;
  327. #endif
  328.  
  329. #if IR_TX_ENABLE==1
  330.     // TODO: hardcoded to 3 channels?? /jm
  331.     irTxChannel[0].timerNum=sns_irTransceive_TX0_REPEATE_TIMER;
  332.     irTxChannel[1].timerNum=sns_irTransceive_TX1_REPEATE_TIMER;
  333.     irTxChannel[2].timerNum=sns_irTransceive_TX2_REPEATE_TIMER;
  334. #endif
  335.    
  336.     IrTransceiver_Init();
  337.     /* Configure all TX VCC pins as outputs and disable them */
  338.     gpio_set_out(sns_irTransceive_VCC_EN0_PIN);
  339.     gpio_set_out(sns_irTransceive_VCC_EN1_PIN);
  340.     gpio_set_out(sns_irTransceive_VCC_EN2_PIN);
  341.     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  342.     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  343.     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  344.  
  345. #if IR_TX_ENABLE==1
  346.     gpio_set_out(sns_irTransceive_MOD_PIN);
  347.     gpio_set_out(sns_irTransceive_TX0_PIN);
  348.     gpio_set_out(sns_irTransceive_TX1_PIN);
  349.     gpio_set_out(sns_irTransceive_TX2_PIN);
  350. #if IR_TX_ACTIVE_LOW==1
  351.     gpio_set_pin(sns_irTransceive_TX0_PIN);
  352.     gpio_set_pin(sns_irTransceive_TX1_PIN);
  353.     gpio_set_pin(sns_irTransceive_TX2_PIN);
  354. #endif
  355. #endif
  356.    
  357.     /* IR tx power pins on PCA95xx */
  358. #if sns_irTransceive_ENABLE_PCA95xx==1
  359.     Pca95xx_Init(0);
  360.  
  361.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRl);
  362.     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  363.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRh);
  364.     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  365.    
  366.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRl);
  367.     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  368.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRh);
  369.     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  370.    
  371.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRl);
  372.     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  373.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRh);
  374.     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  375. #endif
  376.  
  377. #ifdef sns_irTransceive_USEEEPROM
  378.     if (EEDATA_OK)
  379.     {
  380.       /* Use stored data to set initial values for the module */
  381.         sns_irTransceive_setConfig(0, eeprom_read_byte(EEDATA.ch0_config), eeprom_read_byte(EEDATA.ch0_txpower));
  382.         sns_irTransceive_setConfig(1, eeprom_read_byte(EEDATA.ch1_config), eeprom_read_byte(EEDATA.ch1_txpower));
  383.         sns_irTransceive_setConfig(2, eeprom_read_byte(EEDATA.ch2_config), eeprom_read_byte(EEDATA.ch2_txpower));
  384.     }
  385.     else
  386.     {  
  387.     /* The CRC of the EEPROM is not correct, store default values and update CRC */
  388.         eeprom_write_byte_crc(EEDATA.ch0_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  389.         eeprom_write_byte_crc(EEDATA.ch0_txpower, 0, WITHOUT_CRC);
  390.         eeprom_write_byte_crc(EEDATA.ch1_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  391.         eeprom_write_byte_crc(EEDATA.ch1_txpower, 0, WITHOUT_CRC);
  392.         eeprom_write_byte_crc(EEDATA.ch2_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  393.         eeprom_write_byte_crc(EEDATA.ch2_txpower, 0, WITHOUT_CRC);
  394.         EEDATA_UPDATE_CRC;
  395.     }
  396. #endif 
  397. }
  398.  
  399. void sns_irTransceive_Process(void)
  400. {
  401.     for (uint8_t channel=0; channel < IR_SUPPORTED_NUM_CHANNELS; channel++)
  402.     {
  403.  
  404. #if IR_RX_ENABLE==1
  405.         switch (irRxChannel[channel].state)
  406.         {
  407.         case sns_irTransceive_STATE_IDLE:
  408.             irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
  409.             break;
  410.  
  411.         case sns_irTransceive_STATE_START_RECEIVE:
  412.             IrTransceiver_ResetRx(channel);
  413.             cli();
  414.             irRxChannel[channel].newData = FALSE;
  415.             sei();
  416.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  417.             break;
  418.        
  419.         case sns_irTransceive_STATE_RECEIVING:
  420.             if (irRxChannel[channel].newData == TRUE) {
  421.                 //TODO: move this line to the RX callback
  422.                 IrTransceiver_DisableRx(channel);
  423.                 cli();
  424.                 irRxChannel[channel].newData = FALSE;
  425.                 sei();
  426.  
  427.                 /* Let protocol driver parse and then send on CAN */
  428.                 uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
  429.                 if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  430.                     irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  431.                     irTxMsg.Data[0] |= channel<<4;
  432.                     irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  433.                     irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  434.                     irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  435.                     irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  436.                     irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  437.  
  438.                     StdCan_Put(&irTxMsg);
  439.                 }
  440.                 else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
  441.                 {
  442. #if (sns_irTransceive_SEND_DEBUG==1)
  443. #if sns_irTransceive_PRONTO_SUPPORT==0
  444.                     send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
  445.                     irRxChannel[channel].proto.timeout=300;
  446. #endif
  447. #if sns_irTransceive_PRONTO_SUPPORT==1
  448.                     send_pronto(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
  449.                     irRxChannel[channel].proto.timeout=300;
  450. #endif
  451.  
  452. #endif
  453.                 }
  454.                
  455.                 /* Enable the receiver again */
  456.                 IrTransceiver_EnableRx(channel);
  457.                
  458.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  459.             }
  460.             break;
  461.  
  462.         case sns_irTransceive_STATE_START_PAUSE:
  463.             /* set a timer so we can send release button event when no new IR is arriving */
  464.             Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  465.             irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  466.             break;
  467.  
  468.         case sns_irTransceive_STATE_PAUSING:
  469.             /* reset timer if new IR arrived */
  470.             if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
  471.                 cli();
  472.                 irRxChannel[channel].newData = FALSE;
  473.                 sei();
  474.                 Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  475.             }
  476.        
  477.             if (Timer_Expired(irRxChannel[channel].timerNum)) {
  478.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  479.             }
  480.             break;
  481.  
  482.         case sns_irTransceive_STATE_START_IDLE:
  483.             if (irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  484.                 /* Send button release command on CAN */
  485.                 irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  486.                 irTxMsg.Data[0] |= channel<<4;
  487.                 irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  488.                 irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  489.                 irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  490.                 irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  491.                 irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  492.  
  493.                 StdCan_Put(&irTxMsg);
  494.             }
  495.             irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  496.             break;
  497.  
  498.         default:
  499.             break;
  500.         }
  501. #endif
  502.  
  503. #if IR_TX_ENABLE==1
  504.         switch (irTxChannel[channel].state)
  505.         {
  506.         case sns_irTransceive_STATE_IDLE:
  507.             /* transmission is started when a command is received on can */
  508.             break;
  509.  
  510.         case sns_irTransceive_STATE_START_TRANSMIT:
  511.             if (expandProtocol(irTxChannel[channel].txbuf, &irTxChannel[channel].txlen, &irTxChannel[channel].proto) == IR_OK)
  512.             {
  513.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, irTxChannel[channel].txlen);
  514.                 irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  515.             }
  516.             else
  517.             {
  518.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  519.             }
  520.             break;
  521.        
  522.         case sns_irTransceive_STATE_TRANSMITTING:
  523.             if (irTxChannel[channel].sendComplete == TRUE)
  524.             {
  525.                 cli();
  526.                 irTxChannel[channel].sendComplete = FALSE;
  527.                 sei();
  528.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  529.             }
  530.             break;
  531.  
  532.         case sns_irTransceive_STATE_START_PAUSE:
  533.             if (irTxChannel[channel].repeatCount < irTxChannel[channel].proto.repeats)
  534.             {
  535.                 irTxChannel[channel].repeatCount++;
  536.             }
  537.            
  538.             Timer_SetTimeout(irTxChannel[channel].timerNum, irTxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  539.  
  540.             if (irTxChannel[channel].proto.framecnt != 255)
  541.             {
  542.                 irTxChannel[channel].proto.framecnt++;
  543.             }
  544.            
  545.             irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  546.             break;
  547.  
  548.         case sns_irTransceive_STATE_PAUSING:
  549.             if (Timer_Expired(irTxChannel[channel].timerNum))
  550.             {
  551.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  552.             }
  553.            
  554.             /* transmission is stopped when such command is recevied on can */
  555.             if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
  556.             {
  557.                 //TODO maybe send message on can for status? to confirm stopped sending, ready for new command
  558.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  559.             }
  560.             break;
  561.  
  562.         case sns_irTransceive_STATE_START_IDLE:
  563.             irTxChannel[channel].stopSend = FALSE;
  564.             irTxChannel[channel].repeatCount = 0;
  565.             irTxChannel[channel].proto.framecnt = 0;
  566.            
  567.             irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  568.             break;
  569.  
  570.         default:
  571.             break;
  572.         }
  573. #endif
  574.  
  575.     }
  576. }
  577.  
  578.  
  579. /* Handle incoming CAN data */
  580. void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  581. {
  582.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  583.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  584.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_IRTRANSCEIVE &&
  585.         rxMsg->Header.ModuleId == sns_irTransceive_ID)
  586.     {
  587.         uint8_t channel;
  588.         switch (rxMsg->Header.Command)
  589.         {
  590. #if IR_TX_ENABLE==1
  591.         case CAN_MODULE_CMD_PHYSICAL_IR:
  592.             channel = rxMsg->Data[0]>>4;
  593.             if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED && channel < IR_SUPPORTED_NUM_CHANNELS)
  594.             {
  595.                 if (irTxChannel[channel].state == sns_irTransceive_STATE_IDLE)
  596.                 {
  597.                     irTxChannel[channel].proto.protocol = rxMsg->Data[1];
  598.  
  599.                     irTxChannel[channel].proto.data = rxMsg->Data[2];
  600.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  601.                     irTxChannel[channel].proto.data |= rxMsg->Data[3];
  602.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  603.                     irTxChannel[channel].proto.data |= rxMsg->Data[4];
  604.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  605.                     irTxChannel[channel].proto.data |= rxMsg->Data[5];
  606.  
  607.                     irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  608.                 }
  609.             }
  610.             else if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED && channel < IR_SUPPORTED_NUM_CHANNELS)
  611.             {
  612.                 if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE)
  613.                 {
  614.                     irTxChannel[channel].stopSend = TRUE;
  615.                 }
  616.             }
  617.             break;
  618. #endif
  619.        
  620.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRCONFIG:
  621.             channel = rxMsg->Data[0]>>4;
  622.             uint8_t config = rxMsg->Data[0] & 0x0f;
  623.             uint8_t power = rxMsg->Data[1]>>6;
  624.            
  625.             sns_irTransceive_setConfig(channel, config, power);
  626.  
  627. #ifdef sns_irTransceive_USEEEPROM
  628.             if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  629.             {
  630.                 switch (channel)
  631.                 {
  632.                     case 0:
  633.                         eeprom_write_byte_crc(EEDATA.ch0_config, config, WITHOUT_CRC);
  634.                         eeprom_write_byte_crc(EEDATA.ch0_txpower, power, WITHOUT_CRC);
  635.                         break;
  636.                     case 1:
  637.                         eeprom_write_byte_crc(EEDATA.ch1_config, config, WITHOUT_CRC);
  638.                         eeprom_write_byte_crc(EEDATA.ch1_txpower, power, WITHOUT_CRC);
  639.                         break;
  640.                     case 2:
  641.                         eeprom_write_byte_crc(EEDATA.ch2_config, config, WITHOUT_CRC);
  642.                         eeprom_write_byte_crc(EEDATA.ch2_txpower, power, WITHOUT_CRC);
  643.                         break;
  644.                     default:
  645.                         break;
  646.                 }
  647.                 EEDATA_UPDATE_CRC;
  648.             }
  649. #endif 
  650.             break;
  651.            
  652.         default:
  653.             break;
  654.            
  655.         }
  656.     }
  657. }
  658.  
  659. void sns_irTransceive_List(uint8_t ModuleSequenceNumber)
  660. {
  661.     StdCan_Msg_t txMsg;
  662.    
  663.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  664.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  665.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  666.  
  667.     txMsg.Header.ModuleId = sns_irTransceive_ID;
  668.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  669.     txMsg.Length = 6;
  670.  
  671.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  672.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  673.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  674.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  675.    
  676.     txMsg.Data[4] = NUMBER_OF_MODULES;
  677.     txMsg.Data[5] = ModuleSequenceNumber;
  678.    
  679.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  680. }
  681.