![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Complete
MCP23S17.h@0:8f5b2af5e1d5, 2016-01-30 (annotated)
- Committer:
- mikeb
- Date:
- Sat Jan 30 02:21:04 2016 +0000
- Revision:
- 0:8f5b2af5e1d5
Combining Functions;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mikeb | 0:8f5b2af5e1d5 | 1 | /* MCP23S17 - drive the Microchip MCP23S17 16-bit Port Extender using SPI |
mikeb | 0:8f5b2af5e1d5 | 2 | * Copyright (c) 2010 Romilly Cocking |
mikeb | 0:8f5b2af5e1d5 | 3 | * Released under the MIT License: http://mbed.org/license/mit |
mikeb | 0:8f5b2af5e1d5 | 4 | * |
mikeb | 0:8f5b2af5e1d5 | 5 | * version 0.4 |
mikeb | 0:8f5b2af5e1d5 | 6 | */ |
mikeb | 0:8f5b2af5e1d5 | 7 | #include "mbed.h" |
mikeb | 0:8f5b2af5e1d5 | 8 | |
mikeb | 0:8f5b2af5e1d5 | 9 | #ifndef MCP23S17_H |
mikeb | 0:8f5b2af5e1d5 | 10 | #define MCP23S17_H |
mikeb | 0:8f5b2af5e1d5 | 11 | |
mikeb | 0:8f5b2af5e1d5 | 12 | #define INTERRUPT_POLARITY_BIT 0x02 |
mikeb | 0:8f5b2af5e1d5 | 13 | #define INTERRUPT_MIRROR_BIT 0x40 |
mikeb | 0:8f5b2af5e1d5 | 14 | |
mikeb | 0:8f5b2af5e1d5 | 15 | // all register addresses assume IOCON.BANK = 0 (POR default) |
mikeb | 0:8f5b2af5e1d5 | 16 | |
mikeb | 0:8f5b2af5e1d5 | 17 | #define IODIRA 0x00 |
mikeb | 0:8f5b2af5e1d5 | 18 | #define GPINTENA 0x04 |
mikeb | 0:8f5b2af5e1d5 | 19 | #define DEFVALA 0x06 |
mikeb | 0:8f5b2af5e1d5 | 20 | #define INTCONA 0x08 |
mikeb | 0:8f5b2af5e1d5 | 21 | #define IOCON 0x0A |
mikeb | 0:8f5b2af5e1d5 | 22 | #define GPPUA 0x0C |
mikeb | 0:8f5b2af5e1d5 | 23 | #define GPIOA 0x12 |
mikeb | 0:8f5b2af5e1d5 | 24 | #define OLATA 0x14 |
mikeb | 0:8f5b2af5e1d5 | 25 | |
mikeb | 0:8f5b2af5e1d5 | 26 | // Control settings |
mikeb | 0:8f5b2af5e1d5 | 27 | |
mikeb | 0:8f5b2af5e1d5 | 28 | #define IOCON_BANK 0x80 // Banked registers |
mikeb | 0:8f5b2af5e1d5 | 29 | #define IOCON_BYTE_MODE 0x20 // Disables sequential operation. If bank = 0, operations toggle between A and B registers |
mikeb | 0:8f5b2af5e1d5 | 30 | #define IOCON_HAEN 0x08 // Hardware address enable |
mikeb | 0:8f5b2af5e1d5 | 31 | |
mikeb | 0:8f5b2af5e1d5 | 32 | enum Polarity { ACTIVE_LOW , ACTIVE_HIGH }; |
mikeb | 0:8f5b2af5e1d5 | 33 | enum Port { PORT_A, PORT_B }; |
mikeb | 0:8f5b2af5e1d5 | 34 | |
mikeb | 0:8f5b2af5e1d5 | 35 | class MCP23S17 { |
mikeb | 0:8f5b2af5e1d5 | 36 | public: |
mikeb | 0:8f5b2af5e1d5 | 37 | MCP23S17(SPI& spi, PinName ncs, char writeOpcode); |
mikeb | 0:8f5b2af5e1d5 | 38 | void direction(Port port, char direction); |
mikeb | 0:8f5b2af5e1d5 | 39 | void configurePullUps(Port port, char offOrOn); |
mikeb | 0:8f5b2af5e1d5 | 40 | void interruptEnable(Port port, char interruptsEnabledMask); |
mikeb | 0:8f5b2af5e1d5 | 41 | void interruptPolarity(Polarity polarity); |
mikeb | 0:8f5b2af5e1d5 | 42 | void mirrorInterrupts(bool mirror); |
mikeb | 0:8f5b2af5e1d5 | 43 | void defaultValue(Port port, char valuesToCompare); |
mikeb | 0:8f5b2af5e1d5 | 44 | void interruptControl(Port port, char interruptContolBits); |
mikeb | 0:8f5b2af5e1d5 | 45 | char read(Port port); |
mikeb | 0:8f5b2af5e1d5 | 46 | void write(Port port, char byte); |
mikeb | 0:8f5b2af5e1d5 | 47 | protected: |
mikeb | 0:8f5b2af5e1d5 | 48 | SPI& _spi; |
mikeb | 0:8f5b2af5e1d5 | 49 | DigitalOut _ncs; |
mikeb | 0:8f5b2af5e1d5 | 50 | void _init(); |
mikeb | 0:8f5b2af5e1d5 | 51 | void _write(Port port, char address, char data); |
mikeb | 0:8f5b2af5e1d5 | 52 | void _write(char address, char data); |
mikeb | 0:8f5b2af5e1d5 | 53 | char _read(Port port, char address); |
mikeb | 0:8f5b2af5e1d5 | 54 | char _read(char address); |
mikeb | 0:8f5b2af5e1d5 | 55 | char _readOpcode; |
mikeb | 0:8f5b2af5e1d5 | 56 | char _writeOpcode; |
mikeb | 0:8f5b2af5e1d5 | 57 | }; |
mikeb | 0:8f5b2af5e1d5 | 58 | |
mikeb | 0:8f5b2af5e1d5 | 59 | #endif |