Subversion Repositories HomeAutomation

Rev

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