cancel
Showing results for 
Search instead for 
Did you mean: 

spi2 not working in stm32f070

prateek agrawal
Associate II
Posted on October 04, 2017 at 07:38

Hello All

currently i am working in Fatfs with stm32f070 with spi1.

when i move to spi2 its not working.

i am trying multiple times but i cant get success.

here is my init function

void SPIFlashInit(void)

{

SPI_InitTypeDef SPI_Struct;

GPIO_InitTypeDef GPIO_Struct;

uint32_t stat;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB ,ENABLE);

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA ,ENABLE);

GPIO_Struct.GPIO_Pin = GPIO_Pin_15;

GPIO_Struct.GPIO_Mode = GPIO_Mode_OUT;

GPIO_Struct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_Struct);

GPIO_WriteBit(GPIOA,GPIO_Pin_15,Bit_SET);

// Configure SPI1 pins: SCK, MISO and MOSI

GPIO_Struct.GPIO_Pin =

GPIO_Pin_13

|

GPIO_Pin_14

|

GPIO_Pin_15

;

GPIO_Struct.GPIO_Mode = GPIO_Mode_AF;

GPIO_Init(GPIOB, &GPIO_Struct);

//Configure I/O for Flash Chip select //

GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_0);

GPIO_PinAFConfig(

GPIOB

,GPIO_PinSource14,GPIO_AF_0);

GPIO_PinAFConfig(

GPIOB

,GPIO_PinSource15,GPIO_AF_0);

//SPI1 configuration

SPI_Struct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_Struct.SPI_Mode = SPI_Mode_Master;

SPI_Struct.SPI_DataSize = SPI_DataSize_8b;

SPI_Struct.SPI_CPOL = SPI_CPOL_Low;

SPI_Struct.SPI_CPHA = SPI_CPHA_1Edge;

SPI_Struct.SPI_NSS = SPI_NSS_Soft;

SPI_Struct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;

SPI_Struct.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_Struct.SPI_CRCPolynomial = 7;

SPI_Init(SPI2, &SPI_Struct);

SPI_RxFIFOThresholdConfig (SPI2,SPI_RxFIFOThreshold_QF);

SPI_Cmd(SPI2, ENABLE);

}

please ans me where i am wrong in this code.

Note: this post was migrated and contained many threaded conversations, some content may be missing.
10 REPLIES 10
Posted on October 04, 2017 at 14:37

Hello!

What is your device exactly?

0690X00000608NAQAY.png

As you can see AF0  SPI2 is available on F070xB devices

Regards.

vf

Posted on October 04, 2017 at 15:50

hi,

i am using stm32f070 device. i know that its available on AF0  as you can see in my design. i am using AF0 for SPI2.

in my debug i can see that it will stuck on

FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG){}

for SPI_I2S_FLAG_RXNE: Receive buffer not empty flag.

Posted on October 04, 2017 at 17:45

Hello.

So. .. this is not an SPI initialization problem.

Did you change also the porting functions concern SPI1 inside user_diskio.c file.?

Rgrds

vf

Posted on October 04, 2017 at 19:03

>>i am using stm32f070 device

Ok, but what is the *fully* qualified part number? Does the SPI2 clock enable stick in the APB1 register?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 05, 2017 at 06:54

Hi Clive,

Yes SPI2 Clock enable in ABP1 register.

Posted on October 05, 2017 at 06:57

Hi,

i am not going to use any function of fatfs. i just enable SPI2 and send 0xFF to spi and wait for the response. 

as you can see i am using only this function listed below.

uint8_t spi_rw8(uint8_t data){

while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

SPI_SendData8(SPI2, data);

while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);

return SPI_ReceiveData8(SPI2);

Posted on October 05, 2017 at 08:37

Hello again.

As i understood  code stops at the second while function (wait for RXNE).

It is not normal.  Errata sheet not mention something similar.

If is possible, post CR1, CR2 and SR registers, when while loop stucks.

Posted on October 05, 2017 at 14:56

Hi,

register value is

CR1 = 0x0344

CR2 = 0x1700

SR = 0x0002

Posted on October 05, 2017 at 19:03

Hello again.

The values are normal. No interrupts enabled, no error flags, no DMA...

0690X00000608VLQAY.png0690X00000608VQQAY.png0690X00000608VVQAY.png

May be a faulty part .

If you have another board , check this issue  to it.

At least, write some ''clean'' code that will includes only clocks and SPI2 initialization and spi_rw8(..)  function called from main function. .

Post this code , post also the exact part type, so to have an answer from someone able to run the code to the same device.

Regards

vf