Daniel Wyatt / MCP23008
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP23008.hpp Source File

MCP23008.hpp

00001 #include "mbed.h"
00002 
00003 /** MCP23008 class
00004  *
00005  * Allow access to an I2C connected MCP23008 8-bit I/O extender chip
00006  *
00007  */
00008 class MCP23008 {
00009 public:
00010     enum Frequency {
00011         Frequency_100KHz = 100000,
00012         Frequency_400KHz = 400000,
00013         /* Note: 1.7MHz probably won't work for mbed */
00014         Frequency_1700KHz = 1700000
00015     };
00016     enum Pin {
00017         Pin_GP0 = 0x01,
00018         Pin_GP1 = 0x02,
00019         Pin_GP2 = 0x04,
00020         Pin_GP3 = 0x08,
00021         Pin_GP4 = 0x10,
00022         Pin_GP5 = 0x20,
00023         Pin_GP6 = 0x40,
00024         Pin_GP7 = 0x80,
00025         Pin_All = 0xFF
00026     };
00027     /** Constructor
00028      *
00029      * @param sda I2C sda pin
00030      * @param scl I2C scl pin
00031      * @param address The hardware address of the MCP23008. This is the 3-bit
00032      * value that is physically set via A0, A1, and A2.
00033      * @param freq The I2C frequency. Should probably be 100KHz or 400KHz.
00034      */
00035     MCP23008 ( PinName sda, PinName scl, uint8_t address, Frequency freq = Frequency_100KHz );
00036 
00037     /** Set pins to input mode
00038      *
00039      * This function is used to set which pins are inputs (if any). Example:
00040      * set_inputs ( Pin_GP0 | Pin_GP1 | Pin_GP2 );
00041      * Note that these are set to input in addition to the previously set.
00042      * In other words, the following:
00043      * set_inputs ( Pin_GP1 );
00044      * set_inputs ( Pin_GP2 );
00045      * Results in at least two pins set to input.
00046      *
00047      * @param pins A bitmask of pins to set to input mode.
00048      */
00049     void set_input_pins ( uint8_t pins );
00050     /** Set pins to output mode
00051      *
00052      * This function is used to set which pins are outputs (if any). Example:
00053      * set_outputs ( Pin_GP0 | Pin_GP1 | Pin_GP2 );
00054      * Note that these are set to output in addition to the previously set.
00055      * In other words, the following:
00056      * set_outputs ( Pin_GP1 );
00057      * set_outputs ( Pin_GP2 );
00058      * Results in at least two pins set to output.
00059      *
00060      * @param pins A bitmask of pins to set to output mode.
00061      */
00062     void set_output_pins ( uint8_t pins );
00063 
00064     /** Write to the output pins.
00065      *
00066      * This function is used to set output pins on or off.
00067      *
00068      * @param values A bitmask indicating whether a pin should be on or off.
00069      */
00070     void write_outputs ( uint8_t values );
00071     /** Read back the outputs.
00072      *
00073      * This function is used to read the last values written to the output pins.
00074      *
00075      * @returns The value from the OLAT register.
00076      */
00077     uint8_t read_outputs ();
00078 
00079     /** Read from the input pins.
00080      *
00081      * This function is used to read the values from the input pins.
00082      *
00083      * @returns A bitmask of the current state of the input pins.
00084      */
00085     uint8_t read_inputs ();
00086 
00087     /** Set the input pin polarity.
00088      *
00089      * This function sets the polarity of the input pins.
00090      * A 1 bit is inverted polarity, a 0 is normal.
00091      *
00092      * @param values A bitmask of the input polarity.
00093      */
00094     void set_input_polarity ( uint8_t values );
00095     /** Read back the current input pin polarity.
00096      *
00097      * This function reads the current state of the input pin polarity.
00098      *
00099      * @returns The value from the IPOL register.
00100      */
00101     uint8_t get_input_polarity ();
00102 
00103     /** Enable and disable the internal pull-up resistors for input pins.
00104      *
00105      * This function enables the internal 100 kΩ pull-up resistors.
00106      * A 1 bit enables the pull-up resistor for the corresponding input pin.
00107      *
00108      * @param values A bitmask indicating which pull-up resistors should be enabled/disabled.
00109      */
00110     void set_pullups ( uint8_t values );
00111     /** Get the current state of the internal pull-up resistors.
00112      *
00113      * @returns The current state of the pull-up resistors.
00114      */
00115     uint8_t get_pullups ();
00116 
00117     /** Generate an interrupt when a pin changes.
00118      *
00119      * This function enables interrupt generation for the specified pins.
00120      * The interrupt is active-low by default.
00121      * The function acknowledge_interrupt must be called before another
00122      * interrupt will be generated.
00123      * Example:
00124      * @code
00125      * InterruptIn in ( p16 );
00126      * MCP23008 mcp ( p9, p10, 0 );
00127      * in.fall ( &interrupt );
00128      * mcp.interrupt_on_changes ( MCP23008::Pin_GP0 );
00129      * while ( 1 ) {
00130      *      wait ( 1 );
00131      * }
00132      * @endcode
00133      * 
00134      * @param pins A bitmask of the pins that may generate an interrupt.
00135      */
00136     void interrupt_on_changes ( uint8_t pins );
00137     /** Disables interrupts for the specified pins.
00138      *
00139      * @param values A bitmask indicating which interrupts should be disabled.
00140      */
00141     void disable_interrupts ( uint8_t pins );
00142 
00143     /** Acknowledge a generated interrupt.
00144      *
00145      * This function must be called when an interrupt is generated to discover
00146      * which pin caused the interrupt and to enable future interrupts.
00147      *
00148      * @param pin An output paramter that specifies which pin generated the interrupt.
00149      * @param values The current state of the input pins.
00150      */
00151     void acknowledge_interrupt ( uint8_t &pin, uint8_t &values );
00152 
00153 private:
00154     uint8_t read_register ( uint8_t reg );
00155     void write_register ( uint8_t reg, uint8_t value );
00156     void write_mask ( uint8_t reg, uint8_t mask, bool value );
00157 
00158     void reset ();
00159 
00160     I2C i2c;
00161     uint8_t i2c_address;
00162 };