cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Spi CLock Problem

Ronan Tussi
Associate
Posted on December 13, 2016 at 17:00

Hello! Im trying to comunicate via SPI with my AT25f4096 Flash Memory. Im using stm32f103 hal mode. It seems to be that the Clock is not working well. As you will see in the attachment, its not 'constant'. What possibly going wrong?

(sorry bout my osciloscope. Not good quality).

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

RCC_PeriphCLKInitTypeDef PeriphClkInit;

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;

RCC_OscInitStruct.LSEState = RCC_LSE_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL3;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)

{

Error_Handler();

}

PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_ADC

|RCC_PERIPHCLK_USB;

PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;

PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV4;

PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL;

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

{

Error_Handler();

}

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* SPI2 init function */

static void MX_SPI2_Init(void)

{

hspi2.Instance = SPI2;

hspi2.Init.Mode = SPI_MODE_MASTER;

hspi2.Init.Direction = SPI_DIRECTION_2LINES;

hspi2.Init.DataSize = SPI_DATASIZE_8BIT;

hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;

hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi2.Init.NSS = SPI_NSS_SOFT;

hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;

hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi2.Init.TIMode = SPI_TIMODE_DISABLE;

hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi2.Init.CRCPolynomial = 10;

if (HAL_SPI_Init(&hspi2) != HAL_OK)

{

Error_Handler();

}

}

2 REPLIES 2
Kaza K
Associate II
Posted on December 16, 2016 at 14:02

Hello,

I've also noticed problems with SPI on F103 (STM32F103CBT6) while using HAL libraries - same situation - working with serial flash. This drove me crazy, I'd spent over it few days without success.

Finally, I had to use standard libraries, but even then, I wasn't able to clock SCK line as long as I wanted after sending CMD byte(s). Solution is to send dummy bytes and then you can receive data from flash (1 dummy byte transmitted -> 1 byte recived and so on).

Best,

Kaza

Khouloud GARSI
Lead II
Posted on December 23, 2016 at 17:47

Hello,

ST is providing an example about the SPI usage. This will surely help you to correctly configure your peripheral.

You can find the working example

inside the STM32Cube

F1

 library (v 1.4.0) under

 this path: 

STM32Cube_FW_F1_V1.4.0\Projects\STM32F103RB-Nucleo\Examples\SPI.

May this help you.

Best regards,

Khouloud.