Subversion Repositories HomeAutomation

Rev

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