Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 : /*
3 : * Copyright (C) 2021 Steven Stallion <sstallion@gmail.com>
4 : *
5 : * This library is free software; you can redistribute it and/or modify
6 : * it under the terms of the GNU Lesser General Public License as published
7 : * by the Free Software Foundation; either version 2.1 of the License, or
8 : * (at your option) any later version.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 : * the GNU Lesser General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public License
16 : * along with this library; if not, see <http://www.gnu.org/licenses/>.
17 : */
18 :
19 : #ifndef MCP23016_H
20 : #define MCP23016_H
21 :
22 : #include <stdint.h>
23 :
24 : #ifdef __cplusplus
25 : extern "C" {
26 : #endif
27 :
28 : /**
29 : * @defgroup main Main API
30 : *
31 : * @brief Main data structures and functions.
32 : *
33 : * <tt>\#include <mcp23016.h></tt>
34 : *
35 : * @{
36 : */
37 :
38 : /**
39 : * @enum mcp23016_control
40 : * @brief Enum that describes I/O control values.
41 : */
42 : enum mcp23016_control {
43 : MCP23016_CONTROL_IARES_NORMAL = 0, /**< Normal interrupt activity resolution (32ms). */
44 : MCP23016_CONTROL_IARES_FAST = 1 /**< Fast interrupt activity resolution (200us). */
45 : };
46 :
47 : /**
48 : * @struct mcp23016_device
49 : * @brief Handle to a MCP23016 device.
50 : */
51 : struct mcp23016_device;
52 :
53 : /**
54 : * @brief Open the MCP23016 device specified by @p path and @p num.
55 : *
56 : * @param path Pointer to an I2C character device.
57 : * @param num Relative position of device on I2C bus (ie. @c AD0-2).
58 : *
59 : * @return Pointer to a MCP23016 device handle, or @c NULL on error with @c
60 : * errno set appropriately.
61 : */
62 : struct mcp23016_device *mcp23016_open(const char *path, unsigned int num);
63 :
64 : /**
65 : * @brief Close a MCP23016 device handle and free associated memory.
66 : *
67 : * @param dev Pointer to a MCP23016 device handle.
68 : *
69 : * Once closed, @p dev is no longer valid for use.
70 : */
71 : void mcp23016_close(struct mcp23016_device *dev);
72 :
73 : /**
74 : * @brief Issue a software reset, which resets registers to POR defaults and
75 : * clears pending interrupts.
76 : *
77 : * @param dev Pointer to a MCP23016 device handle.
78 : *
79 : * @return 0 on success, or -1 on error with @c errno set appropriately.
80 : */
81 : int mcp23016_reset(struct mcp23016_device *dev);
82 :
83 : /**
84 : * @brief Get the port value.
85 : *
86 : * @param dev Pointer to a MCP23016 device handle.
87 : * @param val Pointer to the value to receive.
88 : *
89 : * @return 0 on success, or -1 on error with @c errno set appropriately.
90 : *
91 : * This function returns the value of the @c GP0 and @c GP1 registers in the
92 : * low and high bytes, respectively.
93 : */
94 : int mcp23016_get_port(struct mcp23016_device *dev, uint16_t *val);
95 :
96 : /**
97 : * @brief Set the port value.
98 : *
99 : * @param dev Pointer to a MCP23016 device handle.
100 : * @param val The value to set.
101 : *
102 : * @return 0 on success, or -1 on error with @c errno set appropriately.
103 : *
104 : * This function writes the value of the @c GP0 and @c GP1 registers in the
105 : * low and high bytes, respectively.
106 : */
107 : int mcp23016_set_port(struct mcp23016_device *dev, uint16_t val);
108 :
109 : /**
110 : * @brief Get the output latch value.
111 : *
112 : * @param dev Pointer to a MCP23016 device handle.
113 : * @param val Pointer to the value to receive.
114 : *
115 : * @return 0 on success, or -1 on error with @c errno set appropriately.
116 : *
117 : * This function returns the value of the @c OLAT0 and @c OLAT1 registers in
118 : * the low and high bytes, respectively.
119 : */
120 : int mcp23016_get_output(struct mcp23016_device *dev, uint16_t *val);
121 :
122 : /**
123 : * @brief Set the output latch value.
124 : *
125 : * @param dev Pointer to a MCP23016 device handle.
126 : * @param val The value to set.
127 : *
128 : * @return 0 on success, or -1 on error with @c errno set appropriately.
129 : *
130 : * This function writes the value of the @c OLAT0 and @c OLAT1 registers in
131 : * the low and high bytes, respectively.
132 : */
133 : int mcp23016_set_output(struct mcp23016_device *dev, uint16_t val);
134 :
135 : /**
136 : * @brief Get the input polarity value.
137 : *
138 : * @param dev Pointer to a MCP23016 device handle.
139 : * @param val Pointer to the value to receive.
140 : *
141 : * @return 0 on success, or -1 on error with @c errno set appropriately.
142 : *
143 : * This function returns the value of the @c IPOL0 and @c IPOL1 registers in
144 : * the low and high bytes, respectively.
145 : */
146 : int mcp23016_get_polarity(struct mcp23016_device *dev, uint16_t *val);
147 :
148 : /**
149 : * @brief Set the input polarity value.
150 : *
151 : * @param dev Pointer to a MCP23016 device handle.
152 : * @param val The value to set.
153 : *
154 : * @return 0 on success, or -1 on error with @c errno set appropriately.
155 : *
156 : * This function writes the value of the @c IPOL0 and @c IPOL1 registers in
157 : * the low and high bytes, respectively.
158 : */
159 : int mcp23016_set_polarity(struct mcp23016_device *dev, uint16_t val);
160 :
161 : /**
162 : * @brief Get the I/O direction value.
163 : *
164 : * @param dev Pointer to a MCP23016 device handle.
165 : * @param val Pointer to the value to receive.
166 : *
167 : * @return 0 on success, or -1 on error with @c errno set appropriately.
168 : *
169 : * This function returns the value of the @c IODIR0 and @c IODIR1 registers in
170 : * the low and high bytes, respectively.
171 : */
172 : int mcp23016_get_direction(struct mcp23016_device *dev, uint16_t *val);
173 :
174 : /**
175 : * @brief Set the I/O direction value.
176 : *
177 : * @param dev Pointer to a MCP23016 device handle.
178 : * @param val The value to set.
179 : *
180 : * @return 0 on success, or -1 on error with @c errno set appropriately.
181 : *
182 : * This function writes the value of the @c IODIR0 and @c IODIR1 registers in
183 : * the low and high bytes, respectively.
184 : */
185 : int mcp23016_set_direction(struct mcp23016_device *dev, uint16_t val);
186 :
187 : /**
188 : * @brief Get the interrupt capture value.
189 : *
190 : * @param dev Pointer to a MCP23016 device handle.
191 : * @param val Pointer to the value to receive.
192 : *
193 : * @return 0 on success, or -1 on error with @c errno set appropriately.
194 : *
195 : * This function returns the value of the @c INTCAP0 and @c INTCAP1 registers
196 : * in the low and high bytes, respectively.
197 : */
198 : int mcp23016_get_interrupt(struct mcp23016_device *dev, uint16_t *val);
199 :
200 : /**
201 : * @brief Clear interrupt status.
202 : *
203 : * @param dev Pointer to a MCP23016 device handle.
204 : *
205 : * @return 0 on success, or -1 on error with @c errno set appropriately.
206 : *
207 : * This function is equivalent to calling:
208 : * @code
209 : * uint16_t dummy;
210 : * mcp23016_get_interrupt(dev, &dummy);
211 : * @endcode
212 : */
213 1 : static inline int mcp23016_clear_interrupt(struct mcp23016_device *dev)
214 : {
215 : uint16_t dummy;
216 :
217 1 : return mcp23016_get_interrupt(dev, &dummy);
218 : }
219 :
220 : /**
221 : * @brief Get the I/O control value.
222 : *
223 : * @param dev Pointer to a MCP23016 device handle.
224 : * @param val Pointer to the value to receive.
225 : *
226 : * @return 0 on success, or -1 on error with @c errno set appropriately.
227 : *
228 : * This function returns the value of the @c IOCON0 and @c IOCON1 registers in
229 : * the low and high bytes, respectively.
230 : */
231 : int mcp23016_get_control(struct mcp23016_device *dev, uint16_t *val);
232 :
233 : /**
234 : * @brief Set the I/O control value.
235 : *
236 : * @param dev Pointer to a MCP23016 device handle.
237 : * @param val The value to set.
238 : *
239 : * @return 0 on success, or -1 on error with @c errno set appropriately.
240 : *
241 : * This function writes the value of the @c IOCON0 and @c IOCON1 registers in
242 : * the low and high bytes, respectively.
243 : */
244 : int mcp23016_set_control(struct mcp23016_device *dev, uint16_t val);
245 :
246 : /**
247 : * @defgroup interrupt Interrupt Output
248 : *
249 : * @brief Interrupt output functions.
250 : *
251 : * These functions manage the interrupt output functionality of the MCP23016,
252 : * which can be shared by multiple devices. Use of these functions is
253 : * considered optional and is only required if the caller wishes to be
254 : * notified of input state changes.
255 : *
256 : * @{
257 : */
258 :
259 : /**
260 : * @struct mcp23016_interrupt
261 : * @brief Handle to a MCP23016 interrupt.
262 : */
263 : struct mcp23016_interrupt;
264 :
265 : /**
266 : * @brief Open the MCP23016 interrupt specified by @p path and @p offset.
267 : *
268 : * @param path Pointer to a GPIO character device.
269 : * @param offset GPIO line offset.
270 : *
271 : * @return Pointer to a MCP23016 interrupt handle, or @c NULL on error with @c
272 : * errno set appropriately.
273 : */
274 : struct mcp23016_interrupt *mcp23016_interrupt_open(const char *path, unsigned int offset);
275 :
276 : /**
277 : * @brief Close a MCP23016 interrupt handle and free associated memory.
278 : *
279 : * @param intr Pointer to a MCP23016 interrupt handle.
280 : *
281 : * Once closed, @p interrupt is no longer valid for use.
282 : */
283 : void mcp23016_interrupt_close(struct mcp23016_interrupt *intr);
284 :
285 : /**
286 : * @brief Check interrupt output status.
287 : *
288 : * @param intr Pointer to a MCP23016 interrupt handle.
289 : *
290 : * @return Interrupt status (0 or 1) on success, or -1 on error with @c errno
291 : * set appropriately.
292 : */
293 : int mcp23016_has_interrupt(struct mcp23016_interrupt *intr);
294 :
295 : /** @} **/
296 : /** @} **/
297 :
298 : #ifdef __cplusplus
299 : }
300 : #endif
301 :
302 : #endif /* MCP23016_H */
|