Subversion Repositories HomeAutomation

Rev

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