Library for the MAX11300

Dependents:   MAX_IOT_KIT MAX_IOT_KIT

Fork of MAX11300 by Maxim Integrated

The MAX11300/01/11/12 are configurable mixed signal integrated circuits. The MAX11300/11 offer a SPI interface while the MAX11301/12 offer an I2C interface. The MAX11300/01 are 20 port devices while the MAX11311/12 are 12 port devices.

This library supports the family of parts by providing member functions that can manipulate the GPIO, ADC, DAC, and analog switches of the device, after it has been configured. For configuration of the device, this library requires a header file that can be generated by the MAX11300/01/11/12 Configuration Software. The configuration software can be found at the following link.

https://www.maximintegrated.com/en/products/analog/data-converters/analog-to-digital-converters/MAX11300.html/tb_tab2

Include the generated MAX113XXHex.h file into your project and update the #include in MAX113XX_Pixi.h.

MAX113XX_Pixi.cpp

Committer:
j3
Date:
2017-05-08
Revision:
11:31e7ca030b8f
Parent:
10:6efe114ef882
Child:
12:8054ee101bad

File content as of revision 11:31e7ca030b8f:

/**********************************************************************
* Copyright (C) 2016 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 "MAX113XX_Pixi.h"


//20 port devices
#if defined(_MAX11300_DESIGNVALUE_H_) || defined(_MAX11301_DESIGNVALUE_H_) 
static const uint16_t portConfigDesignVals[20] = {
    port_cfg_00_DESIGNVALUE,
    port_cfg_01_DESIGNVALUE,
    port_cfg_02_DESIGNVALUE,
    port_cfg_03_DESIGNVALUE,
    port_cfg_04_DESIGNVALUE,
    port_cfg_05_DESIGNVALUE,
    port_cfg_06_DESIGNVALUE,
    port_cfg_07_DESIGNVALUE,
    port_cfg_08_DESIGNVALUE,
    port_cfg_09_DESIGNVALUE,
    port_cfg_10_DESIGNVALUE,
    port_cfg_11_DESIGNVALUE,
    port_cfg_12_DESIGNVALUE,
    port_cfg_13_DESIGNVALUE,
    port_cfg_14_DESIGNVALUE,
    port_cfg_15_DESIGNVALUE,
    port_cfg_16_DESIGNVALUE,
    port_cfg_17_DESIGNVALUE,
    port_cfg_18_DESIGNVALUE,
    port_cfg_19_DESIGNVALUE};
    
static const uint16_t deviceDesignVals[15] = {
    device_control_DESIGNVALUE,
    interrupt_mask_DESIGNVALUE,
    gpi_irqmode_7_to_0_DESIGNVALUE,
    gpi_irqmode_15_to_8_DESIGNVALUE,
    gpi_irqmode_19_to_16_DESIGNVALUE,
    0, //reserved
    dac_preset_data_1_DESIGNVALUE,
    dac_preset_data_2_DESIGNVALUE,
    tmp_mon_cfg_DESIGNVALUE,
    tmp_mon_int_hi_thresh_DESIGNVALUE,
    tmp_mon_int_lo_thresh_DESIGNVALUE,
    tmp_mon_ext1_hi_thresh_DESIGNVALUE,
    tmp_mon_ext1_lo_thresh_DESIGNVALUE,
    tmp_mon_ext2_hi_thresh_DESIGNVALUE,
    tmp_mon_ext2_lo_thresh_DESIGNVALUE};  
#endif

//12 port devices...
#if defined(_MAX11311_DESIGNVALUE_H_) || defined(_MAX11312_DESIGNVALUE_H_)
static const uint16_t portConfigDesignVals[12] = {
    port_cfg_p0_DESIGNVALUE,
    port_cfg_p1_DESIGNVALUE,
    port_cfg_p2_DESIGNVALUE,
    port_cfg_p3_DESIGNVALUE,
    port_cfg_p4_DESIGNVALUE,
    port_cfg_p5_DESIGNVALUE,
    port_cfg_p6_DESIGNVALUE,
    port_cfg_p7_DESIGNVALUE,
    port_cfg_p8_DESIGNVALUE,
    port_cfg_p9_DESIGNVALUE,
    port_cfg_p10_DESIGNVALUE,
    port_cfg_p11_DESIGNVALUE};
#endif
    
    
//************************** Base Class member fxs *****************************
MAX113XX_Pixi::MAX113XX_Pixi(Device_e device, PinName cnvt):
m_device(device), m_cnvt(cnvt, 1)
{
}


//*********************************************************************
MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::gpioWrite(Ports_e port, 
                                                    const uint8_t state)
{
    uint16_t temp;
    uint16_t port_mask;
    
    MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
    
    if(m_device == MAX11300 || m_device == MAX11301) //20 port device
    {
        if(((portConfigDesignVals[port] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_3)
        {
            if(port < MAX113XX_Pixi::PORT16)
            {
                port_mask = (1 << port);
                temp = readRegister(gpo_data_15_to_0);
                if(state & 0x01)
                {
                    temp |= port_mask;
                }
                else
                {
                    temp &= ~port_mask;
                }
                writeRegister(gpo_data_15_to_0, temp);
            }
            else
            {
                port_mask = (1 << (port - MAX113XX_Pixi::PORT16));
                temp = readRegister(gpo_data_19_to_16);
                if(state & 0x01)
                {
                    temp |= port_mask;
                }
                else
                {
                    temp &= ~port_mask;
                }
                writeRegister(gpo_data_19_to_16, temp);
            }
            
            result = MAX113XX_Pixi::Success;
        }
    }
    else //12 port device
    {
    }
    
    return result;
}

//*********************************************************************
MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::gpioRead(Ports_e port, uint8_t &state)
{
    MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
    
    if(m_device == MAX11300 || m_device == MAX11301) //20 port device
    {
        if(((portConfigDesignVals[port] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_1)
        {
            if(port < MAX113XX_Pixi::PORT16)
            {
                state = (readRegister(gpi_data_15_to_0) >> port);
            }
            else
            {
                state = (readRegister(gpi_data_19_to_16) >> (port - MAX113XX_Pixi::PORT16));
            }
            
            result = MAX113XX_Pixi::Success;
        }
    }
    else //12 port device
    {
    }
    
    return result;
}

//*********************************************************************
MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::singleEndedADCRead(Ports_e port, uint16_t &data)
{
    MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
    
    if(m_device == MAX11300 || m_device == MAX11301) //20 port device
    {
        if(((portConfigDesignVals[port] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_7)
        {
            uint8_t num_samples = ((portConfigDesignVals[port] & port_cfg_00_funcprm_nsamples) >> 5);
            num_samples = (1 << num_samples);
            
            while(num_samples--)
            {
                m_cnvt = 0;
                wait_us(1);
                m_cnvt = 1;
                wait_us(100);
            }
            data = readRegister((adc_data_port_00 + port));
            
            result = MAX113XX_Pixi::Success;
        }
    }
    else //12 port device
    {
    }
    
    return result;
}

//*********************************************************************
MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::singleEndedDACWrite(Ports_e port, 
                                                       const uint16_t data)
{
    MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
    
    if(m_device == MAX11300 || m_device == MAX11301) //20 port device
    {
        if(((portConfigDesignVals[port] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_5)
        {
            writeRegister((dac_data_port_00 + port) , data);
            result = MAX113XX_Pixi::Success;
        }
    }
    else //12 port device
    {
    }
    
    return result;
}

//*********************************************************************
void MAX113XX_Pixi::dumpPixiMemory(Serial &ser, MAX113XX_Pixi &pixi)
{
    uint16_t mem[0x74];
    
    pixi.blockRead(dev_id, mem, 0x74);
    for(uint8_t idx = 0; idx < 0x74; idx++)
    {
        ser.printf("Register 0x%2x = 0x%4x\r\n", idx, mem[idx]);
    }
    ser.printf("\r\n");
}


/// SPI first byte when writing MAX11300/11 
//(7-bit address in bits 0x7E; LSB=0 for write)
#define MAX113XXAddr_SPI_Write(RegAddr) ( (RegAddr << 1)     )

/// SPI first byte when reading MAX11300/11 
//(7-bit address in bits 0x7E; LSB=1 for read)
#define MAX113XXAddr_SPI_Read(RegAddr)  ( (RegAddr << 1) | 1 )

//*************************** SPI Implementation ******************************
MAX113XX_SPI::MAX113XX_SPI(SPI & spiBus, PinName cs, 
MAX113XX_Pixi::Device_e device, PinName cnvt):
MAX113XX_Pixi(device, cnvt), m_spiBus(spiBus), m_cs(cs, 1)
{
    if((m_device == MAX11300) || (m_device == MAX11301)) //20 port device
    {
        blockWrite(device_control, deviceDesignVals, 15);
        blockWrite(port_cfg_00, portConfigDesignVals, 20);
        wait(0.1);
    }
    else //12 port device
    {
    }
}

//*********************************************************************
MAX113XX_SPI::~MAX113XX_SPI()
{
    //empty block
}

//*********************************************************************
void MAX113XX_SPI::writeRegister(uint8_t reg, const uint16_t data)
{
    m_cs = 0;
    m_spiBus.write(MAX113XXAddr_SPI_Write(reg));
    m_spiBus.write(((0xFF00 & data) >> 8));
    m_spiBus.write((0x00FF & data));
    m_cs = 1;
}

//*********************************************************************    
uint16_t MAX113XX_SPI::readRegister(uint8_t reg)
{
    uint16_t rtn_val = 0;
    
    m_cs = 0;
    m_spiBus.write(MAX113XXAddr_SPI_Read(reg));
    rtn_val |= (m_spiBus.write(0xFF) << 8);
    rtn_val |= m_spiBus.write(0xFF);
    m_cs = 1;
    
    return rtn_val;
}

//*********************************************************************    
void MAX113XX_SPI::blockWrite(uint8_t reg, const uint16_t *data, 
                              const uint8_t num_reg)
{
    m_cs = 0;
    m_spiBus.write(MAX113XXAddr_SPI_Write(reg));
    for(uint8_t idx = 0; idx < num_reg; idx++)
    {
        m_spiBus.write(((0xFF00 & data[idx]) >> 8));
        m_spiBus.write((0x00FF & data[idx]));
    }
    m_cs = 1;
}

//*********************************************************************        
void MAX113XX_SPI::blockRead(uint8_t reg, uint16_t *data, const uint8_t num_reg)
{
    uint16_t temp;
    m_cs = 0;
    m_spiBus.write(MAX113XXAddr_SPI_Read(reg));
    for(uint8_t idx = 0; idx < num_reg; idx ++)
    {
        temp = 0;
        temp |= (m_spiBus.write(0xFF) << 8);
        temp |= m_spiBus.write(0xFF);
        data[idx] = temp;
    }
    m_cs = 1;
}


//*************************** I2C Implementation ******************************
MAX113XX_I2C::MAX113XX_I2C(I2C &i2cBus, MAX113XX_Pixi::Device_e device, PinName cnvt):
MAX113XX_Pixi(device, cnvt), m_i2cBus(i2cBus)
{
    if((m_device == MAX11300) || (m_device == MAX11301)) //20 port device
    {
        blockWrite(device_control, deviceDesignVals, 15);
        blockWrite(port_cfg_00, portConfigDesignVals, 20);
        wait(0.1);
    }
    else //12 port device
    {
    }
}

//*********************************************************************
MAX113XX_I2C::~MAX113XX_I2C()
{
    //empty block
}

//*********************************************************************
void MAX113XX_I2C::writeRegister(uint8_t reg, const uint16_t data)
{
   
}

//*********************************************************************    
uint16_t MAX113XX_I2C::readRegister(uint8_t reg)
{
    uint16_t rtn_val = 0;
    
    return rtn_val;
}

//*********************************************************************    
void MAX113XX_I2C::blockWrite(uint8_t reg, const uint16_t *data, 
                               const uint8_t num_reg)
{
    
}

//*********************************************************************        
void MAX113XX_I2C::blockRead(uint8_t reg, uint16_t *data, const uint8_t num_reg)
{
    
}