ChibiOS/RT
2.5.1
ADC Driver
Collaboration diagram for ADC Driver:

Detailed Description

Generic ADC Driver.

This module implements a generic ADC (Analog to Digital Converter) driver supporting a variety of buffer and conversion modes.

Precondition:
In order to use the ADC driver the HAL_USE_ADC option must be enabled in halconf.h.

Driver State Machine

The driver implements a state machine internally, not all the driver functionalities can be used in any moment, any transition not explicitly shown in the following diagram has to be considered an error and shall be captured by an assertion (if enabled).

dot_inline_dotgraph_2.png

ADC Operations

The ADC driver is quite complex, an explanation of the terminology and of the operational details follows.

ADC Conversion Groups

The ADCConversionGroup is the objects that specifies a physical conversion operation. This structure contains some standard fields and several implementation-dependent fields.
The standard fields define the CG mode, the number of channels belonging to the CG and the optional callbacks.
The implementation-dependent fields specify the physical ADC operation mode, the analog channels belonging to the group and any other implementation-specific setting. Usually the extra fields just mirror the physical ADC registers, please refer to the vendor's MCU Reference Manual for details about the available settings. Details are also available into the documentation of the ADC low level drivers and in the various sample applications.

ADC Conversion Modes

The driver supports several conversion modes:

ADC Callbacks

The driver is able to invoke callbacks during the conversion process. A callback is invoked when the operation has been completed or, in circular mode, when the buffer has been filled and the operation is restarted. In linear and circular modes a callback is also invoked when the buffer is half filled.
The "half filled" and "filled" callbacks in circular mode allow to implement "streaming processing" of the sampled data, while the driver is busy filling one half of the buffer the application can process the other half, this allows for continuous interleaved operations.

The driver is not thread safe for performance reasons, if you need to access the ADC bus from multiple threads then use the adcAcquireBus() and adcReleaseBus() APIs in order to gain exclusive access.

Data Structures

struct  ADCConversionGroup
 Conversion group configuration structure. More...
struct  ADCConfig
 Driver configuration structure. More...
struct  ADCDriver
 Structure representing an ADC driver. More...

Functions

void adcInit (void)
 ADC Driver initialization.
void adcObjectInit (ADCDriver *adcp)
 Initializes the standard part of a ADCDriver structure.
void adcStart (ADCDriver *adcp, const ADCConfig *config)
 Configures and activates the ADC peripheral.
void adcStop (ADCDriver *adcp)
 Deactivates the ADC peripheral.
void adcStartConversion (ADCDriver *adcp, const ADCConversionGroup *grpp, adcsample_t *samples, size_t depth)
 Starts an ADC conversion.
void adcStartConversionI (ADCDriver *adcp, const ADCConversionGroup *grpp, adcsample_t *samples, size_t depth)
 Starts an ADC conversion.
void adcStopConversion (ADCDriver *adcp)
 Stops an ongoing conversion.
void adcStopConversionI (ADCDriver *adcp)
 Stops an ongoing conversion.
void adcAcquireBus (ADCDriver *adcp)
 Gains exclusive access to the ADC peripheral.
void adcReleaseBus (ADCDriver *adcp)
 Releases exclusive access to the ADC peripheral.
msg_t adcConvert (ADCDriver *adcp, const ADCConversionGroup *grpp, adcsample_t *samples, size_t depth)
 Performs an ADC conversion.
 CH_IRQ_HANDLER (ADC1_IRQHandler)
 ADC interrupt handler.
void adc_lld_init (void)
 Low level ADC driver initialization.
void adc_lld_start (ADCDriver *adcp)
 Configures and activates the ADC peripheral.
void adc_lld_stop (ADCDriver *adcp)
 Deactivates the ADC peripheral.
void adc_lld_start_conversion (ADCDriver *adcp)
 Starts an ADC conversion.
void adc_lld_stop_conversion (ADCDriver *adcp)
 Stops an ongoing conversion.
void adcSTM32EnableTSVREFE (void)
 Enables the TSVREFE bit.
void adcSTM32DisableTSVREFE (void)
 Disables the TSVREFE bit.

Variables

ADCDriver ADCD1
 ADC1 driver identifier.

ADC configuration options

#define ADC_USE_WAIT   TRUE
 Enables synchronous APIs.
#define ADC_USE_MUTUAL_EXCLUSION   TRUE
 Enables the adcAcquireBus() and adcReleaseBus() APIs.

Low Level driver helper macros

#define _adc_reset_i(adcp)
 Resumes a thread waiting for a conversion completion.
#define _adc_reset_s(adcp)
 Resumes a thread waiting for a conversion completion.
#define _adc_wakeup_isr(adcp)
 Wakes up the waiting thread.
#define _adc_timeout_isr(adcp)
 Wakes up the waiting thread with a timeout message.
#define _adc_isr_half_code(adcp)
 Common ISR code, half buffer event.
#define _adc_isr_full_code(adcp)
 Common ISR code, full buffer event.
#define _adc_isr_error_code(adcp, err)
 Common ISR code, error event.

Triggers selection

#define ADC_CR2_EXTSEL_SRC(n)   ((n) << 24)
 Trigger source.

ADC clock divider settings

#define ADC_CCR_ADCPRE_DIV1   0
#define ADC_CCR_ADCPRE_DIV2   1
#define ADC_CCR_ADCPRE_DIV4   2

Available analog channels

