AnsweredAssumed Answered

SPI clock frequency in STM32F769NI Disco board

Question asked by Jeevan Rudramurthy on Dec 12, 2017
Latest reply on Dec 12, 2017 by Jeevan Rudramurthy

how to configure for SPI clock frequency in SPI example provided.

 

Example give below i am using

 

In atmel board we are using 5mhz or 50khz SPI clock frequency.

 

int main(void)

{

  /* Enable the CPU Cache */

  CPU_CACHE_Enable();

  HAL_Init();

 

  /* Configure the system clock to 216 MHz */

  SystemClock_Config();

 

  /*##-1- Configure the SPI peripheral #######################################*/

  /* Set the SPI parameters */

  SpiHandle.Instance               = SPIx;

  SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;

  SpiHandle.Init.Direction         = SPI_DIRECTION_2LINES;

  SpiHandle.Init.CLKPhase          = SPI_PHASE_1EDGE;

  SpiHandle.Init.CLKPolarity       = SPI_POLARITY_LOW;

  SpiHandle.Init.DataSize          = SPI_DATASIZE_8BIT;

  SpiHandle.Init.FirstBit          = SPI_FIRSTBIT_MSB;

  SpiHandle.Init.TIMode            = SPI_TIMODE_DISABLE;

  SpiHandle.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLE;

  SpiHandle.Init.CRCPolynomial     = 7;

  SpiHandle.Init.NSS               = SPI_NSS_SOFT;

 

 

#ifdef MASTER_BOARD

  SpiHandle.Init.Mode = SPI_MODE_MASTER;

#else

  SpiHandle.Init.Mode = SPI_MODE_SLAVE;

  HAL_Delay(5); 

#endif /* MASTER_BOARD */

 

 

  if(HAL_SPI_Init(&SpiHandle) != HAL_OK)

  {

    /* Initialization Error */

    Error_Handler();

  }

#ifdef MASTER_BOARD

  /* Configure User push-button button */

  BSP_PB_Init(BUTTON_USER,BUTTON_MODE_GPIO);

  /* Wait for User push-button press before starting the Communication */

  while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_SET)

  {

    HAL_Delay(100);

  }

#endif /* MASTER_BOARD */

 

  /* Synchronization between Master and Slave */

  Slave_Synchro();

}

 

#ifdef MASTER_BOARD

 

static void Master_Synchro(void)

{

  uint8_t txackbytes = SPI_MASTER_SYNBYTE, rxackbytes = 0x00;

  do

  {

    /* Call SPI write function to send command to slave */

    if(HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t *)&txackbytes, (uint8_t *)&rxackbytes, 1, HAL_MAX_DELAY) != HAL_OK)

    {

      Error_Handler();

    }

  }while(rxackbytes != SPI_SLAVE_SYNBYTE);

}

#else

static void Slave_Synchro(void)

{

  uint8_t txackbyte = SPI_SLAVE_SYNBYTE, rxackbyte = 0x00;

//  do

  {

    if (HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t *)&txackbyte, (uint8_t *)&rxackbyte, 1, HAL_MAX_DELAY) != HAL_OK)

    {

      Error_Handler();

    }

  }

//  while (rxackbyte != SPI_MASTER_SYNBYTE);

}

#endif

 

/**

  * @brief  System Clock Configuration

  *         The system Clock is configured as follow :

  *            System Clock source            = PLL (HSE)

  *            SYSCLK(Hz)                     = 216000000

  *            HCLK(Hz)                       = 216000000

  *            AHB Prescaler                  = 1

  *            APB1 Prescaler                 = 4

  *            APB2 Prescaler                 = 2

  *            HSE Frequency(Hz)              = 25000000

  *            PLL_M                          = 25

  *            PLL_N                          = 432

  *            PLL_P                          = 2

  *            PLL_Q                          = 9

  *            PLL_R                          = 7 

  *            VDD(V)                         = 3.3

  *            Main regulator output voltage  = Scale1 mode

  *            Flash Latency(WS)              = 7

  * @param  None

  * @retval None

  */

void SystemClock_Config(void)

{

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_OscInitTypeDef RCC_OscInitStruct;

  HAL_StatusTypeDef ret = HAL_OK;

 

 

  /* Enable HSE Oscillator and activate PLL with HSE as source */

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

  RCC_OscInitStruct.HSEState = RCC_HSE_ON;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

  RCC_OscInitStruct.PLL.PLLM = 25;

  RCC_OscInitStruct.PLL.PLLN = 432;

  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

  RCC_OscInitStruct.PLL.PLLQ = 9;

  RCC_OscInitStruct.PLL.PLLQ = 7;

 

  ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);

  if(ret != HAL_OK)

  {

    while(1) { ; }

  }

 

  /* Activate the OverDrive to reach the 216 MHz Frequency */ 

  ret = HAL_PWREx_EnableOverDrive();

  if(ret != HAL_OK)

  {

    while(1) { ; }

  }

 

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */

  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; 

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

 

  ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);

  if(ret != HAL_OK)

  {

    while(1) { ; }

  } 

}

Outcomes