Library for the MAX11300

MAX113XX_Pixi.cpp

Committer:
j3
Date:
2017-05-05
Revision:
7:8669a53acd0d
Child:
9:094df3de3616

File content as of revision 7:8669a53acd0d:

/**********************************************************************
* 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"

static const uint16_t port_config_design_vals[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};
    
//************************** Base Class member fxs *****************************
MAX113XX_Pixi::MAX113XX_Pixi(PinName cnvt):
m_cnvt(cnvt, 1)
{
}   

//*********************************************************************
MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::gpioWrite(MAX113XX_Ports_e port, 
                                           const uint8_t state)
{
    MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
    uint16_t temp;
    uint16_t port_mask;
    
    if(((port_config_design_vals[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;
    }
    
    return result;
}

//*********************************************************************
MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::gpioRead(MAX113XX_Ports_e port, uint8_t &state)
{
    MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
    
    if(((port_config_design_vals[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;
    }
    
    return result;
}

//*********************************************************************
MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::singleEndedADCRead(MAX113XX_Ports_e port, uint16_t &data)
{
    MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
    
    if(((port_config_design_vals[port] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_7)
    {
        uint8_t num_samples = ((port_config_design_vals[port] & port_cfg_00_funcprm_nsamples) >> 5);
        num_samples = (1 << num_samples);
        
        while(num_samples--)
        {
            this->m_cnvt = 0;
            wait_us(1);
            this->m_cnvt = 1;
            wait_us(100);
        }
        data = readRegister(static_cast<MAX11300RegAddress_t>(adc_data_port_00 + port));
        
        result = MAX113XX_Pixi::Success;
    }
    
    return result;
}

//*********************************************************************
MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::singleEndedDACWrite(MAX113XX_Ports_e port, 
                                                       const uint16_t data)
{
    MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
    
    if(((port_config_design_vals[port] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_5)
    {
        writeRegister(static_cast<MAX11300RegAddress_t>(dac_data_port_00 + port) , data);
        result = MAX113XX_Pixi::Success;
    }
    
    return result;
}

//*********************************************************************
void MAX113XX_Pixi::dumpPixiMemory(Serial &ser, MAX113XX_Pixi &pixi)
{
    uint16_t mem[256];
    
    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 Implementation ******************************
MAX113XX_SPI::MAX113XX_SPI(SPI & spiBus, PinName cs, PinName cnvt):
MAX113XX_Pixi(cnvt), m_spiBus(spiBus), m_cs(cs, 1)
{
    
}

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

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

//*********************************************************************    
uint16_t MAX113XX_SPI::readRegister(MAX11300RegAddress_t reg)
{
    uint16_t rtn_val = 0;
    
    m_cs = 0;
    m_spiBus.write(MAX11300Addr_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(MAX11300RegAddress_t reg, const uint16_t *data, 
                              const uint8_t num_reg)
{
    for(uint8_t idx = 0; idx < num_reg; idx++)
    {
        writeRegister(static_cast<MAX11300RegAddress_t>(reg + idx), data[idx]);
    }
}

//*********************************************************************        
void MAX113XX_SPI::blockRead(MAX11300RegAddress_t reg, uint16_t *data, 
                             const uint8_t num_reg)
{
    for(uint8_t idx = 0; idx < num_reg; idx++)
    {
        data[idx] = readRegister(static_cast<MAX11300RegAddress_t>(reg + idx));
    }
}


//*************************** I2C Implementation ******************************
MAX113XX_I2C::MAX113XX_I2C(I2C &i2cBus, PinName cnvt):
MAX113XX_Pixi(cnvt), m_i2cBus(i2cBus)
{
    
}

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

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

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

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

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