Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External ADC Driver Implementation #20

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
127 changes: 127 additions & 0 deletions Node/Common/drivers/inc/adc_ADS114S0.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#ifndef ADC_ADS114S0_H
#define ADC_ADS114S0_H

#include <SPI.h>

enum Register : uint8_t
{
STATUS = 0x01,
INPMUX = 0x02,
PGA = 0x03,
DATARATE = 0x04,
REF = 0x05,
IDACMAG = 0x06,
IDACMUX = 0x07,
VBIAS = 0x08,
SYS = 0x09,
OFCAL0 = 0x0B,
OFCAL1 = 0x0C,
FSCAL0 = 0x0E,
FSCAL1 = 0x0F,
GPIODAT = 0x10,
GPIOCON = 0x11
};

enum InputMux : uint8_t
{
MUX_SINGLE_0 = 0x0C, // Single-ended AIN0
MUX_SINGLE_1 = 0x1C, // Single-ended AIN1
MUX_SINGLE_2 = 0x2C, // Single-ended AIN2
MUX_SINGLE_3 = 0x3C, // Single-ended AIN3
MUX_SINGLE_4 = 0x4C, // Single-ended AIN4
MUX_SINGLE_5 = 0x5C, // Single-ended AIN5
MUX_SINGLE_6 = 0x6C, // Single-ended AIN6
MUX_SINGLE_7 = 0x7C, // Single-ended AIN7
MUX_SINGLE_8 = 0x8C, // Single-ended AIN8
MUX_SINGLE_9 = 0x9C, // Single-ended AIN9
MUX_SINGLE_10 = 0xAC, // Single-ended AIN10
MUX_SINGLE_11 = 0xBC // Single-ended AIN11
};

enum PGAGain : uint8_t
{
GAIN_DISABLE = 0x00, // Default
GAIN_1 = 0x08,
GAIN_2 = 0x09,
GAIN_4 = 0x0A,
GAIN_8 = 0x0B,
GAIN_16 = 0x0C,
GAIN_32 = 0x0D,
GAIN_64 = 0x0E,
GAIN_128 = 0x0F
};

enum DataRate : uint8_t
{
SPS_2_5 = 0x10, // 2.5 Samples Per Second
SPS_5 = 0x11, // 5 Samples Per Second
SPS_10 = 0x12, // 10 Samples Per Second
SPS_16_6 = 0x13, // 16.6 Samples Per Second
SPS_20 = 0x14, // 20 Samples Per Second (default)
SPS_50 = 0x15, // 50 Samples Per Second
SPS_60 = 0x16, // 60 Samples Per Second
SPS_100 = 0x17, // 100 Samples Per Second
SPS_200 = 0x18, // 200 Samples Per Second
SPS_400 = 0x19, // 400 Samples Per Second
SPS_800 = 0x1A, // 800 Samples Per Second
SPS_1000 = 0x1B, // 1000 Samples Per Second
SPS_2000 = 0x1C, // 2000 Samples Per Second
SPS_4000 = 0x1D // 4000 Samples Per Second
};

enum ConversionMode : uint8_t
{
SINGLE = 0x10,
CONTINUOUS = 0x30
};

enum ClockSource : uint8_t
{
INTERNAL_CLK = 0x10,
EXTERNAL_CLK = 0x50
};

enum Command : uint8_t
{
WAKEUP = 0x02,
POWERDOWN = 0x04,
RESET = 0x06,
START = 0x08,
STOP = 0x0A,
SYOCAL = 0x16, // System offset calibration
SYGCAL = 0x17, // System gain calibration
SFOCAL = 0x19, // Self offset calibration
RDATA = 0x12
};

constexpr uint16_t RREG = 0x2000;
constexpr uint16_t WREG = 0x3000;
constexpr uint8_t DEFAULTS[] = { 0x80, 0x01, 0x00, 0x14, 0x10, 0x00,
0xFF, 0x00, 0x10, 0x00, 0x00, 0x00,
0x00, 0x00, 0x40, 0x00, 0x00 };

class ADS114S0
{
public:
ADS114S0(const uint8_t CS);
void beginSPI();
void endSPI();
void reset();
void writeRegister(const Register, const uint8_t);
void writeRegisters(const Register, const uint8_t[], const uint8_t);
uint8_t readRegister(const Register);
void readRegisters(const Register, uint8_t[], const uint8_t);
uint16_t readData();
void setMux(const InputMux);
void setPGAGain(const PGAGain);
void setMode(const DataRate, const ClockSource, const ConversionMode);
void setConversionMode(const ConversionMode);
void startConversion();
void stopConversion();

static const uint32_t SPI_CLOCK = 20000000;
private:
const uint8_t m_CS;
};

#endif
1 change: 0 additions & 1 deletion Node/Common/drivers/inc/test.h

This file was deleted.

118 changes: 118 additions & 0 deletions Node/Common/drivers/src/adc_ADS114S0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "adc_ADS114S0.h"

ADS114S0::ADS114S0(const uint8_t CS)
: m_CS(CS)
{
SPI.begin();

pinMode(m_CS, OUTPUT);
endSPI();
}

void ADS114S0::beginSPI()
{
SPI.beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE1));
digitalWrite(m_CS, LOW);
}

void ADS114S0::endSPI()
{
digitalWrite(m_CS, HIGH);
SPI.endTransaction();
}

void ADS114S0::reset()
{
beginSPI();
SPI.transfer(RESET);
endSPI();

delay(10);

writeRegisters(STATUS, DEFAULTS, 16);
}

void ADS114S0::writeRegister(const Register reg, const uint8_t value)
{
beginSPI();
SPI.transfer16(WREG | reg<<16);
SPI.transfer(value);
endSPI();
}

void ADS114S0::writeRegisters(const Register reg, const uint8_t values[], const uint8_t num)
{
beginSPI();
SPI.transfer16(WREG | reg<<16 | (num - 1)<<8);
for (uint8_t i = 0; i < num; i++)
{
SPI.transfer(values[i]);
}
endSPI();
}

uint8_t ADS114S0::readRegister(const Register reg)
{
beginSPI();
SPI.transfer16(RREG | reg<<16);
uint8_t value = SPI.transfer(0x00);
endSPI();

return value;
}

void ADS114S0::readRegisters(const Register reg, uint8_t values[], const uint8_t num)
{
beginSPI();
SPI.transfer16(RREG | reg<<16 | (num - 1)<<8);
for (uint8_t i = 0; i < num; i++)
{
values[i] = SPI.transfer(0x00);
}
endSPI();
}

void ADS114S0::setMux(const InputMux mux)
{
writeRegister(INPMUX, mux);
}

void ADS114S0::setPGAGain(const PGAGain gain)
{
writeRegister(PGA, gain);
}

void ADS114S0::setMode(const DataRate rate, const ClockSource clock, const ConversionMode mode)
{
writeRegister(DATARATE, rate | clock | mode);
}

void ADS114S0::setConversionMode(const ConversionMode mode)
{
uint8_t currentSettings = readRegister(DATARATE);
writeRegister(DATARATE, currentSettings | mode);
}

void ADS114S0::startConversion()
{
beginSPI();
SPI.transfer(START);
endSPI();
}

void ADS114S0::stopConversion()
{
beginSPI();
SPI.transfer(STOP);
endSPI();
}

uint16_t ADS114S0::readData()
{
beginSPI();
SPI.transfer(RDATA);
uint16_t data = SPI.transfer16(0x0000);
endSPI();

return data;
}
2 changes: 0 additions & 2 deletions Node/Common/drivers/src/test.cpp

This file was deleted.