I want to use SPI2 and UART4 peripherals with DMA but, as I see DMA1-SPI2-Tx and DMA1-UART4-Tx uses the same dma stream.
SPI2_TX - DMA1_Stream4 (channel 0)
UART4_TX - DMA1_Stream4 (channel 4)
Is there any way to use the same DMA stream for the different peripherals at the same time?
No, there isn't. Only a single channel can be selected for each stream. Of course if you are not transmitting on both ports at the same time, then you can switch back and forth between channels.
Use another stream
Although it seems that both SPI2 TX and UART4 TX are confined to Stream 4, it is possible to control transmit channels by other means.
Note: this works only with SPI master or UART with no flow control. i.e. as long as the MCU is in complete control of the timing.
There is no rule saying that a DMA transfer should access the same peripheral the request came from (however they must be on the same
APB
bus for DMA1). It is possible to use e.g.TIM6
(or any other timer connected to DMA1) to generate periodic DMA requests on a stream other than 4, and set up that stream to transfer data from memory toSPI2->DR
.Just set the timer frequency to generate update DMA requests with 1/8 of your SPI bitrate, write a few bytes into
SPI2->DR
to fill the FIFO, and start the timer.It would be more tricky with an UART with its various framing options, but it should generally work.