cancel
Showing results for 
Search instead for 
Did you mean: 

How to set the max buffer size for DMA in STM32L4?

Hailing SKY
Associate II
Posted on November 21, 2017 at 23:24

I am trying to collect data using ADC + DMA continuous mode. I aim to get 2400 data points using DMA. The code is based on the one generated using STM32CubeMx. The data I got from my PC using UART were just 2048 points. I guess I need to change the max buffer size of the DMA. I try to set the CNDTR register in the initialization, but it was not successful. I guess the location could be wrong, or there are other codes required.

I am new to the STM32 family. I hope someone can help me address it. I also attached all my codes.

// partial codes.

//This is the function in main.c.

HAL_ADC_Start_DMA(&hadc1,(uint32_t*)adcValue,2400);

//DMA initialization (Partial)

:

void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)

{

/* ADC1 DMA Init */

/* ADC1 Init */

hdma_adc1.Instance = DMA1_Channel1;

hdma_adc1.Init.Request = DMA_REQUEST_0;

hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;

hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;

hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;

hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;

hdma_adc1.Init.Mode = DMA_CIRCULAR;

hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;

hdma_adc1.Instance->CNDTR = DataLength;     // I was trying to change the buffer size here, but it did work.

if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

__HAL_LINKDMA(hadc,DMA_Handle,hdma_adc1);

}

Thanks in advance.

Hailing

#buffer-size #stm32l4-hal #dma-stm32
8 REPLIES 8
Posted on November 22, 2017 at 00:40

What is BUFSIZ and where is it defined?

JW

Posted on November 22, 2017 at 01:05

Hi JW,

By saying 'Buffer Size', I mean the 'DMA channel x number of data register (DMA_CNDTRx) (x = 1..7,

where x = channel number)'. This is mentioned in the reference manual for STM32L4. I guess this register controls the number of data transmitted using ADC+DMA.

I also have a macro called 'BUF_SIZE' defined in the beginning of main.c. It sets the number of data points from each adc channel.

Thanks for your reply. Regards,

Hailing 

Posted on November 22, 2017 at 01:26

By BUFSIZ I mean BUFSIZ, as in HAL_UART_Transmit(&huart1,(uint8_t*)datastore1,BUFSIZ,2000) etc.

JW

Posted on November 22, 2017 at 01:33

Sorry, that should be BUF_SIZE.

HAL_UART_Transmit(&huart1,(uint8_t*)datastore1,BUF_SIZE,2000);

Hailing

Posted on November 22, 2017 at 01:37

OK so you changed the sources. What are the symptoms now?

JW

Posted on November 22, 2017 at 01:48

I am not in my office now. I will test it tomorrow morning and let you know. Thanks for your kindly help.

Do you think the way I set the buffer size is correct (CNDTR)? As I remember, when I tested the code in the debug mode. The points I got in each array(datastore1,2,3,4) were 512. The rest in the array are zero. 

Anyway, I will try tomorrow.

best regards,

Hailing 

Posted on November 22, 2017 at 01:50

Do you think the way I set the buffer size is correct (CNDTR)?

No. In Cube/HAL, CNDTR is set when you start the transfer. Read the Cube documentation, or sources if documentation is not sufficient.

JW

PS. BUFSIZ is a standard macro from <stdint.h>, see C99 7.19.1.

Posted on November 22, 2017 at 12:05

Hi JW,

Thanks for your help. I changed the variable from BUFSIZ to BUF_SIZE. It turns out to be my mistake. I can now transfer 8000 points without setting the CNDTR register. 

Thanks,

Hailing