Subversion Repositories HomeAutomation

Rev

Rev 1834 | Rev 1837 | 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.     uint8_t             modfreq;
  27.     uint16_t            *rxbuf;
  28.     uint8_t             rxlen;
  29.     uint8_t             timerNum;
  30.     Ir_Protocol_Data_t  proto;
  31.     uint8_t             newData;
  32. } irRxChannel[IR_SUPPORTED_NUM_CHANNELS];
  33. #endif
  34.  
  35. #if IR_TX_ENABLE==1
  36. struct {
  37.     uint8_t             state;
  38.     uint8_t             modfreq;
  39.     uint16_t            *txbuf;
  40.     uint8_t             txlen;
  41.     uint8_t             timerNum;
  42.     uint8_t             repeatCount;
  43.     Ir_Protocol_Data_t  proto;
  44.     uint8_t             stopSend;
  45.     uint8_t             sendComplete;
  46.     // pronto params
  47.     uint8_t             sendingPronto;
  48.     uint8_t             onceSeqLen;
  49.     uint8_t             repSeqLen;
  50. } irTxChannel[IR_SUPPORTED_NUM_CHANNELS];
  51. #endif
  52.  
  53. uint16_t    buf[IR_SUPPORTED_NUM_CHANNELS][MAX_NR_TIMES];
  54.  
  55. StdCan_Msg_t        irTxMsg;
  56.  
  57. #if (sns_irTransceive_SEND_DEBUG==1)
  58. void send_debug(uint16_t *buffer, uint8_t len) {
  59.     StdCan_Msg_t dbgIrTxMsg;
  60.     /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */
  61.  
  62.     StdCan_Set_class(dbgIrTxMsg.Header, CAN_MODULE_CLASS_SNS);
  63.     StdCan_Set_direction(dbgIrTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  64.     dbgIrTxMsg.Length = 8;
  65.     dbgIrTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  66.     dbgIrTxMsg.Header.ModuleId = sns_irTransceive_ID;
  67.     dbgIrTxMsg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRRAW;
  68.     for (uint8_t i = 0; i < len>>2; i++) {
  69.         uint8_t index = i<<2;
  70.  
  71.         dbgIrTxMsg.Data[0] = (buffer[index]>>8)&0xff;
  72.         dbgIrTxMsg.Data[1] = (buffer[index]>>0)&0xff;
  73.         dbgIrTxMsg.Data[2] = (buffer[index+1]>>8)&0xff;
  74.         dbgIrTxMsg.Data[3] = (buffer[index+1]>>0)&0xff;
  75.         dbgIrTxMsg.Data[4] = (buffer[index+2]>>8)&0xff;
  76.         dbgIrTxMsg.Data[5] = (buffer[index+2]>>0)&0xff;
  77.         dbgIrTxMsg.Data[6] = (buffer[index+3]>>8)&0xff;
  78.         dbgIrTxMsg.Data[7] = (buffer[index+3]>>0)&0xff;
  79.                
  80.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  81.         while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
  82.         _delay_ms(1);
  83.     }
  84.    
  85.     uint8_t lastpacketcnt = len&0x03;
  86.     if (lastpacketcnt > 0) {
  87.         dbgIrTxMsg.Length = lastpacketcnt<<1;
  88.         for (uint8_t i = 0; i < lastpacketcnt; i++) {
  89.             dbgIrTxMsg.Data[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff;
  90.             dbgIrTxMsg.Data[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff;
  91.         }
  92.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  93.         while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
  94.         _delay_ms(1);
  95.     }
  96.  
  97. }
  98. #endif
  99.  
  100. #define sns_irTransceive_BaseFrq (4145146ULL)
  101.  
  102. #ifndef sns_irTransceive_PRONTO_SUPPORT
  103. #define sns_irTransceive_PRONTO_SUPPORT 0
  104. #endif
  105.  
  106. #if sns_irTransceive_PRONTO_SUPPORT==1
  107. volatile uint32_t sns_irTransceive_LastPronto=0;
  108. static uint8_t activeChannel = 0;
  109. #define sns_irTransceive_MAXTIMING (16*1000)
  110.  
  111. void send_pronto(uint16_t *buffer, uint8_t len, uint8_t channel, uint8_t modfreq) {
  112.     StdCan_Msg_t msg;
  113.  
  114.     StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
  115.     StdCan_Set_direction(msg.Header, DIRECTIONFLAG_FROM_OWNER);
  116.     msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  117.     msg.Header.ModuleId = sns_irTransceive_ID;
  118.    
  119.     /* Send timing command */
  120.     msg.Length = 5;
  121.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOTIMING;
  122.     uint32_t currentTime=Timer_GetTicks();
  123.     if ((currentTime < sns_irTransceive_LastPronto+sns_irTransceive_MAXTIMING) && (sns_irTransceive_LastPronto != 0))
  124.     {
  125.         msg.Data[0] = 0xff;
  126.         msg.Data[1] = 0x10;
  127.         msg.Data[2] = (((currentTime-sns_irTransceive_LastPronto)*1000)>>16)&0xff;
  128.         msg.Data[3] = (((currentTime-sns_irTransceive_LastPronto)*1000)>>8)&0xff;
  129.         msg.Data[4] = ((currentTime-sns_irTransceive_LastPronto)*1000)&0xff;
  130.         while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  131.         _delay_ms(1);
  132.     }
  133.     sns_irTransceive_LastPronto=currentTime;
  134.  
  135.     /* Send pronto start */
  136.     msg.Length = 8;
  137.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTART;
  138.  
  139.     msg.Data[0] = ((channel<<4)|0x0);   /* Channel and Prontoformat 0x000 */
  140.     msg.Data[1] = 0x00;
  141.     msg.Data[2] = 0x00; /* Freq divider */
  142.     msg.Data[3] = modfreq;
  143.     msg.Data[4] = 0x00; /* Once seq length, first byte always 0 */
  144.     msg.Data[5] = len/2;
  145.     msg.Data[6] = 0x00; /* Repeat seq length, always 0 for ir receive */
  146.     msg.Data[7] = 0x00;
  147.    
  148.     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  149.     _delay_ms(1);
  150.  
  151.     /* Send pronto data */
  152.     uint16_t divider = ((1000000ULL*modfreq)/sns_irTransceive_BaseFrq);
  153.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1;
  154.     /* Counter for buffer */
  155.     uint8_t i = 0;
  156.     /* Counter for filling can frame data */
  157.     uint8_t j = 0;
  158.     /* Remember byte if data does not fit 8 bit */
  159.     uint8_t mem = 0;
  160.     while (i < len)
  161.     {
  162.         if (mem > 0)
  163.         {
  164.             /* If byte was memorized then buffer it */
  165.             msg.Data[j] = mem;
  166.             /* Clear memory */
  167.             mem = 0;
  168.         }
  169.         else
  170.         {
  171.             uint16_t data = buffer[i]/divider;
  172.             if (data >= 256)
  173.             {
  174.                 /* If data does not fit into 8 bit, then split, memorize low byte */
  175.                 mem = data&0xff;
  176.                 /* Buffer high byte */
  177.                 msg.Data[j] = (data>>8)&0xff;
  178.                 j++;
  179.                 if (j==8)
  180.                 {
  181.                     /* If send buffer is full, send frame */
  182.                     /* can buffers will be filled when sending more than 2-3 messages, so retry until sent */
  183.                     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  184.                     _delay_ms(1);
  185.                     j=0;
  186.                     msg.Header.Command++;
  187.                 }
  188.                 /* Set complementary burst pair to 0 to indicate 16bit transmission */
  189.                 msg.Data[j] = 0;
  190.             }
  191.             else
  192.             {
  193.                 msg.Data[j] = data&0xff;
  194.             }
  195.            
  196.             i++;
  197.         }
  198.        
  199.         j++;
  200.         if (j==8)
  201.         {
  202.             /* If send buffer is full, send frame */
  203.             /* can buffers will be filled when sending more than 2-3 messages, so retry until sent */
  204.             while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  205.             _delay_ms(1);
  206.             j=0;
  207.             msg.Header.Command++;
  208.         }
  209.     }
  210.    
  211.     /* End with 25ms, TODO fix uglyness */
  212.     msg.Data[j] = ((IR_MAX_PULSE_WIDTH/divider)>>8)&0xff;
  213.     j++;
  214.     if (j==8)
  215.     {
  216.         /* If send buffer is full, send frame */
  217.         /* can buffers will be filled when sending more than 2-3 messages, so retry until sent */
  218.         while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  219.         _delay_ms(1);
  220.         j=0;
  221.         msg.Header.Command++;
  222.     }
  223.     msg.Data[j] = 0;
  224.     j++;
  225.     msg.Data[j] = (IR_MAX_PULSE_WIDTH/divider)&0xff;
  226.     j++;
  227.     if (j==8)
  228.     {
  229.         /* If send buffer is full, send frame */
  230.         /* can buffers will be filled when sending more than 2-3 messages, so retry until sent */
  231.         while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  232.         _delay_ms(1);
  233.         j=0;
  234.         msg.Header.Command++;
  235.     }
  236.    
  237.     /* Msg command kept from for loop, but increase 16 to send ProntoEnd */
  238.     msg.Header.Command+=16;
  239.     msg.Length = j;
  240.     /* data kept from loop, if any */
  241.     /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  242.     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  243.     _delay_ms(1);
  244. }
  245. #endif
  246.  
  247.  
  248. #if IR_RX_ENABLE==1
  249. void sns_irTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
  250. {
  251.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  252.     {
  253.         irRxChannel[channel].newData = TRUE;
  254.         irRxChannel[channel].rxlen = len;
  255.     }
  256. }
  257. #endif
  258.  
  259.  
  260. #if IR_TX_ENABLE==1
  261. void sns_irTransceive_TX_done_callback(uint8_t channel)
  262. {
  263.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  264.     {
  265.         irTxChannel[channel].sendComplete = TRUE;
  266.     }
  267. }
  268. #endif
  269.  
  270.  
  271. void sns_irTransceive_setConfig(uint8_t channel, uint8_t config, uint8_t power, uint8_t modfreq)
  272. {
  273.     if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  274.     {
  275. #if IR_RX_ENABLE==1
  276.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE)
  277.         {
  278. #if IR_TX_ENABLE==1
  279.             irTxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  280. #endif
  281.             irRxChannel[channel].modfreq = modfreq;
  282.             irRxChannel[channel].rxbuf = buf[channel];
  283.             switch (channel)
  284.             {
  285.                 case 0:
  286.                     gpio_clr_pin(sns_irTransceive_VCC_EN0_PIN);
  287.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  288.                     break;
  289.                 case 1:
  290.                     gpio_clr_pin(sns_irTransceive_VCC_EN1_PIN);
  291.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  292.                     break;
  293.                 case 2:
  294.                     gpio_clr_pin(sns_irTransceive_VCC_EN2_PIN);
  295.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  296.                     break;
  297.             }
  298.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  299.         }
  300. #endif
  301.  
  302. #if IR_TX_ENABLE==1
  303.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_TRANSMIT)
  304.         {
  305. #if IR_RX_ENABLE==1
  306.             irRxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  307. #endif
  308.             irTxChannel[channel].modfreq = modfreq;
  309.             irTxChannel[channel].txbuf = buf[channel];
  310.             switch (channel)
  311.             {
  312.                 case 0:
  313. #if IR_RX_ENABLE==1
  314.                     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  315.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  316. #endif
  317.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX0_PIN);
  318.  
  319. #if sns_irTransceive_ENABLE_PCA95xx==1
  320.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  321.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  322.                     if (power&0x1)
  323.                     {
  324.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRl);
  325.                     }
  326.                     if (power&0x2)
  327.                     {
  328.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRh);
  329.                     }
  330. #endif
  331.                     break;
  332.                 case 1:
  333. #if IR_RX_ENABLE==1
  334.                     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  335.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  336. #endif
  337.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX1_PIN);
  338.                    
  339. #if sns_irTransceive_ENABLE_PCA95xx==1
  340.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  341.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  342.                     if (power&0x1)
  343.                     {
  344.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRl);
  345.                     }
  346.                     if (power&0x2)
  347.                     {
  348.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRh);
  349.                     }
  350. #endif
  351.                     break;
  352.                 case 2:
  353. #if IR_RX_ENABLE==1
  354.                     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  355.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  356. #endif
  357.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX2_PIN);
  358.                    
  359. #if sns_irTransceive_ENABLE_PCA95xx==1
  360.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  361.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  362.                     if (power&0x1)
  363.                     {
  364.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRl);
  365.                     }
  366.                     if (power&0x2)
  367.                     {
  368.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRh);
  369.                     }
  370. #endif
  371.                     break;
  372.             }
  373.             irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  374.         }
  375. #endif
  376.     }
  377. }
  378.  
  379.  
  380. void sns_irTransceive_Init(void)
  381. {
  382.     StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
  383.     StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  384.     irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  385.     irTxMsg.Header.ModuleId = sns_irTransceive_ID;
  386.     irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
  387.     irTxMsg.Length = 6;
  388.  
  389.     for (uint8_t i = 0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  390.     {
  391. #if IR_RX_ENABLE==1
  392.         irRxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  393.         irRxChannel[i].newData = FALSE;
  394.         irRxChannel[i].rxlen = 0;
  395.         irRxChannel[i].proto.timeout=0;
  396.         irRxChannel[i].proto.data=0;
  397.         irRxChannel[i].proto.repeats=0;
  398.         irRxChannel[i].proto.protocol=0;
  399. #endif
  400.  
  401. #if IR_TX_ENABLE==1
  402.         irTxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  403.         irTxChannel[i].sendComplete = FALSE;
  404.         irTxChannel[i].repeatCount = 0;
  405.         irTxChannel[i].txlen = 0;
  406.         irTxChannel[i].proto.data=0;
  407.         irTxChannel[i].proto.repeats=0;
  408.         irTxChannel[i].proto.framecnt=0;
  409.         irTxChannel[i].proto.protocol=0;
  410. #endif
  411.     }
  412.    
  413. #if IR_RX_ENABLE==1
  414.     // TODO: hardcoded to 3 channels?? /jm
  415.     irRxChannel[0].timerNum=sns_irTransceive_RX0_REPEATE_TIMER;
  416.     irRxChannel[1].timerNum=sns_irTransceive_RX1_REPEATE_TIMER;
  417.     irRxChannel[2].timerNum=sns_irTransceive_RX2_REPEATE_TIMER;
  418. #endif
  419.  
  420. #if IR_TX_ENABLE==1
  421.     // TODO: hardcoded to 3 channels?? /jm
  422.     irTxChannel[0].timerNum=sns_irTransceive_TX0_REPEATE_TIMER;
  423.     irTxChannel[1].timerNum=sns_irTransceive_TX1_REPEATE_TIMER;
  424.     irTxChannel[2].timerNum=sns_irTransceive_TX2_REPEATE_TIMER;
  425. #endif
  426.    
  427.     IrTransceiver_Init();
  428.     /* Configure all TX VCC pins as outputs and disable them */
  429.     gpio_set_out(sns_irTransceive_VCC_EN0_PIN);
  430.     gpio_set_out(sns_irTransceive_VCC_EN1_PIN);
  431.     gpio_set_out(sns_irTransceive_VCC_EN2_PIN);
  432.     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  433.     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  434.     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  435.  
  436. #if IR_TX_ENABLE==1
  437.     gpio_set_out(sns_irTransceive_MOD_PIN);
  438.     gpio_set_out(sns_irTransceive_TX0_PIN);
  439.     gpio_set_out(sns_irTransceive_TX1_PIN);
  440.     gpio_set_out(sns_irTransceive_TX2_PIN);
  441. #if IR_TX_ACTIVE_LOW==1
  442.     gpio_set_pin(sns_irTransceive_TX0_PIN);
  443.     gpio_set_pin(sns_irTransceive_TX1_PIN);
  444.     gpio_set_pin(sns_irTransceive_TX2_PIN);
  445. #endif
  446. #endif
  447.    
  448.     /* IR tx power pins on PCA95xx */
  449. #if sns_irTransceive_ENABLE_PCA95xx==1
  450.     Pca95xx_Init(0);
  451.  
  452.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRl);
  453.     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  454.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRh);
  455.     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  456.    
  457.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRl);
  458.     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  459.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRh);
  460.     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  461.    
  462.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRl);
  463.     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  464.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRh);
  465.     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  466. #endif
  467.  
  468. #ifdef sns_irTransceive_USEEEPROM
  469.     if (EEDATA_OK)
  470.     {
  471.       /* Use stored data to set initial values for the module */
  472.         sns_irTransceive_setConfig(0, eeprom_read_byte(EEDATA.ch0_config), eeprom_read_byte(EEDATA.ch0_txpower), eeprom_read_byte(EEDATA.ch0_modfreq));
  473.         sns_irTransceive_setConfig(1, eeprom_read_byte(EEDATA.ch1_config), eeprom_read_byte(EEDATA.ch1_txpower), eeprom_read_byte(EEDATA.ch1_modfreq));
  474.         sns_irTransceive_setConfig(2, eeprom_read_byte(EEDATA.ch2_config), eeprom_read_byte(EEDATA.ch2_txpower), eeprom_read_byte(EEDATA.ch2_modfreq));
  475.     }
  476.     else
  477.     {  
  478.     /* The CRC of the EEPROM is not correct, store default values and update CRC */
  479.         eeprom_write_byte_crc(EEDATA.ch0_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  480.         eeprom_write_byte_crc(EEDATA.ch0_txpower, 0, WITHOUT_CRC);
  481.         eeprom_write_byte_crc(EEDATA.ch0_modfreq, sns_irTransceive_BaseFrq/38000, WITHOUT_CRC);
  482.         eeprom_write_byte_crc(EEDATA.ch1_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  483.         eeprom_write_byte_crc(EEDATA.ch1_txpower, 0, WITHOUT_CRC);
  484.         eeprom_write_byte_crc(EEDATA.ch1_modfreq, sns_irTransceive_BaseFrq/38000, WITHOUT_CRC);
  485.         eeprom_write_byte_crc(EEDATA.ch2_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  486.         eeprom_write_byte_crc(EEDATA.ch2_txpower, 0, WITHOUT_CRC);
  487.         eeprom_write_byte_crc(EEDATA.ch2_modfreq, sns_irTransceive_BaseFrq/38000, WITHOUT_CRC);
  488.         EEDATA_UPDATE_CRC;
  489.     }
  490. #endif 
  491. }
  492.  
  493. void sns_irTransceive_Process(void)
  494. {
  495.     for (uint8_t channel=0; channel < IR_SUPPORTED_NUM_CHANNELS; channel++)
  496.     {
  497.  
  498. #if IR_RX_ENABLE==1
  499.         switch (irRxChannel[channel].state)
  500.         {
  501.         case sns_irTransceive_STATE_IDLE:
  502.             irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
  503.             break;
  504.  
  505.         case sns_irTransceive_STATE_START_RECEIVE:
  506.             IrTransceiver_ResetRx(channel);
  507.             cli();
  508.             irRxChannel[channel].newData = FALSE;
  509.             sei();
  510.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  511.             break;
  512.        
  513.         case sns_irTransceive_STATE_RECEIVING:
  514.             if (irRxChannel[channel].newData == TRUE) {
  515.                 //TODO: move this line to the RX callback
  516.                 IrTransceiver_DisableRx(channel);
  517.                 cli();
  518.                 irRxChannel[channel].newData = FALSE;
  519.                 sei();
  520.  
  521.                 /* Let protocol driver parse and then send on CAN */
  522.                 uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
  523.                 if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  524.                     irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  525.                     irTxMsg.Data[0] |= channel<<4;
  526.                     irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  527.                     irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  528.                     irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  529.                     irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  530.                     irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  531.  
  532.                     StdCan_Put(&irTxMsg);
  533.                 }
  534.                 else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
  535.                 {
  536. #if (sns_irTransceive_SEND_DEBUG==1)
  537.                     send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
  538.                     irRxChannel[channel].proto.timeout=300;
  539. #elif sns_irTransceive_PRONTO_SUPPORT==1
  540.                     send_pronto(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, channel, irRxChannel[channel].modfreq);
  541.                     irRxChannel[channel].proto.timeout=1;
  542. #endif
  543.                 }
  544.  
  545.                 /* Enable the receiver again */
  546.                 IrTransceiver_EnableRx(channel);
  547.  
  548.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  549.             }
  550.             break;
  551.  
  552.         case sns_irTransceive_STATE_START_PAUSE:
  553.             /* set a timer so we can send release button event when no new IR is arriving */
  554.             Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  555.             irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  556.             break;
  557.  
  558.         case sns_irTransceive_STATE_PAUSING:
  559.             /* reset timer if new IR arrived */
  560.             if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
  561.                 cli();
  562.                 irRxChannel[channel].newData = FALSE;
  563.                 sei();
  564.                 Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  565.             }
  566.        
  567.             if (Timer_Expired(irRxChannel[channel].timerNum)) {
  568.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  569.             }
  570.             break;
  571.  
  572.         case sns_irTransceive_STATE_START_IDLE:
  573.             if (irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  574.                 /* Send button release command on CAN */
  575.                 irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  576.                 irTxMsg.Data[0] |= channel<<4;
  577.                 irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  578.                 irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  579.                 irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  580.                 irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  581.                 irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  582.  
  583.                 StdCan_Put(&irTxMsg);
  584.             }
  585.             irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  586.             break;
  587.  
  588.         default:
  589.             break;
  590.         }
  591. #endif
  592.  
  593. #if IR_TX_ENABLE==1
  594.         switch (irTxChannel[channel].state)
  595.         {
  596.         case sns_irTransceive_STATE_IDLE:
  597.             /* transmission is started when a command is received on can */
  598.             break;
  599.  
  600.         case sns_irTransceive_STATE_START_TRANSMIT:
  601.         {
  602.             if (expandProtocol(irTxChannel[channel].txbuf, &irTxChannel[channel].txlen, &irTxChannel[channel].proto) == IR_OK)
  603.             {
  604.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].txlen);
  605.                 irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  606.             }
  607.             else
  608.             {
  609.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  610.             }
  611.             break;
  612.         }
  613.         case sns_irTransceive_STATE_START_TRANSMIT_PRONTO:
  614.         {
  615.             /* Start IR transmission. */
  616.             IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].onceSeqLen - 1);
  617.  
  618.             /* Enter transmitting state. */
  619.             irTxChannel[channel].sendingPronto = TRUE;
  620.             irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  621.             break;
  622.         }
  623.         case sns_irTransceive_STATE_PRONTO_REPEAT:
  624.         {
  625.             /* Check if done. */
  626.             if (irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats && irTxChannel[channel].proto.repeats != 0xFF)
  627.             {
  628.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  629.                 break;
  630.             }
  631.  
  632.             if (irTxChannel[channel].repeatCount < 255) {
  633.                 irTxChannel[channel].repeatCount++;
  634.             }
  635.  
  636.             // repeat sequence exists?
  637.             if (irTxChannel[channel].repSeqLen != 0) {
  638.                 // use repeat seq
  639.                 uint16_t offset = ((uint16_t)irTxChannel[channel].onceSeqLen);
  640.                 // don't transmit last passive. last passive handled by timer
  641.                 IrTransceiver_Transmit(channel, &irTxChannel[channel].txbuf[offset], 0, ((uint16_t)irTxChannel[channel].repSeqLen) - 1);
  642.             }
  643.             else {
  644.                 // use once seq
  645.                 // don't transmit last passive. last passive handled by timer
  646.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].txlen - 1);
  647.             }
  648.             irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  649.         }
  650.         case sns_irTransceive_STATE_TRANSMITTING:
  651.         {
  652.             if (irTxChannel[channel].sendComplete == TRUE)
  653.             {
  654.                 cli();
  655.                 irTxChannel[channel].sendComplete = FALSE;
  656.                 sei();
  657.  
  658.                 if (irTxChannel[channel].sendingPronto)
  659.                 {
  660.                     if(irTxChannel[channel].repeatCount == 0 || irTxChannel[channel].repSeqLen == 0)
  661.                     {
  662.                         // use last passive time as timeout
  663.                         uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].onceSeqLen - 1] / 1000;
  664.                         Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
  665.                     }
  666.                     else
  667.                     {
  668.                         // use last passive time as timeout
  669.                         uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].txlen - 1]  / 1000;
  670.                         Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
  671.                     }
  672.                     irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  673.                 }
  674.                 else
  675.                 {
  676.                     irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  677.                 }
  678.             }
  679.             break;
  680.         }
  681.         case sns_irTransceive_STATE_START_PAUSE:
  682.         {
  683.             if (irTxChannel[channel].repeatCount < irTxChannel[channel].proto.repeats)
  684.             {
  685.                 irTxChannel[channel].repeatCount++;
  686.             }
  687.            
  688.             Timer_SetTimeout(irTxChannel[channel].timerNum, irTxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  689.  
  690.             if (irTxChannel[channel].proto.framecnt != 255)
  691.             {
  692.                 irTxChannel[channel].proto.framecnt++;
  693.             }
  694.            
  695.             irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  696.             break;
  697.         }
  698.         case sns_irTransceive_STATE_PAUSING:
  699.         {
  700.             if (irTxChannel[channel].sendingPronto)
  701.             {
  702.                 if (Timer_Expired(irTxChannel[channel].timerNum))
  703.                 {
  704.                     irTxChannel[channel].state = sns_irTransceive_STATE_PRONTO_REPEAT;
  705.                 }
  706.                 break;
  707.             }
  708.  
  709.             if (Timer_Expired(irTxChannel[channel].timerNum))
  710.             {
  711.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  712.             }
  713.            
  714.             /* transmission is stopped when such command is recevied on can */
  715.             if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
  716.             {
  717.                 //TODO maybe send message on can for status? to confirm stopped sending, ready for new command
  718.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  719.             }
  720.             break;
  721.         }
  722.         case sns_irTransceive_STATE_START_IDLE:
  723.             irTxChannel[channel].stopSend = FALSE;
  724.             irTxChannel[channel].repeatCount = 0;
  725.             irTxChannel[channel].proto.framecnt = 0;
  726.             irTxChannel[channel].sendingPronto = FALSE;
  727.             irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  728.             break;
  729.  
  730.         default:
  731.             break;
  732.         }
  733. #endif
  734.  
  735.     }
  736. }
  737.  
  738.  
  739. /* Handle incoming CAN data */
  740. void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  741. {
  742.     /* Sanity check. */
  743.     if (StdCan_Ret_class(rxMsg->Header) != CAN_MODULE_CLASS_SNS ||
  744.         StdCan_Ret_direction(rxMsg->Header) != DIRECTIONFLAG_TO_OWNER ||
  745.         rxMsg->Header.ModuleType != CAN_MODULE_TYPE_SNS_IRTRANSCEIVE ||
  746.         rxMsg->Header.ModuleId != sns_irTransceive_ID) return;
  747.  
  748.     /* Get IR channel. */
  749.     uint8_t channel = rxMsg->Data[0] >> 4;
  750.  
  751.     switch (rxMsg->Header.Command)
  752.     {
  753. #if IR_TX_ENABLE==1
  754.     case CAN_MODULE_CMD_PHYSICAL_IR:
  755.     {
  756.         if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED && channel < IR_SUPPORTED_NUM_CHANNELS)
  757.         {
  758.             if (irTxChannel[channel].state == sns_irTransceive_STATE_IDLE)
  759.             {
  760.                 irTxChannel[channel].proto.protocol = rxMsg->Data[1];
  761.  
  762.                 irTxChannel[channel].proto.data = rxMsg->Data[2];
  763.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  764.                 irTxChannel[channel].proto.data |= rxMsg->Data[3];
  765.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  766.                 irTxChannel[channel].proto.data |= rxMsg->Data[4];
  767.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  768.                 irTxChannel[channel].proto.data |= rxMsg->Data[5];
  769.  
  770.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  771.             }
  772.         }
  773.         else if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED && channel < IR_SUPPORTED_NUM_CHANNELS)
  774.         {
  775.             if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE)
  776.             {
  777.                 irTxChannel[channel].stopSend = TRUE;
  778.             }
  779.         }
  780.         break;
  781.     }
  782.  
  783. #if sns_irTransceive_PRONTO_SUPPORT==1
  784.     /* TODO: add struct which stores pronto info:
  785.     channel, pronto receive state, buffer length of once burst pairs, buffer length of repeat burst pairs */
  786.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTART:
  787.     {
  788.         /* Sanity check. */
  789.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) break;
  790.         if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE) break;
  791.  
  792.         /* Check if format is supported. */
  793.         if ((uint16_t)(rxMsg->Data[0] & 0x0F) << 8 | (uint16_t)(rxMsg->Data[1]) != 0) break;
  794.  
  795.         /* Extract received data. */
  796.         //irTxChannel[channel].modfreq = rxMsg->Data[2];
  797.         irTxChannel[channel].modfreq = (((F_CPU/2000)/IR_NEC_F_MOD) -1); // for testing
  798.         irTxChannel[channel].proto.modfreq = (((F_CPU/2000)/IR_NEC_F_MOD) -1);
  799.         // convert lenghts to bytes
  800.         irTxChannel[channel].onceSeqLen = 2*((((uint16_t)rxMsg->Data[4]) << 8) | (((uint16_t)rxMsg->Data[5]) << 0));
  801.         irTxChannel[channel].repSeqLen = 2*((((uint16_t)rxMsg->Data[6]) << 8) | (((uint16_t)rxMsg->Data[7]) << 0));
  802.  
  803.         /* Enter prepering pronto state and clear transmit buffer. */
  804.         irTxChannel[channel].state = sns_irTransceive_STATE_PREPARING_PRONTO;
  805.         irTxChannel[channel].txlen = 0;
  806.         activeChannel = channel;
  807.  
  808.         /* TODO: Send response frame */
  809.         break;
  810.     }
  811.  
  812.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTOP:
  813.         /* TODO: stop sending IR */
  814.  
  815.         /* TODO: Send response frame */
  816.         break;
  817.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOCONTINUE:
  818.         /* TODO: reset timeout to continue sending repeat sequences */
  819.  
  820.         /* TODO: Send response frame */
  821.         break;
  822.  
  823.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1:  /* Fall through */
  824.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA2:  /* Fall through */
  825.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA3:  /* Fall through */
  826.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA4:  /* Fall through */
  827.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA5:  /* Fall through */
  828.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA6:  /* Fall through */
  829.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA7:  /* Fall through */
  830.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA8:  /* Fall through */
  831.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA9:  /* Fall through */
  832.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA10: /* Fall through */
  833.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA11: /* Fall through */
  834.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA12: /* Fall through */
  835.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA13: /* Fall through */
  836.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA14: /* Fall through */
  837.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA15: /* Fall through */
  838.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA16:
  839.     {
  840.         // unexpected CAN msg, out of order
  841.         if (irTxChannel[activeChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) break;
  842.  
  843.         if ((uint16_t)irTxChannel[activeChannel].txlen + (uint16_t)rxMsg->Length >= MAX_NR_TIMES) {
  844.             // too long
  845.             irTxChannel[activeChannel].state = sns_irTransceive_STATE_IDLE;
  846.             break;
  847.         }
  848.         for (uint8_t i=0; i < rxMsg->Length; i++) {
  849.             // TODO: (109 * 1) / (4.145146 MHz) = 27, fix formula with modfreq
  850.             irTxChannel[activeChannel].txbuf[irTxChannel[activeChannel].txlen++] = 27 * rxMsg->Data[i];
  851.         }
  852.         break;
  853.     }
  854.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1:   /* Fall through */
  855.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND2:   /* Fall through */
  856.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND3:   /* Fall through */
  857.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND4:   /* Fall through */
  858.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND5:   /* Fall through */
  859.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND6:   /* Fall through */
  860.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND7:   /* Fall through */
  861.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND8:   /* Fall through */
  862.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND9:   /* Fall through */
  863.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND10:  /* Fall through */
  864.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND11:  /* Fall through */
  865.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND12:  /* Fall through */
  866.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND13:  /* Fall through */
  867.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND14:  /* Fall through */
  868.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND15:  /* Fall through */
  869.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND16:  /* Fall through */
  870.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND17:
  871.     {
  872.         // unexpected CAN msg, out of order
  873.         if (irTxChannel[activeChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) {
  874.             //printf("StERR:%02d", irTxChannel[activeChannel].state);
  875.             break;
  876.         }
  877.  
  878.         if ((uint16_t)irTxChannel[activeChannel].txlen + (uint16_t)(rxMsg->Length - 1) >= MAX_NR_TIMES) {
  879.             // too long
  880.             irTxChannel[activeChannel].state = sns_irTransceive_STATE_IDLE;
  881.             break;
  882.         }
  883.  
  884.         uint16_t nrBytesRemaining = (((uint16_t)irTxChannel[activeChannel].onceSeqLen + (uint16_t)irTxChannel[activeChannel].repSeqLen)) - irTxChannel[activeChannel].txlen;
  885.         //printf("TXLEN:%02d", irTxChannel[activeChannel].txlen);
  886.         //printf("BYTES:%02d", nrBytesRemaining);
  887.         // end packet does not always contain data, last byte is reserved
  888.         for (uint8_t i=0; i < nrBytesRemaining; i++) {
  889.             // TODO: (109 * 1) / (4.145146 MHz) = 27, fix formula with modfreq
  890.             irTxChannel[activeChannel].txbuf[irTxChannel[activeChannel].txlen++] = 27 * rxMsg->Data[i];
  891.         }
  892.  
  893.         // sanity check
  894.         if(irTxChannel[activeChannel].txlen != (((uint16_t)irTxChannel[activeChannel].onceSeqLen + (uint16_t)irTxChannel[activeChannel].repSeqLen)))
  895.         {
  896.             irTxChannel[activeChannel].state = sns_irTransceive_STATE_IDLE;
  897.             break;
  898.         }
  899.  
  900.         // last byte tells what to do with IR data (nr of repeats)
  901.         irTxChannel[activeChannel].proto.repeats = rxMsg->Data[rxMsg->Length-1];
  902.         irTxChannel[activeChannel].proto.timeout = 1;
  903.  
  904.         // send
  905.         irTxChannel[activeChannel].state = sns_irTransceive_STATE_START_TRANSMIT_PRONTO;
  906.  
  907.         /* TODO: Send response frame */
  908.         break;
  909.     }
  910.  
  911. /* TODO: In IR state machine add sending a response frame when ir stops sending, also implement pronto repeat */
  912. #endif
  913. #endif
  914.  
  915.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRCONFIG: {
  916.         uint8_t config = rxMsg->Data[0] & 0x0f;
  917.         uint8_t power = rxMsg->Data[1]>>6;
  918.         uint8_t modfreq = rxMsg->Data[2];
  919.        
  920.         sns_irTransceive_setConfig(channel, config, power, modfreq);
  921.  
  922. #ifdef sns_irTransceive_USEEEPROM
  923.         if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  924.         {
  925.             switch (channel)
  926.             {
  927.                 case 0:
  928.                     eeprom_write_byte_crc(EEDATA.ch0_config, config, WITHOUT_CRC);
  929.                     eeprom_write_byte_crc(EEDATA.ch0_txpower, power, WITHOUT_CRC);
  930.                     eeprom_write_byte_crc(EEDATA.ch0_modfreq, modfreq, WITHOUT_CRC);
  931.                     break;
  932.                 case 1:
  933.                     eeprom_write_byte_crc(EEDATA.ch1_config, config, WITHOUT_CRC);
  934.                     eeprom_write_byte_crc(EEDATA.ch1_txpower, power, WITHOUT_CRC);
  935.                     eeprom_write_byte_crc(EEDATA.ch1_modfreq, modfreq, WITHOUT_CRC);
  936.                     break;
  937.                 case 2:
  938.                     eeprom_write_byte_crc(EEDATA.ch2_config, config, WITHOUT_CRC);
  939.                     eeprom_write_byte_crc(EEDATA.ch2_txpower, power, WITHOUT_CRC);
  940.                     eeprom_write_byte_crc(EEDATA.ch2_modfreq, modfreq, WITHOUT_CRC);
  941.                     break;
  942.                 default:
  943.                     break;
  944.             }
  945.             EEDATA_UPDATE_CRC;
  946.         }
  947. #endif 
  948.         break;
  949.     }
  950.  
  951.     default:
  952.         break;
  953.  
  954.     }
  955.  
  956. }
  957.  
  958. void sns_irTransceive_List(uint8_t ModuleSequenceNumber)
  959. {
  960.     StdCan_Msg_t txMsg;
  961.    
  962.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  963.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  964.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  965.  
  966.     txMsg.Header.ModuleId = sns_irTransceive_ID;
  967.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  968.     txMsg.Length = 6;
  969.  
  970.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  971.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  972.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  973.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  974.    
  975.     txMsg.Data[4] = NUMBER_OF_MODULES;
  976.     txMsg.Data[5] = ModuleSequenceNumber;
  977.    
  978.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  979. }
  980.