Subversion Repositories HomeAutomation

Rev

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

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