Subversion Repositories HomeAutomation

Rev

Rev 1822 | Rev 1828 | 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.     if (j>0)
  233.     {
  234.         /* msg command kept from for loop, but increase 16 to send ProntoEnd */
  235.         msg.Header.Command+=16;
  236.         msg.Length = j;
  237.         /* data kept from loop */
  238.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  239.         while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
  240.         _delay_ms(1);
  241.     }
  242. }
  243. #endif
  244.  
  245.  
  246. #if IR_RX_ENABLE==1
  247. void sns_irTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
  248. {
  249.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  250.     {
  251.         irRxChannel[channel].newData = TRUE;
  252.         irRxChannel[channel].rxlen = len;
  253.     }
  254. }
  255. #endif
  256.  
  257.  
  258. #if IR_TX_ENABLE==1
  259. void sns_irTransceive_TX_done_callback(uint8_t channel)
  260. {
  261.     if (channel < IR_SUPPORTED_NUM_CHANNELS)
  262.     {
  263.         irTxChannel[channel].sendComplete = TRUE;
  264.     }
  265. }
  266. #endif
  267.  
  268.  
  269. void sns_irTransceive_setConfig(uint8_t channel, uint8_t config, uint8_t power, uint8_t modfreq)
  270. {
  271.     if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  272.     {
  273. #if IR_RX_ENABLE==1
  274.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE)
  275.         {
  276. #if IR_TX_ENABLE==1
  277.             irTxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  278. #endif
  279.             irRxChannel[channel].modfreq = modfreq;
  280.             irRxChannel[channel].rxbuf = buf[channel];
  281.             switch (channel)
  282.             {
  283.                 case 0:
  284.                     gpio_clr_pin(sns_irTransceive_VCC_EN0_PIN);
  285.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  286.                     break;
  287.                 case 1:
  288.                     gpio_clr_pin(sns_irTransceive_VCC_EN1_PIN);
  289.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  290.                     break;
  291.                 case 2:
  292.                     gpio_clr_pin(sns_irTransceive_VCC_EN2_PIN);
  293.                     IrTransceiver_InitRxChannel(channel, irRxChannel[channel].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  294.                     break;
  295.             }
  296.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  297.         }
  298. #endif
  299.  
  300. #if IR_TX_ENABLE==1
  301.         if (config == CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_TRANSMIT)
  302.         {
  303. #if IR_RX_ENABLE==1
  304.             irRxChannel[channel].state = sns_irTransceive_STATE_DISABLED;
  305. #endif
  306.             irTxChannel[channel].modfreq = modfreq;
  307.             irTxChannel[channel].txbuf = buf[channel];
  308.             switch (channel)
  309.             {
  310.                 case 0:
  311. #if IR_RX_ENABLE==1
  312.                     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  313.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  314. #endif
  315.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX0_PIN);
  316.  
  317. #if sns_irTransceive_ENABLE_PCA95xx==1
  318.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  319.                     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  320.                     if (power&0x1)
  321.                     {
  322.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRl);
  323.                     }
  324.                     if (power&0x2)
  325.                     {
  326.                         Pca95xx_set_in(sns_irTransceive_TX0_PWRh);
  327.                     }
  328. #endif
  329.                     break;
  330.                 case 1:
  331. #if IR_RX_ENABLE==1
  332.                     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  333.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX1_PCINT, sns_irTransceive_RX1_PIN);
  334. #endif
  335.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX1_PIN);
  336.                    
  337. #if sns_irTransceive_ENABLE_PCA95xx==1
  338.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  339.                     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  340.                     if (power&0x1)
  341.                     {
  342.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRl);
  343.                     }
  344.                     if (power&0x2)
  345.                     {
  346.                         Pca95xx_set_in(sns_irTransceive_TX1_PWRh);
  347.                     }
  348. #endif
  349.                     break;
  350.                 case 2:
  351. #if IR_RX_ENABLE==1
  352.                     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  353.                     IrTransceiver_DeInitRxChannel(channel, sns_irTransceive_RX2_PCINT, sns_irTransceive_RX2_PIN);
  354. #endif
  355.                     IrTransceiver_InitTxChannel(channel, sns_irTransceive_TX_done_callback, sns_irTransceive_TX2_PIN);
  356.                    
  357. #if sns_irTransceive_ENABLE_PCA95xx==1
  358.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  359.                     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  360.                     if (power&0x1)
  361.                     {
  362.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRl);
  363.                     }
  364.                     if (power&0x2)
  365.                     {
  366.                         Pca95xx_set_in(sns_irTransceive_TX2_PWRh);
  367.                     }
  368. #endif
  369.                     break;
  370.             }
  371.             irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  372.         }
  373. #endif
  374.     }
  375. }
  376.  
  377.  
  378. void sns_irTransceive_Init(void)
  379. {
  380.     StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
  381.     StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  382.     irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  383.     irTxMsg.Header.ModuleId = sns_irTransceive_ID;
  384.     irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
  385.     irTxMsg.Length = 6;
  386.  
  387.     for (uint8_t i = 0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  388.     {
  389. #if IR_RX_ENABLE==1
  390.         irRxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  391.         irRxChannel[i].newData = FALSE;
  392.         irRxChannel[i].rxlen = 0;
  393.         irRxChannel[i].proto.timeout=0;
  394.         irRxChannel[i].proto.data=0;
  395.         irRxChannel[i].proto.repeats=0;
  396.         irRxChannel[i].proto.protocol=0;
  397. #endif
  398.  
  399. #if IR_TX_ENABLE==1
  400.         irTxChannel[i].state = sns_irTransceive_STATE_DISABLED;
  401.         irTxChannel[i].sendComplete = FALSE;
  402.         irTxChannel[i].repeatCount = 0;
  403.         irTxChannel[i].txlen = 0;
  404.         irTxChannel[i].proto.data=0;
  405.         irTxChannel[i].proto.repeats=0;
  406.         irTxChannel[i].proto.framecnt=0;
  407.         irTxChannel[i].proto.protocol=0;
  408. #endif
  409.     }
  410.    
  411. #if IR_RX_ENABLE==1
  412.     // TODO: hardcoded to 3 channels?? /jm
  413.     irRxChannel[0].timerNum=sns_irTransceive_RX0_REPEATE_TIMER;
  414.     irRxChannel[1].timerNum=sns_irTransceive_RX1_REPEATE_TIMER;
  415.     irRxChannel[2].timerNum=sns_irTransceive_RX2_REPEATE_TIMER;
  416. #endif
  417.  
  418. #if IR_TX_ENABLE==1
  419.     // TODO: hardcoded to 3 channels?? /jm
  420.     irTxChannel[0].timerNum=sns_irTransceive_TX0_REPEATE_TIMER;
  421.     irTxChannel[1].timerNum=sns_irTransceive_TX1_REPEATE_TIMER;
  422.     irTxChannel[2].timerNum=sns_irTransceive_TX2_REPEATE_TIMER;
  423. #endif
  424.    
  425.     IrTransceiver_Init();
  426.     /* Configure all TX VCC pins as outputs and disable them */
  427.     gpio_set_out(sns_irTransceive_VCC_EN0_PIN);
  428.     gpio_set_out(sns_irTransceive_VCC_EN1_PIN);
  429.     gpio_set_out(sns_irTransceive_VCC_EN2_PIN);
  430.     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  431.     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  432.     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  433.  
  434. #if IR_TX_ENABLE==1
  435.     gpio_set_out(sns_irTransceive_MOD_PIN);
  436.     gpio_set_out(sns_irTransceive_TX0_PIN);
  437.     gpio_set_out(sns_irTransceive_TX1_PIN);
  438.     gpio_set_out(sns_irTransceive_TX2_PIN);
  439. #if IR_TX_ACTIVE_LOW==1
  440.     gpio_set_pin(sns_irTransceive_TX0_PIN);
  441.     gpio_set_pin(sns_irTransceive_TX1_PIN);
  442.     gpio_set_pin(sns_irTransceive_TX2_PIN);
  443. #endif
  444. #endif
  445.    
  446.     /* IR tx power pins on PCA95xx */
  447. #if sns_irTransceive_ENABLE_PCA95xx==1
  448.     Pca95xx_Init(0);
  449.  
  450.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRl);
  451.     Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
  452.     Pca95xx_clr_pin(sns_irTransceive_TX0_PWRh);
  453.     Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
  454.    
  455.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRl);
  456.     Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
  457.     Pca95xx_clr_pin(sns_irTransceive_TX1_PWRh);
  458.     Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
  459.    
  460.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRl);
  461.     Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
  462.     Pca95xx_clr_pin(sns_irTransceive_TX2_PWRh);
  463.     Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
  464. #endif
  465.  
  466. #ifdef sns_irTransceive_USEEEPROM
  467.     if (EEDATA_OK)
  468.     {
  469.       /* Use stored data to set initial values for the module */
  470.         sns_irTransceive_setConfig(0, eeprom_read_byte(EEDATA.ch0_config), eeprom_read_byte(EEDATA.ch0_txpower), eeprom_read_byte(EEDATA.ch0_modfreq));
  471.         sns_irTransceive_setConfig(1, eeprom_read_byte(EEDATA.ch1_config), eeprom_read_byte(EEDATA.ch1_txpower), eeprom_read_byte(EEDATA.ch1_modfreq));
  472.         sns_irTransceive_setConfig(2, eeprom_read_byte(EEDATA.ch2_config), eeprom_read_byte(EEDATA.ch2_txpower), eeprom_read_byte(EEDATA.ch2_modfreq));
  473.     }
  474.     else
  475.     {  
  476.     /* The CRC of the EEPROM is not correct, store default values and update CRC */
  477.         eeprom_write_byte_crc(EEDATA.ch0_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  478.         eeprom_write_byte_crc(EEDATA.ch0_txpower, 0, WITHOUT_CRC);
  479.         eeprom_write_byte_crc(EEDATA.ch0_modfreq, sns_irTransceive_BaseFrq/38000, WITHOUT_CRC);
  480.         eeprom_write_byte_crc(EEDATA.ch1_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  481.         eeprom_write_byte_crc(EEDATA.ch1_txpower, 0, WITHOUT_CRC);
  482.         eeprom_write_byte_crc(EEDATA.ch1_modfreq, sns_irTransceive_BaseFrq/38000, WITHOUT_CRC);
  483.         eeprom_write_byte_crc(EEDATA.ch2_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
  484.         eeprom_write_byte_crc(EEDATA.ch2_txpower, 0, WITHOUT_CRC);
  485.         eeprom_write_byte_crc(EEDATA.ch2_modfreq, sns_irTransceive_BaseFrq/38000, WITHOUT_CRC);
  486.         EEDATA_UPDATE_CRC;
  487.     }
  488. #endif 
  489. }
  490.  
  491. void sns_irTransceive_Process(void)
  492. {
  493.     for (uint8_t channel=0; channel < IR_SUPPORTED_NUM_CHANNELS; channel++)
  494.     {
  495.  
  496. #if IR_RX_ENABLE==1
  497.         switch (irRxChannel[channel].state)
  498.         {
  499.         case sns_irTransceive_STATE_IDLE:
  500.             irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
  501.             break;
  502.  
  503.         case sns_irTransceive_STATE_START_RECEIVE:
  504.             IrTransceiver_ResetRx(channel);
  505.             cli();
  506.             irRxChannel[channel].newData = FALSE;
  507.             sei();
  508.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  509.             break;
  510.        
  511.         case sns_irTransceive_STATE_RECEIVING:
  512.             if (irRxChannel[channel].newData == TRUE) {
  513.                 //TODO: move this line to the RX callback
  514.                 IrTransceiver_DisableRx(channel);
  515.                 cli();
  516.                 irRxChannel[channel].newData = FALSE;
  517.                 sei();
  518.  
  519.                 /* Let protocol driver parse and then send on CAN */
  520.                 uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
  521.                 if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  522.                     irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  523.                     irTxMsg.Data[0] |= channel<<4;
  524.                     irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  525.                     irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  526.                     irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  527.                     irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  528.                     irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  529.  
  530.                     StdCan_Put(&irTxMsg);
  531.                 }
  532.                 else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
  533.                 {
  534. #if (sns_irTransceive_SEND_DEBUG==1)
  535.                     send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
  536.                     irRxChannel[channel].proto.timeout=300;
  537. #elif sns_irTransceive_PRONTO_SUPPORT==1
  538.                     send_pronto(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, channel, irRxChannel[channel].modfreq);
  539.                     irRxChannel[channel].proto.timeout=1;
  540. #endif
  541.                 }
  542.  
  543.                 /* Enable the receiver again */
  544.                 IrTransceiver_EnableRx(channel);
  545.  
  546.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  547.             }
  548.             break;
  549.  
  550.         case sns_irTransceive_STATE_START_PAUSE:
  551.             /* set a timer so we can send release button event when no new IR is arriving */
  552.             Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  553.             irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  554.             break;
  555.  
  556.         case sns_irTransceive_STATE_PAUSING:
  557.             /* reset timer if new IR arrived */
  558.             if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
  559.                 cli();
  560.                 irRxChannel[channel].newData = FALSE;
  561.                 sei();
  562.                 Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  563.             }
  564.        
  565.             if (Timer_Expired(irRxChannel[channel].timerNum)) {
  566.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  567.             }
  568.             break;
  569.  
  570.         case sns_irTransceive_STATE_START_IDLE:
  571.             if (irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  572.                 /* Send button release command on CAN */
  573.                 irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  574.                 irTxMsg.Data[0] |= channel<<4;
  575.                 irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  576.                 irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  577.                 irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  578.                 irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  579.                 irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  580.  
  581.                 StdCan_Put(&irTxMsg);
  582.             }
  583.             irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  584.             break;
  585.  
  586.         default:
  587.             break;
  588.         }
  589. #endif
  590.  
  591. #if IR_TX_ENABLE==1
  592.         switch (irTxChannel[channel].state)
  593.         {
  594.         case sns_irTransceive_STATE_IDLE:
  595.             /* transmission is started when a command is received on can */
  596.             break;
  597.  
  598.         case sns_irTransceive_STATE_START_TRANSMIT:
  599.             if (expandProtocol(irTxChannel[channel].txbuf, &irTxChannel[channel].txlen, &irTxChannel[channel].proto) == IR_OK)
  600.             {
  601.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, irTxChannel[channel].txlen);
  602.                 irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  603.             }
  604.             else
  605.             {
  606.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  607.             }
  608.             break;
  609.        
  610.         case sns_irTransceive_STATE_TRANSMITTING:
  611.             if (irTxChannel[channel].sendComplete == TRUE)
  612.             {
  613.                 cli();
  614.                 irTxChannel[channel].sendComplete = FALSE;
  615.                 sei();
  616.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  617.             }
  618.             break;
  619.  
  620.         case sns_irTransceive_STATE_START_PAUSE:
  621.             if (irTxChannel[channel].repeatCount < irTxChannel[channel].proto.repeats)
  622.             {
  623.                 irTxChannel[channel].repeatCount++;
  624.             }
  625.            
  626.             Timer_SetTimeout(irTxChannel[channel].timerNum, irTxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  627.  
  628.             if (irTxChannel[channel].proto.framecnt != 255)
  629.             {
  630.                 irTxChannel[channel].proto.framecnt++;
  631.             }
  632.            
  633.             irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  634.             break;
  635.  
  636.         case sns_irTransceive_STATE_PAUSING:
  637.             if (Timer_Expired(irTxChannel[channel].timerNum))
  638.             {
  639.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  640.             }
  641.            
  642.             /* transmission is stopped when such command is recevied on can */
  643.             if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
  644.             {
  645.                 //TODO maybe send message on can for status? to confirm stopped sending, ready for new command
  646.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  647.             }
  648.             break;
  649.  
  650.         case sns_irTransceive_STATE_START_IDLE:
  651.             irTxChannel[channel].stopSend = FALSE;
  652.             irTxChannel[channel].repeatCount = 0;
  653.             irTxChannel[channel].proto.framecnt = 0;
  654.            
  655.             irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  656.             break;
  657.  
  658.         default:
  659.             break;
  660.         }
  661. #endif
  662.  
  663.     }
  664. }
  665.  
  666.  
  667. /* Handle incoming CAN data */
  668. void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  669. {
  670.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  671.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  672.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_IRTRANSCEIVE &&
  673.         rxMsg->Header.ModuleId == sns_irTransceive_ID)
  674.     {
  675.         uint8_t channel;
  676.         switch (rxMsg->Header.Command)
  677.         {
  678. #if IR_TX_ENABLE==1
  679.         case CAN_MODULE_CMD_PHYSICAL_IR:
  680.             channel = rxMsg->Data[0]>>4;
  681.             if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED && channel < IR_SUPPORTED_NUM_CHANNELS)
  682.             {
  683.                 if (irTxChannel[channel].state == sns_irTransceive_STATE_IDLE)
  684.                 {
  685.                     irTxChannel[channel].proto.protocol = rxMsg->Data[1];
  686.  
  687.                     irTxChannel[channel].proto.data = rxMsg->Data[2];
  688.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  689.                     irTxChannel[channel].proto.data |= rxMsg->Data[3];
  690.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  691.                     irTxChannel[channel].proto.data |= rxMsg->Data[4];
  692.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  693.                     irTxChannel[channel].proto.data |= rxMsg->Data[5];
  694.  
  695.                     irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  696.                 }
  697.             }
  698.             else if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED && channel < IR_SUPPORTED_NUM_CHANNELS)
  699.             {
  700.                 if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE)
  701.                 {
  702.                     irTxChannel[channel].stopSend = TRUE;
  703.                 }
  704.             }
  705.             break;
  706. #endif
  707.        
  708.         case CAN_MODULE_CMD_IRTRANSCEIVE_IRCONFIG:
  709.             channel = rxMsg->Data[0]>>4;
  710.             uint8_t config = rxMsg->Data[0] & 0x0f;
  711.             uint8_t power = rxMsg->Data[1]>>6;
  712.             uint8_t modfreq = rxMsg->Data[2];
  713.            
  714.             sns_irTransceive_setConfig(channel, config, power, modfreq);
  715.  
  716. #ifdef sns_irTransceive_USEEEPROM
  717.             if (channel < IR_SUPPORTED_NUM_CHANNELS && config <= CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_RECEIVE && power < 4)
  718.             {
  719.                 switch (channel)
  720.                 {
  721.                     case 0:
  722.                         eeprom_write_byte_crc(EEDATA.ch0_config, config, WITHOUT_CRC);
  723.                         eeprom_write_byte_crc(EEDATA.ch0_txpower, power, WITHOUT_CRC);
  724.                         eeprom_write_byte_crc(EEDATA.ch0_modfreq, modfreq, WITHOUT_CRC);
  725.                         break;
  726.                     case 1:
  727.                         eeprom_write_byte_crc(EEDATA.ch1_config, config, WITHOUT_CRC);
  728.                         eeprom_write_byte_crc(EEDATA.ch1_txpower, power, WITHOUT_CRC);
  729.                         eeprom_write_byte_crc(EEDATA.ch1_modfreq, modfreq, WITHOUT_CRC);
  730.                         break;
  731.                     case 2:
  732.                         eeprom_write_byte_crc(EEDATA.ch2_config, config, WITHOUT_CRC);
  733.                         eeprom_write_byte_crc(EEDATA.ch2_txpower, power, WITHOUT_CRC);
  734.                         eeprom_write_byte_crc(EEDATA.ch2_modfreq, modfreq, WITHOUT_CRC);
  735.                         break;
  736.                     default:
  737.                         break;
  738.                 }
  739.                 EEDATA_UPDATE_CRC;
  740.             }
  741. #endif 
  742.             break;
  743.            
  744.         default:
  745.             break;
  746.            
  747.         }
  748.     }
  749. }
  750.  
  751. void sns_irTransceive_List(uint8_t ModuleSequenceNumber)
  752. {
  753.     StdCan_Msg_t txMsg;
  754.    
  755.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  756.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  757.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  758.  
  759.     txMsg.Header.ModuleId = sns_irTransceive_ID;
  760.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  761.     txMsg.Length = 6;
  762.  
  763.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  764.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  765.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  766.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  767.    
  768.     txMsg.Data[4] = NUMBER_OF_MODULES;
  769.     txMsg.Data[5] = ModuleSequenceNumber;
  770.    
  771.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  772. }
  773.