Library for MAX7300 GPIO Expander

Dependents:   MAX14871_Shield

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers max7300.h Source File

max7300.h

Go to the documentation of this file.
00001 /******************************************************************//**
00002 * @file max7300.h
00003 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
00004 *
00005 * Permission is hereby granted, free of charge, to any person obtaining a
00006 * copy of this software and associated documentation files (the "Software"),
00007 * to deal in the Software without restriction, including without limitation
00008 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00009 * and/or sell copies of the Software, and to permit persons to whom the
00010 * Software is furnished to do so, subject to the following conditions:
00011 *
00012 * The above copyright notice and this permission notice shall be included
00013 * in all copies or substantial portions of the Software.
00014 *
00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00019 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00020 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00021 * OTHER DEALINGS IN THE SOFTWARE.
00022 *
00023 * Except as contained in this notice, the name of Maxim Integrated
00024 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00025 * Products, Inc. Branding Policy.
00026 *
00027 * The mere transfer of this software does not imply any licenses
00028 * of trade secrets, proprietary technology, copyrights, patents,
00029 * trademarks, maskwork rights, or any other form of intellectual
00030 * property whatsoever. Maxim Integrated Products, Inc. retains all
00031 * ownership rights.
00032 **********************************************************************/
00033 
00034 
00035 #ifndef MAX7300_H
00036 #define MAX7300_H
00037 
00038 #include "mbed.h"
00039 
00040 
00041 /**
00042 * @brief 2-Wire-Interfaced, 2.5V to 5.5V, 20-Port or 28-Port I/O 
00043 * Expander
00044 *
00045 * @details The MAX7300 compact, serial-interfaced, I/O expansion 
00046 * peripheral provides microprocessors with up to 28 ports. Each port
00047 * is individually user configurable to either a logic input or logic 
00048 * output.
00049 * @code
00050 * #include "mbed.h"
00051 * #include "max7300.h"
00052 *
00053 * int main (void)
00054 * {
00055 *      I2C i2c_bus(D14, D15);
00056 *
00057 *      Max7300 io_expander(&i2c_bus, Max7300::MAX7300_I2C_ADRS0);   
00058 *      
00059 *      for(uint8_t idx = 0; idx < 10; idx++)
00060 *      {
00061 *          //configure ports 12 - 21 as outputs
00062 *          io_expander.config_port((Max7300::max7300_port_number_t) (idx+12), Max7300::MAX7300_PORT_OUTPUT);
00063 *
00064 *          //configure ports 22 - 31 as inputs
00065 *          io_expander.config_port((Max7300::max7300_port_number_t) (idx+22), Max7300::MAX7300_PORT_INPUT);
00066 *      }
00067 *
00068 *      io_expander.enable_ports();
00069 *      
00070 *      //rest of application...
00071 * }
00072 * @endcode
00073 */
00074 class Max7300
00075 {
00076     public:
00077     
00078     /**
00079     * @brief Valid 7-bit I2C addresses
00080     * @details 16 valid I2C addresses are possible through the 
00081     * connection od AD0 and AD1 to Vcc, GND, SDA, or SCL.
00082     */
00083     typedef enum
00084     {
00085         MAX7300_I2C_ADRS0 = 0x40,
00086         MAX7300_I2C_ADRS1,
00087         MAX7300_I2C_ADRS2,
00088         MAX7300_I2C_ADRS3,
00089         MAX7300_I2C_ADRS4,
00090         MAX7300_I2C_ADRS5,
00091         MAX7300_I2C_ADRS6,
00092         MAX7300_I2C_ADRS7,
00093         MAX7300_I2C_ADRS8,
00094         MAX7300_I2C_ADRS9,
00095         MAX7300_I2C_ADRS10,
00096         MAX7300_I2C_ADRS11,
00097         MAX7300_I2C_ADRS12,
00098         MAX7300_I2C_ADRS13,
00099         MAX7300_I2C_ADRS14,
00100         MAX7300_I2C_ADRS15
00101     }max7300_i2c_adrs_t;
00102     
00103     
00104     /**
00105     * @brief Valid port configurations
00106     * @details Valid port configurations for the MAX7300 are; 
00107     * push-pull GPO, schmitt GPI, schmitt GPI with pull-up.
00108     */
00109     typedef enum
00110     {
00111         MAX7300_PORT_OUTPUT = 1,
00112         MAX7300_PORT_INPUT,
00113         MAX7300_PORT_INPUT_PULLUP
00114     }max7300_port_type_t;
00115         
00116     
00117     /**
00118     * @brief Valid port numbers
00119     * @details Simple enumeration of port numbers
00120     */
00121     typedef enum
00122     {
00123         MAX7300_PORT_04 = 4,
00124         MAX7300_PORT_05,
00125         MAX7300_PORT_06,
00126         MAX7300_PORT_07,
00127         MAX7300_PORT_08,
00128         MAX7300_PORT_09,
00129         MAX7300_PORT_10,
00130         MAX7300_PORT_11,
00131         MAX7300_PORT_12,
00132         MAX7300_PORT_13,
00133         MAX7300_PORT_14,
00134         MAX7300_PORT_15,
00135         MAX7300_PORT_16,
00136         MAX7300_PORT_17,
00137         MAX7300_PORT_18,
00138         MAX7300_PORT_19,
00139         MAX7300_PORT_20,
00140         MAX7300_PORT_21,
00141         MAX7300_PORT_22,
00142         MAX7300_PORT_23,
00143         MAX7300_PORT_24,
00144         MAX7300_PORT_25,
00145         MAX7300_PORT_26,
00146         MAX7300_PORT_27,
00147         MAX7300_PORT_28,
00148         MAX7300_PORT_29,
00149         MAX7300_PORT_30,
00150         MAX7300_PORT_31
00151     }max7300_port_number_t;
00152     
00153     
00154     /**********************************************************//**
00155     * @brief Constructor for Max7300 Class.  
00156     * 
00157     * @details Allows user to use existing I2C object
00158     *
00159     * On Entry:
00160     *     @param[in] i2c_bus - pointer to existing I2C object
00161     *     @param[in] i2c_adrs - 7-bit slave address of MAX7300
00162     *
00163     * On Exit:
00164     *
00165     * @return None
00166     **************************************************************/
00167     Max7300(I2C *i2c_bus, max7300_i2c_adrs_t i2c_adrs);
00168     
00169     
00170     /**********************************************************//**
00171     * @brief Constructor for Max7300 Class.  
00172     * 
00173     * @details Allows user to create a new I2C object if not 
00174     *          already using one
00175     *
00176     * On Entry:
00177     *     @param[in] sda - sda pin of I2C bus
00178     *     @param[in] scl - scl pin of I2C bus
00179     *     @param[in] i2c_adrs - 7-bit slave address of MAX7300
00180     *
00181     * On Exit:
00182     *
00183     * @return None
00184     **************************************************************/
00185     Max7300(PinName sda, PinName scl, max7300_i2c_adrs_t i2c_adrs);
00186     
00187     
00188     /**********************************************************//**
00189     * @brief Default destructor for Max7300 Class.  
00190     *
00191     * @details Destroys I2C object if owner 
00192     *
00193     * On Entry:
00194     *
00195     * On Exit:
00196     *
00197     * @return None
00198     **************************************************************/
00199     ~Max7300();
00200     
00201     
00202     /**********************************************************//**
00203     * @brief Enables MAX7300 GPIO Ports  
00204     *
00205     * @details Sets 'S' bit of configuration register
00206     *
00207     * On Entry:
00208     *
00209     * On Exit:
00210     *
00211     * @return 0 on success, non-0 on failure
00212     **************************************************************/
00213     int16_t enable_ports(void);
00214     
00215     
00216     /**********************************************************//**
00217     * @brief Disables MAX7300 GPIO Ports 
00218     *
00219     * @details Clears 'S' bit of configuration register
00220     *
00221     * On Entry:
00222     *
00223     * On Exit:
00224     *
00225     * @return 0 on success, non-0 on failure
00226     **************************************************************/
00227     int16_t disable_ports(void);
00228     
00229     
00230     /**********************************************************//**
00231     * @brief Enables Transition Detection
00232     *
00233     * @details Sets 'M' bit of configuration register
00234     *
00235     * On Entry:
00236     *
00237     * On Exit:
00238     *
00239     * @return 0 on success, non-0 on failure
00240     **************************************************************/
00241     int16_t enable_transition_detection(void);
00242     
00243     
00244     /**********************************************************//**
00245     * @brief Disables Transition Detection
00246     *
00247     * @details Clears 'M' bit of configuration register
00248     *
00249     * On Entry:
00250     *
00251     * On Exit:
00252     *
00253     * @return 0 on success, non-0 on failure
00254     **************************************************************/
00255     int16_t disable_transition_detection(void);
00256     
00257     
00258     /**********************************************************//**
00259     * @brief Configures a single MAX7300 GPIO port
00260     *
00261     * @details  Configures MAX7300 GPIO port as either an output,
00262     *           input, or input with pullup.
00263     *
00264     * On Entry:
00265     *     @param[in] port_num - GPIO port to configure
00266     *     @param[in] port_type - One of the following port types
00267     *                            MAX7300_PORT_OUTPUT
00268     *                            MAX7300_PORT_INPUT
00269     *                            MAX7300_PORT_INPUT_PULLUP
00270     *
00271     * On Exit:
00272     *
00273     * @return 0 on success, non-0 on failure
00274     **************************************************************/
00275     int16_t config_port(max7300_port_number_t port_num, max7300_port_type_t port_type);
00276     
00277     
00278     /**********************************************************//**
00279     * @brief Configure 4 MAX7300 GPIO ports
00280     *
00281     * @details  Allows user to configure 4 ports at a time
00282     *
00283     * On Entry:
00284     *    @param[in] low_port - lowest of 4 ports to configure, 
00285     *                          on 4 port boundaries as in datasheet
00286     *
00287     *    @param[in] data - Byte with each ports desired type with 
00288     *                      the following format - xx|xx|xx|xx
00289     *
00290     * On Exit:
00291     *
00292     * @return 0 on success, non-0 on failure
00293     **************************************************************/
00294     int16_t config_4_ports(max7300_port_number_t low_port, uint8_t data);
00295     
00296     
00297     /**********************************************************//**
00298     * @brief Configures all MAX7300 GPIO ports
00299     *
00300     * @details  Allows user to configure all ports to a single type
00301     *
00302     * On Entry:
00303     *    @param[in] port_type - One of the following port types
00304     *                            MAX7300_PORT_OUTPUT
00305     *                            MAX7300_PORT_INPUT
00306     *                            MAX7300_PORT_INPUT_PULLUP
00307     *
00308     * On Exit:
00309     *
00310     * @return 0 on success, non-0 on failure
00311     **************************************************************/
00312     int16_t config_all_ports(max7300_port_type_t port_type);
00313     
00314     
00315     /**********************************************************//**
00316     * @brief Read a single MAX7300 GPIO port
00317     *
00318     * @details
00319     *
00320     * On Entry:
00321     *    @param[in] port_num - MAX7300 port number to read
00322     *
00323     * On Exit:
00324     *
00325     * @return State of port, or -1 on failure
00326     **************************************************************/
00327     int16_t read_port(max7300_port_number_t port_num);
00328     
00329     
00330     /**********************************************************//**
00331     * @brief Write a single MAX7300 GPIO port
00332     *
00333     * @details
00334     *
00335     * On Entry:
00336     *    @param[in] port_num - MAX7300 port to write
00337     *    @param[in] data - lsb of byte is written to port
00338     *
00339     * On Exit:
00340     *
00341     * @return 0 on success, non-0 on failure
00342     **************************************************************/
00343     int16_t write_port(max7300_port_number_t port_num, uint8_t data);
00344     
00345     
00346     /**********************************************************//**
00347     * @brief Read 8 MAX7300 GPIO ports
00348     *
00349     * @details
00350     *
00351     * On Entry:
00352     *    @param[in] low_port - lowest port of 8 ports to read, 
00353     *                          on 8 port boundaries as in datasheet.
00354     *                          Max is port 24
00355     *
00356     * On Exit:
00357     *
00358     * @return State of ports, or -1 on failure
00359     **************************************************************/
00360     int16_t read_8_ports(max7300_port_number_t low_port);
00361     
00362     
00363     /**********************************************************//**
00364     * @brief Write 8 MAX7300 GPIO ports
00365     *
00366     * @details
00367     *
00368     * On Entry:
00369     *    @param[in] low_port - lowest port of 8 ports to write, 
00370     *                          on 8 port boundaries as in datasheet.
00371     *                          Max is port 24
00372     *
00373     *    @param[in] data - Data is written to ports
00374     *
00375     * On Exit:
00376     *
00377     * @return 0 on success, non-0 on failure
00378     **************************************************************/
00379     int16_t write_8_ports(max7300_port_number_t low_port, uint8_t data);
00380     
00381     
00382      /**********************************************************//**
00383     * @brief Read transition detection mask register
00384     *
00385     * @details See page 11 of DS, right hand side column, paragraph 2
00386     *          for details on one-shot event.
00387     *          
00388     * On Entry:
00389     *    @param[in] enable_snapshot - true to re-enable transition
00390     *                                 detection
00391     *
00392     * On Exit:
00393     *
00394     * @return 0 on success, non-0 on failure
00395     **************************************************************/
00396     int16_t read_mask_register(bool enable_snapshot);
00397     
00398     
00399     /**********************************************************//**
00400     * @brief Write transition detection mask register
00401     *
00402     * @details Enables transition detection on Ports 30-24
00403     *          
00404     *
00405     * On Entry:
00406     *    @param[in] data - Bits to set
00407     *
00408     * On Exit:
00409     *
00410     * @return 0 on success, non-0 on failure
00411     **************************************************************/
00412     int16_t write_mask_register(uint8_t data);
00413     
00414     
00415     private:
00416     
00417     I2C *_p_i2c;
00418     bool _i2c_owner;
00419     uint8_t _w_adrs;
00420     uint8_t _r_adrs;
00421     
00422     int16_t write_config_register(bool set_clear, uint8_t data);
00423     
00424 };
00425 #endif /* MAX7300_H*/