cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo-64 STM32L452RE Issue SPI MISO

JKol
Associate II

Hello,

I'm a little confused about the SPI communication. I tried to get the MCP2515 working with the STM32, but I end up at the beginning because I can't read data from the SPI. Both the HAL_SPI_TransmitReceive and HAL_SPI_Receive functions give me STATUS_OK, but an empty Rx buffer. I have tried to link MOSI and MISO together to isolate MCP2515, but no luck, zero data.

This code has generated the graphs from the oscilloscope below, but the rx buffer is still empty:

HAL_GPIO_WritePin(CAN_CS_GPIO_Port, CAN_CS_Pin, GPIO_PIN_RESET);

uint8_t spitesttx[2];

uint8_t spitestrx[2];

spitesttx[0] = 0xAA;

spitesttx[1] = 0x55;

HAL_SPI_TransmitReceive(&hspi3,spitesttx, spitestrx, 2, 10);

SPI_rx1 = spitestrx[0];

SPI_rx2 = spitestrx[1];

HAL_GPIO_WritePin(CAN_CS_GPIO_Port, CAN_CS_Pin, GPIO_PIN_SET);

Scope of 1-CS, 4-CLK and 3-MOSI/MISO wire.

JKol_1-1724842716918.png

JKol_0-1724842699772.png

My SPI configuration:

 
hspi3.Instance = SPI3;

hspi3.Init.Mode = SPI_MODE_MASTER;

hspi3.Init.Direction = SPI_DIRECTION_2LINES;

hspi3.Init.DataSize = SPI_DATASIZE_8BIT;

hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi3.Init.NSS = SPI_NSS_SOFT;

hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;

hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi3.Init.TIMode = SPI_TIMODE_DISABLE;

hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi3.Init.CRCPolynomial = 10;



__HAL_RCC_SPI3_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();

/**SPI2 GPIO Configuration

PB3 ------> SPI2_SCK

PB4 ------> SPI2_MISO

PB5 ------> SPI2_MOSI

*/

GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_5;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_4;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

I tried the SPI2 circuit before, with the same result.
Could you please help me, what could be wrong?

Thank you, Jan.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

This pin should be initialized as GPIO_MODE_AF_PP, not GPIO_MODE_INPUT, since you're using it for SPI_MISO.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

5 REPLIES 5

> 3-MOSI/MISO wire.

What do you mean by "MOSI/MISO wire"? Did you tie MOSI and MISO together? Or how exactly did you wire up the MCP2515?

Also, are you aware of the fact that there are many STM32 models with built-in CAN?

JW

Yes, I tie MOSI and MISO together only for test, that should work, or not? MCP2515 i had connected SO-SPI3_MISO, SI-SPI3_MOSI, SCK-SPI3_SCK,CS-PB12.

Jan.

TDK
Guru
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

This pin should be initialized as GPIO_MODE_AF_PP, not GPIO_MODE_INPUT, since you're using it for SPI_MISO.

If you feel a post has answered your question, please click "Accept as Solution".

Nice catch, @TDK !

I guess this is the unfortunate legacy of the 'F1xx GPIO.

JW

JKol
Associate II

Thanks, it works :thumbs_up:. It also works with MCP2515 now :).