Mcp320x
Data Structures | Public Types | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes
MCP320x< ChannelType > Class Template Reference

#include <Mcp320x.h>

Data Structures

union  SpiData
 

Public Types

using Channel = ChannelType
 

Public Member Functions

 MCP320x (uint16_t vref, uint8_t csPin, SPIClass *spi)
 
 MCP320x (uint16_t vref, uint8_t csPin)
 
void calibrate (Channel ch)
 
uint16_t read (Channel ch) const
 
template<typename T , size_t N>
void read (Channel ch, T(&data)[N]) const
 
template<typename T , size_t N>
void read (Channel ch, T(&data)[N], uint32_t splFreq)
 
template<typename T , size_t N, typename Predicate >
void read_if (Channel ch, T(&data)[N], Predicate p) const
 
template<typename T , size_t N, typename Predicate >
void read_if (Channel ch, T(&data)[N], uint32_t splFreq, Predicate p)
 
template<typename T >
void readn (Channel ch, T *data, uint16_t num) const
 
template<typename T >
void readn (Channel ch, T *data, uint16_t num, uint32_t splFreq)
 
template<typename T , typename Predicate >
void readn_if (Channel ch, T *data, uint16_t num, Predicate p) const
 
template<typename T , typename Predicate >
void readn_if (Channel ch, T *data, uint16_t num, uint32_t splFreq, Predicate p)
 
uint32_t testSplSpeed (Channel ch) const
 
uint32_t testSplSpeed (Channel ch, uint16_t num) const
 
uint32_t testSplSpeed (Channel ch, uint16_t num, uint32_t splFreq)
 
uint16_t toAnalog (uint16_t raw) const
 
uint16_t toDigital (uint16_t val) const
 
uint16_t getVref () const
 
uint16_t getAnalogRes () const
 

Static Public Attributes

static const uint8_t kResBits = 12
 
static const uint16_t kRes = (1 << kResBits)
 

Private Types

template<typename >
using Command = SpiData
 

Private Member Functions

uint16_t getSplDelay (Channel ch, uint32_t splFreq)
 
uint16_t execute (Command< Channel > cmd) const
 
template<typename T >
void execute (Command< Channel > cmd, T *data, uint16_t num) const
 
template<typename T >
void execute (Command< Channel > cmd, T *data, uint16_t num, uint16_t delay) const
 
uint16_t transfer () const
 
uint16_t transfer (SpiData cmd) const
 

Static Private Member Functions

static Command< ChannelcreateCmd (Channel ch)
 

Private Attributes

uint16_t mVref
 
uint8_t mCsPin
 
uint32_t mSplSpeed
 
SPIClass * mSpi
 

Detailed Description

template<typename ChannelType>
class MCP320x< ChannelType >

Definition at line 115 of file Mcp320x.h.

Member Typedef Documentation

template<typename ChannelType >
using MCP320x< ChannelType >::Channel = ChannelType

ADC Channel configuration.

Definition at line 125 of file Mcp320x.h.

template<typename ChannelType >
template<typename >
using MCP320x< ChannelType >::Command = SpiData
private

SPI command for a specific channel type.

Definition at line 375 of file Mcp320x.h.

Constructor & Destructor Documentation

template<typename T >
MCP320x< T >::MCP320x ( uint16_t  vref,
uint8_t  csPin,
SPIClass *  spi 
)

Initiates a MCP320x object. The chip select pin must be already configured as output.

Parameters
[in]vrefADC reference voltage in mV.
[in]csPinpin number to use for chip select.
[in]spireference to the SPI interface to use.

Definition at line 17 of file Mcp320x.cpp.

template<typename T >
MCP320x< T >::MCP320x ( uint16_t  vref,
uint8_t  csPin 
)

Initiates a MCP320x object. The chip select pin must be already configured as output. The default SPI interface will be used for communication.

Parameters
[in]vrefthe ADC reference voltage in mV.
[in]csPinthe pin number to use for chip select.

Definition at line 24 of file Mcp320x.cpp.

