Subversion Repositories HomeAutomation

Rev

Rev 1513 | 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 USE_PCA95XX
  25. #define USE_PCA95XX 0
  26. #endif
  27.  
  28.  
  29. #ifndef PCA_INT_VECTOR
  30. #define PCA_INT_VECTOR  INT0_vect
  31. #endif
  32.  
  33. #if defined(__AVR_ATmega8__)
  34.     #define INT1_REG GICR
  35. #elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  36.     #define INT1_REG EIMSK
  37. #else
  38. #error AVR device not supported!
  39. #endif
  40.  
  41. #if PCA_INT_VECTOR==INT0_vect
  42.     #define PCA_INT_ENABLE()    INT1_REG |= _BV(INT0)
  43.     #define PCA_INT_DISABLE()   INT1_REG &= ~(_BV(INT0))
  44. #else
  45.     #define PCA_INT_ENABLE()    INT1_REG |= _BV(INT1)
  46.     #define PCA_INT_DISABLE()   INT1_REG &= ~(_BV(INT1))
  47. #endif
  48.  
  49.  
  50. /*-----------------------------------------------------------------------------
  51.  * Includes
  52.  *---------------------------------------------------------------------------*/
  53. #include <inttypes.h>
  54. #include <stdio.h>
  55. #include <drivers/mcu/TWI_Master.h>
  56.  
  57. /*-----------------------------------------------------------------------------
  58.  * Defines
  59.  *---------------------------------------------------------------------------*/
  60. #define PCA95xx_REG_INPUT0  0
  61. #define PCA95xx_REG_INPUT1  1
  62. #define PCA95xx_REG_OUTPUT0 2
  63. #define PCA95xx_REG_OUTPUT1 3
  64. #define PCA95xx_REG_POLINV0 4
  65. #define PCA95xx_REG_POLINV1 5
  66. #define PCA95xx_REG_CONF0   6
  67. #define PCA95xx_REG_CONF1   7
  68.  
  69. #define PCA95xx_CONF_OUTPUT 0
  70. #define PCA95xx_CONF_INPUT  1
  71.  
  72. #define PCA95xx_I2C_DEV_ADDR    0x20
  73.  
  74. /*-----------------------------------------------------------------------------
  75.  * Public Types
  76.  *---------------------------------------------------------------------------*/
  77.  
  78. /**
  79.  * @brief Type of the callback function pointer
  80.  */
  81. typedef void (*pca95xxCallback_t)(uint16_t);
  82.  
  83. /*-----------------------------------------------------------------------------
  84.  * Public Function Prototypes
  85.  *---------------------------------------------------------------------------*/
  86.  
  87. /**
  88.  * @brief Initiate communication to PCA95xx.
  89.  *
  90.  * Initiate communication to PCA95xx and reads the internal registers, outputs and direction.
  91.  *
  92.  * @param address
  93.  *      The address used when communicating with the device.
  94.  */
  95. void Pca95xx_Init(uint8_t address);
  96.  
  97. /**
  98.  * @brief Stores a callback for an io interrupt.
  99.  *
  100.  * @param callback
  101.  *      Pointer to optional callback function on the form
  102.  *      void callback(uint16_t inputs). The callback function is called with the
  103.  *      port status as the argument whenever the interrupt is thrown. It is called
  104.  *      from the interrupt handler and must be very short.
  105.  */
  106. void Pca95xx_SetCallback(uint8_t index, pca95xxCallback_t callback);
  107.  
  108. /**
  109.  * @brief Poll the PCA95xx for the input status.
  110.  *
  111.  * Reads the input status of the PCA95xx and returns the data.
  112.  *
  113.  * @retval
  114.  *      The input registers.
  115.  */
  116. uint16_t Pca95xx_GetInputs(void);
  117.  
  118. /**
  119.  * @brief Sets the PCA95xx outputs.
  120.  *
  121.  * @param outputs
  122.  *      The data to be set as outputs.
  123.  * @param mask
  124.  *      Passing a mask makes it possible to set one or more output without
  125.  *      affecting the other ports. Set bit to 1 to set the corresponding
  126.  *      port bit to the value of the Output parameter bit.
  127.  */
  128. void Pca95xx_SetOutputs(uint16_t outputs, uint16_t mask);
  129.  
  130. /**
  131.  * @brief Sets the PCA95xx port direction.
  132.  *
  133.  * @param direction
  134.  *      The data to be used for setting port direction.
  135.  * @param mask
  136.  *      Passing a mask makes it possible to set direction for one or more
  137.  *      ports without affecting the others. Set bit to 1 to set the
  138.  *      corresponding direction bit to the value of the Direction parameter
  139.  *      bit.
  140.  */
  141. void Pca95xx_SetDirection(uint16_t direction, uint16_t mask);
  142.  
  143. /* GPIO.H interface */
  144.  
  145. /* Port output functions */
  146. void Pca95xx_set_pin(uint8_t nr);
  147. void Pca95xx_clr_pin(uint8_t nr);
  148. void Pca95xx_set_statement(uint8_t statement, uint8_t nr);
  149. void Pca95xx_toggle_pin(uint8_t nr);
  150. uint8_t Pca95xx_get_output_state(uint8_t nr);
  151.  
  152. /* Port input functions */
  153. uint8_t Pca95xx_get_state(uint8_t nr);
  154.  
  155. /* Direction functions */
  156. void Pca95xx_set_in(uint8_t nr);
  157. void Pca95xx_set_out(uint8_t nr);
  158. uint8_t Pca95xx_get_direction(uint8_t nr);
  159.  
  160.  
  161. /**@}*/
  162. #endif /*PCA95XX_H_*/
  163.