Subversion Repositories HomeAutomation

Rev

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

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