Subversion Repositories HomeAutomation

Rev

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