Member Function Documentation

template<typename T >
void MCP320x< T >::calibrate ( Channel  ch)

Calibrates read timing using the supplied channel. A calibration should be performed after evey SPI frequency changes or other events that could have an impact on the sampling speed. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chdefines the channel to use for calibration.

Definition at line 28 of file Mcp320x.cpp.

References MCP320x< ChannelType >::mSplSpeed, and MCP320x< ChannelType >::testSplSpeed().

Referenced by MCP320x< ChannelType >::getSplDelay().

MCP3208::Command< MCP3208Ch > MCP3208::createCmd ( Channel  ch)
staticprivate

Creates a command from the supplied channel.

Parameters
[in]chthe channel to create the command for.
Returns
the SPI data.

Definition at line 120 of file Mcp320x.cpp.

Referenced by MCP320x< ChannelType >::read(), and MCP320x< ChannelType >::testSplSpeed().

uint16_t MCP3208::execute ( Command< Channel cmd) const
private

Executes the supplied command.

Parameters
[in]cmdthe command to execute.
Returns
the ADC value from the SPI response.

Definition at line 163 of file Mcp320x.cpp.

References MCP320x< ChannelType >::transfer().

Referenced by MCP320x< ChannelType >::read(), and MCP320x< ChannelType >::testSplSpeed().

template<typename ChannelType >
template<typename T >
void MCP320x< ChannelType >::execute ( Command< Channel cmd,
T *  data,
uint16_t  num 
) const
inlineprivate

Executes the supplied command for the requested number of samples.

Parameters
[in]cmdthe command to execute.
[out]dataarray to store the values.
[in]numnumber of reads. The data array needs to be at least that size.

Definition at line 407 of file Mcp320x.h.

template<typename ChannelType >
template<typename T >
void MCP320x< ChannelType >::execute ( Command< Channel cmd,
T *  data,
uint16_t  num,
uint16_t  delay 
) const
inlineprivate

Executes the supplied command for the requested number of samples with delay between reads.

Parameters
[in]cmdthe command to execute.
[out]dataarray to store the values.
[in]numnumber of reads. The data array needs to be at least that size.
[in]delayin us.

Definition at line 423 of file Mcp320x.h.

template<typename T >
uint16_t MCP320x< T >::getAnalogRes ( ) const

Returns the analog resolution in µV based on the defined reference voltage.

Returns
the analog resolution in µV.

Definition at line 100 of file Mcp320x.cpp.

References MCP320x< ChannelType >::kRes, and MCP320x< ChannelType >::mVref.

template<typename T >
uint16_t MCP320x< T >::getSplDelay ( Channel  ch,
uint32_t  splFreq 
)
private

Returns the required delay for the requested sample rate.

Parameters
[in]chthe channel to create the command for.
Returns
the delay in us.

Definition at line 106 of file Mcp320x.cpp.

References MCP320x< ChannelType >::calibrate(), div_round, and MCP320x< ChannelType >::mSplSpeed.

Referenced by MCP320x< ChannelType >::testSplSpeed().

template<typename T >
uint16_t MCP320x< T >::getVref ( ) const

Returns the reference voltage.

Returns
the configured reference voltage in mV.

Definition at line 94 of file Mcp320x.cpp.

References MCP320x< ChannelType >::mVref.

template<typename T >
uint16_t MCP320x< T >::read ( Channel  ch) const

Reads the supplied channel. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chdefines the channel to read from.
Returns
the converted raw value.

Definition at line 34 of file Mcp320x.cpp.

References MCP320x< ChannelType >::createCmd(), and MCP320x< ChannelType >::execute().

template<typename ChannelType >
template<typename T , size_t N>
void MCP320x< ChannelType >::read ( Channel  ch,
T(&)  data[N] 
) const
inline

Reads the supplied channel and stores the data in the supplied data array. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chdefines the channel to read from.
[out]dataarray to store the values.

Definition at line 171 of file Mcp320x.h.

