2016-02-11 01:06 AM
Hi.
I need SPI to connect a radiomodule NRF24L I'm using STM32F429-discovery. According to the UM1670 pins PE2 trhough PE6 are free (nothing is connected to them). PE2 can be used as SPI4-SCK, PE5 – SPI4-MISO, PE6 – SPI4-MOSI.
I cannot make SPI work. The part of the code is attached. I inserted a code assigning the pins as outputs just to check that the physical connection is good (I step through lines while debugging and watch physical changes on the pins with logic analyzer). The physical connections are good.
What did I do wrong?
Thanks!
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE ,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI4,ENABLE);
/* Test of connectors - should be deleted later */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_SetBits(GPIOE,GPIO_Pin_2);
GPIO_ResetBits(GPIOE,GPIO_Pin_2);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_SetBits(GPIOE,GPIO_Pin_6);
GPIO_ResetBits(GPIOE,GPIO_Pin_6);
/* Test of connectors end */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOE, GPIO_Pin_2, GPIO_AF_SPI4);
GPIO_PinAFConfig(GPIOE, GPIO_Pin_5, GPIO_AF_SPI4);
GPIO_PinAFConfig(GPIOE, GPIO_Pin_6, GPIO_AF_SPI4);
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStruct.SPI_Direction= SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI_Init(SPIx, &SPI_InitStruct);
SPI_Cmd(SPI4, ENABLE);
SPI_I2S_SendData(SPI4, 0x12);
2016-02-11 02:52 AM
When you say that it doesn't work, do you mean that nothing comes out of the SCK and MOSI pins? Or that your slave device doesn't respond? If the latter, I don't see you asserting CS anywhere?
2016-02-11 03:41 AM
For STM32F429-Discovery & nRF I use these pins:
/**
* @brief NRF SPI Interface pins
*/
#define NRF_SPI SPI3
#define NRF_SPI_CLK RCC_APB1Periph_SPI3
#define NRF_SPI_SCK_PIN GPIO_Pin_3 /* PB.03 */
#define NRF_SPI_SCK_GPIO_PORT GPIOB /* GPIOB */
#define NRF_SPI_SCK_GPIO_CLK RCC_AHB1Periph_GPIOB
#define NRF_SPI_SCK_SOURCE GPIO_PinSource3
#define NRF_SPI_SCK_AF GPIO_AF_SPI3
#define NRF_SPI_MISO_PIN GPIO_Pin_11 /* PC.011 */
#define NRF_SPI_MISO_GPIO_PORT GPIOC /* GPIOC */
#define NRF_SPI_MISO_GPIO_CLK RCC_AHB1Periph_GPIOC
#define NRF_SPI_MISO_SOURCE GPIO_PinSource11
#define NRF_SPI_MISO_AF GPIO_AF_SPI3
#define NRF_SPI_MOSI_PIN GPIO_Pin_12 /* PC.12 */
#define NRF_SPI_MOSI_GPIO_PORT GPIOC /* GPIOC */
#define NRF_SPI_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOC
#define NRF_SPI_MOSI_SOURCE GPIO_PinSource12
#define NRF_SPI_MOSI_AF GPIO_AF_SPI3
#define NRF_SPI_CSN_PIN GPIO_Pin_7 /* PD.07 */
#define NRF_SPI_CSN_GPIO_PORT GPIOD /* GPIOD */
#define NRF_SPI_CSN_GPIO_CLK RCC_AHB1Periph_GPIOD
#define NRF_SPI_CE_PIN GPIO_Pin_9 /* PG.09 */
#define NRF_SPI_CE_GPIO_PORT GPIOG /* GPIOG */
#define NRF_SPI_CE_GPIO_CLK RCC_AHB1Periph_GPIOG
2016-02-11 12:02 PM
I mean that nothing comes out
2016-02-11 12:06 PM
Just found the problem.
GPIO_PinAFConfig(GPIOE, GPIO_PinSource2, GPIO_AF_SPI4); GPIO_PinSourcex not GPIO_Pin_x