Subversion Repositories HomeAutomation

Rev

Rev 1812 | Rev 1814 | 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. #if sns_irTransceive_PRONTO_SUPPORT==1
  98. #define BaseFrq (4145146ULL)
  99. /* TODO configurable divider parameter */
  100. #define wFrqDiv (0x6d)
  101. #define divider (1000000ULL*wFrqDiv/BaseFrq)
  102.  
  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] = 0x00; //len/2; /* 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.     /* Counter for buffer */
  128.     uint8_t i = 0;
  129.     /* Counter for filling can frame data */
  130.     uint8_t j = 0;
  131.     /* Remember byte if data does not fit 8 bit */
  132.     uint8_t mem = 0;
  133.     while (i < len)
  134.     {
  135.         if (mem > 0)
  136.         {
  137.             /* If byte was memorized then buffer it */
  138.             msg.Data[j] = mem;
  139.             /* Clear memory */
  140.             mem = 0;
  141.         }
  142.         else
  143.         {
  144.             if (buffer[i] >= (2^8))
  145.             {
  146.                 /* If data does not fit into 8 bit, then split, memorize low byte */
  147.                 mem = buffer[i]&0xff;
  148.                 /* Buffer high byte */
  149.                 msg.Data[j] = (buffer[i]>>8)&0xff;
  150.                 j++;
  151.                 if (j==8)
  152.                 {
  153.                     /* If send buffer is full, send frame */
  154.                     /* can buffers will be filled when sending more than 2-3 messages, so retry until sent */
  155.                     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  156.                     _delay_ms(1);
  157.                     j=0;
  158.                     msg.Header.Command++;
  159.                 }
  160.                 /* Set complementary burst pair to 0 to indicate 16bit transmission */
  161.                 msg.Data[j] = 0;
  162.             }
  163.             else
  164.             {
  165.                 msg.Data[j] = buffer[i]&0xff;
  166.             }
  167.            
  168.             i++;
  169.         }
  170.        
  171.         j++;
  172.         if (j==8)
  173.         {
  174.             /* If send buffer is full, send frame */
  175.             /* can buffers will be filled when sending more than 2-3 messages, so retry until sent */
  176.             while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  177.             _delay_ms(1);
  178.             j=0;
  179.             msg.Header.Command++;
  180.         }
  181.        
  182.     }
  183.    
  184.     /* msg command kept from for loop, but increase 16 to send ProntoEnd */
  185.     msg.Header.Command+=16;
  186.     msg.Length = j;
  187.     /* data kept from loop */
  188.     /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  189.     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  190.     _delay_ms(1);
  191. }
  192. #endif
  193.  
  194.  
  195. #if IR_RX_ENABLE==1
  196. void sns_irTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
  197. {
  198.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  199.     {
  200.         irRxChannel[channel].newData = TRUE;
  201.         irRxChannel[channel].rxlen = len;
  202.     }
  203. }
  204. #endif
  205.  
  206.  
  207. #if IR_TX_ENABLE==1
  208. void sns_irTransceive_TX_done_callback(uint8_t channel)
  209. {
  210.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  211.     {
  212.         irTxChannel[channel].sendComplete = TRUE;
  213.     }
  214. }
  215. #endif
  216.  
  217.  
  218. void sns_irTransceive_setConfig(uint8_t channel, uint8_t config, uint8_t power)
  219. {
  220.     if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  221.     {
  222. #if IR_RX_ENABLE==1
  223.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE)
  224.         {
  225. #if IR_TX_ENABLE==1
  226.             irTxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  227. #endif
  228.             irRxChannel[channel].rxbuf = buf[channel];
  229.             switch (channel)
  230.             {
  231.                 case 0:
  232.                     gpio_clr_pin(sns_irTransceive_VCC_EN0_PIN);
  233.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  234.                     break;
  235.                 case 1:
  236.                     gpio_clr_pin(sns_irTransceive_VCC_EN1_PIN);
  237.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  238.                     break;
  239.                 case 2:
  240.                     gpio_clr_pin(sns_irTransceive_VCC_EN2_PIN);
  241.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  242.                     break;
  243.             }
  244.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  245.         }
  246. #endif
  247.  
  248. #if IR_TX_ENABLE==1
  249.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_TRANSMIT)
  250.         {
  251. #if IR_RX_ENABLE==1
  252.             irRxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  253. #endif
  254.            
  255.             irTxChannel[channel].txbuf = buf[channel];
  256.             switch (channel)
  257.             {
  258.                 case 0:
  259. #if IR_RX_ENABLE==1
  260.                     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  261.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  262. #endif
  263.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX0_PIN);
  264.  
  265. #if sns_irTransceive_ENABLE_PCA95xx==1
  266.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  267.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  268.                     if (power&0x1)
  269.                     {
  270.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRl);
  271.                     }
  272.                     if (power&0x2)
  273.                     {
  274.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRh);
  275.                     }
  276. #endif
  277.                     break;
  278.                 case 1:
  279. #if IR_RX_ENABLE==1
  280.                     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  281.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  282. #endif
  283.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX1_PIN);
  284.                    
  285. #if sns_irTransceive_ENABLE_PCA95xx==1
  286.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  287.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  288.                     if (power&0x1)
  289.                     {
  290.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRl);
  291.                     }
  292.                     if (power&0x2)
  293.                     {
  294.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRh);
  295.                     }
  296. #endif
  297.                     break;
  298.                 case 2:
  299. #if IR_RX_ENABLE==1
  300.                     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  301.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  302. #endif
  303.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX2_PIN);
  304.                    
  305. #if sns_irTransceive_ENABLE_PCA95xx==1
  306.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  307.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  308.                     if (power&0x1)
  309.                     {
  310.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRl);
  311.                     }
  312.                     if (power&0x2)
  313.                     {
  314.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRh);
  315.                     }
  316. #endif
  317.                     break;
  318.             }
  319.             irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  320.         }
  321. #endif
  322.     }
  323. }
  324.  
  325.  
  326. void sns_irTransceive_Init(void)
  327. {
  328.     StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
  329.     StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  330.     irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  331.     irTxMsg.Header.ModuleId = sns_irTransceive_ID;
  332.     irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
  333.     irTxMsg.Length = 6;
  334.  
  335.     for (uint8_t i = 0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  336.     {
  337. #if IR_RX_ENABLE==1
  338.         irRxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  339.         irRxChannel[i].newData = FALSE;
  340.         irRxChannel[i].rxlen = 0;
  341.         irRxChannel[i].proto.timeout=0;
  342.         irRxChannel[i].proto.data=0;
  343.         irRxChannel[i].proto.repeats=0;
  344.         irRxChannel[i].proto.protocol=0;
  345. #endif
  346.  
  347. #if IR_TX_ENABLE==1
  348.         irTxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  349.         irTxChannel[i].sendComplete = FALSE;
  350.         irTxChannel[i].repeatCount = 0;
  351.         irTxChannel[i].txlen = 0;
  352.         irTxChannel[i].proto.data=0;
  353.         irTxChannel[i].proto.repeats=0;
  354.         irTxChannel[i].proto.framecnt=0;
  355.         irTxChannel[i].proto.protocol=0;
  356. #endif
  357.     }
  358.    
  359. #if IR_RX_ENABLE==1
  360.     // TODO: hardcoded to 3 channels?? /jm
  361.     irRxChannel[0].timerNum=sns_irTransceive_RX0_REPEATE_TIMER;
  362.     irRxChannel[1].timerNum=sns_irTransceive_RX1_REPEATE_TIMER;
  363.     irRxChannel[2].timerNum=sns_irTransceive_RX2_REPEATE_TIMER;
  364. #endif
  365.  
  366. #if IR_TX_ENABLE==1
  367.     // TODO: hardcoded to 3 channels?? /jm
  368.     irTxChannel[0].timerNum=sns_irTransceive_TX0_REPEATE_TIMER;
  369.     irTxChannel[1].timerNum=sns_irTransceive_TX1_REPEATE_TIMER;
  370.     irTxChannel[2].timerNum=sns_irTransceive_TX2_REPEATE_TIMER;
  371. #endif
  372.    
  373.     IrTransceiver_Init();
  374.     /* Configure all TX VCC pins as outputs and disable them */
  375.     gpio_set_out(sns_irTransceive_VCC_EN0_PIN);
  376.     gpio_set_out(sns_irTransceive_VCC_EN1_PIN);
  377.     gpio_set_out(sns_irTransceive_VCC_EN2_PIN);
  378.     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  379.     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  380.     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  381.  
  382. #if IR_TX_ENABLE==1
  383.     gpio_set_out(sns_irTransceive_MOD_PIN);
  384.     gpio_set_out(sns_irTransceive_TX0_PIN);
  385.     gpio_set_out(sns_irTransceive_TX1_PIN);
  386.     gpio_set_out(sns_irTransceive_TX2_PIN);
  387. #if IR_TX_ACTIVE_LOW==1
  388.     gpio_set_pin(sns_irTransceive_TX0_PIN);
  389.     gpio_set_pin(sns_irTransceive_TX1_PIN);
  390.     gpio_set_pin(sns_irTransceive_TX2_PIN);
  391. #endif
  392. #endif
  393.    
  394.     /* IR tx power pins on PCA95xx */
  395. #if sns_irTransceive_ENABLE_PCA95xx==1
  396.     Pca95xx_Init(0);
  397.  
  398.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRl);
  399.     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  400.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRh);
  401.     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  402.    
  403.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRl);
  404.     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  405.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRh);
  406.     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  407.    
  408.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRl);
  409.     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  410.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRh);
  411.     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  412. #endif
  413.  
  414. #ifdef sns_irTransceive_USEEEPROM
  415.     if (EEDATA_OK)
  416.     {
  417.       /* Use stored data to set initial values for the module */
  418.         sns_irTransceive_setConfig(0, eeprom_read_byte(EEDATA.ch0_config), eeprom_read_byte(EEDATA.ch0_txpower));
  419.         sns_irTransceive_setConfig(1, eeprom_read_byte(EEDATA.ch1_config), eeprom_read_byte(EEDATA.ch1_txpower));
  420.         sns_irTransceive_setConfig(2, eeprom_read_byte(EEDATA.ch2_config), eeprom_read_byte(EEDATA.ch2_txpower));
  421.     }
  422.     else
  423.     {  
  424.     /* The CRC of the EEPROM is not correct, store default values and update CRC */
  425.         eeprom_write_byte_crc(EEDATA.ch0_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  426.         eeprom_write_byte_crc(EEDATA.ch0_txpower, 0, WITHOUT_CRC);
  427.         eeprom_write_byte_crc(EEDATA.ch1_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  428.         eeprom_write_byte_crc(EEDATA.ch1_txpower, 0, WITHOUT_CRC);
  429.         eeprom_write_byte_crc(EEDATA.ch2_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  430.         eeprom_write_byte_crc(EEDATA.ch2_txpower, 0, WITHOUT_CRC);
  431.         EEDATA_UPDATE_CRC;
  432.     }
  433. #endif 
  434. }
  435.  
  436. void sns_irTransceive_Process(void)
  437. {
  438.     for (uint8_t channel=0; channel < IR_SUPPORTED_NUM_CHANNELS; channel++)
  439.     {
  440.  
  441. #if IR_RX_ENABLE==1
  442.         switch (irRxChannel[channel].state)
  443.         {
  444.         case sns_irTransceive_STATE_IDLE:
  445.             irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
  446.             break;
  447.  
  448.         case sns_irTransceive_STATE_START_RECEIVE:
  449.             IrTransceiver_ResetRx(channel);
  450.             cli();
  451.             irRxChannel[channel].newData = FALSE;
  452.             sei();
  453.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  454.             break;
  455.        
  456.         case sns_irTransceive_STATE_RECEIVING:
  457.             if (irRxChannel[channel].newData == TRUE) {
  458.                 //TODO: move this line to the RX callback
  459.                 IrTransceiver_DisableRx(channel);
  460.                 cli();
  461.                 irRxChannel[channel].newData = FALSE;
  462.                 sei();
  463.  
  464.                 /* Let protocol driver parse and then send on CAN */
  465.                 uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
  466.                 if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  467.                     irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  468.                     irTxMsg.Data[0] |= channel<<4;
  469.                     irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  470.                     irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  471.                     irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  472.                     irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  473.                     irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  474.  
  475.                     StdCan_Put(&irTxMsg);
  476.                 }
  477.                 else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
  478.                 {
  479. #if (sns_irTransceive_SEND_DEBUG==1)
  480. #if sns_irTransceive_PRONTO_SUPPORT==0
  481.                     send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
  482.                     irRxChannel[channel].proto.timeout=300;
  483. #endif
  484. #if sns_irTransceive_PRONTO_SUPPORT==1
  485.                     send_pronto(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
  486.                     irRxChannel[channel].proto.timeout=300;
  487. #endif
  488.  
  489. #endif
  490.                 }
  491.                
  492.                 /* Enable the receiver again */
  493.                 IrTransceiver_EnableRx(channel);
  494.                
  495.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  496.             }
  497.             break;
  498.  
  499.         case sns_irTransceive_STATE_START_PAUSE:
  500.             /* set a timer so we can send release button event when no new IR is arriving */
  501.             Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  502.             irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  503.             break;
  504.  
  505.         case sns_irTransceive_STATE_PAUSING:
  506.             /* reset timer if new IR arrived */
  507.             if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
  508.                 cli();
  509.                 irRxChannel[channel].newData = FALSE;
  510.                 sei();
  511.                 Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  512.             }
  513.        
  514.             if (Timer_Expired(irRxChannel[channel].timerNum)) {
  515.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  516.             }
  517.             break;
  518.  
  519.         case sns_irTransceive_STATE_START_IDLE:
  520.             if (irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  521.                 /* Send button release command on CAN */
  522.                 irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  523.                 irTxMsg.Data[0] |= channel<<4;
  524.                 irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  525.                 irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  526.                 irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  527.                 irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  528.                 irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  529.  
  530.                 StdCan_Put(&irTxMsg);
  531.             }
  532.             irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  533.             break;
  534.  
  535.         default:
  536.             break;
  537.         }
  538. #endif
  539.  
  540. #if IR_TX_ENABLE==1
  541.         switch (irTxChannel[channel].state)
  542.         {
  543.         case sns_irTransceive_STATE_IDLE:
  544.             /* transmission is started when a command is received on can */
  545.             break;
  546.  
  547.         case sns_irTransceive_STATE_START_TRANSMIT:
  548.             if (expandProtocol(irTxChannel[channel].txbuf, &irTxChannel[channel].txlen, &irTxChannel[channel].proto) == IR_OK)
  549.             {
  550.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, irTxChannel[channel].txlen);
  551.                 irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  552.             }
  553.             else
  554.             {
  555.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  556.             }
  557.             break;
  558.        
  559.         case sns_irTransceive_STATE_TRANSMITTING:
  560.             if (irTxChannel[channel].sendComplete == TRUE)
  561.             {
  562.                 cli();
  563.                 irTxChannel[channel].sendComplete = FALSE;
  564.                 sei();
  565.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  566.             }
  567.             break;
  568.  
  569.         case sns_irTransceive_STATE_START_PAUSE:
  570.             if (irTxChannel[channel].repeatCount < irTxChannel[channel].proto.repeats)
  571.             {
  572.                 irTxChannel[channel].repeatCount++;
  573.             }
  574.            
  575.             Timer_SetTimeout(irTxChannel[channel].timerNum, irTxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  576.  
  577.             if (irTxChannel[channel].proto.framecnt != 255)
  578.             {
  579.                 irTxChannel[channel].proto.framecnt++;
  580.             }
  581.            
  582.             irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  583.             break;
  584.  
  585.         case sns_irTransceive_STATE_PAUSING:
  586.             if (Timer_Expired(irTxChannel[channel].timerNum))
  587.             {
  588.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  589.             }
  590.            
  591.             /* transmission is stopped when such command is recevied on can */
  592.             if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
  593.             {
  594.                 //TODO maybe send message on can for status? to confirm stopped sending, ready for new command
  595.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  596.             }
  597.             break;
  598.  
  599.         case sns_irTransceive_STATE_START_IDLE:
  600.             irTxChannel[channel].stopSend = FALSE;
  601.             irTxChannel[channel].repeatCount = 0;
  602.             irTxChannel[channel].proto.framecnt = 0;
  603.            
  604.             irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  605.             break;
  606.  
  607.         default:
  608.             break;
  609.         }
  610. #endif
  611.  
  612.     }
  613. }
  614.  
  615.  
  616. /* Handle incoming CAN data */
  617. void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  618. {
  619.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  620.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  621.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_IRTRANSCEIVE &&
  622.         rxMsg->Header.ModuleId == sns_irTransceive_ID)
  623.     {
  624.         uint8_t channel;
  625.         switch (rxMsg->Header.Command)
  626.         {
  627. #if IR_TX_ENABLE==1
  628.         case CAN_MODULE_CMD_PHYSICAL_IR:
  629.             channel = rxMsg->Data[0]>>4;
  630.             if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED && channel < IR_SUPPORTED_NUM_CHANNELS)
  631.             {
  632.                 if (irTxChannel[channel].state == sns_irTransceive_STATE_IDLE)
  633.                 {
  634.                     irTxChannel[channel].proto.protocol = rxMsg->Data[1];
  635.  
  636.                     irTxChannel[channel].proto.data = rxMsg->Data[2];
  637.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  638.                     irTxChannel[channel].proto.data |= rxMsg->Data[3];
  639.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  640.                     irTxChannel[channel].proto.data |= rxMsg->Data[4];
  641.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  642.                     irTxChannel[channel].proto.data |= rxMsg->Data[5];
  643.  
  644.                     irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  645.                 }
  646.             }
  647.             else if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED && channel < IR_SUPPORTED_NUM_CHANNELS)
  648.             {
  649.                 if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE)
  650.                 {
  651.                     irTxChannel[channel].stopSend = TRUE;
  652.                 }
  653.             }
  654.             break;
  655. #endif
  656.        
  657.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRCONFIG:
  658.             channel = rxMsg->Data[0]>>4;
  659.             uint8_t config = rxMsg->Data[0] & 0x0f;
  660.             uint8_t power = rxMsg->Data[1]>>6;
  661.            
  662.             sns_irTransceive_setConfig(channel, config, power);
  663.  
  664. #ifdef sns_irTransceive_USEEEPROM
  665.             if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  666.             {
  667.                 switch (channel)
  668.                 {
  669.                     case 0:
  670.                         eeprom_write_byte_crc(EEDATA.ch0_config, config, WITHOUT_CRC);
  671.                         eeprom_write_byte_crc(EEDATA.ch0_txpower, power, WITHOUT_CRC);
  672.                         break;
  673.                     case 1:
  674.                         eeprom_write_byte_crc(EEDATA.ch1_config, config, WITHOUT_CRC);
  675.                         eeprom_write_byte_crc(EEDATA.ch1_txpower, power, WITHOUT_CRC);
  676.                         break;
  677.                     case 2:
  678.                         eeprom_write_byte_crc(EEDATA.ch2_config, config, WITHOUT_CRC);
  679.                         eeprom_write_byte_crc(EEDATA.ch2_txpower, power, WITHOUT_CRC);
  680.                         break;
  681.                     default:
  682.                         break;
  683.                 }
  684.                 EEDATA_UPDATE_CRC;
  685.             }
  686. #endif 
  687.             break;
  688.            
  689.         default:
  690.             break;
  691.            
  692.         }
  693.     }
  694. }
  695.  
  696. void sns_irTransceive_List(uint8_t ModuleSequenceNumber)
  697. {
  698.     StdCan_Msg_t txMsg;
  699.    
  700.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  701.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  702.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  703.  
  704.     txMsg.Header.ModuleId = sns_irTransceive_ID;
  705.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  706.     txMsg.Length = 6;
  707.  
  708.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  709.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  710.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  711.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  712.    
  713.     txMsg.Data[4] = NUMBER_OF_MODULES;
  714.     txMsg.Data[5] = ModuleSequenceNumber;
  715.    
  716.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  717. }
  718.