mikroSDK Reference Manual

DMA Driver API Reference. More...

Functions list

void dma_configure_default (dma_config_t *config)
 Configure DMA Driver configuration structure.
 
err_t dma_open (dma_t *obj, dma_config_t *config)
 Open the DMA Driver object.
 
void dma_get_channels (dma_channel_t *channels)
 Get DMA modules/streams/channels.
 
err_t dma_set_mode (dma_t *obj, dma_mode_t mode)
 Set the DMA mode.
 
err_t dma_set_direction (dma_t *obj, dma_direction_t direction)
 Set the DMA direction.
 
err_t dma_set_priority (dma_t *obj, dma_priority_t priority)
 Set the DMA priority.
 
err_t dma_set_transfer_config (dma_t *obj, uint32_t addr_src, uint32_t addr_dst, size_t transfer_size, dma_source_memory_region_t src_mem_type)
 Configure the DMA transfer.
 
err_t dma_transfer_start (dma_t *obj)
 Initializes the DMA transfer.
 
err_t dma_transfer_stop (dma_t *obj)
 Stops the DMA transfer.
 
err_t dma_transfer_abort (dma_t *obj)
 Aborts a DMA transfer.
 
err_t dma_close (dma_t *obj)
 Close DMA Driver object.
 

Function Documentation

◆ dma_close()

err_t dma_close ( dma_t * obj)

Closes DMA driver object, aborts any ongoing transfers, disables the stream, deinitializes the module, clears all buffers used by object and disables module clock for lower power consumption.

Parameters
[in,out]objDMA driver object. See dma_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by err_t, which is dependant on the architecture and ported low level layer.

Example

