Subversion Repositories HomeAutomation

Rev

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