STM32 SPI - FLAG/CS-Timing Problem

OP #3403527
Lesenswert?

Hallo Forum,

ich wollte gerne einmal in die Welt der STM32 µC hineinschnuppern und 
versuche mich hier gerade an einigen simplen Programmen. Jetzt habe ich 
aber ein großes Problem mit der SPI-Peripherie. Ich habe (hoffentlich) 
alles, GPIO und SPI-Modul richtig initialisiert und möchte in einer 
Endlosschleife einfach nur einen Wert (0x01) ausgeben. Das funktioniert 
auch, aber leider stimmt der CS Pin nicht mit meinen Daten überein. 
Wollte wie beim MSP430 warten, dass das TX-Empty-Flag gesetzt wird und 
anschließend soll er CS wieder auf HIGH legen. Das funktioniert aber aus 
irgendeinem Grunde nicht. Siehe SPI.PNG im Anhang. Ich verstehe nicht, 
warum der CS erst zwei Takte zu spät gesetzt wird.


Danke für die Hilfe:-)
1
int main(void) {
2

3
  GPIO_InitTypeDef GPIO_InitStruct;
4
  SPI_InitTypeDef SPI_InitStructure;
5

6
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
7

8
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
9

10
  /** SPI1 GPIO Configuration
11
   PA5   ------> SPI1_SCK
12
   PA7   ------> SPI1_MOSI
13

14
   PC0   ------> SPI1_CS
15
   */
16

17
  /*Enable or disable the AHB peripheral clock */
18
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
19

20
  /*Configure GPIO pin */
21
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
22
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
23
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
24
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
25
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
26
  GPIO_Init(GPIOA, &GPIO_InitStruct);
27

28
  /*Configure GPIO pin alternate function */
29
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_5 );
30

31
  /*Configure GPIO pin alternate function */
32
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_5 );
33

34
  /* Configure SPI1 pins:  CS PC0 ----------------------------*/
35
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
36
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
37
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
38
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
39
  GPIO_Init(GPIOC, &GPIO_InitStruct);
40

41
  /*SPI Config*/
42

43
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
44
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
45
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
46
  SPI_InitStructure.SPI_CRCPolynomial = 0;
47
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
48
  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
49
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
50
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
51
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
52
  SPI_Init(SPI1, &SPI_InitStructure);
53

54
  SPI_Cmd(SPI1, ENABLE);
55

56
  //int count = 0;
57

58
  while (1) {
59

60
    GPIO_WriteBit(GPIOC, GPIO_Pin_0, 0);  //CS auf LOW
61
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE ) == RESET)
62
      ;
63
    SPI_SendData8(SPI1, 0x01);
64
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE ) == RESET)
65
      ;
66
    GPIO_WriteBit(GPIOC, GPIO_Pin_0, 1);  //CS auf HIGH
67
    //for(count = 0; count <= 999; count++){}
68

69
  }
70

71
  /* Program will never run to this line */
72
  return 0;
73
}
Angehängte Dateien:
OP #3403562
Lesenswert?

Ah, das Brett vor meinem Kopf;-)

1
GPIO_WriteBit(GPIOC, GPIO_Pin_0, 0);  //CS auf LOW
2

3
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY ) == SET)
4
      ;
5
SPI_SendData8(SPI1, 0x01);
6

7
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY ) == SET)
8
      ;
9
GPIO_WriteBit(GPIOC, GPIO_Pin_0, 1);  //CS auf HIGH


Geht perfekt!


Vielen Dank

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren