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 "periph/gpio.h"
350 #include "periph/i2c.h"
351 #include "periph/spi.h"
352 
353 #if IS_USED(MODULE_SAUL_GPIO) || DOXYGEN
354 #include "saul/periph.h"
355 #endif /* MODULE_SAUL_GPIO */
356 
357 #if !IS_USED(MODULE_MCP23X17_I2C) && !IS_USED(MODULE_MCP23X17_SPI)
358 #error "Please provide the MCP23x17 variants used by the application."
359 #error "At least one variant has to be specified (mcp23017 and/or mcp23s17)."
360 #endif
361 
362 #if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
363 
364 #include "event.h"
365 
366 #endif /* MODULE_MCP23X17_IRQ */
367 
379 #define MCP23X17_BASE_ADDR (0x20)
380 
384 #define MCP23X17_GPIO_PIN_NUM (16)
385 
397 #define MCP23X17_GPIO_PIN(port, pin) ((gpio_t)((port << 3) | pin))
398 
402 typedef enum {
413 
417 typedef enum {
418 #if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN
420 #endif
421 #if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN
423 #endif
424 } mcp23x17_if_t;
425 
429 #if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN
430 typedef struct {
433 #endif
434 
438 #if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN
439 typedef struct {
440  spi_t dev;
442  gpio_t cs;
444 #endif
445 
449 typedef struct {
451  union {
452 #if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN
454 #endif
455 #if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN
457 #endif
458  };
460 
464 typedef struct {
479  uint8_t addr;
480  gpio_t int_pin;
483  gpio_t reset_pin;
486 
487 #if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
504 typedef struct {
506  void *dev;
508 
509 #endif /* MODULE_MCP23X17_IRQ */
510 
514 typedef struct {
517  uint16_t od_pins;
519 #if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
524 #endif /* MODULE_MCP23X17_IRQ */
525 
526 } mcp23x17_t;
527 
528 #if IS_USED(MODULE_SAUL_GPIO) || DOXYGEN
539 typedef struct {
540  uint8_t dev;
543 #endif
544 
557 int mcp23x17_init(mcp23x17_t *dev, const mcp23x17_params_t *params);
558 
570 int mcp23x17_gpio_init(mcp23x17_t *dev, gpio_t pin, gpio_mode_t mode);
571 
572 #if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
599 int mcp23x17_gpio_init_int(mcp23x17_t *dev, gpio_t pin,
600  gpio_mode_t mode,
601  gpio_flank_t flank,
602  gpio_cb_t isr,
603  void *arg);
604 #endif /* MODULE_MCP23X17_IRQ || DOXYGEN */
605 
617 int mcp23x17_gpio_read(mcp23x17_t *dev, gpio_t pin);
618 
626 void mcp23x17_gpio_write(mcp23x17_t *dev, gpio_t pin, int value);
627 
634 void mcp23x17_gpio_clear(mcp23x17_t *dev, gpio_t pin);
635 
642 void mcp23x17_gpio_set(mcp23x17_t *dev, gpio_t pin);
643 
650 void mcp23x17_gpio_toggle(mcp23x17_t *dev, gpio_t pin);
651 
652 #if IS_USED(MODULE_MCP23X17_IRQ) || DOXYGEN
665 void mcp23x17_gpio_irq_enable(mcp23x17_t *dev, gpio_t pin);
666 
679 void mcp23x17_gpio_irq_disable(mcp23x17_t *dev, gpio_t pin);
680 
681 #endif /* MODULE_MCP23X17_IRQ || DOXYGEN */
682 
683 #ifdef __cplusplus
684 }
685 #endif
686 
gpio_flank_t
Definition: periph_cpu.h:179
spi_clk_t
Definition: periph_cpu.h:351
Low-level GPIO peripheral driver interface definitions.
gpio_mode_t
Available pin modes.
Definition: periph_cpu.h:91
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:384
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:417
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:402
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:422
@ MCP23X17_I2C
I2C interface used.
Definition: mcp23x17.h:419
@ MCP23X17_ERROR_RESET_PIN
RESET pin error
Definition: mcp23x17.h:411
@ MCP23X17_ERROR_INV_FLANK
invalid interrupt flank
Definition: mcp23x17.h:408
@ MCP23X17_ERROR_SPI
SPI communication error.
Definition: mcp23x17.h:405
@ MCP23X17_ERROR_NO_DEV
no MCP23x17 I/O expander device
Definition: mcp23x17.h:406
@ MCP23X17_ERROR_I2C
I2C communication error.
Definition: mcp23x17.h:404
@ MCP23X17_ERROR_INV_MODE
invalid pin mode
Definition: mcp23x17.h:407
@ MCP23X17_ERROR_GPIO
GPIO pin error.
Definition: mcp23x17.h:409
@ MCP23X17_OK
success
Definition: mcp23x17.h:403
@ MCP23X17_ERROR_INT_PIN
INTA/INTB pin error
Definition: mcp23x17.h:410
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.
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:430
i2c_t dev
I2C device used.
Definition: mcp23x17.h:431
MCP23x17 Hardware interface parameters union.
Definition: mcp23x17.h:449
mcp23x17_if_t type
I2C/SPI interface type selector.
Definition: mcp23x17.h:450
mcp23x17_spi_params_t spi
SPI specific interface parameters.
Definition: mcp23x17.h:456
mcp23x17_i2c_params_t i2c
I2C specific interface parameters.
Definition: mcp23x17.h:453
IRQ event type.
Definition: mcp23x17.h:504
void * dev
MCP23x17 device reference.
Definition: mcp23x17.h:506
event_t event
Super event data structure.
Definition: mcp23x17.h:505
Struct containing the peripheral configuration.
Definition: mcp23x17.h:464
uint8_t addr
MCP2317 device address.
Definition: mcp23x17.h:479
mcp23x17_if_params_t if_params
specific I2C/SPI interface parameters
Definition: mcp23x17.h:484
gpio_t reset_pin
GPIO pin used for RESET signal.
Definition: mcp23x17.h:483
gpio_t int_pin
GPIO pin used for combined INTA/INTB signal.
Definition: mcp23x17.h:480
MCP23x17 configuration structure for mapping expander pins to SAUL.
Definition: mcp23x17.h:539
uint8_t dev
MCP23x17 device index.
Definition: mcp23x17.h:540
saul_gpio_params_t gpio
GPIO configuration for mapping to SAUL.
Definition: mcp23x17.h:541
MCP23S17 SPI parameters.
Definition: mcp23x17.h:439
spi_t dev
SPI device used.
Definition: mcp23x17.h:440
spi_clk_t clk
SPI clock speed.
Definition: mcp23x17.h:441
gpio_t cs
SPI chip Select pin.
Definition: mcp23x17.h:442
Device descriptor for MCP23x17 I/O expanders.
Definition: mcp23x17.h:514
mcp23x17_irq_event_t irq_event
IRQ event object used for the device.
Definition: mcp23x17.h:523
uint16_t od_pins
Pins defined as GPIO_OD or GPIO_OD_PU.
Definition: mcp23x17.h:517
mcp23x17_params_t params
Device initialization parameters.
Definition: mcp23x17.h:515
Direct mapped GPIO configuration values.
Definition: periph.h:50