cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 103RB Problems with SPI1

fabseostner100
Associate II
Posted on March 25, 2009 at 15:22

STM32 103RB Problems with SPI1

3 REPLIES 3
fabseostner100
Associate II
Posted on May 17, 2011 at 13:07

Hi guys,

I have a problem with the SPI1-Interface. I use the Code below. When I replace the SPI1's with SPI2's in the code the SPI2-Interface runs. But the SPI1-Interface don't like it. I don't know why the Interface stops in the line:

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

I use a STM32F103RBT6 µC (128kB Flash & 20kB Ram), with the IAR ARM v.5.11, currently the Kickstart Version with the EWARM libraries.

1.) Are there differences between SPI2 and SPI1 Interface to setup ?

2.) Does anyone see a failure in my Interface setup ?

3.) Can someone give me a running code for the SPI1 ?

4.) Thx all for reading and I hope for helping me !

Code:

<BR>SPI_InitTypeDef SPI_struct; <BR> <BR> //deInit <BR> SPI_I2S_DeInit(SPI1); <BR> //GPIO_PinRemapConfig(GPIO_Remap_SPI1,DISABLE); <BR> <BR> /*Initialize Pins*/ <BR> //SS Pin <BR> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; <BR> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; <BR> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; <BR> GPIO_Init(GPIOA, &GPIO_InitStructure); <BR> <BR> //CD Pin <BR> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; <BR> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; <BR> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; <BR> GPIO_Init(GPIOA, &GPIO_InitStructure); <BR> <BR> //WP Pin <BR> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; <BR> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; <BR> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; <BR> GPIO_Init(GPIOC, &GPIO_InitStructure); <BR> <BR> //SCK / MISO / MOSI Pins <BR> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; <BR> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; <BR> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7; <BR> GPIO_Init(GPIOA, &GPIO_InitStructure); <BR> <BR> /* Enable GPIOA clock */ <BR> RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC , ENABLE); <BR> <BR> /* SPI1 Periph clock enable */ <BR> RCC_APB1PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); <BR> <BR> <BR>//SPI_struct. <BR> SPI_struct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; <BR> SPI_struct.SPI_Mode = SPI_Mode_Master; <BR> SPI_struct.SPI_DataSize = SPI_DataSize_8b; <BR> SPI_struct.SPI_CPOL = SPI_CPOL_Low; <BR> SPI_struct.SPI_CPHA = SPI_CPHA_1Edge; <BR> SPI_struct.SPI_NSS = SPI_NSS_Soft; <BR> SPI_struct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; <BR> SPI_struct.SPI_FirstBit = SPI_FirstBit_MSB; <BR> SPI_struct.SPI_CRCPolynomial = 7; <BR> <BR> //SPI init <BR> SPI_Init(SPI1, &SPI_struct); <BR> <BR> //Enable SPI <BR> SPI_Cmd(SPI1 , ENABLE); <BR>

Code:

<BR>//function to send a byte <BR>u8 MmcTransferByte (SPI_TypeDef* SPIx , u8 ch) <BR>{ <BR> SPI_I2S_SendData(SPIx, ch); <BR> while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET); //in this line the µC never come back ! <BR> return 0; <BR>} <BR>

[ This message was edited by: fabseostner100 on 25-03-2009 11:04 ]

sofiene
Associate III
Posted on May 17, 2011 at 13:07

Hi fabseostner100;

You have a little mistake in your SPI1 clock configuration

/* SPI1 Periph clock enable */

RCC_APB1PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

The SPI1 is clocked by the APB2 bridge and not APB1. You are using the wrong function to enable the SPI1 clock. So you have to correct your clock configuration as following:

/* SPI1 Periph clock enable */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

B.R.

M3allem

fabseostner100
Associate II
Posted on May 17, 2011 at 13:07

Thx, M3allem.

I search 2 days and doesn't see the fault.

Thx and greetz.

[ This message was edited by: fabseostner100 on 25-03-2009 19:53 ]