Subversion Repositories HomeAutomation

Rev

Rev 1500 | Rev 1516 | 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_CONF_OUTPUT 0
  66. #define PCA95xx_CONF_INPUT  1
  67.  
  68. #define PCA95xx_I2C_DEV_ADDR    0x20
  69.  
  70. /*-----------------------------------------------------------------------------
  71.  * Public Types
  72.  *---------------------------------------------------------------------------*/
  73.  
  74. /**
  75.  * @brief Type of the callback function pointer
  76.  */
  77. typedef void (*pca95xxCallback_t)(uint16_t);
  78.  
  79. /*-----------------------------------------------------------------------------
  80.  * Public Function Prototypes
  81.  *---------------------------------------------------------------------------*/
  82.  
  83. /**
  84.  * @brief Initiate communication to PCA95xx.
  85.  *
  86.  * Initiate communication to PCA95xx and reads the internal registers, outputs and direction.
  87.  *
  88.  * @param address
  89.  *      The address used when communicating with the device.
  90.  */
  91. void Pca95xx_Init(uint8_t address);
  92.  
  93. /**
  94.  * @brief Stores a callback for an io interrupt.
  95.  *
  96.  * @param callback
  97.  *      Pointer to optional callback function on the form
  98.  *      void callback(uint16_t inputs). The callback function is called with the
  99.  *      port status as the argument whenever the interrupt is thrown. It is called
  100.  *      from the interrupt handler and must be very short.
  101.  */
  102. void Pca95xx_SetCallback(pca95xxCallback_t callback);
  103.  
  104. /**
  105.  * @brief Poll the PCA95xx for the input status.
  106.  *
  107.  * Reads the input status of the PCA95xx and returns the data.
  108.  *
  109.  * @retval
  110.  *      The input registers.
  111.  */
  112. uint16_t Pca95xx_GetInputs(void);
  113.  
  114. /**
  115.  * @brief Sets the PCA95xx outputs.
  116.  *
  117.  * @param outputs
  118.  *      The data to be set as outputs.
  119.  * @param mask
  120.  *      Passing a mask makes it possible to set one or more output without
  121.  *      affecting the other ports. Set bit to 1 to set the corresponding
  122.  *      port bit to the value of the Output parameter bit.
  123.  */
  124. void Pca95xx_SetOutputs(uint16_t outputs, uint16_t mask);
  125.  
  126. /**
  127.  * @brief Sets the PCA95xx port direction.
  128.  *
  129.  * @param direction
  130.  *      The data to be used for setting port direction.
  131.  * @param mask
  132.  *      Passing a mask makes it possible to set direction for one or more
  133.  *      ports without affecting the others. Set bit to 1 to set the
  134.  *      corresponding direction bit to the value of the Direction parameter
  135.  *      bit.
  136.  */
  137. void Pca95xx_SetDirection(uint16_t direction, uint16_t mask);
  138.  
  139. /* GPIO.H interface */
  140.  
  141. /* Port output functions */
  142. void Pca95xx_set_pin(uint8_t nr);
  143. void Pca95xx_clr_pin(uint8_t nr);
  144. void Pca95xx_set_statement(uint8_t statement, uint8_t nr);
  145. void Pca95xx_toggle_pin(uint8_t nr);
  146. uint8_t Pca95xx_get_output_state(uint8_t nr);
  147.  
  148. /* Port input functions */
  149. uint8_t Pca95xx_get_state(uint8_t nr);
  150.  
  151. /* Direction functions */
  152. void Pca95xx_set_in(uint8_t nr);
  153. void Pca95xx_set_out(uint8_t nr);
  154. uint8_t Pca95xx_get_direction(uint8_t nr);
  155.  
  156.  
  157. /**@}*/
  158. #endif /*PCA95XX_H_*/
  159.