cancel
Showing results for 
Search instead for 
Did you mean: 

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

Kishor Bhayani
Associate II
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
9 REPLIES 9
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

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

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

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

Posted on September 08, 2017 at 08:42

Yes, these values were read out (at Keil->System Viewer->SPI->SPI1 ) 

from the running chip right after SPI initialization.

Posted on September 08, 2017 at 09:26

Yes, I tried doing that also ( 

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

Posted on September 08, 2017 at 09:35

Hello!

In errata sheet there is not such an issue.

It would help to  post the initialization code of SPI and GPIO.

Posted on September 08, 2017 at 10:39

I have updated the 

SPI and GPIO initialization code in the main question.

Posted on September 08, 2017 at 15:10

Hello!

You wrote that RXNE is set after initialization.  In your posted CR1 Reg. SPE bit is enabled.

During initialization SPE bit is normaly not enabled by HAL_SPI_Init() . So some other code enables the SPE after initialization. Check if this code is doing also something else relevant with issue.

Also check If macros SPI1_MOSI_AF, SPI1_SCK_AF lead both to AF0  (0x00)  and  if the   SPI1_SCK_GPIO_CLK_ENABLE(),  SPI1_MOSI_GPIO_CLK_ENABLE()   enable both the same  GPIOA clock. (pins PA7 and PA9) .

At your project->options ->C/C++ the STM32F030x6 must be defined (x4 and x6 uses the same headers)