AnsweredAssumed Answered

[SOLVED] stm32f407 SPI2 setup

Question asked by Vidar (Z) on Sep 1, 2012
Latest reply on Sep 2, 2012 by Clive One
Hi

I have some issues with having SPI2 up and running on port B on the stm32f4 discovery board.  By reading the forum i see that several is having troubles setting up SPI and I have checked the most common errors mentioned like RCC settings and setting of AF, but all has been counted for already. I also used stm32f4_discovery_lis302dl.c as reference.

I went through the schematic to ensure the portpins are free and is using the following settup to enable SPI2. I would be grateful if someone could point out if there is some misunderstandings here. I have been looking into this code for so long now that I have got "code blind". I have probed the port pins with a logic-analyzer but there is no activity on either sck mosi or miso when trying to write to data register (used several methods). (EDIT: All lines remains low after reset and there is no sign that they will change level ever. Adding pullup will drive them high when enabled, no change in activity)

FYI: I am able to toggle chipselect using the setup as shown below.

First we have a few defines
<block>
// SPI interface
#define GFX_SPI_CHANNEL               SPI2
#define GFX_SPI_AF                    GPIO_AF_SPI2
#define GFX_SPI_RCC                   RCC_APB1Periph_SPI2

#define GFX_SPI_SCK_PIN               GPIO_Pin_13                 /* PB.13 - out */
#define GFX_SPI_SCK_PORT              GPIOB                       /* GPIOB */
#define GFX_SPI_SCK_RCC               RCC_AHB1Periph_GPIOB
#define GFX_SPI_SCK_PIN_NR            GPIO_PinSource13

#define GFX_SPI_MISO_PIN              GPIO_Pin_14                 /* PB.14 - in */
#define GFX_SPI_MISO_PORT             GPIOB                       /* GPIOB */
#define GFX_SPI_MISO_RCC              RCC_AHB1Periph_GPIOB
#define GFX_SPI_MISO_PIN_NR           GPIO_PinSource14

#define GFX_SPI_MOSI_PIN              GPIO_Pin_15                 /* PB.15 - out */
#define GFX_SPI_MOSI_PORT             GPIOB                       /* GPIOB */
#define GFX_SPI_MOSI_RCC              RCC_AHB1Periph_GPIOB
#define GFX_SPI_MOSI_PIN_NR           GPIO_PinSource15

#define GFX_SPI_CS_PIN                GPIO_Pin_12                 /* PB.12 - out */
#define GFX_SPI_CS_PORT               GPIOB                       /* GPIOB */
#define GFX_SPI_CS_RCC                RCC_AHB1Periph_GPIOB
#define GFX_SELECT()            (GFX_SPI_CS_PORT->BSRRH = GFX_SPI_CS_PIN)
#define GFX_DESELECT()          (GFX_SPI_CS_PORT->BSRRL = GFX_SPI_CS_PIN)

</block>

And then we have the init function
<block>
void GfxSpiInit(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  SPI_InitTypeDef  SPI_InitStructure;
 
  // Configure CS - Set to deselect
  RCC_AHB1PeriphClockCmd(GFX_SPI_CS_RCC, ENABLE);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
  GPIO_InitStructure.GPIO_Pin = GFX_SPI_CS_PIN;
  GPIO_Init(GFX_SPI_CS_PORT, &GPIO_InitStructure);

  GFX_DESELECT();

  // Configure the SPIx interface selected - ADDED TO SE IF IT WORKS
  SPI_I2S_DeInit(GFX_SPI_CHANNEL);

  /* Enable the SPI periph */
  RCC_APB2PeriphClockCmd(GFX_SPI_RCC, ENABLE);

  /* Enable GPIO clocks */
  RCC_AHB1PeriphClockCmd(GFX_SPI_SCK_RCC | GFX_SPI_MISO_RCC | GFX_SPI_MOSI_RCC, ENABLE);

  /* Set port pin to use an alternate function */
  GPIO_PinAFConfig(GFX_SPI_SCK_PORT, GFX_SPI_SCK_PIN_NR, GFX_SPI_AF);
  GPIO_PinAFConfig(GFX_SPI_MISO_PORT, GFX_SPI_MISO_PIN_NR, GFX_SPI_AF);
  GPIO_PinAFConfig(GFX_SPI_MOSI_PORT, GFX_SPI_MOSI_PIN_NR, GFX_SPI_AF);

  /*Configurate port pins - set common values first */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;

  /* SPI SCK pin configuration */
  GPIO_InitStructure.GPIO_Pin = GFX_SPI_SCK_PIN;
  GPIO_Init(GFX_SPI_SCK_PORT, &GPIO_InitStructure);

  /* SPI  MOSI pin configuration */
  GPIO_InitStructure.GPIO_Pin =  GFX_SPI_MOSI_PIN;
  GPIO_Init(GFX_SPI_MOSI_PORT, &GPIO_InitStructure);

  /* SPI MISO pin configuration */
  GPIO_InitStructure.GPIO_Pin = GFX_SPI_MISO_PIN;
  GPIO_Init(GFX_SPI_MISO_PORT, &GPIO_InitStructure);

  /* SPI block register configuration */
  SPI_I2S_DeInit(GFX_SPI_CHANNEL);
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // Control with CS from software
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_Init(GFX_SPI_CHANNEL, &SPI_InitStructure);

  /* Enable SPI */
  //SPI_I2S_DMACmd(GFX_SPI_CHANNEL, (SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx), DISABLE);
  //SPI_I2S_ITConfig(GFX_SPI_CHANNEL, SPI_I2S_IT_TXE, DISABLE);
  //SPI_I2S_ITConfig(GFX_SPI_CHANNEL, SPI_I2S_IT_RXNE, DISABLE);
  //SPI_I2S_ITConfig(GFX_SPI_CHANNEL, SPI_I2S_IT_ERR, DISABLE);
  SPI_Cmd(GFX_SPI_CHANNEL, ENABLE);  
 
  /* TEST TO SE IF ISSUE - Clear flags by read DR */
  {
    uint16_t Dummy = GFX_SPI_CHANNEL->DR;
  }
 
}
</block>

And finally I have a read/write function, but I have tested several including demo code with no success
<block>
uint8_t GfxSpi(uint8_t Data)
{
  GFX_SPI_CHANNEL->DR = Data;
  while ((GFX_SPI_CHANNEL->SR & SPI_SR_TXE) == 1); // Check on TXE bit (bit 1)
  return (uint8_t)GFX_SPI_CHANNEL->DR;
}
</block>

Thanks for any help

Breg
Vidar


----
And by the way: How can one add html tags here, the help link below the edit body field was pretty much useless. No need to say I am no found of MS-solutions, but all respect to those who are and can handle it ;)


Outcomes