2021-12-26 06:45 PM
STM32F103
STM32CUBEMX V4.25.1
The slave device on my side is a fake PP output. It needs a pull-up resistor. I know this is not in accordance with the SPI agreement, but there is no way. When I run the HAL_SPI_Transmit() function, an incorrect pull-up signal will appear in the first cycle of initialization. If I remove the pull-up resistor, the wrong pull-up signal will disappear. So I think HAL_SPI function has wrong OD output in the first cycle of initialization.
My English is not very good. This is generated by translation software. I hope you understand.
thank
2021-12-26 08:03 PM
> So I think HAL_SPI function has wrong OD output in the first cycle of initialization.
There is no need to guess. The pin mode is shown in CubeMX and can be verified by looking at registers.
Before the peripheral is active, the line is not driven on the STM32 side. If you need it to be high or low, you will need an external resistor.
Typically a CS line with an external pullup is used so the master and slave can sync and the slave can ignore clock pulses before the peripheral is ready.
> STM32CUBEMX V4.25.1
This is a very old version, but it should still show the mode.
2021-12-27 12:01 AM
Thank you for your reply
I know pin mode in Hal_ SPI_ MspInit()。
// code
GPIO_InitStruct.Pin = SPI1_SCK_Pin|SPI1_MOSI_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SPI1_MISO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(SPI1_MISO_GPIO_Port, &GPIO_InitStruct);
//
Except for the first cycle, this program can work normally.
My question is why there is an OD signal before sending data in the first cycle of initialization.
As shown in the figure, there should be no signal driven by the pull-up resistor.
key factors:
1,Only the first signal after initialization appears.It won't happen again
2,If there is no pull-up resistor, this signal will not be generated
3,Occurs only after HAL_SPI_Transmit () or HAL_SPI_TransmitReceive() function is called. It only occurs before the data occurs. After the data is sent, the OD phenomenon will not occur again.Since this happens inside the HAL_SPI_Transmit () function, my CSB signal cannot remove it from it.
2021-12-27 06:33 AM
STM32F103 is master or slave?
> 2,If there is no pull-up resistor, this signal will not be generated
On which signal is that pull-up resistor?
Can you please show us the correct waveform?
Also, please read out and post content of SPI registers, just before calling HAL_SPI_Transmit().
JW
2021-12-27 06:39 AM
If you want SCK to be idle low, why do you have a pullup resistor? Why not pulldown?
What pins are these exactly?
As I said, pins that are not driven will take on the value of the external pullup. The SCK pin isn't driven until a certain point during initialization.
2021-12-27 05:30 PM
My slave device is a fake PP output. So it must have a pull-up resistor.
>1,Only the first signal after initialization appears .It won't happen again
Initialization was completed at startup. This time has been initialized. So there should not be no driving force.
2021-12-27 06:19 PM
Thank you for your reply
My slave device is a fake PP output. So it must have a pull-up resistor,include SCLK MOSI MISO CSB.
The second transmission has no wrong clock signal.Never again.
/* SPI1 init function */
static void MX_SPI1_Init(void)
{
/* 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 = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
Code generated by cubeMX.
SPI1_NSS_0;//soft CSB low
HAL_SPI_Transmit(&hspi1, spidata, reg_length, 10);//send data
while (hspi1.State == HAL_SPI_STATE_BUSY);//wait
SPI1_NSS_1;//soft CSB high
2021-12-27 06:55 PM
Do a dummy transaction after initialization to enable the peripheral before you set CS low. The other lines toggling before that point is a non-issue.
HAL_SPI_Transmit(&hspi1, spidata, 1, 0);
Or just enable the peripheral.
__HAL_SPI_ENABLE(&hspi1);
2021-12-27 07:33 PM
wow�?�?�?Solved�?�?�?
thanks very very much�?�?�?
__HAL_SPI_ENABLE(&hspi1);This command is added to the initialization function, and the problem is solved.