本文介绍了在STM32传输结束时,循环DMA内存中断将如何表现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下,在以下情况下,STM32中的DMA SPI rx将如何运行.我有一个称为A的指定的(例如)96字节数组,该数组用于存储从SPI接收的数据.我打开循环SPI DMA,它在每个字节上运行,配置为96字节.是否有可能,当DMA填充我的96字节数组时,传输完成中断将消失,以快速将96字节数组复制到另一个-B,然后循环DMA将开始写入A(并销毁保存在B中的数据)?我想(每次从B中的A获取新数据时)通过USB将数据从B迅速传输到PC.

I wanted to ask, how will behave DMA SPI rx in STM32 in following situation.I have a specified (for example) 96 Bytes array called A which is intended to store the data received from the SPI. I turn on my circular SPI DMA which operates on each Byte, is configured to 96 Byte.Is it possible, when DMA will fill my 96 Bytes array, the Transfer Complete interrupt will went off, to quickly copy the 96 Byte array to another - B, before circular DMA will start writing to A(and destroy the data saved in B)?I want to transfer(every time when I will get new data from A in B) data from B quickly over USB to PC.

我只是在想如何通过USB将STM32的连续数据流SPI从USB传输到PC,因为每隔一定时间一次USB传输96字节数据的块比通过将实时SPI实时传输到USB更容易.STM32?我什至不知道

I'm just thinking how to transmit continous data stream SPI from STM32 over USB to PC, because a block of 96 Bytes of data transferred by USB once per certain time is easier I think than stream in real time SPI to USB by STM32? I don't know it's even possible

推荐答案

要使其正常工作,您必须能够保证可以在接收到下一个SPI字节并将其传输到SPI的开始之前复制所有数据.缓冲区.是否可行取决于处理器的时钟速度和SPI的速度,并能够保证不会发生更高优先级的中断,而这可能会延迟传输.为了安全起见,它需要非常慢的SPI速度,在那种情况下可能根本不需要使用DMA.

For that to work, you would have to be able to guarantee that you can copy all the data before the next SPI byte is received and transferred to the start of the buffer. Whether that were possible would depend on the clock speed of the processor and the speed of the SPI, and be able to guarantee that no higher priority interrupts occur that might delay the transfer. To be safe it would need an exceptionally slow SPI speed, and in that case would probably not need to use DMA at all.

总而言之,这是一个坏主意,完全没有必要.正是出于这个目的,DMA控制器具有一个半传输"中断.传输前48个字节时,您将获得HT中断,并且在复制下半部缓冲区时,DMA将继续传输其余48个字节.完成转移后,您将转移上半部.这样一来,您将数据传输的时间从一个字节的接收时间延长到48个字节的接收时间.

All in all it is a bad idea and entirely unnecessary. The DMA controller has a "half-transfer" interrupt for exactly this purpose. You will get the HT interrupt when the first 48 bytes are transferred, and the DMA will continue transferring the remaining 48 bytes while you copy lower half buffer. When you get the transfer complete you transfer the upper half. That extends the time you have to transfer the data from the receive time of a single byte to the receive time of 48 bytes.

如果每次传输实际上需要96字节,那么只需将缓冲区长192字节(2 x 96).

If you actually need 96 bytes on each transfer, then you simply make your buffer 192 bytes long (2 x 96).

使用伪代码:

#define BUFFER_LENGTH 96
char DMA_Buffer[2][BUFFER_LENGTH] ;

void DMA_IRQHandler()
{
    if( DMA_IT_Flag(DMA_HT) == SET )
    {
        memcpy( B, DMA_Buffer[0], BUFFER_LENGTH ) ;
        Clear_IT_Flag(DMA_HT) ;
    }
    else if( DMA_IT_Flag(DMA_TC) == SET )
    {
        memcpy( B, DMA_Buffer[1], BUFFER_LENGTH ) ;
        Clear_IT_Flag(DMA_TC) ;
    }
}

关于通过USB将数据传输到PC,首先需要确保USB传输速率至少与SPI传输速率相同或更快.USB传输的确定性可能较低(因为它是由PC主机控制的-也就是说,当主机明确要求USB传输时,您只能在USB上输出数据),即使平均值传输速率已足够,可能存在需要进一步缓冲的延迟,因此,与其简单地从DMA缓冲区A复制到USB缓冲区B,您可能需要循环缓冲区或FIFO队列来馈送USB.另一方面,如果您已经具有缓冲区 DMA_Buffer [0] DMA_Buffer [1] B ,则您实际上已经具有FIFO为三个96字节的块,可能就足够了

With respect to transferring the data to a PC over USB, first of all you need to be sure that your USB transfer rate is at least as fast or faster than the SPI transfer rate. It is likely that the USB transfer is less deterministic (because it is controlled by the PC host - that is you can only output data on the USB when the host explicitly asks for it), so even if the the average transfer rate is sufficient, there may be latency that requires further buffering, so rather then simply copying from the DMA buffer A to a USB buffer B, you may need a circular buffer or FIFO queue to feed the USB. On the other hand, if you already have the buffer DMA_Buffer[0], DMA_Buffer[1] and B you already effectively have a FIFO of three blocks of 96 bytes, which may be sufficient

这篇关于在STM32传输结束时,循环DMA内存中断将如何表现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 14:28