Weird SPI driver behavior on STM32H750 Topic is solved

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
djedski30
Posts: 6
Joined: Thu Jun 11, 2020 1:13 am

Weird SPI driver behavior on STM32H750

Post by djedski30 »

Hi,

I'm trying to transmit data over SPI and am finding that how the TX buffer is declared matters. I seem to only get it to work if the TX buffer is a static buffer. If it's a local variable, I get a halt. I'm on the HEAD of master (as of today) and am running on the STM32H750XB Discovery board. Below is my patch. In there, I have 3 ways to initialize the TX buffer. Theoretically, all 3 should work, but only the static buffer that's initialized works.

If I use a local variable, it halts and here's the call stack:

chSysHalt() at chsys.c:133 0x8000672
osalSysHalt() at osal.h:544 0x80006ee
spi_lld_serve_dma_tx_interrupt() at hal_spi_lld.c:202 0x80006ee
Vector70() at stm32_dma.c:188 0x8002b90

Please have a look at my patch as well. Appreciate the help!


diff --git a/demos/STM32/RT-STM32H750XB-DISCOVERY/cfg/halconf.h b/demos/STM32/RT-STM32H750XB-DISCOVERY/cfg/halconf.h
index cdbad17f1..2ead6f3f3 100644
--- a/demos/STM32/RT-STM32H750XB-DISCOVERY/cfg/halconf.h
+++ b/demos/STM32/RT-STM32H750XB-DISCOVERY/cfg/halconf.h
@@ -163,7 +163,7 @@
* @brief Enables the SPI subsystem.
*/
#if !defined(HAL_USE_SPI) || defined(__DOXYGEN__)
-#define HAL_USE_SPI FALSE
+#define HAL_USE_SPI TRUE
#endif

/**
diff --git a/demos/STM32/RT-STM32H750XB-DISCOVERY/cfg/mcuconf.h b/demos/STM32/RT-STM32H750XB-DISCOVERY/cfg/mcuconf.h
index b6a9cc38b..8720c5ec5 100644
--- a/demos/STM32/RT-STM32H750XB-DISCOVERY/cfg/mcuconf.h
+++ b/demos/STM32/RT-STM32H750XB-DISCOVERY/cfg/mcuconf.h
@@ -393,7 +393,7 @@
* SPI driver system settings.
*/
#define STM32_SPI_USE_SPI1 FALSE
-#define STM32_SPI_USE_SPI2 FALSE
+#define STM32_SPI_USE_SPI2 TRUE
#define STM32_SPI_USE_SPI3 FALSE
#define STM32_SPI_USE_SPI4 FALSE
#define STM32_SPI_USE_SPI5 FALSE
diff --git a/demos/STM32/RT-STM32H750XB-DISCOVERY/main.c b/demos/STM32/RT-STM32H750XB-DISCOVERY/main.c
index 744cb4ffd..1657a2508 100644
--- a/demos/STM32/RT-STM32H750XB-DISCOVERY/main.c
+++ b/demos/STM32/RT-STM32H750XB-DISCOVERY/main.c
@@ -39,6 +39,15 @@ static THD_FUNCTION(Thread1, arg) {
chThdSleepMilliseconds(200);
}
}
+static const SPIConfig spi2_cfg = {
+ false,
+ NULL,
+ IOPORT2,
+ 4,
+ SPI_CFG1_MBR_DIV8 | SPI_CFG1_DSIZE_VALUE(7),
+ 0
+};
+

/*
* Application entry point.
@@ -55,6 +64,49 @@ int main(void) {
halInit();
chSysInit();

+ /*
+ * SPI2 I/O pins setup.
+ */
+ palSetPadMode(GPIOD, 3, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* SPI SCK. */
+ palSetPadMode(GPIOI, 2, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* MISO. */
+ palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* MOSI. */
+
+ palSetPad(GPIOB, 4);
+ palSetPadMode(GPIOB, 4, PAL_MODE_OUTPUT_PUSHPULL); /* CS0. */
+
+ spiStart(&SPID2, &spi2_cfg);
+
+ /*
+ * Case 1: static var initialized = works. SPI sends 0xA5
+ */
+
+// static uint8_t txbuf = 0xA5;
+
+ /*
+ * Case 2: static var initialzied and assigned at runtime = fails, SPI sends 0xA5 instead of 0xFF
+ */
+
+// static uint8_t txbuf = 0xA5;
+// txbuf = 0xFF;
+
+ /*
+ * Case 3: local var = fails, spiSend() hangs
+ */
+
+ uint8_t txbuf = 0xA5;
+
+ spiAcquireBus(&SPID2);
+
+ while (1)
+ {
+ spiSelect(&SPID2);
+ spiSend(&SPID2, sizeof(txbuf), &txbuf);
+ spiUnselect(&SPID2);
+ chThdSleepMilliseconds(500);
+ }
+
+ spiReleaseBus(&SPID2);
+
/*
* Activates the serial driver 1 using the driver default configuration.
*/
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Weird SPI driver behavior on STM32H750

Post by Giovanni »

Hi,

SPI buffers have to be placed in DMA-accessible areas, is your stack placed into an accessible RAM?

Giovanni
djedski30
Posts: 6
Joined: Thu Jun 11, 2020 1:13 am

Re: Weird SPI driver behavior on STM32H750

Post by djedski30 »

Sorry, how do I check if my stack is placed into accessible RAM? I'm just using the demo code demos/STM32/RT-STM32H750XB-DISCOVERY/main.c
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Weird SPI driver behavior on STM32H750

Post by Giovanni »

Hi,

Placement is defined in the linker file included by the makefile, the default ones puts the C stack in TCM which is not DMA-accessible.

The easy approach is to use static buffers.

Giovanni
Post Reply