Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Direct Memory Access controller (DMA)

See Quick start guide for the XMEGA DMA driver.

This is a driver for configuration, enabling, disabling and use of the on-chip XMEGA Direct Memory Access (DMA) controller.

Dependencies

The direct memory access controller depends on the following modules:

Modules

 
 Quick Start Guide(s)
 In this section you can find a list of all Quick Start guides related to the Direct Memory Access controller (DMA).
 

Data Structures

struct  dma_channel_config
 DMA channel configuration struct. More...
 

Macros

#define dma_get_channel_address_from_num(num)   ((DMA_CH_t *)((uintptr_t)(&DMA.CH0) + (sizeof(DMA_CH_t) * num)))
 Helper macro to get the channel offset given the channel number. More...
 
#define DMA_NUMBER_OF_CHANNELS   2
 Number of available DMA channels, device dependent. More...
 

Typedefs

typedef uint8_t dma_channel_num_t
 DMA channel number type. More...
 

Enumerations

enum  dma_channel_status {
  DMA_CH_FREE = 0,
  DMA_CH_PENDING,
  DMA_CH_BUSY,
  DMA_CH_TRANSFER_COMPLETED,
  DMA_CH_TRANSFER_ERROR
}
 DMA channel status. More...
 
enum  dma_int_level_t {
  DMA_INT_LVL_OFF = 0x00,
  DMA_INT_LVL_LO = 0x01,
  DMA_INT_LVL_MED = 0x02,
  DMA_INT_LVL_HI = 0x03
}
 DMA interrupt levels. More...
 

Functions

void dma_disable (void)
 Disable DMA controller. More...
 
void dma_enable (void)
 Enable DMA controller. More...
 

DMA Controller Management

static void dma_set_priority_mode (DMA_PRIMODE_t primode)
 Set DMA channel priority mode. More...
 
static void dma_set_double_buffer_mode (DMA_DBUFMODE_t dbufmode)
 Set DMA channel double buffering mode. More...
 

DMA Channel Management

typedef void(* dma_callback_t )(enum dma_channel_status status)
 Callback definition for DMA channel interrupts. More...
 
void dma_channel_write_config (dma_channel_num_t num, struct dma_channel_config *config)
 Write DMA channel configuration to hardware. More...
 
void dma_channel_read_config (dma_channel_num_t num, struct dma_channel_config *config)
 Read DMA channel configuration from hardware. More...
 
static void dma_channel_enable (dma_channel_num_t num)
 Enable a DMA channel. More...
 
static void dma_channel_disable (dma_channel_num_t num)
 Disable a DMA channel. More...
 
static bool dma_channel_is_enabled (dma_channel_num_t num)
 Check if DMA channel is enabled. More...
 
static bool dma_channel_is_busy (dma_channel_num_t num)
 Check if DMA channel is busy. More...
 
static enum dma_channel_status dma_get_channel_status (dma_channel_num_t num)
 Get channel status. More...
 
static void dma_channel_trigger_block_transfer (dma_channel_num_t num)
 DMA channel trigger block transfer. More...
 
static void dma_channel_reset (dma_channel_num_t num)
 Reset a DMA channel. More...
 
void dma_set_callback (dma_channel_num_t num, dma_callback_t callback)
 Set callback for given DMA channel. More...
 

DMA Channel Direct Configuration Functions

These functions allows direct configuration on the DMA channels, not going through a dma_channel_config struct.

This allows update of the most commonly changed DMA channel properties.

static void dma_channel_write_burst_length (dma_channel_num_t num, DMA_CH_BURSTLEN_t burst_length)
 Write DMA channel burst length to hardware. More...
 
static void dma_channel_write_transfer_count (dma_channel_num_t num, uint16_t count)
 Write DMA channel transfer count to hardware. More...
 
static void dma_channel_write_repeats (dma_channel_num_t num, uint8_t repeats)
 Write DMA channel number of transfer repeats to hardware. More...
 
static void dma_channel_write_destination (dma_channel_num_t num, uint16_t destination)
 Write DMA channel 16-bit destination address to hardware. More...
 
