Skip to main content
e d
Associate III
May 22, 2017
Solved

HAL_SPI_Transmit takes too long

  • May 22, 2017
  • 10 replies
  • 3964 views
Posted on May 22, 2017 at 19:51

Hi guys,

I am working with the STM32L486 using the CubeMX tool and running into some SPI issue that I can't seem to figure out (I am new to ST chipsets btw). Specifically the

HAL_SPI_Transmit routine takes to long compared to the fraction of the time the data is transmitted, as can be seen in the scope image below with yellow being the clock, blue being the data and pink the test point PA0_SET I wiggle (code below). The scope shows that theHAL_SPI_Transmit takes up to 4uS, which is ridiculous. Any idea how to speed this up close to the time it takes for the data to transmit (around 1.44uS)?

Thanks,

ED

PA0_SET;

Spi.Status = HAL_SPI_Transmit(&hspi2,(uint8_t *) &DDS_Cmd, sizeof(DDS_Cmd)>>1, 2);

PA0_CLEAR;

0690X000006072dQAA.png

#hal_spi_transmit

Note: this post was migrated and contained many threaded conversations, some content may be missing.
This topic has been closed for replies.
Best answer by Chris1
Posted on May 24, 2017 at 16:09

I used the higher level initialization function HAL_SPI_Init(); followed by LL_SPI_Enable(SPI2);

The latter may be what you are missing.  Also, you may need to enable the clock for GPIOB.

10 replies

Chris1
Associate II
May 23, 2017
Posted on May 23, 2017 at 16:05

Consider use of the Low Level HAL functions, especially if you are writing a small number of bytes.

For example:

static void SPI2_WriteByte (uint8_t tx_byte)

{

    uint32_t start_time_w;

    uint32_t this_time_w;

    while (!LL_SPI_IsActiveFlag_TXE(SPI2))

    {

        ;

    }

    start_time_w = HAL_GetTick();

    LL_SPI_TransmitData8(SPI2, tx_byte);

    while (LL_SPI_IsActiveFlag_BSY(SPI2))

    {

        this_time_w = HAL_GetTick() - start_time_w;

        if (this_time_w > SPI2_READ_TIMEOUT)

        {

            // timeout error!

            break; 

        }

    }

    LL_SPI_ClearFlag_OVR(SPI2); // clear overrun flag

}
e d
e dAuthor
Associate III
May 23, 2017
Posted on May 23, 2017 at 20:02

Thanks Chris for the tip!

I tried to use LL with CubeMX to generate SPI instead of HAL and it wouldn't run (I don't see the clock coming). I think I am missing something simple but cannot put my finger on. The SPI init runs as follows:

/* SPI2 init function */

static void MX_SPI2_Init(void)

{

LL_SPI_InitTypeDef SPI_InitStruct;

LL_GPIO_InitTypeDef GPIO_InitStruct;

/* Peripheral clock enable */

LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2);

/**SPI2 GPIO Configuration

PB13 ------> SPI2_SCK

PB14 ------> SPI2_MISO

PB15 ------> SPI2_MOSI

*/

GPIO_InitStruct.Pin = LL_GPIO_PIN_13|LL_GPIO_PIN_14|LL_GPIO_PIN_15;

GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;

GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;

GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;

GPIO_InitStruct.Alternate = LL_GPIO_AF_5;

LL_GPIO_Init(GPIOB, &GPIO_InitStruct);

SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;

SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;

SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_16BIT;

SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_HIGH;

SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;

SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;

SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV8;

SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;

SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;

SPI_InitStruct.CRCPoly = 7;

LL_SPI_Init(SPI2, &SPI_InitStruct);

LL_SPI_SetStandard(SPI2, LL_SPI_PROTOCOL_MOTOROLA);

LL_SPI_EnableNSSPulseMgt(SPI2);

}

I send a simpel 0x5A5A (16 bit SPI) but see no clocks running. Please let me know what else is needed to get it going.

ED

Chris1
Chris1Best answer
Associate II
May 24, 2017
Posted on May 24, 2017 at 16:09

I used the higher level initialization function HAL_SPI_Init(); followed by LL_SPI_Enable(SPI2);

The latter may be what you are missing.  Also, you may need to enable the clock for GPIOB.

Amez-Droz.Philippe
Associate II
September 12, 2017
Posted on September 12, 2017 at 22:31

Hello,

I use the HAL Library (not the low level LL) on a STM32F407VGT which is a master of a communication with STM32F410RBT through SPI.

The communication is mainly OK but sometimes it's seems that there is a problem when an intterupt occurs during tje HAL_SPI_transmit execution.

What can I do?

How can I replace with the minimum of change trhe HAL Library with the LL Library?

Is the problem known or not?

I use the V1.4.4 version of the HAL Library. I am affraid to change to a new version because my soft is big and I am affraid to introduce new bugs.

Thank you for you help

Best regards