Subversion Repositories HomeAutomation

Rev

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