Subversion Repositories HomeAutomation

Rev

Rev 1865 | Rev 1931 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * IR receiver and transmitter driver.
  3.  *
  4.  * @date    2006-12-10
  5.  *
  6.  * @author  Anders Runeson, Andreas Fritiofson
  7.  *  
  8.  */
  9.  
  10. /*-----------------------------------------------------------------------------
  11.  * Includes
  12.  *---------------------------------------------------------------------------*/
  13. #include <config.h>
  14. #include <drivers/mcu/pcint.h>
  15. #include <drivers/ir/transceiver/irtransceiverMulti.h>
  16. #include <drivers/ir/protocols.h>
  17. #include <avr/io.h>
  18. #include <avr/interrupt.h>
  19.  
  20.  
  21. #include <drivers/mcu/gpio.h>
  22.  
  23. /*-----------------------------------------------------------------------------
  24.  * Prerequisites
  25.  *---------------------------------------------------------------------------*/
  26. #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__)
  27. #define TIMSK1  TIMSK
  28. #define TIFR1   TIFR
  29. #define ICIE1   TICIE1
  30. #endif
  31.  
  32. #define IR_COMPARE_VECTOR       TIMER1_COMPB_vect
  33. #define IR_TIMEOUT_VECTOR       TIMER1_COMPA_vect
  34. #define IR_CAPTURE_VECTOR       TIMER1_CAPT_vect
  35. #define IR_TIMEOUT_REG          OCR1A
  36. #define IR_COMPARE_REG          OCR1B
  37. #define IR_CAPTURE_REG          ICR1
  38. #define IR_COUNT_REG            TCNT1
  39. #define IR_MODULATION_REG       OCR0A
  40. /*#define IR_TIMER_INIT()           TCCR1A = 0; \
  41.                                 TCCR1B = (1<<ICNC1)|(2<<CS10);
  42.                                 */
  43. #define IR_TIMER_INIT()         TCCR1A = 0; \
  44.                                 TCCR1B = (2<<CS10);
  45. #define IR_MASK_COMPARE()       TIMSK1 &= ~(1<<OCIE1B);
  46. #define IR_UNMASK_COMPARE()     TIFR1 = (1<<OCF1B); TIMSK1 |= (1<<OCIE1B);
  47. #define IR_MASK_TIMEOUT()       TIMSK1 &= ~(1<<OCIE1A);
  48. #define IR_UNMASK_TIMEOUT()     TIFR1 = (1<<OCF1A); TIMSK1 |= (1<<OCIE1A);
  49. #define IR_MASK_CAPTURE()       TIMSK1 &= ~(1<<ICIE1);
  50. #define IR_UNMASK_CAPTURE()     TIFR1 = (1<<ICF1); TIMSK1 |= (1<<ICIE1);
  51. #define IR_CAPTURE_FALLING()    TCCR1B &= ~(1<<ICES1); \
  52.                                 TIFR1 = (1<<ICF1);
  53. #define IR_CAPTURE_RISING()     TCCR1B |= (1<<ICES1); \
  54.                                 TIFR1 = (1<<ICF1);
  55.  
  56. //#define IR_OUTP_HIGH()            TCCR0A |= (1<<COM0A0);
  57. //#define IR_OUTP_LOW()         TCCR0A &= ~(1<<COM0A0);
  58. #if IR_TX_ACTIVE_LOW==1
  59. #define IR_OUTP_HIGH()  *drvIrTxChannel[channel].port &= ~drvIrTxChannel[channel].pinmask
  60. #define IR_OUTP_LOW()   *drvIrTxChannel[channel].port |= drvIrTxChannel[channel].pinmask
  61. #else
  62. #define IR_OUTP_HIGH()  *drvIrTxChannel[channel].port |= drvIrTxChannel[channel].pinmask
  63. #define IR_OUTP_LOW()   *drvIrTxChannel[channel].port &= ~drvIrTxChannel[channel].pinmask
  64. #endif
  65.  
  66.  
  67. /*-----------------------------------------------------------------------------
  68.  * Globals
  69.  *---------------------------------------------------------------------------*/
  70. #if IR_RX_ENABLE==1
  71. struct {
  72.     uint8_t     enable;
  73.     uint16_t    timeout;
  74.     uint16_t    *rxbuf;
  75.     uint8_t     rxlen;
  76.     uint8_t     storeEnable;
  77.     irRxCallback_t callback;
  78. } drvIrRxChannel[3];
  79. #endif
  80.  
  81. #if IR_TX_ENABLE==1
  82. struct {
  83.     uint8_t     enable;
  84.     uint16_t    timeout;
  85.     uint8_t     timeoutEnable;
  86.     uint16_t    *txbuf;
  87.     uint8_t     txlen;
  88.     uint8_t     txindex;
  89.     irTxCallback_t callback;
  90.     volatile uint8_t    *port;
  91.     uint8_t     pinmask;
  92. } drvIrTxChannel[3];
  93.  
  94. uint16_t drvIrTxModFreqkHz = 38; /* Default to 38kHz */
  95.  
  96. #endif
  97.  
  98.  
  99. /*-----------------------------------------------------------------------------
  100.  * Interrupt Handlers
  101.  *---------------------------------------------------------------------------*/
  102. #if IR_TX_ENABLE==1
  103. ISR(IR_COMPARE_VECTOR)
  104. {
  105.     /* find which channel */
  106.     uint16_t time = IR_COMPARE_REG;
  107.     uint16_t diff;
  108.     uint8_t channel;
  109.  
  110.     /* go through all channels and check which of thems have overflowed */
  111.     for (channel=0; channel < IR_SUPPORTED_NUM_CHANNELS; channel++)
  112.     {
  113.         /* if channel is waiting for an overflow */
  114.         if (drvIrTxChannel[channel].timeoutEnable==TRUE)
  115.         {
  116.             /* check if channel have overflowed or is about to overflow */
  117.             diff = (time+50)-drvIrTxChannel[channel].timeout;
  118.             if (diff < IR_MAX_PULSE_WIDTH)
  119.             {
  120.                 /* check what to output next */
  121.                 if ((drvIrTxChannel[channel].txindex&1) == 1) {     /* if odd, ir-pause */
  122.                     IR_OUTP_LOW();
  123.                 } else {
  124.                     IR_OUTP_HIGH();
  125.                 }
  126.                
  127.                 /* is there more to send */
  128.                 if (drvIrTxChannel[channel].txindex < drvIrTxChannel[channel].txlen)
  129.                 {
  130.                     /* load next timeout value */
  131.                     drvIrTxChannel[channel].timeout = IR_COMPARE_REG + drvIrTxChannel[channel].txbuf[drvIrTxChannel[channel].txindex++];
  132.                 }
  133.                 else
  134.                 {
  135.                     /* disable this channel */
  136.                     drvIrTxChannel[channel].timeoutEnable = FALSE;
  137.  
  138.                     /* disable the overflow interrupt */
  139.                     IR_MASK_COMPARE();
  140.        
  141.                     /* notify the application that a pulse train has been sent. */
  142.                     drvIrTxChannel[channel].callback(channel);
  143.                 }
  144.             }
  145.         }
  146.     }
  147.    
  148.     channel = 0;
  149.     diff = 0xffff;
  150.     /* go through all channels and find the one that have the next overflow */
  151.     for (uint8_t i=0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  152.     {
  153.         /* if channel is waiting for an overflow */
  154.         if (drvIrTxChannel[i].timeoutEnable==TRUE)
  155.         {
  156.             if (drvIrTxChannel[i].timeout - time < diff)
  157.             {
  158.                 diff = drvIrTxChannel[i].timeout - time;
  159.                 channel = i;
  160.             }
  161.             /* enable overflow interrupt */
  162.             IR_UNMASK_COMPARE();
  163.         }      
  164.     }  
  165.  
  166.     /* set compare value to the channel that have the next overflow */
  167.     IR_COMPARE_REG = drvIrTxChannel[channel].timeout;
  168. }
  169. #endif
  170.  
  171.  
  172. #if IR_RX_ENABLE==1
  173. /* When this timeout occurs a pulsetrain is complete */
  174. ISR(IR_TIMEOUT_VECTOR)
  175. {
  176.     for (uint8_t i=0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  177.     {
  178.         /* If more than 2 edges were recevied */
  179.         if (drvIrRxChannel[i].storeEnable == TRUE && drvIrRxChannel[i].rxlen > 2)
  180.         {
  181.             /* Notify the application that a pulse train has been received. */
  182.             drvIrRxChannel[i].callback(i, drvIrRxChannel[i].rxbuf, drvIrRxChannel[i].rxlen);
  183.         }
  184.  
  185.         drvIrRxChannel[i].storeEnable = FALSE;
  186.     }
  187.  
  188.     /* Disable ISR */
  189.     IR_MASK_TIMEOUT();
  190.    
  191. //  gpio_toggle_pin(EXP_J);
  192. }
  193. #endif
  194.  
  195.  
  196. /*-----------------------------------------------------------------------------
  197.  * Private Functions
  198.  *---------------------------------------------------------------------------*/
  199.  
  200. #if IR_RX_ENABLE==1
  201.  
  202. /* these wrapper functions are called from the pcint driver (callback) */
  203. void IrTransceiver_Store_ch0(uint8_t id, uint8_t status)
  204. {
  205.     IrTransceiver_Store(0);
  206. }
  207. void IrTransceiver_Store_ch1(uint8_t id, uint8_t status)
  208. {
  209.     IrTransceiver_Store(1);
  210. }
  211. void IrTransceiver_Store_ch2(uint8_t id, uint8_t status)
  212. {
  213.     IrTransceiver_Store(2);
  214. }
  215.  
  216. void IrTransceiver_Store(uint8_t channel)
  217. {
  218.     static uint16_t prev_time[3];
  219.     uint16_t pulsewidth;
  220.  
  221.     /* Read the timer counter register to get the current "time". */
  222.     uint16_t time = IR_COUNT_REG;
  223.    
  224.     /* Subtract the current measurement from the previous to get the pulse width. */
  225.     pulsewidth = time - prev_time[channel];
  226.     prev_time[channel] = time;
  227.  
  228. #if IR_MIN_STARTPULSE_WIDTH>0
  229.     if ((pulsewidth <= IR_MIN_STARTPULSE_WIDTH) && (drvIrRxChannel[channel].storeEnable == TRUE) && (drvIrRxChannel[channel].rxlen == 0))
  230.     {
  231.         IR_MASK_TIMEOUT();
  232.         return;
  233.     }
  234.     else
  235.     {
  236.         IR_UNMASK_TIMEOUT();
  237.     }
  238. #endif
  239. //gpio_toggle_pin(EXP_K);
  240.  
  241.     if (drvIrRxChannel[channel].storeEnable)
  242.     {
  243.         /* Store the measurement. */
  244.         drvIrRxChannel[channel].rxbuf[drvIrRxChannel[channel].rxlen++] = pulsewidth;
  245.  
  246.         /* Disable future measurements if we've filled the buffer. */
  247.         //TODO: Report overflow to application
  248.         if (drvIrRxChannel[channel].rxlen == MAX_NR_TIMES)
  249.         {
  250.             drvIrRxChannel[channel].rxlen = MAX_NR_TIMES-1;
  251.         }
  252.         else
  253.         {
  254.             /* Set the timeout for detection of the end of the pulse train. */
  255.             drvIrRxChannel[channel].timeout = time + IR_MAX_PULSE_WIDTH;
  256.             IR_TIMEOUT_REG = drvIrRxChannel[channel].timeout;
  257.         }
  258.     }
  259.     else if (drvIrRxChannel[channel].enable == TRUE)
  260.     {
  261.         /* The first edge of the pulse train has been detected. Enable the storage of the following pulsewidths. */
  262.         drvIrRxChannel[channel].storeEnable = TRUE;
  263.         drvIrRxChannel[channel].rxlen = 0;
  264.  
  265.         /* Enable timeout interrupt for detection of the end of the pulse train. */
  266.         IR_TIMEOUT_REG = time + IR_MAX_PULSE_WIDTH;
  267.         IR_UNMASK_TIMEOUT();
  268.     }
  269. }
  270. #endif
  271.  
  272.  
  273. /* call this function like this:
  274.     IrTransceiver_InitRxChannel(channelnumber, buffer, callback, pcint-id, GPIO_D6);
  275.     buffer is a memory location
  276.     callback is a function to call when a receive/transmitt is complete
  277.     also call with IO port to use  
  278. */
  279.  
  280. #if IR_RX_ENABLE==1
  281. void IrTransceiver_InitRxChannel(uint8_t channel, uint16_t *buffer, irRxCallback_t callback, uint8_t pcint_id, volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint)
  282. {
  283.     if (channel == 0)
  284.     {
  285.         Pcint_SetCallback(pcint_id, pcint, &IrTransceiver_Store_ch0);
  286.     }
  287.     else if (channel == 1)
  288.     {
  289.         Pcint_SetCallback(pcint_id, pcint, &IrTransceiver_Store_ch1);
  290.     }
  291.     else if (channel == 2)
  292.     {
  293.         Pcint_SetCallback(pcint_id, pcint, &IrTransceiver_Store_ch2);
  294.     }
  295.    
  296.     if (channel < 3)
  297.     {
  298.         /* Set port direction to input */
  299.         *ddr &= ~(1 << nr);
  300.        
  301.         drvIrRxChannel[channel].rxbuf = buffer;
  302.         drvIrRxChannel[channel].rxlen = 0;
  303.         drvIrRxChannel[channel].storeEnable = FALSE;
  304.         drvIrRxChannel[channel].callback = callback;
  305.         drvIrRxChannel[channel].enable = TRUE;
  306.     }
  307. }
  308.  
  309. /* call this function like this:
  310.     IrTransceiver_DeInitRxChannel(channelnumber, pcint-id, GPIO_D6);
  311. */
  312.  
  313. void IrTransceiver_DeInitRxChannel(uint8_t channel, uint8_t pcint_id, volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint)
  314. {
  315.     /* Deactivate interrupt */
  316.     Pcint_SetCallback(pcint_id, pcint, 0);
  317.    
  318.     if (channel < 3)
  319.     {
  320.         /* Deactivate channel */
  321.         drvIrRxChannel[channel].enable = FALSE;
  322.         drvIrRxChannel[channel].rxlen = 0;
  323.         drvIrRxChannel[channel].storeEnable = FALSE;
  324.         drvIrRxChannel[channel].callback = 0;
  325.     }
  326. }
  327.  
  328. uint8_t IrTransceiver_GetStoreEnableRx(uint8_t channel)
  329. {
  330.     return drvIrRxChannel[channel].storeEnable;
  331. }
  332.  
  333. void IrTransceiver_DisableRx(uint8_t channel)
  334. {
  335.     if (channel < 3)
  336.     {
  337.         /* Soft disable channel */
  338.         drvIrRxChannel[channel].enable = FALSE;
  339.     }
  340. }
  341.  
  342. void IrTransceiver_EnableRx(uint8_t channel)
  343. {
  344.     if (channel < 3)
  345.     {
  346.         /* Soft enable channel */
  347.         drvIrRxChannel[channel].enable = TRUE;
  348.     }
  349. }
  350.  
  351. void IrTransceiver_ResetRx(uint8_t channel)
  352. {
  353.     if (channel < 3)
  354.     {
  355.         /* Reset channel */
  356.         drvIrRxChannel[channel].rxlen = 0;
  357.         drvIrRxChannel[channel].storeEnable = FALSE;
  358.     }
  359. }
  360. #endif
  361.  
  362.  
  363. void IrTransceiver_Init(void)
  364. {
  365.     IR_TIMER_INIT();
  366.  
  367. #if IR_RX_ENABLE==1
  368.     for (uint8_t i = 0; i < 3; i++)
  369.     {
  370.         drvIrRxChannel[i].storeEnable = FALSE;
  371.         drvIrRxChannel[i].rxlen = 0;
  372.     }
  373. #endif
  374.    
  375. #if IR_TX_ENABLE==1
  376.     /* Set up transmitter */
  377.     #if defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  378.     TCCR0A = (0<<COM0A1)|(0<<COM0A0)|(1<<WGM01)|(0<<WGM00);
  379.     TCCR0B = (0<<WGM02)|(1<<CS00)|(0<<CS01)|(0<<CS02);
  380.    
  381.     /* Set up modulation frequency */
  382.     IR_MODULATION_REG = (((F_CPU / 2000) / drvIrTxModFreqkHz) - 1);
  383.  
  384.     /* start pwm generator */
  385.     TCCR0A |= (1<<COM0A0);
  386.     #endif
  387.  
  388. #endif
  389.  
  390. }
  391.  
  392. #if IR_TX_ENABLE==1
  393. void IrTransceiver_InitTxChannel(uint8_t channel, irTxCallback_t callback, volatile uint8_t *port, volatile uint8_t *pin, volatile uint8_t *ddr,uint8_t nr, uint8_t pcint)
  394. {
  395.     if (channel < 3)
  396.     {
  397.         drvIrTxChannel[channel].port=port;
  398.         drvIrTxChannel[channel].pinmask=(1<<nr);
  399.    
  400.         /* set up port as output */
  401.         *ddr |= (1<<nr);
  402.  
  403.         IR_OUTP_LOW();
  404.            
  405.         drvIrTxChannel[channel].callback = callback;
  406.         drvIrTxChannel[channel].enable = TRUE;
  407.         drvIrTxChannel[channel].timeoutEnable = FALSE;
  408.     }
  409. }
  410.  
  411. int IrTransceiver_Transmit(uint8_t channel, uint16_t *buffer, uint8_t start, uint8_t length, uint16_t modfreqkHz)
  412. {
  413.     if (modfreqkHz == 0) {
  414.         /* Invalid frequency. */
  415.         return -1;
  416.     }
  417.  
  418.     if (length == 0) {
  419.         /* No data. */
  420.         return -2;
  421.     }
  422.  
  423.     if (drvIrTxChannel[channel].timeoutEnable == TRUE) {
  424.         /* Channel busy. */
  425.         return -3;
  426.     }
  427.  
  428.     /* New modulation frequency. */
  429.     if (modfreqkHz != drvIrTxModFreqkHz) {
  430.         /* Check that no transmissions are ongoing. */
  431.         for (uint8_t i = 0; i < IR_SUPPORTED_NUM_CHANNELS; i++) {
  432.             /* Channel is waiting for an overflow. */
  433.             if (drvIrTxChannel[i].timeoutEnable == TRUE) {
  434.                 /* Transmission active. */
  435.                 return -4;
  436.             }
  437.         }
  438.     }
  439.  
  440.     /* Update modulation frequency. */
  441.     drvIrTxModFreqkHz = modfreqkHz;
  442.  
  443.     /* Start new transmission. */
  444.     drvIrTxChannel[channel].timeoutEnable = TRUE;
  445.     drvIrTxChannel[channel].txbuf = buffer;
  446.     drvIrTxChannel[channel].txlen = start + length;
  447.  
  448.     /* first value will be loaded directly to timer below, therefore index is set to 1 here */
  449.     drvIrTxChannel[channel].txindex = 1 + start;
  450.     drvIrTxChannel[channel].timeout = IR_COUNT_REG + drvIrTxChannel[channel].txbuf[start];
  451.  
  452.     /* go through all channels and find the one that have the next overflow */
  453.     uint8_t nextChannel = 0;
  454.     uint16_t timeRemaining = 0xffff;
  455.     uint16_t timeNow = IR_COMPARE_REG;
  456.     for (uint8_t i = 0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  457.     {
  458.         /* channel is not waiting for an overflow */
  459.         if (drvIrTxChannel[i].timeoutEnable == FALSE) continue;
  460.  
  461.         if (drvIrTxChannel[i].timeout - timeNow < timeRemaining)    // || (timeout - time > 0xffff-IR_MAX_PULSE_WIDTH)
  462.         {
  463.             timeRemaining = drvIrTxChannel[i].timeout - timeNow;
  464.             nextChannel = i;
  465.         }
  466.     }
  467.  
  468.     /* set compare value to the channel that have the next overflow */
  469.     IR_COMPARE_REG = drvIrTxChannel[nextChannel].timeout;
  470.  
  471.     /* set up modulation frequency */
  472.     IR_MODULATION_REG = (((F_CPU / 2000) / drvIrTxModFreqkHz) - 1);
  473.  
  474.     /* Start send on pin */
  475.     IR_OUTP_HIGH();
  476.  
  477.     /* enable overflow interrupt */
  478.     IR_UNMASK_COMPARE();
  479.    
  480.     return 1;
  481. }
  482. #endif
  483.  
  484.