Well, I found the root cause…
I thought that SPI GPIO will be configured after call HAL_SPI_Init(), but when I checked its detail, I found out that HAL_SPI_MspInit is not implemented.
So I need to manually configure the GPIO.
Below is my initial code, after that, my SPI2 controls are work fine.
(Post here because I think that may help some people…)
// Init GPIO for SPI2.
GPIO_InitTypeDef GPIO_InitStructure;
__HAL_RCC_SPI2_CLK_ENABLE();
GPIO_InitStructure.Pin = MISO_PIN | MOSI_PIN | SCLK_PIN;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(SPI_PORT, &GPIO_InitStructure);
// Init SPI
memset(&SPIHandle, 0, sizeof(SPIHandle));
SPIHandle.Instance = SPI2;
SPIHandle.Init.Mode = SPI_MODE_MASTER;
SPIHandle.Init.Direction = SPI_DIRECTION_2LINES;
SPIHandle.Init.DataSize = SPI_DATASIZE_8BIT;
SPIHandle.Init.CLKPolarity = SPI_POLARITY_HIGH;
SPIHandle.Init.CLKPhase = SPI_CR1_CPHA;
SPIHandle.Init.NSS = SPI_NSS_SOFT;
SPIHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
SPIHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SPIHandle.Init.TIMode = SPI_TIMODE_DISABLED;
SPIHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
SPIHandle.Init.CRCPolynomial = 0;
if (HAL_SPI_Init(&SPIHandle) != HAL_OK)
{
/* Initialization Error */
printf("HAL_SPI_Init() failed.\n");
BREAK();
}
// Init GPIO for SPI SS(CS) pin, software control.
GPIO_InitStructure.Pin = CS_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(CS_PORT, &GPIO_InitStructure);