template<typename ChannelType >
template<typename T , size_t N>
void MCP320x< ChannelType >::read ( Channel  ch,
T(&)  data[N],
uint32_t  splFreq 
)
inline

Reads the supplied channel limited to the specified frequency and stores the data in the supplied data array. The sample rate limit is software controlled, and has a low precision. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chdefines the channel to read from.
[out]dataarray to store the values.
[in]splFreqsample frequency limit in hz.

Definition at line 187 of file Mcp320x.h.

template<typename ChannelType >
template<typename T , size_t N, typename Predicate >
void MCP320x< ChannelType >::read_if ( Channel  ch,
T(&)  data[N],
Predicate  p 
) const
inline

Reads the supplied channel and stores the data in the supplied data array after the predicate is true. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chdefines the channel to read from.
[out]dataarray to store the values.
[in]ppredicate funtion to control sampling start.

Definition at line 201 of file Mcp320x.h.

template<typename ChannelType >
template<typename T , size_t N, typename Predicate >
void MCP320x< ChannelType >::read_if ( Channel  ch,
T(&)  data[N],
uint32_t  splFreq,
Predicate  p 
)
inline

Reads the supplied channel limited to the specified frequency and stores the data in the supplied data array after the predicate is true. The sample rate limit is software controlled, and has a low precision. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chdefines the channel to read from.
[out]dataarray to store the values.
[in]splFreqsample frequency limit in hz.
[in]ppredicate funtion to control sampling start.

Definition at line 218 of file Mcp320x.h.

template<typename ChannelType >
template<typename T >
void MCP320x< ChannelType >::readn ( Channel  ch,
T *  data,
uint16_t  num 
) const
inline

Reads the supplied channel and stores N values in the supplied data array. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chdefines the channel to read from.
[out]dataarray to store the values.
[in]numnumber of reads. The data array needs to be at least that size.

Definition at line 233 of file Mcp320x.h.

template<typename ChannelType >
template<typename T >
void MCP320x< ChannelType >::readn ( Channel  ch,
T *  data,
uint16_t  num,
uint32_t  splFreq 
)
inline

Reads the supplied channel limited to the specified frequency and stores N values in the supplied data array. The sample rate limit is software controlled, and has a low precision. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chdefines the channel to read from.
[out]dataarray to store the values.
[in]numnumber of reads. The data array needs to be at least that size.
[in]splFreqsample frequency limit in hz.

Definition at line 251 of file Mcp320x.h.

template<typename ChannelType >
template<typename T , typename Predicate >
void MCP320x< ChannelType >::readn_if ( Channel  ch,
T *  data,
uint16_t  num,
Predicate  p 
) const
inline

Reads the supplied channel and stores N values in the supplied data array after the predicate is true. As long as the predicate is false, the function keeps sampling without storing any data. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chdefines the channel to read from.
[out]dataarray to store the values.
[in]numnumber of reads. The data array needs to be at least that size.
[in]ppredicate funtion to control sampling start.

Definition at line 269 of file Mcp320x.h.

template<typename ChannelType >
template<typename T , typename Predicate >
void MCP320x< ChannelType >::readn_if ( Channel  ch,
T *  data,
uint16_t  num,
uint32_t  splFreq,
Predicate  p 
)
inline

Reads the supplied channel limited to the specified frequency and stores N values in the supplied data array after the predicate is true. As long as the predicate is false, the function keeps sampling without storing any data. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chdefines the channel to read from.
[out]dataarray to store the values.
[in]numnumber of reads. The data array needs to be at least that size.
[in]splFreqsample frequency limit in hz.
[in]ppredicate funtion to control sampling start.

Definition at line 291 of file Mcp320x.h.

template<typename T >
uint32_t MCP320x< T >::testSplSpeed ( Channel  ch) const

Performs a sampling speed test over 64 reads. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chthe channel to use for the speed test.
Returns
the average sampling time needed for one sample in ns.

Definition at line 40 of file Mcp320x.cpp.

Referenced by MCP320x< ChannelType >::calibrate().

