HAL_SPI_Transmit is returning HAL_ERROR

227 Views Asked by At

I´m trying to send and recibe data from a SPI Slave in Full-Duplex mode using HAL librarys and I´m struggling with a problem that I don´t know how to solve.

I'm having an issue with my SPI Slave code that utilizes interrupts. I have implemented the HAL_SPI_RxCpltCallback function, which is called by the SPI interrupt. Inside this function, I activate the FlagIRQ to initiate the transmission of the tx buffer. However, when I use the HAL_SPI_Transmit function, it returns HAL_ERROR and doesn't transmit anything.

I have already sent 18 bytes of data, where 9 bytes are for the receiving function and the other 9 bytes are for the transmission of the slave. Based on this, I expected it to work correctly.

#include "main.h"


SPI_HandleTypeDef hspi1;
DMA_HandleTypeDef hdma_spi1_rx;
DMA_HandleTypeDef hdma_spi1_tx;


void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_SPI1_Init(void);


#define SPI_TIMEOUT 50000

uint8_t spi_tx_data[9];
uint8_t spi_rx_data[9];
int FlagIRQ = 0;



int main(void)
{

  HAL_Init();


  SystemClock_Config();


  MX_GPIO_Init();
  MX_DMA_Init();
  MX_SPI1_Init();

  spi_tx_data[0] = 0x34;
  spi_tx_data[1] = 0x34;
  spi_tx_data[2] = 0x34;
  spi_tx_data[3] = 0x34;
  spi_tx_data[4] = 0x09;
  spi_tx_data[5] = 0x05;
  spi_tx_data[6] = 0x05;
  spi_tx_data[7] = 0x00;
  spi_tx_data[8] = 0x02;

  while (1)
  {
     HAL_SPI_Receive_IT(&hspi1, spi_rx_data, 9);
    

      if (FlagIRQ==1){
          if (HAL_SPI_Transmit(&hspi1, spi_tx_data, 9, SPI_TIMEOUT)!= HAL_OK){
              Error_Handler();
          }
          FlagIRQ=0;
        

      }
  }

}

void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi1)
{

    HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_10); //Toggle the led to see if we detect any  SPI signal.
    FlagIRQ= 1;
    }
}

    

I´ve also checked that the spi inicalization is correct: SPI_POLARITY_LOW and SPI_PHASE_1EDGE for mode 0, that is the same that I´ve configured in the master.

static void MX_SPI1_Init(void)
{

  /* USER CODE BEGIN SPI1_Init 0 */

  /* USER CODE END SPI1_Init 0 */

  /* USER CODE BEGIN SPI1_Init 1 */

  /* USER CODE END SPI1_Init 1 */
  /* SPI1 parameter configuration*/
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_SLAVE;
  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_HARD_INPUT;
  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();
  }
  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */

}

I´ have checked also that the SPI signal from the master is correct, and as you can see in the screenshot below, it seems good abd the pins are properly connected. SPI Signal from the Master

Do anybody have an idea of what could be happening and why HAL fuctions are returning error?

Thanks in advance!

0

There are 0 best solutions below