FlexBook / Mbed 2 deprecated FlexBook171204a

Dependencies:   SDFileSystem app epson mbed msp430 pl tests

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mcp23s17.h Source File

mcp23s17.h

00001 //
00002 // Filename: mcp23s17.h
00003 //
00004 // Flexbook Hardware Abstraction Layer.
00005 //
00006 
00007 #ifndef MCP23S17_H
00008 #define MCP23S17_H
00009 
00010 #include "mbed.h"
00011 
00012 namespace HAL {
00013 
00014 /**
00015  * @brief MCP23S17 register addresses in bank 0 mode.
00016  */
00017 enum REG_MCP23S17
00018 {
00019 IODIRA   = 0x00,
00020 IODIRB   = 0x01,
00021 IPOLA    = 0x02,
00022 IPOLB    = 0x03,
00023 GPINTENA = 0x04,
00024 GPINTENB = 0x05,
00025 DEFVALA  = 0x06,
00026 DEFVALB  = 0x07,
00027 INTCONA  = 0x08,
00028 INTCONB  = 0x09,
00029 IOCON1   = 0x0a,
00030 IOCON2   = 0x0b,
00031 GPPUA    = 0x0c,
00032 GPPUB    = 0x0d,
00033 INTFA    = 0x0e,
00034 INTFB    = 0x0f,
00035 INTCAPA  = 0x10,
00036 INTCAPB  = 0x11,
00037 GPIOA    = 0x12,
00038 GPIOB    = 0x13,
00039 OLATA    = 0x14,
00040 OLATB    = 0x15
00041 };
00042 
00043 /**
00044  * @brief Microchip MCP23S17 encapsulation.
00045  * This chip is an SPI connected I/O expander.
00046  *
00047  * Example usage:
00048  <pre>
00049     SPI spi(p5, p6, p7);
00050     DigitalOut cs(p19);
00051     MCP23S17 mcp23s17(0x00, spi, cs);
00052     ...
00053     mcp23s17.Write(GPIOB, 0xaa);
00054  </pre>
00055  *
00056  * The MCP23S17 uses pins 5, 6, 7 (SPI MOSI, MISO, SCK) and 19 (CS) and
00057  * the write command sets GPIOB to 0xaa. The chip address is 0x00.
00058  *
00059  * Note that this code example does not cover the setup of the chip
00060  * configuration registers.
00061  */
00062 class MCP23S17
00063 {
00064 public:
00065     /**
00066      * @brief Constructor.
00067      * @param address The SPI address offset of the chip.
00068      * @param SPI the SPI to use for communication.
00069      * @param cs The chip select pin.
00070      */
00071     MCP23S17(int address, SPI &spi, DigitalOut &cs);
00072 
00073     /**
00074      * @brief Read a register.
00075      * @param address The SPI address of the MCP23S17.
00076      * @param reg The register to read.
00077      * @return The register value.
00078      */
00079     int Read(REG_MCP23S17 reg);
00080 
00081     /**
00082      * @brief Write a register.
00083      * @param address The SPI address of the MCP23S17.
00084      * @param reg The register to write.
00085      * @param value The value to write to the register.
00086      */
00087     void Write(REG_MCP23S17 reg, int value);
00088 
00089 private:
00090     int address;
00091     SPI &spi;
00092     DigitalOut &cs;
00093 };
00094 
00095 } // End HAL namespace.
00096 
00097 #endif // MCP23S17_H