Subversion Repositories HomeAutomation

Rev

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