infineon4engineers Facebook

infineon@google+ Google+

infineon@linkedin linkedin

infineon4engi@twitter twitter

infineon@youtube youtube

+ Reply to Thread
Results 1 to 2 of 2

Thread: How to make a DMA cirular Ringbuffer

  1. #1
    New Member New Member Embedded is on a distinguished road
    Join Date
    Jun 2018
    Posts
    5
    Points
    35

    How to make a DMA cirular Ringbuffer

    I want to make a cirular rx uart buffer with DMA, receive 4 Bytes continuous without interation of software(After receiving 4 bytes DMA should start a rx_message address)

    Here is my code, after receiving 4 bytes, no reload of dst address, what is wrong here ?

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >
    static const XMC_DMA_CH_CONFIG_t dma_rx_ch_config = {

    .src_addr = (uint32_t)&XMC_UART0_CH1->RBUF,
    .dst_addr = (uint32_t)rx_message,
    .src_transfer_width = XMC_DMA_CH_TRANSFER_WIDTH_8,
    .dst_transfer_width = XMC_DMA_CH_TRANSFER_WIDTH_8,
    .src_address_count_mode = XMC_DMA_CH_ADDRESS_COUNT_MODE_NO_CHANGE,
    .dst_address_count_mode = XMC_DMA_CH_ADDRESS_COUNT_MODE_INCREMENT,
    .src_burst_length = XMC_DMA_CH_BURST_LENGTH_1,
    .dst_burst_length = XMC_DMA_CH_BURST_LENGTH_1,
    .transfer_flow = XMC_DMA_CH_TRANSFER_FLOW_P2M_DMA,
    .transfer_type = XMC_DMA_CH_TRANSFER_TYPE_MULTI_BLOCK_SRCADR_CONTIG UOUS_DSTADR_RELOAD,
    .src_handshaking = XMC_DMA_CH_SRC_HANDSHAKING_HARDWARE,
    .src_peripheral_request = DMA0_PERIPHERAL_REQUEST_USIC0_SR1_2,
    .enable_interrupt = 0,
    .block_size = UART_BUFFER_LENGTH,
    };

    oid UARTInit(void) {


    XMC_DMA_Init(XMC_DMA0);
    XMC_DMA_CH_Init(XMC_DMA0, 3, &dma_rx_ch_config);

    XMC_UART_CH_Init(XMC_UART0_CH1, &uart_config);

    XMC_GPIO_SetMode(UART_RX, XMC_GPIO_MODE_INPUT_TRISTATE);
    XMC_UART_CH_SetInputSource(XMC_UART0_CH1, XMC_UART_CH_INPUT_RXD ,USIC0_C1_DX0_P2_2);

    XMC_GPIO_SetMode(UART_TX, XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT2);

    XMC_UART_CH_SelectInterruptNodePointer(XMC_UART0_C H1, XMC_UART_CH_INTERRUPT_NODE_POINTER_RECEIVE , 1);
    XMC_UART_CH_EnableEvent(XMC_UART0_CH1, XMC_UART_CH_EVENT_STANDARD_RECEIVE);


    XMC_UART_CH_Start(XMC_UART0_CH1);

    XMC_UART_CH_TriggerServiceRequest(XMC_UART0_CH1, 2); // Receive
    XMC_DMA_CH_Enable(XMC_DMA0, 3);
    }

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >

  2. #2

    Infineon Employee
    Infineon Employee
    jferreira will become famous soon enough
    Join Date
    Oct 2012
    Posts
    419
    Hi,

    You can implement a circular buffer using the linked list feature of the DMA channels 0 and 1.
    See code below
    Code:
    #include <xmc_gpio.h>
    #include <xmc_dma.h>
    #include <xmc_uart.h>
    
    // 1ms task which reads from the ring buffer and copies to the working buffer
    #define TICKS_PER_SECOND 1000
    
    #define BUFFER_SIZE 1024
    static volatile uint8_t buffer[BUFFER_SIZE];
    static volatile uint32_t buffer_idx = 0;
    
    // Ring buffer using DMA
    // The ring buffer is divided into two equal sized buffers
    #define RING_BUFFER_SIZE 32
    static volatile uint8_t ring_buffer[RING_BUFFER_SIZE];
    static volatile uint8_t ring_buffer_cur = 0;
    
    /* DMA linked list */
    static __attribute__((aligned(32))) XMC_DMA_LLI_t dma_ll[2] =
    {
      {
    	.block_size = RING_BUFFER_SIZE / 2,
    	.src_addr = (uint32_t)&(XMC_UART0_CH0->RBUF),
    	.dst_addr = (uint32_t)&ring_buffer[0],
    	.llp = &dma_ll[1],
    	.enable_interrupt = true,
    	.dst_transfer_width = XMC_DMA_CH_TRANSFER_WIDTH_8,
    	.src_transfer_width = XMC_DMA_CH_TRANSFER_WIDTH_8,
    	.dst_address_count_mode = XMC_DMA_CH_ADDRESS_COUNT_MODE_INCREMENT,
    	.src_address_count_mode = XMC_DMA_CH_ADDRESS_COUNT_MODE_NO_CHANGE,
    	.dst_burst_length = XMC_DMA_CH_BURST_LENGTH_1,
    	.src_burst_length = XMC_DMA_CH_BURST_LENGTH_1,
    	.transfer_flow = XMC_DMA_CH_TRANSFER_FLOW_P2M_DMA,
    	.enable_dst_linked_list = true,
    	.enable_src_linked_list = true,
      },
      {
    	.block_size = RING_BUFFER_SIZE / 2,
    	.src_addr = (uint32_t)&(XMC_UART0_CH0->RBUF),
    	.dst_addr = (uint32_t)&ring_buffer[RING_BUFFER_SIZE / 2],
    	.llp = &dma_ll[0],
    	.enable_interrupt = true,
    	.dst_transfer_width = XMC_DMA_CH_TRANSFER_WIDTH_8,
    	.src_transfer_width = XMC_DMA_CH_TRANSFER_WIDTH_8,
    	.dst_address_count_mode = XMC_DMA_CH_ADDRESS_COUNT_MODE_INCREMENT,
    	.src_address_count_mode = XMC_DMA_CH_ADDRESS_COUNT_MODE_NO_CHANGE,
    	.dst_burst_length = XMC_DMA_CH_BURST_LENGTH_1,
    	.src_burst_length = XMC_DMA_CH_BURST_LENGTH_1,
    	.transfer_flow = XMC_DMA_CH_TRANSFER_FLOW_P2M_DMA,
    	.enable_dst_linked_list = true,
    	.enable_src_linked_list = true,
      }
    };
    
    static const XMC_DMA_CH_CONFIG_t dma_ch_config =
    {
      {
    	.enable_interrupt = true,
    	.dst_transfer_width = XMC_DMA_CH_TRANSFER_WIDTH_8,
    	.src_transfer_width = XMC_DMA_CH_TRANSFER_WIDTH_8,
    	.dst_address_count_mode = XMC_DMA_CH_ADDRESS_COUNT_MODE_INCREMENT,
    	.src_address_count_mode = XMC_DMA_CH_ADDRESS_COUNT_MODE_NO_CHANGE,
    	.dst_burst_length = XMC_DMA_CH_BURST_LENGTH_1,
    	.src_burst_length = XMC_DMA_CH_BURST_LENGTH_1,
    	.transfer_flow = XMC_DMA_CH_TRANSFER_FLOW_P2M_DMA,
      },
      .block_size = RING_BUFFER_SIZE / 2,
      .src_addr = (uint32_t)&(XMC_UART0_CH0->RBUF),
      .dst_addr = (uint32_t)&ring_buffer[0],
      .linked_list_pointer = &dma_ll[0],
      .transfer_type = XMC_DMA_CH_TRANSFER_TYPE_MULTI_BLOCK_SRCADR_LINKED_DSTADR_LINKED, /* Transfer type */
      .priority = XMC_DMA_CH_PRIORITY_0, /* Priority level */
      .src_handshaking = XMC_DMA_CH_SRC_HANDSHAKING_HARDWARE, /* Source handshaking */
      .src_peripheral_request = DMA0_PERIPHERAL_REQUEST_USIC0_SR0_0, /* Source peripheral trigger */
      .dst_handshaking = XMC_DMA_CH_DST_HANDSHAKING_SOFTWARE  /* Destination handshaking */
    };
    
    static const XMC_UART_CH_CONFIG_t uart_config =
    {
      .baudrate = 115200,
      .data_bits = 8,
      .stop_bits = 1,
    };
    
    uint32_t XMC_DMA_CH_GetTransferredData(XMC_DMA_t *const dma, const uint8_t channel)
    {
      return (dma->CH[channel].CTLH & GPDMA0_CH_CTLH_BLOCK_TS_Msk);
    
    }
    
    void SysTick_Handler(void)
    {
      static uint32_t start = 0;
    
      uint32_t end = (ring_buffer_cur * (RING_BUFFER_SIZE / 2)) + XMC_DMA_CH_GetTransferredData(XMC_DMA0, 0);
    
      if ((start != end) && (buffer_idx < BUFFER_SIZE))
      {
        if (start < end)
        {
          memcpy((void *)&buffer[buffer_idx], (void *)&ring_buffer[start], end - start);
          buffer_idx += end - start;
        }
        else
        {
    	  memcpy((void *)&buffer[buffer_idx], (void *)&ring_buffer[start], RING_BUFFER_SIZE - start);
    	  buffer_idx += RING_BUFFER_SIZE - start;
    
          memcpy((void *)&buffer[buffer_idx], (void *)&ring_buffer[0], end);
          buffer_idx += end;
        }
    
        start = end;
      }
    }
    
    void dma_ch0_event_handler(XMC_DMA_CH_EVENT_t event)
    {
      ring_buffer_cur ^= 1;
    }
    
    void GPDMA0_0_IRQHandler(void)
    {
      XMC_DMA_IRQHandler(XMC_DMA0);
    }
    
    int main(void)
    {
      /* Enable DMA module */
      XMC_DMA_Init(XMC_DMA0);
      XMC_DMA_CH_Init(XMC_DMA0, 0, &dma_ch_config);
      XMC_DMA_CH_EnableEvent(XMC_DMA0, 0, XMC_DMA_CH_EVENT_BLOCK_TRANSFER_COMPLETE);
      XMC_DMA_CH_SetEventHandler(XMC_DMA0, 0, dma_ch0_event_handler);
      XMC_DMA_CH_Enable(XMC_DMA0, 0);
    
      /* Enable DMA event handling */
      NVIC_SetPriority(GPDMA0_0_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 62, 0));
      NVIC_EnableIRQ(GPDMA0_0_IRQn);
    
      /* System timer configuration */
      SysTick_Config(SystemCoreClock / TICKS_PER_SECOND);
    
      /* UART configuration */
      XMC_UART_CH_Init(XMC_UART0_CH0, &uart_config);
    
      XMC_GPIO_SetMode(P1_4, XMC_GPIO_MODE_INPUT_TRISTATE);
      XMC_UART_CH_SetInputSource(XMC_UART0_CH0, XMC_UART_CH_INPUT_RXD, USIC0_C0_DX0_P1_4);
    
      /*Configure transmit FIFO*/
      XMC_USIC_CH_TXFIFO_Configure(XMC_UART0_CH0, 0U, XMC_USIC_CH_FIFO_SIZE_32WORDS, 0U);
    
      /* Set service request for receive interrupt */
      XMC_USIC_CH_SetInterruptNodePointer(XMC_UART0_CH0, XMC_USIC_CH_INTERRUPT_NODE_POINTER_RECEIVE, 0U);
      XMC_USIC_CH_SetInterruptNodePointer(XMC_UART0_CH0, XMC_USIC_CH_INTERRUPT_NODE_POINTER_ALTERNATE_RECEIVE, 0U);
      XMC_UART_CH_EnableEvent(XMC_UART0_CH0, XMC_UART_CH_EVENT_STANDARD_RECEIVE | XMC_UART_CH_EVENT_ALTERNATIVE_RECEIVE);
    
      XMC_UART_CH_Start(XMC_UART0_CH0);
    
      XMC_GPIO_SetMode(P1_5, XMC_GPIO_MODE_OUTPUT_PUSH_PULL | P1_5_AF_U0C0_DOUT0);
    
      /* Placeholder for user application code. The while loop below can be replaced with user application code. */
      while(1U);
    }
    The views expressed here are my personal opinions, have not been reviewed or authorized by Infineon and do not necessarily represent the views of Infineon.

+ Reply to Thread
Disclaimer

All content and materials on this site are provided “as is“. Infineon makes no warranties or representations with regard to this content and these materials of any kind, whether express or implied, including without limitation, warranties or representations of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. No license, whether express or implied, is granted by Infineon. Use of the information on this site may require a license from a third party, or a license from Infineon.


Infineon accepts no liability for the content and materials on this site being accurate, complete or up- to-date or for the contents of external links. Infineon distances itself expressly from the contents of the linked pages, over the structure of which Infineon has no control.


Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Usage Terms of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Usage Terms of this site. Infineon reserves the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.