#define ADC_CHANNEL_IN0   0
 External analog input 0.
#define ADC_CHANNEL_IN1   1
 External analog input 1.
#define ADC_CHANNEL_IN2   2
 External analog input 2.
#define ADC_CHANNEL_IN3   3
 External analog input 3.
#define ADC_CHANNEL_IN4   4
 External analog input 4.
#define ADC_CHANNEL_IN5   5
 External analog input 5.
#define ADC_CHANNEL_IN6   6
 External analog input 6.
#define ADC_CHANNEL_IN7   7
 External analog input 7.
#define ADC_CHANNEL_IN8   8
 External analog input 8.
#define ADC_CHANNEL_IN9   9
 External analog input 9.
#define ADC_CHANNEL_IN10   10
 External analog input 10.
#define ADC_CHANNEL_IN11   11
 External analog input 11.
#define ADC_CHANNEL_IN12   12
 External analog input 12.
#define ADC_CHANNEL_IN13   13
 External analog input 13.
#define ADC_CHANNEL_IN14   14
 External analog input 14.
#define ADC_CHANNEL_IN15   15
 External analog input 15.
#define ADC_CHANNEL_SENSOR   16
 Internal temperature sensor.
#define ADC_CHANNEL_VREFINT   17
 Internal reference.
#define ADC_CHANNEL_IN18   18
 External analog input 18.
#define ADC_CHANNEL_IN19   19
 External analog input 19.
#define ADC_CHANNEL_IN20   20
 External analog input 20.
#define ADC_CHANNEL_IN21   21
 External analog input 21.
#define ADC_CHANNEL_IN22   22
 External analog input 22.
#define ADC_CHANNEL_IN23   23
 External analog input 23.
#define ADC_CHANNEL_IN24   24
 External analog input 24.
#define ADC_CHANNEL_IN25   25
 External analog input 25.

Sampling rates

#define ADC_SAMPLE_4   0
 4 cycles sampling time.
#define ADC_SAMPLE_9   1
 9 cycles sampling time.
#define ADC_SAMPLE_16   2
 16 cycles sampling time.
#define ADC_SAMPLE_24   3
 24 cycles sampling time.
#define ADC_SAMPLE_48   4
 48 cycles sampling time.
#define ADC_SAMPLE_96   5
 96 cycles sampling time.
#define ADC_SAMPLE_192   6
 192 cycles sampling time.
#define ADC_SAMPLE_384   7
 384 cycles sampling time.

Configuration options

#define STM32_ADC_USE_ADC1   FALSE
 ADC1 driver enable switch.
#define STM32_ADC_ADCPRE   ADC_CCR_ADCPRE_DIV1
 ADC common clock divider.
#define STM32_ADC_ADC1_DMA_PRIORITY   2
 ADC1 DMA priority (0..3|lowest..highest).
#define STM32_ADC_IRQ_PRIORITY   5
 ADC interrupt priority level setting.
#define STM32_ADC_ADC1_DMA_IRQ_PRIORITY   5
 ADC1 DMA interrupt priority level setting.

Sequences building helper macros

#define ADC_SQR1_NUM_CH(n)   (((n) - 1) << 20)
 Number of channels in a conversion sequence.
#define ADC_SQR5_SQ1_N(n)   ((n) << 0)
 1st channel in seq.
#define ADC_SQR5_SQ2_N(n)   ((n) << 5)
 2nd channel in seq.
#define ADC_SQR5_SQ3_N(n)   ((n) << 10)
 3rd channel in seq.
#define ADC_SQR5_SQ4_N(n)   ((n) << 15)
 4th channel in seq.
#define ADC_SQR5_SQ5_N(n)   ((n) << 20)
 5th channel in seq.
#define ADC_SQR5_SQ6_N(n)   ((n) << 25)
 6th channel in seq.
#define ADC_SQR4_SQ7_N(n)   ((n) << 0)
 7th channel in seq.
#define ADC_SQR4_SQ8_N(n)   ((n) << 5)
 8th channel in seq.
#define ADC_SQR4_SQ9_N(n)   ((n) << 10)
 9th channel in seq.
#define ADC_SQR4_SQ10_N(n)   ((n) << 15)
 10th channel in seq.
#define ADC_SQR4_SQ11_N(n)   ((n) << 20)
 11th channel in seq.
#define ADC_SQR4_SQ12_N(n)   ((n) << 25)
 12th channel in seq.
#define ADC_SQR3_SQ13_N(n)   ((n) << 0)
 13th channel in seq.
#define ADC_SQR3_SQ14_N(n)   ((n) << 5)
 14th channel in seq.
#define ADC_SQR3_SQ15_N(n)   ((n) << 10)
 15th channel in seq.
#define ADC_SQR3_SQ16_N(n)   ((n) << 15)
 16th channel in seq.
#define ADC_SQR3_SQ17_N(n)   ((n) << 20)
 17th channel in seq.
#define ADC_SQR3_SQ18_N(n)   ((n) << 25)
 18th channel in seq.
#define ADC_SQR2_SQ19_N(n)   ((n) << 0)
 19th channel in seq.
#define ADC_SQR2_SQ20_N(n)   ((n) << 5)
 20th channel in seq.
#define ADC_SQR2_SQ21_N(n)   ((n) << 10)
 21th channel in seq.
#define ADC_SQR2_SQ22_N(n)   ((n) << 15)
 22th channel in seq.
