modm API documentation

#include <modm/platform/adc/adc_1.hpp>

Inheritance diagram for modm::platform::Adc1:
modm::platform::AdcInterrupt1

Classes

struct  SequenceChannel
 

Public Types

enum  Channel : uint8_t {
  Channel1 = 1, Channel2 = 2, Channel3 = 3, Channel4 = 4,
  Channel5 = 5, Channel6 = 6, Channel7 = 7, Channel8 = 8,
  Channel9 = 9, Channel10 = 10, Channel11 = 11, Channel12 = 12,
  Channel13 = 13, Channel14 = 14, Channel15 = 15, Channel16 = 16,
  Temperature = 17, BatDiv3 = 18
}
 Channels, which can be used with this ADC.
 
enum  ClockMode : uint32_t {
  DoNotChange = 0xff, Asynchronous = 0, SynchronousPrescaler1 = ADC_CCR_CKMODE_0, SynchronousPrescaler2 = ADC_CCR_CKMODE_1,
  SynchronousPrescaler4 = ADC_CCR_CKMODE_1 | ADC_CCR_CKMODE_0
}
 
enum  ClockSource : uint32_t { NoClock = 0, PllSai1 = RCC_CCIPR_ADCSEL_0, PllSai2 = RCC_CCIPR_ADCSEL_1, SystemClock = RCC_CCIPR_ADCSEL_1 | RCC_CCIPR_ADCSEL_0 }
 
enum  Prescaler : uint32_t {
  Disabled = 0, Div1 = 0, Div2 = ADC_CCR_PRESC_0, Div4 = ADC_CCR_PRESC_1,
  Div6 = ADC_CCR_PRESC_1 | ADC_CCR_PRESC_0, Div8 = ADC_CCR_PRESC_2, Div10 = ADC_CCR_PRESC_2 | ADC_CCR_PRESC_0, Div12 = ADC_CCR_PRESC_2 | ADC_CCR_PRESC_1,
  Div16 = ADC_CCR_PRESC_2 | ADC_CCR_PRESC_1 | ADC_CCR_PRESC_0, Div32 = ADC_CCR_PRESC_3, Div64 = ADC_CCR_PRESC_3 | ADC_CCR_PRESC_0, Div128 = ADC_CCR_PRESC_3 | ADC_CCR_PRESC_1,
  Div256 = ADC_CCR_PRESC_3 | ADC_CCR_PRESC_1 | ADC_CCR_PRESC_0, Div256AllBits = ADC_CCR_PRESC_3 | ADC_CCR_PRESC_2 | ADC_CCR_PRESC_1 | ADC_CCR_PRESC_0
}
 
enum  SampleTime : uint8_t {
  Cycles2 = 0b000, Cycles7 = 0b001, Cycles13 = 0b010, Cycles25 = 0b011,
  Cycles48 = 0b100, Cycles93 = 0b101, Cycles248 = 0b110, Cycles641 = 0b111
}
 
enum  CalibrationMode : uint32_t { SingleEndedInputsMode = 0, DifferntialInputsMode = ADC_CR_ADCALDIF, DoNotCalibrate = 0xff }
 
enum  VoltageRegulatorState : uint32_t { Enabled = ADC_CR_ADVREGEN }
 
enum  DmaMode : uint32_t { Disabled = 0, OneShot = ADC_CFGR_DMAEN, Circular = ADC_CFGR_DMACFG | ADC_CFGR_DMAEN, Mask = Circular }
 
enum  ExternalTriggerPolarity { NoTriggerDetection = 0x0u, RisingEdge = 0x1u, FallingEdge = 0x2u, RisingAndFallingEdge = 0x3u }
 
enum  Interrupt : uint32_t {
  Ready = ADC_IER_ADRDYIE, EndOfSampling = ADC_IER_EOSMPIE, EndOfRegularConversion = ADC_IER_EOCIE, EndOfRegularSequenceOfConversions = ADC_IER_EOSIE,
  Overrun = ADC_IER_OVRIE, EndOfInjectedConversion = ADC_IER_JEOCIE, EndOfInjectedSequenceOfConversions = ADC_IER_JEOSIE, AnalogWatchdog1 = ADC_IER_AWD1IE,
  AnalogWatchdog2 = ADC_IER_AWD2IE, AnalogWatchdog3 = ADC_IER_AWD3IE, InjectedContextQueueOverflow = ADC_IER_JQOVFIE
}
 