template<typename T >
uint32_t MCP320x< T >::testSplSpeed ( Channel  ch,
uint16_t  num 
) const

Performs a sampling speed test. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chthe channel to use for the speed test.
[in]numthe number of reads to perform.
Returns
the average sampling time needed for one sample in ns.

Definition at line 46 of file Mcp320x.cpp.

References MCP320x< ChannelType >::createCmd(), div_round, and MCP320x< ChannelType >::execute().

template<typename T >
uint32_t MCP320x< T >::testSplSpeed ( Channel  ch,
uint16_t  num,
uint32_t  splFreq 
)

Performs a sampling speed test limited to the specified frequency. The SPI interface must be initialized and put in a usable state before calling this function.

Parameters
[in]chthe channel to use for the speed test.
[in]numthe number of reads to perform.
[in]splFreqsample frequency limit in hz.
Returns
the average sampling time needed for one sample in ns.

Definition at line 61 of file Mcp320x.cpp.

References MCP320x< ChannelType >::createCmd(), div_round, MCP320x< ChannelType >::execute(), and MCP320x< ChannelType >::getSplDelay().

template<typename T >
uint16_t MCP320x< T >::toAnalog ( uint16_t  raw) const

Converts the supplied raw value to an analog value in mV based on the defined reference voltage.

Parameters
[in]rawthe sampled ADC value.
Returns
the converted analog value in mV.

Definition at line 82 of file Mcp320x.cpp.

References MCP320x< ChannelType >::kRes, and MCP320x< ChannelType >::mVref.

template<typename T >
uint16_t MCP320x< T >::toDigital ( uint16_t  val) const

Converts the supplied analog value to the digital representation based on the defined reference voltage.

Parameters
[in]valthe analog value in mV.
Returns
the digital representation off the supplied analog value.

Definition at line 88 of file Mcp320x.cpp.

References MCP320x< ChannelType >::kRes, and MCP320x< ChannelType >::mVref.

template<typename T >
uint16_t MCP320x< T >::transfer ( ) const
private

Transfers without SPI command data.

Returns
the ADC value from the SPI response.

Definition at line 187 of file Mcp320x.cpp.

References MCP320x< ChannelType >::SpiData::hiByte, MCP320x< ChannelType >::SpiData::loByte, MCP320x< ChannelType >::mCsPin, MCP320x< ChannelType >::mSpi, and MCP320x< ChannelType >::SpiData::value.

Referenced by MCP320x< ChannelType >::execute().

template<typename T >
uint16_t MCP320x< T >::transfer ( SpiData  cmd) const
private

Transfers the supplied SPI command data.

Parameters
[in]cmdthe SPI command data to transfer.
Returns
the ADC value from the SPI response.

Definition at line 208 of file Mcp320x.cpp.

References MCP320x< ChannelType >::SpiData::hiByte, MCP320x< ChannelType >::SpiData::loByte, MCP320x< ChannelType >::mCsPin, MCP320x< ChannelType >::mSpi, and MCP320x< ChannelType >::SpiData::value.

Field Documentation

template<typename ChannelType >
const uint16_t MCP320x< ChannelType >::kRes = (1 << kResBits)
static
template<typename ChannelType >
const uint8_t MCP320x< ChannelType >::kResBits = 12
static

ADC resolution in bits.

Definition at line 120 of file Mcp320x.h.

template<typename ChannelType >
uint8_t MCP320x< ChannelType >::mCsPin
private

Definition at line 448 of file Mcp320x.h.

Referenced by MCP320x< ChannelType >::transfer().

template<typename ChannelType >
SPIClass* MCP320x< ChannelType >::mSpi
private

Definition at line 450 of file Mcp320x.h.

Referenced by MCP320x< ChannelType >::transfer().

template<typename ChannelType >
uint32_t MCP320x< ChannelType >::mSplSpeed
private
template<typename ChannelType >
uint16_t MCP320x< ChannelType >::mVref
private

The documentation for this class was generated from the following files: