cancel
Showing results for 
Search instead for 
Did you mean: 

Data transmission is not happening using SPI ?

Sm.2
Associate II

Hi Team,

I am using the STM32H745ZI-Q nucleo board . I am Trying to Transmit the data through the SPI using LL.

Here I am using one nucleo board cm7 for transmition and I am using another same model nucleo board cm7 for the receiving.

In the transmition side I am using the SPI1. I am unable to trasmit the data. Ia m attaching my code below . Please check and let me know if any modifications needed.

Below is SPI master code.

/* Includes ------------------------------------------------------------------*/

#include "main.h"

uint32_t timeout = 0;

/* SPI Init Structure */

LL_SPI_InitTypeDef  SPI_InitStruct;

LL_GPIO_InitTypeDef GPIO_InitStruct;

uint8_t  TxBuffer[] = Welcome_master_slave_SPI;

uint32_t  TransmitIndex = 0;

void SystemClock_Config(void);

void Error_Handler(void);

static void MX_GPIO_Init(void);

static void MX_SPI1_Init(void);

static void GPIOPins_SPI1_Config(void);

void SPI_TransferError_Callback(void);

void SPI1_EOT_Callback(void);

void SPI1_Tx_Callback(void);

int main(void)

{

 timeout = 0xFF;

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 LL_APB4_GRP1_EnableClock(LL_APB4_GRP1_PERIPH_SYSCFG);

 /* System interrupt init*/

 NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

 SystemClock_Config();

 GPIOPins_SPI1_Config();

 MX_GPIO_Init();

 MX_SPI1_Init();

  

 while (1)

 {

   LL_SPI_TransmitData8(SPI1, TxBuffer[TransmitIndex]);

 }

}

void SystemClock_Config(void)

{

 LL_FLASH_SetLatency(LL_FLASH_LATENCY_4);

 while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_4)

 {

 }

 LL_PWR_ConfigSupply(LL_PWR_DIRECT_SMPS_SUPPLY);

 LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE0);

 LL_RCC_HSE_Enable();

  /* Wait till HSE is ready */

 while(LL_RCC_HSE_IsReady() != 1)

 {

 }

 LL_RCC_PLL_SetSource(LL_RCC_PLLSOURCE_HSE);

 LL_RCC_PLL1P_Enable();

 LL_RCC_PLL1Q_Enable();

 LL_RCC_PLL1_SetVCOInputRange(LL_RCC_PLLINPUTRANGE_8_16);

 LL_RCC_PLL1_SetVCOOutputRange(LL_RCC_PLLVCORANGE_WIDE);

 LL_RCC_PLL1_SetM(1);

 LL_RCC_PLL1_SetN(120);

 LL_RCC_PLL1_SetP(2);

 LL_RCC_PLL1_SetQ(6);

 LL_RCC_PLL1_SetR(2);

 LL_RCC_PLL1_Enable();

  /* Wait till PLL is ready */

 while(LL_RCC_PLL1_IsReady() != 1)

 {

 }

  /* Intermediate AHB prescaler 2 when target frequency clock is higher than 80 MHz */

  LL_RCC_SetAHBPrescaler(LL_RCC_AHB_DIV_2);

 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL1);

  /* Wait till System clock is ready */

 while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL1)

 {

 }

 LL_RCC_SetSysPrescaler(LL_RCC_SYSCLK_DIV_1);

 LL_RCC_SetAHBPrescaler(LL_RCC_AHB_DIV_2);

 LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);

 LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_2);

 LL_RCC_SetAPB3Prescaler(LL_RCC_APB3_DIV_2);

 LL_RCC_SetAPB4Prescaler(LL_RCC_APB4_DIV_2);

 LL_Init1msTick(480000000);

 LL_SetSystemCoreClock(480000000);

}

static void GPIOPins_SPI1_Config(void)

{

 /* (1) Enables GPIO clock and configures the SPI1 pins *******************/

 /* Enable the peripheral clock of GPIOA */

 LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_GPIOA);

 /* SPI1 SCK GPIO pin configuration*/

 GPIO_InitStruct.Pin    = LL_GPIO_PIN_5 | LL_GPIO_PIN_6 | LL_GPIO_PIN_7;

 GPIO_InitStruct.Mode   = LL_GPIO_MODE_ALTERNATE;

 GPIO_InitStruct.Pull   = LL_GPIO_PULL_DOWN;

 GPIO_InitStruct.Speed   = LL_GPIO_SPEED_HIGH;

 GPIO_InitStruct.Alternate = LL_GPIO_AF_5;

 LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 /* (2) Configure NVIC for SPI1 transfer complete/error interrupts **********/

 /* Set priority for SPI1_IRQn */

 NVIC_SetPriority(SPI1_IRQn, 0);

 /* Enable SPI1_IRQn */

 NVIC_EnableIRQ(SPI1_IRQn);

}

static void MX_SPI1_Init(void)

