Peripheral DMA Library Code

group dma-ring-buffer

Functions

int dma_ring_buffer_periph_to_mem_initialize(struct dma_ring_buffer_to_mem *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size, volatile void *data_buffer, void *src_reg, uint8_t dma_trigger_channel)

Initialize a ring buffer used for transferring data from a peripheral to memory.

Note

The ring buffers do not have an overrun detection. You have to make sure to empty the buffer in time.

Return

Status (0 is ok)

Parameters
  • [inout] dma_buffer: structure representing the newly initialized ring buffer

  • base_dma_id: Id of the DMA controller, the stream belongs to. Either 1 or 2

  • [in] dma_stream: DMA Stream to use

  • buffer_element_count: Size of buffer in elements

  • element_size: Size of an element. Either 1, 2, or 4 bytes

  • [in] data_buffer: Buffer to operate on

  • [in] src_reg: Source register to read data from

  • dma_trigger_channel: Trigger channel for DMA stream. Consult reference manual for a valid number.

int dma_ring_buffer_periph_to_mem_get_data(struct dma_ring_buffer_to_mem *buff, volatile const void **data_buff, size_t *len)

Get data from a peripheral to memory ring buffer.

Return

0 if successful, but no data), -1 if error, 1 if data, and 2 if data with wrap around. Call function again in this case to retrieve rest after wrap around.

Parameters
  • [in] buff: Ring buffer structure

  • [out] data_buff: Pointer to set to new data. This must not be modified!

  • [out] len: Length in elements

void dma_ring_buffer_periph_to_mem_stop(struct dma_ring_buffer_to_mem *buff)

Stop the ring buffer operation.

Note

The ring buffer loses all its configuration after this call.

Parameters
  • buff: Ring buffer to stop

int dma_ring_buffer_periph_to_mem_fill_level(struct dma_ring_buffer_to_mem *buff, size_t *fill_level)

Get fill level of peripheral to memory DMA ring buffer.

Return

0 if success

Parameters
  • buff: Buffer

  • fill_level: fill level to write data to

int dma_ring_buffer_mem_to_periph_initialize(struct dma_ring_buffer_to_periph *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size, volatile void *data_buffer, uint8_t dma_trigger_channel, void *dest_reg)

Initialize ring buffer to streaming data from meory to a peripheral.

Return

0 if successful

Parameters
  • [inout] dma_buffer: DMA ring buffer structure

  • base_dma_id: Id of the DMA controller, the stream belongs to. Either 1 or 2

  • [in] dma_stream: DMA stream to use

  • buffer_element_count: size of ring buffer in elements

  • element_size: Size of an element. Either 1, 2, or 4 bytes

  • [in] data_buffer: Ring buffer

  • dma_trigger_channel: Trigger channel to start DMA. Consult reference manual for values

  • [in] dest_reg: Destination register to stream data to

int dma_ring_buffer_mem_to_periph_insert_data(struct dma_ring_buffer_to_periph *buff, const void *data_to_insert, size_t count)

Insert data into the ring buffer.

This function will try to insert data into the ring buffer. If the buffer is full, it will wait for the buffer to become empty, until the data fits into the buffer. If the data is larger than the full buffer, it is split. The buffer is fully written. The rest is written after the buffer has emptied out, until the rest fits in

Return

0 if successful.

Parameters
  • buff: Ring buffer element

  • data_to_insert: Data to put in buffer

  • count: Element count of data to insert

void dma_ring_buffer_mem_to_periph_int_callback(struct dma_ring_buffer_to_periph *buff)

Call this function on a transfer complete interrupt of the DMA.

Note

It is mandatory to call this function in order to provide a working ring buffer.

Parameters
  • buff: Ring buffer struct

void dma_ring_buffer_mem_to_periph_stop(struct dma_ring_buffer_to_periph *buff)

Stop the ring buffer operation.

Note

The ring buffer loses all its configuration after this call.

Parameters
  • buff: Ring buffer to stop

int dma_ring_buffer_mem_to_periph_fill_level(struct dma_ring_buffer_to_periph *buff, size_t *fill_level)

Get fill level of mem to periph DMA ring buffer.

Return

0 if success

Parameters
  • buff: Buffer

  • fill_level: fill level to write data to

size_t calculate_ring_buffer_fill_level(size_t buffer_size, size_t get_idx, size_t put_idx)
int dma_ring_buffer_switch_clock_enable(uint8_t base_dma, bool clk_en)
void queue_or_start_dma_transfer(struct dma_ring_buffer_to_periph *buff)
struct dma_ring_buffer_to_mem
#include <dma-ring-buffer.h>

DMA ring buffer for data transfer from peripheral to memory.

Public Members

void *data_ptr

Ring buffer.

size_t buffer_count

Size of buffer in multiples of elements.

DMA_Stream_TypeDef *dma

DMA Stream used to transfer data.

size_t get_idx

Get index for SW. Indicates the next element to be read.

uint8_t base_dma_id

Id of the DMA controller, the stream belongs to. Either 1 or 2.

size_t element_size

Size of a buffer element (1, 2, or 4 bytes)

struct dma_ring_buffer_to_periph
#include <dma-ring-buffer.h>

DMA ring buffer for data transfer from memory to peripheral.

Public Members

void *src_buffer

Ring buffer.

size_t buffer_count

Size of buffer in multiples of elements.

DMA_Stream_TypeDef *dma

DMA Stream used to transfer data.

size_t dma_get_idx_current

Current get index of the (not yet) running DMA Stream.

size_t dma_get_idx_future

Get index in the buffer, after the current DMA transfer has finished.

size_t sw_put_idx

Put index for software.

uint8_t dma_base_id

Id of the DMA controller, the stream belongs to. Either 1 or 2.

size_t element_size

Size of a buffer element (1, 2, or 4 bytes)