#define ADC_SQR2_SQ23_N(n)   ((n) << 20)
 23th channel in seq.
#define ADC_SQR2_SQ24_N(n)   ((n) << 25)
 24th channel in seq.
#define ADC_SQR1_SQ25_N(n)   ((n) << 0)
 25th channel in seq.
#define ADC_SQR1_SQ26_N(n)   ((n) << 5)
 26th channel in seq.
#define ADC_SQR1_SQ27_N(n)   ((n) << 10)
 27th channel in seq.

Sampling rate settings helper macros

#define ADC_SMPR3_SMP_AN0(n)   ((n) << 0)
 AN0 sampling time.
#define ADC_SMPR3_SMP_AN1(n)   ((n) << 3)
 AN1 sampling time.
#define ADC_SMPR3_SMP_AN2(n)   ((n) << 6)
 AN2 sampling time.
#define ADC_SMPR3_SMP_AN3(n)   ((n) << 9)
 AN3 sampling time.
#define ADC_SMPR3_SMP_AN4(n)   ((n) << 12)
 AN4 sampling time.
#define ADC_SMPR3_SMP_AN5(n)   ((n) << 15)
 AN5 sampling time.
#define ADC_SMPR3_SMP_AN6(n)   ((n) << 18)
 AN6 sampling time.
#define ADC_SMPR3_SMP_AN7(n)   ((n) << 21)
 AN7 sampling time.
#define ADC_SMPR3_SMP_AN8(n)   ((n) << 24)
 AN8 sampling time.
#define ADC_SMPR3_SMP_AN9(n)   ((n) << 27)
 AN9 sampling time.
#define ADC_SMPR2_SMP_AN10(n)   ((n) << 0)
 AN10 sampling time.
#define ADC_SMPR2_SMP_AN11(n)   ((n) << 3)
 AN11 sampling time.
#define ADC_SMPR2_SMP_AN12(n)   ((n) << 6)
 AN12 sampling time.
#define ADC_SMPR2_SMP_AN13(n)   ((n) << 9)
 AN13 sampling time.
#define ADC_SMPR2_SMP_AN14(n)   ((n) << 12)
 AN14 sampling time.
#define ADC_SMPR2_SMP_AN15(n)   ((n) << 15)
 AN15 sampling time.
#define ADC_SMPR2_SMP_SENSOR(n)   ((n) << 18)
 Temperature Sensor sampling time.
#define ADC_SMPR2_SMP_VREF(n)   ((n) << 21)
 Voltage Reference sampling time.
#define ADC_SMPR2_SMP_AN18(n)   ((n) << 24)
 AN18 sampling time.
#define ADC_SMPR2_SMP_AN19(n)   ((n) << 27)
 AN19 sampling time.
#define ADC_SMPR1_SMP_AN20(n)   ((n) << 0)
 AN20 sampling time.
#define ADC_SMPR1_SMP_AN21(n)   ((n) << 3)
 AN21 sampling time.
#define ADC_SMPR1_SMP_AN22(n)   ((n) << 6)
 AN22 sampling time.
#define ADC_SMPR1_SMP_AN23(n)   ((n) << 9)
 AN23 sampling time.
#define ADC_SMPR1_SMP_AN24(n)   ((n) << 12)
 AN24 sampling time.
#define ADC_SMPR1_SMP_AN25(n)   ((n) << 15)
 AN25 sampling time.

Typedefs

typedef uint16_t adcsample_t
 ADC sample data type.
typedef uint16_t adc_channels_num_t
 Channels number in a conversion group.
typedef struct ADCDriver ADCDriver
 Type of a structure representing an ADC driver.
typedef void(* adccallback_t )(ADCDriver *adcp, adcsample_t *buffer, size_t n)
 ADC notification callback type.
typedef void(* adcerrorcallback_t )(ADCDriver *adcp, adcerror_t err)
 ADC error callback type.

Enumerations

enum  adcstate_t {
  ADC_UNINIT = 0, ADC_STOP = 1, ADC_READY = 2, ADC_ACTIVE = 3,
  ADC_COMPLETE = 4, ADC_ERROR = 5
}
 Driver state machine possible states. More...
enum  adcerror_t { ADC_ERR_DMAFAILURE = 0, ADC_ERR_OVERFLOW = 1 }
 Possible ADC failure causes. More...

Function Documentation

void adcInit ( void  )

ADC Driver initialization.

Note:
This function is implicitly invoked by halInit(), there is no need to explicitly initialize the driver.
Function Class:
Initializer, this function just initializes an object and can be invoked before the kernel is initialized.

Definition at line 61 of file adc.c.

References adc_lld_init().

Referenced by halInit().

Here is the call graph for this function:

void adcObjectInit ( ADCDriver adcp)

Initializes the standard part of a ADCDriver structure.

Parameters:
[out]adcppointer to the ADCDriver object
Function Class:
Initializer, this function just initializes an object and can be invoked before the kernel is initialized.

Definition at line 73 of file adc.c.

References ADC_STOP, ADCDriver::config, ADCDriver::depth, ADCDriver::grpp, ADCDriver::mutex, ADCDriver::samples, ADCDriver::state, and ADCDriver::thread.

Referenced by adc_lld_init().

void adcStart ( ADCDriver adcp,
const ADCConfig config 
)

Configures and activates the ADC peripheral.

