ADCDMAGen3_RK
|
Analog to digital conversion using DMA for Particle Gen 3 devices (Argon, Boron, Xenon) More...
#include <ADCDMAGen3_RK.h>
Public Member Functions | |
ret_code_t | init () |
Initialize the ADC. More... | |
void | start () |
Start sampling. More... | |
void | stop () |
Stop sampling. | |
void | uninit () |
Uninitialize the ADC and free its resources. More... | |
void | restoreDefaults () |
Restore the bit size, oversampling, and IRQ settings used by analogRead(). | |
ADCDMAGen3 & | withSingleBuffer (size_t size) |
Set a single buffer of the specified size, allocated on the heap during init() More... | |
ADCDMAGen3 & | withSingleBuffer (size_t size, nrf_saadc_value_t *buf) |
Set a single buffer of the specified size and pointer to buffer. More... | |
ADCDMAGen3 & | withDoubleBuffer (size_t size) |
Set double buffers, each one of the specified size, allocated on the heap during init() More... | |
ADCDMAGen3 & | withDoubleBuffer (size_t size, nrf_saadc_value_t *buf0, nrf_saadc_value_t *buf1) |
Set double buffers, each one of the specified size. More... | |
ADCDMAGen3 & | withDoubleBufferSplit (size_t size, nrf_saadc_value_t *buf) |
Set double buffers, created by taking a larger buffer and splitting it into two halves. More... | |
ADCDMAGen3 & | withSampleFreqHz (size_t sampleFreqHz) |
Sets the sampling frequency. More... | |
size_t | getSampleFreqHz () const |
Gets the sampling frequency in Hertz (samples per second). | |
ADCDMAGen3 & | withHardwareTimer (nrfx_timer_t hardwareTimer) |
Sets the hardware timer to use if using automatic acquisition. More... | |
ADCDMAGen3 & | withResolution (nrf_saadc_resolution_t resolution) |
Sets the ADC resolution. More... | |
nrf_saadc_resolution_t | getResolution () const |
Gets the ADC resolution. More... | |
ADCDMAGen3 & | withOversample (nrf_saadc_oversample_t oversample, nrf_saadc_burst_t burst) |
Sets the oversampling mode and burst mode. More... | |
nrf_saadc_oversample_t | getOversample () const |
Get the oversample setting. More... | |
nrf_saadc_burst_t | getBurstMode () const |
Get the burstMode setting (true = burst mode enabled, false = disabled) More... | |
ADCDMAGen3 & | withInterruptPriority (uint8_t interruptPriority) |
Sets the interrupt priority for ADC interrupts. More... | |
uint8_t | getInterruptPriority () const |
Gets the interrupt priority. More... | |
ADCDMAGen3 & | withReferenceGain (nrf_saadc_reference_t reference, nrf_saadc_gain_t gain) |
Set ADC reference and gain. More... | |
nrf_saadc_reference_t | getReference () const |
Gets the voltage reference. More... | |
nrf_saadc_gain_t | getGain () const |
Get the gain. More... | |
ADCDMAGen3 & | withAcqTime (nrf_saadc_acqtime_t acqTime) |
Get the acquisition time. More... | |
nrf_saadc_acqtime_t | getAcqTime () const |
Get the acquisition time setting. More... | |
ADCDMAGen3 & | withSamplePin (pin_t pin) |
Sample a single pin. More... | |
ADCDMAGen3 & | withSampleMultiplePins (std::initializer_list< pin_t > pins) |
Sample multiple pins. More... | |
ADCDMAGen3 & | withTimerCcChannel (nrf_timer_cc_channel_t timerCcChannel) |
Set the timer CC channel. The default is NRF_TIMER_CC_CHANNEL0. More... | |
nrf_timer_cc_channel_t | getTimerCcChannel () const |
Gets the timer CC channel. | |
ADCDMAGen3 & | withTimerShortMask (nrf_timer_short_mask_t timerShortMask) |
Set the timer short mask. The default is NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK. More... | |
nrf_timer_short_mask_t | getTimerShortMask () const |
Get the timer short mask. | |
ADCDMAGen3 & | withContinuousMode (bool continuousMode=true) |
Enable continuous mode. This is the default. More... | |
ADCDMAGen3 & | withOneShotMode () |
Enable one-shot mode (the opposite of continuous mode) More... | |
ADCDMAGen3 & | withBufferCallback (std::function< void(nrf_saadc_value_t *buf, size_t size)> bufferCallback) |
Sets the buffer callback function. More... | |
nrf_saadc_input_t | particlePinToAIN (pin_t pin) const |
Convert a Particle Pin A0 - A7 into the underlying ADC channel. More... | |
void | attachTimerInterrupt () |
Attaches the timer interrupt to user code for the specified hardwareTimer. More... | |
void | detachTimerInterrupt () |
Detaches the timer interrupt for the specified hardwareTimer. More... | |
void | freeBufferPoolIfNecessary () |
Frees allocated buffer pools (if they were allocated and not passed in) | |
Protected Types | |
enum | SetupState { NOT_INITIALIZED, ATTACH_INTERRUPT, SAADC_INIT, TIMER_INIT, PPI_ALLOC, COMPLETE } |
State constants for how much of the initialization completed. | |
Protected Member Functions | |
void | adcCallback (nrfx_saadc_evt_t const *event) |
ADC callback function. More... | |
Static Protected Member Functions | |
static void | timerCallbackStatic (nrf_timer_event_t eventType, void *context) |
Timer callback function. More... | |
static void | adcCallbackStatic (nrfx_saadc_evt_t const *event) |
ADC callback function. More... | |
static void | adcEmptyCallbackStatic (nrfx_saadc_evt_t const *event) |
ADC callback function used by restoreDefaults. | |
Protected Attributes | |
size_t | sampleFreqHz = 16000 |
Sample frequency in Hertz (samples per second) More... | |
nrfx_timer_t | hardwareTimer = NRFX_TIMER_INSTANCE(4) |
The hardware timer to use. More... | |
nrfx_saadc_config_t | saadcConfig = NRFX_SAADC_DEFAULT_CONFIG |
SAADC configuration. More... | |
nrf_saadc_channel_config_t | channelConfig = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1) |
SAADC channel configuration. More... | |
nrfx_timer_config_t | timerConfig = NRFX_TIMER_DEFAULT_CONFIG |
Timer configuration. More... | |
nrf_timer_cc_channel_t | timerCcChannel = NRF_TIMER_CC_CHANNEL0 |
Timer CC channel. More... | |
nrf_timer_short_mask_t | timerShortMask = NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK |
Timer Short Mask. More... | |
std::vector< pin_t > | pinList |
List of pins to sample. More... | |
size_t | bufferSize = 512 |
Buffer size. More... | |
size_t | poolSize = 2 |
Number of buffers (1 or 2) More... | |
nrf_saadc_value_t * | bufferPool [2] = { 0, 0 } |
Pointers to the buffers. The number of non-zero entries depends on poolSize. More... | |
bool | freeBufferPool = true |
Whether to delete[] the buffers when calling freeBufferPoolIfNecessary() More... | |
SetupState | setupState = SetupState::NOT_INITIALIZED |
State constants for how far the initialization completed. More... | |
nrf_ppi_channel_t | ppiChannel = NRF_PPI_CHANNEL0 |
The PPI channel to use. More... | |
bool | continuousMode = true |
Whether the sampling is one-shot or continuous. More... | |
std::function< void(nrf_saadc_value_t *buf, size_t size)> | bufferCallback = 0 |
Static Protected Attributes | |
static ADCDMAGen3 * | instance = 0 |
Analog to digital conversion using DMA for Particle Gen 3 devices (Argon, Boron, Xenon)
Note: You can only instantiate one of these per application since there can only be one ADC DMA in use at a time (though you can sample multiple channels).
Configuration is done using the withXXX() methods and can be chained, fluent-style.
After setting it up, use the init() method to initialize the ADC DMA.
|
protected |
ADC callback function.
This is called from adcCallbackStatic when an ADC event occurs. It calls bufferCallback if defined on NRFX_SAADC_EVT_DONE.
|
staticprotected |
ADC callback function.
This is the callback function passed to nrfx_saadc_init. It calls addCallback (not static) using the instance static member to find the class instance (there can only be one).
void ADCDMAGen3::attachTimerInterrupt | ( | ) |
Attaches the timer interrupt to user code for the specified hardwareTimer.
Calls attachInterruptDirect() based on hardwareTimer.p_reg
void ADCDMAGen3::detachTimerInterrupt | ( | ) |
Detaches the timer interrupt for the specified hardwareTimer.
Calls detachInterrupt() based on hardwareTimer.p_reg
|
inline |
Get the acquisition time setting.
See withAcqTime() for values.
|
inline |
Get the burstMode setting (true = burst mode enabled, false = disabled)
See withOversample() for for information.
|
inline |
Get the gain.
See withReferenceGain() for values.
|
inline |
Gets the interrupt priority.
See withInterruptPriority() for values
|
inline |
Get the oversample setting.
See withOversample() for values
|
inline |
Gets the voltage reference.
See withReferenceGain() for values.
|
inline |
Gets the ADC resolution.
See withResolution() for an explanation of return values.
ret_code_t ADCDMAGen3::init | ( | ) |
nrf_saadc_input_t ADCDMAGen3::particlePinToAIN | ( | pin_t | pin | ) | const |
Convert a Particle Pin A0 - A7 into the underlying ADC channel.
Particle ADC nRF pin_p notes Pin Chan Pin 0 P0.02 NRF_SAADC_INPUT_AIN0 A7 on SoMs A0 1 P0.03 NRF_SAADC_INPUT_AIN1 A1 2 P0.04 NRF_SAADC_INPUT_AIN2 3 P0.05 NRF_SAADC_INPUT_AIN3 A6 on SoMs, BAT on Argon and Xenon A2 4 P0.28 NRF_SAADC_INPUT_AIN4 A3 5 P0.29 NRF_SAADC_INPUT_AIN5 A4 6 P0.30 NRF_SAADC_INPUT_AIN6 A5 7 P0.31 NRF_SAADC_INPUT_AIN7
|
inline |
Start sampling.
The default is to sample continuously (or until stop() is called).
If you use withOneShotMode(), then the first buffer is filled and the sampling is stopped automatically and you should not call stop().
|
staticprotected |
Timer callback function.
This is the callback function passed to nrfx_timer_init. It doesn't currently do anything useful as the timer event is handled by PPI not code.
void ADCDMAGen3::uninit | ( | ) |
|
inline |
Get the acquisition time.
acqTime | The acquisition time value |
Longer acquisition times allows for higher resistance inputs, but lower the maximum sampling frequency
|
inline |
Sets the buffer callback function.
bufferCallback | The function to call when a buffer is filled |
The bufferCallback function must have this prototype:
void bufferCallback(nrf_saadc_value_t *buf, size_t size);
The buf is not used after this call completes, so you can modify it in place if desired for efficiency, but you must not rely on the contents after you return from the callback because it will be reused and filled with new data.
Note that the buffer callback is called from an interrupt context so you must take care in what functions you call from it. In particular:
|
inline |
Enable continuous mode. This is the default.
In continuous mode, the ADC is sampled continuously until stop() is called. Each full buffer triggers the bufferCallback.
The opposite of continuous mode is one shot mode, enabled using withOneShotMode().
ADCDMAGen3 & ADCDMAGen3::withDoubleBuffer | ( | size_t | size | ) |
Set double buffers, each one of the specified size, allocated on the heap during init()
size | size in samples, not bytes. Each sample is 16-bits (2 bytes). And since it's a double buffer, 4 * size bytes will be allocated on the heap. |
Double buffers should be used with continuous mode is enabled (the default).
ADCDMAGen3 & ADCDMAGen3::withDoubleBuffer | ( | size_t | size, |
nrf_saadc_value_t * | buf0, | ||
nrf_saadc_value_t * | buf1 | ||
) |
Set double buffers, each one of the specified size.
size | size in samples, not bytes. Each sample is 16-bits (2 bytes). |
buf0 | Pointer to a buffer of samples. Note that each sample a nrf_saadc_value_t (16 bits, 2 bytes). You must not deallocate this buffer. It's typically allocated as a global variable. |
buf1 | Pointer to a buffer of samples. Note that each sample a nrf_saadc_value_t (16 bits, 2 bytes). You must not deallocate this buffer. It's typically allocated as a global variable. |
Double buffers should be used with continuous mode is enabled (the default).
ADCDMAGen3 & ADCDMAGen3::withDoubleBufferSplit | ( | size_t | size, |
nrf_saadc_value_t * | buf | ||
) |
Set double buffers, created by taking a larger buffer and splitting it into two halves.
size | size in samples, not bytes, of buf Each sample is 16-bits (2 bytes). The sample size will be half that, because the buffer is split into two half-buffers. |
buf | Pointer to a buffer of samples. Note that each sample a nrf_saadc_value_t (16 bits, 2 bytes). You must not deallocate this buffer. It's typically allocated as a global variable. |
Double buffers should be used with continuous mode is enabled (the default).
|
inline |
Sets the hardware timer to use if using automatic acquisition.
Using a hardware timer is necessary to control the frequency of sampling. It's possible to manually trigger a single sample, but that tends to not be very useful and isn't supported by this library.
|
inline |
Sets the interrupt priority for ADC interrupts.
|
inline |
Enable one-shot mode (the opposite of continuous mode)
In one-shot mode, the buffer is filled once then sampling stops. The ADC is left configured. You can resume sampling again using start().
|
inline |
Sets the oversampling mode and burst mode.
oversample | The oversampling value. The default is disabled. |
burst | Enable burst mode. Default is disabled. |
When enabling oversampling, multiple samples are taken and averaged to reduce noise. For example, if NRF_SAADC_OVERSAMPLE_4X is set, then 4 samples are taken and averaged.
If burst mode is enabled, then those 4 samples are taken as quickly as possible.
If burst mode is disabled, then those 4 samples are taken one on each trigger, resulting in a buffer taking 4 times longer to fill up, for example.
|
inline |
Set ADC reference and gain.
reference | the voltage reference |
gain | the gain on the input signal |
The default setting is NRF_SAADC_REFERENCE_VDD4, NRF_SAADC_GAIN1_4. Another common setting is NRF_SAADC_REFERENCE_INTERNAL, NRF_SAADC_GAIN1_6.
|
inline |
Sets the ADC resolution.
Note that the values stored in the buffer are always 16-bit values (even if the resolution is 8 bit).
|
inline |
Sets the sampling frequency.
sampleFreqHz | The sampling frequency in Hertz or samples per second. Default is 16000. |
Maximum is around 200000 however make sure you also reduce the acqTime to NRF_SAADC_ACQTIME_3US when using the maximum sampling frequency.
|
inline |
Sample multiple pins.
pins | List of pins |
To use this, pass the list of pins within {}. For example:
Calling this replaces the previous list of pins to sample. If withSamplePin() was previously called, that pin will no longer be sampled (unless it's in this list of pins).
To sample a single pin you can pass one pin in the {} or use withSamplePin().
|
inline |
Sample a single pin.
pin | The pin. Typically A0 - A5 on Gen 3, except for the B Series SoM which also supports A6 and A7. |
Calling this more than once replaces the previously sampled pin. If withSampleMultiplePins was previously called, calling this will sample only this pin.
To sample multiple pins sequentially, use withSampleMultiplePins();
ADCDMAGen3 & ADCDMAGen3::withSingleBuffer | ( | size_t | size | ) |
Set a single buffer of the specified size, allocated on the heap during init()
size | size in samples, not bytes. Each sample is 16-bits (2 bytes). |
Single buffer should only be used in with withOneShotMode(). If you are continuously sampling you should use double buffer mode so writes can continue in the other buffer while you are handling the full buffer.
ADCDMAGen3 & ADCDMAGen3::withSingleBuffer | ( | size_t | size, |
nrf_saadc_value_t * | buf | ||
) |
Set a single buffer of the specified size and pointer to buffer.
size | size in samples, not bytes. Each sample is 16-bits (2 bytes). |
buf | Pointer to a buffer of samples. Note that each sample a nrf_saadc_value_t (16 bits, 2 bytes). You must not deallocate this buffer. It's typically allocated as a global variable. |
Single buffer should only be used in with withOneShotMode(). If you are continuously sampling you should use double buffer mode so writes can continue in the other buffer while you are handling the full buffer.
|
inline |
Set the timer CC channel. The default is NRF_TIMER_CC_CHANNEL0.
You probably won't need to change this value. If you do, set it before calling init().
|
inline |
Set the timer short mask. The default is NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK.
You probably won't need to change this value. If you do, set it before calling init().
|
protected |
Callback | function that is called when a buffer is filled |
Note that this callback is called in an interrupt context so you need to be careful about what you do in it. Set it using withBufferCallback().
|
protected |
Pointers to the buffers. The number of non-zero entries depends on poolSize.
Set using withSingleBuffer() or withDoubleBuffer(). You must set this before calling init().
|
protected |
Buffer size.
Set using withSingleBuffer() or withDoubleBuffer(). You must set this before calling init().
|
protected |
SAADC channel configuration.
Note: we override settings in NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE in the constructor for this class. Use with withXXX() methods to change fields before calling init(). The pin is updated before use (multiple times if using multi-pin mode).
You must set this before calling init().
|
protected |
Whether the sampling is one-shot or continuous.
The default is continuous. You should always use double buffers when using continuous mode. Use withOneShotMode() to set this to false.
|
protected |
Whether to delete[] the buffers when calling freeBufferPoolIfNecessary()
Set using withSingleBuffer() or withDoubleBuffer(). You should not modify this member.
|
protected |
The hardware timer to use.
Set using withHardwareTimer(). You must set this before calling init().
|
staticprotected |
Global | instance of this class |
Since there is only one SAADC engine on the nRF52, you can only have one instance of this class. It is stored in this variable. It's used from the static callbacks to find the singleton instance of this class.
|
protected |
List of pins to sample.
Set using withSamplePin() or withSampleMultiplePins(). You must set this before calling init().
|
protected |
Number of buffers (1 or 2)
Set using withSingleBuffer() or withDoubleBuffer(). You must set this before calling init().
|
protected |
The PPI channel to use.
DeviceOS uses PPI channel
|
protected |
SAADC configuration.
Note: we override settings in NRFX_SAADC_DEFAULT_CONFIG in the constructor for this class. Use with withXXX() methods to change fields before calling init().
|
protected |
Sample frequency in Hertz (samples per second)
Set using withSampleFreqHz(). You must set this before calling init().
|
protected |
|
protected |
Timer CC channel.
You normally don't need to override this setting, but if you do, use withTimerCcChannel() and withTimerShortMask().
Default value is NRF_TIMER_CC_CHANNEL0. You must set this before calling init().
|
protected |
Timer configuration.
You normally don't need to override these settings. You must set this before calling init().
|
protected |
Timer Short Mask.
You normally don't need to override this setting, but if you do, use withTimerCcChannel() and withTimerShortMask().
Default value is NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK. You must set this before calling init().