static void dma_channel_write_source (dma_channel_num_t num, uint16_t source)
 Write DMA channel 16-bit source address to hardware. More...
 

DMA Channel Configuration Helper Functions

static void dma_channel_set_burst_length (struct dma_channel_config *config, DMA_CH_BURSTLEN_t burst_length)
 Set DMA channel burst length. More...
 
static void dma_channel_set_single_shot (struct dma_channel_config *config)
 Set DMA channel in single shot transfer mode. More...
 
static void dma_channel_unset_single_shot (struct dma_channel_config *config)
 Unset DMA channel from single shot transfer mode. More...
 
static void dma_channel_set_interrupt_level (struct dma_channel_config *config, enum dma_int_level_t level)
 Set DMA channel interrupt level. More...
 
static void dma_channel_set_src_reload_mode (struct dma_channel_config *config, DMA_CH_SRCRELOAD_t mode)
 Set DMA channel source address reload mode. More...
 
static void dma_channel_set_dest_reload_mode (struct dma_channel_config *config, DMA_CH_DESTRELOAD_t mode)
 Set DMA channel destination address reload mode. More...
 
static void dma_channel_set_src_dir_mode (struct dma_channel_config *config, DMA_CH_SRCDIR_t mode)
 Set DMA channel source addressing mode. More...
 
static void dma_channel_set_dest_dir_mode (struct dma_channel_config *config, DMA_CH_DESTDIR_t mode)
 Set DMA channel destination addressing mode. More...
 
static void dma_channel_set_trigger_source (struct dma_channel_config *config, DMA_CH_TRIGSRC_t source)
 Set DMA channel trigger source. More...
 
static void dma_channel_set_transfer_count (struct dma_channel_config *config, uint16_t count)
 Set DMA channel transfer count. More...
 
static void dma_channel_set_repeats (struct dma_channel_config *config, uint8_t repeats)
 Set DMA channel number of transfer repeats. More...
 
static void dma_channel_set_destination_address (struct dma_channel_config *config, uint16_t destination)
 Set DMA channel 16-bit destination address. More...
 
static void dma_channel_set_source_address (struct dma_channel_config *config, uint16_t source)
 Set DMA channel 16-bit source address. More...
 

#define dma_get_channel_address_from_num (   num)    ((DMA_CH_t *)((uintptr_t)(&DMA.CH0) + (sizeof(DMA_CH_t) * num)))
#define DMA_NUMBER_OF_CHANNELS   2

Number of available DMA channels, device dependent.

Referenced by run_dma_config_interface_test(), run_dma_direction_test(), and run_dma_memory_copy_burst_length_test().

typedef void(* dma_callback_t)(enum dma_channel_status status)

Callback definition for DMA channel interrupts.

Parameters
statusStatus of DMA channel block transfer
typedef uint8_t dma_channel_num_t

DMA channel number type.

DMA channel status.

Enumerator
DMA_CH_FREE 

DMA channel is idling.

DMA_CH_PENDING 

DMA channel has a block transfer pending.

DMA_CH_BUSY 

DMA channel is busy doing a block transfer.

DMA_CH_TRANSFER_COMPLETED 

DMA channel has completed a block transfer.

DMA_CH_TRANSFER_ERROR 

DMA channel failed to complete a block transfer.

DMA interrupt levels.

Enumerator
DMA_INT_LVL_OFF 
DMA_INT_LVL_LO 
DMA_INT_LVL_MED 
DMA_INT_LVL_HI 

static void dma_channel_disable ( dma_channel_num_t  num)
inlinestatic

Disable a DMA channel.

This function disables a DMA channel. If the DMA channel was already enabled the DMA channel is not disabled before the internal transfer buffer is empty and the DMA transfer is aborted.

Parameters
numDMA channel to disable

References cpu_irq_restore(), cpu_irq_save(), and dma_get_channel_address_from_num.

Referenced by run_dma_direction_test(), run_dma_double_buffering_test(), run_dma_error_handling_test(), and run_dma_memory_copy_burst_length_test().

static void dma_channel_enable ( dma_channel_num_t  num)
inlinestatic

Enable a DMA channel.

