2023-08-29 11:29 PM
i am using the STM32G031 and the SPI1 port. i have used the SPI port many times on various STM32F devices. I have found some of the STM32G devices have odd behavior. Today i have a strange problem i am running 16 bit SPI1 and when i observe the waveforms i am getting what i expect. However the return value is always 0. despite the waveform showing a value. Waveform attached, yellow CS light Blue clock purple MOSI dark blue MISO
this is my init code, i do not use HAL had too many problems in the past.
I suspect i have overlooked something, most likely obvious, but cannot find it.
void spi1_init(void)
{
// initilise SPI1 port
// PA1 - SCK
// PA2 - MOSI
// PA3 - MISO
// AF0
RCC->APBENR2|=RCC_APBENR2_SPI1EN;
// setup alternative registers
GPIOA->MODER&=~(GPIO_MODER_MODE1_0|GPIO_MODER_MODE2_0|GPIO_MODER_MODE3_0);
GPIOA->MODER|=(GPIO_MODER_MODE1_1|GPIO_MODER_MODE2_1|GPIO_MODER_MODE3_1);
// setup the AF number 0
GPIOA->AFR[0]&=~(GPIO_AFRL_AFSEL1_3|GPIO_AFRL_AFSEL1_2|GPIO_AFRL_AFSEL1_1|GPIO_AFRL_AFSEL1_0);
GPIOA->AFR[0]&=~(GPIO_AFRL_AFSEL2_3|GPIO_AFRL_AFSEL2_2|GPIO_AFRL_AFSEL2_1|GPIO_AFRL_AFSEL2_0);
GPIOA->AFR[0]&=~(GPIO_AFRL_AFSEL3_3|GPIO_AFRL_AFSEL3_2|GPIO_AFRL_AFSEL3_1|GPIO_AFRL_AFSEL3_0);
// set for very high speed
GPIOA->OSPEEDR|=GPIO_OSPEEDR_OSPEED1_0|GPIO_OSPEEDR_OSPEED1_1;
GPIOA->OSPEEDR|=GPIO_OSPEEDR_OSPEED2_0|GPIO_OSPEEDR_OSPEED2_1;
GPIOA->OSPEEDR|=GPIO_OSPEEDR_OSPEED3_0|GPIO_OSPEEDR_OSPEED3_1;
// now setup SPI1 registers
// setup clock rate to 40MHz
SPI1->CR1|=(SPI_CR1_BR);
// bidirectional mode
SPI1->CR1&=~SPI_CR1_BIDIMODE;
// we are the master
SPI1->CR1|=SPI_CR1_MSTR;
// CPOL clock idle low
SPI2->CR1&=~SPI_CR1_CPOL;
// CPHA clock transistion
SPI2->CR1&=~SPI_CR1_CPHA;
// set LSBFIRST low we send MSB first
SPI2->CR1&=~SPI_CR1_LSBFIRST;
// configure data size 16 bit
SPI1->CR2|=(SPI_CR2_DS_3|SPI_CR2_DS_2|SPI_CR2_DS_1|SPI_CR2_DS_0);
//SPI2->CR2&=~SPI_CR2_DS_3;
// configure RXNE threshold to 16 bit
SPI1->CR2&=~SPI_CR2_FRXTH;
// set up SSM pin
SPI1->CR1|=SPI_CR1_SSM;
// set up SSI pin
SPI1->CR1|=SPI_CR1_SSI;
// enable SPI port
SPI1->CR1|=SPI_CR1_SPE;
}
2023-08-29 11:48 PM - edited 2023-08-29 11:49 PM
Code looks OK.
Read out and check/post content of SPI and relevant GPIO registers.
Check HW (think of bad solder joints and shorts).
JW