mcp23x17.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Gunar Schorcht
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
345 #ifdef __cplusplus
346 extern "C" {
347 #endif
348 
349 #include "modules.h"
350 #include "periph/gpio.h"
351 #include "periph/i2c.h"
352 #include "periph/spi.h"
353 
354 #if IS_USED(MODULE_SAUL_GPIO) || DOXYGEN
355 #include "saul/periph.h"
356 #endif /* MODULE_SAUL_GPIO */
357 
358 #if !IS_USED(MODULE_MCP23X17_I2C) && !IS_USED(MODULE_MCP23X17_SPI)
359 #error "Please provide the MCP23x17 variants used by the application."
360 #error "At least one variant has to be specified (mcp23017 and/or mcp23s17)."
361 #endif
362 
363 #if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
364 
365 #include "event.h"
366 
367 #endif /* MODULE_MCP23X17_IRQ */
368 
380 #define MCP23X17_BASE_ADDR (0x20)
381 
385 #define MCP23X17_GPIO_PIN_NUM (16)
386 
398 #define MCP23X17_GPIO_PIN(port, pin) ((gpio_t)((port << 3) | pin))
399 
403 typedef enum {
414 
418 typedef enum {
419 #if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN
421 #endif
422 #if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN
424 #endif
425 } mcp23x17_if_t;
426 
430 #if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN
431 typedef struct {
434 #endif
435 
439 #if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN
440 typedef struct {
441  spi_t dev;
443  gpio_t cs;
445 #endif
446 
450 typedef struct {
452  union {
453 #if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN
455 #endif
456 #if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN
458 #endif
459  };
461 
465 typedef struct {
480  uint8_t addr;
481  gpio_t int_pin;
484  gpio_t reset_pin;
487 
488 #if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
505 typedef struct {
507  void *dev;
509 
510 #endif /* MODULE_MCP23X17_IRQ */
511 
515 typedef struct {
518  uint16_t od_pins;
520 #if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
525 #endif /* MODULE_MCP23X17_IRQ */
526 
527 } mcp23x17_t;
528 
529 #if IS_USED(MODULE_SAUL_GPIO) || DOXYGEN
540 typedef struct {
541  uint8_t dev;
544 #endif
545 
558 int mcp23x17_init(mcp23x17_t *dev, const mcp23x17_params_t *params);
559 
571 int mcp23x17_gpio_init(mcp23x17_t *dev, gpio_t pin, gpio_mode_t mode);
572 
573 #if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
600 int mcp23x17_gpio_init_int(mcp23x17_t *dev, gpio_t pin,
601  gpio_mode_t mode,
602  gpio_flank_t flank,
603  gpio_cb_t isr,
604  void *arg);
605 #endif /* MODULE_MCP23X17_IRQ || DOXYGEN */
606 
618 int mcp23x17_gpio_read(mcp23x17_t *dev, gpio_t pin);
619 
627 void mcp23x17_gpio_write(mcp23x17_t *dev, gpio_t pin, int value);
628 
635 void mcp23x17_gpio_clear(mcp23x17_t *dev, gpio_t pin);
636 
643 void mcp23x17_gpio_set(mcp23x17_t *dev, gpio_t pin);
644 
651 void mcp23x17_gpio_toggle(mcp23x17_t *dev, gpio_t pin);
652 
653 #if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
666 void mcp23x17_gpio_irq_enable(mcp23x17_t *dev, gpio_t pin);
667 
680 void mcp23x17_gpio_irq_disable(mcp23x17_t *dev, gpio_t pin);
681 
682 #endif /* MODULE_MCP23X17_IRQ || DOXYGEN */
683 
684 #ifdef __cplusplus
685 }
686 #endif
687 
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:385
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:418
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:403
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:423
@ MCP23X17_I2C
I2C interface used.
Definition: mcp23x17.h:420
@ MCP23X17_ERROR_RESET_PIN
RESET pin error
Definition: mcp23x17.h:412
@ MCP23X17_ERROR_INV_FLANK
invalid interrupt flank
Definition: mcp23x17.h:409
@ MCP23X17_ERROR_SPI
SPI communication error.
Definition: mcp23x17.h:406
@ MCP23X17_ERROR_NO_DEV
no MCP23x17 I/O expander device
Definition: mcp23x17.h:407
@ MCP23X17_ERROR_I2C
I2C communication error.
Definition: mcp23x17.h:405
@ MCP23X17_ERROR_INV_MODE
invalid pin mode
Definition: mcp23x17.h:408
@ MCP23X17_ERROR_GPIO
GPIO pin error.
Definition: mcp23x17.h:410
@ MCP23X17_OK
success
Definition: mcp23x17.h:404
@ MCP23X17_ERROR_INT_PIN
INTA/INTB pin error
Definition: mcp23x17.h:411
void(* gpio_cb_t)(void *arg)
Signature of event callback functions triggered from interrupts.
Definition: gpio.h:146
uint_fast8_t i2c_t
Default i2c_t type definition.
Definition: i2c.h:144
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.
event structure
Definition: event.h:145
Default interrupt context for GPIO pins.
Definition: gpio.h:152
MCP23017 I2C parameters.
Definition: mcp23x17.h:431
i2c_t dev
I2C device used.
Definition: mcp23x17.h:432
MCP23x17 Hardware interface parameters union.
Definition: mcp23x17.h:450
mcp23x17_if_t type
I2C/SPI interface type selector.
Definition: mcp23x17.h:451
mcp23x17_spi_params_t spi
SPI specific interface parameters.
Definition: mcp23x17.h:457
mcp23x17_i2c_params_t i2c
I2C specific interface parameters.
Definition: mcp23x17.h:454
IRQ event type.
Definition: mcp23x17.h:505
void * dev
MCP23x17 device reference.
Definition: mcp23x17.h:507
event_t event
Super event data structure.
Definition: mcp23x17.h:506
Struct containing the peripheral configuration.
Definition: mcp23x17.h:465
uint8_t addr
MCP2317 device address.
Definition: mcp23x17.h:480
mcp23x17_if_params_t if_params
specific I2C/SPI interface parameters
Definition: mcp23x17.h:485
gpio_t reset_pin
GPIO pin used for RESET signal.
Definition: mcp23x17.h:484
gpio_t int_pin
GPIO pin used for combined INTA/INTB signal.
Definition: mcp23x17.h:481
MCP23x17 configuration structure for mapping expander pins to SAUL.
Definition: mcp23x17.h:540
uint8_t dev
MCP23x17 device index.
Definition: mcp23x17.h:541
saul_gpio_params_t gpio
GPIO configuration for mapping to SAUL.
Definition: mcp23x17.h:542
MCP23S17 SPI parameters.
Definition: mcp23x17.h:440
spi_t dev
SPI device used.
Definition: mcp23x17.h:441
spi_clk_t clk
SPI clock speed.
Definition: mcp23x17.h:442
gpio_t cs
SPI chip Select pin.
Definition: mcp23x17.h:443
Device descriptor for MCP23x17 I/O expanders.
Definition: mcp23x17.h:515
mcp23x17_irq_event_t irq_event
IRQ event object used for the device.
Definition: mcp23x17.h:524
uint16_t od_pins
Pins defined as GPIO_OD or GPIO_OD_PU.
Definition: mcp23x17.h:518
mcp23x17_params_t params
Device initialization parameters.
Definition: mcp23x17.h:516
Direct mapped GPIO configuration values.
Definition: periph.h:50