cancel
Showing results for 
Search instead for 
Did you mean: 

SPI DMA variable data length

ESale.2
Associate II

Hi,

I'm working with stm32H743zi (Nucleo-144) ,

I have a device that transmitting data via SPI to my stm32H7, the problem is that the device sending data with a variable length that I have no idea about it and there is no way to receive the length before .. the only thing that I know is the maximum data length that could be received

so I decided to work with the CS porting it to an EXTI (interrupt), and according to that I will turn on/off the dma receiving

I will define a static buffer with the maximum data size , then for every interrupt I will use the two commands:

HAL_SPI_DMAStop(&hspi1)

//TODO: need to memcpy the buffer to another one

//start a new recieving

HAL_SPI_Receive_DMA(&hspi1, mybuff, maximum_size)

the question is, how I could pull the data from the DMA buffer after DMA stop and I need to know the total count of bytes that have been used in the given buffer

I appreciate your suggestions ...

11 REPLIES 11

> uint32_t total = MAX_BUF_SIZE - __HAL_DMA_GET_COUNTER(hspi1.hdmarx);

As I've said, I don't use Cube, but you are probably not supposed to __HAL_DMA_GET_COUNTER(hspi1.hdmarx); rather, __HAL_DMA_GET_COUNTER(hspi1.hdmarx->Instance)

> i tried also the DMA1_Stream0->NDTR , it always return 0 !

Maybe you call some Cube component which clears it.

Are you sure you are using DMA1_Stream0?

Try to read out NDTR just after you enabled the DMA, before receiving any data.

Cube is simply not intended for anything out of "usual"; and what's "usual" is given by Cube's authors. They deem "usual" to transfer a known number of data only.

JW

ESale.2
Associate II

i found something in UART : https://www.programmersought.com/article/6663824121/

any ideas how i can convert this to SPI ?