Subversion Repositories HomeAutomation

Rev

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

  1. /**
  2.  * ...
  3.  *
  4.  * @author  Linus Lundin
  5.  *
  6.  * @date    2009-03-01
  7.  */
  8.  
  9. /*-----------------------------------------------------------------------------
  10.  * Includes
  11.  *---------------------------------------------------------------------------*/
  12. #include <inttypes.h>
  13. #include <avr/io.h>
  14. #include <avr/interrupt.h>
  15.  
  16. #include <drivers/mcu/pcint.h>
  17.  
  18. /*-----------------------------------------------------------------------------
  19.  * Globals
  20.  *---------------------------------------------------------------------------*/
  21.  
  22. struct {
  23.     uint8_t pcintBit;
  24.     //uint8_t lastStatus;
  25.     pcintCallback_t callback;
  26. } pcints[PCINT_NUM_PCINTS];
  27.  
  28. uint32_t pinLastStatus;
  29.  
  30. /*-----------------------------------------------------------------------------
  31.  * Interrupt Service Routines
  32.  *---------------------------------------------------------------------------*/
  33.  
  34. /*********************************************************************//**
  35. Function: ISR(PCINT0_vect)
  36. Purpose:  Executed when pin change on PCINT0.
  37. Input:    -
  38. Returns:  -
  39. **************************************************************************/
  40. ISR(PCINT0_vect) {
  41.     uint8_t p;
  42.  
  43.     uint32_t pin = ((uint32_t)PIND<<16)|((uint32_t)PINC<<8)|(PINB);
  44.     /* Check which pin that have had a change and call the callback.*/
  45.     //printf("I\n");
  46.     for (p = 0; p < PCINT_NUM_PCINTS; p++)
  47.     {
  48.         if (pinLastStatus != ((pin & (1 << pcints[p].pcintBit)) != 0)) {
  49.                 pcints[p].callback(p);
  50.         }
  51.        
  52.         /*if (pcints[p].pcintBit < 8) {
  53.             if (pcints[p].lastStatus != ((PINB & (1 << pcints[p].pcintBit)) != 0)) {
  54.                 pcints[p].lastStatus = ((PINB & (1 << pcints[p].pcintBit)) != 0);
  55.                 pcints[p].callback(p);
  56.             }
  57.         } else if (pcints[p].pcintBit < 16) {
  58.             if (pcints[p].lastStatus != ((PINC & (1 << pcints[p].pcintBit%8)) != 0)) {
  59.                 pcints[p].lastStatus = ((PINC & (1 << pcints[p].pcintBit%8)) != 0);
  60.                 pcints[p].callback(p);
  61.             }
  62.         } else if (pcints[p].pcintBit < 24) {
  63.             if (pcints[p].lastStatus != ((PIND & (1 << pcints[p].pcintBit%8)) != 0)) {
  64.                 pcints[p].lastStatus = ((PIND & (1 << pcints[p].pcintBit%8)) != 0);
  65.                 pcints[p].callback(p);
  66.             }
  67.         }*/
  68.     }
  69.    
  70.     pinLastStatus = pin;
  71. } /* ISR(PCINT0_vect) */
  72.  
  73.  
  74. /*********************************************************************//**
  75. Function: ISR(PCINT1_vect)
  76. Purpose:  Executed when pin change on PCINT1.
  77. Input:    -
  78. Returns:  -
  79. **************************************************************************/
  80. #if ((__GNUC__ == 4  && __GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ > 1)||(__GNUC__ == 4  && __GNUC_MINOR__ > 2)||__GNUC__ > 4)
  81. ISR(PCINT1_vect, ISR_ALIASOF(PCINT0_vect));
  82. #else
  83. ISR_ALIAS(PCINT1_vect, PCINT0_vect);
  84. #endif
  85. /*********************************************************************//**
  86. Function: ISR(PCINT0_vect)
  87. Purpose:  Executed when pin change on PCINT2.
  88. Input:    -
  89. Returns:  -
  90. **************************************************************************/
  91. #if ((__GNUC__ == 4  && __GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ > 1)||(__GNUC__ == 4  && __GNUC_MINOR__ > 2)||__GNUC__ > 4)
  92. ISR(PCINT2_vect, ISR_ALIASOF(PCINT0_vect));
  93. #else
  94. ISR_ALIAS(PCINT2_vect, PCINT0_vect);
  95. #endif
  96. /*-----------------------------------------------------------------------------
  97.  * Public Functions
  98.  *---------------------------------------------------------------------------*/
  99.  
  100. void Pcint_SetCallback(uint8_t pcint_id, uint8_t pcintBit, pcintCallback_t callback)
  101. {
  102.     //printf("Test\n");
  103.     if (pcint_id<PCINT_NUM_PCINTS)
  104.     {
  105.         uint8_t sreg = SREG;
  106.         cli();
  107.  
  108.         PCIFR=(1<<PCIF0|1<<PCIF1|1<<PCIF2); /* clear any pending interrupt before enabling interrupts */
  109.         PCICR|=(1<<PCIE0|1<<PCIE1|1<<PCIE2);    /* enable interrupt for PCINT0-2 */
  110. #if 0
  111.         PCIFR=(1<<PCIF1);   /* clear any pending interrupt before enabling interrupts */
  112.         PCICR|=(1<<PCIE1);  /* enable interrupt for PCINT1 */
  113.         PCIFR=(1<<PCIF2);   /* clear any pending interrupt before enabling interrupts */
  114.         PCICR|=(1<<PCIE2);  /* enable interrupt for PCINT2 */
  115. #endif
  116.         pcints[pcint_id].pcintBit = pcintBit;
  117.         //pcints[pcint_id].lastStatus = 0;
  118.         if (callback != 0) {
  119.             if (pcints[pcint_id].pcintBit < 8) {
  120.                 //pcints[pcint_id].lastStatus = ((PINB & (1 << pcints[pcint_id].pcintBit%8)) != 0);
  121.                 PCMSK0 |= (1<<pcints[pcint_id].pcintBit%8);
  122. //printf("PCMSK %u\n", PCMSK0);
  123.             } else if (pcints[pcint_id].pcintBit < 16) {
  124.                 //pcints[pcint_id].lastStatus = ((PINC & (1 << pcints[pcint_id].pcintBit%8)) != 0);
  125.                 PCMSK1 |= (1<<pcints[pcint_id].pcintBit%8);
  126. //printf("PCMSK1 %u\n", PCMSK1);
  127.             } else if (pcints[pcint_id].pcintBit < 24) {
  128.                 //pcints[pcint_id].lastStatus = ((PIND & (1 << pcints[pcint_id].pcintBit%8)) != 0);
  129.                 PCMSK2 |= (1<<pcints[pcint_id].pcintBit%8);
  130. //printf("PCMSK2 %u\n", PCMSK2);
  131.             }
  132.         } else { //turn off interupt
  133.             if (pcints[pcint_id].pcintBit < 8) {
  134.                 PCMSK0 &= ~(1<<(pcints[pcint_id].pcintBit%8));
  135.             } else if (pcints[pcint_id].pcintBit < 16) {
  136.                 PCMSK1 &= ~(1<<(pcints[pcint_id].pcintBit%8));
  137.             } else if (pcints[pcint_id].pcintBit < 24) {
  138.                 PCMSK2 &= ~(1<<(pcints[pcint_id].pcintBit%8));
  139.             }
  140.         }
  141.  
  142.         uint32_t pinLastStatus = ((uint32_t)PIND<<16)|((uint32_t)PINC<<8)|(PINB);
  143.  
  144.         pcints[pcint_id].callback = callback;
  145.         SREG = sreg;
  146.     }
  147. }
  148.  
  149. /*---------------------------------------------------------------------------*/
  150.  
  151.