This function enables a DMA channel, depending on the configuration the DMA channel will start the block transfer upon a dma_channel_trigger_block_transfer() or when other hardware modules trigger the DMA hardware.

Precondition
The DMA channel configuration must be written to the channel before enabling the channel.
Parameters
numDMA channel to enable

References cpu_irq_restore(), cpu_irq_save(), and dma_get_channel_address_from_num.

Referenced by aes_dma_input(), aes_dma_output(), main(), run_dma_direction_test(), run_dma_double_buffering_test(), run_dma_error_handling_test(), run_dma_memory_copy_burst_length_test(), and run_dma_triggered_with_callback().

static bool dma_channel_is_busy ( dma_channel_num_t  num)
inlinestatic

Check if DMA channel is busy.

This function checks if a DMA channel is busy doing or going to do (pending) a block transfer.

Parameters
numDMA channel number to query
Return values
trueDMA channel is busy or have a block transfer pending
falseDMA channel is not busy or have a block transfer pending

Referenced by run_dma_error_handling_test().

static bool dma_channel_is_enabled ( dma_channel_num_t  num)
inlinestatic

Check if DMA channel is enabled.

This function checks if a DMA channel is enabled.

Parameters
numDMA channel number to query
Return values
trueDMA channel is enabled
falseDMA channel is disabled

References dma_get_channel_address_from_num.

Referenced by run_dma_double_buffering_test().

void dma_channel_read_config ( dma_channel_num_t  num,
struct dma_channel_config config 
)

Read DMA channel configuration from hardware.

This function will read the DMA channel configuration into a dma_channel_config.

Parameters
numDMA channel number to read configuration from
configPointer to a DMA channel config, given by a dma_channel_config

References dma_channel_config::addrctrl, cpu_irq_restore(), cpu_irq_save(), dma_channel_config::ctrla, dma_channel_config::ctrlb, dma_channel_config::destaddr16, dma_get_channel_address_from_num, dma_channel_config::repcnt, dma_channel_config::srcaddr16, dma_channel_config::trfcnt, and dma_channel_config::trigsrc.

Referenced by run_dma_config_interface_test().

static void dma_channel_reset ( dma_channel_num_t  num)
inlinestatic

Reset a DMA channel.

This function resets a given DMA channel.

Parameters
numDMA channel number to trigger block transfer for

References cpu_irq_restore(), cpu_irq_save(), and dma_get_channel_address_from_num.

Referenced by run_dma_config_interface_test(), run_dma_direction_test(), run_dma_error_handling_test(), run_dma_memory_copy_burst_length_test(), and run_dma_triggered_with_callback().

static void dma_channel_set_burst_length ( struct dma_channel_config config,
DMA_CH_BURSTLEN_t  burst_length 
)
inlinestatic

Set DMA channel burst length.

This function helps the caller setting the DMA channel burst length. The burst lengths are defined in the toolchain header files in the form of DMA_CH_BURSTLEN_*_gc, where * represents the various available burst lengths.

Parameters
configPointer to a dma_channel_config variable
burst_lengthDMA channel burst length given by a DMA_CH_BURSTLEN_t type

References dma_channel_config::ctrla.

Referenced by aes_dma_input(), aes_dma_output(), main(), run_dma_config_interface_test(), run_dma_direction_test(), run_dma_double_buffering_test(), run_dma_memory_copy_burst_length_test(), and run_dma_triggered_with_callback().

static void dma_channel_set_dest_dir_mode ( struct dma_channel_config config,
DMA_CH_DESTDIR_t  mode 
)
inlinestatic

Set DMA channel destination addressing mode.

This function helps the caller setting the DMA channel destination addressing mode. The reload modes are defined in the toolchain header files in the form of DMA_CH_DESTDIR_*_gc, where * is FIXED, INC, DEC.

Parameters
configPointer to a dma_channel_config variable
modeDMA channel destination addressing mode given by an DMA_CH_DESTDIR_t type

References dma_channel_config::addrctrl.

Referenced by aes_dma_input(), aes_dma_output(), main(), run_dma_config_interface_test(), run_dma_direction_test(), run_dma_double_buffering_test(), run_dma_memory_copy_burst_length_test(), and run_dma_triggered_with_callback().

