SPI working on STM32F401, not on STM32F405
Posted: Mon May 10, 2021 6:23 pm
Hello,
I am working on an application that uses SPI=SPI1 with alternate pins (MOSI=PB5, MISO not used, SCK=PB3) to communicate with a TFT screen and another SPI (SPI2) to communicate with an ADC.
I tested ok with STM32F401. I thought it would run the same on STM32F405. Unfortunately I was not able to have these SPI work.
The pins are working since I can have the TFT work when using the software SPI constructor of the TFT object. But then, the other SPI (SPI2) does not seem to work neither. The data line from the ADC seems to remain high. I haven't investigated more this other issue (it migh have another reason like the clocking of the ADC) since anyway I can't have the TFT work over SPI.
Here is the essential parts of the code concerning only SPI1 to test the TFT
To define the pins and alternate pins for SPI1:
Creating the TFT object
Starting SPI and initializing the TFT inside setup()
I am using Arduino IDE 1.8.13 and tried both Arduino Core STM32 1.9.0 and 2.0.0
I suspect
1) a peripheral conflict (CAN)
2) a possible clock issue since I had to redefine the clockConfig to have the USB work. But I tried different prescalers to no avail.
I am working on an application that uses SPI=SPI1 with alternate pins (MOSI=PB5, MISO not used, SCK=PB3) to communicate with a TFT screen and another SPI (SPI2) to communicate with an ADC.
I tested ok with STM32F401. I thought it would run the same on STM32F405. Unfortunately I was not able to have these SPI work.
The pins are working since I can have the TFT work when using the software SPI constructor of the TFT object. But then, the other SPI (SPI2) does not seem to work neither. The data line from the ADC seems to remain high. I haven't investigated more this other issue (it migh have another reason like the clocking of the ADC) since anyway I can't have the TFT work over SPI.
Here is the essential parts of the code concerning only SPI1 to test the TFT
Code: Select all
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <SPI.h>
Code: Select all
#define TFT_CS PB4
#define TFT_RST PB9
#define TFT_DC PB8
// Redefining the SPI pin mapping to use alternate pins PB3/PB5
const PinMap PinMap_SPI_MOSI[] = {
{PB_5, SPI1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
{NC, NP, 0}
};
const PinMap PinMap_SPI_SCLK[] = {
{PB_3, SPI1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
{NC, NP, 0}
};
Code: Select all
// For 1.44" and 1.8" TFT with ST7735 use:
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Code: Select all
void setup(void) {
...
SPI.setSCLK(PB_3); // Not certain it is necessary given the pins were defined above
SPI.setMOSI(PB_5); // I tried both syntax PB_3 / PB_5 and PB3 / PB5
SPI.setClockDivider(SPI_CLOCK_DIV16); // tried DIV4, DIV8 etc
SPI.begin();
__HAL_RCC_I2C1_CLK_DISABLE(); // don't remember why. it was interacting with SPI1 - might be that another peripheral interacts with SPI. On STM32F405 there is CAN that uses PB5, but is not enabled by default and I tried to disable it anyway but the SPI still not working
tft.initR(INITR_GREENTAB); // Init ST7735S chip, black tab
tft.fillScreen(ST77XX_BLACK);
...
I suspect
1) a peripheral conflict (CAN)
2) a possible clock issue since I had to redefine the clockConfig to have the USB work. But I tried different prescalers to no avail.