Subversion Repositories HomeAutomation

Rev

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

  1. /**
  2.  * @defgroup pca95xx PCA95xx Library
  3.  * @code #include <drivers/io/pca95xx.h> @endcode
  4.  *
  5.  * @brief Functions for using a pca95xx I2C IO-expander.
  6.  *
  7.  * ...
  8.  *
  9.  * @author  Anders Runeson
  10.  * @date    2010-04-01
  11.  */
  12.  
  13. /**@{*/
  14.  
  15. #ifndef PCA95XX_H_
  16. #define PCA95XX_H_
  17.  
  18. #include <config.h>
  19.  
  20. #ifndef PCA95XX_NUM_CALLBACKS
  21. #define PCA95XX_NUM_CALLBACKS 0
  22. #endif
  23.  
  24. #ifndef PCA_INT_VECTOR
  25. #define PCA_INT_VECTOR  INT0_vect
  26. #endif
  27.  
  28.  
  29. #if defined(__AVR_ATmega8__)
  30.     #define INT1_REG GICR
  31. #elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  32.     #define INT1_REG EIMSK
  33. #else
  34. #error AVR device not supported!
  35. #endif
  36.  
  37. #if PCA_INT_VECTOR==INT0_vect
  38.     #define PCA_INT_ENABLE()    INT1_REG |= _BV(INT0)
  39.     #define PCA_INT_DISABLE()   INT1_REG &= ~(_BV(INT0))
  40. #else
  41.     #define PCA_INT_ENABLE()    INT1_REG |= _BV(INT1)
  42.     #define PCA_INT_DISABLE()   INT1_REG &= ~(_BV(INT1))
  43. #endif
  44.  
  45.  
  46. /*-----------------------------------------------------------------------------
  47.  * Includes
  48.  *---------------------------------------------------------------------------*/
  49. #include <inttypes.h>
  50. #include <stdio.h>
  51. #include <drivers/mcu/TWI_Master.h>
  52.  
  53. /*-----------------------------------------------------------------------------
  54.  * Defines
  55.  *---------------------------------------------------------------------------*/
  56. #define PCA95xx_REG_INPUT0  0
  57. #define PCA95xx_REG_INPUT1  1
  58. #define PCA95xx_REG_OUTPUT0 2
  59. #define PCA95xx_REG_OUTPUT1 3
  60. #define PCA95xx_REG_POLINV0 4
  61. #define PCA95xx_REG_POLINV1 5
  62. #define PCA95xx_REG_CONF0   6
  63. #define PCA95xx_REG_CONF1   7
  64.  
  65. #define PCA95xx_I2C_DEV_ADDR    0x40
  66.  
  67. /*-----------------------------------------------------------------------------
  68.  * Public Types
  69.  *---------------------------------------------------------------------------*/
  70.  
  71. /**
  72.  * @brief Type of the callback function pointer
  73.  */
  74. typedef void (*pca95xxCallback_t)(uint16_t);
  75.  
  76. /*-----------------------------------------------------------------------------
  77.  * Public Function Prototypes
  78.  *---------------------------------------------------------------------------*/
  79.  
  80. /**
  81.  * @brief Poll the PCA95xx for the input status.
  82.  *
  83.  * Reads the input status of the PCA95xx and returns the data.
  84.  *
  85.  * @param address
  86.  *      The address used when communicating with the device.
  87.  */
  88. void Pca95xx_Init(uint8_t address);
  89.  
  90. /**
  91.  * @brief Stores a callback for an io interrupt.
  92.  *
  93.  * @param callback
  94.  *      Pointer to optional callback function on the form
  95.  *      void callback(uint16_t inputs). The callback function is called with the
  96.  *      port status as the argument whenever the interrupt is thrown. It is called
  97.  *      from the interrupt handler and must be very short.
  98.  */
  99. void Pca95xx_SetCallback(pca95xxCallback_t callback);
  100.  
  101. /**
  102.  * @brief Poll the PCA95xx for the input status.
  103.  *
  104.  * Reads the input status of the PCA95xx and returns the data.
  105.  *
  106.  * @retval
  107.  *      The input registers.
  108.  */
  109. uint16_t Pca95xx_GetInputs(void);
  110.  
  111. /**
  112.  * @brief Sets the PCA95xx outputs.
  113.  *
  114.  * @param outputs
  115.  *      The data to be set as outputs.
  116.  * @param mask
  117.  *      Passing a mask makes it possible to set one or more output without
  118.  *      affecting the other ports. Set bit to 1 to set the corresponding
  119.  *      port bit to the value of the Output parameter bit.
  120.  */
  121. void Pca95xx_SetOutputs(uint16_t outputs, uint16_t mask);
  122.  
  123. /**
  124.  * @brief Sets the PCA95xx port direction.
  125.  *
  126.  * @param direction
  127.  *      The data to be used for setting port direction.
  128.  * @param mask
  129.  *      Passing a mask makes it possible to set direction for one or more
  130.  *      ports without affecting the others. Set bit to 1 to set the
  131.  *      corresponding direction bit to the value of the Direction parameter
  132.  *      bit.
  133.  */
  134. void Pca95xx_SetDirection(uint16_t direction, uint16_t mask);
  135.  
  136.  
  137. /**@}*/
  138. #endif /*PCA95XX_H_*/
  139.