cancel
Showing results for 
Search instead for 
Did you mean: 

stm32-p107 spi

michaelscott2509
Associate II
Posted on February 03, 2012 at 21:20

Hi,

I am trying to get the SPI interface to work so that I can access the microSD card on the board. However I find that I cannot put the card into SPI mode, when sending CMD0 (go to idle) I read back 0, instead of the expected value of 1. Has anyone else come across this? I am using the example code from olimex as a base.

Here is my initialisation code:

/* Enable SPIy Periph clock */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);

/* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */

RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE );

/* Configure the MMC_CS (SS) output pin */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure the UEXT_CS pin as output */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Configure SCK and MOSI output pins */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_12;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOC, &GPIO_InitStructure);

/* Configure MISO input pin */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;//

GPIO_Init(GPIOC, &GPIO_InitStructure);

/* Rempa SPI3 to pins used in above initialisation */

GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;

SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

SPI_Init(SPI3, &SPI_InitStructure);

// Clock Freq. Identification Mode < 400kHz 

SdSetClockFreq(IdentificationModeClock);

/* Enable SPI3 */

SPI_Cmd(SPI3, ENABLE);

Any help is appreciated.

#spi-problems-stm32
3 REPLIES 3
rosarium
Associate II
Posted on February 04, 2012 at 10:02

Why are you using SPI in such a slow speed. Is it your requirement. I found you using a very high pre-scaler value.

Any find the following example. it works ok my side.

void SPI_Config(void)

{

  GPIO_InitTypeDef  GPIO_InitStructure;

  SPI_InitTypeDef   SPI_InitStructure;

  /* GPIOA and GPIOC Periph clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);

 

   GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);

     /* SPI3 Periph clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);

  /* Configure SPI3 pins: SCK, MISO and MOSI */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* Configure PA4 pin: CS pin */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* SPI3 Config */

  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

  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_2;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI3, &SPI_InitStructure);

  /* SPI3 enable */

  SPI_Cmd(SPI3, ENABLE);

}

rosarium
Associate II
Posted on February 04, 2012 at 10:06

Why are you using SPI in such a slow speed. Is it your requirement. I found you using a very high pre-scaler value.

Any way find the following example. it works ok my side.

void SPI_Config(void)

{

  GPIO_InitTypeDef  GPIO_InitStructure;

  SPI_InitTypeDef   SPI_InitStructure;

  /* GPIOA and GPIOC Periph clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);

 

   GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);

     /* SPI3 Periph clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);

  /* Configure SPI3 pins: SCK, MISO and MOSI */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* Configure PA4 pin: CS pin */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* SPI3 Config */

  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

  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_2;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI3, &SPI_InitStructure);

  /* SPI3 enable */

  SPI_Cmd(SPI3, ENABLE);

}

michaelscott2509
Associate II
Posted on February 04, 2012 at 12:48

Thanks for the example, I don't have a specific speed requirement at the moment, so I've changed it to you're suggested value. 

What I notice when hooking up the SPI to an oscillascope is that the chip select pin doesn't seem to change when I tell it to, I'm guessing this is why when sending data to SPI that nothing shows up on the MOSI line as well? 

Here is my chip select code:

 /* To select the MMC card, set CS_MMC to 0, CS_UEXT to 1 */

GPIO_ResetBits(GPIOA, GPIO_Pin_4);

GPIO_SetBits(GPIOB, GPIO_Pin_15);

 /* To de-select MMC card, set CS_MMC to 1, CS_UEXT to 0 */

GPIO_SetBits(GPIOA, GPIO_Pin_4);

GPIO_ResetBits(GPIOB, GPIO_Pin_15);

Is this not the correct way to do it?