Bug in CubeMX code generation for SPI/DMA
I think I have discovered a bug in the way CubeMX behaves when both SPI and DMA are enabled. I am using the P-NUCLEO-WB55 board. If you look at the SPI_FullDuplex_ComDMA_Master example application, this runs fine on the P-NUCLEO-WB55 with a jumper between MISO and MOSI. However, if you exchange two lines in main.c so that we have:
MX_GPIO_Init();
MX_SPI1_Init();
MX_DMA_Init();instead of:
MX_GPIO_Init();
MX_DMA_Init();
MX_SPI1_Init();then the example fails. However, this is exactly what CubeMX does if we follow these steps:
- Create a new project for the P-NUCLEO-WB55-Nucleo (do not initialize peripherals in default mode)
- Enable SPI1 as full duplex master with SPI1_SCK on PA6
- Save and generate code
- Add SPI1_RX and SPI1_TX DMA requests
- Save and generate code
The problem does not occur if we don't save and generate code after enabling SPI1, i.e.:
- Create a new project for the P-NUCLEO-WB55-Nucleo (do not initialize peripherals in default mode)
- Enable SPI1 as full duplex master with SPI1_SCK on PA6
- Add SPI1_RX and SPI1_TX DMA requests
- Save and generate code
In this case, the system correctly calls MX_DMA_Init() before MX_SPI1_Init(). I see two possible solutions to the problem:
- Either ensure that CubeMX generates these two lines in the correct order always, or
- Modify the code in MX_DMA_Init() and MX_SPI1_Init() so that they do not depend on the order in which they are called.