Subversion Repositories HomeAutomation

Rev

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