Subversion Repositories HomeAutomation

Rev

Rev 1483 | Rev 1488 | 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/mcu/gpio.h>
  16. #include <drivers/ir/transceiver/irtransceiverMulti.h>
  17. #include <drivers/ir/protocols.h>
  18. #include <avr/io.h>
  19. #include <avr/interrupt.h>
  20.  
  21. /*-----------------------------------------------------------------------------
  22.  * Prerequisites
  23.  *---------------------------------------------------------------------------*/
  24. //TODO: Use this define, might fix the bug
  25. #ifndef IR_RX_ACTIVE_LOW
  26. #define IR_RX_ACTIVE_LOW    1
  27. #endif
  28.  
  29. #ifndef IR_TX_ACTIVE_LOW
  30. #define IR_TX_ACTIVE_LOW    0
  31. #endif
  32.  
  33. #ifndef IR_TX_ENABLE
  34. #define IR_TX_ENABLE    0
  35. #endif
  36.  
  37. #ifndef IR_RX_ENABLE
  38. #define IR_RX_ENABLE    0
  39. #endif
  40.  
  41.  
  42. #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__)
  43. #define TIMSK1  TIMSK
  44. #define TIFR1   TIFR
  45. #define ICIE1   TICIE1
  46. #endif
  47.  
  48. #define IR_COMPARE_VECTOR       TIMER1_COMPB_vect
  49. #define IR_TIMEOUT_VECTOR       TIMER1_COMPA_vect
  50. #define IR_CAPTURE_VECTOR       TIMER1_CAPT_vect
  51. #define IR_TIMEOUT_REG          OCR1A
  52. #define IR_COMPARE_REG          OCR1B
  53. #define IR_CAPTURE_REG          ICR1
  54. #define IR_COUNT_REG            TCNT1
  55. #define IR_MODULATION_REG       OCR0A
  56. /*#define IR_TIMER_INIT()           TCCR1A = 0; \
  57.                                 TCCR1B = (1<<ICNC1)|(2<<CS10);
  58.                                 */
  59. #define IR_TIMER_INIT()         TCCR1A = 0; \
  60.                                 TCCR1B = (2<<CS10);
  61. #define IR_MASK_COMPARE()       TIMSK1 &= ~(1<<OCIE1B);
  62. #define IR_UNMASK_COMPARE()     TIFR1 = (1<<OCF1B); TIMSK1 |= (1<<OCIE1B);
  63. #define IR_MASK_TIMEOUT()       TIMSK1 &= ~(1<<OCIE1A);
  64. #define IR_UNMASK_TIMEOUT()     TIFR1 = (1<<OCF1A); TIMSK1 |= (1<<OCIE1A);
  65. #define IR_MASK_CAPTURE()       TIMSK1 &= ~(1<<ICIE1);
  66. #define IR_UNMASK_CAPTURE()     TIFR1 = (1<<ICF1); TIMSK1 |= (1<<ICIE1);
  67. #define IR_CAPTURE_FALLING()    TCCR1B &= ~(1<<ICES1); \
  68.                                 TIFR1 = (1<<ICF1);
  69. #define IR_CAPTURE_RISING()     TCCR1B |= (1<<ICES1); \
  70.                                 TIFR1 = (1<<ICF1);
  71.  
  72. //#define IR_OUTP_HIGH()            TCCR0A |= (1<<COM0A0);
  73. //#define IR_OUTP_LOW()         TCCR0A &= ~(1<<COM0A0);
  74. #if IR_TX_ACTIVE_LOW==1
  75. #define IR_OUTP_HIGH()  *drvIrTxChannel[channel].port &= ~drvIrTxChannel[channel].pinmask
  76. #define IR_OUTP_LOW()   *drvIrTxChannel[channel].port |= drvIrTxChannel[channel].pinmask
  77. #else
  78. #define IR_OUTP_HIGH()  *drvIrTxChannel[channel].port |= drvIrTxChannel[channel].pinmask
  79. #define IR_OUTP_LOW()   *drvIrTxChannel[channel].port &= ~drvIrTxChannel[channel].pinmask
  80. #endif
  81.  
  82.  
  83. /*-----------------------------------------------------------------------------
  84.  * Globals
  85.  *---------------------------------------------------------------------------*/
  86. #if IR_RX_ENABLE==1
  87. struct {
  88.     uint8_t     enable;
  89.     uint16_t    timeout;
  90.     uint8_t     timeoutEnable;
  91.     uint16_t    *rxbuf;
  92.     uint8_t     rxlen;
  93.     uint8_t     storeEnable;
  94.     irRxCallback_t callback;
  95. } drvIrRxChannel[3];
  96. #endif
  97.  
  98. #if IR_TX_ENABLE==1
  99. struct {
  100.     uint8_t     enable;
  101.     uint16_t    timeout;
  102.     uint8_t     timeoutEnable;
  103.     uint16_t    *txbuf;
  104.     uint8_t     txlen;
  105.     uint8_t     txindex;
  106.     irTxCallback_t callback;
  107.     volatile uint8_t    *port;
  108.     uint8_t     pinmask;
  109. } drvIrTxChannel[3];
  110. #endif
  111.  
  112.  
  113. /*-----------------------------------------------------------------------------
  114.  * Interrupt Handlers
  115.  *---------------------------------------------------------------------------*/
  116.  
  117.  
  118. /*
  119. timer 1 has two comprare registers and compare ISRs, one is used below for "timeout" on receive and the other is used here for transmitter
  120. //TODO: how to handle more than one transmitter on one compare interrupt, spara en array med tider och nån enableflagga för varje kanal
  121. */
  122.  
  123. #if IR_TX_ENABLE==1
  124. ISR(IR_COMPARE_VECTOR)
  125. {
  126.  
  127.     /* TODO find which channel */
  128.     uint8_t channel = 0;
  129.    
  130.    
  131.     if ((drvIrTxChannel[channel].txindex&1) == 1) {     /* if odd, ir-pause */
  132.         IR_OUTP_LOW();
  133.     } else {
  134.         IR_OUTP_HIGH();
  135.     }
  136.    
  137.     if (drvIrTxChannel[channel].txindex < drvIrTxChannel[channel].txlen)
  138.     {
  139.         drvIrTxChannel[channel].timeout = IR_COMPARE_REG + drvIrTxChannel[channel].txbuf[drvIrTxChannel[channel].txindex++];
  140.     }
  141.     else
  142.     {
  143.         //TODO only mask if noone is sending (checked below)
  144.         IR_MASK_COMPARE();
  145.  
  146.         /* Notify the application that a pulse train has been sent. */
  147.         drvIrTxChannel[channel].callback(channel);
  148.     }
  149.  
  150.     /* TODO: find channel with lowest time left */
  151.     IR_COMPARE_REG = drvIrTxChannel[channel].timeout;
  152. }
  153. #endif
  154.  
  155. #if IR_RX_ENABLE==1
  156. //TODO: hur hantera detta i multi receiver
  157. //TODO: ha en array med tider och nån enableflagga för varje kanal
  158. //TODO: mot slutet av ISRen laddas nästa tid in...
  159.  
  160. /* When this timeout occurs a pulsetrain is complete */
  161. ISR(IR_TIMEOUT_VECTOR)
  162. {
  163.     /* Find which channel timed out */
  164.     uint16_t timeDiff = 0xffff;
  165.     uint8_t channel = 0;
  166. /*  for (uint8_t i=0; i < 3; i++)
  167.     {
  168.         if ((drvIrRxChannel[channel].timeoutEnable==TRUE) && (IR_TIMEOUT_REG-drvIrRxChannel[i].timeout < timeDiff))
  169.         {
  170.             timeDiff = IR_TIMEOUT_REG-drvIrRxChannel[i].timeout;
  171.             channel = i;
  172.         }
  173.     }
  174. */
  175.     /* If more than 3 flanks was recevied */
  176.     if (drvIrRxChannel[channel].rxlen > 3)
  177.     {
  178.         /* Notify the application that a pulse train has been received. */
  179.         drvIrRxChannel[channel].callback(channel, drvIrRxChannel[channel].rxbuf, drvIrRxChannel[channel].rxlen);
  180.     }
  181.  
  182.     drvIrRxChannel[channel].storeEnable = FALSE;
  183.     drvIrRxChannel[channel].timeoutEnable = FALSE;
  184.  
  185.     IR_MASK_TIMEOUT();
  186.    
  187. //TODO: reset the timeout for next enabled channel
  188. /*
  189.     uint16_t nextTimeout = drvIrRxChannel[channel].timeout;
  190.     for (uint8_t i=0; i < 3; i++)
  191.     {
  192. //TODO: hitta den med timeout härnäst (svårt med tanke på att den tiden kan ha passerat eftersom vi är under interrupt)
  193. //kanske inte är så noga, i värsta fall tar det 65ms extra
  194.         if ((drvIrRxChannel[channel].timeoutEnable) && (drvIrRxChannel[i].timeout < nextTimeout))
  195.         {
  196.             nextTimeout = drvIrRxChannel[i].timeout;
  197.         }
  198.     }
  199.     IR_UNMASK_TIMEOUT();
  200.     IR_TIMEOUT_REG = nextTimeout;
  201. */
  202. }
  203. #endif
  204.  
  205.  
  206. /*-----------------------------------------------------------------------------
  207.  * Private Functions
  208.  *---------------------------------------------------------------------------*/
  209.  
  210. #if IR_RX_ENABLE==1
  211.  
  212. /* these wrapper functions are called from the pcint driver (callback) */
  213. void IrTransceiver_Store_ch0(uint8_t id, uint8_t status)
  214. {
  215.     IrTransceiver_Store(0);
  216. }
  217. void IrTransceiver_Store_ch1(uint8_t id, uint8_t status)
  218. {
  219.     IrTransceiver_Store(1);
  220. }
  221. void IrTransceiver_Store_ch2(uint8_t id, uint8_t status)
  222. {
  223.     IrTransceiver_Store(2);
  224. }
  225.  
  226. void IrTransceiver_Store(uint8_t channel)
  227. {
  228.     static uint16_t prev_time[3];
  229.     uint16_t pulsewidth;
  230.  
  231.     /* Read the timer counter register to get the current "time". */
  232.     uint16_t time = IR_COUNT_REG;
  233.    
  234.     /* Subtract the current measurement from the previous to get the pulse width. */
  235.     pulsewidth = time - prev_time[channel];
  236.     prev_time[channel] = time;
  237.  
  238.     if (drvIrRxChannel[channel].storeEnable)
  239.     {
  240.         /* Store the measurement. */
  241.         drvIrRxChannel[channel].rxbuf[drvIrRxChannel[channel].rxlen++] = pulsewidth;
  242.  
  243.         /* Disable future measurements if we've filled the buffer. */
  244.         //TODO: What to do in multi recevier?
  245.         //TODO: Report overflow to application
  246.         if (drvIrRxChannel[channel].rxlen == MAX_NR_TIMES)
  247.         {
  248.             drvIrRxChannel[channel].rxlen = MAX_NR_TIMES-1;
  249.         }
  250.     }
  251.     else if (drvIrRxChannel[channel].enable == TRUE)
  252.     {
  253.         /* The first edge of the pulse train has been detected. Enable the storage of the following pulsewidths. */
  254.         drvIrRxChannel[channel].storeEnable = TRUE;
  255.         drvIrRxChannel[channel].rxlen = 0;
  256.         drvIrRxChannel[channel].timeoutEnable = TRUE;
  257.  
  258.         /* Set the timeout for detection of the end of the pulse train. */
  259.         drvIrRxChannel[channel].timeout = time + IR_MAX_PULSE_WIDTH;
  260.         uint16_t nextTimeout = drvIrRxChannel[channel].timeout;
  261.         for (uint8_t i=0; i < 3; i++)
  262.         {
  263.     //TODO: hitta den med timeout härnäst (svårt med tanke på att den tiden kan ha passerat eftersom vi är under interrupt)
  264.     //kanske inte är så noga, i värsta fall tar det 65ms extra
  265.             if ((drvIrRxChannel[channel].timeoutEnable) && (drvIrRxChannel[i].timeout < nextTimeout))
  266.             {
  267.                 nextTimeout = drvIrRxChannel[i].timeout;
  268.             }
  269.         }
  270.         IR_TIMEOUT_REG = nextTimeout;
  271.  
  272.         /* Enable timeout interrupt for detection of the end of the pulse train. */
  273.         IR_UNMASK_TIMEOUT();
  274.  
  275.         //TODO: if buffer resource coordinator is implemented this is where a buffer should be assinged to this channel
  276.     }
  277. }
  278. #endif
  279.  
  280.  
  281. /* call this function like this:
  282.     IrTransceiver_Init_RX_Channel(channelnumber, buffer, callback, pcint-id, GPIO_D6);
  283.     buffer is a memory location
  284.     callback is a function to call when a receive/transmitt is complete
  285.     also call with IO port to use  
  286. */
  287.  
  288. #if IR_RX_ENABLE==1
  289. 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)
  290. {
  291.     if (channel == 0)
  292.     {
  293.         Pcint_SetCallback(pcint_id, pcint, &IrTransceiver_Store_ch0);
  294.     }
  295.     else if (channel == 1)
  296.     {
  297.         Pcint_SetCallback(pcint_id, pcint, &IrTransceiver_Store_ch1);
  298.     }
  299.     else if (channel == 2)
  300.     {
  301.         Pcint_SetCallback(pcint_id, pcint, &IrTransceiver_Store_ch2);
  302.     }
  303.    
  304.     if (channel < 3)
  305.     {
  306.         /* Set port direction to input */
  307.         *ddr &= ~(1 << nr);
  308.        
  309.         drvIrRxChannel[channel].rxbuf = buffer;
  310.         drvIrRxChannel[channel].rxlen = 0;
  311.         drvIrRxChannel[channel].storeEnable = FALSE;
  312.         drvIrRxChannel[channel].timeoutEnable = FALSE;
  313.         drvIrRxChannel[channel].callback = callback;
  314.         drvIrRxChannel[channel].enable = TRUE;
  315.     }
  316. }
  317.  
  318. /* call this function like this:
  319.     IrTransceiver_DeInit_RX_Channel(channelnumber, pcint-id, GPIO_D6);
  320. */
  321.  
  322. 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)
  323. {
  324.     /* Deactivate interrupt */
  325.     Pcint_SetCallback(pcint_id, pcint, 0);
  326.    
  327.     if (channel < 3)
  328.     {
  329.         /* Deactivate channel */
  330.         drvIrRxChannel[channel].enable = FALSE;
  331.         drvIrRxChannel[channel].rxlen = 0;
  332.         drvIrRxChannel[channel].storeEnable = FALSE;
  333.         drvIrRxChannel[channel].timeoutEnable = FALSE;
  334.         drvIrRxChannel[channel].callback = 0;
  335.     }
  336. }
  337.  
  338. uint8_t IrTransceiver_GetStoreEnableRx(uint8_t channel)
  339. {
  340.     return drvIrRxChannel[channel].storeEnable;
  341. }
  342.  
  343. void IrTransceiver_DisableRx(uint8_t channel)
  344. {
  345.     if (channel < 3)
  346.     {
  347.         /* Soft disable channel */
  348.         drvIrRxChannel[channel].enable = FALSE;
  349.     }
  350. }
  351.  
  352. void IrTransceiver_EnableRx(uint8_t channel)
  353. {
  354.     if (channel < 3)
  355.     {
  356.         /* Soft enable channel */
  357.         drvIrRxChannel[channel].enable = TRUE;
  358.     }
  359. }
  360.  
  361. void IrTransceiver_ResetRx(uint8_t channel)
  362. {
  363.     if (channel < 3)
  364.     {
  365.         /* Reset channel */
  366.         drvIrRxChannel[channel].rxlen = 0;
  367.         drvIrRxChannel[channel].storeEnable = FALSE;
  368.         drvIrRxChannel[channel].timeoutEnable = FALSE;
  369.     }
  370. }
  371. #endif
  372.  
  373.  
  374. void IrTransceiver_Init(void)
  375. {
  376.     IR_TIMER_INIT();
  377.  
  378. #if IR_RX_ENABLE==1
  379.     for (uint8_t i = 0; i < 3; i++)
  380.     {
  381.         drvIrRxChannel[i].storeEnable = FALSE;
  382.         drvIrRxChannel[i].timeoutEnable = FALSE;
  383.         drvIrRxChannel[i].rxlen = 0;
  384.     }
  385. #endif
  386.    
  387. #if IR_TX_ENABLE==1
  388.     /* Set up transmitter */
  389.     #if defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  390.     TCCR0A = (0<<COM0A1)|(0<<COM0A0)|(1<<WGM01)|(0<<WGM00);
  391.     TCCR0B = (0<<WGM02)|(1<<CS00)|(0<<CS01)|(0<<CS02);
  392.    
  393.     /* set up modulation frequency to 38kHz */
  394.     IR_MODULATION_REG = (((F_CPU/2000)/38) -1);
  395.  
  396.     /* start pwm generator */
  397.     TCCR0A |= (1<<COM0A0);
  398.     #endif
  399.  
  400. #endif
  401.  
  402. }
  403.  
  404. #if IR_TX_ENABLE==1
  405. 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)
  406. {
  407.     if (channel < 3)
  408.     {
  409.         drvIrTxChannel[channel].port=port;
  410.         drvIrTxChannel[channel].pinmask=(1<<*pin);
  411.    
  412.         /* set up port as output */
  413.         *ddr |= (1<<*pin);
  414.  
  415.         IR_OUTP_LOW();
  416.            
  417.         drvIrTxChannel[channel].callback = callback;
  418.         drvIrTxChannel[channel].enable = TRUE;
  419.         drvIrTxChannel[channel].timeoutEnable = FALSE;
  420.     }
  421. }
  422.  
  423. void IrTransceiver_Transmit(uint8_t channel, uint16_t *buffer, uint8_t length)
  424. {
  425.     if (length > 0)
  426.     {
  427.         if (drvIrTxChannel[channel].timeoutEnable == FALSE)
  428.         {
  429.             drvIrTxChannel[channel].timeoutEnable = TRUE;
  430.             drvIrTxChannel[channel].txbuf = buffer;
  431.             drvIrTxChannel[channel].txlen = length;
  432.             /* first value will be loaded directly to timer below */
  433.             drvIrTxChannel[channel].txindex = 1;
  434.            
  435.             drvIrTxChannel[channel].timeout = IR_COUNT_REG + drvIrTxChannel[channel].txbuf[0];
  436.             /* TODO: only modify timer reg if this channel has the lowest time left */
  437.             IR_COMPARE_REG = drvIrTxChannel[channel].timeout;
  438.  
  439.             IR_OUTP_HIGH();
  440.  
  441.             IR_UNMASK_COMPARE();
  442.         }
  443.     }
  444. }
  445. #endif
  446.  
  447.