STM32F303 Discovery: SPI basics

Guest #4530204
Hi,

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:
1
int main(int argc, char* argv[])
2
{
3
  BlinkLed blinkLed; 
4
  GPIO_InitTypeDef GPIO_InitStructure;
5
  SPI_InitTypeDef SPI_InitStructure;
6

7
  blinkLed.powerUp();
8

9
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
10
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
11

12
  // PB13 = SCK; PB15 =  MOSI
13
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13 | GPIO_Pin_15;
14
  GPIO_InitStructure.GPIO_Mode   = GPIO_Mode_AF;
15
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
16
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
17
  GPIO_Init(GPIOB, &GPIO_InitStructure);
18

19
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_6);
20
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_6);
21

22
  // Chip select pin
23
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8;
24
  GPIO_InitStructure.GPIO_Mode   = GPIO_Mode_OUT;
25
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
26
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
27
  GPIO_Init(GPIOB, &GPIO_InitStructure);
28

29
  GPIO_SetBits(GPIOB, GPIO_Pin_8);
30

31
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
32
  SPI_InitStructure.SPI_CPHA       = SPI_CPHA_2Edge;
33
  SPI_InitStructure.SPI_CPOL       = SPI_CPOL_High;
34
  SPI_InitStructure.SPI_CRCPolynomial     = 1;
35
  SPI_InitStructure.SPI_DataSize     = SPI_DataSize_8b;
36
  SPI_InitStructure.SPI_Direction     = SPI_Direction_1Line_Tx;
37
  SPI_InitStructure.SPI_FirstBit     = SPI_FirstBit_MSB;
38
  SPI_InitStructure.SPI_Mode       = SPI_Mode_Master;
39
  SPI_InitStructure.SPI_NSS       = SPI_NSS_Soft;
40
  SPI_Init(SPI2, &SPI_InitStructure);
41

42
  SPI_Cmd(SPI2, ENABLE);
43

44
  uint8_t i = 0;
45

46
  while (1)
47
    {
48
      i++;
49

50
      blinkLed.turnOff();
51
      SPI_SendData8(SPI2, i);
52

53
      while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET)
54
    {};
55

56
      blinkLed.turnOn();
57
    }
58
}

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 
while-loop...

Any help would be appreciated. Thank you.
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;

Reply

A question of your own? This thread is over 10 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.