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...
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)
◆ adc_claim_completion()
Atomically claim transfer completion for either polling or interrupt code.
◆ adc_config_is_valid()
Validate a public ADC configuration before touching hardware.
◆ adc_dma_callback()
| static void adc_dma_callback |
( |
void * | ctx | ) |
|
|
static |
◆ adc_dma_irq_handler()
Process DMA completion or error status for an ADC handle.
◆ 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.
◆ adc_dma_wiring()
Return the fixed DMA wiring assigned to an ADC instance.
◆ 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.
◆ adc_teardown()
Stop the ADC and DMA channel, then clear the DMA status flags.
◆ PHAL_ADC_busy()
Check whether a transfer is currently in flight.
- Parameters
-
- Returns
- true while a PHAL_ADC_readDMA() transfer is running
◆ PHAL_ADC_conversionCompleteCallback()
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
-
| handle | the handle whose transfer just completed |
◆ PHAL_ADC_init()
Configure an ADC instance and its DMA channel.
What this does, in order:
- Enables the ADC (and DMA) clocks
- Runs the ADC hardware calibration
- Configures the conversion sequence, sample times, and fixed conversion options (12-bit, right-aligned, continuous, DMA-enabled, no oversampling)
- Enables the ADC and waits until it reports ready
- 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
-
| handle | zero-initialized handle to populate |
| config | static 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.
◆ 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
-
| handle | initialized handle |
| buffer | destination for the samples |
| length | number of 16-bit samples to collect |
| timeout | busy-wait iteration budget (0 = return immediately) |
- Returns
- true if all samples were collected, false on timeout or DMA error
◆ 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
-
| handle | initialized handle |
| buffer | destination, must stay valid until PHAL_ADC_stop() is called |
| length | number 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.
◆ PHAL_ADC_stop()
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
-
- Returns
- true if the handle is initialized, false otherwise
◆ g_active_adc