Device driver for AD converters MCP3426, MCP3427, and MCP3428.

mcp342x.h

Committer:
coisme
Date:
2016-04-29
Revision:
0:78c512aa4d18
Child:
1:bc877c37027c

File content as of revision 0:78c512aa4d18:

#ifndef __MCP342X_H__
#define __MCP342X_H__

#include "mbed.h"

class MCP342X
{
public:
    /**
     * Slave addresses.
     */
    typedef enum {
        SLAVE_ADDRESS_68H,     /**< When Adr0 pin = L and Adr1 pin = L, or Adr0 pin = float and Adr1 pin = float. */
        SLAVE_ADDRESS_69H,     /**< When Adr0 pin = L and Adr1 pin = float. */
        SLAVE_ADDRESS_6AH,     /**< When Adr0 pin = L and Adr1 pin = H. */
        SLAVE_ADDRESS_6CH,     /**< When Adr0 pin = H and Adr1 pin = L. */
        SLAVE_ADDRESS_6DH,     /**< When Adr0 pin = H and Adr1 pin = float. */
        SLAVE_ADDRESS_6EH,     /**< When Adr0 pin = H and Adr1 pin = H. */
        SLAVE_ADDRESS_6BH,     /**< When Adr0 pin = float and Adr1 pin = L. */
        SLAVE_ADDRESS_6FH,     /**< When Adr0 pin = float and Adr1 pin = H. */
    } SlaveAddress;
    
    /**
     * Status of function. 
     */
    typedef enum {
        SUCCESS,               /**< The function processed successfully. */
        ERROR_I2C_READ,        /**< Error related to I2C read. */
        ERROR_I2C_WRITE,       /**< Error related to I2C write. */
        ERROR,                 /**< General Error */
    } Status;

    /**
     * Conversion mode setting.
     */
    typedef enum {
        CONTINUOUS,            /**< Continuous conversion mode. Default. */
        ONE_SHOT,              /**< One-shot conversion mode. */
    } ConversionMode;

    /**
     * Data ready status.
     */
    typedef enum {
        DATA_NOT_UPDATED,       /**< Output register has not been updated. */
        DATA_UPDATED,           /**< Output register has been updated with the latest conversion result. */
    } DataStatus;

    /**
     * Measurement trigger command.
     */
    typedef enum {
        TRIGGER,              /**< Initiate a new conversion. */
        NONE,                 /**< No effect. */
    } MeasurementTrigger;
    
    /**
     * Sample rate and resolution setting.
     */
    typedef enum {
        SAMPLE_240HZ_12BIT,       /**< 240 sample per second with 12 bit data. Default. */
        SAMPLE_60HZ_14BIT,        /**< 60 sample per second with 14 bit data. */
        SAMPLE_15HZ_16BIT,        /**< 15 sample per second with 16 bit data. */
    } SampleSetting;

    
    /**
     * ADC channel selection.
     */
    typedef enum {
        ADC_CH1,                 /**< Channel 1, default. */
        ADC_CH2,                 /**< Channel 2 */
        ADC_CH3,                 /**< Channel 3, MCP3428 only, treated as channel 1 by the MCP3426/MCP3427. */
        ADC_CH4,                 /**< Channel 4, MCP3428 only, treated as channel 2 by the MCP3426/MCP3427. */
    } AdcChannel;
    
    /**
     * Programmable Gain Amplifier setting.
     */
    typedef enum {
        PGA_1X,                  /**< Gain 1x, Default. */
        PGA_2X,                  /**< Gain 2x. */
        PGA_4X,                  /**< Gain 4x. */
        PGA_8X,                  /**< Gain 8x. */
    } PgaSetting;
        
    /**
     * Constructor.
     *
     * @param conn instance of I2C
     * @param addr slave address of the device
     */
    MCP342X(I2C *conn, SlaveAddress addr);

    /**
     * Sets a ADC channel.
     * @param ch ADC channel which to be the input.
     * @return SUCCESS when succeeded. Other value will be returned when error.
     */
    Status setChannel(AdcChannel ch);
    
    /**
     * Gets the current selected ADC channel.
     * @return ADC channel currently set.
     */
    AdcChannel getChannel();
    
    /**
     * Sets a conversion mode.
     * @param mode Conversion mode which to be set.
     * @return SUCCESS when succeeded. Other value will be returned when error.
     */
    Status setConversionMode(ConversionMode mode);

    /**
     * Gets the current conversion mode.
     * @return Current conversion mode.
     */
    ConversionMode getConversionMode();

    /**
     * Sets sample setting.
     * @param s Sample setting to be set.
     * @return SUCCESS when succeeded. Other value will be returned when error.
     */
    Status setSampleSetting(SampleSetting s);

    /**
     * Gets the current sample setting.
     * @return Current sample setting.
     */
    SampleSetting getSampleSetting();
    
    /**
     * Sets gain of Programmable Gain Amplifier (PGA).
     * @param s PGA seeting to be set.
     * @return SUCCESS when succeeded. Other value will be returned when error.
     */
    Status setPgaSetting(PgaSetting s);
    
    /**
     * Gets the current PGA setting.
     * @return Current PGA setting.
     */
    PgaSetting getPgaSetting();

    /**
     * Checks availability of new data.
     * @return Returns true if new data is available. Returns false if no new data is available.
     */
    bool isNewDataAvailable();    

    /**
     * Gets the AD value.
     * @return AD value.
     */
    int16_t getValue();
    
    /**
     * Trigger AD conversion.
     * @return SUCCESS when succeeded. Other value will be returned when error.
     */
    Status trigger();
    
    /**
     * Gets the data conversion time.
     * @return Conversion time based on the current sample setting in milli-second unit.
     */
     float getDataConversionTime();
    
private:
    typedef struct {
        DataStatus      dataStatus;
        ConversionMode  conversionMode;
        SampleSetting   sampleSetting;
        AdcChannel      adcChannel;
        PGASetting      pgaSetting;
    } Config;


    I2C *connection;        /**< Pointer to an I2C object. */
    uint8_t slaveAddress;   /**< Slave address. */
    uint8_t dataBuffer[3];  /**< Data buffer including configuration register. */
    uint8_t Config config;  /**< Stores the latest configuration. */

    /**
     * Reads the data registers including the configuration register.
     * @param val Pointer to the buffer which stores ADC value.
     * @param currentConfig Pointer to the structure which stores the current configuration.
     * @return SUCCESS when succeeded. Other value will be returned when error.
     */
    Status readData(int16_t *val, Config *currentConfig);
    
    /**
     * Sets the configuration register.
     * @param Configuration to be set.
     * @return SUCCESS when succeeded. Other value will be returned when error.
     */
    Status setConfig(const Config *config);
};

#endif