amiro-lld / drivers / PCAL6524 / v1 / alld_PCAL6524.h @ f0dd1ac4
History | View | Annotate | Download (16.462 KB)
1 |
/*
|
---|---|
2 |
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2020 Thomas Schöpping et al.
|
4 |
|
5 |
This program is free software: you can redistribute it and/or modify
|
6 |
it under the terms of the GNU Lesser General Public License as published by
|
7 |
the Free Software Foundation, either version 3 of the License, or
|
8 |
(at your option) any later version.
|
9 |
|
10 |
This program 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 the
|
13 |
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 program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
*/
|
18 |
|
19 |
/**
|
20 |
* @file alld_PCAL6524.h
|
21 |
* @brief GPIO extender macros and structures.
|
22 |
*
|
23 |
* @addtogroup lld_gpioext
|
24 |
* @{
|
25 |
*/
|
26 |
|
27 |
#ifndef AMIROLLD_PCAL6524_H
|
28 |
#define AMIROLLD_PCAL6524_H
|
29 |
|
30 |
#include <amiro-lld.h> |
31 |
|
32 |
/******************************************************************************/
|
33 |
/* CONSTANTS */
|
34 |
/******************************************************************************/
|
35 |
|
36 |
/**
|
37 |
* @brief Maximum I2C frequency.
|
38 |
*/
|
39 |
#define PCAL6524_LLD_I2C_MAXFREQUENCY 1000000 |
40 |
|
41 |
/**
|
42 |
* @brief A falling edge indicats an interrupt.
|
43 |
*/
|
44 |
#define PCAL6524_LLD_INT_EDGE APAL_GPIO_EDGE_FALLING
|
45 |
|
46 |
/******************************************************************************/
|
47 |
/* SETTINGS */
|
48 |
/******************************************************************************/
|
49 |
|
50 |
/******************************************************************************/
|
51 |
/* CHECKS */
|
52 |
/******************************************************************************/
|
53 |
|
54 |
/******************************************************************************/
|
55 |
/* DATA STRUCTURES AND TYPES */
|
56 |
/******************************************************************************/
|
57 |
|
58 |
/**
|
59 |
* @brief The PCAL6524Driver sruct.
|
60 |
*/
|
61 |
typedef struct { |
62 |
apalI2CDriver_t* i2cd; |
63 |
apalI2Caddr_t addr; |
64 |
} PCAL6524Driver; |
65 |
|
66 |
/**
|
67 |
* @brief Possible I2C address configurations.
|
68 |
*/
|
69 |
enum {
|
70 |
PCAL6524_LLD_I2C_ADDR_FIXED = 0x0020u, /**< Fixed part of the I2C address. */ |
71 |
PCAL6524_LLD_I2C_ADDR_SCL = 0x0020u, /**< ADDR pin connected to SCL. */ |
72 |
PCAL6524_LLD_I2C_ADDR_SDA = 0x0021u, /**< ADDR pin connected to SDA. */ |
73 |
PCAL6524_LLD_I2C_ADDR_VSS = 0x0022u, /**< ADDR pin connected to VSS. */ |
74 |
PCAL6524_LLD_I2C_ADDR_VDD = 0x0023u, /**< ADDR pin connected to VDD. */ |
75 |
PCAL6524_LLD_I2C_ADDR_DEVICEID = 0x007Cu, /**< Special address to read device ID information. */ |
76 |
}; |
77 |
|
78 |
/**
|
79 |
* @brief Command bit to enable auto-incrementation of command value.
|
80 |
* @details Can be added (ORed) to any command value.
|
81 |
*/
|
82 |
#define PCAL6524_LLD_CMD_AUTOINCREMENT 0x80u |
83 |
|
84 |
/**
|
85 |
* @brief The total number of registers that can be accessed.
|
86 |
* @note This is the maximum number of bytes that may be read or written continuously.
|
87 |
*/
|
88 |
#define PCAL6524_LLD_NUM_REGISTERS 52 |
89 |
|
90 |
typedef union { |
91 |
uint8_t raw[3];
|
92 |
struct {
|
93 |
uint16_t name : 12;
|
94 |
uint16_t part : 9;
|
95 |
uint8_t revision : 3;
|
96 |
}; |
97 |
} pcal6524_lld_deviceid_t; |
98 |
|
99 |
/**
|
100 |
* @brief Control commands for the PCAL6524.
|
101 |
*/
|
102 |
typedef enum { |
103 |
PCAL6524_LLD_CMD_INPUT_P0 = 0x00u, /**< read only */ |
104 |
PCAL6524_LLD_CMD_INPUT_P1 = 0x01u, /**< read only */ |
105 |
PCAL6524_LLD_CMD_INPUT_P2 = 0x02u, /**< read only */ |
106 |
PCAL6524_LLD_CMD_OUTPUT_P0 = 0x04u, /**< read/write */ |
107 |
PCAL6524_LLD_CMD_OUTPUT_P1 = 0x05u, /**< read/write */ |
108 |
PCAL6524_LLD_CMD_OUTPUT_P2 = 0x06u, /**< read/write */ |
109 |
PCAL6524_LLD_CMD_POLARITYINVERSION_P0 = 0x08u, /**< read/write */ |
110 |
PCAL6524_LLD_CMD_POLARITYINVERSION_P1 = 0x09u, /**< read/write */ |
111 |
PCAL6524_LLD_CMD_POLARITYINVERSION_P2 = 0x0Au, /**< read/write */ |
112 |
PCAL6524_LLD_CMD_CONFIGURATION_P0 = 0x0Cu, /**< read/write */ |
113 |
PCAL6524_LLD_CMD_CONFIGURATION_P1 = 0x0Du, /**< read/write */ |
114 |
PCAL6524_LLD_CMD_CONFIGURATION_P2 = 0x0Eu, /**< read/write */ |
115 |
PCAL6524_LLD_CMD_OUTPUTDRIVESTRENGTH_P0A = 0x40u, /**< read/write */ |
116 |
PCAL6524_LLD_CMD_OUTPUTDRIVESTRENGTH_P0B = 0x41u, /**< read/write */ |
117 |
PCAL6524_LLD_CMD_OUTPUTDRIVESTRENGTH_P1A = 0x42u, /**< read/write */ |
118 |
PCAL6524_LLD_CMD_OUTPUTDRIVESTRENGTH_P1B = 0x43u, /**< read/write */ |
119 |
PCAL6524_LLD_CMD_OUTPUTDRIVESTRENGTH_P2A = 0x44u, /**< read/write */ |
120 |
PCAL6524_LLD_CMD_OUTPUTDRIVESTRENGTH_P2B = 0x45u, /**< read/write */ |
121 |
PCAL6524_LLD_CMD_INPUTLATCH_P0 = 0x48u, /**< read/write */ |
122 |
PCAL6524_LLD_CMD_INPUTLATCH_P1 = 0x49u, /**< read/write */ |
123 |
PCAL6524_LLD_CMD_INPUTLATCH_P2 = 0x4Au, /**< read/write */ |
124 |
PCAL6524_LLD_CMD_PUPDENABLE_P0 = 0x4Cu, /**< read/write */ |
125 |
PCAL6524_LLD_CMD_PUPDENABLE_P1 = 0x4Du, /**< read/write */ |
126 |
PCAL6524_LLD_CMD_PUPDENABLE_P2 = 0x4Eu, /**< read/write */ |
127 |
PCAL6524_LLD_CMD_PUPDSELECTION_P0 = 0x50u, /**< read/write */ |
128 |
PCAL6524_LLD_CMD_PUPDSELECTION_P1 = 0x51u, /**< read/write */ |
129 |
PCAL6524_LLD_CMD_PUPDSELECTION_P2 = 0x52u, /**< read/write */ |
130 |
PCAL6524_LLD_CMD_INTERRUPTMASK_P0 = 0x54u, /**< read/write */ |
131 |
PCAL6524_LLD_CMD_INTERRUPTMASK_P1 = 0x55u, /**< read/write */ |
132 |
PCAL6524_LLD_CMD_INTERRUPTMASK_P2 = 0x56u, /**< read/write */ |
133 |
PCAL6524_LLD_CMD_INTERRUPTSTATUS_P0 = 0x58u, /**< read only */ |
134 |
PCAL6524_LLD_CMD_INTERRUPTSTATUS_P1 = 0x59u, /**< read only */ |
135 |
PCAL6524_LLD_CMD_INTERRUPTSTATUS_P2 = 0x5Au, /**< read only */ |
136 |
PCAL6524_LLD_CMD_OUTPUTCONFIGURATION = 0x5Cu, /**< read/write */ |
137 |
PCAL6524_LLD_CMD_INTERRUPTEDGE_P0A = 0x60u, /**< read/write */ |
138 |
PCAL6524_LLD_CMD_INTERRUPTEDGE_P0B = 0x61u, /**< read/write */ |
139 |
PCAL6524_LLD_CMD_INTERRUPTEDGE_P1A = 0x62u, /**< read/write */ |
140 |
PCAL6524_LLD_CMD_INTERRUPTEDGE_P1B = 0x63u, /**< read/write */ |
141 |
PCAL6524_LLD_CMD_INTERRUPTEDGE_P2A = 0x64u, /**< read/write */ |
142 |
PCAL6524_LLD_CMD_INTERRUPTEDGE_P2B = 0x65u, /**< read/write */ |
143 |
PCAL6524_LLD_CMD_INTERRUPTCLEAR_P0 = 0x68u, /**< write only */ |
144 |
PCAL6524_LLD_CMD_INTERRUPTCLEAR_P1 = 0x69u, /**< write only */ |
145 |
PCAL6524_LLD_CMD_INTERRUPTCLEAR_P2 = 0x6Au, /**< write only */ |
146 |
PCAL6524_LLD_CMD_INPUTSTATUS_P0 = 0x6Cu, /**< read only */ |
147 |
PCAL6524_LLD_CMD_INPUTSTATUS_P1 = 0x6Du, /**< read only */ |
148 |
PCAL6524_LLD_CMD_INPUTSTATUS_P2 = 0x6Eu, /**< read only */ |
149 |
PCAL6524_LLD_CMD_INDIVIDUALPINOUTPUTCONFIGURATION_P0 = 0x70u, /**< read/write */ |
150 |
PCAL6524_LLD_CMD_INDIVIDUALPINOUTPUTCONFIGURATION_P1 = 0x71u, /**< read/write */ |
151 |
PCAL6524_LLD_CMD_INDIVIDUALPINOUTPUTCONFIGURATION_P2 = 0x72u, /**< read/write */ |
152 |
PCAL6524_LLD_CMD_SWITCHDEBOUNCEENABLE_P0 = 0x74u, /**< read/write */ |
153 |
PCAL6524_LLD_CMD_SWITCHDEBOUNCEENABLE_P1 = 0x75u, /**< read/write */ |
154 |
PCAL6524_LLD_CMD_SWITCHDEBOUNCECOUNT = 0x76u, /**< read/write */ |
155 |
} pcal6524_lld_cmd_t; |
156 |
|
157 |
/**
|
158 |
* @brief Input register bit values.
|
159 |
* @details The bits in the input register reflect the incoming logic levels per pin.
|
160 |
* If a pin is configured as ouput, the bit reflects th set value or is forced to 0 in case the outpus is configured as open-drain.
|
161 |
* If a pin is configured as input with latched interrupts, reading the according port will reset the input value and clear the interrupt.
|
162 |
*/
|
163 |
typedef enum { |
164 |
PCAL6524_LLD_INPUT_LOW = 0b0,
|
165 |
PCAL6524_LLD_INPUT_HIGH = 0b1,
|
166 |
} pcal6524_lld_input_t; |
167 |
|
168 |
/**
|
169 |
* @brief Output register bit values.
|
170 |
* @details Defines the logic level to be driven by output pins.
|
171 |
* The default value (after reset) is 0b1 (high),
|
172 |
*/
|
173 |
typedef enum { |
174 |
PCAL6524_LLD_OUTPUT_LOW = 0b0,
|
175 |
PCAL6524_LLD_OUTPUT_HIGH = 0b1,
|
176 |
} pcal6524_lld_output_t; |
177 |
|
178 |
/**
|
179 |
* @brief Polarity inversion register values.
|
180 |
* @details Allows to inverse the logic values written to the input register.
|
181 |
* The default value (after reset) is 0b0 (disabled).
|
182 |
*/
|
183 |
typedef enum { |
184 |
PCAL6524_LLD_POLARITYINVERSION_DISABLED = 0b0,
|
185 |
PCAL6524_LLD_POLARITYINVERSION_ENABLED = 0b1,
|
186 |
} pcal6524_lld_polarityinversion_t; |
187 |
|
188 |
/**
|
189 |
* @brief Configuration regsiter bit values.
|
190 |
* @details Configures the direction of the I/O pins to either high-impedance input or output.
|
191 |
* The default value (after reset) is 0b1 (input).
|
192 |
*/
|
193 |
typedef enum { |
194 |
PCAL6524_LLD_CONFIGURATION_INPUT = 0b1,
|
195 |
PCAL6524_LLD_CONFIGURATION_OUTPUT = 0b0,
|
196 |
} pcal6524_lld_configuration_t; |
197 |
|
198 |
/**
|
199 |
* @brief Output drive strength register mask values.
|
200 |
* @details Configures maximum current of output pins can be defined via a 2 bit mask per pin.
|
201 |
* Toggling multiple output pins simultaneously a peak current may induce noise to supply voltage and ground.
|
202 |
* By lowering the maximum current per pin. this effect can be minimized.
|
203 |
* The default value (after reset) is 0b11 (factor 1x)
|
204 |
*/
|
205 |
typedef enum { |
206 |
PCAL6524_LL_OUTPUTDRIVESTRENGTH_0_25 = 0b00,
|
207 |
PCAL6524_LL_OUTPUTDRIVESTRENGTH_0_5 = 0b01,
|
208 |
PCAL6524_LL_OUTPUTDRIVESTRENGTH_0_75 = 0b10,
|
209 |
PCAL6524_LL_OUTPUTDRIVESTRENGTH_1 = 0b11,
|
210 |
} pcal6524_lld_outputdrivestrength_t; |
211 |
|
212 |
/**
|
213 |
* @brief Input latch register bit values.
|
214 |
* @details Allows to latch interrupt and input states per pin, if an interrupt occurred.
|
215 |
* The default value (after reset) is 0b0 (disabled).
|
216 |
*/
|
217 |
typedef enum { |
218 |
PCAL6524_LLD_INPUTLATCH_ENABLED = 0b1,
|
219 |
PCAL6524_LLD_INPUTLATCH_DISABLED = 0b0,
|
220 |
} pcal6524_lld_inputlatch_t; |
221 |
|
222 |
/**
|
223 |
* @brief Pull-up/Pull-down enable register bis values.
|
224 |
* @details Configures per pin whether the pull-up/pull-down resistors shall be enabled.
|
225 |
* If a pin is configured as open-drain output, the setting in this register are overridden and the resistors are disconnected.
|
226 |
* The default value (after reset) is 0b0 (disabled).
|
227 |
*/
|
228 |
typedef enum { |
229 |
PCAL6524_LLD_PUPD_ENABLED = 0b1,
|
230 |
PCAL6524_LLD_PUPD_DISABLED = 0b0,
|
231 |
} pcal6524_lld_pupdenable_t; |
232 |
|
233 |
/**
|
234 |
* @brief Pull-up/Pull-dpwn selection register bit values.
|
235 |
* @details Selects between pull-up and pull-down resistor (100 kΩ) per pin.
|
236 |
* Has no effect if the according bit is the pull-up/pull-down enable register is disabled.
|
237 |
* The default value (after reset) is 0b1 (pull-up).
|
238 |
*/
|
239 |
typedef enum { |
240 |
PCAL6524_LLD_PUPDSELECTION_PULLUP = 0b1,
|
241 |
PCAL6524_LLD_PUPDSELECTION_PULLDOWN = 0b0,
|
242 |
} pcal6524_lld_pupdselection_t; |
243 |
|
244 |
/**
|
245 |
* @brief Interrupt mask register bit values.
|
246 |
* @details Allows to enable (value 0) or disable (value 1) interrupts per pin.
|
247 |
* The default value (after reset) is 0b1 (interrupt disabled).
|
248 |
*/
|
249 |
typedef enum { |
250 |
PCAL6524_LLD_INTERRUPTMASK_ENABLED = 0b0,
|
251 |
PCAL6524_LLD_INTERRUPTMASK_DSIABLED = 0b1,
|
252 |
} pcal6524_lld_interruptmask_t; |
253 |
|
254 |
/**
|
255 |
* @brief Interrupt status register bit value.
|
256 |
* @details Indicates whether an interrupt occurred per pin.
|
257 |
* After reset the register is initialized with 0b0 (no interrupt occurred yet).
|
258 |
*/
|
259 |
typedef enum { |
260 |
PCAL6524_LLD_INTERRUPTSTATUS_ACTIVE = 0b1,
|
261 |
PCAL6524_LLD_INTERRUPTSTATUS_INACTIVE = 0b0,
|
262 |
} pcal6542_lld_interruptstatus_t; |
263 |
|
264 |
/**
|
265 |
* @brief Output port configuration register mask of valid bits.
|
266 |
*/
|
267 |
#define PCAL6524_LLD_OUTPUTCONFIGURATION_MASK 0x07u |
268 |
|
269 |
/**
|
270 |
* @brief Output port configuration register mask for I/O port 0.
|
271 |
*/
|
272 |
#define PCAL6524_LLD_OUTPUTCONFIGURATION_MASK_PORT0 0x01u |
273 |
|
274 |
/**
|
275 |
* @brief Output port configuration register mask for I/O port 1.
|
276 |
*/
|
277 |
#define PCAL6524_LLD_OUTPUTCONFIGURATION_MASK_PORT1 0x02u |
278 |
|
279 |
/**
|
280 |
* @brief Output port configuration register mask for I/O port 2.
|
281 |
*/
|
282 |
#define PCAL6524_LLD_OUTPUTCONFIGURATION_MASK_PORT2 0x04u |
283 |
|
284 |
/**
|
285 |
* @brief Output port configuration register bit values.
|
286 |
* @details Configures all ouput pins per port to be push-pull or open-drain.
|
287 |
* The default value (after reset) is 0b0 (push-pull).
|
288 |
*/
|
289 |
typedef enum { |
290 |
PCAL6524_LLD_OUTPUTCONFIGURATION_PUSHPULL = 0b0,
|
291 |
PCAL6524_LLD_OUTPUTCONFIGURATION_OPENDRAIN = 0b1,
|
292 |
} pcal6524_lld_outputconfiguration_t; |
293 |
|
294 |
/**
|
295 |
* @brief Interrupt edge register mask values.
|
296 |
* @details Configures the type of event that would cause an interrupt per pin.
|
297 |
* The default value (after reset) is 0b00 (level triggered).
|
298 |
*/
|
299 |
typedef enum { |
300 |
PCAL6524_LLD_INTERRUPTEDGE_LEVELTRIGGERED = 0b00,
|
301 |
PCAL6524_LLD_INTERRUPTEDGE_RISINGEDGE = 0b01,
|
302 |
PCAL6524_LLD_INTERRUPTEDGE_FALLINGEDGE = 0b10,
|
303 |
PCAL6524_LLD_INTERRUPTEDGE_ANYEDGE = 0b11,
|
304 |
} pcal6524_lld_interruptedge_t; |
305 |
|
306 |
/**
|
307 |
* @brief Input status register bit values.
|
308 |
* @details Reflects the current logic level per pin similar to the input register.
|
309 |
* However, values are not latched and reading the register will not reset interrupts.
|
310 |
*/
|
311 |
typedef enum { |
312 |
PCAL6524_LLD_INPUTSTATUS_LOW = 0b0,
|
313 |
PCAL6524_LLD_INPUTSTATUS_HIGH = 0b1,
|
314 |
} pcal6524_lld_inputstatus_t; |
315 |
|
316 |
/**
|
317 |
* @brief Individual pin output configuration register bit values.
|
318 |
* @details Can be used to invert the port-wide push-pull/open-drain configuration via the ouput port configuration register per pin.
|
319 |
* the default value (after reset) is 0b0 (not inverted).
|
320 |
*/
|
321 |
typedef enum { |
322 |
PCAL6524_LLD_INDIVIDUALPINOUTPUTCONFIGURATION_PORT = 0b0,
|
323 |
PCAL6524_LLD_INDIVIDUALPINOUTPUTCONFIGURATION_INVERTED = 0b1,
|
324 |
} pcal6524_lld_individualpinoutputconfiguration_t; |
325 |
|
326 |
/**
|
327 |
* @brief Switch debounce enable register bit values.
|
328 |
* @details Allows to enable debounce functionality for I/O ports 0 and 1.
|
329 |
* In order to use the debounce feature, an oscillator signal must be applied to pin 0 of port 0.
|
330 |
* The default value (after reset) is 0b0 (disabled).
|
331 |
*/
|
332 |
typedef enum { |
333 |
PCAL6524_LLD_SWITCHDEBOUNCE_ENABLED = 0b1,
|
334 |
PCAL6524_LLD_SWITCHDEBOUNCE_DISABLED = 0b0,
|
335 |
} pcal6524_lld_switchdebounceenable_t; |
336 |
|
337 |
/******************************************************************************/
|
338 |
/* MACROS */
|
339 |
/******************************************************************************/
|
340 |
|
341 |
/******************************************************************************/
|
342 |
/* EXTERN DECLARATIONS */
|
343 |
/******************************************************************************/
|
344 |
|
345 |
#ifdef __cplusplus
|
346 |
extern "C" { |
347 |
#endif
|
348 |
uint8_t pcal6524_lld_cmd_groupsize(const pcal6524_lld_cmd_t cmd);
|
349 |
|
350 |
// apalExitStatus_t pcal6524_lld_read_id(const PCAL6524Driver* const pcal6524d, uint8_t* const data, const apalTime_t timeout);
|
351 |
apalExitStatus_t pcal6524_lld_read_reg(const PCAL6524Driver* const pcal6524d, const pcal6524_lld_cmd_t reg, uint8_t* const data, const apalTime_t timeout); |
352 |
apalExitStatus_t pcal6524_lld_write_reg(const PCAL6524Driver* const pcal6524d, const pcal6524_lld_cmd_t reg, const uint8_t data, const apalTime_t timeout); |
353 |
apalExitStatus_t pcal6524_lld_read_group(const PCAL6524Driver* const pcal6524d, const pcal6524_lld_cmd_t reg, uint8_t* const data, const apalTime_t timeout); |
354 |
apalExitStatus_t pcal6524_lld_write_group(const PCAL6524Driver* const pcal6524d, const pcal6524_lld_cmd_t reg, const uint8_t* const data, const apalTime_t timeout); |
355 |
apalExitStatus_t pcal6524_lld_read_continuous(const PCAL6524Driver* const pcal6524d, const pcal6524_lld_cmd_t reg, uint8_t* const data, const uint8_t length, const apalTime_t timeout); |
356 |
apalExitStatus_t pcal6524_lld_write_continuous(const PCAL6524Driver* const pcal6524d, const pcal6524_lld_cmd_t reg, const uint8_t* const data, const uint8_t length, const apalTime_t timeout); |
357 |
#ifdef __cplusplus
|
358 |
} |
359 |
#endif
|
360 |
|
361 |
/******************************************************************************/
|
362 |
/* INLINE FUNCTIONS */
|
363 |
/******************************************************************************/
|
364 |
|
365 |
#endif /* AMIROLLD_PCAL6524_H */ |
366 |
|
367 |
/** @} */
|
368 |
|