Subversion Repositories HomeAutomation

Rev

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