Subversion Repositories HomeAutomation

Rev

Rev 1978 | Rev 2045 | 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 = 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, &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.         }
  890.  
  891.         case sns_irTransceive_STATE_TRANSMITTING:
  892.         {
  893.             if (irTxChannel[channel].sendComplete == TRUE)
  894.             {
  895.                 cli();
  896.                 irTxChannel[channel].sendComplete = FALSE;
  897.                 sei();
  898.  
  899.                 if (irTxChannel[channel].sendingPronto)
  900.                 {
  901.                     /* First repeat, or no repeat sequence defined => use last passive time in once seq. */
  902.                     if(irTxChannel[channel].repeatCount == 0 || irTxChannel[channel].repSeqLen == 0)
  903.                     {
  904.                         /* Use last passive time as timeout. */
  905.                         uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].onceSeqLen - 1] / 1000;
  906.                         Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
  907.                     }
  908.                     /* Second, or later repeat => use last passive time in repeat seq. */
  909.                     else
  910.                     {
  911.                         /* Use last passive time as timeout. */
  912.                         uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].txlen - 1]  / 1000;
  913.                         Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
  914.                     }
  915.                     irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  916.                 }
  917.                 else
  918.                 {
  919.                     irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  920.                 }
  921.             }
  922.             break;
  923.         }
  924.  
  925.         case sns_irTransceive_STATE_START_PAUSE:
  926.         {
  927.             if (irTxChannel[channel].repeatCount < irTxChannel[channel].proto.repeats)
  928.             {
  929.                 irTxChannel[channel].repeatCount++;
  930.             }
  931.  
  932.             Timer_SetTimeout(irTxChannel[channel].timerNum, irTxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  933.  
  934.             if (irTxChannel[channel].proto.framecnt != 255)
  935.             {
  936.                 irTxChannel[channel].proto.framecnt++;
  937.             }
  938.  
  939.             irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  940.             break;
  941.         }
  942.  
  943.         case sns_irTransceive_STATE_PAUSING:
  944.         {
  945.             if (irTxChannel[channel].sendingPronto)
  946.             {
  947.                 if (Timer_Expired(irTxChannel[channel].timerNum))
  948.                 {
  949.                     irTxChannel[channel].state = sns_irTransceive_STATE_PRONTO_REPEAT;
  950.                 }
  951.                 break;
  952.             }
  953.  
  954.             if (Timer_Expired(irTxChannel[channel].timerNum))
  955.             {
  956.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  957.             }
  958.  
  959.             /* Transmission is stopped when such command is recevied on can. */
  960.             if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
  961.             {
  962.                 /* Non-pronto transmission was stopped/completed. */
  963.                 // TODO: use another non-pronto code here? /jm
  964.                 pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_STOPPED);
  965.  
  966.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  967.             }
  968.             break;
  969.         }
  970.         default:
  971.             break;
  972.         }
  973. #endif
  974.  
  975.     }
  976. }
  977.  
  978.  
  979. /* Handle incoming CAN data. */
  980. void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  981. {
  982.     /* Sanity check. */
  983.     if (StdCan_Ret_class(rxMsg->Header) != CAN_MODULE_CLASS_SNS ||
  984.         StdCan_Ret_direction(rxMsg->Header) != DIRECTIONFLAG_TO_OWNER ||
  985.         rxMsg->Header.ModuleType != CAN_MODULE_TYPE_SNS_IRTRANSCEIVE ||
  986.         rxMsg->Header.ModuleId != sns_irTransceive_ID) return;
  987.  
  988.     switch (rxMsg->Header.Command)
  989.     {
  990. #if IR_TX_ENABLE==1
  991.     case CAN_MODULE_CMD_PHYSICAL_IR:
  992.     {
  993.         /* Get IR channel. */
  994.         uint8_t channel = rxMsg->Data[0] >> 4;
  995.  
  996.         /* Get button status. */
  997.         uint8_t buttonStatus = rxMsg->Data[0] & 0x0F;
  998.  
  999.         /* Sanity check. */
  1000.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1001.             /* Invalid channel. */
  1002.             break;
  1003.         }
  1004.  
  1005.         /* Check if channel is busy sending Pronto. */
  1006.         if (irTxChannel[channel].sendingPronto) {
  1007.             /* Channel busy. */
  1008.             break;
  1009.         }
  1010.  
  1011.         if (buttonStatus == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED)
  1012.         {
  1013.             if (irTxChannel[channel].state == sns_irTransceive_STATE_IDLE)
  1014.             {
  1015.                 irTxChannel[channel].proto.protocol = rxMsg->Data[1];
  1016.                 irTxChannel[channel].proto.data = rxMsg->Data[2];
  1017.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1018.                 irTxChannel[channel].proto.data |= rxMsg->Data[3];
  1019.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1020.                 irTxChannel[channel].proto.data |= rxMsg->Data[4];
  1021.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1022.                 irTxChannel[channel].proto.data |= rxMsg->Data[5];
  1023.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1024.                 irTxChannel[channel].proto.data |= rxMsg->Data[6];
  1025.                 irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  1026.                 irTxChannel[channel].proto.data |= rxMsg->Data[7];
  1027.  
  1028.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  1029.             }
  1030.         }
  1031.         else if (buttonStatus == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED)
  1032.         {
  1033.             if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE)
  1034.             {
  1035.                 irTxChannel[channel].stopSend = TRUE;
  1036.             }
  1037.         }
  1038.         break;
  1039.     }
  1040.  
  1041. #if sns_irTransceive_PRONTO_SUPPORT==1
  1042.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTART:
  1043.     {
  1044.         /* Get IR channel. */
  1045.         uint8_t channel = rxMsg->Data[0] >> 4;
  1046.  
  1047.         /* Sanity check. */
  1048.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1049.             /* Invalid channel. Generic error code. */
  1050.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1051.             break;
  1052.         }
  1053.  
  1054.         /* Check channel configuration. */
  1055.         if (irTxChannel[channel].state == sns_irTransceive_STATE_DISABLED) {
  1056.             /* Not a TX channel. */
  1057.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1058.             break;
  1059.         }
  1060.  
  1061.         /* Check if channel is busy. */
  1062.         if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE) {
  1063.             /* We're already transmitting on this channel. */
  1064.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_CHANNELBUSY);
  1065.             break;
  1066.         }
  1067.  
  1068.         /* Check if format is supported. */
  1069.         if ((((uint16_t)(rxMsg->Data[0] & 0x0F) << 8) | (uint16_t)(rxMsg->Data[1])) != 0) {
  1070.             /* Invalid pronto format. Generic error code. */
  1071.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1072.             break;
  1073.         }
  1074.  
  1075.         /* Check if any other pronto transmission is ongoing (i.e. if the pronto activeChannel is busy). */
  1076.         if (prontoChannelIsAllocated() && irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_IDLE) {
  1077.             /* Pronto data is already being transmitted (or prepared) on another channel. */
  1078.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_ALREADYSENDINGPRONTO);
  1079.             break;
  1080.         }
  1081.  
  1082.         /* Extract received data. */
  1083.  
  1084.         uint32_t divider = (rxMsg->Data[2] << 8) | (rxMsg->Data[3] << 0);
  1085.         if (divider == 0) {
  1086.             /* Invalid wFrqDiv value. Generic error code. */
  1087.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1088.             break;
  1089.         }
  1090.         uint32_t freqkHz = (sns_irTransceive_BaseFrq / (divider*1000));
  1091.         if (freqkHz == 0) {
  1092.             /* Invalid wFrqDiv value. Generic error code. */
  1093.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1094.             break;
  1095.         }
  1096.         irTxChannel[channel].proto.modfreq = freqkHz;
  1097.  
  1098.         /* In case other channels are busy sending with other modfreqs, this will be reported by IrTransceiver_Transmit. */
  1099.  
  1100.         /* Convert lenghts to bytes. */
  1101.         irTxChannel[channel].onceSeqLen = 2*((((uint16_t)rxMsg->Data[4]) << 8) | (((uint16_t)rxMsg->Data[5]) << 0));
  1102.         irTxChannel[channel].repSeqLen = 2*((((uint16_t)rxMsg->Data[6]) << 8) | (((uint16_t)rxMsg->Data[7]) << 0));
  1103.  
  1104.         /* Enter prepering pronto state and clear transmit buffer. */
  1105.         irTxChannel[channel].state = sns_irTransceive_STATE_PREPARING_PRONTO;
  1106.         irTxChannel[channel].txlen = 0;
  1107.         irTxChannel[channel].expectedSeqNr = 0;
  1108.         prontoAllocatedTxChannel = channel;
  1109.  
  1110.         /* We're now waiting for pronto data. */
  1111.         pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_WAITING);
  1112.         break;
  1113.     }
  1114.  
  1115.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTOP:
  1116.     {
  1117.         /* Get IR channel. */
  1118.         uint8_t channel = rxMsg->Data[0] >> 4;
  1119.  
  1120.         /* Sanity check. */
  1121.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1122.             /* Invalid channel. */
  1123.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1124.             break;
  1125.         }
  1126.  
  1127.         /* Check channel configuration. */
  1128.         if (irTxChannel[channel].state == sns_irTransceive_STATE_DISABLED) {
  1129.             /* Not a TX channel. */
  1130.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1131.             break;
  1132.         }
  1133.  
  1134.         irTxChannel[channel].stopSend = TRUE;
  1135.  
  1136.         if (!prontoChannelIsAllocated()) {
  1137.             /* Nothing to stop. No pronto TX channel allocated right now. */
  1138.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1139.             break;
  1140.         }
  1141.  
  1142.         /* State check. If we're not currently transmitting anything, the STOPPED reponse will never occur. */
  1143.         if (prontoChannelIsAllocated() &&
  1144.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_TRANSMIT_PRONTO &&
  1145.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_TRANSMITTING &&
  1146.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PRONTO_REPEAT &&
  1147.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PAUSING &&
  1148.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_PAUSE)
  1149.         {
  1150.             /* Nothing to stop. Not in TX state. Respond that we're already stopped. */
  1151.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1152.             break;
  1153.         }
  1154.  
  1155.         /* Is pronto activeChannel active, but wrong channel specified? */
  1156.         if (prontoChannelIsAllocated() && prontoAllocatedTxChannel != channel) {
  1157.             /* Wrong pronto channel specified. */
  1158.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR_WRONGPRONTOCHANNEL);
  1159.             break;
  1160.         }
  1161.  
  1162.         /* Response will be sent when transmission has been completed (i.e. stopped). */
  1163.         break;
  1164.     }
  1165.  
  1166.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOCONTINUE:
  1167.     {
  1168.         /* Get IR channel. */
  1169.         uint8_t channel = rxMsg->Data[0] >> 4;
  1170.  
  1171.         /* Sanity check. */
  1172.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1173.             /* Invalid channel. */
  1174.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1175.             break;
  1176.         }
  1177.  
  1178.         /* Check channel configuration. */
  1179.         if (irTxChannel[channel].state == sns_irTransceive_STATE_DISABLED) {
  1180.             /* Not a TX channel. */
  1181.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1182.             break;
  1183.         }
  1184.  
  1185.         /* Check if channel is busy. */
  1186.         if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE) {
  1187.             /* We're already transmitting on this channel. */
  1188.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_BUSY_CHANNELBUSY);
  1189.             break;
  1190.         }
  1191.  
  1192.         if (!prontoChannelIsAllocated()) {
  1193.             /* Nothing to continue. No pronto TX channel allocated right now. */
  1194.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1195.             break;
  1196.         }
  1197.  
  1198.         /* State check. We must already be in some transmit state. */
  1199.         if (prontoChannelIsAllocated() &&
  1200.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_TRANSMIT_PRONTO &&
  1201.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_TRANSMITTING &&
  1202.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PRONTO_REPEAT &&
  1203.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PAUSING &&
  1204.             irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_START_PAUSE)
  1205.         {
  1206.             /* Too late to continue! Not in TX state anymore. */
  1207.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
  1208.             break;
  1209.         }
  1210.  
  1211.         /* Is pronto activeChannel active, but wrong channel specified? */
  1212.         if (prontoChannelIsAllocated() && prontoAllocatedTxChannel != channel) {
  1213.             /* Wrong pronto channel specified. */
  1214.             pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR_WRONGPRONTOCHANNEL);
  1215.             break;
  1216.         }
  1217.  
  1218.         /* Reset repeat counter, i.e. request additional repeats. */
  1219.         irTxChannel[channel].repeatCount = 0;
  1220.  
  1221.         /* The repeat period was extended. Still transmitting. */
  1222.         pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_TRANSMITTING);
  1223.         break;
  1224.     }
  1225.  
  1226.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1:  /* Fall through. */
  1227.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA2:  /* Fall through. */
  1228.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA3:  /* Fall through. */
  1229.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA4:  /* Fall through. */
  1230.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA5:  /* Fall through. */
  1231.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA6:  /* Fall through. */
  1232.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA7:  /* Fall through. */
  1233.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA8:  /* Fall through. */
  1234.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA9:  /* Fall through. */
  1235.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA10: /* Fall through. */
  1236.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA11: /* Fall through. */
  1237.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA12: /* Fall through. */
  1238.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA13: /* Fall through. */
  1239.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA14: /* Fall through. */
  1240.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA15: /* Fall through. */
  1241.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA16:
  1242.     {
  1243.         if (!prontoChannelIsAllocated()) {
  1244.             /* Invalid state. */
  1245.             pronto_sendResponse(0, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1246.             break;
  1247.         }
  1248.  
  1249.         if (irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) {
  1250.             /* Invalid state. CAN msg probably out of order. */
  1251.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1252.             break;
  1253.         }
  1254.  
  1255.         if ((rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1) != (irTxChannel[prontoAllocatedTxChannel].expectedSeqNr % 16)) {
  1256.             /* Unexpected CAN msg, out of order. */
  1257.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1258.             irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1259.             break;
  1260.         }
  1261.  
  1262.         /* Decode pronto data. */
  1263.         for (uint8_t i = 0; i < rxMsg->Length; i++) {
  1264.             if (decodeProntoData(prontoAllocatedTxChannel, rxMsg->Data[i]) != 1) {
  1265.                 /* Decode error. */
  1266.                 pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1267.                 irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1268.                 return;
  1269.             }
  1270.         }
  1271.  
  1272.         irTxChannel[prontoAllocatedTxChannel].expectedSeqNr++;
  1273.  
  1274.         /* We're still waiting for more pronto data, or for pronto end. */
  1275.         pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_WAITING);
  1276.         break;
  1277.     }
  1278.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1:   /* Fall through. */
  1279.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND2:   /* Fall through. */
  1280.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND3:   /* Fall through. */
  1281.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND4:   /* Fall through. */
  1282.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND5:   /* Fall through. */
  1283.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND6:   /* Fall through. */
  1284.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND7:   /* Fall through. */
  1285.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND8:   /* Fall through. */
  1286.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND9:   /* Fall through. */
  1287.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND10:  /* Fall through. */
  1288.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND11:  /* Fall through. */
  1289.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND12:  /* Fall through. */
  1290.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND13:  /* Fall through. */
  1291.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND14:  /* Fall through. */
  1292.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND15:  /* Fall through. */
  1293.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND16:  /* Fall through. */
  1294.     {
  1295.         if (!prontoChannelIsAllocated()) {
  1296.             /* Invalid state. */
  1297.             pronto_sendResponse(0, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1298.             break;
  1299.         }
  1300.  
  1301.         if (irTxChannel[prontoAllocatedTxChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) {
  1302.             /* Invalid state. CAN msg probably out of order. */
  1303.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1304.             break;
  1305.         }
  1306.  
  1307.         if (((rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1) % 16) != (irTxChannel[prontoAllocatedTxChannel].expectedSeqNr % 16)) {
  1308.             /* Unexpected CAN msg, out of order. */
  1309.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1310.             irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1311.             break;
  1312.         }
  1313.  
  1314.         /* Decode remaining pronto data (if any). -1 because last byte is repeat count. */
  1315.         for (uint16_t i = 0; i < rxMsg->Length - 1; i++) {
  1316.             if (decodeProntoData(prontoAllocatedTxChannel, rxMsg->Data[i]) < 0) {
  1317.                 /* Decode error. */
  1318.                 pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1319.                 irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1320.                 return;
  1321.             }
  1322.         }
  1323.  
  1324.         /* Sanity check. */
  1325.         if (irTxChannel[prontoAllocatedTxChannel].txlen != (((uint16_t)irTxChannel[prontoAllocatedTxChannel].onceSeqLen + (uint16_t)irTxChannel[prontoAllocatedTxChannel].repSeqLen))) {
  1326.             /* Length doesn't match the expected length. */
  1327.             pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
  1328.             irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_IDLE;
  1329.             break;
  1330.         }
  1331.  
  1332.         /* Last byte determines nr of repeats. 0xFF means INF, 0 means no repeats. */
  1333.         irTxChannel[prontoAllocatedTxChannel].proto.repeats = rxMsg->Data[rxMsg->Length - 1];
  1334.         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. */
  1335.  
  1336.         /* Convert pronto data to µs. */
  1337.         uint32_t nsPerTick = 1000000UL / ((uint32_t)irTxChannel[prontoAllocatedTxChannel].proto.modfreq);
  1338.         for (uint16_t i = 0; i < irTxChannel[prontoAllocatedTxChannel].txlen; i++) {
  1339.             uint32_t us = (((uint32_t)irTxChannel[prontoAllocatedTxChannel].txbuf[i]) * nsPerTick) / 1000UL;
  1340.             if (us > 0xFFFFUL) {
  1341.                 us = 0xFFFFUL;
  1342.             }
  1343.             irTxChannel[prontoAllocatedTxChannel].txbuf[i] = (uint16_t)us;
  1344.         }
  1345.  
  1346.         /* Send IR data. */
  1347.         irTxChannel[prontoAllocatedTxChannel].state = sns_irTransceive_STATE_START_TRANSMIT_PRONTO;
  1348.  
  1349.         /* We're now transmitting the pronto data. */
  1350.         pronto_sendResponse(prontoAllocatedTxChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_TRANSMITTING);
  1351.         break;
  1352.     }
  1353.  
  1354. /* TODO: In IR state machine add sending a response frame when ir stops sending, also implement pronto repeat. */
  1355. #endif
  1356. #endif
  1357.  
  1358.     case CAN_MODULE_CMD_IRTRANSCEIVE_IRCONFIG:
  1359.     {
  1360.         /* Get IR channel. */
  1361.         uint8_t channel = rxMsg->Data[0] >> 4;
  1362.  
  1363.         /* Sanity check. */
  1364.         if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
  1365.             /* Invalid channel. */
  1366.             break;
  1367.         }
  1368.  
  1369.         uint8_t config = rxMsg->Data[0] & 0x0f;
  1370.         uint8_t power = rxMsg->Data[1]>>6;
  1371.         uint32_t divider = ((uint16_t)rxMsg->Data[2] << 8) | ((uint16_t)rxMsg->Data[3] << 0);
  1372.         if (divider == 0) {
  1373.             /* Invalid modfreq. */
  1374.             break;
  1375.         }
  1376.         uint32_t modfreq = (sns_irTransceive_BaseFrq / (divider*1000));
  1377.         if (modfreq == 0) {
  1378.             /* Invalid modfreq. */
  1379.             break;
  1380.         }
  1381.         sns_irTransceive_setConfig(channel, config, power, modfreq);
  1382.  
  1383. #if sns_irTransceive_USEEEPROM==1
  1384.         if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  1385.         {
  1386.             switch (channel)
  1387.             {
  1388.                 case 0:
  1389.                     eeprom_write_byte_crc(EEDATA.ch0_config, config, WITHOUT_CRC);
  1390.                     eeprom_write_byte_crc(EEDATA.ch0_txpower, power, WITHOUT_CRC);
  1391.                     eeprom_write_word_crc(EEDATA16.ch0_modfreq, modfreq, WITHOUT_CRC);
  1392.                     break;
  1393.                 case 1:
  1394.                     eeprom_write_byte_crc(EEDATA.ch1_config, config, WITHOUT_CRC);
  1395.                     eeprom_write_byte_crc(EEDATA.ch1_txpower, power, WITHOUT_CRC);
  1396.                     eeprom_write_word_crc(EEDATA16.ch1_modfreq, modfreq, WITHOUT_CRC);
  1397.                     break;
  1398.                 case 2:
  1399.                     eeprom_write_byte_crc(EEDATA.ch2_config, config, WITHOUT_CRC);
  1400.                     eeprom_write_byte_crc(EEDATA.ch2_txpower, power, WITHOUT_CRC);
  1401.                     eeprom_write_word_crc(EEDATA16.ch2_modfreq, modfreq, WITHOUT_CRC);
  1402.                     break;
  1403.                 default:
  1404.                     break;
  1405.             }
  1406.             EEDATA_UPDATE_CRC;
  1407.         }
  1408. #endif
  1409.         break;
  1410.     }
  1411.  
  1412.     default:
  1413.         break;
  1414.  
  1415.     }
  1416.  
  1417. }
  1418.  
  1419. void sns_irTransceive_List(uint8_t ModuleSequenceNumber)
  1420. {
  1421.     StdCan_Msg_t txMsg;
  1422.  
  1423.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  1424.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  1425.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  1426.  
  1427.     txMsg.Header.ModuleId = sns_irTransceive_ID;
  1428.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  1429.     txMsg.Length = 6;
  1430.  
  1431.     uint32_t HwId=BIOS_GetHwId();
  1432.     txMsg.Data[0] = HwId&0xff;
  1433.     txMsg.Data[1] = (HwId>>8)&0xff;
  1434.     txMsg.Data[2] = (HwId>>16)&0xff;
  1435.     txMsg.Data[3] = (HwId>>24)&0xff;
  1436.  
  1437.     txMsg.Data[4] = NUMBER_OF_MODULES;
  1438.     txMsg.Data[5] = ModuleSequenceNumber;
  1439.  
  1440.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  1441. }
  1442.