Subversion Repositories HomeAutomation

Rev

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

  1. /**
  2.  * PCA95xx device driver. Provides possibility for several independent users of the
  3.  * device.
  4.  *
  5.  * @author  Anders Runeson
  6.  *
  7.  * @date    2010-04-02
  8.  */
  9.  
  10. /*-----------------------------------------------------------------------------
  11.  * Includes
  12.  *---------------------------------------------------------------------------*/
  13. #include <inttypes.h>
  14. #include <avr/io.h>
  15. #include <avr/interrupt.h>
  16.  
  17. #include <drivers/io/PCA95xx.h>
  18.  
  19.  
  20. /*-----------------------------------------------------------------------------
  21.  * Globals
  22.  *---------------------------------------------------------------------------*/
  23. volatile uint8_t Pca95xx_address;
  24. volatile uint16_t Pca95xx_direction;
  25. volatile uint16_t Pca95xx_outputs;
  26.  
  27. #if PCA95XX_NUM_CALLBACKS > 0
  28. volatile pca95xxCallback_t[PCA95XX_NUM_CALLBACKS] Pca95xx_callback;
  29. #endif
  30.  
  31.  
  32. /*-----------------------------------------------------------------------------
  33.  * Interrupt Service Routines
  34.  *---------------------------------------------------------------------------*/
  35. #if PCA95XX_NUM_CALLBACKS > 0
  36. ISR(PCA_INT_VECTOR)
  37. {
  38.     uint8_t t;
  39.     /* Read input registers from device */
  40.     //TODO: communication with device
  41.     uint16_t inputs = ;
  42.    
  43.     /* Call all callbacks with the input as argument */
  44.     for (t = 0; t < PCA95XX_NUM_CALLBACKS; t++)
  45.     {
  46.         Pca95xx_callback[t](inputs);
  47.     }
  48. }
  49. #endif
  50.  
  51. /*-----------------------------------------------------------------------------
  52.  * Public Functions
  53.  *---------------------------------------------------------------------------*/
  54.  
  55. void Pca95xx_Init(uint8_t address)
  56. {
  57.     /* Disable intterupts while tampering with the global vars */
  58.     uint8_t sreg = SREG;
  59.     cli();
  60.  
  61.     /* Store address */
  62.     Pca95xx_address = address;
  63.    
  64.     /* Set up interrupt */
  65.    
  66.     /* Set up ports? */
  67.    
  68.     /* Read direction registers from device to global var */
  69.     //TODO: communication with device
  70.     //Pca95xx_direction = ;
  71.     /* Read output registers from device to global var */
  72.     //TODO: communication with device
  73.     //Pca95xx_outputs = ;
  74.    
  75.     /* Enable interrupt */
  76.     PCA_INT_ENABLE();
  77.     sei();
  78. }
  79.  
  80. /*---------------------------------------------------------------------------*/
  81.  
  82. #if PCA95XX_NUM_CALLBACKS > 0
  83. void Pca95xx_SetCallback(uint8_t index, pca95xxCallback_t callback)
  84. {
  85.     /* Disable intterupts while tampering with the global vars */
  86.     uint8_t sreg = SREG;
  87.     cli();
  88.  
  89.     if (index < PCA95XX_NUM_CALLBACKS)
  90.     {
  91.         /* Store the callback */
  92.         Pca95xx_callback[index] = callback;
  93.     }
  94.  
  95.     sei();
  96. }
  97. #endif
  98.  
  99. /*---------------------------------------------------------------------------*/
  100.  
  101. uint16_t Pca95xx_GetInputs(void)
  102. {
  103.     /* Disable device interupts while communicating with the device */
  104.     PCA_INT_DISABLE();
  105.  
  106.     /* Read input registers from device */
  107.     //TODO: communication with device
  108.     uint16_t inputs = ;
  109.    
  110.     PCA_INT_ENABLE();
  111.     return inputs;
  112. }
  113.  
  114. /*---------------------------------------------------------------------------*/
  115.  
  116. void Pca95xx_SetOutputs(uint16_t outputs, uint16_t mask)
  117. {
  118.     /* Disable intterupts while tampering with the global vars */
  119.     uint8_t sreg = SREG;
  120.     cli();
  121.    
  122.     /* Set masked ones in outputs to global var */
  123.     Pca95xx_outputs |= (outputs&mask);
  124.     /* Clear masked zeros in outputs to global var */
  125.     Pca95xx_outputs &= (outputs|(~mask));
  126.     /* Set output registers */
  127.     //TODO: communication with device
  128.     sei();
  129. }
  130.  
  131. /*---------------------------------------------------------------------------*/
  132.  
  133. void Pca95xx_SetDirection(uint16_t direction, uint16_t mask)
  134. {
  135.     /* Disable intterupts while tampering with the global vars */
  136.     uint8_t sreg = SREG;
  137.     cli();
  138.  
  139.     /* Set masked ones in direction to global var */
  140.     Pca95xx_direction |= (direction&mask);
  141.     /* Clear masked zeros in direction to global var */
  142.     Pca95xx_direction &= (direction|(~mask));
  143.     /* Set direction registers */
  144.     //TODO: communication with device
  145.     sei();
  146. }
  147.