Parameters:
[in]adcppointer to the ADCDriver object
[in]configpointer to the ADCConfig object. Depending on the implementation the value can be NULL.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 104 of file adc.c.

References adc_lld_start(), ADC_READY, ADC_STOP, ADCDriver::config, and ADCDriver::state.

Here is the call graph for this function:

void adcStop ( ADCDriver adcp)

Deactivates the ADC peripheral.

Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 124 of file adc.c.

References adc_lld_stop(), ADC_READY, ADC_STOP, and ADCDriver::state.

Here is the call graph for this function:

void adcStartConversion ( ADCDriver adcp,
const ADCConversionGroup grpp,
adcsample_t samples,
size_t  depth 
)

Starts an ADC conversion.

Starts an asynchronous conversion operation.

Note:
The buffer is organized as a matrix of M*N elements where M is the channels number configured into the conversion group and N is the buffer depth. The samples are sequentially written into the buffer with no gaps.
Parameters:
[in]adcppointer to the ADCDriver object
[in]grpppointer to a ADCConversionGroup object
[out]samplespointer to the samples buffer
[in]depthbuffer depth (matrix rows number). The buffer depth must be one or an even number.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 152 of file adc.c.

References adcStartConversionI().

Here is the call graph for this function:

void adcStartConversionI ( ADCDriver adcp,
const ADCConversionGroup grpp,
adcsample_t samples,
size_t  depth 
)

Starts an ADC conversion.

Starts an asynchronous conversion operation.

Postcondition:
The callbacks associated to the conversion group will be invoked on buffer fill and error events.
Note:
The buffer is organized as a matrix of M*N elements where M is the channels number configured into the conversion group and N is the buffer depth. The samples are sequentially written into the buffer with no gaps.
Parameters:
[in]adcppointer to the ADCDriver object
[in]grpppointer to a ADCConversionGroup object
[out]samplespointer to the samples buffer
[in]depthbuffer depth (matrix rows number). The buffer depth must be one or an even number.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 180 of file adc.c.

References ADC_ACTIVE, ADC_COMPLETE, ADC_ERROR, adc_lld_start_conversion(), ADC_READY, ADCDriver::depth, ADCDriver::grpp, ADCDriver::samples, and ADCDriver::state.

Referenced by adcConvert(), and adcStartConversion().

Here is the call graph for this function:

void adcStopConversion ( ADCDriver adcp)

Stops an ongoing conversion.

This function stops the currently ongoing conversion and returns the driver in the ADC_READY state. If there was no conversion being processed then the function does nothing.

Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 211 of file adc.c.

References _adc_reset_s, ADC_ACTIVE, adc_lld_stop_conversion(), ADC_READY, ADCDriver::grpp, and ADCDriver::state.

Here is the call graph for this function:

void adcStopConversionI ( ADCDriver adcp)

Stops an ongoing conversion.

This function stops the currently ongoing conversion and returns the driver in the ADC_READY state. If there was no conversion being processed then the function does nothing.

Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 238 of file adc.c.

References _adc_reset_i, ADC_ACTIVE, ADC_COMPLETE, adc_lld_stop_conversion(), ADC_READY, ADCDriver::grpp, and ADCDriver::state.

Here is the call graph for this function:

void adcAcquireBus ( ADCDriver adcp)

Gains exclusive access to the ADC peripheral.

This function tries to gain ownership to the ADC bus, if the bus is already being used then the invoking thread is queued.

Precondition:
In order to use this function the option ADC_USE_MUTUAL_EXCLUSION must be enabled.
Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 308 of file adc.c.

References ADCDriver::mutex.

void adcReleaseBus ( ADCDriver adcp)

Releases exclusive access to the ADC peripheral.

Precondition:
In order to use this function the option ADC_USE_MUTUAL_EXCLUSION must be enabled.
Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 328 of file adc.c.

msg_t adcConvert ( ADCDriver adcp,
const ADCConversionGroup grpp,
adcsample_t samples,
size_t  depth 
)

Performs an ADC conversion.

Performs a synchronous conversion operation.

Note:
The buffer is organized as a matrix of M*N elements where M is the channels number configured into the conversion group and N is the buffer depth. The samples are sequentially written into the buffer with no gaps.
Parameters:
[in]adcppointer to the ADCDriver object
[in]grpppointer to a ADCConversionGroup object
[out]samplespointer to the samples buffer
[in]depthbuffer depth (matrix rows number). The buffer depth must be one or an even number.
Returns:
The operation result.
Return values:
RDY_OKConversion finished.
RDY_RESETThe conversion has been stopped using acdStopConversion() or acdStopConversionI(), the result buffer may contain incorrect data.
RDY_TIMEOUTThe conversion has been stopped because an hardware error.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 279 of file adc.c.

References adcStartConversionI(), and ADCDriver::thread.

Here is the call graph for this function:

CH_IRQ_HANDLER ( ADC1_IRQHandler  )

ADC interrupt handler.

Function Class:
Interrupt handler, this function should not be directly invoked.

Definition at line 91 of file adc_lld.c.

References _adc_isr_error_code, ADC_ERR_OVERFLOW, ADCDriver::dmastp, dmaStreamGetTransactionSize, and ADCDriver::grpp.

void adc_lld_init ( void  )

Low level ADC driver initialization.

Function Class:
Not an API, this function is for internal use only.

Definition at line 121 of file adc_lld.c.

