AnsweredAssumed Answered

Configuring SPI HAL driver to control L6482H

Question asked by ramirez_prieto.valen on May 14, 2015
Latest reply on May 21, 2015 by Enrico Poli
Hello,

I need a right SPI configuration and Send_Receive_byte() funtion for using with STM32F4, STM32CubeMX 4.7.0 and HAL drivers... Does anyone have it?

I have a project running fine with STM32F4xx StdPeriph_Driver V1.2.0 to connect my STM32F4 and my L6482H chips.

Now, I am trying to migrate my code, for using STM32 CubeMX v4.7.0 and HAL drivers. See below:

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  if(hspi->Instance==SPI1)
  {
    /* Peripheral clock enable */
    __SPI1_CLK_ENABLE();
 
    /**SPI1 GPIO Configuration    
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
*/
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* System interrupt init*/
    HAL_NVIC_SetPriority(SPI1_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(SPI1_IRQn);
  }
}

----------------------------------------------------------
/* SPI1 init function */
void MX_SPI1_Init(void)
{
  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_HIGH;
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_ENABLED;
    hspi1.Init.CRCPolynomial = 7;
  HAL_SPI_Init(&hspi1);
}

----------------------------------------------------------
uint8_t CSPIN_Write_Byte(SPI_HandleTypeDef *hspi, uint8_t byte) {
        
    // Check if SPI is running
    if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE) __HAL_SPI_ENABLE(&hspi1);
    
    /* nSS signal activation - low */
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET);
    
    // SPI byte send.
   hspi->Instance->DR = byte;
    
    while (__HAL_SPI_GET_FLAG(&hspi1, SPI_FLAG_BSY) == true){};            

     // nSS signal deactivation - high
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET);         
            
        return (uint8_t)(hspi->Instance->DR);
}

-------------------------------------------------------

But it doesnt work.

As you can see in pictures, the waveforms seem correct, but it does not work...


With STM32F4xx StdPeriph_Driver V1.2.0 
(it runs fine)

cap1.png


With STM32CubeMX and HAL drivers 
(It does not run, and MISO always returns 0x00)

Cap2.png

Outcomes