YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
mcp23s17_drv.h
1//
2// mcp23s17_drv.h
3//
4// Created on: 28.02.2016
5// Author: Andreas Terstegge
6//
7
8
9/*******************************************************
10 MCP23S17 I/O Expander with Serial interface
11
12 This is a driver for the MCP23S17 GPIO expander, which
13 has 2 8-bit ports (A & B), and is accessed via the SPI
14 interface.
15
16 The two ports are accessed as one 16-bit port, using
17 the following bit-numbering:
18
19 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
20 \_______GPIOB_________/ \_______GPIOA_________/
21 MSB LSB MSB LSB
22
23 29.01.2015 initial version
24
25 (c) 2015 A. Terstegge
26
27********************************************************/
28#ifndef _MCP23S17_DRV_H_
29#define _MCP23S17_DRV_H_
30
31#include "gpio_interface.h"
32#include "spi_interface.h"
33
35
36 public:
37 // User interface
39 // The 2. constructor parameter is the SPI hardware
40 // address of the device (0...7);
41 mcp23s17_drv(spi_interface & hw, uint8_t spi_addr);
42
43 // Basic GPIO handling
44 void gpioMode (uint16_t mode);
45 bool gpioRead ();
46 void gpioWrite(bool value);
47
48 // Interrupt handling
49 void attachInterrupt (void (*)(uint16_t gpio), uint16_t mode);
50 void detachInterrupt ();
51 void enableInterrupt ();
52 void disableInterrupt();
53 void handleInterrupt();
54
55 // Methods to read/write the entire port
56 // Only bits with '1' in the mask are processed
57 uint16_t digitalReadPort (uint16_t mask = 0xffff);
58 void digitalWritePort(uint16_t value, uint16_t mask = 0xffff);
59
60 virtual ~mcp23s17_drv() { }
61
62 private:
63 uint8_t _port;
64 uint8_t _pin;
65
66 // SPI data
67 spi_interface & _spi;
68 uint8_t _spi_addr; // 0...7
69
70 // register values
71 uint16_t _iodir;
72 uint16_t _gppu;
73 uint16_t _olat;
74 uint16_t _gpinten;
75 uint16_t _defval;
76 uint16_t _intcon;
77
78 // interrupt handler functions
79 void (*intHandler[2][8])(uint16_t gpio);
80 uint16_t intMode[2][8];
81
82 // Hardware interface
84 uint16_t readRegister (uint8_t reg);
85 void writeRegister(uint8_t reg, uint16_t value);
86
87};
88
89#endif // _MCP23S17_DRV_H_