References ADCDriver::adc, adcObjectInit(), ADCDriver::dmamode, ADCDriver::dmastp, STM32_ADC_ADC1_DMA_PRIORITY, STM32_ADC_IRQ_PRIORITY, and STM32_DMA_CR_DMEIE.

Referenced by adcInit().

Here is the call graph for this function:

void adc_lld_start ( ADCDriver adcp)

Configures and activates the ADC peripheral.

Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 147 of file adc_lld.c.

References ADCDriver::adc, ADC_STOP, ADCDriver::dmastp, dmaStreamAllocate(), dmaStreamSetPeripheral, rccEnableADC1, ADCDriver::state, and STM32_ADC_ADC1_DMA_IRQ_PRIORITY.

Referenced by adcStart().

Here is the call graph for this function:

void adc_lld_stop ( ADCDriver adcp)

Deactivates the ADC peripheral.

Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 179 of file adc_lld.c.

References ADCDriver::adc, ADC_READY, ADCDriver::dmastp, dmaStreamRelease(), rccDisableADC1, and ADCDriver::state.

Referenced by adcStop().

Here is the call graph for this function:

void adc_lld_stop_conversion ( ADCDriver adcp)

Stops an ongoing conversion.

Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 250 of file adc_lld.c.

References ADCDriver::adc, ADCDriver::dmastp, and dmaStreamDisable.

Referenced by adcStopConversion(), and adcStopConversionI().

void adcSTM32EnableTSVREFE ( void  )

Enables the TSVREFE bit.

The TSVREFE bit is required in order to sample the internal temperature sensor and internal reference voltage.

Note:
This is an STM32-only functionality.

Definition at line 264 of file adc_lld.c.

void adcSTM32DisableTSVREFE ( void  )

Disables the TSVREFE bit.

The TSVREFE bit is required in order to sample the internal temperature sensor and internal reference voltage.

Note:
This is an STM32-only functionality.

Definition at line 275 of file adc_lld.c.


Variable Documentation

ADC1 driver identifier.

Definition at line 40 of file adc_lld.c.


Define Documentation

#define ADC_USE_WAIT   TRUE

Enables synchronous APIs.

Note:
Disabling this option saves both code and data space.

Definition at line 51 of file adc.h.

#define ADC_USE_MUTUAL_EXCLUSION   TRUE

Enables the adcAcquireBus() and adcReleaseBus() APIs.

Note:
Disabling this option saves both code and data space.

Definition at line 59 of file adc.h.

#define _adc_reset_i (   adcp)
Value:
{                                                \
  if ((adcp)->thread != NULL) {                                             \
    Thread *tp = (adcp)->thread;                                            \
    (adcp)->thread = NULL;                                                  \
    tp->p_u.rdymsg  = RDY_RESET;                                            \
    chSchReadyI(tp);                                                        \
  }                                                                         \
}

Resumes a thread waiting for a conversion completion.

Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 105 of file adc.h.

Referenced by adcStopConversionI().

#define _adc_reset_s (   adcp)
Value:
{                                                \
  if ((adcp)->thread != NULL) {                                             \
    Thread *tp = (adcp)->thread;                                            \
    (adcp)->thread = NULL;                                                  \
    chSchWakeupS(tp, RDY_RESET);                                            \
  }                                                                         \
}

Resumes a thread waiting for a conversion completion.

Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 121 of file adc.h.

Referenced by adcStopConversion().

#define _adc_wakeup_isr (   adcp)
Value:
{                                             \
  chSysLockFromIsr();                                                       \
  if ((adcp)->thread != NULL) {                                             \
    Thread *tp;                                                             \
    tp = (adcp)->thread;                                                    \
    (adcp)->thread = NULL;                                                  \
    tp->p_u.rdymsg = RDY_OK;                                                \
    chSchReadyI(tp);                                                        \
  }                                                                         \
  chSysUnlockFromIsr();                                                     \
}

Wakes up the waiting thread.

Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 136 of file adc.h.

#define _adc_timeout_isr (   adcp)
Value:
{                                            \
  chSysLockFromIsr();                                                       \
  if ((adcp)->thread != NULL) {                                             \
    Thread *tp;                                                             \
    tp = (adcp)->thread;                                                    \
    (adcp)->thread = NULL;                                                  \
    tp->p_u.rdymsg = RDY_TIMEOUT;                                           \
    chSchReadyI(tp);                                                        \
  }                                                                         \
  chSysUnlockFromIsr();                                                     \
}

Wakes up the waiting thread with a timeout message.

Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 155 of file adc.h.

#define _adc_isr_half_code (   adcp)
Value:
{                                          \
  if ((adcp)->grpp->end_cb != NULL) {                                       \
    (adcp)->grpp->end_cb(adcp, (adcp)->samples, (adcp)->depth / 2);         \
  }                                                                         \
}

Common ISR code, half buffer event.

This code handles the portable part of the ISR code:

  • Callback invocation.
Note:
This macro is meant to be used in the low level drivers implementation only.
Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 186 of file adc.h.

#define _adc_isr_full_code (   adcp)

Common ISR code, full buffer event.

This code handles the portable part of the ISR code:

  • Callback invocation.
  • Waiting thread wakeup, if any.
  • Driver state transitions.
Note:
This macro is meant to be used in the low level drivers implementation only.
Parameters:
[in]adcppointer to the ADCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 206 of file adc.h.

