Subversion Repositories HomeAutomation

Rev

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

  1. #include "sns_irTransceive.h"
  2.  
  3. #if sns_irTransceive_USEEEPROM==1
  4. #include "sns_irTransceive_eeprom.h"
  5. struct eeprom_sns_irTransceive EEMEM eeprom_sns_irTransceive =
  6. {
  7.     {
  8.         /* Define initialization values on the EEPROM variables here.
  9.         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.
  10.         Write the values in the exact same order as the struct is defined in the *.h file. */
  11.         CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO,
  12.         0,
  13.         CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO,
  14.         0,
  15.         CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO,
  16.         0,
  17.     },
  18.     0   /* crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts. */
  19. };
  20. #endif
  21.  
  22. #if IR_RX_ENABLE==1
  23. struct {
  24.     uint8_t             state;
  25.     uint16_t            *rxbuf;
  26.     uint8_t             rxlen;
  27.     uint8_t             timerNum;
  28.     Ir_Protocol_Data_t  proto;
  29.     uint8_t             newData;
  30. } irRxChannel[IR_SUPPORTED_NUM_CHANNELS];
  31. #endif
  32.  
  33. #if IR_TX_ENABLE==1
  34. struct {
  35.     uint8_t             state;
  36.     uint16_t            *txbuf;
  37.     uint8_t             txlen;
  38.     uint8_t             timerNum;
  39.     uint8_t             repeatCount;
  40.     Ir_Protocol_Data_t  proto;
  41.     uint8_t             stopSend;
  42.     uint8_t             sendComplete;
  43.     /* Pronto params. */
  44.     uint8_t             sendingPronto;
  45.     uint8_t             onceSeqLen;
  46.     uint8_t             repSeqLen;
  47.     uint8_t             expectedSeqNr;
  48. } irTxChannel[IR_SUPPORTED_NUM_CHANNELS];
  49. #endif
  50.  
  51. uint16_t    buf[IR_SUPPORTED_NUM_CHANNELS][MAX_NR_TIMES];
  52.  
  53. StdCan_Msg_t        irTxMsg;
  54.  
  55. #if (sns_irTransceive_SEND_DEBUG==1)
  56. void send_debug(uint16_t *buffer, uint8_t len) {
  57.     StdCan_Msg_t dbgIrTxMsg;
  58.     /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol. */
  59.  
  60.     StdCan_Set_class(dbgIrTxMsg.Header, CAN_MODULE_CLASS_SNS);
  61.     StdCan_Set_direction(dbgIrTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  62.     dbgIrTxMsg.Length = 8;
  63.     dbgIrTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  64.     dbgIrTxMsg.Header.ModuleId = sns_irTransceive_ID;
  65.     dbgIrTxMsg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRRAW;
  66.     for (uint8_t i = 0; i < len>>2; i++) {
  67.         uint8_t index = i<<2;
  68.  
  69.         dbgIrTxMsg.Data[0] = (buffer[index]>>8)&0xff;
  70.         dbgIrTxMsg.Data[1] = (buffer[index]>>0)&0xff;
  71.         dbgIrTxMsg.Data[2] = (buffer[index+1]>>8)&0xff;
  72.         dbgIrTxMsg.Data[3] = (buffer[index+1]>>0)&0xff;
  73.         dbgIrTxMsg.Data[4] = (buffer[index+2]>>8)&0xff;
  74.         dbgIrTxMsg.Data[5] = (buffer[index+2]>>0)&0xff;
  75.         dbgIrTxMsg.Data[6] = (buffer[index+3]>>8)&0xff;
  76.         dbgIrTxMsg.Data[7] = (buffer[index+3]>>0)&0xff;
  77.  
  78.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent. */
  79.         while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
  80.         _delay_ms(1);
  81.     }
  82.  
  83.     uint8_t lastpacketcnt = len&0x03;
  84.     if (lastpacketcnt > 0) {
  85.         dbgIrTxMsg.Length = lastpacketcnt<<1;
  86.         for (uint8_t i = 0; i < lastpacketcnt; i++) {
  87.             dbgIrTxMsg.Data[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff;
  88.             dbgIrTxMsg.Data[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff;
  89.         }
  90.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent. */
  91.         while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
  92.         _delay_ms(1);
  93.     }
  94.  
  95. }
  96. #endif
  97.  
  98. #define sns_irTransceive_BaseFrq (4145146UL)
  99.  
  100. #define channelIsOk(_ch)    ((_ch)>=0 && (_ch)<IR_SUPPORTED_NUM_CHANNELS)
  101.  
  102. /*
  103.  * PRONTO HEX Routines
  104.  */
  105. #if IR_TX_ENABLE==1
  106. static void pronto_sendResponse(uint8_t channel, uint8_t responseValue)
  107. {
  108.     StdCan_Msg_t msg;
  109.     StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
  110.     StdCan_Set_direction(msg.Header, DIRECTIONFLAG_FROM_OWNER);
  111.     msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  112.     msg.Header.ModuleId = sns_irTransceive_ID;
  113.     /* Send response command. */
  114.     msg.Length = 3;
  115.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTORESPONSE;
  116.     msg.Data[0] = ((channel<<4)|0xf);
  117.     msg.Data[1] = 0xF0;
  118.     msg.Data[2] = (uint8_t)responseValue;
  119.     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  120.     _delay_ms(1);
  121. }
  122.  
  123. static uint8_t prontoAllocatedTxChannel = -1;
  124. #endif
  125.  
  126. #if sns_irTransceive_PRONTO_SUPPORT==1
  127. volatile uint32_t sns_irTransceive_LastPronto=0;
  128.  
  129. #if IR_TX_ENABLE==1
  130. /* is any channel transmitting (or preparing to transmit) pronto? */
  131. static inline int8_t prontoChannelIsAllocated(void)
  132. {
  133.     if (prontoAllocatedTxChannel >= 0 &&
  134.         prontoAllocatedTxChannel < IR_SUPPORTED_NUM_CHANNELS &&
  135.         irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_DISABLED)
  136.     {
  137.         return 1;
  138.     }
  139.     return 0;
  140. }
  141. #endif
  142.  
  143. #define sns_irTransceive_MAXTIMING (16*1000)
  144.  
  145. static void pronto_sendData(uint16_t *buffer, uint8_t len, uint8_t channel, uint16_t modfreq)
  146. {
  147.     uint32_t currentTime = Timer_GetTicks();
  148.  
  149.     StdCan_Msg_t msg;
  150.  
  151.     StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
  152.     StdCan_Set_direction(msg.Header, DIRECTIONFLAG_FROM_OWNER);
  153.     msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  154.     msg.Header.ModuleId = sns_irTransceive_ID;
  155.  
  156.     /* Send timing command. */
  157.     msg.Length = 5;
  158.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOTIMING;
  159.  
  160.     if ((currentTime < sns_irTransceive_LastPronto+sns_irTransceive_MAXTIMING) && (sns_irTransceive_LastPronto != 0))
  161.     {
  162.         /* Calculate elapsed time since last pronto packet. */
  163.         uint32_t elapsedTime = ((currentTime - sns_irTransceive_LastPronto) * 1000) & 0x00FFFFFFUL; /* 24bit value. */
  164.  
  165.         /* Remove length of the last received sequence. */
  166.         uint32_t sequenceLength = IR_MAX_PULSE_WIDTH; /* timeout value not included in buffer. */
  167.         for (uint8_t i = 0; i < len; i++) {
  168.             sequenceLength += (uint32_t)buffer[i];
  169.         }
  170.         if (sequenceLength <= elapsedTime) {
  171.             elapsedTime -= sequenceLength;
  172.         }
  173.  
  174.         msg.Data[0] = ((channel<<4)|0xf);
  175.         msg.Data[1] = 0x10;
  176.         msg.Data[2] = (uint8_t)(elapsedTime >> 16);
  177.         msg.Data[3] = (uint8_t)(elapsedTime >> 8);
  178.         msg.Data[4] = (uint8_t)(elapsedTime >> 0);
  179.         while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  180.         _delay_ms(1);
  181.     }
  182.     sns_irTransceive_LastPronto = currentTime;
  183.  
  184.     /* Calculate frequency divider. */
  185.     uint16_t divider = (uint16_t)(sns_irTransceive_BaseFrq / (1000*(uint32_t)modfreq));
  186.  
  187.     /* Send pronto start. */
  188.     msg.Length = 8;
  189.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTART;
  190.  
  191.     msg.Data[0] = ((channel<<4)|0x0);   /* Channel and Prontoformat 0x0000. */
  192.     msg.Data[1] = 0x00;
  193.     msg.Data[2] = (divider & 0xFF00) >> 8;  /* Freq divider. */
  194.     msg.Data[3] = (divider & 0x00FF) >> 0;
  195.     msg.Data[4] = 0x00; /* Once seq length, first byte always 0. */
  196.     msg.Data[5] = (len + 1) / 2;
  197.     msg.Data[6] = 0x00; /* Repeat seq length, always 0 for ir receive. */
  198.     msg.Data[7] = 0x00;
  199.  
  200.     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  201.     _delay_ms(1);
  202.  
  203.     /* Send pronto data. */
  204.     //uint16_t divider = ((1000000ULL*modfreq)/sns_irTransceive_BaseFrq);
  205.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1;
  206.     uint8_t seqNr = 0;
  207.     /* Counter for buffer. */
  208.     uint8_t i = 0;
  209.     /* Counter for filling can frame data. */
  210.     uint8_t j = 0;
  211.     /* Remember byte if data does not fit 8 bit. */
  212.     uint8_t mem = 0;
  213.     while (i < len)
  214.     {
  215.         if (mem > 0)
  216.         {
  217.             /* If byte was memorized then buffer it. */
  218.             msg.Data[j] = mem;
  219.             /* Clear memory. */
  220.             mem = 0;
  221.         }
  222.         else
  223.         {
  224.             /* Convert µs to pronto hex modulation pulses. */
  225.             uint16_t data = (uint16_t)(((uint32_t)buffer[i] * (uint32_t)modfreq) / 1000);
  226.  
  227.             /* Protocol compression. */
  228.             if (data >= 256)
  229.             {
  230.                 /* If data does not fit into 8 bit, then split, memorize low byte. */
  231.                 mem = data&0xff;
  232.                 /* Buffer high byte. */
  233.                 msg.Data[j] = (data>>8)&0xff;
  234.                 j++;
  235.                 if (j==8)
  236.                 {
  237.                     /* If send buffer is full, send frame. */
  238.                     /* Can buffers will be filled when sending more than 2-3 messages, so retry until sent. */
  239.                     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  240.                     _delay_ms(1);
  241.                     j=0;
  242.                     seqNr = (seqNr + 1) % 16;
  243.                     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1 + seqNr;
  244.                 }
  245.                 /* Set complementary burst pair to 0 to indicate 16bit transmission. */
  246.                 msg.Data[j] = 0;
  247.             }
  248.             else
  249.             {
  250.                 msg.Data[j] = data&0xff;
  251.             }
  252.  
  253.             i++;
  254.         }
  255.  
  256.         j++;
  257.         if (j==8)
  258.         {
  259.             /* If send buffer is full, send frame. */
  260.             /* Can buffers will be filled when sending more than 2-3 messages, so retry until sent. */
  261.             while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  262.             _delay_ms(1);
  263.             j=0;
  264.             seqNr = (seqNr + 1) % 16;
  265.             msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1 + seqNr;
  266.         }
  267.     }
  268.  
  269.     /* End with max pulse. TODO: fix uglyness. */
  270.     msg.Data[j] = ((IR_MAX_PULSE_WIDTH/((1000000ULL*109)/sns_irTransceive_BaseFrq))>>8) & 0xFF;
  271.     j++;
  272.     if (j==8)
  273.     {
  274.         /* If send buffer is full, send frame. */
  275.         /* Can buffers will be filled when sending more than 2-3 messages, so retry until sent. */
  276.         while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  277.         _delay_ms(1);
  278.         j=0;
  279.         seqNr = (seqNr + 1) % 16;
  280.         msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1 + seqNr;
  281.     }
  282.     msg.Data[j] = 0;
  283.     j++;
  284.     msg.Data[j] = (IR_MAX_PULSE_WIDTH/((1000000ULL*109)/sns_irTransceive_BaseFrq))&0xff;
  285.     j++;
  286.     if (j==8)
  287.     {
  288.         /* If send buffer is full, send frame. */
  289.         /* Can buffers will be filled when sending more than 2-3 messages, so retry until sent. */
  290.         while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  291.         _delay_ms(1);
  292.         j=0;
  293.         seqNr = (seqNr + 1) % 16;
  294.         msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1 + seqNr;
  295.     }
  296.  
  297.     /* Msg command kept from for loop, but increase 16 to send ProntoEnd. */
  298.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1 + seqNr;
  299.     msg.Length = j;
  300.     /* Data kept from loop, if any. */
  301.     /* Buffers will be filled when sending more than 2-3 messages, so retry until sent. */
  302.     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  303.     _delay_ms(1);
  304. }
  305.  
  306.  
  307. #if IR_TX_ENABLE==1
  308. static int decodeProntoData(int channel, uint8_t data)
  309. {
  310.     static uint8_t waitingForLSB = FALSE;
  311.     static uint8_t previousValueWas16bit = FALSE;
  312.  
  313.     if (irTxChannel[channel].txlen >= MAX_NR_TIMES) {
  314.         /* Max length exceeded. */
  315.         return -1;
  316.     }
  317.  
  318.     if (irTxChannel[channel].txlen == 0) {
  319.         waitingForLSB = FALSE;
  320.         if (data == 0) {
  321.             /* Cannot start with 0x00. */
  322.             return -2;
  323.         }
  324.     }
  325.  
  326.     /* 0x00 means that previously received time is MSB of 16bit value, and that next byte will be LSB. */
  327.     if (data == 0 && !waitingForLSB && previousValueWas16bit==FALSE) {
  328.         /* Convert previous time to MSB of the 16bit value. next received byte will become LSB. */
  329.         irTxChannel[channel].txbuf[irTxChannel[channel].txlen - 1] <<= 8;
  330.  
  331.         /* Flag that next received byte is LSB rather than a new timing value. */
  332.         waitingForLSB = TRUE;
  333.     }
  334.  
  335.     /* Non-zero value! is this part of a 16bit-value? use the data as LSB for previously received time. */
  336.     else if (waitingForLSB) {
  337.         /* Let this byte be LSB of previous 16bit value. */
  338.         irTxChannel[channel].txbuf[irTxChannel[channel].txlen - 1] |= data;
  339.  
  340.         /* We've now completed reception of the 16bit value. */
  341.         waitingForLSB = FALSE;
  342.  
  343.         previousValueWas16bit = TRUE;
  344.     }
  345.  
  346.     /* Non-zero value! simply new 8bit value. */
  347.     else {
  348.         /* Check if we're already done. */
  349.         if (irTxChannel[channel].txlen >= (((uint16_t)irTxChannel[channel].onceSeqLen + (uint16_t)irTxChannel[channel].repSeqLen))) {
  350.             /* Too much data received, not necessarily a problem in case frames are padded. */
  351.             return 2;
  352.         }
  353.  
  354.         irTxChannel[channel].txbuf[irTxChannel[channel].txlen] = data;
  355.  
  356.         previousValueWas16bit = FALSE;
  357.  
  358.         irTxChannel[channel].txlen++;
  359.     }
  360.  
  361.     return 1;
  362. }
  363. #endif
  364.  
  365. #endif
  366.  
  367.  
  368. #if IR_RX_ENABLE==1
  369. void sns_irTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
  370. {
  371.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  372.     {
  373.         irRxChannel[channel].newData = TRUE;
  374.         irRxChannel[channel].rxlen = len;
  375.     }
  376. }
  377. #endif
  378.  
  379.  
  380. #if IR_TX_ENABLE==1
  381. void sns_irTransceive_TX_done_callback(uint8_t channel)
  382. {
  383.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  384.     {
  385.         irTxChannel[channel].sendComplete = TRUE;
  386.     }
  387. }
  388. #endif
  389.  
  390.  
  391. static void sns_irTransceive_setConfig(uint8_t channel, uint8_t config, uint8_t power, uint16_t modfreq)
  392. {
  393.     if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  394.     {
  395. #if IR_RX_ENABLE==1
  396.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE)
  397.         {
  398. #if IR_TX_ENABLE==1
  399.             irTxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  400. #endif
  401.             irRxChannel[channel].proto.modfreq = modfreq;
  402.             irRxChannel[channel].rxbuf = buf[channel];
  403.             switch (channel)
  404.             {
  405.                 case 0:
  406. #ifdef sns_irTransceive_VCC_EN0_PIN
  407.                     gpio_clr_pin(sns_irTransceive_VCC_EN0_PIN);
  408. #endif
  409. #ifdef sns_irTransceive_RX0_PIN
  410.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  411. #endif
  412.                     break;
  413.                 case 1:
  414. #ifdef sns_irTransceive_VCC_EN1_PIN
  415.                     gpio_clr_pin(sns_irTransceive_VCC_EN1_PIN);
  416. #endif
  417. #ifdef sns_irTransceive_RX1_PIN
  418.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  419. #endif
  420.                     break;
  421.                 case 2:
  422. #ifdef sns_irTransceive_VCC_EN2_PIN
  423.                     gpio_clr_pin(sns_irTransceive_VCC_EN2_PIN);
  424. #endif
  425. #ifdef sns_irTransceive_RX2_PIN
  426.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  427. #endif
  428.                     break;
  429.             }
  430.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  431.         }
  432. #endif
  433.  
  434. #if IR_TX_ENABLE==1
  435.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_TRANSMIT)
  436.         {
  437. #if IR_RX_ENABLE==1
  438.             irRxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  439. #endif
  440.             irTxChannel[channel].txbuf = buf[channel];
  441.             switch (channel)
  442.             {
  443.                 case 0:
  444. #if IR_RX_ENABLE==1
  445. #ifdef sns_irTransceive_VCC_EN0_PIN
  446.                     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  447. #endif
  448.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  449. #endif
  450.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX0_PIN);
  451.  
  452. #if sns_irTransceive_ENABLE_PCA95xx==1
  453.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  454.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  455.                     if (power&0x1)
  456.                     {
  457.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRl);
  458.                     }
  459.                     if (power&0x2)
  460.                     {
  461.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRh);
  462.                     }
  463. #endif
  464.                     break;
  465.                 case 1:
  466. #if IR_RX_ENABLE==1
  467. #ifdef sns_irTransceive_VCC_EN1_PIN
  468.                     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  469. #endif
  470.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  471. #endif
  472.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX1_PIN);
  473.  
  474. #if sns_irTransceive_ENABLE_PCA95xx==1
  475.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  476.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  477.                     if (power&0x1)
  478.                     {
  479.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRl);
  480.                     }
  481.                     if (power&0x2)
  482.                     {
  483.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRh);
  484.                     }
  485. #endif
  486.                     break;
  487.                 case 2:
  488. #if IR_RX_ENABLE==1
  489. #ifdef sns_irTransceive_VCC_EN2_PIN
  490.                     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  491. #endif
  492.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  493. #endif
  494.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX2_PIN);
  495.  
  496. #if sns_irTransceive_ENABLE_PCA95xx==1
  497.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  498.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  499.                     if (power&0x1)
  500.                     {
  501.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRl);
  502.                     }
  503.                     if (power&0x2)
  504.                     {
  505.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRh);
  506.                     }
  507. #endif
  508.                     break;
  509.             }
  510.             irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  511.         }
  512. #endif
  513.     }
  514. }
  515.  
  516.  
  517. void sns_irTransceive_Init(void)
  518. {
  519.     StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
  520.     StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  521.     irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  522.     irTxMsg.Header.ModuleId = sns_irTransceive_ID;
  523.     irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
  524.     irTxMsg.Length = 6;
  525.  
  526.     for (uint8_t i = 0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  527.     {
  528. #if IR_RX_ENABLE==1
  529.         irRxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  530.         irRxChannel[i].newData = FALSE;
  531.         irRxChannel[i].rxlen = 0;
  532.         irRxChannel[i].proto.timeout=0;
  533.         irRxChannel[i].proto.data=0;
  534.         irRxChannel[i].proto.repeats=0;
  535.         irRxChannel[i].proto.protocol=0;
  536. #endif
  537.  
  538. #if IR_TX_ENABLE==1
  539.         irTxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  540.         irTxChannel[i].sendComplete = FALSE;
  541.         irTxChannel[i].repeatCount = 0;
  542.         irTxChannel[i].txlen = 0;
  543.         irTxChannel[i].proto.data=0;
  544.         irTxChannel[i].proto.repeats=0;
  545.         irTxChannel[i].proto.framecnt=0;
  546.         irTxChannel[i].proto.protocol=0;
  547. #endif
  548.     }
  549.  
  550. #if IR_RX_ENABLE==1
  551. #ifdef sns_irTransceive_RX0_REPEATE_TIMER
  552.     if (IR_SUPPORTED_NUM_CHANNELS>=1) irRxChannel[0].timerNum=sns_irTransceive_RX0_REPEATE_TIMER;
  553. #endif
  554. #ifdef sns_irTransceive_RX1_REPEATE_TIMER
  555.     if (IR_SUPPORTED_NUM_CHANNELS>=2) irRxChannel[1].timerNum=sns_irTransceive_RX1_REPEATE_TIMER;
  556. #endif
  557. #ifdef sns_irTransceive_RX2_REPEATE_TIMER
  558.     if (IR_SUPPORTED_NUM_CHANNELS>=3) irRxChannel[2].timerNum=sns_irTransceive_RX2_REPEATE_TIMER;
  559. #endif
  560. #endif
  561.  
  562. #if IR_TX_ENABLE==1
  563.     if (IR_SUPPORTED_NUM_CHANNELS>=1) irTxChannel[0].timerNum=sns_irTransceive_TX0_REPEATE_TIMER;
  564.     if (IR_SUPPORTED_NUM_CHANNELS>=2) irTxChannel[1].timerNum=sns_irTransceive_TX1_REPEATE_TIMER;
  565.     if (IR_SUPPORTED_NUM_CHANNELS>=3) irTxChannel[2].timerNum=sns_irTransceive_TX2_REPEATE_TIMER;
  566. #endif
  567.  
  568.     IrTransceiver_Init();
  569.     /* Configure all TX VCC pins as outputs and disable them. */
  570. #ifdef sns_irTransceive_VCC_EN0_PIN
  571.     gpio_set_out(sns_irTransceive_VCC_EN0_PIN);
  572.     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  573. #endif
  574. #ifdef sns_irTransceive_VCC_EN1_PIN
  575.     gpio_set_out(sns_irTransceive_VCC_EN1_PIN);
  576.     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  577. #endif
  578. #ifdef sns_irTransceive_VCC_EN2_PIN
  579.     gpio_set_out(sns_irTransceive_VCC_EN2_PIN);
  580.     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  581. #endif
  582.  
  583. #if IR_TX_ENABLE==1
  584.     gpio_set_out(sns_irTransceive_MOD_PIN);
  585.     gpio_set_out(sns_irTransceive_TX0_PIN);
  586.     gpio_set_out(sns_irTransceive_TX1_PIN);
  587.     gpio_set_out(sns_irTransceive_TX2_PIN);
  588. #if IR_TX_ACTIVE_LOW==1
  589.     gpio_set_pin(sns_irTransceive_TX0_PIN);
  590.     gpio_set_pin(sns_irTransceive_TX1_PIN);
  591.     gpio_set_pin(sns_irTransceive_TX2_PIN);
  592. #endif
  593. #endif
  594.  
  595.     /* IR tx power pins on PCA95xx. */
  596. #if sns_irTransceive_ENABLE_PCA95xx==1
  597.     Pca95xx_Init(0);
  598.  
  599.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRl);
  600.     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  601.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRh);
  602.     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  603.  
  604.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRl);
  605.     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  606.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRh);
  607.     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  608.  
  609.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRl);
  610.     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  611.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRh);
  612.     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  613. #endif
  614.  
  615. #if sns_irTransceive_USEEEPROM==1
  616.     if (EEDATA_OK)
  617.     {
  618.       /* Use stored data to set initial values for the module. */
  619.         sns_irTransceive_setConfig(0, eeprom_read_byte(EEDATA.ch0_config), eeprom_read_byte(EEDATA.ch0_txpower), eeprom_read_word(EEDATA16.ch0_modfreq));
  620.         sns_irTransceive_setConfig(1, eeprom_read_byte(EEDATA.ch1_config), eeprom_read_byte(EEDATA.ch1_txpower), eeprom_read_word(EEDATA16.ch1_modfreq));
  621.         sns_irTransceive_setConfig(2, eeprom_read_byte(EEDATA.ch2_config), eeprom_read_byte(EEDATA.ch2_txpower), eeprom_read_word(EEDATA16.ch2_modfreq));
  622.     }
  623.     else
  624.     {
  625.     /* The CRC of the EEPROM is not correct, store default values and update CRC. */
  626.         eeprom_write_byte_crc(EEDATA.ch0_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  627.         eeprom_write_byte_crc(EEDATA.ch0_txpower, 0, WITHOUT_CRC);
  628.         eeprom_write_word_crc(EEDATA16.ch0_modfreq, sns_irTransceive_BaseFrq/38000UL, WITHOUT_CRC);
  629.         eeprom_write_byte_crc(EEDATA.ch1_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  630.         eeprom_write_byte_crc(EEDATA.ch1_txpower, 0, WITHOUT_CRC);
  631.         eeprom_write_word_crc(EEDATA16.ch1_modfreq, sns_irTransceive_BaseFrq/38000UL, WITHOUT_CRC);
  632.         eeprom_write_byte_crc(EEDATA.ch2_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  633.         eeprom_write_byte_crc(EEDATA.ch2_txpower, 0, WITHOUT_CRC);
  634.         eeprom_write_word_crc(EEDATA16.ch2_modfreq, sns_irTransceive_BaseFrq/38000UL, WITHOUT_CRC);
  635.         EEDATA_UPDATE_CRC;
  636.     }
  637. #endif
  638.  
  639. /*gpio_clr_pin(EXP_K);
  640. gpio_set_out(EXP_K);
  641. gpio_clr_pin(EXP_J);
  642. gpio_set_out(EXP_J);
  643. gpio_clr_pin(EXP_H);
  644. gpio_set_out(EXP_H);*/
  645. }
  646.  
  647. void sns_irTransceive_Process(void)
  648. {
  649.     for (uint8_t channel=0; channel < IR_SUPPORTED_NUM_CHANNELS; channel++)
  650.     {
  651.  
  652. #if IR_RX_ENABLE==1
  653.         switch (irRxChannel[channel].state)
  654.         {
  655.         case sns_irTransceive_STATE_IDLE:
  656.         {
  657.             if (irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  658.                 /* Send button release command on CAN. */
  659.                 irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  660.                 irTxMsg.Data[0] |= channel<<4;
  661.                 irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  662.                 irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  663.                 irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  664.                 irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  665.                 irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  666.  
  667.                 StdCan_Put(&irTxMsg);
  668.             }
  669.             irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
  670.             break;
  671.         }
  672.  
  673.         case sns_irTransceive_STATE_START_RECEIVE:
  674.             IrTransceiver_ResetRx(channel);
  675.             cli();
  676.             irRxChannel[channel].newData = FALSE;
  677.             sei();
  678.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  679. //gpio_set_pin(EXP_J);
  680.             break;
  681.  
  682.         case sns_irTransceive_STATE_RECEIVING:
  683.             if (irRxChannel[channel].newData == TRUE) {
  684. //printf("sRx\n");
  685.                 /* TODO: move this line to the RX callback. */
  686.                 IrTransceiver_DisableRx(channel);
  687.                 cli();
  688.                 irRxChannel[channel].newData = FALSE;
  689.                 sei();
  690.  
  691.                 /* Let protocol driver parse and then send on CAN. */
  692.                 uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
  693.                 if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  694.                     irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  695.                     irTxMsg.Data[0] |= channel<<4;
  696.                     irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  697.                     irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  698.                     irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  699.                     irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  700.                     irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  701.  
  702.                     StdCan_Put(&irTxMsg);
  703.                 }
  704.                 else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
  705.                 {
  706. #if (sns_irTransceive_SEND_DEBUG==1)
  707.                     send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
  708.                     irRxChannel[channel].proto.timeout=300;
  709. #elif sns_irTransceive_PRONTO_SUPPORT==1
  710.                     pronto_sendData(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, channel, irRxChannel[channel].proto.modfreq);
  711.                     irRxChannel[channel].proto.timeout=1;
  712. #endif
  713.                 }
  714.  
  715.                 /* Enable the receiver again. */
  716.                 IrTransceiver_EnableRx(channel);
  717.  
  718.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  719. //gpio_clr_pin(EXP_H);
  720. //gpio_clr_pin(EXP_J);
  721.             }
  722.             break;
  723.  
  724.         case sns_irTransceive_STATE_START_PAUSE:
  725. //printf("sP\n");
  726.             /* Set a timer so we can send release button event when no new IR is arriving. */
  727.             Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  728.             irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  729.             break;
  730.  
  731.         case sns_irTransceive_STATE_PAUSING:
  732.             /* Reset timer if new IR arrived. */
  733.             if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
  734. //printf("sT\n");
  735.                 cli();
  736.                 irRxChannel[channel].newData = FALSE;
  737.                 sei();
  738.                 Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  739.             }
  740.  
  741.             if (Timer_Expired(irRxChannel[channel].timerNum)) {
  742. //printf("sD\n");
  743.                 irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  744.             }
  745.             break;
  746.  
  747.         default:
  748.             break;
  749.         }
  750. #endif
  751.  
  752. #if IR_TX_ENABLE==1
  753.         switch (irTxChannel[channel].state)
  754.         {
  755.         case sns_irTransceive_STATE_IDLE:
  756.         {
  757.             /* Transmission is started when a command is received on can. */
  758.             irTxChannel[channel].stopSend = FALSE;
  759.             irTxChannel[channel].repeatCount = 0;
  760.             irTxChannel[channel].proto.framecnt = 0;
  761.             irTxChannel[channel].sendingPronto = FALSE;
  762.             break;
  763.         }
  764.  
  765.         case sns_irTransceive_STATE_START_TRANSMIT:
  766.         {
  767.             /* Expand protocol. */
  768.             if (expandProtocol(irTxChannel[channel].txbuf, &irTxChannel[channel].txlen, &irTxChannel[channel].proto) != IR_OK) {
  769.                 /* Failed to expand protocol -> enter idle state. */
  770.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  771.                 break;
  772.             }
  773.  
  774.             /* Start IR transmission. */
  775.             /* TODO Send respone. Function call may fail when different modulation frequencies are used simultaneously. */
  776.             IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].txlen, irTxChannel[channel].proto.modfreq);
  777.  
  778.             /* Enter transmitting state. */
  779.             irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  780.             break;
  781.         }
  782.  
  783.         case sns_irTransceive_STATE_START_TRANSMIT_PRONTO:
  784.         {
  785.             /* Once sequence exists? */
  786.             if (irTxChannel[channel].onceSeqLen>0) {
  787.                 /* Start IR transmission of the once sequence. */
  788.                 int res;
  789.                 if((res=IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].onceSeqLen - 1, irTxChannel[channel].proto.modfreq)) < 0) {
  790.                     /* Send response code. */
  791.                     switch (res) {
  792.                         case -1: /* Invalid freq. */
  793.                         case -2: /* No data. */
  794.                             /* Generic error code, probably invalid packet. */
  795.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  796.                             break;
  797.                         case -3: /* Channel busy. */
  798.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_CHANNELBUSY);
  799.                             break;
  800.                         case -4: /* Frequency cannot be changed due to transmission on other channels. */
  801.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_OTHERFREQUENCYLOCKED);
  802.                             break;
  803.                     }
  804.                     /* Enter idle state. */
  805.                     irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  806.                     prontoAllocatedTxChannel = -1;
  807.                     break;
  808.                 }
  809.                 else {
  810.                     /* Enter transmitting state. */
  811.                     irTxChannel[channel].sendingPronto = TRUE;
  812.                     irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  813.                     break;
  814.                 }
  815.             }
  816.             /* Repeat sequence exists? */
  817.             else if (irTxChannel[channel].repSeqLen>0) {
  818.                 /* Start IR transmission of the repeat sequence. */
  819.                 int res;
  820.                 if((res=IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].repSeqLen - 1, irTxChannel[channel].proto.modfreq)) < 0) {
  821.                     /* Send response code. */
  822.                     switch (res) {
  823.                         case -1: /* Invalid freq. */
  824.                         case -2: /* No data. */
  825.                             /* Generic error code, probably invalid packet. */
  826.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  827.                             break;
  828.                         case -3: /* Channel busy. */
  829.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_CHANNELBUSY);
  830.                             break;
  831.                         case -4: /* Frequency cannot be changed due to transmission on other channels. */
  832.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_OTHERFREQUENCYLOCKED);
  833.                             break;
  834.                     }
  835.                     /* Enter idle state. */
  836.                     irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  837.                     prontoAllocatedTxChannel = -1;
  838.                     break;
  839.                 }
  840.                 else {
  841.                     /* Enter transmitting state. */
  842.                     irTxChannel[channel].sendingPronto = TRUE;
  843.                     irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  844.                     break;
  845.                 }
  846.             }
  847.             /* Neither once sequence nor repeat sequence exists! */
  848.             /* Generic error code, probably invalid packet. */
  849.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  850.             /* Enter idle state. */
  851.             irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  852.             prontoAllocatedTxChannel = -1;
  853.             break;
  854.         }
  855.  
  856.         case sns_irTransceive_STATE_PRONTO_REPEAT:
  857.         {
  858.             /* Check if done. */
  859.             if ((irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats && irTxChannel[channel].proto.repeats != 0xFF) || irTxChannel[channel].stopSend) {
  860.                 /* Pronto transmission was stopped/completed. */
  861.                 pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_STOPPED);
  862.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  863.                 prontoAllocatedTxChannel = -1;
  864.                 break;
  865.             }
  866.  
  867.             if (irTxChannel[channel].repeatCount < 255) {
  868.                 irTxChannel[channel].repeatCount++;
  869.             }
  870.  
  871.             /* Repeat sequence exists? */
  872.             if (irTxChannel[channel].repSeqLen > 0) {
  873.                 /* Use repeat seq. */
  874.                 uint16_t offset = ((uint16_t)irTxChannel[channel].onceSeqLen);
  875.                 /* Don't transmit last passive. last passive handled by timer. */
  876.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, offset, ((uint16_t)irTxChannel[channel].repSeqLen) - 1, irTxChannel[channel].proto.modfreq);
  877.             }
  878.             /* Once sequence exists? */
  879.             else if (irTxChannel[channel].onceSeqLen > 0) {
  880.                 /* Use once seq. */
  881.                 /* Don't transmit last passive. last passive handled by timer. */
  882.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].txlen - 1, irTxChannel[channel].proto.modfreq);
  883.             }
  884.             irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  885.         }
  886.  
  887.         case sns_irTransceive_STATE_TRANSMITTING:
  888.         {
  889.             if (irTxChannel[channel].sendComplete == TRUE)
  890.             {
  891.                 cli();
  892.                 irTxChannel[channel].sendComplete = FALSE;
  893.                 sei();
  894.  
  895.                 if (irTxChannel[channel].sendingPronto)
  896.                 {
  897.                     /* First repeat, or no repeat sequence defined => use last passive time in once seq. */
  898.                     if(irTxChannel[channel].repeatCount == 0 || irTxChannel[channel].repSeqLen == 0)
  899.                     {
  900.                         /* Use last passive time as timeout. */
  901.                         uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].onceSeqLen - 1] / 1000;
  902.                         Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
  903.                     }
  904.                     /* Second, or later repeat => use last passive time in repeat seq. */
  905.                     else
  906.                     {
  907.                         /* Use last passive time as timeout. */
  908.                         uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].txlen - 1]  / 1000;
  909.                         Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
  910.                     }
  911.                     irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  912.                 }
  913.                 else
  914.                 {
  915.                     irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  916.                 }
  917.             }
  918.             break;
  919.         }
  920.  
  921.         case sns_irTransceive_STATE_START_PAUSE:
  922.         {
  923.             if (irTxChannel[channel].repeatCount < irTxChannel[channel].proto.repeats)
  924.             {
  925.                 irTxChannel[channel].repeatCount++;
  926.             }
  927.  
  928.             Timer_SetTimeout(irTxChannel[channel].timerNum, irTxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  929.  
  930.             if (irTxChannel[channel].proto.framecnt != 255)
  931.             {
  932.                 irTxChannel[channel].proto.framecnt++;
  933.             }
  934.  
  935.             irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  936.             break;
  937.         }
  938.  
  939.         case sns_irTransceive_STATE_PAUSING:
  940.         {
  941.             if (irTxChannel[channel].sendingPronto)
  942.             {
  943.                 if (Timer_Expired(irTxChannel[channel].timerNum))
  944.                 {
  945.                     irTxChannel[channel].state = sns_irTransceive_STATE_PRONTO_REPEAT;
  946.                 }
  947.                 break;
  948.             }
  949.  
  950.             if (Timer_Expired(irTxChannel[channel].timerNum))
  951.             {
  952.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  953.             }
  954.  
  955.             /* Transmission is stopped when such command is recevied on can. */
  956.             if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
  957.             {
  958.                 /* Non-pronto transmission was stopped/completed. */
  959.                 // TODO: use another non-pronto code here? /jm
  960.                 pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_STOPPED);
  961.  
  962.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  963.             }
  964.             break;
  965.         }
  966.         default:
  967.             break;
  968.         }
  969. #endif
  970.  
  971.     }
  972. }
  973.  
  974.  
  975. /* Handle incoming CAN data. */
  976. void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  977. {
  978.     /* Sanity check. */
  979.     if (StdCan_Ret_class(rxMsg->Header) != CAN_MODULE_CLASS_SNS ||
  980.         StdCan_Ret_direction(rxMsg->Header) != DIRECTIONFLAG_TO_OWNER ||
  981.         rxMsg->Header.ModuleType != CAN_MODULE_TYPE_SNS_IRTRANSCEIVE ||
  982.         rxMsg->Header.ModuleId != sns_irTransceive_ID) return;
  983.  
  984.     switch (rxMsg->Header.Command)
  985.     {
  986. #if IR_TX_ENABLE==1
  987.     case CAN_MODULE_CMD_PHYSICAL_IR:
  988.     {
  989.         /* Get IR channel. */
  990.         uint8_t channel = rxMsg->Data[0] >> 4;
  991.  
  992.         /* Get button status. */
  993.         uint8_t buttonStatus = rxMsg->Data[0] & 0x0F;
  994.  
  995.         /* Sanity check. */
  996.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  997.             /* Invalid channel. */
  998.             break;
  999.         }
  1000.  
  1001.         /* Check if channel is busy sending Pronto. */
  1002.         if (irTxChannel[channel].sendingPronto) {
  1003.             /* Channel busy. */
  1004.             break;
  1005.         }
  1006.  
  1007.         if (buttonStatus == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED)
  1008.         {
  1009.             if (irTxChannel[channel].state == sns_irTransceive_STATE_IDLE)
  1010.             {
  1011.                 irTxChannel[channel].proto.protocol = rxMsg->Data[1];
  1012.                 irTxChannel[channel].proto.data = rxMsg->Data[2];
  1013.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1014.                 irTxChannel[channel].proto.data |= rxMsg->Data[3];
  1015.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1016.                 irTxChannel[channel].proto.data |= rxMsg->Data[4];
  1017.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1018.                 irTxChannel[channel].proto.data |= rxMsg->Data[5];
  1019.  
  1020.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  1021.             }
  1022.         }
  1023.         else if (buttonStatus == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED)
  1024.         {
  1025.             if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE)
  1026.             {
  1027.                 irTxChannel[channel].stopSend = TRUE;
  1028.             }
  1029.         }
  1030.         break;
  1031.     }
  1032.  
  1033. #if sns_irTransceive_PRONTO_SUPPORT==1
  1034.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTART:
  1035.     {
  1036.         /* Get IR channel. */
  1037.         uint8_t channel = rxMsg->Data[0] >> 4;
  1038.  
  1039.         /* Sanity check. */
  1040.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1041.             /* Invalid channel. Generic error code. */
  1042.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1043.             break;
  1044.         }
  1045.  
  1046.         /* Check channel configuration. */
  1047.         if (irTxChannel[channel].state == sns_irTransceive_STATE_DISABLED) {
  1048.             /* Not a TX channel. */
  1049.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1050.             break;
  1051.         }
  1052.  
  1053.         /* Check if channel is busy. */
  1054.         if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE) {
  1055.             /* We're already transmitting on this channel. */
  1056.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_CHANNELBUSY);
  1057.             break;
  1058.         }
  1059.  
  1060.         /* Check if format is supported. */
  1061.         if ((((uint16_t)(rxMsg->Data[0] & 0x0F) << 8) | (uint16_t)(rxMsg->Data[1])) != 0) {
  1062.             /* Invalid pronto format. Generic error code. */
  1063.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1064.             break;
  1065.         }
  1066.  
  1067.         /* Check if any other pronto transmission is ongoing (i.e. if the pronto activeChannel is busy). */
  1068.         if (prontoChannelIsAllocated() && irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_IDLE) {
  1069.             /* Pronto data is already being transmitted (or prepared) on another channel. */
  1070.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_ALREADYSENDINGPRONTO);
  1071.             break;
  1072.         }
  1073.  
  1074.         /* Extract received data. */
  1075.  
  1076.         uint32_t divider = (rxMsg->Data[2] << 8) | (rxMsg->Data[3] << 0);
  1077.         if (divider == 0) {
  1078.             /* Invalid wFrqDiv value. Generic error code. */
  1079.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1080.             break;
  1081.         }
  1082.         uint32_t freqkHz = (sns_irTransceive_BaseFrq / (divider*1000));
  1083.         if (freqkHz == 0) {
  1084.             /* Invalid wFrqDiv value. Generic error code. */
  1085.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1086.             break;
  1087.         }
  1088.         irTxChannel[channel].proto.modfreq = freqkHz;
  1089.  
  1090.         /* In case other channels are busy sending with other modfreqs, this will be reported by IrTransceiver_Transmit. */
  1091.  
  1092.         /* Convert lenghts to bytes. */
  1093.         irTxChannel[channel].onceSeqLen = 2*((((uint16_t)rxMsg->Data[4]) << 8) | (((uint16_t)rxMsg->Data[5]) << 0));
  1094.         irTxChannel[channel].repSeqLen = 2*((((uint16_t)rxMsg->Data[6]) << 8) | (((uint16_t)rxMsg->Data[7]) << 0));
  1095.  
  1096.         /* Enter prepering pronto state and clear transmit buffer. */
  1097.         irTxChannel[channel].state = sns_irTransceive_STATE_PREPARING_PRONTO;
  1098.         irTxChannel[channel].txlen = 0;
  1099.         irTxChannel[channel].expectedSeqNr = 0;
  1100.         prontoAllocatedTxChannel = channel;
  1101.  
  1102.         /* We're now waiting for pronto data. */
  1103.         pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_WAITING);
  1104.         break;
  1105.     }
  1106.  
  1107.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTOP:
  1108.     {
  1109.         /* Get IR channel. */
  1110.         uint8_t channel = rxMsg->Data[0] >> 4;
  1111.  
  1112.         /* Sanity check. */
  1113.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1114.             /* Invalid channel. */
  1115.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1116.             break;
  1117.         }
  1118.  
  1119.         /* Check channel configuration. */
  1120.         if (irTxChannel[channel].state == sns_irTransceive_STATE_DISABLED) {
  1121.             /* Not a TX channel. */
  1122.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1123.             break;
  1124.         }
  1125.  
  1126.         irTxChannel[channel].stopSend = TRUE;
  1127.  
  1128.         if (!prontoChannelIsAllocated()) {
  1129.             /* Nothing to stop. No pronto TX channel allocated right now. */
  1130.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1131.             break;
  1132.         }
  1133.  
  1134.         /* State check. If we're not currently transmitting anything, the STOPPED reponse will never occur. */
  1135.         if (prontoChannelIsAllocated() &&
  1136.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_TRANSMIT_PRONTO &&
  1137.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_TRANSMITTING &&
  1138.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PRONTO_REPEAT &&
  1139.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PAUSING &&
  1140.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_PAUSE)
  1141.         {
  1142.             /* Nothing to stop. Not in TX state. Respond that we're already stopped. */
  1143.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1144.             break;
  1145.         }
  1146.  
  1147.         /* Is pronto activeChannel active, but wrong channel specified? */
  1148.         if (prontoChannelIsAllocated() && prontoAllocatedTxChannel != channel) {
  1149.             /* Wrong pronto channel specified. */
  1150.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR_WRONGPRONTOCHANNEL);
  1151.             break;
  1152.         }
  1153.  
  1154.         /* Response will be sent when transmission has been completed (i.e. stopped). */
  1155.         break;
  1156.     }
  1157.  
  1158.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOCONTINUE:
  1159.     {
  1160.         /* Get IR channel. */
  1161.         uint8_t channel = rxMsg->Data[0] >> 4;
  1162.  
  1163.         /* Sanity check. */
  1164.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1165.             /* Invalid channel. */
  1166.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1167.             break;
  1168.         }
  1169.  
  1170.         /* Check channel configuration. */
  1171.         if (irTxChannel[channel].state == sns_irTransceive_STATE_DISABLED) {
  1172.             /* Not a TX channel. */
  1173.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1174.             break;
  1175.         }
  1176.  
  1177.         /* Check if channel is busy. */
  1178.         if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE) {
  1179.             /* We're already transmitting on this channel. */
  1180.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_CHANNELBUSY);
  1181.             break;
  1182.         }
  1183.  
  1184.         if (!prontoChannelIsAllocated()) {
  1185.             /* Nothing to continue. No pronto TX channel allocated right now. */
  1186.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1187.             break;
  1188.         }
  1189.  
  1190.         /* State check. We must already be in some transmit state. */
  1191.         if (prontoChannelIsAllocated() &&
  1192.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_TRANSMIT_PRONTO &&
  1193.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_TRANSMITTING &&
  1194.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PRONTO_REPEAT &&
  1195.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PAUSING &&
  1196.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_PAUSE)
  1197.         {
  1198.             /* Too late to continue! Not in TX state anymore. */
  1199.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1200.             break;
  1201.         }
  1202.  
  1203.         /* Is pronto activeChannel active, but wrong channel specified? */
  1204.         if (prontoChannelIsAllocated() && prontoAllocatedTxChannel != channel) {
  1205.             /* Wrong pronto channel specified. */
  1206.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR_WRONGPRONTOCHANNEL);
  1207.             break;
  1208.         }
  1209.  
  1210.         /* Reset repeat counter, i.e. request additional repeats. */
  1211.         irTxChannel[channel].repeatCount = 0;
  1212.  
  1213.         /* The repeat period was extended. Still transmitting. */
  1214.         pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_TRANSMITTING);
  1215.         break;
  1216.     }
  1217.  
  1218.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1:  /* Fall through. */
  1219.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA2:  /* Fall through. */
  1220.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA3:  /* Fall through. */
  1221.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA4:  /* Fall through. */
  1222.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA5:  /* Fall through. */
  1223.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA6:  /* Fall through. */
  1224.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA7:  /* Fall through. */
  1225.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA8:  /* Fall through. */
  1226.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA9:  /* Fall through. */
  1227.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA10: /* Fall through. */
  1228.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA11: /* Fall through. */
  1229.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA12: /* Fall through. */
  1230.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA13: /* Fall through. */
  1231.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA14: /* Fall through. */
  1232.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA15: /* Fall through. */
  1233.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA16:
  1234.     {
  1235.         if (!prontoChannelIsAllocated()) {
  1236.             /* Invalid state. */
  1237.             pronto_sendResponse(0, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1238.             break;
  1239.         }
  1240.  
  1241.         if (irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) {
  1242.             /* Invalid state. CAN msg probably out of order. */
  1243.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1244.             break;
  1245.         }
  1246.  
  1247.         if ((rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1) != (irTxChannel[prontoAllocatedTxChannel].expectedSeqNr % 16)) {
  1248.             /* Unexpected CAN msg, out of order. */
  1249.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1250.             irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1251.             break;
  1252.         }
  1253.  
  1254.         /* Decode pronto data. */
  1255.         for (uint8_t i = 0; i < rxMsg->Length; i++) {
  1256.             if (decodeProntoData(prontoAllocatedTxChannel, rxMsg->Data[i]) != 1) {
  1257.                 /* Decode error. */
  1258.                 pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1259.                 irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1260.                 return;
  1261.             }
  1262.         }
  1263.  
  1264.         irTxChannel[prontoAllocatedTxChannel].expectedSeqNr++;
  1265.  
  1266.         /* We're still waiting for more pronto data, or for pronto end. */
  1267.         pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_WAITING);
  1268.         break;
  1269.     }
  1270.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1:   /* Fall through. */
  1271.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND2:   /* Fall through. */
  1272.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND3:   /* Fall through. */
  1273.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND4:   /* Fall through. */
  1274.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND5:   /* Fall through. */
  1275.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND6:   /* Fall through. */
  1276.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND7:   /* Fall through. */
  1277.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND8:   /* Fall through. */
  1278.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND9:   /* Fall through. */
  1279.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND10:  /* Fall through. */
  1280.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND11:  /* Fall through. */
  1281.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND12:  /* Fall through. */
  1282.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND13:  /* Fall through. */
  1283.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND14:  /* Fall through. */
  1284.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND15:  /* Fall through. */
  1285.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND16:  /* Fall through. */
  1286.     {
  1287.         if (!prontoChannelIsAllocated()) {
  1288.             /* Invalid state. */
  1289.             pronto_sendResponse(0, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1290.             break;
  1291.         }
  1292.  
  1293.         if (irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) {
  1294.             /* Invalid state. CAN msg probably out of order. */
  1295.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1296.             break;
  1297.         }
  1298.  
  1299.         if (((rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1) % 16) != (irTxChannel[prontoAllocatedTxChannel].expectedSeqNr % 16)) {
  1300.             /* Unexpected CAN msg, out of order. */
  1301.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1302.             irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1303.             break;
  1304.         }
  1305.  
  1306.         /* Decode remaining pronto data (if any). -1 because last byte is repeat count. */
  1307.         for (uint16_t i = 0; i < rxMsg->Length - 1; i++) {
  1308.             if (decodeProntoData(prontoAllocatedTxChannel, rxMsg->Data[i]) < 0) {
  1309.                 /* Decode error. */
  1310.                 pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1311.                 irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1312.                 return;
  1313.             }
  1314.         }
  1315.  
  1316.         /* Sanity check. */
  1317.         if (irTxChannel[prontoAllocatedTxChannel].txlen != (((uint16_t)irTxChannel[prontoAllocatedTxChannel].onceSeqLen + (uint16_t)irTxChannel[prontoAllocatedTxChannel].repSeqLen))) {
  1318.             /* Length doesn't match the expected length. */
  1319.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1320.             irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1321.             break;
  1322.         }
  1323.  
  1324.         /* Last byte determines nr of repeats. 0xFF means INF, 0 means no repeats. */
  1325.         irTxChannel[prontoAllocatedTxChannel].proto.repeats = rxMsg->Data[rxMsg->Length - 1];
  1326.         irTxChannel[prontoAllocatedTxChannel].proto.timeout = 1; /* TODO: timer is buggy and doesn't support timeout 0, so we're forced to use 1ms extra between repeats. */
  1327.  
  1328.         /* Convert pronto data to µs. */
  1329.         uint32_t nsPerTick = 1000000UL / ((uint32_t)irTxChannel[prontoAllocatedTxChannel].proto.modfreq);
  1330.         for (uint16_t i = 0; i < irTxChannel[prontoAllocatedTxChannel].txlen; i++) {
  1331.             uint32_t us = (((uint32_t)irTxChannel[prontoAllocatedTxChannel].txbuf[i]) * nsPerTick) / 1000UL;
  1332.             if (us > 0xFFFFUL) {
  1333.                 us = 0xFFFFUL;
  1334.             }
  1335.             irTxChannel[prontoAllocatedTxChannel].txbuf[i] = (uint16_t)us;
  1336.         }
  1337.  
  1338.         /* Send IR data. */
  1339.         irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_START_TRANSMIT_PRONTO;
  1340.  
  1341.         /* We're now transmitting the pronto data. */
  1342.         pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_TRANSMITTING);
  1343.         break;
  1344.     }
  1345.  
  1346. /* TODO: In IR state machine add sending a response frame when ir stops sending, also implement pronto repeat. */
  1347. #endif
  1348. #endif
  1349.  
  1350.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRCONFIG:
  1351.     {
  1352.         /* Get IR channel. */
  1353.         uint8_t channel = rxMsg->Data[0] >> 4;
  1354.  
  1355.         /* Sanity check. */
  1356.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1357.             /* Invalid channel. */
  1358.             break;
  1359.         }
  1360.  
  1361.         uint8_t config = rxMsg->Data[0] & 0x0f;
  1362.         uint8_t power = rxMsg->Data[1]>>6;
  1363.         uint32_t divider = ((uint16_t)rxMsg->Data[2] << 8) | ((uint16_t)rxMsg->Data[3] << 0);
  1364.         if (divider == 0) {
  1365.             /* Invalid modfreq. */
  1366.             break;
  1367.         }
  1368.         uint32_t modfreq = (sns_irTransceive_BaseFrq / (divider*1000));
  1369.         if (modfreq == 0) {
  1370.             /* Invalid modfreq. */
  1371.             break;
  1372.         }
  1373.         sns_irTransceive_setConfig(channel, config, power, modfreq);
  1374.  
  1375. #if sns_irTransceive_USEEEPROM==1
  1376.         if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  1377.         {
  1378.             switch (channel)
  1379.             {
  1380.                 case 0:
  1381.                     eeprom_write_byte_crc(EEDATA.ch0_config, config, WITHOUT_CRC);
  1382.                     eeprom_write_byte_crc(EEDATA.ch0_txpower, power, WITHOUT_CRC);
  1383.                     eeprom_write_word_crc(EEDATA16.ch0_modfreq, modfreq, WITHOUT_CRC);
  1384.                     break;
  1385.                 case 1:
  1386.                     eeprom_write_byte_crc(EEDATA.ch1_config, config, WITHOUT_CRC);
  1387.                     eeprom_write_byte_crc(EEDATA.ch1_txpower, power, WITHOUT_CRC);
  1388.                     eeprom_write_word_crc(EEDATA16.ch1_modfreq, modfreq, WITHOUT_CRC);
  1389.                     break;
  1390.                 case 2:
  1391.                     eeprom_write_byte_crc(EEDATA.ch2_config, config, WITHOUT_CRC);
  1392.                     eeprom_write_byte_crc(EEDATA.ch2_txpower, power, WITHOUT_CRC);
  1393.                     eeprom_write_word_crc(EEDATA16.ch2_modfreq, modfreq, WITHOUT_CRC);
  1394.                     break;
  1395.                 default:
  1396.                     break;
  1397.             }
  1398.             EEDATA_UPDATE_CRC;
  1399.         }
  1400. #endif
  1401.         break;
  1402.     }
  1403.  
  1404.     default:
  1405.         break;
  1406.  
  1407.     }
  1408.  
  1409. }
  1410.  
  1411. void sns_irTransceive_List(uint8_t ModuleSequenceNumber)
  1412. {
  1413.     StdCan_Msg_t txMsg;
  1414.  
  1415.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  1416.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  1417.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  1418.  
  1419.     txMsg.Header.ModuleId = sns_irTransceive_ID;
  1420.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  1421.     txMsg.Length = 6;
  1422.  
  1423.     uint32_t HwId=BIOS_GetHwId();
  1424.     txMsg.Data[0] = HwId&0xff;
  1425.     txMsg.Data[1] = (HwId>>8)&0xff;
  1426.     txMsg.Data[2] = (HwId>>16)&0xff;
  1427.     txMsg.Data[3] = (HwId>>24)&0xff;
  1428.  
  1429.     txMsg.Data[4] = NUMBER_OF_MODULES;
  1430.     txMsg.Data[5] = ModuleSequenceNumber;
  1431.  
  1432.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  1433. }
  1434.