enum  InterruptFlag : uint32_t {
  Ready = ADC_ISR_ADRDY, EndOfSampling = ADC_ISR_EOSMP, EndOfRegularConversion = ADC_ISR_EOC, EndOfRegularSequenceOfConversions = ADC_ISR_EOS,
  Overrun = ADC_ISR_OVR, EndOfInjectedConversion = ADC_ISR_JEOC, EndOfInjectedSequenceOfConversions = ADC_ISR_JEOS, AnalogWatchdog1 = ADC_ISR_AWD1,
  AnalogWatchdog2 = ADC_ISR_AWD2, AnalogWatchdog3 = ADC_ISR_AWD3, InjectedContextQueueOverflow = ADC_ISR_JQOVF, All = ADC_ISR_ADRDY | ADC_ISR_EOSMP | ADC_ISR_EOC | ADC_ISR_EOS | ADC_ISR_OVR | ADC_ISR_JEOC | ADC_ISR_JEOS | ADC_ISR_AWD1 | ADC_ISR_AWD2 | ADC_ISR_AWD3 | ADC_ISR_JQOVF
}
 
enum  ExternalTriggerEvent {
  Event0 = 0x00u, Event1 = 0x01u, Event2 = 0x02u, Event3 = 0x03u,
  Event4 = 0x04u, Event5 = 0x05u, Event6 = 0x06u, Event7 = 0x07u,
  Event9 = 0x09u, Event10 = 0x0Au, Event11 = 0x0Bu, Event12 = 0x0Cu,
  Event13 = 0x0Du, Event14 = 0x0Eu, Event15 = 0x0Fu
}
 
enum  OffsetSlot : uint8_t { Slot0 = 0, Slot1 = 1, Slot2 = 2, Slot3 = 3 }
 

Public Member Functions

 MODM_FLAGS32 (Interrupt)
 
 MODM_FLAGS32 (InterruptFlag)
 

Static Public Member Functions

template<class... Signals>
static void connect ()
 
static void initialize (const ClockMode clk=ClockMode::DoNotChange, const ClockSource clk_src=ClockSource::SystemClock, const Prescaler pre=Prescaler::Disabled, const CalibrationMode cal=CalibrationMode::DoNotCalibrate, const bool blocking=true)
 
static void disable (const bool blocking=true)
 
static void setPrescaler (const Prescaler pre)
 
static bool isReady ()
 
static void calibrate (const CalibrationMode mode, const bool blocking=true)
 
static void setLeftAdjustResult (const bool enable)
 
static bool setChannel (const Channel channel, const SampleTime sampleTime=static_cast< SampleTime >(0b000))
 
static bool setChannelSequence (std::span< const SequenceChannel > sequence)
 
template<class Gpio >
static bool setPinChannel (SampleTime sampleTime=static_cast< SampleTime >(0b000))
 Setting the channel for a Pin.
 
template<class Gpio >
static constexpr Channel getPinChannel ()
 Get the channel for a Pin.
 
static void setFreeRunningMode (const bool enable)
 
static void startConversion ()
 
static void stopConversion ()
 
static void enableRegularConversionExternalTrigger (ExternalTriggerPolarity externalTriggerPolarity, ExternalTriggerEvent externalTriggerEvent)
 
static void disableRegularConversionExternalTrigger (void)
 
static bool isConversionFinished ()
 
static bool isConversionSequenceFinished ()
 
static void startInjectedConversionSequence ()
 
static bool setInjectedConversionChannel (uint8_t index, Channel channel, SampleTime sampleTime)
 
template<class Gpio >
static bool setInjectedConversionChannel (uint8_t index, SampleTime sampleTime)
 
static bool setInjectedConversionSequenceLength (uint8_t length)
 
static void enableInjectedConversionExternalTrigger (ExternalTriggerPolarity externalTriggerPolarity, ExternalTriggerEvent externalTriggerEvent)
 
static void disableInjectedConversionExternalTrigger ()
 
static bool isInjectedConversionFinished ()
 
static auto getValue ()
 
static auto getInjectedConversionValue (uint8_t index)
 
static bool enableChannelOffset (const OffsetSlot slot, const Channel channel, const uint16_t offset)
 