#define _adc_isr_error_code (   adcp,
  err 
)
Value:
{                                    \
  adc_lld_stop_conversion(adcp);                                            \
  if ((adcp)->grpp->error_cb != NULL) {                                     \
    (adcp)->state = ADC_ERROR;                                              \
    (adcp)->grpp->error_cb(adcp, err);                                      \
    if ((adcp)->state == ADC_ERROR)                                         \
      (adcp)->state = ADC_READY;                                            \
  }                                                                         \
  (adcp)->grpp = NULL;                                                      \
  _adc_timeout_isr(adcp);                                                   \
}

Common ISR code, error event.

This code handles the portable part of the ISR code:

  • Callback invocation.
  • Waiting thread timeout signaling, if any.
  • Driver state transitions.
Note:
This macro is meant to be used in the low level drivers implementation only.
Parameters:
[in]adcppointer to the ADCDriver object
[in]errplatform dependent error code
Function Class:
Not an API, this function is for internal use only.

Definition at line 262 of file adc.h.

Referenced by CH_IRQ_HANDLER().

#define ADC_CR2_EXTSEL_SRC (   n)    ((n) << 24)

Trigger source.

Definition at line 42 of file adc_lld.h.

#define ADC_CHANNEL_IN0   0

External analog input 0.

Definition at line 58 of file adc_lld.h.

#define ADC_CHANNEL_IN1   1

External analog input 1.

Definition at line 59 of file adc_lld.h.

#define ADC_CHANNEL_IN2   2

External analog input 2.

Definition at line 60 of file adc_lld.h.

#define ADC_CHANNEL_IN3   3

External analog input 3.

Definition at line 61 of file adc_lld.h.

#define ADC_CHANNEL_IN4   4

External analog input 4.

Definition at line 62 of file adc_lld.h.

#define ADC_CHANNEL_IN5   5

External analog input 5.

Definition at line 63 of file adc_lld.h.

#define ADC_CHANNEL_IN6   6

External analog input 6.

Definition at line 64 of file adc_lld.h.

#define ADC_CHANNEL_IN7   7

External analog input 7.

Definition at line 65 of file adc_lld.h.

#define ADC_CHANNEL_IN8   8

External analog input 8.

Definition at line 66 of file adc_lld.h.

#define ADC_CHANNEL_IN9   9

External analog input 9.

Definition at line 67 of file adc_lld.h.

#define ADC_CHANNEL_IN10   10

External analog input 10.

Definition at line 68 of file adc_lld.h.

#define ADC_CHANNEL_IN11   11

External analog input 11.

Definition at line 69 of file adc_lld.h.

#define ADC_CHANNEL_IN12   12

External analog input 12.

Definition at line 70 of file adc_lld.h.

#define ADC_CHANNEL_IN13   13

External analog input 13.

Definition at line 71 of file adc_lld.h.

#define ADC_CHANNEL_IN14   14

External analog input 14.

Definition at line 72 of file adc_lld.h.

#define ADC_CHANNEL_IN15   15

External analog input 15.

Definition at line 73 of file adc_lld.h.

#define ADC_CHANNEL_SENSOR   16

Internal temperature sensor.

Definition at line 74 of file adc_lld.h.

#define ADC_CHANNEL_VREFINT   17

Internal reference.

Definition at line 75 of file adc_lld.h.

#define ADC_CHANNEL_IN18   18

External analog input 18.

Definition at line 76 of file adc_lld.h.

#define ADC_CHANNEL_IN19   19

External analog input 19.

Definition at line 77 of file adc_lld.h.

#define ADC_CHANNEL_IN20   20

External analog input 20.

Definition at line 78 of file adc_lld.h.

#define ADC_CHANNEL_IN21   21

External analog input 21.

Definition at line 79 of file adc_lld.h.

#define ADC_CHANNEL_IN22   22

External analog input 22.

Definition at line 80 of file adc_lld.h.

#define ADC_CHANNEL_IN23   23

External analog input 23.

Definition at line 81 of file adc_lld.h.

#define ADC_CHANNEL_IN24   24

External analog input 24.

Definition at line 82 of file adc_lld.h.

#define ADC_CHANNEL_IN25   25

External analog input 25.

Definition at line 83 of file adc_lld.h.

#define ADC_SAMPLE_4   0

4 cycles sampling time.

Definition at line 90 of file adc_lld.h.

#define ADC_SAMPLE_9   1

9 cycles sampling time.

Definition at line 91 of file adc_lld.h.

#define ADC_SAMPLE_16   2

16 cycles sampling time.

Definition at line 92 of file adc_lld.h.

#define ADC_SAMPLE_24   3

24 cycles sampling time.

Definition at line 93 of file adc_lld.h.

#define ADC_SAMPLE_48   4

48 cycles sampling time.

Definition at line 94 of file adc_lld.h.

#define ADC_SAMPLE_96   5

96 cycles sampling time.

Definition at line 95 of file adc_lld.h.

#define ADC_SAMPLE_192   6

192 cycles sampling time.

Definition at line 96 of file adc_lld.h.

#define ADC_SAMPLE_384   7

384 cycles sampling time.

Definition at line 97 of file adc_lld.h.

#define STM32_ADC_USE_ADC1   FALSE

ADC1 driver enable switch.

If set to TRUE the support for ADC1 is included.

Note:
The default is TRUE.

Definition at line 114 of file adc_lld.h.

#define STM32_ADC_ADCPRE   ADC_CCR_ADCPRE_DIV1

ADC common clock divider.

