2021-09-07 04:44 AM
Hi,
I'm facing an issue after configuring the SPI1 of STM32G491REI6 MCU using STm32CubeMX for SPI_DATASIZE_8BIT. I have captured the signals on the Oscilloscope after Transmitting the followed three-byte (0x03, 0x00, 0x50) from master(MCU) to slave. you can see clearly in the image for each byte transmit MCU is generating a clock two times, which is undesired. Clock (Yellow) and data(Green)
For SPI_DATASIZE_9BIT is working fine, see the second photo.
static void MX_SPI1_Init(void)
{
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_16;
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;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
@
I have tested the same code SPI_DATASIZE_8BIT for the STM32F446RE MCU and that's working fine, See Photo.
The only issue is with STM32G491REI6 using SPI_DATASIZE_8BIT. Please provide the scalable solution as per the mentioned query.
Thanks in Advance.
2021-09-07 05:58 AM
Show the code you're using to write the data to the SPI.
Possibly you're using a 16-bit instruction, so sending two bytes instead of one. The G4 has a FIFO here whereas the F4 does not.
2021-09-07 07:27 AM
It's probably not FIFO but data packing.
JW
2021-09-08 04:47 AM
Hi,
I am placing two type code snippet I tried-
In main routine, NSS is Reset forcefully by firmware (GPIO control), after that "SPI_Transmit" function is called.
when you set debugger breakpoint at line4 or line3 of functions, still 2 Bytes sent by the controller (Image attached)
Case 1:-
void SPI_Transmit (uint8_t data) {
line1: while (!((SPI1->SR) & (1 << 1))) {}; // wait for TXE bit to set
line2: SPI1->DR = data;
line3: while (((SPI1->SR)&(1<<7))) {};
line4: while (((SPI1->SR)&(1<<7))) {}; // wait for BSY bit to Reset
}
Case 2:
void SPI_Transmit (uint8_t data) {
line1: while (!((SPI1->SR) & (1 << 1))) { }; // wait for TXE bit to set
line2: SPI1->DR = data;
line3: while (((SPI1->SR)&(1<<7))) {};
line4: while (((SPI1->SR)&(1<<7))) {}; // wait for BSY bit to Reset
line5: uint8_t temp = SPI1->DR; // dummy Read SPI1->DR
line6: temp = SPI1->SR; // dummy Read SPI1->SR
}
Thanks
2021-09-08 07:13 AM
> SPI1->DR = data;
If you want to write a byte (as opposed to a word), you need to force the compiler to use a byte access instruction.
*((volatile uint8_t *) &SPI1->DR) = data