static void dma_channel_set_dest_reload_mode ( struct dma_channel_config config,
DMA_CH_DESTRELOAD_t  mode 
)
inlinestatic

Set DMA channel destination address reload mode.

This function helps the caller setting the DMA channel destination address reload mode. The reload modes are defined in the toolchain header files in the form of DMA_CH_DESTRELOAD_*_gc, where * is NONE, BLOCK, BURST, TRANSACTION.

Parameters
configPointer to a dma_channel_config variable
modeDMA channel destination address reload mode given by an DMA_CH_DESTRELOAD_t type

References dma_channel_config::addrctrl.

Referenced by aes_dma_input(), aes_dma_output(), main(), run_dma_config_interface_test(), run_dma_direction_test(), run_dma_double_buffering_test(), run_dma_memory_copy_burst_length_test(), and run_dma_triggered_with_callback().

static void dma_channel_set_destination_address ( struct dma_channel_config config,
uint16_t  destination 
)
inlinestatic

Set DMA channel 16-bit destination address.

This function helps the caller setting the DMA channel destination 16-bit LSB address.

Note
This function assumes the config was initialized with zeros (static or intentionally zeroed). If not zero filled, there might be garbage left in the upper byte of the destination address.
Parameters
configPointer to a dma_channel_config variable
destination16-bit LSB destination address

References dma_channel_config::destaddr16, and destination.

Referenced by aes_dma_input(), aes_dma_output(), main(), run_dma_config_interface_test(), run_dma_direction_test(), run_dma_double_buffering_test(), run_dma_memory_copy_burst_length_test(), and run_dma_triggered_with_callback().

static void dma_channel_set_interrupt_level ( struct dma_channel_config config,
enum dma_int_level_t  level 
)
inlinestatic

Set DMA channel interrupt level.

This function helps the caller setting the DMA channel interrupt level for transaction complete and channel error.

Parameters
configPointer to a dma_channel_config variable
levelInterrupt level given by a dma_int_level_t type

References dma_channel_config::ctrlb.

Referenced by main(), run_dma_config_interface_test(), and run_dma_triggered_with_callback().

static void dma_channel_set_repeats ( struct dma_channel_config config,
uint8_t  repeats 
)
inlinestatic

Set DMA channel number of transfer repeats.

This function helps the caller setting the DMA channel number of block transfer repeats. It will also enable the repeat feature.

Parameters
configPointer to a dma_channel_config variable
repeatsNumber of block transfer repeats

References dma_channel_config::ctrla, and dma_channel_config::repcnt.

Referenced by run_dma_config_interface_test(), and run_dma_double_buffering_test().

static void dma_channel_set_single_shot ( struct dma_channel_config config)
inlinestatic

Set DMA channel in single shot transfer mode.

This function helps the caller setting the DMA channel in single shot transfer mode.

Parameters
configPointer to a dma_channel_config variable

References dma_channel_config::ctrla.

Referenced by run_dma_config_interface_test(), and run_dma_triggered_with_callback().

static void dma_channel_set_source_address ( struct dma_channel_config config,
uint16_t  source 
)
inlinestatic

Set DMA channel 16-bit source address.

This function helps the caller setting the DMA channel source 16-bit LSB address.

Note
This function assumes the config was initialized with zeros (static or intentionally zeroed). If not zero filled, there might be garbage left in the upper byte of the source address.
Parameters
configPointer to a dma_channel_config variable
source16-bit LSB source address

References source, and dma_channel_config::srcaddr16.

Referenced by aes_dma_input(), aes_dma_output(), main(), run_dma_config_interface_test(), run_dma_direction_test(), run_dma_double_buffering_test(), run_dma_memory_copy_burst_length_test(), and run_dma_triggered_with_callback().

static void dma_channel_set_src_dir_mode ( struct dma_channel_config config,
DMA_CH_SRCDIR_t  mode 
)
inlinestatic

Set DMA channel source addressing mode.

