Forum: µC & Digital Electronics STM32F303 Discovery: SPI basics

Author: Harald (Guest)
Posted on:

Rate this post
0 useful
not useful

I try my first programming on an STM32 Discovery Board (STM32F303). I 
was using avrs before for years :-)

First step should be to get the SPI to work. My IDE is Eclipse with gnu 
arm eclipse-plugin.
All I want to see for now is the working SPI on an oscilloscope.
But the following code shows nothing at CLK or MOSI:
int main(int argc, char* argv[])
  BlinkLed blinkLed; 
  GPIO_InitTypeDef GPIO_InitStructure;
  SPI_InitTypeDef SPI_InitStructure;


  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

  // PB13 = SCK; PB15 =  MOSI
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13 | GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode   = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_6);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_6);

  // Chip select pin
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode   = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_SetBits(GPIOB, GPIO_Pin_8);

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  SPI_InitStructure.SPI_CPHA       = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_CPOL       = SPI_CPOL_High;
  SPI_InitStructure.SPI_CRCPolynomial     = 1;
  SPI_InitStructure.SPI_DataSize     = SPI_DataSize_8b;
  SPI_InitStructure.SPI_Direction     = SPI_Direction_1Line_Tx;
  SPI_InitStructure.SPI_FirstBit     = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_Mode       = SPI_Mode_Master;
  SPI_InitStructure.SPI_NSS       = SPI_NSS_Soft;
  SPI_Init(SPI2, &SPI_InitStructure);


  uint8_t i = 0;

  while (1)

      SPI_SendData8(SPI2, i);

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


Is there any bus or clock I didn't switch on?
Trouble with the TX-FiFos? Must I use the TX-Interrupt?
I inspected the code with the debugger - it cycles correctly through the 

Any help would be appreciated. Thank you.

Author: Harald Graef (andobrawan)
Posted on:

Rate this post
0 useful
not useful
Wrong alternate port-function!!!!
Changed it to GPIO_AF_5 and it works.

GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_5);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_5);

Further on I had to add a line in the GPIO_InitStructure:
GPIO_InitStructure.GPIO_PuPd   = GPIO_PuPd_NOPULL;


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.