cancel
Showing results for 
Search instead for 
Did you mean: 

Hal_SPI will be an Open Drain output will appear in the first cycle of initialization

xhan.11
Associate II

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.

0693W00000Hpp9JQAR.bmpMy English is not very good. This is generated by translation software. I hope you understand.

thank

8 REPLIES 8
TDK
Guru

> 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.

0693W00000HppDGQAZ.png 

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.

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

0693W00000HppXzQAJ.jpgThank 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.

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

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.

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

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.

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.

0693W00000Hpv1LQAR.jpgThe second transmission has no wrong clock signal.Never again.

0693W00000Hpv1QQAR.png/* 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

TDK
Guru

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);

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

wow�?�?�?Solved�?�?�?

thanks very very much�?�?�?

__HAL_SPI_ENABLE(&hspi1);This command is added to the initialization function, and the problem is solved.