| nRF5 SDK v12.0.0 |

Choose documentation: | nRF5 SDK | S130 SoftDevice API | S132 SoftDevice API | S212 SoftDevice API | S332 SoftDevice API

All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
PDM
This information applies to the nRF52 Series only.

The PDM driver includes two layers: the hardware access layer (HAL) and the driver layer (DRV).

The hardware access layer provides basic APIs for accessing the registers of the PDM peripheral. See the API documentation for the PDM HAL for details.

The driver layer provides APIs on a higher level than the HAL. See the API documentation for the PDM driver for details.

Key features include:

- Asynchronous conversion using DMA.
- Mono or stereo sampling.
- Adjustable gain levels.
- PCM output.

# Driver configuration

The PDM default configuration is located in `sdk_config.h`. The PDM peripheral must be explicitly enabled in `sdk_config.h` before the driver can be used. You must provide a valid configuration structure and an event handler function during initialization. The driver requires two sample buffers that are filled alternately.

The interface is initialized by calling the nrf_drv_pdm_init function. The driver configuration includes:

- Sampling mode.
- PDM clock frequency.
- Gain levels.
- Two sample buffers.
- Interrupt priority level.

Example:

uint16_t buff[2][512]
void pdm_event_handler(uint32_t * p_buffer, uint16_t length)
{
// Process the data.
}
/* ... */
nrf_drv_pdm_config_t pdm_cfg = NRF_DRV_PDM_DEFAULT_CONFIG(CLK_PIN, DOUT_PIN,
buff[0], buff[1], 512);
ret_code_t err_code = nrf_drv_pdm_init(&pdm_cfg, &pdm_event_handler);

# Using the PDM driver

After initialization, sampling is started by calling the nrf_drv_pdm_start function. When the current buffer is filled with data, the event handler function is called with the buffer pointer and size as arguments, and the driver starts filling the second buffer. The output PCM samples are stored as 16-bit signed integers. If stereo mode is selected, the buffer will contain alternating left and right channel samples. Sampling runs continuously until nrf_drv_pdm_stop is called.