Subversion Repositories HomeAutomation

Rev

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