template<class Gpio >
static bool enableChannelOffset (const OffsetSlot slot, const uint16_t offset)
 
static bool disableChannelOffset (OffsetSlot slot)
 
template<class Gpio >
static bool disableChannelOffset (OffsetSlot slot)
 
static void enableInterruptVector (const uint32_t priority, const bool enable=true)
 
static void enableInterrupt (const Interrupt_t interrupt)
 
static void disableInterrupt (const Interrupt_t interrupt)
 
static InterruptFlag_t getInterruptFlags ()
 
static void acknowledgeInterruptFlags (const InterruptFlag_t flags)
 
static volatile uint32_t * dataRegister ()
 
static void setDmaMode (DmaMode mode)
 

Static Public Attributes

static constexpr uint8_t Resolution = 12
 

Detailed Description

Analog/Digital-Converter module (ADC1).

The 12-bit ADC is a successive approximation analog-to-digital converter. It has up to 19 multiplexed channels allowing it measure signals from 16 external and three internal sources. The result of the ADC is stored in a left-aligned or right-aligned 16-bit data register.

This API is designed for the internal ADCs of STM32F30X/STM32F31X

Author
Kevin Laeufer
Sascha Schade (strongly-typed)

Member Enumeration Documentation

Enum mapping all events on a external trigger converter. The source mapped to each event varies on controller family, refer to the ADC external trigger section on reference manual of your controller for more information

Member Function Documentation

static volatile uint32_t* modm::platform::Adc1::dataRegister ( )
inlinestatic
Returns
ADC data register pointer, for DMA use only.
static bool modm::platform::Adc1::disableChannelOffset ( OffsetSlot  slot)
inlinestatic
  • slot for the offset register (0..3)
    Returns
    true if the offset was successfully disabled, false if the slot is invalid
    Note
    The offset can only be disabled when the adc is stopped!
template<class Gpio >
static bool modm::platform::Adc1::disableChannelOffset ( OffsetSlot  slot)
inlinestatic
  • slot for the offset register (0..3)
    Returns
    true if the offset was successfully disabled, false if the slot is invalid
    Note
    The channel is determined by the GPIO pin type.
    The offset can only be disabled when the adc is stopped!
static void modm::platform::Adc1::disableInjectedConversionExternalTrigger ( )
inlinestatic

Disable injected conversions external trigger.

static void modm::platform::Adc1::disableRegularConversionExternalTrigger ( void  )
inlinestatic

Disable regular conversions external trigger.

static bool modm::platform::Adc1::enableChannelOffset ( const OffsetSlot  slot,
const Channel  channel,
const uint16_t  offset 
)
inlinestatic
  • slot for the offset register (0..3)
  • channel channel to which the offset is applied
  • offset offset value to be applied to the channel
    Returns
    true if the offset was successfully enabled, false if the slot is invalid
    Note
    The offset can only be applied when the adc is stopped!
template<class Gpio >
static bool modm::platform::Adc1::enableChannelOffset ( const OffsetSlot  slot,
const uint16_t  offset 
)
inlinestatic
  • slot for the offset register (0..3)
  • offset value applied to the channel
    Returns
    true if the offset was successfully enabled, false if the slot is invalid
    Note
    The channel is determined by the GPIO pin type.
    The offset can only be applied when the adc is stopped!
static void modm::platform::Adc1::enableInjectedConversionExternalTrigger ( ExternalTriggerPolarity  externalTriggerPolarity,
ExternalTriggerEvent  externalTriggerEvent 
)
inlinestatic

enable injected conversions on external trigger.

Parameters
externalTriggerPolarityPolarity of the external trigger signal.
externalTriggerEventExternal trigger source.
static void modm::platform::Adc1::enableRegularConversionExternalTrigger ( ExternalTriggerPolarity  externalTriggerPolarity,
ExternalTriggerEvent  externalTriggerEvent 
)
inlinestatic

enable regular conversions on external trigger.

Parameters
externalTriggerPolarityPolarity of the external trigger signal.
externalTriggerEventRegular conversion external trigger source.
static auto modm::platform::Adc1::getInjectedConversionValue ( uint8_t  index)
inlinestatic

Get result of injected conversion.

