2020-06-13 10:35 AM
Hi,
I'm having some difficulties implementing an SPI communication on the STM32WB55 example "Peripheral Lite".
I've started from scratch with CubeMX and a i've set up SPI and UART and obtained a working firmware that correctly communicates with an accelerometer, printing values on PC console.
Now I'm trying to migrate my code to the Peripheral Lite example and I don't understand why SPItransmit gets stuck in a loop waiting for TXE flag to be set.
I'm suspecting this is caused by the differences between firmwares' clock configurations.
I am not able to fully understand the initialization code and, by the way Peripheral Lite is written , it doesn't seem possible to use CubeMX generator. Could somebody check my code?
I have uploaded both files:
1)"spi_uart" works fine but it doesn't include BLE functionality
2) "peripheral_mod" is the one derived from the example and it includes code for accelerometer +BLE functionality. It gets stuck at "accInit()".
2020-06-13 11:12 AM
Your zip file doesn't open for me.
Is TXE isn't getting set, ensure the SPI clock is enabled, the SPI is in master mode, and that the SPI peripheral are enabled. Examine the SPI registers.
2020-06-14 11:43 AM
I checked and they seem to open up fine. Anyway I have uploaded them again.
The following code is reffered to SPI initialization:
static void MX_SPI1_Init(void)
{
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
__SPI1_CLK_ENABLE();
__HAL_RCC_SPI1_CLK_ENABLE();
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
After forcing SPI clock start using SPI1_CLK_ENABLE(), i can see registers correctly set as well as TXE flag. Despite that the execution gets caught in an infinite loop inside this"while" (from "stm32wbxx_hal_spi.c"):
while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U)){
if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U))
{
/*code omitted*/
}
if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U))
{
/*code omitted*/
}
if ((((HAL_GetTick() - tickstart) >= Timeout) && ((Timeout != HAL_MAX_DELAY))) || (Timeout == 0U))
{
/*code omitted*/
}
}