1
votes

I am using STM32L496 microcontroller where I am initialising hspi3 as follows -

void MX_SPI3_Init()
{
    SPI_HandleTypeDef hspi3;

    /* SPI3 parameter configuration*/
    hspi3.Instance = SPI3;
    hspi3.Init.Mode = SPI_MODE_MASTER;
    hspi3.Init.Direction = SPI_DIRECTION_2LINES;
    hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
    hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
    hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
    hspi3.Init.NSS = SPI_NSS_SOFT;
    hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
    hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
    hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
    hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
    hspi3.Init.CRCPolynomial = 7;
    hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
    hspi3.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
    if (HAL_SPI_Init(&hspi3) != HAL_OK)
    {
        Error_Handler();
    }
}

Inside my HAL_SPI_Init(&hspi3) function, hspi3 does not give any error code and it goes to hspi->State = HAL_SPI_STATE_READY; IMAGE. But in the watch window, the hspi3 state does not get updated; it remains at HAL_SPI_STATE_RESET. Hence, when I try to send some data using SPI as shown below, it fails -

void TIM3_IRQHandler(void)
{
    static uint16_t sinTableIndex = 0;
    uint8_t data_on_spi[3];
    SysTime_mainFunction();
    if (getOperatingState() == CONTROL){
            data_on_spi[0]=sinTable[sinTableIndex] & MASK_LOW_BYTE;
            data_on_spi[1]=sinTable[sinTableIndex] & MASK_MID_BYTE;
            data_on_spi[2]=sinTable[sinTableIndex] & MASK_HIGH_BYTE;

            HAL_GPIO_WritePin(SPI3_Chip_Select_DAC_GPIO_Port,SPI3_Chip_Select_DAC_Pin,GPIO_PIN_RESET);
            if(HAL_SPI_Transmit(&hspi3, (uint8_t*)data_on_spi, 3,1) != HAL_OK)
            {
                    /* Transfer error in transmission process */
                    Error_Handler();
            }
            HAL_GPIO_WritePin(SPI3_Chip_Select_DAC_GPIO_Port,SPI3_Chip_Select_DAC_Pin,GPIO_PIN_SET);
            sinTableIndex++;
            if (sinTableIndex >= maxTableSize)
            {
                    sinTableIndex = 0;
            }
    }
  HAL_TIM_IRQHandler(&htim3);
}

When I HAL_SPI_Transmit(&hspi3, (uint8_t*)data_on_spi, 3,1) , it goes to error handler as it recognizes SPI state as HAL_SPI_STATE_RESET. Need help to understand why this is happening.

EDIT: I think the problem is that when I try to do HAL_SPI_Transmit(&hspi3, (uint8_t*)data_on_spi, 3,1),LOCK remains unlocked even if __HAL_LOCK(hspi) is done.

1
Have you enabled the SPI clock? I can't find this in your code snippet. I sometimes have issues with my Watch Window that it is not showing the correct enumerated value. Try to find the memory address and recheck the value there. Which debugger are you using?A.R.C.
@A.R.C. - yes, the spi clock is enabled inside 'HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)' function as '__HAL_RCC_SPI3_CLK_ENABLE()'. I am using Keil uVision 5.blazingcannon

1 Answers

1
votes
  1. Do not transfer in the polling mode in the interrupt handlers. Why: because it consumes a lots of time. Those HAL functions relay on the tick value which is increased in the another interrupt which may not happen because of the interrupt priorities.