pca9555.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /********************************************************************************************
  2. * drivers/ioexpander/pca9555.h
  3. *
  4. * Copyright (C) 2015 Gregory Nutt. All rights reserved.
  5. * Author: Sebastien Lorquet <sebastien@lorquet.fr>
  6. *
  7. * References:
  8. * "16-bit I2C-bus and SMBus I/O port with interrupt product datasheet",
  9. * Rev. 08 - 22 October 2009, NXP
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. * 3. Neither the name NuttX nor the names of its contributors may be
  22. * used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ********************************************************************************************/
  39. #ifndef __DRIVERS_IOEXPANDER_PCA9555_H
  40. #define __DRIVERS_IOEXPANDER_PCA9555_H
  41. /********************************************************************************************
  42. * Included Files
  43. ********************************************************************************************/
  44. #include <nuttx/config.h>
  45. #include <semaphore.h>
  46. #include <nuttx/wdog.h>
  47. #include <nuttx/clock.h>
  48. #include <nuttx/wqueue.h>
  49. #include <nuttx/ioexpander/ioexpander.h>
  50. #include <nuttx/ioexpander/pca9555.h>
  51. #include <nuttx/i2c/i2c_master.h>
  52. #include <nuttx/irq.h>
  53. #if defined(CONFIG_IOEXPANDER) && defined(CONFIG_IOEXPANDER_PCA9555)
  54. /********************************************************************************************
  55. * Pre-processor Definitions
  56. ********************************************************************************************/
  57. /* Configuration ****************************************************************************/
  58. /* Prerequisites:
  59. * CONFIG_I2C
  60. * I2C support is required
  61. * CONFIG_IOEXPANDER
  62. * Enables support for the PCA9555 I/O expander
  63. *
  64. * CONFIG_IOEXPANDER_PCA9555
  65. * Enables support for the PCA9555 driver (Needs CONFIG_INPUT)
  66. * CONFIG_PCA9555_MULTIPLE
  67. * Can be defined to support multiple PCA9555 devices on board.
  68. * CONFIG_PCA9555_INT_NCALLBACKS
  69. * Maximum number of supported pin interrupt callbacks.
  70. */
  71. #ifdef CONFIG_IOEXPANDER_INT_ENABLE
  72. # ifndef CONFIG_PCA9555_INT_NCALLBACKS
  73. # define CONFIG_PCA9555_INT_NCALLBACKS 4
  74. # endif
  75. #endif
  76. #ifdef CONFIG_IOEXPANDER_INT_ENABLE
  77. # ifndef CONFIG_SCHED_WORKQUEUE
  78. # error Work queue support required. CONFIG_SCHED_WORKQUEUE must be selected.
  79. # endif
  80. #endif
  81. #undef CONFIG_PCA9555_REFCNT
  82. /* Driver support ***************************************************************************/
  83. /* This format is used to construct the /dev/input[n] device driver path. It defined here
  84. * so that it will be used consistently in all places.
  85. */
  86. /* PCA9555 Resources ************************************************************************/
  87. #define PCA9555_GPIO_NPINS 16 /* All pins can be used as GPIOs */
  88. #ifndef CONFIG_I2C
  89. #error "CONFIG_I2C is required by PCA9555"
  90. #endif
  91. #define PCA9555_MAXDEVS 8
  92. /* I2C frequency */
  93. #define PCA9555_I2C_MAXFREQUENCY 400000 /* 400KHz */
  94. /* PCA9555 Registers ************************************************************************/
  95. /* Register Addresses */
  96. #define PCA9555_REG_INPUT 0x00
  97. #define PCA9555_REG_OUTPUT 0x02
  98. #define PCA9555_REG_POLINV 0x04
  99. #define PCA9555_REG_CONFIG 0x06
  100. /********************************************************************************************
  101. * Public Types
  102. ********************************************************************************************/
  103. #ifdef CONFIG_IOEXPANDER_INT_ENABLE
  104. /* This type represents on registered pin interrupt callback */
  105. struct pca9555_callback_s
  106. {
  107. ioe_pinset_t pinset; /* Set of pin interrupts that will generate
  108. * the callback. */
  109. ioe_callback_t cbfunc; /* The saved callback function pointer */
  110. FAR void *cbarg; /* Callback argument */
  111. };
  112. #endif
  113. /* This structure represents the state of the PCA9555 driver */
  114. struct pca9555_dev_s
  115. {
  116. struct ioexpander_dev_s dev; /* Nested structure to allow casting as public gpio
  117. * expander. */
  118. #ifdef CONFIG_PCA9555_SHADOW_MODE
  119. uint8_t sreg[8]; /* Shadowed registers of the PCA9555 */
  120. #endif
  121. #ifdef CONFIG_PCA9555_MULTIPLE
  122. FAR struct pca9555_dev_s *flink; /* Supports a singly linked list of drivers */
  123. #endif
  124. FAR struct pca9555_config_s *config; /* Board configuration data */
  125. FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */
  126. sem_t exclsem; /* Mutual exclusion */
  127. #ifdef CONFIG_IOEXPANDER_INT_ENABLE
  128. struct work_s work; /* Supports the interrupt handling "bottom half" */
  129. /* Saved callback information for each I/O expander client */
  130. struct pca9555_callback_s cb[CONFIG_PCA9555_INT_NCALLBACKS];
  131. #endif
  132. };
  133. #endif /* CONFIG_IOEXPANDER && CONFIG_IOEXPANDER_PCA9555 */
  134. #endif /* __DRIVERS_IOEXPANDER_PCA9555_H */