Library for MAX7300 GPIO Expander

Dependents:   MAX14871_Shield

Files at this revision

API Documentation at this revision

Comitter:
j3
Date:
Wed Jul 15 15:10:04 2015 +0000
Child:
1:e1ee2549a047
Commit message:
initial commit

Changed in this revision

max7300.cpp Show annotated file Show diff for this revision Revisions of this file
max7300.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/max7300.cpp	Wed Jul 15 15:10:04 2015 +0000
@@ -0,0 +1,267 @@
+/******************************************************************//**
+* @file max7300.h
+*
+* @author Justin Jordan
+*
+* @version 0.0
+*
+* Started: 14JUL15
+*
+* Updated: 
+*
+* @brief Source file for Max7300 class
+***********************************************************************
+* Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included
+* in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
+* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+* OTHER DEALINGS IN THE SOFTWARE.
+*
+* Except as contained in this notice, the name of Maxim Integrated
+* Products, Inc. shall not be used except as stated in the Maxim Integrated
+* Products, Inc. Branding Policy.
+*
+* The mere transfer of this software does not imply any licenses
+* of trade secrets, proprietary technology, copyrights, patents,
+* trademarks, maskwork rights, or any other form of intellectual
+* property whatsoever. Maxim Integrated Products, Inc. retains all
+* ownership rights.
+**********************************************************************/
+
+
+#include "max7300.h"
+
+
+//*********************************************************************
+Max7300::Max7300(I2C *i2c_bus, max7300_i2c_adrs_t i2c_adrs): _p_i2c(i2c_bus)
+{
+    _i2c_owner = false;
+    
+    _r_adrs = ((i2c_adrs << 1) | 1);
+    _w_adrs = (i2c_adrs << 1);
+}
+
+
+//*********************************************************************
+Max7300::Max7300(PinName sda, PinName scl, max7300_i2c_adrs_t i2c_adrs)
+{
+    _p_i2c = new I2C(sda, scl);
+    _i2c_owner = true;
+    
+    _r_adrs = ((i2c_adrs << 1) | 1);
+    _w_adrs = (i2c_adrs << 1);
+}
+
+
+//*********************************************************************
+Max7300::~Max7300()
+{
+    if(_i2c_owner) 
+    {
+        delete _p_i2c;
+    }
+}
+
+
+//*********************************************************************
+int16_t Max7300::enable_ports(void)
+{
+    int16_t result = -1;
+    char data[] = {MAX7300_CONFIGURATION, 0};
+    
+    //set internal register pointer to configuration register
+    result = _p_i2c->write(_w_adrs, data, 1, true);
+    if(!result)
+    {
+        //get current configuration register
+        result = _p_i2c->read(_w_adrs, (data +1 ), 1, true);
+        
+        if(!result)
+        {
+            //set 'S' bit
+            data[1] |= 0x01;
+            
+            //write back to device
+            result = result = _p_i2c->write(_w_adrs, data, 2, false);
+        } 
+    }
+
+    return result;
+}
+
+
+//*********************************************************************
+int16_t Max7300::disable_ports(void)
+{
+    int16_t result = -1;
+    char data[] = {MAX7300_CONFIGURATION, 0};
+    
+    //set internal register pointer to configuration register
+    result = _p_i2c->write(_w_adrs, data, 1, true);
+    if(!result)
+    {
+        //get current configuration register
+        result = _p_i2c->read(_w_adrs, (data +1 ), 1, true);
+        
+        if(!result)
+        {
+            //clear 'S' bit
+            data[1] &= ~0x01;
+            
+            //write back to device
+            result = result = _p_i2c->write(_w_adrs, data, 2, false);
+        } 
+    }
+
+    return result;
+}
+
+
+//*********************************************************************
+int16_t Max7300::config_port(max7300_port_number_t port_num, max7300_port_type_t port_type)
+{
+    int16_t result = -1;
+    char data[2];
+    
+    //get address of port configuration register 
+    data[0]  = ((port_num/4) + 8);
+    
+    //get port config bits offset in that register
+    uint8_t offset = (port_num % 4);
+    
+    //set internal register pointer to port configuration register
+    result = _p_i2c->write(_w_adrs, data, 1, true);
+    if(!result)
+    {
+        //get current port configuration register
+        result = _p_i2c->read(_w_adrs, (data +1 ), 1, true);
+        
+        if(!result)
+        {
+            //clear old port configuration
+            data[1] &= ~(0x03 << (offset*2));
+            //set port configuration bits
+            data[1] |= ((port_type & 0x03) << (offset*2));
+            
+            //write back to device
+            result = result = _p_i2c->write(_w_adrs, data, 2, false);
+        } 
+    }
+    
+    return result;
+}
+
+
+//*********************************************************************
+int16_t Max7300::config_ports(max7300_registers_t reg, uint8_t data)
+{
+    int16_t result = -1;
+    char local_data[] = {reg, data};
+    
+    //no need for read, modify, write.  
+    //Fx is intended to write whole register
+    result = _p_i2c->write(_w_adrs, local_data, 2, false);
+    
+    return result;
+}
+
+
+//*********************************************************************
+int16_t Max7300::read_port(max7300_port_number_t port_num)
+{
+    int16_t result = -1;
+    char data[2];
+    
+    data[0] = (port_num + 0x20);
+    
+    //set internal register pointer to port data register
+    result = _p_i2c->write(_w_adrs, data, 1, true);
+    if(!result)
+    {
+        //get port data
+        result = _p_i2c->read(_w_adrs, (data +1 ), 1, false);
+        if(!result)
+        {
+            result = data[1];
+        }
+        else
+        {
+            result = -1;
+        }
+    }
+     
+    return result;
+}
+
+
+//*********************************************************************
+int16_t Max7300::write_port(max7300_port_number_t port_num, uint8_t data)
+{
+    int16_t result = -1;
+    char local_data[] = {(port_num + 0x20), data};
+    
+    //no need for read, modify, write.  
+    //Fx is intended to write whole register
+    result = _p_i2c->write(_w_adrs, local_data, 2, false);
+    
+    return result;
+}
+
+
+//*********************************************************************
+int16_t Max7300::read_eight_ports(max7300_registers_t reg)
+{
+    int16_t result = -1;
+    char data[2];
+    
+    data[0] = reg;
+    
+    //set internal register pointer to port data register
+    result = _p_i2c->write(_w_adrs, data, 1, true);
+    if(!result)
+    {
+        //get port data
+        result = _p_i2c->read(_w_adrs, (data +1 ), 1, false);
+        if(!result)
+        {
+            result = data[1];
+        }
+        else
+        {
+            result = -1;
+        }
+    }
+     
+    return result;
+}
+
+
+//*********************************************************************
+int16_t Max7300::write_eight_ports(max7300_registers_t reg, uint8_t data)
+{
+    int16_t result = -1;
+    char local_data[] = {reg, data};
+    
+    //no need for read, modify, write.  
+    //Fx is intended to write whole register
+    result = _p_i2c->write(_w_adrs, local_data, 2, false);
+    
+    return result;
+}
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/max7300.h	Wed Jul 15 15:10:04 2015 +0000
@@ -0,0 +1,350 @@
+/******************************************************************//**
+* @file max7300.h
+*
+* @author Justin Jordan
+*
+* @version 0.0
+*
+* Started: 14JUL15
+*
+* Updated: 
+*
+* @brief Header file for Max7300 class
+*
+***********************************************************************
+* Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included
+* in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
+* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+* OTHER DEALINGS IN THE SOFTWARE.
+*
+* Except as contained in this notice, the name of Maxim Integrated
+* Products, Inc. shall not be used except as stated in the Maxim Integrated
+* Products, Inc. Branding Policy.
+*
+* The mere transfer of this software does not imply any licenses
+* of trade secrets, proprietary technology, copyrights, patents,
+* trademarks, maskwork rights, or any other form of intellectual
+* property whatsoever. Maxim Integrated Products, Inc. retains all
+* ownership rights.
+**********************************************************************/
+
+
+#ifndef MAX7300_H
+#define MAX7300_H
+
+#include "mbed.h"
+
+
+class Max7300
+{
+    public:
+    
+    typedef enum
+    {
+        MAX7300_I2C_ADRS0 = 0x40,
+        MAX7300_I2C_ADRS1,
+        MAX7300_I2C_ADRS2,
+        MAX7300_I2C_ADRS3,
+        MAX7300_I2C_ADRS4,
+        MAX7300_I2C_ADRS5,
+        MAX7300_I2C_ADRS6,
+        MAX7300_I2C_ADRS7,
+        MAX7300_I2C_ADRS8,
+        MAX7300_I2C_ADRS9,
+        MAX7300_I2C_ADRS10,
+        MAX7300_I2C_ADRS11,
+        MAX7300_I2C_ADRS12,
+        MAX7300_I2C_ADRS13,
+        MAX7300_I2C_ADRS14,
+        MAX7300_I2C_ADRS15
+    }max7300_i2c_adrs_t;
+    
+    
+    typedef enum
+    {
+        MAX7300_PORT_OUTPUT = 1,
+        MAX7300_PORT_INPUT,
+        MAX7300_PORT_INPUT_PULLUP
+    }max7300_port_type_t;
+        
+    
+    typedef enum
+    {
+        MAX7300_PORT_04 = 4,
+        MAX7300_PORT_05,
+        MAX7300_PORT_06,
+        MAX7300_PORT_07,
+        MAX7300_PORT_08,
+        MAX7300_PORT_09,
+        MAX7300_PORT_10,
+        MAX7300_PORT_11,
+        MAX7300_PORT_12,
+        MAX7300_PORT_13,
+        MAX7300_PORT_14,
+        MAX7300_PORT_15,
+        MAX7300_PORT_16,
+        MAX7300_PORT_17,
+        MAX7300_PORT_18,
+        MAX7300_PORT_19,
+        MAX7300_PORT_20,
+        MAX7300_PORT_21,
+        MAX7300_PORT_22,
+        MAX7300_PORT_23,
+        MAX7300_PORT_24,
+        MAX7300_PORT_25,
+        MAX7300_PORT_26,
+        MAX7300_PORT_27,
+        MAX7300_PORT_28,
+        MAX7300_PORT_29,
+        MAX7300_PORT_30,
+        MAX7300_PORT_31
+    }max7300_port_number_t;
+    
+    
+    typedef enum
+    {
+        MAX7300_NO_OP = 0,
+        MAX7300_CONFIGURATION = 4,
+        MAX7300_MASK = 6,
+        MAX7300_PORT_CONFIG_P07_P04 = 9,
+        MAX7300_PORT_CONFIG_P11_P8,
+        MAX7300_PORT_CONFIG_P15_P12,
+        MAX7300_PORT_CONFIG_P19_P16,
+        MAX7300_PORT_CONFIG_P23_P20,
+        MAX7300_PORT_CONFIG_P27_P24,
+        MAX7300_PORT_CONFIG_P31_P28,
+        MAX7300_PORT_04_DATA = 0x24,
+        MAX7300_PORT_05_DATA,
+        MAX7300_PORT_06_DATA,
+        MAX7300_PORT_07_DATA,
+        MAX7300_PORT_08_DATA,
+        MAX7300_PORT_09_DATA,
+        MAX7300_PORT_10_DATA,
+        MAX7300_PORT_11_DATA,
+        MAX7300_PORT_12_DATA,
+        MAX7300_PORT_13_DATA,
+        MAX7300_PORT_14_DATA,
+        MAX7300_PORT_15_DATA,
+        MAX7300_PORT_16_DATA,
+        MAX7300_PORT_17_DATA,
+        MAX7300_PORT_18_DATA,
+        MAX7300_PORT_19_DATA,
+        MAX7300_PORT_20_DATA,
+        MAX7300_PORT_21_DATA,
+        MAX7300_PORT_22_DATA,
+        MAX7300_PORT_23_DATA,
+        MAX7300_PORT_24_DATA,
+        MAX7300_PORT_25_DATA,
+        MAX7300_PORT_26_DATA,
+        MAX7300_PORT_27_DATA,
+        MAX7300_PORT_28_DATA,
+        MAX7300_PORT_29_DATA,
+        MAX7300_PORT_30_DATA,
+        MAX7300_PORT_31_DATA,
+        MAX7300_8_PORTS_P04_P11_DATA = 0x44,
+        MAX7300_8_PORTS_P12_P19_DATA = 0x4C,
+        MAX7300_8_PORTS_P20_P27_DATA = 0x54,
+        MAX7300_8_PORTS_P24_P31_DATA = 0x58
+    }max7300_registers_t;
+    
+    
+    /**********************************************************//**
+    * @brief Constructor for Max7300 Class.  
+    * 
+    * @details Allows user to use existing I2C object
+    *
+    * On Entry:
+    *     @param[in] i2c_bus - pointer to existing I2C object
+    *     @param[in] i2c_adrs - 7-bit slave address of MAX7300
+    *
+    * On Exit:
+    *    @return none
+    **************************************************************/
+    Max7300(I2C *i2c_bus, max7300_i2c_adrs_t i2c_adrs);
+    
+    
+    /**********************************************************//**
+    * @brief Constructor for Max7300 Class.  
+    * 
+    * @details Allows user to create a new I2C object if not 
+    *          already using one
+    *
+    * On Entry:
+    *     @param[in] sda - sda pin of I2C bus
+    *     @param[in] scl - scl pin of I2C bus
+    *     @param[in] i2c_adrs - 7-bit slave address of MAX7300
+    *
+    * On Exit:
+    *    @return none
+    **************************************************************/
+    Max7300(PinName sda, PinName scl, max7300_i2c_adrs_t i2c_adrs);
+    
+    
+    /**********************************************************//**
+    * @brief Default destructor for Max7300 Class.  
+    *
+    * @details Destroys I2C object if owner 
+    *
+    * On Entry:
+    *
+    * On Exit:
+    *    @return none
+    **************************************************************/
+    ~Max7300();
+    
+    
+    /**********************************************************//**
+    * @brief Enables MAX7300 GPIO Ports  
+    *
+    * @details Sets 'S' bit of configuration register
+    *
+    * On Entry:
+    *
+    * On Exit:
+    *    @return 0 on success, non-0 on failure
+    **************************************************************/
+    int16_t enable_ports(void);
+    
+    
+    /**********************************************************//**
+    * @brief Disables MAX7300 GPIO Ports 
+    *
+    * @details Clears 'S' bit of configuration register
+    *
+    * On Entry:
+    *
+    * On Exit:
+    *    @return 0 on success, non-0 on failure 
+    **************************************************************/
+    int16_t disable_ports(void);
+    
+    
+    /**********************************************************//**
+    * @brief Configures a single MAX7300 GPIO port
+    *
+    * @details  Configures MAX7300 GPIO port as either an output,
+    *           input, or input with pullup.
+    *
+    * On Entry:
+    *     @param[in] port_num - GPIO port to configure
+    *     @param[in] port_type - One of the following port types
+    *                            MAX7300_PORT_OUTPUT
+    *                            MAX7300_PORT_INPUT
+    *                            MAX7300_PORT_INPUT_PULLUP
+    *
+    * On Exit:
+    *    @return 0 on success, non-0 on failure
+    **************************************************************/
+    int16_t config_port(max7300_port_number_t port_num, max7300_port_type_t port_type);
+    
+    
+    /**********************************************************//**
+    * @brief Configure 4 MAX7300 GPIO ports
+    *
+    * @details  Allows user to configure 4 ports at a time
+    *
+    * On Entry:
+    *    @param[in] reg - One of the following 7 registers
+    *                     MAX7300_PORT_CONFIG_P7_P4,
+    *                     MAX7300_PORT_CONFIG_P11_P8,
+    *                     MAX7300_PORT_CONFIG_P15_P12,
+    *                     MAX7300_PORT_CONFIG_P19_P16,
+    *                     MAX7300_PORT_CONFIG_P23_P20,
+    *                     MAX7300_PORT_CONFIG_P27_P24,
+    *                     MAX7300_PORT_CONFIG_P31_P28
+    *
+    *    @param[in] data - Byte with each ports desired type with 
+    *                      the following format - xx|xx|xx|xx
+    *
+    * On Exit:
+    *    @return 0 on success, non-0 on failure
+    **************************************************************/
+    int16_t config_ports(max7300_registers_t reg, uint8_t data);
+    
+    
+    /**********************************************************//**
+    * @brief Read a single MAX7300 GPIO port
+    *
+    * @details
+    *
+    * On Entry:
+    *
+    * On Exit:
+    *    @return state of port, or  
+    **************************************************************/
+    int16_t read_port(max7300_port_number_t port_num);
+    
+    
+    /**********************************************************//**
+    * @brief Write a single MAX7300 GPIO port
+    *
+    * @details
+    *
+    * On Entry:
+    *
+    * On Exit:
+    *    @return none 
+    **************************************************************/
+    int16_t write_port(max7300_port_number_t port_num, uint8_t data);
+    
+    
+    /**********************************************************//**
+    * @brief Read 8 MAX7300 GPIO ports
+    *
+    * @details
+    *
+    * On Entry:
+    *
+    * On Exit:
+    *    @return none 
+    **************************************************************/
+    int16_t read_eight_ports(max7300_registers_t reg);
+    
+    
+    /**********************************************************//**
+    * @brief Write 8 MAX7300 GPIO ports
+    *
+    * @details
+    *
+    * On Entry:
+    *
+    * On Exit:
+    *    @return none 
+    **************************************************************/
+    int16_t write_eight_ports(max7300_registers_t reg, uint8_t data);
+    
+    
+    /**********************************************************//**
+    * @brief 
+    *
+    * @details
+    *
+    * On Entry:
+    *
+    * On Exit:
+    *    @return none 
+    **************************************************************/
+    
+    private:
+    
+    I2C *_p_i2c;
+    bool _i2c_owner;
+    uint8_t _w_adrs;
+    uint8_t _r_adrs;
+};
+#endif /* MAX7300_H*/
\ No newline at end of file