Subversion Repositories HomeAutomation

Rev

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