{

 /* Configure SPI MASTER ****************************************************/

 /* Enable SPI1 Clock */

 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);

 /* Configure the SPI1 parameters */

 SPI_InitStruct.BaudRate     = LL_SPI_BAUDRATEPRESCALER_DIV256;

 SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;

 SPI_InitStruct.ClockPhase    = LL_SPI_PHASE_2EDGE;

 SPI_InitStruct.ClockPolarity   = LL_SPI_POLARITY_HIGH;

 SPI_InitStruct.BitOrder     = LL_SPI_MSB_FIRST;

 SPI_InitStruct.DataWidth     = LL_SPI_DATAWIDTH_8BIT;

 SPI_InitStruct.NSS        = LL_SPI_NSS_SOFT;

 SPI_InitStruct.CRCCalculation  = LL_SPI_CRCCALCULATION_DISABLE;

 SPI_InitStruct.Mode       = LL_SPI_MODE_MASTER;

 LL_SPI_Init(SPI1, &SPI_InitStruct);

 LL_SPI_Enable(SPI1);

 LL_SPI_EnableIT_TXP(SPI1);

 LL_SPI_EnableIT_RXP(SPI1);

 LL_SPI_EnableIT_CRCERR(SPI1);

 LL_SPI_EnableIT_UDR(SPI1);

 LL_SPI_EnableIT_OVR(SPI1);

 LL_SPI_EnableIT_EOT(SPI1);

}

static void MX_GPIO_Init(void)

{

 LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* GPIO Ports Clock Enable */

 LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_GPIOC);

 LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_GPIOH);

 LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_GPIOA);

 LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_GPIOB);

 LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_GPIOE);

 /**/

 LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_0|LL_GPIO_PIN_14);

 /**/

 LL_GPIO_ResetOutputPin(GPIOE, LL_GPIO_PIN_1);

 /**/

 GPIO_InitStruct.Pin = LL_GPIO_PIN_13;

 GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;

 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;

 LL_GPIO_Init(GPIOC, &GPIO_InitStruct);

 /**/

 GPIO_InitStruct.Pin = LL_GPIO_PIN_0|LL_GPIO_PIN_14;

 GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;

 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;

 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;

 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;

 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 GPIO_InitStruct.Pin = LL_GPIO_PIN_1;

 GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;

 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;

 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;

 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;

 LL_GPIO_Init(GPIOE, &GPIO_InitStruct);

}

void SPI1_IRQHandler(void)

{

 /* USER CODE BEGIN SPI1_IRQn 0 */

 /* Check OVR/UDR flag value in ISR register */

 if(LL_SPI_IsActiveFlag_OVR(SPI1) || LL_SPI_IsActiveFlag_UDR(SPI1)) {

 /* Call Error function */

 SPI_TransferError_Callback();

 }

 /* Check TXP flag value in ISR register */

 if((LL_SPI_IsActiveFlag_TXP(SPI1) && LL_SPI_IsEnabledIT_TXP(SPI1))) {

 /* Call function Reception Callback */

 SPI1_Tx_Callback();

 return;

 }

 /* Check EOT flag value in ISR register */

 if(LL_SPI_IsActiveFlag_EOT(SPI1) && LL_SPI_IsEnabledIT_EOT(SPI1)) {

 /* Call function Reception Callback */

 SPI1_EOT_Callback();

 return;

 }

}

void SPI1_Tx_Callback(void)

{

 /* Write character in Data register.

  * TXP flag is cleared by filling data into TXDR register */

 LL_GPIO_TogglePin(GPIOB, LL_GPIO_PIN_14);

 LL_SPI_TransmitData8(SPI1,TxBuffer[TransmitIndex++]);

 if(sizeof(TxBuffer) == TransmitIndex) {

 TransmitIndex = 0;

 }

}

4 REPLIES 4
Andrew Neil
Evangelist III

"I am unable to trasmit the data"

How do you know it's a failure to transmit?

Perhaps the failure is in receiving?

Or both?

Have you carefully checked the hardware connections?

Have you used an oscilloscope or analyser to see what's actually happening on the wires?

0693W000008xsqBQAQ.png

Sm.2
Associate II

Hi @Andrew Neil​ 

I checked with the receive first But I am not getting Then After I checked with the transmit .. When the transmission is happened it should come to call back ... but it is not coming sometimes it is coming to call back sometimes it not.

I am attaching the receiving code below Please check with the above transmit code and let me know any changes needed.

Please help me out in this . If you have any LL spi code snippet and send it to me that would be great.

Thank you.

Sm.2
Associate II

Hi Team,

Please share the LL_SPI working code base for stm32h745zi-Q

Or Else

Please give me input for my next step by refer the above code.

Thank you .

Hi @Andrew Neil​ 

In the Above code In MX_SPI1_Init(); function when I am enabling the interrupts its directly going in to the call back function and increasing the index value based on the code in the call back function , and after all the interrupts enable the index value become 16.

and when coming to main function of transmit line it is transmitting 16th character of the tx buff and it is coming out from that loop... It is transmitting only on character.

why the index value becoming 16 while enabling the interrupts and why it is only transmitting one character.. could you please help me to sort out this.

BestRegards

Sm.