Loading...
Searching...
No Matches
mcp23x17.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021 Gunar Schorcht
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
341
342#ifdef __cplusplus
343extern "C" {
344#endif
345
346#include "modules.h"
347#include "periph/gpio.h"
348#include "periph/i2c.h"
349#include "periph/spi.h"
350
351#if IS_USED(MODULE_SAUL_GPIO) || DOXYGEN
352#include "saul/periph.h"
353#endif /* MODULE_SAUL_GPIO */
354
355#if !IS_USED(MODULE_MCP23X17_I2C) && !IS_USED(MODULE_MCP23X17_SPI)
356#error "Please provide the MCP23x17 variants used by the application."
357#error "At least one variant has to be specified (mcp23017 and/or mcp23s17)."
358#endif
359
360#if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
361
362#include "event.h"
363
364#endif /* MODULE_MCP23X17_IRQ */
365
377#define MCP23X17_BASE_ADDR (0x20)
378
382#define MCP23X17_GPIO_PIN_NUM (16)
383
395#define MCP23X17_GPIO_PIN(port, pin) ((gpio_t)((port << 3) | pin))
396
411
415typedef enum {
416#if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN
418#endif
419#if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN
421#endif
423
427#if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN
428typedef struct {
431#endif
432
436#if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN
437typedef struct {
438 spi_t dev;
440 gpio_t cs;
442#endif
443
447typedef struct {
449 union {
450#if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN
452#endif
453#if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN
455#endif
456 };
458
484
485#if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
502typedef struct {
504 void *dev;
506
507#endif /* MODULE_MCP23X17_IRQ */
508
512typedef struct {
514
515 uint16_t od_pins;
516
517#if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
520
522#endif /* MODULE_MCP23X17_IRQ */
523
524} mcp23x17_t;
525
526#if IS_USED(MODULE_SAUL_GPIO) || DOXYGEN
541#endif
542
556
568int mcp23x17_gpio_init(mcp23x17_t *dev, gpio_t pin, gpio_mode_t mode);
569
570#if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
598 gpio_mode_t mode,
599 gpio_flank_t flank,
600 gpio_cb_t isr,
601 void *arg);
602#endif /* MODULE_MCP23X17_IRQ || DOXYGEN */
603
615int mcp23x17_gpio_read(mcp23x17_t *dev, gpio_t pin);
616
624void mcp23x17_gpio_write(mcp23x17_t *dev, gpio_t pin, int value);
625
632void mcp23x17_gpio_clear(mcp23x17_t *dev, gpio_t pin);
633
640void mcp23x17_gpio_set(mcp23x17_t *dev, gpio_t pin);
641
648void mcp23x17_gpio_toggle(mcp23x17_t *dev, gpio_t pin);
649
650#if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
664
678
679#endif /* MODULE_MCP23X17_IRQ || DOXYGEN */
680
681#ifdef __cplusplus
682}
683#endif
684
gpio_flank_t
Definition periph_cpu.h:176
spi_clk_t
Definition periph_cpu.h:348
Low-level GPIO peripheral driver interface definitions.
gpio_mode_t
Available pin modes.
Definition periph_cpu.h:96
int mcp23x17_gpio_init_int(mcp23x17_t *dev, gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t isr, void *arg)
Initialize a MCP23x17 pin for external interrupt usage.
void mcp23x17_gpio_toggle(mcp23x17_t *dev, gpio_t pin)
Toggle the value of the MCP23x17 output pin.
#define MCP23X17_GPIO_PIN_NUM
MCP23x17 has 16 I/O pins.
Definition mcp23x17.h:382
void mcp23x17_gpio_set(mcp23x17_t *dev, gpio_t pin)
Set the MCP23x17 output pin.
void mcp23x17_gpio_irq_disable(mcp23x17_t *dev, gpio_t pin)
Disable pin interrupt.
void mcp23x17_gpio_irq_enable(mcp23x17_t *dev, gpio_t pin)
Enable pin interrupt.
mcp23x17_if_t
MCP23x17 interface types.
Definition mcp23x17.h:415
int mcp23x17_init(mcp23x17_t *dev, const mcp23x17_params_t *params)
Initialize the MCP23x17 I/O expander.
int mcp23x17_gpio_init(mcp23x17_t *dev, gpio_t pin, gpio_mode_t mode)
Initialize a MCP23x17 pin.
void mcp23x17_gpio_clear(mcp23x17_t *dev, gpio_t pin)
Clear the MCP23x17 output pin.
mcp23x17_error_codes_t
Named MCP23x17 driver error codes.
Definition mcp23x17.h:400
void mcp23x17_gpio_write(mcp23x17_t *dev, gpio_t pin, int value)
Write the value to MCP23x17 input pin.
int mcp23x17_gpio_read(mcp23x17_t *dev, gpio_t pin)
Get the value from MCP23x17 input pin.
@ MCP23X17_SPI
SPI interface used.
Definition mcp23x17.h:420
@ MCP23X17_I2C
I2C interface used.
Definition mcp23x17.h:417
@ MCP23X17_ERROR_RESET_PIN
RESET pin error
Definition mcp23x17.h:409
@ MCP23X17_ERROR_INV_FLANK
invalid interrupt flank
Definition mcp23x17.h:406
@ MCP23X17_ERROR_SPI
SPI communication error.
Definition mcp23x17.h:403
@ MCP23X17_ERROR_NO_DEV
no MCP23x17 I/O expander device
Definition mcp23x17.h:404
@ MCP23X17_ERROR_I2C
I2C communication error.
Definition mcp23x17.h:402
@ MCP23X17_ERROR_INV_MODE
invalid pin mode
Definition mcp23x17.h:405
@ MCP23X17_ERROR_GPIO
GPIO pin error.
Definition mcp23x17.h:407
@ MCP23X17_OK
success
Definition mcp23x17.h:401
@ MCP23X17_ERROR_INT_PIN
INTA/INTB pin error
Definition mcp23x17.h:408
void(* gpio_cb_t)(void *arg)
Signature of event callback functions triggered from interrupts.
Definition gpio.h:143
uint_fast8_t i2c_t
Default i2c_t type definition.
Definition i2c.h:155
struct event event_t
event structure forward declaration
Definition event.h:135
Low-level I2C peripheral driver interface definition.
Common macros and compiler attributes/pragmas configuration.
Parameter definitions for mapping peripherals directly to SAUL.
Low-level SPI peripheral driver interface definition.
Default interrupt context for GPIO pins.
Definition gpio.h:149
MCP23017 I2C parameters.
Definition mcp23x17.h:428
i2c_t dev
I2C device used.
Definition mcp23x17.h:429
MCP23x17 Hardware interface parameters union.
Definition mcp23x17.h:447
mcp23x17_if_t type
I2C/SPI interface type selector.
Definition mcp23x17.h:448
mcp23x17_spi_params_t spi
SPI specific interface parameters.
Definition mcp23x17.h:454
mcp23x17_i2c_params_t i2c
I2C specific interface parameters.
Definition mcp23x17.h:451
IRQ event type.
Definition mcp23x17.h:502
void * dev
MCP23x17 device reference.
Definition mcp23x17.h:504
event_t event
Super event data structure.
Definition mcp23x17.h:503
Struct containing the peripheral configuration.
Definition mcp23x17.h:462
uint8_t addr
MCP2317 device address.
Definition mcp23x17.h:477
mcp23x17_if_params_t if_params
specific I2C/SPI interface parameters
Definition mcp23x17.h:482
gpio_t reset_pin
GPIO pin used for RESET signal.
Definition mcp23x17.h:481
gpio_t int_pin
GPIO pin used for combined INTA/INTB signal.
Definition mcp23x17.h:478
MCP23x17 configuration structure for mapping expander pins to SAUL.
Definition mcp23x17.h:537
uint8_t dev
MCP23x17 device index.
Definition mcp23x17.h:538
saul_gpio_params_t gpio
GPIO configuration for mapping to SAUL.
Definition mcp23x17.h:539
MCP23S17 SPI parameters.
Definition mcp23x17.h:437
spi_t dev
SPI device used.
Definition mcp23x17.h:438
spi_clk_t clk
SPI clock speed.
Definition mcp23x17.h:439
gpio_t cs
SPI chip Select pin.
Definition mcp23x17.h:440
Device descriptor for MCP23x17 I/O expanders.
Definition mcp23x17.h:512
mcp23x17_irq_event_t irq_event
IRQ event object used for the device.
Definition mcp23x17.h:521
uint16_t od_pins
Pins defined as GPIO_OD or GPIO_OD_PU.
Definition mcp23x17.h:515
gpio_isr_ctx_t isr[MCP23X17_GPIO_PIN_NUM]
ISR with arg for each expander pin.
Definition mcp23x17.h:518
mcp23x17_params_t params
Device initialization parameters.
Definition mcp23x17.h:513
gpio_flank_t flank[MCP23X17_GPIO_PIN_NUM]
interrupt flank for each expander pin
Definition mcp23x17.h:519
Direct mapped GPIO configuration values.
Definition periph.h:47