Subversion Repositories HomeAutomation

Rev

Rev 1875 | 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.  
  103.  /* TRANSMIT */
  104. #if IR_TX_ENABLE==1
  105. ISR(IR_COMPARE_VECTOR)
  106. {
  107.     /* find which channel */
  108.     uint16_t time = IR_COMPARE_REG;
  109.     uint16_t diff;
  110.     uint8_t channel;
  111.  
  112.     /* go through all channels and check which of thems have overflowed */
  113.     for (channel=0; channel < IR_SUPPORTED_NUM_CHANNELS; channel++)
  114.     {
  115.         /* if channel is waiting for an overflow */
  116.         if (drvIrTxChannel[channel].timeoutEnable==TRUE)
  117.         {
  118.             /* check if channel have overflowed or is about to overflow */
  119.             diff = (time+50)-drvIrTxChannel[channel].timeout;
  120.             if (diff < IR_MAX_PULSE_WIDTH)
  121.             {
  122.                 /* check what to output next */
  123.                 if ((drvIrTxChannel[channel].txindex&1) == 1) {     /* if odd, ir-pause */
  124.                     IR_OUTP_LOW();
  125.                 } else {
  126.                     IR_OUTP_HIGH();
  127.                 }
  128.                
  129.                 /* is there more to send */
  130.                 if (drvIrTxChannel[channel].txindex < drvIrTxChannel[channel].txlen)
  131.                 {
  132.                     /* load next timeout value */
  133.                     drvIrTxChannel[channel].timeout = IR_COMPARE_REG + drvIrTxChannel[channel].txbuf[drvIrTxChannel[channel].txindex++];
  134.                 }
  135.                 else
  136.                 {
  137.                     /* disable this channel */
  138.                     drvIrTxChannel[channel].timeoutEnable = FALSE;
  139.  
  140.                     /* disable the overflow interrupt */
  141.                     IR_MASK_COMPARE();
  142.        
  143.                     /* notify the application that a pulse train has been sent. */
  144.                     drvIrTxChannel[channel].callback(channel);
  145.                 }
  146.             }
  147.         }
  148.     }
  149.    
  150.     channel = 0;
  151.     diff = 0xffff;
  152.     /* go through all channels and find the one that have the next overflow */
  153.     for (uint8_t i=0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  154.     {
  155.         /* if channel is waiting for an overflow */
  156.         if (drvIrTxChannel[i].timeoutEnable==TRUE)
  157.         {
  158.             if (drvIrTxChannel[i].timeout - time < diff)
  159.             {
  160.                 diff = drvIrTxChannel[i].timeout - time;
  161.                 channel = i;
  162.             }
  163.             /* enable overflow interrupt */
  164.             IR_UNMASK_COMPARE();
  165.         }      
  166.     }  
  167.  
  168.     /* set compare value to the channel that have the next overflow */
  169.     IR_COMPARE_REG = drvIrTxChannel[channel].timeout;
  170. }
  171. #endif
  172.  
  173.  
  174. #if IR_RX_ENABLE==1
  175. /* RECEIVE */
  176. /* When this timeout occurs a pulsetrain is complete */
  177. ISR(IR_TIMEOUT_VECTOR)
  178. {
  179. #if IR_RX_CONTINUOUS_MODE==0
  180.     for (uint8_t i=0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  181.     {
  182.         /* If more than 2 edges were recevied */
  183.         if (drvIrRxChannel[i].storeEnable == TRUE && drvIrRxChannel[i].rxlen > 2)
  184.         {
  185.             /* Notify the application that a pulse train has been received. */
  186.             drvIrRxChannel[i].callback(i, drvIrRxChannel[i].rxbuf, drvIrRxChannel[i].rxlen);
  187.         }
  188.  
  189.         drvIrRxChannel[i].storeEnable = FALSE;
  190.     }
  191.  
  192.     /* Disable ISR */
  193.     IR_MASK_TIMEOUT();
  194. #endif
  195. //  gpio_toggle_pin(EXP_J);
  196. }
  197. #endif
  198.  
  199.  
  200. /*-----------------------------------------------------------------------------
  201.  * Private Functions
  202.  *---------------------------------------------------------------------------*/
  203.  
  204. #if IR_RX_ENABLE==1
  205.  
  206. /* these wrapper functions are called from the pcint driver (callback) */
  207. void IrTransceiver_Store_ch0(uint8_t id, uint8_t status)
  208. {
  209.     IrTransceiver_Store(0);
  210. }
  211. void IrTransceiver_Store_ch1(uint8_t id, uint8_t status)
  212. {
  213.     IrTransceiver_Store(1);
  214. }
  215. void IrTransceiver_Store_ch2(uint8_t id, uint8_t status)
  216. {
  217.     IrTransceiver_Store(2);
  218. }
  219.  
  220. void IrTransceiver_Store(uint8_t channel)
  221. {
  222.     static uint16_t prev_time[3];
  223.     uint16_t pulsewidth;
  224.  
  225.     /* Read the timer counter register to get the current "time". */
  226.     uint16_t time = IR_COUNT_REG;
  227.    
  228.     /* Subtract the current measurement from the previous to get the pulse width. */
  229.     pulsewidth = time - prev_time[channel];
  230.     prev_time[channel] = time;
  231.  
  232. #if IR_MIN_STARTPULSE_WIDTH>0
  233.     if ((pulsewidth <= IR_MIN_STARTPULSE_WIDTH) && (drvIrRxChannel[channel].storeEnable == TRUE) && (drvIrRxChannel[channel].rxlen == 0))
  234.     {
  235.         IR_MASK_TIMEOUT();
  236.         return;
  237.     }
  238.     else
  239.     {
  240.         IR_UNMASK_TIMEOUT();
  241.     }
  242. #endif
  243. //gpio_toggle_pin(EXP_K);
  244.  
  245. #if IR_RX_CONTINUOUS_MODE==0
  246. /* in continuous mode when short pulse arrives received len should be set to zero */
  247. //TODO
  248.     if ((pulsewidth <= IR_MIN_PULSE_WIDTH) && (drvIrRxChannel[channel].storeEnable == TRUE))
  249.     {
  250.         drvIrRxChannel[channel].rxlen = 0;
  251.         //IR_MASK_TIMEOUT();
  252.         return;
  253.     }
  254.     else
  255.     {
  256.         //IR_UNMASK_TIMEOUT();
  257.     }
  258. #endif
  259.  
  260.  
  261.     if (drvIrRxChannel[channel].storeEnable)
  262.     {
  263.         /* Store the measurement. */
  264.         drvIrRxChannel[channel].rxbuf[drvIrRxChannel[channel].rxlen++] = pulsewidth;
  265.  
  266.         /* Disable future measurements if we've filled the buffer. */
  267.         //TODO: Report overflow to application
  268.         if (drvIrRxChannel[channel].rxlen == MAX_NR_TIMES)
  269.         {
  270.             drvIrRxChannel[channel].rxlen = MAX_NR_TIMES-1;
  271.         }
  272.         else
  273.         {
  274. #if IR_RX_CONTINUOUS_MODE==0
  275.             /* Set the timeout for detection of the end of the pulse train. */
  276.             drvIrRxChannel[channel].timeout = time + IR_MAX_PULSE_WIDTH;
  277.             IR_TIMEOUT_REG = drvIrRxChannel[channel].timeout;
  278. #else          
  279.             /* Notify the application that a pulse has been received. */
  280.             drvIrRxChannel[i].callback(i, drvIrRxChannel[i].rxbuf, drvIrRxChannel[i].rxlen);
  281. #endif
  282.  
  283.         }
  284.     }
  285.     else if (drvIrRxChannel[channel].enable == TRUE)
  286.     {
  287.         /* The first edge of the pulse train has been detected. Enable the storage of the following pulsewidths. */
  288.         drvIrRxChannel[channel].storeEnable = TRUE;
  289.         drvIrRxChannel[channel].rxlen = 0;
  290.  
  291. #if IR_RX_CONTINUOUS_MODE==0
  292.         /* Enable timeout interrupt for detection of the end of the pulse train. */
  293.         IR_TIMEOUT_REG = time + IR_MAX_PULSE_WIDTH;
  294.         IR_UNMASK_TIMEOUT();
  295. #endif
  296.     }
  297. }
  298. #endif
  299.  
  300.  
  301. /* call this function like this:
  302.     IrTransceiver_InitRxChannel(channelnumber, buffer, callback, pcint-id, GPIO_D6);
  303.     buffer is a memory location
  304.     callback is a function to call when a receive/transmitt is complete
  305.     also call with IO port to use  
  306. */
  307.  
  308. #if IR_RX_ENABLE==1
  309. 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)
  310. {
  311.     if (channel == 0)
  312.     {
  313.         Pcint_SetCallback(pcint_id, pcint, &IrTransceiver_Store_ch0);
  314.     }
  315.     else if (channel == 1)
  316.     {
  317.         Pcint_SetCallback(pcint_id, pcint, &IrTransceiver_Store_ch1);
  318.     }
  319.     else if (channel == 2)
  320.     {
  321.         Pcint_SetCallback(pcint_id, pcint, &IrTransceiver_Store_ch2);
  322.     }
  323.    
  324.     if (channel < 3)
  325.     {
  326.         /* Set port direction to input */
  327.         *ddr &= ~(1 << nr);
  328.        
  329.         drvIrRxChannel[channel].rxbuf = buffer;
  330.         drvIrRxChannel[channel].rxlen = 0;
  331.         drvIrRxChannel[channel].storeEnable = FALSE;
  332.         drvIrRxChannel[channel].callback = callback;
  333.         drvIrRxChannel[channel].enable = TRUE;
  334.     }
  335. }
  336.  
  337. /* call this function like this:
  338.     IrTransceiver_DeInitRxChannel(channelnumber, pcint-id, GPIO_D6);
  339. */
  340.  
  341. 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)
  342. {
  343.     /* Deactivate interrupt */
  344.     Pcint_SetCallback(pcint_id, pcint, 0);
  345.    
  346.     if (channel < 3)
  347.     {
  348.         /* Deactivate channel */
  349.         drvIrRxChannel[channel].enable = FALSE;
  350.         drvIrRxChannel[channel].rxlen = 0;
  351.         drvIrRxChannel[channel].storeEnable = FALSE;
  352.         drvIrRxChannel[channel].callback = 0;
  353.     }
  354. }
  355.  
  356. uint8_t IrTransceiver_GetStoreEnableRx(uint8_t channel)
  357. {
  358.     return drvIrRxChannel[channel].storeEnable;
  359. }
  360.  
  361. void IrTransceiver_DisableRx(uint8_t channel)
  362. {
  363.     if (channel < 3)
  364.     {
  365.         /* Soft disable channel */
  366.         drvIrRxChannel[channel].enable = FALSE;
  367.     }
  368. }
  369.  
  370. void IrTransceiver_EnableRx(uint8_t channel)
  371. {
  372.     if (channel < 3)
  373.     {
  374.         /* Soft enable channel */
  375.         drvIrRxChannel[channel].enable = TRUE;
  376.     }
  377. }
  378.  
  379. void IrTransceiver_ResetRx(uint8_t channel)
  380. {
  381.     if (channel < 3)
  382.     {
  383.         /* Reset channel */
  384.         drvIrRxChannel[channel].rxlen = 0;
  385.         drvIrRxChannel[channel].storeEnable = FALSE;
  386.     }
  387. }
  388. #endif
  389.  
  390.  
  391. void IrTransceiver_Init(void)
  392. {
  393.     IR_TIMER_INIT();
  394.  
  395. #if IR_RX_ENABLE==1
  396.     for (uint8_t i = 0; i < 3; i++)
  397.     {
  398.         drvIrRxChannel[i].storeEnable = FALSE;
  399.         drvIrRxChannel[i].rxlen = 0;
  400.     }
  401. #endif
  402.    
  403. #if IR_TX_ENABLE==1
  404.     /* Set up transmitter */
  405.     #if defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  406.     TCCR0A = (0<<COM0A1)|(0<<COM0A0)|(1<<WGM01)|(0<<WGM00);
  407.     TCCR0B = (0<<WGM02)|(1<<CS00)|(0<<CS01)|(0<<CS02);
  408.    
  409.     /* Set up modulation frequency */
  410.     IR_MODULATION_REG = (((F_CPU / 2000) / drvIrTxModFreqkHz) - 1);
  411.  
  412.     /* start pwm generator */
  413.     TCCR0A |= (1<<COM0A0);
  414.     #endif
  415.  
  416. #endif
  417.  
  418. }
  419.  
  420. #if IR_TX_ENABLE==1
  421. 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)
  422. {
  423.     if (channel < 3)
  424.     {
  425.         drvIrTxChannel[channel].port=port;
  426.         drvIrTxChannel[channel].pinmask=(1<<nr);
  427.    
  428.         /* set up port as output */
  429.         *ddr |= (1<<nr);
  430.  
  431.         IR_OUTP_LOW();
  432.            
  433.         drvIrTxChannel[channel].callback = callback;
  434.         drvIrTxChannel[channel].enable = TRUE;
  435.         drvIrTxChannel[channel].timeoutEnable = FALSE;
  436.     }
  437. }
  438.  
  439. int IrTransceiver_Transmit(uint8_t channel, uint16_t *buffer, uint8_t start, uint8_t length, uint16_t modfreqkHz)
  440. {
  441.     if (modfreqkHz == 0) {
  442.         /* Invalid frequency. */
  443.         return -1;
  444.     }
  445.  
  446.     if (length == 0) {
  447.         /* No data. */
  448.         return -2;
  449.     }
  450.  
  451.     if (drvIrTxChannel[channel].timeoutEnable == TRUE) {
  452.         /* Channel busy. */
  453.         return -3;
  454.     }
  455.  
  456.     /* New modulation frequency. */
  457.     if (modfreqkHz != drvIrTxModFreqkHz) {
  458.         /* Check that no transmissions are ongoing. */
  459.         for (uint8_t i = 0; i < IR_SUPPORTED_NUM_CHANNELS; i++) {
  460.             /* Channel is waiting for an overflow. */
  461.             if (drvIrTxChannel[i].timeoutEnable == TRUE) {
  462.                 /* Transmission active. */
  463.                 return -4;
  464.             }
  465.         }
  466.     }
  467.  
  468.     /* Update modulation frequency. */
  469.     drvIrTxModFreqkHz = modfreqkHz;
  470.  
  471.     /* Start new transmission. */
  472.     drvIrTxChannel[channel].timeoutEnable = TRUE;
  473.     drvIrTxChannel[channel].txbuf = buffer;
  474.     drvIrTxChannel[channel].txlen = start + length;
  475.  
  476.     /* first value will be loaded directly to timer below, therefore index is set to 1 here */
  477.     drvIrTxChannel[channel].txindex = 1 + start;
  478.     drvIrTxChannel[channel].timeout = IR_COUNT_REG + drvIrTxChannel[channel].txbuf[start];
  479.  
  480.     /* go through all channels and find the one that have the next overflow */
  481.     uint8_t nextChannel = 0;
  482.     uint16_t timeRemaining = 0xffff;
  483.     uint16_t timeNow = IR_COMPARE_REG;
  484.     for (uint8_t i = 0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
  485.     {
  486.         /* channel is not waiting for an overflow */
  487.         if (drvIrTxChannel[i].timeoutEnable == FALSE) continue;
  488.  
  489.         if (drvIrTxChannel[i].timeout - timeNow < timeRemaining)    // || (timeout - time > 0xffff-IR_MAX_PULSE_WIDTH)
  490.         {
  491.             timeRemaining = drvIrTxChannel[i].timeout - timeNow;
  492.             nextChannel = i;
  493.         }
  494.     }
  495.  
  496.     /* set compare value to the channel that have the next overflow */
  497.     IR_COMPARE_REG = drvIrTxChannel[nextChannel].timeout;
  498.  
  499.     /* set up modulation frequency */
  500.     IR_MODULATION_REG = (((F_CPU / 2000) / drvIrTxModFreqkHz) - 1);
  501.  
  502.     /* Start send on pin */
  503.     IR_OUTP_HIGH();
  504.  
  505.     /* enable overflow interrupt */
  506.     IR_UNMASK_COMPARE();
  507.    
  508.     return 1;
  509. }
  510. #endif
  511.  
  512.