Note:
This setting is influenced by the VDDA voltage and other external conditions, please refer to the STM32L15x datasheet for more info.
See section 6.3.15 "12-bit ADC characteristics".

Definition at line 125 of file adc_lld.h.

#define STM32_ADC_ADC1_DMA_PRIORITY   2

ADC1 DMA priority (0..3|lowest..highest).

Definition at line 132 of file adc_lld.h.

Referenced by adc_lld_init().

#define STM32_ADC_IRQ_PRIORITY   5

ADC interrupt priority level setting.

Definition at line 139 of file adc_lld.h.

Referenced by adc_lld_init().

#define STM32_ADC_ADC1_DMA_IRQ_PRIORITY   5

ADC1 DMA interrupt priority level setting.

Definition at line 146 of file adc_lld.h.

Referenced by adc_lld_start().

#define ADC_SQR1_NUM_CH (   n)    (((n) - 1) << 20)

Number of channels in a conversion sequence.

Definition at line 391 of file adc_lld.h.

#define ADC_SQR5_SQ1_N (   n)    ((n) << 0)

1st channel in seq.

Definition at line 393 of file adc_lld.h.

#define ADC_SQR5_SQ2_N (   n)    ((n) << 5)

2nd channel in seq.

Definition at line 394 of file adc_lld.h.

#define ADC_SQR5_SQ3_N (   n)    ((n) << 10)

3rd channel in seq.

Definition at line 395 of file adc_lld.h.

#define ADC_SQR5_SQ4_N (   n)    ((n) << 15)

4th channel in seq.

Definition at line 396 of file adc_lld.h.

#define ADC_SQR5_SQ5_N (   n)    ((n) << 20)

5th channel in seq.

Definition at line 397 of file adc_lld.h.

#define ADC_SQR5_SQ6_N (   n)    ((n) << 25)

6th channel in seq.

Definition at line 398 of file adc_lld.h.

#define ADC_SQR4_SQ7_N (   n)    ((n) << 0)

7th channel in seq.

Definition at line 400 of file adc_lld.h.

#define ADC_SQR4_SQ8_N (   n)    ((n) << 5)

8th channel in seq.

Definition at line 401 of file adc_lld.h.

#define ADC_SQR4_SQ9_N (   n)    ((n) << 10)

9th channel in seq.

Definition at line 402 of file adc_lld.h.

#define ADC_SQR4_SQ10_N (   n)    ((n) << 15)

10th channel in seq.

Definition at line 403 of file adc_lld.h.

#define ADC_SQR4_SQ11_N (   n)    ((n) << 20)

11th channel in seq.

Definition at line 404 of file adc_lld.h.

#define ADC_SQR4_SQ12_N (   n)    ((n) << 25)

12th channel in seq.

Definition at line 405 of file adc_lld.h.

#define ADC_SQR3_SQ13_N (   n)    ((n) << 0)

13th channel in seq.

Definition at line 407 of file adc_lld.h.

#define ADC_SQR3_SQ14_N (   n)    ((n) << 5)

14th channel in seq.

Definition at line 408 of file adc_lld.h.

#define ADC_SQR3_SQ15_N (   n)    ((n) << 10)

15th channel in seq.

Definition at line 409 of file adc_lld.h.

#define ADC_SQR3_SQ16_N (   n)    ((n) << 15)

16th channel in seq.

Definition at line 410 of file adc_lld.h.

#define ADC_SQR3_SQ17_N (   n)    ((n) << 20)

17th channel in seq.

Definition at line 411 of file adc_lld.h.

#define ADC_SQR3_SQ18_N (   n)    ((n) << 25)

18th channel in seq.

Definition at line 412 of file adc_lld.h.

#define ADC_SQR2_SQ19_N (   n)    ((n) << 0)

19th channel in seq.

Definition at line 414 of file adc_lld.h.

#define ADC_SQR2_SQ20_N (   n)    ((n) << 5)

20th channel in seq.

Definition at line 415 of file adc_lld.h.

#define ADC_SQR2_SQ21_N (   n)    ((n) << 10)

21th channel in seq.

Definition at line 416 of file adc_lld.h.

#define ADC_SQR2_SQ22_N (   n)    ((n) << 15)

22th channel in seq.

Definition at line 417 of file adc_lld.h.

#define ADC_SQR2_SQ23_N (   n)    ((n) << 20)

23th channel in seq.

Definition at line 418 of file adc_lld.h.

#define ADC_SQR2_SQ24_N (   n)    ((n) << 25)

24th channel in seq.

Definition at line 419 of file adc_lld.h.

#define ADC_SQR1_SQ25_N (   n)    ((n) << 0)

25th channel in seq.

Definition at line 421 of file adc_lld.h.

#define ADC_SQR1_SQ26_N (   n)    ((n) << 5)

26th channel in seq.

Definition at line 422 of file adc_lld.h.

#define ADC_SQR1_SQ27_N (   n)    ((n) << 10)

27th channel in seq.

Definition at line 423 of file adc_lld.h.

#define ADC_SMPR3_SMP_AN0 (   n)    ((n) << 0)

AN0 sampling time.

Definition at line 430 of file adc_lld.h.

#define ADC_SMPR3_SMP_AN1 (   n)    ((n) << 3)

AN1 sampling time.

Definition at line 431 of file adc_lld.h.

#define ADC_SMPR3_SMP_AN2 (   n)    ((n) << 6)

AN2 sampling time.

