Subversion Repositories HomeAutomation

Rev

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