cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L0 HAL_SPI_RECEIVE unexpected clk generation

Thomas LB
Associate III
Posted on September 18, 2017 at 17:02

Hi all,

First time programming with STM32 MCU. I'm using STM32CubeMX V4.22.0 with STM32Cube_FW_L0_V1.9.0. 

I'm trying to get 16bits ADC value with HAL_SPI_RECEIVE call and got a very strange behavior :

1st call : OK

2nd call : KO (32 clk pulse instead of 16 expected)

all next : OK

with step by step debug : always KO (didn't check clk generation at that time)

Same issue with HAL_SPI_TRANSMITRECEIVE

I tried with Hadware NSS (which is then always active by the way...) or GPIO controled : same behavior.

Very strange hum ?

main.c :

/* Includes ------------------------------------------------------------------*/

&sharpinclude 'main.h'

&sharpinclude 'stm32l0xx_hal.h'

&sharpinclude 'spi.h'

&sharpinclude 'usart.h'

&sharpinclude 'gpio.h'

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

void read_spi(uint8_t * buffer);

int main(void)

{

/* Buffer used for reception */

uint8_t Result_read[]={0,0};

HAL_Init();

SystemClock_Config();

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_USART2_UART_Init();

MX_SPI1_Init();

while (1)

{

HAL_Delay(10000); //delay 10s

read_spi(Result_read);

}

}

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

RCC_PeriphCLKInitTypeDef PeriphClkInit;

/**Configure the main internal regulator output voltage

*/

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;

RCC_OscInitStruct.MSIState = RCC_MSI_ON;

RCC_OscInitStruct.MSICalibrationValue = 0;

RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;

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(__FILE__, __LINE__);

}

PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;

PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Configure the Systick interrupt time

*/

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick

*/

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

void read_spi(uint8_t * buffer)

{

HAL_SPI_Receive(&hspi1, buffer, 1, 0xFF); // ok

}

spi.c

/* Includes ------------------------------------------------------------------*/

&sharpinclude 'spi.h'

&sharpinclude 'gpio.h'

SPI_HandleTypeDef hspi1;

/* SPI1 init function */

void MX_SPI1_Init(void)

{

HAL_SPI_MspInit(&hspi1);

hspi1.Instance = SPI1;

hspi1.Init.Mode = SPI_MODE_MASTER;

hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;

hspi1.Init.DataSize = SPI_DATASIZE_16BIT;

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;

hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;

hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi1.Init.CRCPolynomial = 7;

if (HAL_SPI_Init(&hspi1) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)

{

GPIO_InitTypeDef GPIO_InitStruct;

if(spiHandle->Instance==SPI1)

{

/* SPI1 clock enable */

__HAL_RCC_SPI1_CLK_ENABLE();

/**SPI1 GPIO Configuration

PA4 ------> SPI1_NSS

PA5 ------> SPI1_SCK

PA6 ------> SPI1_MISO

*/

GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

}

Any help will be highly appreciated

#hal_spi_transmitreceive #hal_spi_receive #spi
1 REPLY 1
Thomas LB
Associate III
Posted on September 21, 2017 at 09:37

up 

(First time using this community support, don't hesitate to tell me if you need somlething more).