pcf8574.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /********************************************************************************************
  2. * drivers/ioexpander/pcf8574.h
  3. *
  4. * Copyright (C) 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ********************************************************************************************/
  35. #ifndef __DRIVERS_IOEXPANDER_PCF8574_H
  36. #define __DRIVERS_IOEXPANDER_PCF8574_H
  37. /********************************************************************************************
  38. * Included Files
  39. ********************************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <semaphore.h>
  42. #include <nuttx/wdog.h>
  43. #include <nuttx/clock.h>
  44. #include <nuttx/wqueue.h>
  45. #include <nuttx/ioexpander/ioexpander.h>
  46. #include <nuttx/ioexpander/pcf8574.h>
  47. #include <nuttx/i2c/i2c_master.h>
  48. #include <nuttx/irq.h>
  49. #if defined(CONFIG_IOEXPANDER) && defined(CONFIG_IOEXPANDER_PCF8574)
  50. /********************************************************************************************
  51. * Pre-processor Definitions
  52. ********************************************************************************************/
  53. /* Configuration ****************************************************************************/
  54. /* Prerequisites:
  55. * CONFIG_I2C
  56. * I2C support is required
  57. * CONFIG_IOEXPANDER
  58. * Enables I/O expander support
  59. *
  60. * CONFIG_IOEXPANDER_PCF8574
  61. * Enables support for the PCF8574 driver (Needs CONFIG_INPUT)
  62. * CONFIG_PCF8574_MULTIPLE
  63. * Can be defined to support multiple PCF8574 devices on board.
  64. * CONFIG_PCF8574_INT_NCALLBACKS
  65. * Maximum number of supported pin interrupt callbacks.
  66. * CONFIG_PCF8574_INT_POLL
  67. * Enables a poll for missed interrupts
  68. * CONFIG_PCF8574_INT_POLLDELAY
  69. * If CONFIG_PCF8574_INT_POLL=y, then this is the delay in microseconds
  70. * between polls for missed interrupts.
  71. */
  72. #ifndef CONFIG_I2C
  73. # error "CONFIG_I2C is required by PCF8574"
  74. #endif
  75. #ifdef CONFIG_IOEXPANDER_INT_ENABLE
  76. # ifndef CONFIG_PCF8574_INT_NCALLBACKS
  77. # define CONFIG_PCF8574_INT_NCALLBACKS 4
  78. # endif
  79. #endif
  80. #ifdef CONFIG_IOEXPANDER_INT_ENABLE
  81. # ifndef CONFIG_SCHED_WORKQUEUE
  82. # error Work queue support required. CONFIG_SCHED_WORKQUEUE must be selected.
  83. # endif
  84. #endif
  85. #ifndef CONFIG_PCF8574_INT_POLLDELAY
  86. # define CONFIG_PCF8574_INT_POLLDELAY 500000
  87. #endif
  88. /* PCF8574 Definitions **********************************************************************/
  89. #define PCF8574_I2C_MAXFREQUENCY 400000 /* 400KHz */
  90. #define PCF8574_POLLDELAY (CONFIG_PCF8574_INT_POLLDELAY / USEC_PER_TICK)
  91. #define PCF8574_LEVEL_SENSITIVE(d,p) \
  92. (((d)->trigger & ((ioe_pinset_t)1 << (p))) == 0)
  93. #define PCF8574_LEVEL_HIGH(d,p) \
  94. (((d)->level[0] & ((ioe_pinset_t)1 << (p))) != 0)
  95. #define PCF8574_LEVEL_LOW(d,p) \
  96. (((d)->level[1] & ((ioe_pinset_t)1 << (p))) != 0)
  97. #define PCF8574_EDGE_SENSITIVE(d,p) \
  98. (((d)->trigger & ((ioe_pinset_t)1 << (p))) != 0)
  99. #define PCF8574_EDGE_RISING(d,p) \
  100. (((d)->level[0] & ((ioe_pinset_t)1 << (p))) != 0)
  101. #define PCF8574_EDGE_FALLING(d,p) \
  102. (((d)->level[1] & ((ioe_pinset_t)1 << (p))) != 0)
  103. #define PCF8574_EDGE_BOTH(d,p) \
  104. (PCF8574_LEVEL_RISING(d,p) && PCF8574_LEVEL_FALLING(d,p))
  105. /********************************************************************************************
  106. * Public Types
  107. ********************************************************************************************/
  108. #ifdef CONFIG_IOEXPANDER_INT_ENABLE
  109. /* This type represents on registered pin interrupt callback */
  110. struct pcf8574_callback_s
  111. {
  112. ioe_pinset_t pinset; /* Set of pin interrupts that will generate
  113. * the callback. */
  114. ioe_callback_t cbfunc; /* The saved callback function pointer */
  115. FAR void *cbarg; /* Callback argument */
  116. };
  117. #endif
  118. /* This structure represents the state of the PCF8574 driver */
  119. struct pcf8574_dev_s
  120. {
  121. struct ioexpander_dev_s dev; /* Nested structure to allow casting as public gpio
  122. * expander. */
  123. FAR struct pcf8574_config_s *config; /* Board configuration data */
  124. FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */
  125. sem_t exclsem; /* Mutual exclusion */
  126. uint8_t inpins; /* Set of input pins */
  127. uint8_t outstate; /* State of all output pins */
  128. #ifdef CONFIG_IOEXPANDER_INT_ENABLE
  129. #ifdef CONFIG_PCF8574_INT_POLL
  130. WDOG_ID wdog; /* Timer used to poll for missed interrupts */
  131. #endif
  132. uint8_t input; /* Last input registeres */
  133. uint8_t intstat; /* Pending interrupts */
  134. uint8_t trigger; /* Bit encoded: 0=level 1=edge */
  135. uint8_t level[2]; /* Bit encoded: 01=high/rising, 10 low/falling, 11 both */
  136. struct work_s work; /* Supports the interrupt handling "bottom half" */
  137. /* Saved callback information for each I/O expander client */
  138. struct pcf8574_callback_s cb[CONFIG_PCF8574_INT_NCALLBACKS];
  139. #endif
  140. };
  141. #endif /* CONFIG_IOEXPANDER && CONFIG_IOEXPANDER_PCF8574 */
  142. #endif /* __DRIVERS_IOEXPANDER_PCF8574_H */