Subversion Repositories HomeAutomation

Rev

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

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