Subversion Repositories HomeAutomation

Rev

Rev 1828 | Rev 1830 | 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.     uint8_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.     uint8_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. } irTxChannel[IR_SUPPORTED_NUM_CHANNELS];
  47. #endif
  48.  
  49. uint16_t    buf[IR_SUPPORTED_NUM_CHANNELS][MAX_NR_TIMES];
  50.  
  51. StdCan_Msg_t        irTxMsg;
  52.  
  53. #if (sns_irTransceive_SEND_DEBUG==1)
  54. void send_debug(uint16_t *buffer, uint8_t len) {
  55.     StdCan_Msg_t dbgIrTxMsg;
  56.     /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */
  57.  
  58.     StdCan_Set_class(dbgIrTxMsg.Header, CAN_MODULE_CLASS_SNS);
  59.     StdCan_Set_direction(dbgIrTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  60.     dbgIrTxMsg.Length = 8;
  61.     dbgIrTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  62.     dbgIrTxMsg.Header.ModuleId = sns_irTransceive_ID;
  63.     dbgIrTxMsg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRRAW;
  64.     for (uint8_t i = 0; i < len>>2; i++) {
  65.         uint8_t index = i<<2;
  66.  
  67.         dbgIrTxMsg.Data[0] = (buffer[index]>>8)&0xff;
  68.         dbgIrTxMsg.Data[1] = (buffer[index]>>0)&0xff;
  69.         dbgIrTxMsg.Data[2] = (buffer[index+1]>>8)&0xff;
  70.         dbgIrTxMsg.Data[3] = (buffer[index+1]>>0)&0xff;
  71.         dbgIrTxMsg.Data[4] = (buffer[index+2]>>8)&0xff;
  72.         dbgIrTxMsg.Data[5] = (buffer[index+2]>>0)&0xff;
  73.         dbgIrTxMsg.Data[6] = (buffer[index+3]>>8)&0xff;
  74.         dbgIrTxMsg.Data[7] = (buffer[index+3]>>0)&0xff;
  75.                
  76.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  77.         while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
  78.         _delay_ms(1);
  79.     }
  80.    
  81.     uint8_t lastpacketcnt = len&0x03;
  82.     if (lastpacketcnt > 0) {
  83.         dbgIrTxMsg.Length = lastpacketcnt<<1;
  84.         for (uint8_t i = 0; i < lastpacketcnt; i++) {
  85.             dbgIrTxMsg.Data[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff;
  86.             dbgIrTxMsg.Data[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff;
  87.         }
  88.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  89.         while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
  90.         _delay_ms(1);
  91.     }
  92.  
  93. }
  94. #endif
  95.  
  96. #define sns_irTransceive_BaseFrq (4145146ULL)
  97.  
  98. #ifndef sns_irTransceive_PRONTO_SUPPORT
  99. #define sns_irTransceive_PRONTO_SUPPORT 0
  100. #endif
  101.  
  102. #if sns_irTransceive_PRONTO_SUPPORT==1
  103. volatile uint32_t sns_irTransceive_LastPronto=0;
  104. #define sns_irTransceive_MAXTIMING (16*1000)
  105.  
  106. void send_pronto(uint16_t *buffer, uint8_t len, uint8_t channel, uint8_t modfreq) {
  107.     StdCan_Msg_t msg;
  108.  
  109.     StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
  110.     StdCan_Set_direction(msg.Header, DIRECTIONFLAG_FROM_OWNER);
  111.     msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  112.     msg.Header.ModuleId = sns_irTransceive_ID;
  113.    
  114.     /* Send timing command */
  115.     msg.Length = 5;
  116.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOTIMING;
  117.     uint32_t currentTime=Timer_GetTicks();
  118.     if ((currentTime < sns_irTransceive_LastPronto+sns_irTransceive_MAXTIMING) && (sns_irTransceive_LastPronto != 0))
  119.     {
  120.         msg.Data[0] = 0xff;
  121.         msg.Data[1] = 0x10;
  122.         msg.Data[2] = (((currentTime-sns_irTransceive_LastPronto)*1000)>>16)&0xff;
  123.         msg.Data[3] = (((currentTime-sns_irTransceive_LastPronto)*1000)>>8)&0xff;
  124.         msg.Data[4] = ((currentTime-sns_irTransceive_LastPronto)*1000)&0xff;
  125.         while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  126.         _delay_ms(1);
  127.     }
  128.     sns_irTransceive_LastPronto=currentTime;
  129.  
  130.     /* Send pronto start */
  131.     msg.Length = 8;
  132.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTART;
  133.  
  134.     msg.Data[0] = ((channel<<4)|0x0);   /* Channel and Prontoformat 0x000 */
  135.     msg.Data[1] = 0x00;
  136.     msg.Data[2] = 0x00; /* Freq divider */
  137.     msg.Data[3] = modfreq;
  138.     msg.Data[4] = 0x00; /* Once seq length, first byte always 0 */
  139.     msg.Data[5] = len/2;
  140.     msg.Data[6] = 0x00; /* Repeat seq length, always 0 for ir receive */
  141.     msg.Data[7] = 0x00;
  142.    
  143.     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  144.     _delay_ms(1);
  145.  
  146.     /* Send pronto data */
  147.     uint16_t divider = (1000000ULL*modfreq/sns_irTransceive_BaseFrq);
  148.     msg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1;
  149.     /* Counter for buffer */
  150.     uint8_t i = 0;
  151.     /* Counter for filling can frame data */
  152.     uint8_t j = 0;
  153.     /* Remember byte if data does not fit 8 bit */
  154.     uint8_t mem = 0;
  155.     while (i < len)
  156.     {
  157.         if (mem > 0)
  158.         {
  159.             /* If byte was memorized then buffer it */
  160.             msg.Data[j] = mem;
  161.             /* Clear memory */
  162.             mem = 0;
  163.         }
  164.         else
  165.         {
  166.             uint16_t data = buffer[i]/divider;
  167.             if (data >= 256)
  168.             {
  169.                 /* If data does not fit into 8 bit, then split, memorize low byte */
  170.                 mem = data&0xff;
  171.                 /* Buffer high byte */
  172.                 msg.Data[j] = (data>>8)&0xff;
  173.                 j++;
  174.                 if (j==8)
  175.                 {
  176.                     /* If send buffer is full, send frame */
  177.                     /* can buffers will be filled when sending more than 2-3 messages, so retry until sent */
  178.                     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  179.                     _delay_ms(1);
  180.                     j=0;
  181.                     msg.Header.Command++;
  182.                 }
  183.                 /* Set complementary burst pair to 0 to indicate 16bit transmission */
  184.                 msg.Data[j] = 0;
  185.             }
  186.             else
  187.             {
  188.                 msg.Data[j] = data&0xff;
  189.             }
  190.            
  191.             i++;
  192.         }
  193.        
  194.         j++;
  195.         if (j==8)
  196.         {
  197.             /* If send buffer is full, send frame */
  198.             /* can buffers will be filled when sending more than 2-3 messages, so retry until sent */
  199.             while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  200.             _delay_ms(1);
  201.             j=0;
  202.             msg.Header.Command++;
  203.         }
  204.     }
  205.    
  206.     /* End with 25ms, TODO fix uglyness */
  207.     msg.Data[j] = ((IR_MAX_PULSE_WIDTH/divider)>>8)&0xff;
  208.     j++;
  209.     if (j==8)
  210.     {
  211.         /* If send buffer is full, send frame */
  212.         /* can buffers will be filled when sending more than 2-3 messages, so retry until sent */
  213.         while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  214.         _delay_ms(1);
  215.         j=0;
  216.         msg.Header.Command++;
  217.     }
  218.     msg.Data[j] = 0;
  219.     j++;
  220.     msg.Data[j] = (IR_MAX_PULSE_WIDTH/divider)&0xff;
  221.     j++;
  222.     if (j==8)
  223.     {
  224.         /* If send buffer is full, send frame */
  225.         /* can buffers will be filled when sending more than 2-3 messages, so retry until sent */
  226.         while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  227.         _delay_ms(1);
  228.         j=0;
  229.         msg.Header.Command++;
  230.     }
  231.    
  232.     /* Msg command kept from for loop, but increase 16 to send ProntoEnd */
  233.     msg.Header.Command+=16;
  234.     msg.Length = j;
  235.     /* data kept from loop, if any */
  236.     /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  237.     while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  238.     _delay_ms(1);
  239. }
  240. #endif
  241.  
  242.  
  243. #if IR_RX_ENABLE==1
  244. void sns_irTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
  245. {
  246.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  247.     {
  248.         irRxChannel[channel].newData = TRUE;
  249.         irRxChannel[channel].rxlen = len;
  250.     }
  251. }
  252. #endif
  253.  
  254.  
  255. #if IR_TX_ENABLE==1
  256. void sns_irTransceive_TX_done_callback(uint8_t channel)
  257. {
  258.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  259.     {
  260.         irTxChannel[channel].sendComplete = TRUE;
  261.     }
  262. }
  263. #endif
  264.  
  265.  
  266. void sns_irTransceive_setConfig(uint8_t channel, uint8_t config, uint8_t power, uint8_t modfreq)
  267. {
  268.     if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  269.     {
  270. #if IR_RX_ENABLE==1
  271.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE)
  272.         {
  273. #if IR_TX_ENABLE==1
  274.             irTxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  275. #endif
  276.             irRxChannel[channel].modfreq = modfreq;
  277.             irRxChannel[channel].rxbuf = buf[channel];
  278.             switch (channel)
  279.             {
  280.                 case 0:
  281.                     gpio_clr_pin(sns_irTransceive_VCC_EN0_PIN);
  282.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  283.                     break;
  284.                 case 1:
  285.                     gpio_clr_pin(sns_irTransceive_VCC_EN1_PIN);
  286.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  287.                     break;
  288.                 case 2:
  289.                     gpio_clr_pin(sns_irTransceive_VCC_EN2_PIN);
  290.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  291.                     break;
  292.             }
  293.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  294.         }
  295. #endif
  296.  
  297. #if IR_TX_ENABLE==1
  298.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_TRANSMIT)
  299.         {
  300. #if IR_RX_ENABLE==1
  301.             irRxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  302. #endif
  303.             irTxChannel[channel].modfreq = modfreq;
  304.             irTxChannel[channel].txbuf = buf[channel];
  305.             switch (channel)
  306.             {
  307.                 case 0:
  308. #if IR_RX_ENABLE==1
  309.                     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  310.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  311. #endif
  312.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX0_PIN);
  313.  
  314. #if sns_irTransceive_ENABLE_PCA95xx==1
  315.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  316.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  317.                     if (power&0x1)
  318.                     {
  319.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRl);
  320.                     }
  321.                     if (power&0x2)
  322.                     {
  323.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRh);
  324.                     }
  325. #endif
  326.                     break;
  327.                 case 1:
  328. #if IR_RX_ENABLE==1
  329.                     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  330.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  331. #endif
  332.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX1_PIN);
  333.                    
  334. #if sns_irTransceive_ENABLE_PCA95xx==1
  335.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  336.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  337.                     if (power&0x1)
  338.                     {
  339.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRl);
  340.                     }
  341.                     if (power&0x2)
  342.                     {
  343.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRh);
  344.                     }
  345. #endif
  346.                     break;
  347.                 case 2:
  348. #if IR_RX_ENABLE==1
  349.                     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  350.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  351. #endif
  352.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX2_PIN);
  353.                    
  354. #if sns_irTransceive_ENABLE_PCA95xx==1
  355.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  356.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  357.                     if (power&0x1)
  358.                     {
  359.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRl);
  360.                     }
  361.                     if (power&0x2)
  362.                     {
  363.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRh);
  364.                     }
  365. #endif
  366.                     break;
  367.             }
  368.             irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  369.         }
  370. #endif
  371.     }
  372. }
  373.  
  374.  
  375. void sns_irTransceive_Init(void)
  376. {
  377.     StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
  378.     StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  379.     irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  380.     irTxMsg.Header.ModuleId = sns_irTransceive_ID;
  381.     irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
  382.     irTxMsg.Length = 6;
  383.  
  384.     for (uint8_t i = 0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  385.     {
  386. #if IR_RX_ENABLE==1
  387.         irRxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  388.         irRxChannel[i].newData = FALSE;
  389.         irRxChannel[i].rxlen = 0;
  390.         irRxChannel[i].proto.timeout=0;
  391.         irRxChannel[i].proto.data=0;
  392.         irRxChannel[i].proto.repeats=0;
  393.         irRxChannel[i].proto.protocol=0;
  394. #endif
  395.  
  396. #if IR_TX_ENABLE==1
  397.         irTxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  398.         irTxChannel[i].sendComplete = FALSE;
  399.         irTxChannel[i].repeatCount = 0;
  400.         irTxChannel[i].txlen = 0;
  401.         irTxChannel[i].proto.data=0;
  402.         irTxChannel[i].proto.repeats=0;
  403.         irTxChannel[i].proto.framecnt=0;
  404.         irTxChannel[i].proto.protocol=0;
  405. #endif
  406.     }
  407.    
  408. #if IR_RX_ENABLE==1
  409.     // TODO: hardcoded to 3 channels?? /jm
  410.     irRxChannel[0].timerNum=sns_irTransceive_RX0_REPEATE_TIMER;
  411.     irRxChannel[1].timerNum=sns_irTransceive_RX1_REPEATE_TIMER;
  412.     irRxChannel[2].timerNum=sns_irTransceive_RX2_REPEATE_TIMER;
  413. #endif
  414.  
  415. #if IR_TX_ENABLE==1
  416.     // TODO: hardcoded to 3 channels?? /jm
  417.     irTxChannel[0].timerNum=sns_irTransceive_TX0_REPEATE_TIMER;
  418.     irTxChannel[1].timerNum=sns_irTransceive_TX1_REPEATE_TIMER;
  419.     irTxChannel[2].timerNum=sns_irTransceive_TX2_REPEATE_TIMER;
  420. #endif
  421.    
  422.     IrTransceiver_Init();
  423.     /* Configure all TX VCC pins as outputs and disable them */
  424.     gpio_set_out(sns_irTransceive_VCC_EN0_PIN);
  425.     gpio_set_out(sns_irTransceive_VCC_EN1_PIN);
  426.     gpio_set_out(sns_irTransceive_VCC_EN2_PIN);
  427.     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  428.     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  429.     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  430.  
  431. #if IR_TX_ENABLE==1
  432.     gpio_set_out(sns_irTransceive_MOD_PIN);
  433.     gpio_set_out(sns_irTransceive_TX0_PIN);
  434.     gpio_set_out(sns_irTransceive_TX1_PIN);
  435.     gpio_set_out(sns_irTransceive_TX2_PIN);
  436. #if IR_TX_ACTIVE_LOW==1
  437.     gpio_set_pin(sns_irTransceive_TX0_PIN);
  438.     gpio_set_pin(sns_irTransceive_TX1_PIN);
  439.     gpio_set_pin(sns_irTransceive_TX2_PIN);
  440. #endif
  441. #endif
  442.    
  443.     /* IR tx power pins on PCA95xx */
  444. #if sns_irTransceive_ENABLE_PCA95xx==1
  445.     Pca95xx_Init(0);
  446.  
  447.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRl);
  448.     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  449.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRh);
  450.     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  451.    
  452.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRl);
  453.     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  454.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRh);
  455.     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  456.    
  457.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRl);
  458.     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  459.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRh);
  460.     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  461. #endif
  462.  
  463. #ifdef sns_irTransceive_USEEEPROM
  464.     if (EEDATA_OK)
  465.     {
  466.       /* Use stored data to set initial values for the module */
  467.         sns_irTransceive_setConfig(0, eeprom_read_byte(EEDATA.ch0_config), eeprom_read_byte(EEDATA.ch0_txpower), eeprom_read_byte(EEDATA.ch0_modfreq));
  468.         sns_irTransceive_setConfig(1, eeprom_read_byte(EEDATA.ch1_config), eeprom_read_byte(EEDATA.ch1_txpower), eeprom_read_byte(EEDATA.ch1_modfreq));
  469.         sns_irTransceive_setConfig(2, eeprom_read_byte(EEDATA.ch2_config), eeprom_read_byte(EEDATA.ch2_txpower), eeprom_read_byte(EEDATA.ch2_modfreq));
  470.     }
  471.     else
  472.     {  
  473.     /* The CRC of the EEPROM is not correct, store default values and update CRC */
  474.         eeprom_write_byte_crc(EEDATA.ch0_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  475.         eeprom_write_byte_crc(EEDATA.ch0_txpower, 0, WITHOUT_CRC);
  476.         eeprom_write_byte_crc(EEDATA.ch0_modfreq, sns_irTransceive_BaseFrq/38000, WITHOUT_CRC);
  477.         eeprom_write_byte_crc(EEDATA.ch1_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  478.         eeprom_write_byte_crc(EEDATA.ch1_txpower, 0, WITHOUT_CRC);
  479.         eeprom_write_byte_crc(EEDATA.ch1_modfreq, sns_irTransceive_BaseFrq/38000, WITHOUT_CRC);
  480.         eeprom_write_byte_crc(EEDATA.ch2_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  481.         eeprom_write_byte_crc(EEDATA.ch2_txpower, 0, WITHOUT_CRC);
  482.         eeprom_write_byte_crc(EEDATA.ch2_modfreq, sns_irTransceive_BaseFrq/38000, WITHOUT_CRC);
  483.         EEDATA_UPDATE_CRC;
  484.     }
  485. #endif 
  486. }
  487.  
  488. void sns_irTransceive_Process(void)
  489. {
  490.     for (uint8_t channel=0; channel < IR_SUPPORTED_NUM_CHANNELS; channel++)
  491.     {
  492.  
  493. #if IR_RX_ENABLE==1
  494.         switch (irRxChannel[channel].state)
  495.         {
  496.         case sns_irTransceive_STATE_IDLE:
  497.             irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
  498.             break;
  499.  
  500.         case sns_irTransceive_STATE_START_RECEIVE:
  501.             IrTransceiver_ResetRx(channel);
  502.             cli();
  503.             irRxChannel[channel].newData = FALSE;
  504.             sei();
  505.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  506.             break;
  507.        
  508.         case sns_irTransceive_STATE_RECEIVING:
  509.             if (irRxChannel[channel].newData == TRUE) {
  510.                 //TODO: move this line to the RX callback
  511.                 IrTransceiver_DisableRx(channel);
  512.                 cli();
  513.                 irRxChannel[channel].newData = FALSE;
  514.                 sei();
  515.  
  516.                 /* Let protocol driver parse and then send on CAN */
  517.                 uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
  518.                 if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  519.                     irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  520.                     irTxMsg.Data[0] |= channel<<4;
  521.                     irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  522.                     irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  523.                     irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  524.                     irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  525.                     irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  526.  
  527.                     StdCan_Put(&irTxMsg);
  528.                 }
  529.                 else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
  530.                 {
  531. #if (sns_irTransceive_SEND_DEBUG==1)
  532.                     send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
  533.                     irRxChannel[channel].proto.timeout=300;
  534. #elif sns_irTransceive_PRONTO_SUPPORT==1
  535.                     send_pronto(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, channel, irRxChannel[channel].modfreq);
  536.                     irRxChannel[channel].proto.timeout=1;
  537. #endif
  538.                 }
  539.  
  540.                 /* Enable the receiver again */
  541.                 IrTransceiver_EnableRx(channel);
  542.  
  543.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  544.             }
  545.             break;
  546.  
  547.         case sns_irTransceive_STATE_START_PAUSE:
  548.             /* set a timer so we can send release button event when no new IR is arriving */
  549.             Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  550.             irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  551.             break;
  552.  
  553.         case sns_irTransceive_STATE_PAUSING:
  554.             /* reset timer if new IR arrived */
  555.             if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
  556.                 cli();
  557.                 irRxChannel[channel].newData = FALSE;
  558.                 sei();
  559.                 Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  560.             }
  561.        
  562.             if (Timer_Expired(irRxChannel[channel].timerNum)) {
  563.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  564.             }
  565.             break;
  566.  
  567.         case sns_irTransceive_STATE_START_IDLE:
  568.             if (irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  569.                 /* Send button release command on CAN */
  570.                 irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  571.                 irTxMsg.Data[0] |= channel<<4;
  572.                 irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  573.                 irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  574.                 irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  575.                 irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  576.                 irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  577.  
  578.                 StdCan_Put(&irTxMsg);
  579.             }
  580.             irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  581.             break;
  582.  
  583.         default:
  584.             break;
  585.         }
  586. #endif
  587.  
  588. #if IR_TX_ENABLE==1
  589.         switch (irTxChannel[channel].state)
  590.         {
  591.         case sns_irTransceive_STATE_IDLE:
  592.             /* transmission is started when a command is received on can */
  593.             break;
  594.  
  595.         case sns_irTransceive_STATE_START_TRANSMIT:
  596.             if (expandProtocol(irTxChannel[channel].txbuf, &irTxChannel[channel].txlen, &irTxChannel[channel].proto) == IR_OK)
  597.             {
  598.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].txlen);
  599.                 irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  600.             }
  601.             else
  602.             {
  603.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  604.             }
  605.             break;
  606.        
  607.         case sns_irTransceive_STATE_TRANSMITTING:
  608.             if (irTxChannel[channel].sendComplete == TRUE)
  609.             {
  610.                 cli();
  611.                 irTxChannel[channel].sendComplete = FALSE;
  612.                 sei();
  613.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  614.             }
  615.             break;
  616.  
  617.         case sns_irTransceive_STATE_START_PAUSE:
  618.             if (irTxChannel[channel].repeatCount < irTxChannel[channel].proto.repeats)
  619.             {
  620.                 irTxChannel[channel].repeatCount++;
  621.             }
  622.            
  623.             Timer_SetTimeout(irTxChannel[channel].timerNum, irTxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  624.  
  625.             if (irTxChannel[channel].proto.framecnt != 255)
  626.             {
  627.                 irTxChannel[channel].proto.framecnt++;
  628.             }
  629.            
  630.             irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  631.             break;
  632.  
  633.         case sns_irTransceive_STATE_PAUSING:
  634.             if (Timer_Expired(irTxChannel[channel].timerNum))
  635.             {
  636.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  637.             }
  638.            
  639.             /* transmission is stopped when such command is recevied on can */
  640.             if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
  641.             {
  642.                 //TODO maybe send message on can for status? to confirm stopped sending, ready for new command
  643.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  644.             }
  645.             break;
  646.  
  647.         case sns_irTransceive_STATE_START_IDLE:
  648.             irTxChannel[channel].stopSend = FALSE;
  649.             irTxChannel[channel].repeatCount = 0;
  650.             irTxChannel[channel].proto.framecnt = 0;
  651.            
  652.             irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  653.             break;
  654.  
  655.         default:
  656.             break;
  657.         }
  658. #endif
  659.  
  660.     }
  661. }
  662.  
  663.  
  664. /* Handle incoming CAN data */
  665. void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  666. {
  667.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  668.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  669.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_IRTRANSCEIVE &&
  670.         rxMsg->Header.ModuleId == sns_irTransceive_ID)
  671.     {
  672.         uint8_t channel;
  673.         uint8_t length = rxMsg->Length;
  674.         switch (rxMsg->Header.Command)
  675.         {
  676. #if IR_TX_ENABLE==1
  677.         case CAN_MODULE_CMD_PHYSICAL_IR:
  678.             channel = rxMsg->Data[0]>>4;
  679.             if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED && channel < IR_SUPPORTED_NUM_CHANNELS)
  680.             {
  681.                 if (irTxChannel[channel].state == sns_irTransceive_STATE_IDLE)
  682.                 {
  683.                     irTxChannel[channel].proto.protocol = rxMsg->Data[1];
  684.  
  685.                     irTxChannel[channel].proto.data = rxMsg->Data[2];
  686.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  687.                     irTxChannel[channel].proto.data |= rxMsg->Data[3];
  688.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  689.                     irTxChannel[channel].proto.data |= rxMsg->Data[4];
  690.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  691.                     irTxChannel[channel].proto.data |= rxMsg->Data[5];
  692.  
  693.                     irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  694.                 }
  695.             }
  696.             else if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED && channel < IR_SUPPORTED_NUM_CHANNELS)
  697.             {
  698.                 if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE)
  699.                 {
  700.                     irTxChannel[channel].stopSend = TRUE;
  701.                 }
  702.             }
  703.             break;
  704.        
  705.         /* TODO: add struct which stores pronto info:
  706.         channel, pronto receive state, buffer length of once burst pairs, buffer length of repeat burst pairs */
  707.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTART:
  708.             /* TODO: check pronto state, response is different if already sending data */
  709.             if (((rxMsg->Data[0]&0xf) == 0) && rxMsg->Data[1] == 0)
  710.             {
  711.                 uint8_t channel = (rxMsg->Data[0]>>4)&0xf;
  712.                 /* TODO: add channel to pronto struct */
  713.                 /* TODO: find freqdiv, number of once burst pairs, number of repeat burst pairs */
  714.                 uint8_t modfreq;
  715.                 uint8_t oncebursts;
  716.                 uint8_t repeatbursts;
  717.                 /* TODO: add to pronto struct */
  718.             }
  719.            
  720.             /* TODO: Send response frame */
  721.             break;
  722.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTOP:
  723.             /* TODO: stop sending IR */
  724.            
  725.             /* TODO: Send response frame */
  726.             break;
  727.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOCONTINUE:
  728.             /* TODO: reset timeout to continue sending repeat sequences */
  729.            
  730.             /* TODO: Send response frame */
  731.             break;
  732.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1:  /* Fall through */
  733.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA2:  /* Fall through */
  734.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA3:  /* Fall through */
  735.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA4:  /* Fall through */
  736.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA5:  /* Fall through */
  737.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA6:  /* Fall through */
  738.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA7:  /* Fall through */
  739.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA8:  /* Fall through */
  740.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA9:  /* Fall through */
  741.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA10: /* Fall through */
  742.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA11: /* Fall through */
  743.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA12: /* Fall through */
  744.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA13: /* Fall through */
  745.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA14: /* Fall through */
  746.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA15: /* Fall through */
  747.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA16: /* Fall through */
  748.             ; /* Added to avoid compilation error "a label can only be part of a statement ..." */
  749.             uint8_t dataindex = rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1;
  750.             for (uint8_t i=0; i<length; i++)
  751.             {
  752.                 /* TODO: pack data into buffer irTxChannel[channel].txbuf */
  753.             }
  754.             break;
  755.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1:   /* Fall through */
  756.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND2:   /* Fall through */
  757.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND3:   /* Fall through */
  758.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND4:   /* Fall through */
  759.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND5:   /* Fall through */
  760.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND6:   /* Fall through */
  761.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND7:   /* Fall through */
  762.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND8:   /* Fall through */
  763.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND9:   /* Fall through */
  764.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND10:  /* Fall through */
  765.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND11:  /* Fall through */
  766.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND12:  /* Fall through */
  767.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND13:  /* Fall through */
  768.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND14:  /* Fall through */
  769.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND15:  /* Fall through */
  770.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND16:  /* Fall through */
  771.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND17:  /* Fall through */
  772.             ; /* Added to avoid compilation error "a label can only be part of a statement ..." */
  773.             uint8_t endindex = rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1;
  774.             /* TODO: pack data, if any, into buffer irTxChannel[channel].txbuf */
  775.  
  776.             /* TODO: Last byte contains once/repeat information */
  777.            
  778.             /* TODO: Send response frame */
  779.            
  780.             /* TODO: start transmitt ir! */
  781.             break;
  782.  
  783. /* TODO: In IR state machine add sending a response frame when ir stops sending, also implement pronto repeat */
  784. #endif
  785.        
  786.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRCONFIG:
  787.             channel = rxMsg->Data[0]>>4;
  788.             uint8_t config = rxMsg->Data[0] & 0x0f;
  789.             uint8_t power = rxMsg->Data[1]>>6;
  790.             uint8_t modfreq = rxMsg->Data[2];
  791.            
  792.             sns_irTransceive_setConfig(channel, config, power, modfreq);
  793.  
  794. #ifdef sns_irTransceive_USEEEPROM
  795.             if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  796.             {
  797.                 switch (channel)
  798.                 {
  799.                     case 0:
  800.                         eeprom_write_byte_crc(EEDATA.ch0_config, config, WITHOUT_CRC);
  801.                         eeprom_write_byte_crc(EEDATA.ch0_txpower, power, WITHOUT_CRC);
  802.                         eeprom_write_byte_crc(EEDATA.ch0_modfreq, modfreq, WITHOUT_CRC);
  803.                         break;
  804.                     case 1:
  805.                         eeprom_write_byte_crc(EEDATA.ch1_config, config, WITHOUT_CRC);
  806.                         eeprom_write_byte_crc(EEDATA.ch1_txpower, power, WITHOUT_CRC);
  807.                         eeprom_write_byte_crc(EEDATA.ch1_modfreq, modfreq, WITHOUT_CRC);
  808.                         break;
  809.                     case 2:
  810.                         eeprom_write_byte_crc(EEDATA.ch2_config, config, WITHOUT_CRC);
  811.                         eeprom_write_byte_crc(EEDATA.ch2_txpower, power, WITHOUT_CRC);
  812.                         eeprom_write_byte_crc(EEDATA.ch2_modfreq, modfreq, WITHOUT_CRC);
  813.                         break;
  814.                     default:
  815.                         break;
  816.                 }
  817.                 EEDATA_UPDATE_CRC;
  818.             }
  819. #endif 
  820.             break;
  821.            
  822.         default:
  823.             break;
  824.            
  825.         }
  826.     }
  827. }
  828.  
  829. void sns_irTransceive_List(uint8_t ModuleSequenceNumber)
  830. {
  831.     StdCan_Msg_t txMsg;
  832.    
  833.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  834.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  835.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  836.  
  837.     txMsg.Header.ModuleId = sns_irTransceive_ID;
  838.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  839.     txMsg.Length = 6;
  840.  
  841.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  842.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  843.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  844.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  845.    
  846.     txMsg.Data[4] = NUMBER_OF_MODULES;
  847.     txMsg.Data[5] = ModuleSequenceNumber;
  848.    
  849.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  850. }
  851.