Subversion Repositories HomeAutomation

Rev

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