cancel
Showing results for 
Search instead for 
Did you mean: 

SPI clock polarity initalization in STM32 Cube L0

johan2
Associate II
Posted on June 18, 2014 at 10:07

I have a project based on the example found in

STM32Cube_FW_L0_V1.0.0\Projects\STM32L053R8-Nucleo\Examples\SPI\SPI_FullDuplex_ComIT

I have changed the initialization of   SpiHandle.Init.CLKPolarity  to SPI_POLARITY_LOW in the call to HAL_SPI_Init.

However it seems like the logic level of the clock stays high until I send the first byte of data on the SPI. After the first byte is sent the clock stays low and so on for the following bytes.

Are not the setting of low clock polarity supposed to keep the clock line low when in idle state? I think that this would be the correct state after the init call. Or am I missing some additional initalization to do when using low clock polarity?

#spi #stm32l0
1 REPLY 1
RoofG
Associate

I am having the same problem getting my spi to work. Required is CPOL=0 (Clock low when inactive) and CPHA=1 ( get data on trailing clock). The initial clock level is high and only goes to the required low once the first package is sent. Slave is active low.

These are my setup functions, am i missing something?

static 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_2EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
 
 
}
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 */
    __HAL_RCC_SPI1_CLK_ENABLE();
  
    /**SPI1 GPIO Configuration    
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    GPIO_InitStruct.Pin = GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    /* SPI1 interrupt Init */
    HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(SPI1_IRQn);
  /* USER CODE BEGIN SPI1_MspInit 1 */
 
  /* USER CODE END SPI1_MspInit 1 */
  }
 
}

and then i send by using this:

HAL_StatusTypeDef spi_status = HAL_SPI_TransmitReceive(&hspi1,0b000000000000, 0b000000000000, sizeof(uint16_t),0xffff);

This is what the communication looks like, the clock line is initially high which in my opinion should be low. After and between the transmissions the clock line goes low as expected.

0690X0000060NdIQAU.jpg