Skip to main content
OFauc.1
Associate II
January 25, 2022
Solved

Bare metal SPI DMA initialization problem on STM32WLE5

  • January 25, 2022
  • 2 replies
  • 1126 views

Hi all,

I'm trying to implement SPI TX communication with DMA.

The following "prepareDMASPI()" function should configure and start DMA transferts from a buffer to the SPI device.

But it doesn't work (No spi clock or MOSI data), although the SPI is properly configured

(and working when used without DMA)

Could you please tell me if you see something missing ?

(It's Go code, but should be easy to understand)

Thanks.

func prepareDMASPI() {
	// Disable device, memory to memory mode, peripheral increment
	stm32.DMA1.CCR1.ClearBits(stm32.DMA_CCR1_MEM2MEM | stm32.DMA_CCR1_PINC | stm32.DMA_CCR1_EN)
	// Set Memory increment, circular mode, memory to peripheral
	stm32.DMA1.CCR1.SetBits(stm32.DMA_CCR1_MINC | stm32.DMA_CCR1_CIRC | stm32.DMA_CCR1_DIR)
 
	stm32.DMA1.CCR1.ReplaceBits(stm32.DMA_CCR1_MSIZE_Bits8, stm32.DMA_CCR1_MSIZE_Msk, 0) // 8bit mem mode
	stm32.DMA1.CCR1.ReplaceBits(stm32.DMA_CCR1_PSIZE_Bits8, stm32.DMA_CCR1_PSIZE_Msk, 0) // 8bit periph mode
	stm32.DMA1.CCR1.ReplaceBits(stm32.DMA_CCR1_PL_High, stm32.DMA_CCR1_PL_Msk, 0) // High priority
 
	stm32.DMAMUX.C0CR.ReplaceBits(stm32.DMAMUX_C0CR_DMAREQ_ID_SPI1_TX_DMA, stm32.DMAMUX_C0CR_DMAREQ_ID_Msk, 0) // Route DMA to SPI1 TX
	stm32.DMA1.CMAR1.Set(uint32(uintptr(unsafe.Pointer(&ws2812_buffer[0])))) // Memory address
	stm32.DMA1.CPAR1.Set(uint32(uintptr(unsafe.Pointer(&machine.SPI0.Bus.DR)))) // Periph. data register addr
	stm32.DMA1.CNDTR1.Set(WS2812_BUFFER_SIZE) // Transfert size
 
	stm32.SPI1.CR1.ClearBits(stm32.SPI_CR1_SPE) // Disable SPI 
	stm32.SPI1.CR2.SetBits(stm32.SPI_CR2_TXDMAEN) // Tx Buffer DMA
	stm32.SPI1.CR1.SetBits(stm32.SPI_CR1_SPE) // Enable SPI
	stm32.DMA1.CCR1.SetBits(stm32.DMA_CCR1_EN) // Enable DMA1 Channel 1
 
}

This topic has been closed for replies.
Best answer by OFauc.1

Hi,

So stupid ....

You were right : I forgot to enable clock

The above code is now working, as soon as you add the clock enable at the beginning of the function !

// Enable DMA/DMAMUX clocks
stm32.RCC.AHB1ENR.SetBits(stm32.RCC_AHB1ENR_DMAMUX1EN | stm32.RCC_AHB1ENR_DMA1EN)

Thanks for your help.

2 replies

waclawek.jan
Super User
January 25, 2022

> It's Go code, but should be easy to understand

Regardless, it may be reason for problems. Also, we don't see rest of the program - maybe it's gotcha #1 - no peripheral (e.g. DMA) clock.

Read out and check/post SPI, DMA and DMAMUX registers content.

JW

OFauc.1
OFauc.1AuthorBest answer
Associate II
January 25, 2022

Hi,

So stupid ....

You were right : I forgot to enable clock

The above code is now working, as soon as you add the clock enable at the beginning of the function !

// Enable DMA/DMAMUX clocks
stm32.RCC.AHB1ENR.SetBits(stm32.RCC_AHB1ENR_DMAMUX1EN | stm32.RCC_AHB1ENR_DMA1EN)

Thanks for your help.