Subversion Repositories HomeAutomation

Rev

Rev 2198 | 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, uint8_t index)
  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 = 8;
  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>>40)&0xff;
  663.                 irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>32)&0xff;
  664.                 irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>24)&0xff;
  665.                 irTxMsg.Data[5] = (irRxChannel[channel].proto.data>>16)&0xff;
  666.                 irTxMsg.Data[6] = (irRxChannel[channel].proto.data>>8)&0xff;
  667.                 irTxMsg.Data[7] = irRxChannel[channel].proto.data&0xff;
  668.  
  669.                 StdCan_Put(&irTxMsg);
  670.             }
  671.             irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
  672.             break;
  673.         }
  674.  
  675.         case sns_irTransceive_STATE_START_RECEIVE:
  676.             IrTransceiver_ResetRx(channel);
  677.             cli();
  678.             irRxChannel[channel].newData = FALSE;
  679.             sei();
  680.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  681. //gpio_set_pin(EXP_J);
  682.             break;
  683.  
  684.         case sns_irTransceive_STATE_RECEIVING:
  685.             if (irRxChannel[channel].newData == TRUE) {
  686. //printf("sRx\n");
  687.                 /* TODO: move this line to the RX callback. */
  688.                 IrTransceiver_DisableRx(channel);
  689.                 cli();
  690.                 irRxChannel[channel].newData = FALSE;
  691.                 sei();
  692.  
  693.                 /* Let protocol driver parse and then send on CAN. */
  694.                 uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, 0, &irRxChannel[channel].proto);
  695.                 if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  696.                     irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  697.                     irTxMsg.Data[0] |= channel<<4;
  698.                     irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  699.                     irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>40)&0xff;
  700.                     irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>32)&0xff;
  701.                     irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>24)&0xff;
  702.                     irTxMsg.Data[5] = (irRxChannel[channel].proto.data>>16)&0xff;
  703.                     irTxMsg.Data[6] = (irRxChannel[channel].proto.data>>8)&0xff;
  704.                     irTxMsg.Data[7] = irRxChannel[channel].proto.data&0xff;
  705.  
  706.                     StdCan_Put(&irTxMsg);
  707.                 }
  708.                 else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
  709.                 {
  710. #if (sns_irTransceive_SEND_DEBUG==1)
  711.                     send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
  712.                     irRxChannel[channel].proto.timeout=300;
  713. #elif sns_irTransceive_PRONTO_SUPPORT==1
  714.                     pronto_sendData(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, channel, irRxChannel[channel].proto.modfreq);
  715.                     irRxChannel[channel].proto.timeout=1;
  716. #endif
  717.                 }
  718.  
  719.                 /* Enable the receiver again. */
  720.                 IrTransceiver_EnableRx(channel);
  721.  
  722.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  723. //gpio_clr_pin(EXP_H);
  724. //gpio_clr_pin(EXP_J);
  725.             }
  726.             break;
  727.  
  728.         case sns_irTransceive_STATE_START_PAUSE:
  729. //printf("sP\n");
  730.             /* Set a timer so we can send release button event when no new IR is arriving. */
  731.             Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  732.             irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  733.             break;
  734.  
  735.         case sns_irTransceive_STATE_PAUSING:
  736.             /* Reset timer if new IR arrived. */
  737.             if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
  738. //printf("sT\n");
  739.                 cli();
  740.                 irRxChannel[channel].newData = FALSE;
  741.                 sei();
  742.                 Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  743.             }
  744.  
  745.             if (Timer_Expired(irRxChannel[channel].timerNum)) {
  746. //printf("sD\n");
  747.                 irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  748.             }
  749.             break;
  750.  
  751.         default:
  752.             break;
  753.         }
  754. #endif
  755.  
  756. #if IR_TX_ENABLE==1
  757.         switch (irTxChannel[channel].state)
  758.         {
  759.         case sns_irTransceive_STATE_IDLE:
  760.         {
  761.             /* Transmission is started when a command is received on can. */
  762.             irTxChannel[channel].stopSend = FALSE;
  763.             irTxChannel[channel].repeatCount = 0;
  764.             irTxChannel[channel].proto.framecnt = 0;
  765.             irTxChannel[channel].sendingPronto = FALSE;
  766.             break;
  767.         }
  768.  
  769.         case sns_irTransceive_STATE_START_TRANSMIT:
  770.         {
  771.             /* Expand protocol. */
  772.             if (expandProtocol(irTxChannel[channel].txbuf, &irTxChannel[channel].txlen, &irTxChannel[channel].proto) != IR_OK) {
  773.                 /* Failed to expand protocol -> enter idle state. */
  774.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  775.                 break;
  776.             }
  777.  
  778.             /* Start IR transmission. */
  779.             /* TODO Send respone. Function call may fail when different modulation frequencies are used simultaneously. */
  780.             IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].txlen, irTxChannel[channel].proto.modfreq);
  781.  
  782.             /* Enter transmitting state. */
  783.             irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  784.             break;
  785.         }
  786.  
  787.         case sns_irTransceive_STATE_START_TRANSMIT_PRONTO:
  788.         {
  789.             /* Once sequence exists? */
  790.             if (irTxChannel[channel].onceSeqLen>0) {
  791.                 /* Start IR transmission of the once sequence. */
  792.                 int res;
  793.                 if((res=IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].onceSeqLen - 1, irTxChannel[channel].proto.modfreq)) < 0) {
  794.                     /* Send response code. */
  795.                     switch (res) {
  796.                         case -1: /* Invalid freq. */
  797.                         case -2: /* No data. */
  798.                             /* Generic error code, probably invalid packet. */
  799.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  800.                             break;
  801.                         case -3: /* Channel busy. */
  802.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_CHANNELBUSY);
  803.                             break;
  804.                         case -4: /* Frequency cannot be changed due to transmission on other channels. */
  805.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_OTHERFREQUENCYLOCKED);
  806.                             break;
  807.                     }
  808.                     /* Enter idle state. */
  809.                     irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  810.                     prontoAllocatedTxChannel = -1;
  811.                     break;
  812.                 }
  813.                 else {
  814.                     /* Enter transmitting state. */
  815.                     irTxChannel[channel].sendingPronto = TRUE;
  816.                     irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  817.                     break;
  818.                 }
  819.             }
  820.             /* Repeat sequence exists? */
  821.             else if (irTxChannel[channel].repSeqLen>0) {
  822.                 /* Start IR transmission of the repeat sequence. */
  823.                 int res;
  824.                 if((res=IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].repSeqLen - 1, irTxChannel[channel].proto.modfreq)) < 0) {
  825.                     /* Send response code. */
  826.                     switch (res) {
  827.                         case -1: /* Invalid freq. */
  828.                         case -2: /* No data. */
  829.                             /* Generic error code, probably invalid packet. */
  830.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  831.                             break;
  832.                         case -3: /* Channel busy. */
  833.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_CHANNELBUSY);
  834.                             break;
  835.                         case -4: /* Frequency cannot be changed due to transmission on other channels. */
  836.                             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_OTHERFREQUENCYLOCKED);
  837.                             break;
  838.                     }
  839.                     /* Enter idle state. */
  840.                     irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  841.                     prontoAllocatedTxChannel = -1;
  842.                     break;
  843.                 }
  844.                 else {
  845.                     /* Enter transmitting state. */
  846.                     irTxChannel[channel].sendingPronto = TRUE;
  847.                     irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  848.                     break;
  849.                 }
  850.             }
  851.             /* Neither once sequence nor repeat sequence exists! */
  852.             /* Generic error code, probably invalid packet. */
  853.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  854.             /* Enter idle state. */
  855.             irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  856.             prontoAllocatedTxChannel = -1;
  857.             break;
  858.         }
  859.  
  860.         case sns_irTransceive_STATE_PRONTO_REPEAT:
  861.         {
  862.             /* Check if done. */
  863.             if ((irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats && irTxChannel[channel].proto.repeats != 0xFF) || irTxChannel[channel].stopSend) {
  864.                 /* Pronto transmission was stopped/completed. */
  865.                 pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_STOPPED);
  866.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  867.                 prontoAllocatedTxChannel = -1;
  868.                 break;
  869.             }
  870.  
  871.             if (irTxChannel[channel].repeatCount < 255) {
  872.                 irTxChannel[channel].repeatCount++;
  873.             }
  874.  
  875.             /* Repeat sequence exists? */
  876.             if (irTxChannel[channel].repSeqLen > 0) {
  877.                 /* Use repeat seq. */
  878.                 uint16_t offset = ((uint16_t)irTxChannel[channel].onceSeqLen);
  879.                 /* Don't transmit last passive. last passive handled by timer. */
  880.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, offset, ((uint16_t)irTxChannel[channel].repSeqLen) - 1, irTxChannel[channel].proto.modfreq);
  881.             }
  882.             /* Once sequence exists? */
  883.             else if (irTxChannel[channel].onceSeqLen > 0) {
  884.                 /* Use once seq. */
  885.                 /* Don't transmit last passive. last passive handled by timer. */
  886.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].txlen - 1, irTxChannel[channel].proto.modfreq);
  887.             }
  888.             irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  889.             break;
  890.         }
  891.  
  892.         case sns_irTransceive_STATE_TRANSMITTING:
  893.         {
  894.             if (irTxChannel[channel].sendComplete == TRUE)
  895.             {
  896.                 cli();
  897.                 irTxChannel[channel].sendComplete = FALSE;
  898.                 sei();
  899.  
  900.                 if (irTxChannel[channel].sendingPronto)
  901.                 {
  902.                     /* First repeat, or no repeat sequence defined => use last passive time in once seq. */
  903.                     if(irTxChannel[channel].repeatCount == 0 || irTxChannel[channel].repSeqLen == 0)
  904.                     {
  905.                         /* Use last passive time as timeout. */
  906.                         uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].onceSeqLen - 1] / 1000;
  907.                         Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
  908.                     }
  909.                     /* Second, or later repeat => use last passive time in repeat seq. */
  910.                     else
  911.                     {
  912.                         /* Use last passive time as timeout. */
  913.                         uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].txlen - 1]  / 1000;
  914.                         Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
  915.                     }
  916.                     irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  917.                 }
  918.                 else
  919.                 {
  920.                     irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  921.                 }
  922.             }
  923.             break;
  924.         }
  925.  
  926.         case sns_irTransceive_STATE_START_PAUSE:
  927.         {
  928.             if (irTxChannel[channel].repeatCount < irTxChannel[channel].proto.repeats)
  929.             {
  930.                 irTxChannel[channel].repeatCount++;
  931.             }
  932.  
  933.             Timer_SetTimeout(irTxChannel[channel].timerNum, irTxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  934.  
  935.             if (irTxChannel[channel].proto.framecnt != 255)
  936.             {
  937.                 irTxChannel[channel].proto.framecnt++;
  938.             }
  939.  
  940.             irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  941.             break;
  942.         }
  943.  
  944.         case sns_irTransceive_STATE_PAUSING:
  945.         {
  946.             if (irTxChannel[channel].sendingPronto)
  947.             {
  948.                 if (Timer_Expired(irTxChannel[channel].timerNum))
  949.                 {
  950.                     irTxChannel[channel].state = sns_irTransceive_STATE_PRONTO_REPEAT;
  951.                 }
  952.                 break;
  953.             }
  954.  
  955.             if (Timer_Expired(irTxChannel[channel].timerNum))
  956.             {
  957.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  958.             }
  959.  
  960.             /* Transmission is stopped when such command is recevied on can. */
  961.             if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
  962.             {
  963.                 /* Non-pronto transmission was stopped/completed. */
  964.                 // TODO: use another non-pronto code here? /jm
  965.                 pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_STOPPED);
  966.  
  967.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  968.             }
  969.             break;
  970.         }
  971.  
  972.         default:
  973.         {
  974.             break;
  975.         }
  976.  
  977.         }
  978. #endif
  979.  
  980.     }
  981. }
  982.  
  983.  
  984. /* Handle incoming CAN data. */
  985. void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  986. {
  987.     /* Sanity check. */
  988.     if (StdCan_Ret_class(rxMsg->Header) != CAN_MODULE_CLASS_SNS ||
  989.         StdCan_Ret_direction(rxMsg->Header) != DIRECTIONFLAG_TO_OWNER ||
  990.         rxMsg->Header.ModuleType != CAN_MODULE_TYPE_SNS_IRTRANSCEIVE ||
  991.         rxMsg->Header.ModuleId != sns_irTransceive_ID) return;
  992.  
  993.     switch (rxMsg->Header.Command)
  994.     {
  995. #if IR_TX_ENABLE==1
  996.     case CAN_MODULE_CMD_PHYSICAL_IR:
  997.     {
  998.         /* Get IR channel. */
  999.         uint8_t channel = rxMsg->Data[0] >> 4;
  1000.  
  1001.         /* Get button status. */
  1002.         uint8_t buttonStatus = rxMsg->Data[0] & 0x0F;
  1003.  
  1004.         /* Sanity check. */
  1005.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1006.             /* Invalid channel. */
  1007.             break;
  1008.         }
  1009.  
  1010.         /* Check if channel is busy sending Pronto. */
  1011.         if (irTxChannel[channel].sendingPronto) {
  1012.             /* Channel busy. */
  1013.             break;
  1014.         }
  1015.  
  1016.         if (buttonStatus == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED)
  1017.         {
  1018.             if (irTxChannel[channel].state == sns_irTransceive_STATE_IDLE)
  1019.             {
  1020.                 irTxChannel[channel].proto.protocol = rxMsg->Data[1];
  1021.                 irTxChannel[channel].proto.data = rxMsg->Data[2];
  1022.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1023.                 irTxChannel[channel].proto.data |= rxMsg->Data[3];
  1024.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1025.                 irTxChannel[channel].proto.data |= rxMsg->Data[4];
  1026.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1027.                 irTxChannel[channel].proto.data |= rxMsg->Data[5];
  1028.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1029.                 irTxChannel[channel].proto.data |= rxMsg->Data[6];
  1030.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1031.                 irTxChannel[channel].proto.data |= rxMsg->Data[7];
  1032.  
  1033.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  1034.             }
  1035.         }
  1036.         else if (buttonStatus == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED)
  1037.         {
  1038.             if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE)
  1039.             {
  1040.                 irTxChannel[channel].stopSend = TRUE;
  1041.             }
  1042.         }
  1043.         break;
  1044.     }
  1045.  
  1046. #if sns_irTransceive_PRONTO_SUPPORT==1
  1047.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTART:
  1048.     {
  1049.         /* Get IR channel. */
  1050.         uint8_t channel = rxMsg->Data[0] >> 4;
  1051.  
  1052.         /* Sanity check. */
  1053.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1054.             /* Invalid channel. Generic error code. */
  1055.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1056.             break;
  1057.         }
  1058.  
  1059.         /* Check channel configuration. */
  1060.         if (irTxChannel[channel].state == sns_irTransceive_STATE_DISABLED) {
  1061.             /* Not a TX channel. */
  1062.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1063.             break;
  1064.         }
  1065.  
  1066.         /* Check if channel is busy. */
  1067.         if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE) {
  1068.             /* We're already transmitting on this channel. */
  1069.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_CHANNELBUSY);
  1070.             break;
  1071.         }
  1072.  
  1073.         /* Check if format is supported. */
  1074.         if ((((uint16_t)(rxMsg->Data[0] & 0x0F) << 8) | (uint16_t)(rxMsg->Data[1])) != 0) {
  1075.             /* Invalid pronto format. Generic error code. */
  1076.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1077.             break;
  1078.         }
  1079.  
  1080.         /* Check if any other pronto transmission is ongoing (i.e. if the pronto activeChannel is busy). */
  1081.         if (prontoChannelIsAllocated() && irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_IDLE) {
  1082.             /* Pronto data is already being transmitted (or prepared) on another channel. */
  1083.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_ALREADYSENDINGPRONTO);
  1084.             break;
  1085.         }
  1086.  
  1087.         /* Extract received data. */
  1088.  
  1089.         uint32_t divider = (rxMsg->Data[2] << 8) | (rxMsg->Data[3] << 0);
  1090.         if (divider == 0) {
  1091.             /* Invalid wFrqDiv value. Generic error code. */
  1092.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1093.             break;
  1094.         }
  1095.         uint32_t freqkHz = (sns_irTransceive_BaseFrq / (divider*1000));
  1096.         if (freqkHz == 0) {
  1097.             /* Invalid wFrqDiv value. Generic error code. */
  1098.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1099.             break;
  1100.         }
  1101.         irTxChannel[channel].proto.modfreq = freqkHz;
  1102.  
  1103.         /* In case other channels are busy sending with other modfreqs, this will be reported by IrTransceiver_Transmit. */
  1104.  
  1105.         /* Convert lenghts to bytes. */
  1106.         irTxChannel[channel].onceSeqLen = 2*((((uint16_t)rxMsg->Data[4]) << 8) | (((uint16_t)rxMsg->Data[5]) << 0));
  1107.         irTxChannel[channel].repSeqLen = 2*((((uint16_t)rxMsg->Data[6]) << 8) | (((uint16_t)rxMsg->Data[7]) << 0));
  1108.  
  1109.         /* Enter prepering pronto state and clear transmit buffer. */
  1110.         irTxChannel[channel].state = sns_irTransceive_STATE_PREPARING_PRONTO;
  1111.         irTxChannel[channel].txlen = 0;
  1112.         irTxChannel[channel].expectedSeqNr = 0;
  1113.         prontoAllocatedTxChannel = channel;
  1114.  
  1115.         /* We're now waiting for pronto data. */
  1116.         pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_WAITING);
  1117.         break;
  1118.     }
  1119.  
  1120.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTOP:
  1121.     {
  1122.         /* Get IR channel. */
  1123.         uint8_t channel = rxMsg->Data[0] >> 4;
  1124.  
  1125.         /* Sanity check. */
  1126.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1127.             /* Invalid channel. */
  1128.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1129.             break;
  1130.         }
  1131.  
  1132.         /* Check channel configuration. */
  1133.         if (irTxChannel[channel].state == sns_irTransceive_STATE_DISABLED) {
  1134.             /* Not a TX channel. */
  1135.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1136.             break;
  1137.         }
  1138.  
  1139.         irTxChannel[channel].stopSend = TRUE;
  1140.  
  1141.         if (!prontoChannelIsAllocated()) {
  1142.             /* Nothing to stop. No pronto TX channel allocated right now. */
  1143.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1144.             irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  1145.             break;
  1146.         }
  1147.  
  1148.         /* State check. If we're not currently transmitting anything, the STOPPED reponse will never occur. */
  1149.         if (prontoChannelIsAllocated() &&
  1150.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_TRANSMIT_PRONTO &&
  1151.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_TRANSMITTING &&
  1152.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PRONTO_REPEAT &&
  1153.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PAUSING &&
  1154.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_PAUSE)
  1155.         {
  1156.             /* Nothing to stop. Not in TX state. Respond that we're already stopped. */
  1157.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1158.             irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  1159.             break;
  1160.         }
  1161.  
  1162.         /* Is pronto activeChannel active, but wrong channel specified? */
  1163.         if (prontoChannelIsAllocated() && prontoAllocatedTxChannel != channel) {
  1164.             /* Wrong pronto channel specified. */
  1165.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR_WRONGPRONTOCHANNEL);
  1166.             break;
  1167.         }
  1168.  
  1169.         /* Response will be sent when transmission has been completed (i.e. stopped). */
  1170.         break;
  1171.     }
  1172.  
  1173.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOCONTINUE:
  1174.     {
  1175.         /* Get IR channel. */
  1176.         uint8_t channel = rxMsg->Data[0] >> 4;
  1177.  
  1178.         /* Sanity check. */
  1179.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1180.             /* Invalid channel. */
  1181.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1182.             break;
  1183.         }
  1184.  
  1185.         /* Check channel configuration. */
  1186.         if (irTxChannel[channel].state == sns_irTransceive_STATE_DISABLED) {
  1187.             /* Not a TX channel. */
  1188.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1189.             break;
  1190.         }
  1191.  
  1192.         /* Check if channel is busy. */
  1193.         if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE) {
  1194.             /* We're already transmitting on this channel. */
  1195.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_CHANNELBUSY);
  1196.             break;
  1197.         }
  1198.  
  1199.         if (!prontoChannelIsAllocated()) {
  1200.             /* Nothing to continue. No pronto TX channel allocated right now. */
  1201.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1202.             break;
  1203.         }
  1204.  
  1205.         /* State check. We must already be in some transmit state. */
  1206.         if (prontoChannelIsAllocated() &&
  1207.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_TRANSMIT_PRONTO &&
  1208.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_TRANSMITTING &&
  1209.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PRONTO_REPEAT &&
  1210.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PAUSING &&
  1211.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_PAUSE)
  1212.         {
  1213.             /* Too late to continue! Not in TX state anymore. */
  1214.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1215.             break;
  1216.         }
  1217.  
  1218.         /* Is pronto activeChannel active, but wrong channel specified? */
  1219.         if (prontoChannelIsAllocated() && prontoAllocatedTxChannel != channel) {
  1220.             /* Wrong pronto channel specified. */
  1221.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR_WRONGPRONTOCHANNEL);
  1222.             break;
  1223.         }
  1224.  
  1225.         /* Reset repeat counter, i.e. request additional repeats. */
  1226.         irTxChannel[channel].repeatCount = 0;
  1227.  
  1228.         /* The repeat period was extended. Still transmitting. */
  1229.         pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_TRANSMITTING);
  1230.         break;
  1231.     }
  1232.  
  1233.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1:  /* Fall through. */
  1234.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA2:  /* Fall through. */
  1235.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA3:  /* Fall through. */
  1236.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA4:  /* Fall through. */
  1237.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA5:  /* Fall through. */
  1238.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA6:  /* Fall through. */
  1239.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA7:  /* Fall through. */
  1240.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA8:  /* Fall through. */
  1241.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA9:  /* Fall through. */
  1242.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA10: /* Fall through. */
  1243.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA11: /* Fall through. */
  1244.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA12: /* Fall through. */
  1245.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA13: /* Fall through. */
  1246.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA14: /* Fall through. */
  1247.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA15: /* Fall through. */
  1248.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA16:
  1249.     {
  1250.         if (!prontoChannelIsAllocated()) {
  1251.             /* Invalid state. */
  1252.             pronto_sendResponse(0, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1253.             break;
  1254.         }
  1255.  
  1256.         if (irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) {
  1257.             /* Invalid state. CAN msg probably out of order. */
  1258.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1259.             break;
  1260.         }
  1261.  
  1262.         if ((rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1) != (irTxChannel[prontoAllocatedTxChannel].expectedSeqNr % 16)) {
  1263.             /* Unexpected CAN msg, out of order. */
  1264.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1265.             irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1266.             break;
  1267.         }
  1268.  
  1269.         /* Decode pronto data. */
  1270.         for (uint8_t i = 0; i < rxMsg->Length; i++) {
  1271.             if (decodeProntoData(prontoAllocatedTxChannel, rxMsg->Data[i]) != 1) {
  1272.                 /* Decode error. */
  1273.                 pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1274.                 irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1275.                 return;
  1276.             }
  1277.         }
  1278.  
  1279.         irTxChannel[prontoAllocatedTxChannel].expectedSeqNr++;
  1280.  
  1281.         /* We're still waiting for more pronto data, or for pronto end. */
  1282.         pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_WAITING);
  1283.         break;
  1284.     }
  1285.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1:   /* Fall through. */
  1286.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND2:   /* Fall through. */
  1287.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND3:   /* Fall through. */
  1288.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND4:   /* Fall through. */
  1289.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND5:   /* Fall through. */
  1290.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND6:   /* Fall through. */
  1291.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND7:   /* Fall through. */
  1292.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND8:   /* Fall through. */
  1293.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND9:   /* Fall through. */
  1294.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND10:  /* Fall through. */
  1295.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND11:  /* Fall through. */
  1296.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND12:  /* Fall through. */
  1297.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND13:  /* Fall through. */
  1298.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND14:  /* Fall through. */
  1299.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND15:  /* Fall through. */
  1300.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND16:  /* Fall through. */
  1301.     {
  1302.         if (!prontoChannelIsAllocated()) {
  1303.             /* Invalid state. */
  1304.             pronto_sendResponse(0, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1305.             break;
  1306.         }
  1307.  
  1308.         if (irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) {
  1309.             /* Invalid state. CAN msg probably out of order. */
  1310.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1311.             break;
  1312.         }
  1313.  
  1314.         if (((rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1) % 16) != (irTxChannel[prontoAllocatedTxChannel].expectedSeqNr % 16)) {
  1315.             /* Unexpected CAN msg, out of order. */
  1316.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1317.             irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1318.             break;
  1319.         }
  1320.  
  1321.         /* Decode remaining pronto data (if any). -1 because last byte is repeat count. */
  1322.         for (uint16_t i = 0; i < rxMsg->Length - 1; i++) {
  1323.             if (decodeProntoData(prontoAllocatedTxChannel, rxMsg->Data[i]) < 0) {
  1324.                 /* Decode error. */
  1325.                 pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1326.                 irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1327.                 return;
  1328.             }
  1329.         }
  1330.  
  1331.         /* Sanity check. */
  1332.         if (irTxChannel[prontoAllocatedTxChannel].txlen != (((uint16_t)irTxChannel[prontoAllocatedTxChannel].onceSeqLen + (uint16_t)irTxChannel[prontoAllocatedTxChannel].repSeqLen))) {
  1333.             /* Length doesn't match the expected length. */
  1334.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1335.             irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1336.             break;
  1337.         }
  1338.  
  1339.         /* Last byte determines nr of repeats. 0xFF means INF, 0 means no repeats. */
  1340.         irTxChannel[prontoAllocatedTxChannel].proto.repeats = rxMsg->Data[rxMsg->Length - 1];
  1341.         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. */
  1342.  
  1343.         /* Convert pronto data to µs. */
  1344.         uint32_t nsPerTick = 1000000UL / ((uint32_t)irTxChannel[prontoAllocatedTxChannel].proto.modfreq);
  1345.         for (uint16_t i = 0; i < irTxChannel[prontoAllocatedTxChannel].txlen; i++) {
  1346.             uint32_t us = (((uint32_t)irTxChannel[prontoAllocatedTxChannel].txbuf[i]) * nsPerTick) / 1000UL;
  1347.             if (us > 0xFFFFUL) {
  1348.                 us = 0xFFFFUL;
  1349.             }
  1350.             irTxChannel[prontoAllocatedTxChannel].txbuf[i] = (uint16_t)us;
  1351.         }
  1352.  
  1353.         /* Send IR data. */
  1354.         irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_START_TRANSMIT_PRONTO;
  1355.  
  1356.         /* We're now transmitting the pronto data. */
  1357.         pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_TRANSMITTING);
  1358.         break;
  1359.     }
  1360.  
  1361. /* TODO: In IR state machine add sending a response frame when ir stops sending, also implement pronto repeat. */
  1362. #endif
  1363. #endif
  1364.  
  1365.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRCONFIG:
  1366.     {
  1367.         /* Get IR channel. */
  1368.         uint8_t channel = rxMsg->Data[0] >> 4;
  1369.  
  1370.         /* Sanity check. */
  1371.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1372.             /* Invalid channel. */
  1373.             break;
  1374.         }
  1375.  
  1376.         uint8_t config = rxMsg->Data[0] & 0x0f;
  1377.         uint8_t power = rxMsg->Data[1]>>6;
  1378.         uint32_t divider = ((uint16_t)rxMsg->Data[2] << 8) | ((uint16_t)rxMsg->Data[3] << 0);
  1379.         if (divider == 0) {
  1380.             /* Invalid modfreq. */
  1381.             break;
  1382.         }
  1383.         uint32_t modfreq = (sns_irTransceive_BaseFrq / (divider*1000));
  1384.         if (modfreq == 0) {
  1385.             /* Invalid modfreq. */
  1386.             break;
  1387.         }
  1388.         sns_irTransceive_setConfig(channel, config, power, modfreq);
  1389.  
  1390. #if sns_irTransceive_USEEEPROM==1
  1391.         if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  1392.         {
  1393.             switch (channel)
  1394.             {
  1395.                 case 0:
  1396.                     eeprom_write_byte_crc(EEDATA.ch0_config, config, WITHOUT_CRC);
  1397.                     eeprom_write_byte_crc(EEDATA.ch0_txpower, power, WITHOUT_CRC);
  1398.                     eeprom_write_word_crc(EEDATA16.ch0_modfreq, modfreq, WITHOUT_CRC);
  1399.                     break;
  1400.                 case 1:
  1401.                     eeprom_write_byte_crc(EEDATA.ch1_config, config, WITHOUT_CRC);
  1402.                     eeprom_write_byte_crc(EEDATA.ch1_txpower, power, WITHOUT_CRC);
  1403.                     eeprom_write_word_crc(EEDATA16.ch1_modfreq, modfreq, WITHOUT_CRC);
  1404.                     break;
  1405.                 case 2:
  1406.                     eeprom_write_byte_crc(EEDATA.ch2_config, config, WITHOUT_CRC);
  1407.                     eeprom_write_byte_crc(EEDATA.ch2_txpower, power, WITHOUT_CRC);
  1408.                     eeprom_write_word_crc(EEDATA16.ch2_modfreq, modfreq, WITHOUT_CRC);
  1409.                     break;
  1410.                 default:
  1411.                     break;
  1412.             }
  1413.             EEDATA_UPDATE_CRC;
  1414.         }
  1415. #endif
  1416.         break;
  1417.     }
  1418.  
  1419.     default:
  1420.         break;
  1421.  
  1422.     }
  1423.  
  1424. }
  1425.  
  1426. void sns_irTransceive_List(uint8_t ModuleSequenceNumber)
  1427. {
  1428.     StdCan_Msg_t txMsg;
  1429.  
  1430.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  1431.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  1432.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  1433.  
  1434.     txMsg.Header.ModuleId = sns_irTransceive_ID;
  1435.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  1436.     txMsg.Length = 6;
  1437.  
  1438.     uint32_t HwId=BIOS_GetHwId();
  1439.     txMsg.Data[0] = HwId&0xff;
  1440.     txMsg.Data[1] = (HwId>>8)&0xff;
  1441.     txMsg.Data[2] = (HwId>>16)&0xff;
  1442.     txMsg.Data[3] = (HwId>>24)&0xff;
  1443.  
  1444.     txMsg.Data[4] = NUMBER_OF_MODULES;
  1445.     txMsg.Data[5] = ModuleSequenceNumber;
  1446.  
  1447.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  1448. }
  1449.