AnsweredAssumed Answered

Adapting SPI from F1 to F0

Question asked by navarro.benjamin.001 on Apr 23, 2013
Latest reply on May 2, 2013 by Jacobs.Carl.001
Hi,

I have some working code on STM32F103 that I want to use with a STM32F051 (Discovery board) but I have some troubles to get the SPI communication working. The program get stuck when waiting for an incoming data.

This is the initialization code :
01.SPI_InitTypeDef SPI_InitStructure;
02.GPIO_InitTypeDef GPIO_InitStructure;
03. 
04. //enable SPI2, GPIOA & GPIOB clocks.
05.RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
06.RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
07. 
08.//SCK, MISO, MOSI AF
09.GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_0);
10.GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_0);
11.GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_0);
12. 
13.//SCK, MISO, MOSI (AF, PP, NO PULL)
14.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15;
15.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
16.GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
17.GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
18.GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
19.GPIO_Init(GPIOB, &GPIO_InitStructure);
20. 
21.// CSN (OUT, PP, NO PULL)
22.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
23.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
24.GPIO_Init(GPIOB, &GPIO_InitStructure);
25. 
26.//CE (OUT PP, NO PULL)
27.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
28.GPIO_Init(GPIOA, &GPIO_InitStructure);
29. 
30.//IRQ (IN, NO PULL)
31.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
32.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
33.GPIO_Init(GPIOA, &GPIO_InitStructure);
34. 
35.SPI2_releaseChip();
36. 
37.SPI_Cmd(SPI2, DISABLE);
38./* SPI2 configuration */
39.SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
40.SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
41.SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
42.SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
43.SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
44.SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
45.SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
46.SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
47.SPI_InitStructure.SPI_CRCPolynomial = 7;
48.SPI_Init(SPI2, &SPI_InitStructure);
49. 
50.SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF);
51. 
52./* Enable SPI2   */
53.SPI_Cmd(SPI2, ENABLE);

and then the read/write function :
01.unsigned char SPI2_readWriteByte(unsigned char byte)
02.{
03.    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
04.    /* Send byte through the SPI2 peripheral */
05.    SPI_SendData8(SPI2, byte);
06.    /* Wait to receive a byte */
07.    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
08.    /* Return the byte read from the SPI bus */
09.    return SPI_ReceiveData8(SPI2);
10.}

The program hang on line 7.

I'm trying to communicate with an nRF24L01 but I also tried to connect MOSI to MISO directly with the same result.

Am I doing something wrong?

Thanks

Outcomes