// DMA driver context structure.
static dma_t dma;
// DMA driver configuration structure.
static dma_config_t dma_cfg;
// Fill structure with default values.
// Initialize and allocate resources for DMA module.
if ( DMA_ERROR == dma_open( &dma, &dma_cfg ) )
{
// Error handling strategy.
}
// Close previously initialized DMA module object handler.
if ( DMA_SUCCESS == dma_close( &dma ) {
// No error.
} else {
// Handle the error.
}
@ DMA_ERROR
Definition drv_dma.h:60
@ DMA_SUCCESS
Definition drv_dma.h:59
err_t dma_open(dma_t *obj, dma_config_t *config)
Open the DMA Driver object.
err_t dma_close(dma_t *obj)
Close DMA Driver object.
void dma_configure_default(dma_config_t *config)
Configure DMA Driver configuration structure.
DMA Configuration Structure prototype.
Definition drv_dma.h:177
DMA Handle prototype.
Definition drv_dma.h:230

◆ dma_configure_default()

void dma_configure_default ( dma_config_t * config)

Configures dma_config_t structure to default initialization values. Take into consideration that this is just structure variable initial values setting. Values need to be redefined by user.

Parameters
[in,out]configconfigure DMA driver configuration structure. See dma_config_t structure definition for detailed explanation.

Default values:

Function Default value
Module 0xFF
Stream 0xFF
Channel 0xFF
Direction DMA_DIRECTION_DEFAULT
Mode DMA_MODE_DEFAULT
Priority DMA_PRIORITY_DEFAULT
Memory increment false
Memory data alignment DMA_DATA_ALIGN_DEFAULT
Memory burst size DMA_BURST_SIZE_INCREMENT_DEFAULT
Peripheral increment false
Peripheral data alignment DMA_DATA_ALIGN_DEFAULT
Peripheral burst size DMA_BURST_SIZE_INCREMENT_DEFAULT
Source address 0
Destination address 0
Transfer length 0
Returns
Nothing.

Example

// DMA driver configuration structure.
static dma_config_t dma_cfg;
// Fill structure with default values.

◆ dma_get_channels()

void dma_get_channels ( dma_channel_t * channels)

Checks current MCU for available number of modules, streams and channels.

Parameters
[in,out]channelsDMA channel context array. See dma_channel_t definition for detailed explanation.
Returns
Nothing.
Precondition
Make sure that channels array has been adequately instantiated before calling this API.
Note
It is recommended to use this API in order to check available resources and see which ones are free to use.

Example

// DMA channel context structure.
static dma_channel_t dma_channels;
// Get list of available channels per stream per module.
dma_get_channels( &dma_channels );
// Lets say we want to use DMA module 0, stream 3 and channel 4.
if ( DMA_STATE_FREE == dma_channels[0][3][4] )
{
// Module 0, stream 3, channel 4 is free for use.
}
uint8_t dma_channel_t[DMA_NUM_OF_MODULES][DMA_NUM_OF_STREAMS_FAMILY][DMA_NUM_OF_CHANNELS_FAMILY]
Definition drv_dma.h:153
@ DMA_STATE_FREE
Definition drv_dma.h:68
void dma_get_channels(dma_channel_t *channels)
Get DMA modules/streams/channels.

◆ dma_open()

err_t dma_open ( dma_t * obj,
dma_config_t * config )

Opens the DMA driver object on selected module, stream and channel. Allocates memory for specified object. Additionally, initializes the DMA module.

Parameters
[in,out]objDMA driver object. See dma_t structure definition for detailed explanation.
[in]configconfigure DMA driver configuration settings. See dma_config_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by err_t, which is dependant on the architecture and ported low level layer.
Precondition
Make sure that configuration structure has been adequately populated beforehand. See dma_configure_default definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// DMA driver context structure.
static dma_t dma;
// DMA driver configuration structure.
static dma_config_t dma_cfg;
// Fill structure with default values.
// Specify desired DMA module.
dma_cfg.module = 0; // First module (DMA0 or DMA1 based on MCU).
// Specify desired DMA stream number.
dma_cfg.stream = 5;
// Specify desired DMA channel number.
dma_cfg.channel = 3;
// Initialize and allocate resources for DMA module.
if ( DMA_ERROR == dma_open( &dma, &dma_cfg ) )
{
// Error handling strategy.
}
uint8_t channel
Definition drv_dma.h:180
uint8_t uint8_t stream
Definition drv_dma.h:179

◆ dma_set_direction()

err_t dma_set_direction ( dma_t * obj,
dma_direction_t direction )

Sets DMA direction to be used by the DMA driver.

Parameters
[in,out]objDMA driver object. See dma_t structure definition for detailed explanation.
[in]directionDMA direction. See dma_direction_t for valid values.
Returns
The function can return one of the values defined by err_t, which is dependant on the architecture and ported low level layer.
Precondition
Make sure that adequate memory has been allocated beforehand. See dma_open definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// DMA driver context structure.
static dma_t dma;
// DMA driver configuration structure.
static dma_config_t dma_cfg;
// Fill structure with default values.
// Initialize and allocate resources for DMA module.
if ( DMA_ERROR == dma_open( &dma, &dma_cfg ) )
{
// Error handling strategy.
}
// Set desired DMA mode.
{
// Error handling strategy.
}
@ DMA_DIRECTION_MEMORY_TO_MEMORY
Definition drv_dma.h:87
err_t dma_set_direction(dma_t *obj, dma_direction_t direction)
Set the DMA direction.

◆ dma_set_mode()

err_t dma_set_mode ( dma_t * obj,
dma_mode_t mode )

Sets DMA mode to be used by the DMA driver.

Parameters
[in,out]objDMA driver object. See dma_t structure definition for detailed explanation.
[in]modeDMA mode. See dma_mode_t for valid values.
Returns
The function can return one of the values defined by err_t, which is dependant on the architecture and ported low level layer.
Precondition
Make sure that adequate memory has been allocated beforehand. See dma_open definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// DMA driver context structure.
static dma_t dma;
// DMA driver configuration structure.
static dma_config_t dma_cfg;
// Fill structure with default values.
// Initialize and allocate resources for DMA module.
if ( DMA_ERROR == dma_open( &dma, &dma_cfg ) )
{
// Error handling strategy.
}
// Set desired DMA mode.
{
// Error handling strategy.
}
@ DMA_MODE_NORMAL
Definition drv_dma.h:113
err_t dma_set_mode(dma_t *obj, dma_mode_t mode)
Set the DMA mode.

◆ dma_set_priority()

err_t dma_set_priority ( dma_t * obj,
dma_priority_t priority )

Sets DMA priority level to be used by the DMA driver.

Parameters
[in,out]objDMA driver object. See dma_t structure definition for detailed explanation.
[in]priorityDMA priority level. See dma_priority_t for valid values.
Returns
The function can return one of the values defined by err_t, which is dependant on the architecture and ported low level layer.
Precondition
Make sure that adequate memory has been allocated beforehand. See dma_open definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// DMA driver context structure.
static dma_t dma;
// DMA driver configuration structure.
static dma_config_t dma_cfg;
// Fill structure with default values.
// Initialize and allocate resources for DMA module.
if ( DMA_ERROR == dma_open( &dma, &dma_cfg ) )
{
// Error handling strategy.
}
// Set desired DMA mode.
{
// Error handling strategy.
}
@ DMA_PRIORITY_MEDIUM
Definition drv_dma.h:125
err_t dma_set_priority(dma_t *obj, dma_priority_t priority)
Set the DMA priority.

◆ dma_set_transfer_config()

err_t dma_set_transfer_config ( dma_t * obj,
uint32_t addr_src,
uint32_t addr_dst,
size_t transfer_size,
dma_source_memory_region_t src_mem_type )

Configures initialized DMA stream for transfer.

Parameters
[in,out]objDMA driver object. See dma_t structure definition for detailed explanation.
[in]addr_srcSource address for transfer.
[in]addr_dstDestination address for transfer.
[in]transfer_sizeNumber of bytes to transfer.
[in]src_mem_typeSource address memory region.
Returns
The function can return one of the values defined by err_t, which is dependant on the architecture and ported low level layer.
Precondition
Make sure that adequate memory has been allocated beforehand. See dma_open definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// DMA driver context structure.
static dma_t dma;
// DMA driver configuration structure.
static dma_config_t dma_cfg;
// Source buffer in flash.
static const uint8_t buffer_src[2] = {0xCa, 0xfe};
// Destination buffer in RAM.
static uint8_t buffer_dst[2];
// Fill structure with default values.
// Set data alignment to BYTE(8-bit) because
// arrays are byte size.
dma_cfg.data_align_memory = DMA_DATA_ALIGN_BYTES_1;
dma_cfg.data_align_peripheral = DMA_DATA_ALIGN_BYTES_1;
// Set memory addresses to be incremented
// for both memory and peripheral.
dma_cfg.mem_inc = true;
dma_cfg.periph_inc = true;
// Initialize and allocate resources for DMA module.
if ( DMA_ERROR == dma_open( &dma, &dma_cfg ) )
{
// Error handling strategy.
}
// Configure transfer to copy 2 bytes from buffer_src to buffer_dst.
if ( DMA_ERROR == dma_set_transfer_config( &dma, (uint32_t)buffer_src, (uint32_t)buffer_dst, 2, HAL_DMA_SOURCE_MEMORY_REGION_PFM ) )
{
// Error handling strategy.
}
// Transfer is now configured and can be started.
@ DMA_DATA_ALIGN_BYTES_1
Definition drv_dma.h:100
err_t dma_set_transfer_config(dma_t *obj, uint32_t addr_src, uint32_t addr_dst, size_t transfer_size, dma_source_memory_region_t src_mem_type)
Configure the DMA transfer.
@ HAL_DMA_SOURCE_MEMORY_REGION_PFM
Definition hal_dma.h:77

◆ dma_transfer_abort()

err_t dma_transfer_abort ( dma_t * obj)

Aborts any ongoing DMA transfers and disables stream.

Parameters
[in,out]objDMA driver object. See dma_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by err_t, which is dependant on the architecture and ported low level layer.
Precondition
Make sure that adequate memory has been allocated beforehand. See dma_open definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// DMA driver context structure.
static dma_t dma;
// DMA driver configuration structure.
static dma_config_t dma_cfg;
// Source buffer in flash.
static const uint8_t buffer_src[2] = {0xCa, 0xfe};
// Destination buffer in RAM.
static uint8_t buffer_dst[2];
// Fill structure with default values.
// Set data alignment to BYTE(8-bit) because
// arrays are byte size.
dma_cfg.data_align_memory = DMA_DATA_ALIGN_BYTES_1;
dma_cfg.data_align_peripheral = DMA_DATA_ALIGN_BYTES_1;
// Set memory addresses to be incremented
// for both memory and peripheral.
dma_cfg.mem_inc = true;
dma_cfg.periph_inc = true;
// Initialize and allocate resources for DMA module.
if ( DMA_ERROR == dma_open( &dma, &dma_cfg ) )
{
// Error handling strategy.
}
// Configure transfer to copy 2 bytes from buffer_src to buffer_dst.
if ( DMA_ERROR == dma_set_transfer_config( &dma, (uint32_t)buffer_src, (uint32_t)buffer_dst, 2 ) )
{
// Error handling strategy.
}
// Start the transfer and check the destination buffer.
if ( DMA_ERROR == dma_transfer_start( &dma ) )
{
// Error handling strategy.
}
// Compare buffers now.
if ( !memcmp(buffer_src_flash, buffer_dst, sizeof(buffer_src)) )
{
// Successful comparison.
}
// Abort any further ongoing transfers.
if ( DMA_ERROR == dma_transfer_abort( &dma ) )
{
// Error handling strategy.
}
err_t dma_transfer_start(dma_t *obj)
Initializes the DMA transfer.
err_t dma_transfer_abort(dma_t *obj)
Aborts a DMA transfer.

◆ dma_transfer_start()

err_t dma_transfer_start ( dma_t * obj)

Starts previously configured DMA transfer by enabling stream.

Parameters
[in,out]objDMA driver object. See dma_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by err_t, which is dependant on the architecture and ported low level layer.
Precondition
Make sure that adequate memory has been allocated beforehand. See dma_open definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// DMA driver context structure.
static dma_t dma;
// DMA driver configuration structure.
static dma_config_t dma_cfg;
// Source buffer in flash.
static const uint8_t buffer_src[2] = {0xCa, 0xfe};
// Destination buffer in RAM.
static uint8_t buffer_dst[2];
// Fill structure with default values.
// Set data alignment to BYTE(8-bit) because
// arrays are byte size.
dma_cfg.data_align_memory = DMA_DATA_ALIGN_BYTES_1;
dma_cfg.data_align_peripheral = DMA_DATA_ALIGN_BYTES_1;
// Set memory addresses to be incremented
// for both memory and peripheral.
dma_cfg.mem_inc = true;
dma_cfg.periph_inc = true;
// Initialize and allocate resources for DMA module.
if ( DMA_ERROR == dma_open( &dma, &dma_cfg ) )
{
// Error handling strategy.
}
// Configure transfer to copy 2 bytes from buffer_src to buffer_dst.
if ( DMA_ERROR == dma_set_transfer_config( &dma, (uint32_t)buffer_src, (uint32_t)buffer_dst, 2 ) )
{
// Error handling strategy.
}
// Start the transfer and check the destination buffer.
if ( DMA_ERROR == dma_transfer_start( &dma ) )
{
// Error handling strategy.
}
// Compare buffers now.
if ( !memcmp(buffer_src_flash, buffer_dst, sizeof(buffer_src)) )
{
// Successful comparison.
}

◆ dma_transfer_stop()

err_t dma_transfer_stop ( dma_t * obj)

Stops an active DMA transfer by disabling stream.

Parameters
[in,out]objDMA driver object. See dma_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by err_t, which is dependant on the architecture and ported low level layer.
Precondition
Make sure that adequate memory has been allocated beforehand. See dma_open definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// DMA driver context structure.
static dma_t dma;
// DMA driver configuration structure.
static dma_config_t dma_cfg;
// Source buffer in flash.
static const uint8_t buffer_src[2] = {0xCa, 0xfe};
// Destination buffer in RAM.
static uint8_t buffer_dst[2];
// Fill structure with default values.
// Set data alignment to BYTE(8-bit) because
// arrays are byte size.
dma_cfg.data_align_memory = DMA_DATA_ALIGN_BYTES_1;
dma_cfg.data_align_peripheral = DMA_DATA_ALIGN_BYTES_1;
// Set memory addresses to be incremented
// for both memory and peripheral.
dma_cfg.mem_inc = true;
dma_cfg.periph_inc = true;
// Initialize and allocate resources for DMA module.
if ( DMA_ERROR == dma_open( &dma, &dma_cfg ) )
{
// Error handling strategy.
}
// Configure transfer to copy 2 bytes from buffer_src to buffer_dst.
if ( DMA_ERROR == dma_set_transfer_config( &dma, (uint32_t)buffer_src, (uint32_t)buffer_dst, 2 ) )
{
// Error handling strategy.
}
// Start the transfer and check the destination buffer.
if ( DMA_ERROR == dma_transfer_start( &dma ) )
{
// Error handling strategy.
}
// Compare buffers now.
if ( !memcmp(buffer_src_flash, buffer_dst, sizeof(buffer_src)) )
{
// Successful comparison. Stop the stream now.
if ( DMA_ERROR == dma_transfer_stop( &dma ) )
{
// Error handling strategy.
}
}
err_t dma_transfer_stop(dma_t *obj)
Stops the DMA transfer.