PER Firmware
Loading...
Searching...
No Matches
adc.h File Reference

G4 ADC public API. DMA-only, DMA-first conversions. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "stm32g474xx.h"
#include "common/phal_G4/dma/dma.h"
Include dependency graph for adc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  PHAL_ADC_ChannelConfig_t
 One entry in the conversion sequence. More...
 
struct  PHAL_ADC_Config_t
 Static configuration for one ADC instance. More...
 
struct  PHAL_ADC_Handle_t
 Runtime handle for one ADC instance. More...
 

Functions

bool PHAL_ADC_init (PHAL_ADC_Handle_t *handle, const PHAL_ADC_Config_t *config)
 Configure an ADC instance and its DMA channel.
 
bool PHAL_ADC_readDMA (PHAL_ADC_Handle_t *handle, uint16_t *buffer, uint16_t length)
 Start an asynchronous circular-DMA conversion into buffer
 
bool PHAL_ADC_readBlocking (PHAL_ADC_Handle_t *handle, uint16_t *buffer, uint16_t length, uint32_t timeout)
 Synchronous one-buffer conversion using the DMA engine.
 
bool PHAL_ADC_stop (PHAL_ADC_Handle_t *handle)
 Abort any transfer in flight and leave the ADC idle.
 
bool PHAL_ADC_busy (const PHAL_ADC_Handle_t *handle)
 Check whether a transfer is currently in flight.
 
void PHAL_ADC_conversionCompleteCallback (PHAL_ADC_Handle_t *handle)
 Weak callback fired once per completed DMA buffer.
 

Variables

static constexpr uint8_t PHAL_ADC_MAX_CHANNEL_COUNT = 16U
 Maximum number of channels one ADC conversion sequence can hold.
 
static constexpr uint8_t PHAL_ADC_MAX_CHANNEL_NUMBER = 18U
 Highest ADC channel number (IN18 on the G474)
 

Detailed Description

G4 ADC public API. DMA-only, DMA-first conversions.

Author
Ronak Jain (jain7.nosp@m.17@p.nosp@m.urdue.nosp@m..edu)

Function Documentation

◆ PHAL_ADC_busy()

bool PHAL_ADC_busy ( const PHAL_ADC_Handle_t * handle)

Check whether a transfer is currently in flight.

Parameters
handleinitialized handle
Returns
true while a PHAL_ADC_readDMA() transfer is running

◆ PHAL_ADC_conversionCompleteCallback()

void PHAL_ADC_conversionCompleteCallback ( PHAL_ADC_Handle_t * handle)
extern

Weak callback fired once per completed DMA buffer.

Called from the DMA interrupt context after each asynchronous circular-DMA buffer completes, and from the caller's context at the end of a successful PHAL_ADC_readBlocking(). On a DMA error, handle->transfer_error is true. Keep it short; the default implementation does nothing.

Parameters
handlethe handle whose transfer just completed
Here is the caller graph for this function:

◆ PHAL_ADC_init()

bool PHAL_ADC_init ( PHAL_ADC_Handle_t * handle,
const PHAL_ADC_Config_t * config )

Configure an ADC instance and its DMA channel.

What this does, in order:

  1. Enables the ADC (and DMA) clocks
  2. Runs the ADC hardware calibration
  3. Configures the conversion sequence, sample times, and fixed conversion options (12-bit, right-aligned, continuous, DMA-enabled, no oversampling)
  4. Enables the ADC and waits until it reports ready
  5. Claims the ADC's dedicated DMA channel and enables its interrupt

The DMA channel is fixed per instance (ADC1 -> DMA1 ch1, ADC2 -> DMA2 ch1, ADC3 -> DMA2 ch2, ADC4 -> DMA2 ch3). Only one handle may claim each ADC instance; handles remain claimed for the lifetime of the application.

Parameters
handlezero-initialized handle to populate
configstatic configuration to use; must stay valid for the lifetime of the handle
Returns
true on success, false if the config is invalid, the instance is already claimed, calibration fails, or the DMA channel is taken
Note
Initialization configures the ADC and DMA but does not start sampling; call PHAL_ADC_readDMA() once after initialization to start background acquisition.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PHAL_ADC_readBlocking()

bool PHAL_ADC_readBlocking ( PHAL_ADC_Handle_t * handle,
uint16_t * buffer,
uint16_t length,
uint32_t timeout )

Synchronous one-buffer conversion using the DMA engine.

Starts the circular acquisition, blocks until one buffer has completed, then stops the acquisition. A successful transfer fires PHAL_ADC_conversionCompleteCallback() before returning.

Note: timeout is a count of busy-wait iterations, not a wall-clock time. At 16 MHz one iteration is a handful of CPU cycles; the ADC itself takes roughly (640.5 + 12.5) / fADC seconds per sample (about 163 us at 4 MHz fADC).

Parameters
handleinitialized handle
bufferdestination for the samples
lengthnumber of 16-bit samples to collect
timeoutbusy-wait iteration budget (0 = return immediately)
Returns
true if all samples were collected, false on timeout or DMA error
Here is the call graph for this function:

◆ PHAL_ADC_readDMA()

bool PHAL_ADC_readDMA ( PHAL_ADC_Handle_t * handle,
uint16_t * buffer,
uint16_t length )

Start an asynchronous circular-DMA conversion into buffer

The ADC continuously refreshes buffer and remains active until PHAL_ADC_stop() is called. PHAL_ADC_conversionCompleteCallback() fires (from the DMA interrupt context) after each buffer wrap.

If length is not a multiple of the sequence length, the final burst is a partial sequence (still one sample per conversion).

Fails without starting anything if a transfer is already in flight, the handle was never initialized, buffer is null or not halfword-aligned, or length is zero.

Parameters
handleinitialized handle
bufferdestination, must stay valid until PHAL_ADC_stop() is called
lengthnumber of 16-bit samples to collect (1 - 65535)
Returns
true if the transfer was started, false otherwise
Note
Only one background transfer may be active for a handle.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PHAL_ADC_stop()

bool PHAL_ADC_stop ( PHAL_ADC_Handle_t * handle)

Abort any transfer in flight and leave the ADC idle.

Safe to call at any time, even with no transfer running (it is then a no-op). Does not fire the completion callback.

Parameters
handleinitialized handle
Returns
true if the handle is initialized, false otherwise
Here is the call graph for this function:

Variable Documentation

◆ PHAL_ADC_MAX_CHANNEL_COUNT

uint8_t PHAL_ADC_MAX_CHANNEL_COUNT = 16U
staticconstexpr

Maximum number of channels one ADC conversion sequence can hold.

◆ PHAL_ADC_MAX_CHANNEL_NUMBER

uint8_t PHAL_ADC_MAX_CHANNEL_NUMBER = 18U
staticconstexpr

Highest ADC channel number (IN18 on the G474)