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

G4 ADC public API implementation. All register-level detail lives in adc_priv.c; this file only coordinates the ADC with its DMA channel. More...

Include dependency graph for adc.c:

Functions

static void adc_dma_callback (void *ctx)
 
static uint8_t adc_instance_index (ADC_TypeDef *instance)
 Map a supported ADC instance to its zero-based active-handle slot.
 
static const PHAL_DMA_Wiring_tadc_dma_wiring (ADC_TypeDef *instance)
 Return the fixed DMA wiring assigned to an ADC instance.
 
static bool adc_config_is_valid (const PHAL_ADC_Config_t *config)
 Validate a public ADC configuration before touching hardware.
 
static IRQn_Type adc_dma_irqn (ADC_TypeDef *instance)
 Return the NVIC interrupt assigned to an ADC instance's DMA channel.
 
static bool adc_claim_completion (PHAL_ADC_Handle_t *handle)
 Atomically claim transfer completion for either polling or interrupt code.
 
static void adc_teardown (PHAL_ADC_Handle_t *handle)
 Stop the ADC and DMA channel, then clear the DMA status flags.
 
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.
 
static void adc_dma_irq_handler (PHAL_ADC_Handle_t *handle)
 Process DMA completion or error status for an ADC handle.
 
void PHAL_ADC_conversionCompleteCallback (PHAL_ADC_Handle_t *handle)
 Weak callback fired once per completed DMA buffer.
 

Variables

static PHAL_ADC_Handle_tg_active_adc [4]
 

Detailed Description

G4 ADC public API implementation. All register-level detail lives in adc_priv.c; this file only coordinates the ADC with its DMA channel.

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

Function Documentation

◆ adc_claim_completion()

static bool adc_claim_completion ( PHAL_ADC_Handle_t * handle)
static

Atomically claim transfer completion for either polling or interrupt code.

Here is the caller graph for this function:

◆ adc_config_is_valid()

static bool adc_config_is_valid ( const PHAL_ADC_Config_t * config)
static

Validate a public ADC configuration before touching hardware.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ adc_dma_callback()

static void adc_dma_callback ( void * ctx)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ adc_dma_irq_handler()

static void adc_dma_irq_handler ( PHAL_ADC_Handle_t * handle)
static

Process DMA completion or error status for an ADC handle.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ adc_dma_irqn()

static IRQn_Type adc_dma_irqn ( ADC_TypeDef * instance)
static

Return the NVIC interrupt assigned to an ADC instance's DMA channel.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ adc_dma_wiring()

static const PHAL_DMA_Wiring_t * adc_dma_wiring ( ADC_TypeDef * instance)
static

Return the fixed DMA wiring assigned to an ADC instance.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ adc_instance_index()

static uint8_t adc_instance_index ( ADC_TypeDef * instance)
static

Map a supported ADC instance to its zero-based active-handle slot.

Here is the caller graph for this function:

◆ adc_teardown()

static void adc_teardown ( PHAL_ADC_Handle_t * handle)
static

Stop the ADC and DMA channel, then clear the DMA status flags.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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)

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

◆ g_active_adc

PHAL_ADC_Handle_t* g_active_adc[4]
static