AnsweredAssumed Answered

STM32 discovery board SPI receive

Question asked by Mali.Shyam.001 on May 26, 2015
Latest reply on May 27, 2015 by waclawek.jan
Intro
I am using the STM32 Discovery board to talk with sensor that receives and sends data packet on SPI1. I have unpopulated U5 on the discovery board so that it lets me use the SPI1 port. I can send the data packet (3 bytes of data accompanied by 2 bytes of modbusCRC16 checksum. The sensor receives the packet ok and sends data back in a similar packet.
On implementing the checksum I found that none of the packets that i received were matching. On probing(literally) with a scope I found that the data on the scope did not match the memory (using KeilMDK debugger)
Issue
Last bit on certain bytes is carried or borrowed i.e. in a packet where the scope shows 04h, 00h,00h,30h,01h the memory shows 05h, 00h,00h,30h, 00h and is fairly consistent, another example is Scope : F0h,00h,00h,71h,F3h Memory: F1h,00h,00h,70h,F3h
I know that there is a dummy byte that comes as zero when first read, however, i do not understand this. Kindly advise.

spi.c

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_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
//  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
    hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  HAL_SPI_Init(&hspi1);
}

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hspi->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspInit 0 */

  /* USER CODE END SPI1_MspInit 0 */
    /* Peripheral clock enable */
    __SPI1_CLK_ENABLE();
 
    /**SPI1 GPIO Configuration    
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    PA15     ------> SPI1_NSS
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN SPI1_MspInit 1 */

  /* USER CODE END SPI1_MspInit 1 */
  }
}

void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
{

  if(hspi->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspDeInit 0 */

  /* USER CODE END SPI1_MspDeInit 0 */
    /* Peripheral clock disable */
    __SPI1_CLK_DISABLE();
 
    /**SPI1 GPIO Configuration    
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    PA15     ------> SPI1_NSS
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_15);

  /* USER CODE BEGIN SPI1_MspDeInit 1 */

  /* USER CODE END SPI1_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */
void HAL_SPI_enable(SPI_HandleTypeDef *hspi)
{
        if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
        {
      /* Enable SPI peripheral */
      __HAL_SPI_ENABLE(hspi);
        }
}
uint8_t HAL_SPI_TxRx1Byte(SPI_HandleTypeDef *hspi, uint8_t TxData)
{
        uint8_t RxData;
        static uint8_t firstEntryFlag = 0;
    /* Process Locked */
    __HAL_LOCK(hspi);
    /* Check if the SPI is not enabled */
    if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
    {
      /* Enable SPI peripheral */
      __HAL_SPI_ENABLE(hspi);
    }
        hspi->Instance->DR = TxData;
        while (!SPI_FLAG_RXNE);
        RxData = hspi->Instance->DR;

        __HAL_UNLOCK(hspi);
        
        return RxData;
}


Attachments

Outcomes