Subversion Repositories HomeAutomation

Rev

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