This function helps the caller setting the DMA channel source addressing mode. The addressing modes are defined in the toolchain header files in the form of DMA_CH_SRCDIR_*_gc, where * is FIXED, INC, DEC.

Parameters
configPointer to a dma_channel_config variable
modeDMA channel source addressing mode given by an DMA_CH_SRCDIR_t type

References dma_channel_config::addrctrl.

Referenced by aes_dma_input(), aes_dma_output(), main(), run_dma_config_interface_test(), run_dma_direction_test(), run_dma_double_buffering_test(), run_dma_memory_copy_burst_length_test(), and run_dma_triggered_with_callback().

static void dma_channel_set_src_reload_mode ( struct dma_channel_config config,
DMA_CH_SRCRELOAD_t  mode 
)
inlinestatic

Set DMA channel source address reload mode.

This function helps the caller setting the DMA channel source address reload mode. The reload modes are defined in the toolchain header files in the form of DMA_CH_SRCRELOAD_*_gc, where * is NONE, BLOCK, BURST, TRANSACTION.

Parameters
configPointer to a dma_channel_config variable
modeDMA channel source address reload mode given by an DMA_CH_SRCRELOAD_t type

References dma_channel_config::addrctrl.

Referenced by aes_dma_input(), aes_dma_output(), main(), run_dma_config_interface_test(), run_dma_direction_test(), run_dma_double_buffering_test(), run_dma_memory_copy_burst_length_test(), and run_dma_triggered_with_callback().

static void dma_channel_set_transfer_count ( struct dma_channel_config config,
uint16_t  count 
)
inlinestatic

Set DMA channel transfer count.

This function helps the caller setting the DMA channel number of bytes in each block transfer.

Parameters
configPointer to a dma_channel_config variable
countNumber of bytes in each block transfer

References dma_channel_config::trfcnt.

Referenced by aes_dma_input(), aes_dma_output(), main(), run_dma_config_interface_test(), run_dma_direction_test(), run_dma_double_buffering_test(), run_dma_memory_copy_burst_length_test(), and run_dma_triggered_with_callback().

static void dma_channel_set_trigger_source ( struct dma_channel_config config,
DMA_CH_TRIGSRC_t  source 
)
inlinestatic

Set DMA channel trigger source.

This function helps the caller setting the DMA channel trigger source. The trigger sources are defined in the toolchain header files in the form of DMA_CH_TRIGSRC_*_gc, where * represents the various sources.

Parameters
configPointer to a dma_channel_config variable
sourceDMA channel trigger source given by a DMA_CH_TRIGSRC_t type

References source, and dma_channel_config::trigsrc.

Referenced by run_dma_config_interface_test(), and run_dma_triggered_with_callback().

static void dma_channel_trigger_block_transfer ( dma_channel_num_t  num)
inlinestatic

DMA channel trigger block transfer.

This function triggers a start of a block transfer on a given DMA channel.

Precondition
The DMA channel must be configured and enabled for this to have any affect.
Parameters
numDMA channel number to trigger block transfer for

References cpu_irq_restore(), cpu_irq_save(), and dma_get_channel_address_from_num.

Referenced by aes_dma_input(), aes_dma_output(), dma_transfer_block(), main(), run_dma_double_buffering_test(), and run_dma_error_handling_test().

static void dma_channel_unset_single_shot ( struct dma_channel_config config)
inlinestatic

Unset DMA channel from single shot transfer mode.

This function helps the caller clear the DMA channel single shot transfer mode feature.

Parameters
configPointer to a dma_channel_config variable

References dma_channel_config::ctrla.

Referenced by run_dma_config_interface_test().

static void dma_channel_write_burst_length ( dma_channel_num_t  num,
DMA_CH_BURSTLEN_t  burst_length 
)
inlinestatic

Write DMA channel burst length to hardware.

This function writes the DMA channel burst length directly to hardware. The burst lengths are defined in the toolchain header files in the form of DMA_CH_BURSTLEN_*_gc, where * represents the various available burst lengths.

Parameters
numDMA channel number to write burst length for
burst_lengthDMA channel burst length given by a DMA_CH_BURSTLEN_t type

References cpu_irq_restore(), cpu_irq_save(), and dma_get_channel_address_from_num.

