Subversion Repositories HomeAutomation

Rev

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

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