Skip to main content
Kishor Bhayani
Associate
September 7, 2017
Question

Data is being sampled by SPI even if clock signal is not provided, when configured as slave.

  • September 7, 2017
  • 9 replies
  • 1812 views
Posted on September 07, 2017 at 15:40

We are using the STM32F030F4 micro-controller for our project. We need to receive the data from other STM32 micro-controller.

We are able to receive the data through SPI. However, data is being sampled even if clock is not provided ( i..e Only data line ( MOSI ) is connected, Clock is not connected ).

Below is the SPI register configuration:

CR1 Register

Register Bit parameter

Config value

Description

BIDIMODE

0

2-line unidirectional data mode

BIDIOE

0

Receive-only mode

CRCEN

0

CRC calculation disabled

CRCNEXT

0

Next transmit value is from TX buffer

CRCL

0

8-bit CRC length

RXONLY

1

Output disabled (Receive-only mode)

SSM

1

Software slave management enabled

SSI

0

LSBFIRST

0

data is received with the MSB first

SPE

1

Peripheral enabled

BR[2:0]

0

fPCLK/2

MSTR

0

Slave mode

CPOL

1

CK to 1 when idle

CPHA

1

The second clock transition is the first data capture edge

CR2 Register

Register Bit parameter

Config value

Description

DS [3:0]:

0x07

8 - bits data length for SPI transfers

Are we missing any configurations ? Could you please guide us to resolve this issue ?

Below is the SPI & GPIO initialization code:

[NOTE : The function HAL_SPI_MspInit() is getting invoked inside the HAL_SPI_Init() ]

SPI_HandleTypeDef     stSpiHandle = { 0 };  // SPI handler declaration

void SPIInit( void )

{

/* Set the SPI parameters */

   stSpiHandle.Instance = SPI1;

   stSpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;

   stSpiHandle.Init.Direction                  = SPI_DIRECTION_2LINES_RXONLY;

   stSpiHandle.Init.CLKPhase               = SPI_PHASE_2EDGE;

   stSpiHandle.Init.CLKPolarity             = SPI_POLARITY_HIGH;

   stSpiHandle.Init.CRCCalculation      = SPI_CRCCALCULATION_DISABLE;

   stSpiHandle.Init.DataSize                 = SPI_DATASIZE_8BIT;

   stSpiHandle.Init.FirstBit                    = SPI_FIRSTBIT_MSB;

   stSpiHandle.Init.NSS                        = SPI_NSS_SOFT;

   stSpiHandle.Init.NSSPMode             = SPI_NSS_PULSE_DISABLE;

   stSpiHandle.Init.TIMode                   = SPI_TIMODE_DISABLE;

   stSpiHandle.Init.Mode                      = SPI_MODE_SLAVE;

   if ( HAL_SPI_Init( &stSpiHandle ) != HAL_OK )

   {

         SET_FAULT_STATUS( FAULT_SPI_CONFIG_FAILED );

   }

}

void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)

{

   GPIO_InitTypeDef GPIO_InitStruct;

   if(hspi->Instance == SPI1)

   {

      /* Enable peripherals and GPIO Clocks */

      SPI1_SCK_GPIO_CLK_ENABLE();

      SPI1_MOSI_GPIO_CLK_ENABLE();

      SPI1_CLK_ENABLE();

      /* SPI SCK GPIO pin configuration */

 GPIO_InitStruct.Mode        = GPIO_MODE_AF_PP;

 GPIO_InitStruct.Speed       = GPIO_SPEED_FREQ_HIGH;

      GPIO_InitStruct.Pin            = SPI1_SCK_PIN;

      GPIO_InitStruct.Alternate   = SPI1_SCK_AF;

      HAL_GPIO_Init(SPI1_SCK_GPIO_PORT, &GPIO_InitStruct);

      /* SPI MOSI GPIO pin configuration */

 GPIO_InitStruct.Mode        = GPIO_MODE_AF_PP;

 GPIO_InitStruct.Speed       = GPIO_SPEED_FREQ_HIGH;

      GPIO_InitStruct.Pin            = SPI1_MOSI_PIN;

      GPIO_InitStruct.Alternate   = SPI1_MOSI_AF;

      HAL_GPIO_Init(SPI1_MOSI_GPIO_PORT, &GPIO_InitStruct);

   }

}

Thanks & Regards,

Kishor   

#stm32f030-spi-clock #stm32f0-spi #spi
This topic has been closed for replies.

9 replies

Vangelis Fortounas
Associate II
September 7, 2017
Posted on September 07, 2017 at 16:29

Hello!

I suppose , when you say '

Data is being sampled' you mean that the previously cleared RXNE flag is set.

Is the SCK pin grounded or floated?

If floated it could intercept 50/60 HZ from mains.

regards.

vf

Kishor Bhayani
Associate
September 8, 2017
Posted on September 08, 2017 at 08:13

Hello Thanks for your reply.

The SCK pin is pulled HIGH (Its not floated).

I have observed that RXNE flag is getting set right after SPI GPIO initialization and it is always set.

I am using 'HAL_SPI_Init()' for SPI initialization.

Regards,

Kishor

waclawek.jan
Super User
September 8, 2017
Posted on September 08, 2017 at 08:24

Below is the SPI register configuration:

Are these values read out from the running chip after SPI initialization?

JW

waclawek.jan
Super User
September 8, 2017
Posted on September 08, 2017 at 09:00

And what happens if you don't set the RXONLY bit?

It shouldn't matter, as long as you don't assign any pin to Tx (MISO ).

JW

Kishor Bhayani
Associate
September 8, 2017
Posted on September 08, 2017 at 09:26

Yes, I tried doing that also ( 

RXONLY bit is not set ). I got the same result.