Definition at line 432 of file adc_lld.h.

#define ADC_SMPR3_SMP_AN3 (   n)    ((n) << 9)

AN3 sampling time.

Definition at line 433 of file adc_lld.h.

#define ADC_SMPR3_SMP_AN4 (   n)    ((n) << 12)

AN4 sampling time.

Definition at line 434 of file adc_lld.h.

#define ADC_SMPR3_SMP_AN5 (   n)    ((n) << 15)

AN5 sampling time.

Definition at line 435 of file adc_lld.h.

#define ADC_SMPR3_SMP_AN6 (   n)    ((n) << 18)

AN6 sampling time.

Definition at line 436 of file adc_lld.h.

#define ADC_SMPR3_SMP_AN7 (   n)    ((n) << 21)

AN7 sampling time.

Definition at line 437 of file adc_lld.h.

#define ADC_SMPR3_SMP_AN8 (   n)    ((n) << 24)

AN8 sampling time.

Definition at line 438 of file adc_lld.h.

#define ADC_SMPR3_SMP_AN9 (   n)    ((n) << 27)

AN9 sampling time.

Definition at line 439 of file adc_lld.h.

#define ADC_SMPR2_SMP_AN10 (   n)    ((n) << 0)

AN10 sampling time.

Definition at line 441 of file adc_lld.h.

#define ADC_SMPR2_SMP_AN11 (   n)    ((n) << 3)

AN11 sampling time.

Definition at line 442 of file adc_lld.h.

#define ADC_SMPR2_SMP_AN12 (   n)    ((n) << 6)

AN12 sampling time.

Definition at line 443 of file adc_lld.h.

#define ADC_SMPR2_SMP_AN13 (   n)    ((n) << 9)

AN13 sampling time.

Definition at line 444 of file adc_lld.h.

#define ADC_SMPR2_SMP_AN14 (   n)    ((n) << 12)

AN14 sampling time.

Definition at line 445 of file adc_lld.h.

#define ADC_SMPR2_SMP_AN15 (   n)    ((n) << 15)

AN15 sampling time.

Definition at line 446 of file adc_lld.h.

#define ADC_SMPR2_SMP_SENSOR (   n)    ((n) << 18)

Temperature Sensor sampling time.

Definition at line 447 of file adc_lld.h.

#define ADC_SMPR2_SMP_VREF (   n)    ((n) << 21)

Voltage Reference sampling time.

Definition at line 449 of file adc_lld.h.

#define ADC_SMPR2_SMP_AN18 (   n)    ((n) << 24)

AN18 sampling time.

Definition at line 451 of file adc_lld.h.

#define ADC_SMPR2_SMP_AN19 (   n)    ((n) << 27)

AN19 sampling time.

Definition at line 452 of file adc_lld.h.

#define ADC_SMPR1_SMP_AN20 (   n)    ((n) << 0)

AN20 sampling time.

Definition at line 454 of file adc_lld.h.

#define ADC_SMPR1_SMP_AN21 (   n)    ((n) << 3)

AN21 sampling time.

Definition at line 455 of file adc_lld.h.

#define ADC_SMPR1_SMP_AN22 (   n)    ((n) << 6)

AN22 sampling time.

Definition at line 456 of file adc_lld.h.

#define ADC_SMPR1_SMP_AN23 (   n)    ((n) << 9)

AN23 sampling time.

Definition at line 457 of file adc_lld.h.

#define ADC_SMPR1_SMP_AN24 (   n)    ((n) << 12)

AN24 sampling time.

Definition at line 458 of file adc_lld.h.

#define ADC_SMPR1_SMP_AN25 (   n)    ((n) << 15)

AN25 sampling time.

Definition at line 459 of file adc_lld.h.


Typedef Documentation

typedef uint16_t adcsample_t

ADC sample data type.

Definition at line 189 of file adc_lld.h.

typedef uint16_t adc_channels_num_t

Channels number in a conversion group.

Definition at line 194 of file adc_lld.h.

typedef struct ADCDriver ADCDriver

Type of a structure representing an ADC driver.

Definition at line 209 of file adc_lld.h.

typedef void(* adccallback_t)(ADCDriver *adcp, adcsample_t *buffer, size_t n)

ADC notification callback type.

Parameters:
[in]adcppointer to the ADCDriver object triggering the callback
[in]bufferpointer to the most recent samples data
[in]nnumber of buffer rows available starting from buffer

Definition at line 219 of file adc_lld.h.

typedef void(* adcerrorcallback_t)(ADCDriver *adcp, adcerror_t err)

ADC error callback type.

Parameters:
[in]adcppointer to the ADCDriver object triggering the callback
[in]errADC error code

Definition at line 228 of file adc_lld.h.


Enumeration Type Documentation

enum adcstate_t

Driver state machine possible states.

Enumerator:
ADC_UNINIT 

Not initialized.

ADC_STOP 

Stopped.

ADC_READY 

Ready.

ADC_ACTIVE 

Converting.

ADC_COMPLETE 

Conversion complete.

ADC_ERROR 

Conversion complete.

Definition at line 78 of file adc.h.

enum adcerror_t

Possible ADC failure causes.

Note:
Error codes are architecture dependent and should not relied upon.
Enumerator:
ADC_ERR_DMAFAILURE 

DMA operations failure.

ADC_ERR_OVERFLOW 

ADC overflow condition.

Definition at line 201 of file adc_lld.h.