Subversion Repositories HomeAutomation

Rev

Rev 1131 | Rev 1136 | 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. /*-----------------------------------------------------------------------------
  29.  * Interrupt Service Routines
  30.  *---------------------------------------------------------------------------*/
  31.  
  32. /*********************************************************************//**
  33. Function: ISR(PCINT0_vect)
  34. Purpose:  Executed when pin change on PCINT0.
  35. Input:    -
  36. Returns:  -
  37. **************************************************************************/
  38. ISR(PCINT0_vect) {
  39.     uint8_t p;
  40.     /* Check which pin that have had a change and call the callback.*/
  41.     //printf("I\n");
  42.     for (p = 0; p < PCINT_NUM_PCINTS; p++)
  43.     {
  44.         if (pcints[p].pcintBit < 8) {
  45.             if (pcints[p].lastStatus != ((PINB & (1 << pcints[p].pcintBit)) != 0)) {
  46.                 pcints[p].callback(p);
  47.                 pcints[p].lastStatus = ((PINB & (1 << pcints[p].pcintBit)) != 0);
  48.             }
  49.         } else if (pcints[p].pcintBit < 16) {
  50.             if (pcints[p].lastStatus != ((PINC & (1 << pcints[p].pcintBit%8)) != 0)) {
  51.                 pcints[p].callback(p);
  52.                 pcints[p].lastStatus = ((PINC & (1 << pcints[p].pcintBit%8)) != 0);
  53.             }
  54.         } else if (pcints[p].pcintBit < 24) {
  55.             if (pcints[p].lastStatus != ((PIND & (1 << pcints[p].pcintBit%8)) != 0)) {
  56.                 pcints[p].callback(p);
  57.                 pcints[p].lastStatus = ((PIND & (1 << pcints[p].pcintBit%8)) != 0);
  58.             }
  59.         }
  60.     }
  61. } /* ISR(PCINT0_vect) */
  62.  
  63.  
  64. /*********************************************************************//**
  65. Function: ISR(PCINT1_vect)
  66. Purpose:  Executed when pin change on PCINT1.
  67. Input:    -
  68. Returns:  -
  69. **************************************************************************/
  70. #if ((__GNUC__ == 4  && __GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ > 1)||(__GNUC__ == 4  && __GNUC_MINOR__ > 2)||__GNUC__ > 4)
  71. ISR(PCINT1_vect, ISR_ALIASOF(PCINT0_vect));
  72. #else
  73. ISR_ALIAS(PCINT1_vect, PCINT0_vect);
  74. #endif
  75. /*********************************************************************//**
  76. Function: ISR(PCINT0_vect)
  77. Purpose:  Executed when pin change on PCINT2.
  78. Input:    -
  79. Returns:  -
  80. **************************************************************************/
  81. #if ((__GNUC__ == 4  && __GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ > 1)||(__GNUC__ == 4  && __GNUC_MINOR__ > 2)||__GNUC__ > 4)
  82. ISR(PCINT2_vect, ISR_ALIASOF(PCINT0_vect));
  83. #else
  84. ISR_ALIAS(PCINT2_vect, PCINT0_vect);
  85. #endif
  86. /*-----------------------------------------------------------------------------
  87.  * Public Functions
  88.  *---------------------------------------------------------------------------*/
  89.  
  90. void Pcint_SetCallback(uint8_t pcint_id, uint8_t pcintBit, pcintCallback_t callback)
  91. {
  92.     //printf("Test\n");
  93.     if (pcint_id<PCINT_NUM_PCINTS)
  94.     {
  95.         uint8_t sreg = SREG;
  96.         cli();
  97.  
  98.         PCIFR=(1<<PCIF0);   /* clear any pending interrupt before enabling interrupts */
  99.         PCICR|=(1<<PCIE0);  /* enable interrupt for PCINT0 */
  100.         PCIFR=(1<<PCIF1);   /* clear any pending interrupt before enabling interrupts */
  101.         PCICR|=(1<<PCIE1);  /* enable interrupt for PCINT1 */
  102.         PCIFR=(1<<PCIF2);   /* clear any pending interrupt before enabling interrupts */
  103.         PCICR|=(1<<PCIE2);  /* enable interrupt for PCINT2 */
  104.         pcints[pcint_id].pcintBit = pcintBit;
  105.         pcints[pcint_id].lastStatus = 0;
  106.         if (callback != 0) {
  107.             if (pcints[pcint_id].pcintBit < 8) {
  108.                 pcints[pcint_id].lastStatus = ((PINB & (1 << pcints[pcint_id].pcintBit%8)) != 0);
  109.                 PCMSK0 |= (1<<pcints[pcint_id].pcintBit%8);
  110. //printf("PCMSK %u\n", PCMSK0);
  111.             } else if (pcints[pcint_id].pcintBit < 16) {
  112.                 pcints[pcint_id].lastStatus = ((PINC & (1 << pcints[pcint_id].pcintBit%8)) != 0);
  113.                 PCMSK1 |= (1<<pcints[pcint_id].pcintBit%8);
  114. //printf("PCMSK1 %u\n", PCMSK1);
  115.             } else if (pcints[pcint_id].pcintBit < 24) {
  116.                 pcints[pcint_id].lastStatus = ((PIND & (1 << pcints[pcint_id].pcintBit%8)) != 0);
  117.                 PCMSK2 |= (1<<pcints[pcint_id].pcintBit%8);
  118. //printf("PCMSK2 %u\n", PCMSK2);
  119.             }
  120.         } else { //turn off interupt
  121.             if (pcints[pcint_id].pcintBit < 8) {
  122.                 PCMSK0 &= ~(1<<(pcints[pcint_id].pcintBit%8));
  123.             } else if (pcints[pcint_id].pcintBit < 16) {
  124.                 PCMSK1 &= ~(1<<(pcints[pcint_id].pcintBit%8));
  125.             } else if (pcints[pcint_id].pcintBit < 24) {
  126.                 PCMSK2 &= ~(1<<(pcints[pcint_id].pcintBit%8));
  127.             }
  128.         }
  129.         pcints[pcint_id].callback = callback;
  130.         SREG = sreg;
  131.     }
  132. }
  133.  
  134. /*---------------------------------------------------------------------------*/
  135.  
  136.