Precondition
The injected conversion sequence is completed.
static auto modm::platform::Adc1::getValue ( )
inlinestatic
Returns
The most recent result of the ADC conversion.
Precondition
A conversion should have been started with startConversion()

To have a blocking GET you might do it this way:

{
// Waiting for conversion
}
static void modm::platform::Adc1::initialize ( const ClockMode  clk = ClockMode::DoNotChange,
const ClockSource  clk_src = ClockSource::SystemClock,
const Prescaler  pre = Prescaler::Disabled,
const CalibrationMode  cal = CalibrationMode::DoNotCalibrate,
const bool  blocking = true 
)
inlinestatic

Initialize and enable the A/D converter.

Enables the ADC clock and switches on the ADC. The ADC clock prescaler will be set as well.

The ADC can be clocked

Parameters
clkClock Mode for ADC1/ADC2 or ADC3/ADC4. Set to ClockMode::DoNotChange or leave blank if you want to leave this setting untouched.
preThe prescaler for the asynchronous ADC clock. This parameter is only taken into account if clk == ClockMode::Asynchronous.
static bool modm::platform::Adc1::isConversionFinished ( )
inlinestatic
Returns
If the conversion is finished.
Precondition
A conversion should have been started with startConversion()
static bool modm::platform::Adc1::isInjectedConversionFinished ( )
inlinestatic
Returns
If the injected conversion sequence is finished.
Precondition
An injected conversion should have been started with startInjectedConversionSequence()
static bool modm::platform::Adc1::isReady ( )
inlinestatic

Returns true if the ADRDY bit of the ISR is set

static bool modm::platform::Adc1::setChannel ( const Channel  channel,
const SampleTime  sampleTime = static_cast< SampleTime >(0b000) 
)
inlinestatic

Analog channel selection.

This not for scan mode. The number of channels will be set to 1, the channel selected and the corresponding pin will be set to analog input. If the the channel is modified during a conversion, the current conversion is reset and a new start pulse is sent to the ADC to convert the new chosen channnel / group of channels.

Parameters
channelThe channel which shall be read.
sampleTimeThe sample time to sample the input voltage.
Precondition
The ADC clock must be started and the ADC switched on with initialize()
static void modm::platform::Adc1::setFreeRunningMode ( const bool  enable)
inlinestatic

Enables free running mode

The ADC will continously start conversions and provide the most recent result in the ADC register.

Precondition
The ADC clock must be started and the ADC switched on with initialize()
static bool modm::platform::Adc1::setInjectedConversionChannel ( uint8_t  index,
Channel  channel,
SampleTime  sampleTime 
)
inlinestatic
  • index Index of injected conversion in sequence (0..3)
    Returns
    true if configuration is successful, false if arguments are invalid
template<class Gpio >
static bool modm::platform::Adc1::setInjectedConversionChannel ( uint8_t  index,
SampleTime  sampleTime 
)
inlinestatic
  • index Index of injected conversion in sequence (0..3)
    Returns
    true if configuration is successful, false if arguments are invalid
static bool modm::platform::Adc1::setInjectedConversionSequenceLength ( uint8_t  length)
inlinestatic
  • length Length of injected conversion sequence (1..4)
    Returns
    true if configuration is successful, false if arguments are invalid
static void modm::platform::Adc1::setLeftAdjustResult ( const bool  enable)
inlinestatic

Change the presentation of the ADC conversion result.

Parameters
enableSet to true to left adjust the result. Otherwise, the result is right adjusted.
Precondition
The ADC clock must be started and the ADC switched on with initialize()
static void modm::platform::Adc1::setPrescaler ( const Prescaler  pre)
inlinestatic

Select the frequency of the clock to the ADC. The clock is common for ADC1/ADC2 and ADC3/ADC4.

Precondition
The PLL must be running.
Parameters
prescalerThe prescaler specifies by which factor the system clock will be divided.
static void modm::platform::Adc1::startConversion ( )
inlinestatic

Start a new conversion or continuous conversions.

Precondition
A ADC channel must be selected with setChannel().
Postcondition
The result can be fetched with getValue()

TODO: is there any limitation to when is can be called??

static void modm::platform::Adc1::startInjectedConversionSequence ( )
inlinestatic

Start a new injected conversion sequence.

Precondition
Channels must be selected with setInjectedConversionChannel().

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