cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with SPI2 on STM32F103RB

Stastny.Petr
Associate III
Posted on August 20, 2014 at 03:11

Hello, I want to use SPI2 on STM32F103RB to communicate with RFM12 module but SPI does not work. Can you help me where is a problem?

This is the definitions:

#define RFM_SPI2_PORT GPIOB

#define RFM_GPIO_CLK           RCC_APB2Periph_GPIOB

#define RFM_SPI2_IRQ GPIO_Pin_11

#define RFM_SPI2_NSS GPIO_Pin_12

#define RFM_SPI2_SCK GPIO_Pin_13

#define RFM_SPI2_MISO GPIO_Pin_14

#define RFM_SPI2_MOSI GPIO_Pin_15

void init_RFM_SPI2master(void){

GPIO_InitTypeDef gpioinit;

SPI_InitTypeDef SPI_InitStructure;

RCC_APB2PeriphClockCmd(RFM_GPIO_CLK, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

gpioinit.GPIO_Mode=GPIO_Mode_AF_PP;

gpioinit.GPIO_Speed=GPIO_Speed_50MHz;

gpioinit.GPIO_Pin = RFM_SPI2_MOSI|RFM_SPI2_SCK;

GPIO_Init(RFM_SPI2_PORT, &gpioinit);

gpioinit.GPIO_Mode=GPIO_Mode_IPU;

gpioinit.GPIO_Pin=RFM_SPI2_MISO;

GPIO_Init(RFM_SPI2_PORT, &gpioinit);

gpioinit.GPIO_Mode=GPIO_Mode_Out_PP;

gpioinit.GPIO_Pin=RFM_SPI2_NSS;

GPIO_Init(RFM_SPI2_PORT, &gpioinit);

GPIO_SetBits(RFM_SPI2_PORT,RFM_SPI2_NSS);

gpioinit.GPIO_Mode=GPIO_Mode_IN_FLOATING;

gpioinit.GPIO_Pin=RFM_SPI2_IRQ;

GPIO_Init(RFM_SPI2_PORT, &gpioinit);

SPI_Cmd(SPI2, DISABLE);

SPI_I2S_DeInit(SPI2);

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;

SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_Init(SPI2, &SPI_InitStructure);

SPI_Cmd(SPI2, ENABLE);

}

I send these commands:

RFM12_ClrNSEL();

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

    SPI_I2S_SendData(SPI2, 0x80E7);

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

keyreading = SPI_I2S_ReceiveData(SPI2);

while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);

RFM12_SetNSEL();

This is a picture from logic analyzer.

0690X000006056iQAA.png

Thank you very much for your help
1 REPLY 1
Posted on August 20, 2014 at 04:22

RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);

So not APB2 then?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..