Referenced by run_dma_config_interface_test(), and run_dma_error_handling_test().

static void dma_channel_write_destination ( dma_channel_num_t  num,
uint16_t  destination 
)
inlinestatic

Write DMA channel 16-bit destination address to hardware.

This function writes a DMA channel destination 16-bit LSB address to hardware.

Parameters
numDMA channel number to write 16-bit destination address for
destination16-bit LSB destination address

References cpu_irq_restore(), cpu_irq_save(), destination, and dma_get_channel_address_from_num.

Referenced by run_dma_config_interface_test(), and run_dma_error_handling_test().

static void dma_channel_write_repeats ( dma_channel_num_t  num,
uint8_t  repeats 
)
inlinestatic

Write DMA channel number of transfer repeats to hardware.

This function writes the DMA channel number of block transfer repeats to hardware. It will also enable the repeat feature.

Parameters
numDMA channel number to write transfer repeats for
repeatsNumber of block transfer repeats

References cpu_irq_restore(), cpu_irq_save(), and dma_get_channel_address_from_num.

Referenced by run_dma_config_interface_test().

static void dma_channel_write_source ( dma_channel_num_t  num,
uint16_t  source 
)
inlinestatic

Write DMA channel 16-bit source address to hardware.

This function writes a DMA channel source 16-bit LSB address to hardware.

Parameters
numDMA channel number to write 16-bit source address for
source16-bit LSB source address

References cpu_irq_restore(), cpu_irq_save(), dma_get_channel_address_from_num, and source.

Referenced by run_dma_config_interface_test(), and run_dma_error_handling_test().

static void dma_channel_write_transfer_count ( dma_channel_num_t  num,
uint16_t  count 
)
inlinestatic

Write DMA channel transfer count to hardware.

This function writes the DMA channel number of bytes in each block transfer to hardware.

Parameters
numDMA channel number to write transfer count for
countNumber of bytes in each block transfer

References cpu_irq_restore(), cpu_irq_save(), and dma_get_channel_address_from_num.

Referenced by run_dma_config_interface_test(), and run_dma_error_handling_test().

void dma_enable ( void  )
static enum dma_channel_status dma_get_channel_status ( dma_channel_num_t  num)
inlinestatic

Get channel status.

This function returns the current channel status.

Parameters
numDMA channel number
Returns
Channel status given by a dma_channel_status

References DMA_CH_BUSY, DMA_CH_FREE, DMA_CH_PENDING, DMA_CH_TRANSFER_COMPLETED, and DMA_CH_TRANSFER_ERROR.

Referenced by dma_interrupt(), dma_transfer_block(), run_dma_double_buffering_test(), and run_dma_error_handling_test().

void dma_set_callback ( dma_channel_num_t  num,
dma_callback_t  callback 
)

Set callback for given DMA channel.

Parameters
numdma_channel_num_t
callbackdma_callback_t

References dma_data.

Referenced by main(), and run_dma_triggered_with_callback().

static void dma_set_double_buffer_mode ( DMA_DBUFMODE_t  dbufmode)
inlinestatic

Set DMA channel double buffering mode.

This function modifies which channels should be in double buffer mode. The modes are defined in the toolchain header files in the form of DMA_DBUFMODE_*_gc for devices with more than 2 channels, or DMA_DBUFMODE_*_bm for dual channel devices.

Parameters
dbufmodeDouble buffer channel configuration given by a DMA_DBUFMODE_t type

References Assert, cpu_irq_restore(), and cpu_irq_save().

Referenced by run_dma_double_buffering_test().

static void dma_set_priority_mode ( DMA_PRIMODE_t  primode)
inlinestatic

Set DMA channel priority mode.

This functions sets the channel priority mode. Users can select between fixed priority, round-robin or a mix of both.

The modes are defined in the toolchain header files in the form of DMA_PRIMODE_*_gc for devices with more than 2 channels, or DMA_PRIMODE_*_bm for dual channel devices.

Parameters
primodeDMA channel priority mode given by a DMA_PRIMODE_t type

References Assert, cpu_irq_restore(), and cpu_irq_save().