Forum: Mikrocontroller und Digitale Elektronik STM32 SPI2 hängt sich auf


von ArtF (Gast)


Lesenswert?

Habe ein Problem beim STM32 und komme nicht weiter. Der STM32 läuft mit 
72MHz und an der SPI2 Schnittstelle ist nichts weiter dran, als nur ein 
FM25H20 Fram (/W ist via pull-up an Vcc). Habe in der STM32 config SPI 
und SPI2 freigegeben, GPIOB eingeschaltet und SPI2 am APB1 ebenfalls 
aktiviert. Leider hängt sich der uC in der Leseroutine nach 3 Bytes auf 
und gibt davor nur 0xFF aus, obwohl der FRAM theoretisch mit 0x55 und 
0xAA abwechselnd beschrieben wird. Habe schon fremden Code für Flash 
Ansteuerung angesehen, abgesehen von paar Unterschieden bei 
Initialisierung, finde ich nichts besonderes. Hier sind die Routinen:
1
void 
2
spi2_config (void) {
3
  
4
  GPIO_InitTypeDef GPIO_InitStructure;
5
6
  /* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/
7
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
8
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
9
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
10
  GPIO_Init(GPIOB, &GPIO_InitStructure);
11
  
12
  /* Configure Slave Select Pin --------------------------------------------*/
13
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_12;
14
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
15
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
16
  GPIO_Init(GPIOB, &GPIO_InitStructure);
17
18
  // set slave select to high  
19
  GPIO_WriteBit(GPIOB, GPIO_Pin_12, 1);
20
}
21
22
void 
23
spi2_init (void) {
24
  
25
  SPI_I2S_DeInit(SPI2);
26
  spi2_config ();
27
   
28
  SPI_InitTypeDef  SPI_InitStructure;
29
30
  /* SPI2 configuration ------------------------------------------------------*/
31
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
32
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
33
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
34
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
35
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
36
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
37
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
38
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
39
  SPI_Init(SPI2, &SPI_InitStructure);
40
  
41
  SPI_CalculateCRC(SPI2, DISABLE);
42
  SPI_Cmd(SPI2, ENABLE);
43
}
44
45
46
///////////////////////////////////////////////////////////////////////////////
47
// enable FRAM
48
///////////////////////////////////////////////////////////////////////////////
49
void 
50
FRAM_enable (void) {
51
    //SPI_SSOutputCmd(SPI2, ENABLE);
52
    GPIO_WriteBit(GPIOB, GPIO_Pin_12, 0);
53
}
54
55
///////////////////////////////////////////////////////////////////////////////
56
// disable FRAM
57
///////////////////////////////////////////////////////////////////////////////
58
void 
59
FRAM_disable (void) {
60
    //SPI_SSOutputCmd(SPI2, DISABLE);
61
    GPIO_WriteBit(GPIOB, GPIO_Pin_12, 1);
62
}
63
64
///////////////////////////////////////////////////////////////////////////////
65
// initialize FRAM
66
///////////////////////////////////////////////////////////////////////////////
67
void 
68
FRAM_init (void) {
69
70
  // set write enable latch
71
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET);
72
  
73
  FRAM_enable();
74
  SPI_I2S_SendData(SPI2, 0x06);    
75
  FRAM_disable();
76
}
77
78
///////////////////////////////////////////////////////////////////////////////
79
// Write Byte to Fram
80
///////////////////////////////////////////////////////////////////////////////
81
void 
82
FRAM_write_byte (unsigned char b) {
83
  
84
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET);
85
  SPI_I2S_SendData(SPI2, b);
86
}
87
88
///////////////////////////////////////////////////////////////////////////////
89
// Write Byte to Fram
90
///////////////////////////////////////////////////////////////////////////////
91
unsigned char 
92
FRAM_read_byte (void) {
93
    
94
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE)==RESET);
95
  return SPI_I2S_ReceiveData(SPI2); 
96
}
97
98
99
100
void 
101
FRAM_Test () {
102
   
103
    unsigned char mod_byte= 2;
104
    unsigned int limit = 0;
105
    unsigned char byte = 0;
106
    
107
    // write data to fram
108
    FRAM_enable();
109
110
    // write address -> start address is 0
111
    FRAM_write_byte (0x02); // write memory data command
112
    FRAM_write_byte (0x00); // addr
113
    FRAM_write_byte (0x00); // addr
114
    FRAM_write_byte (0x00); // addr
115
    
116
    for (limit = 0; limit < 200; limit++) {
117
        if (mod_byte % 2) {
118
            FRAM_write_byte (0xAA);
119
        }
120
        else {
121
            FRAM_write_byte (0x55);
122
        }
123
        mod_byte++;
124
    }
125
126
    FRAM_disable(); 
127
128
    // read data from fram
129
    FRAM_enable();
130
    printf("\r\nread data from FRAM\r\n");
131
    FRAM_write_byte (0x03); // read memory data command
132
    FRAM_write_byte (0x00); // addr
133
    FRAM_write_byte (0x00); // addr
134
    FRAM_write_byte (0x00); // addr
135
    
136
    for (limit = 0; limit < 200; limit++) {
137
        byte = FRAM_read_byte();
138
        printf("%#X ", byte); // nach 0x55 0x55 0x55 läuft der uC nicht weiter
139
    }
140
    FRAM_disable();
141
/**/
142
}

Die Funktion FRAM_Test () wird auf jeden Fall erst aufgerufen, nach dem 
SPI und Fram in main initialisiert werden. Jemand noch eine Idee, was 
ich übersehen/vergessen konnte?

von Daniel R. (zerrome)


Lesenswert?

Hallo,
müsste der MISO Pin nicht irgendwie als Eingang definiert werden?
Zudem würde ich da den Pullup für aktivieren.

Viele Grüße

Daniel

von ArtF (Gast)


Lesenswert?

Die sind definiert wie in den Beispielen, als Alternativ Function Pins.

von Daniel R. (zerrome)


Lesenswert?

Auszug aus der FW Lib 3.4.0

1
  if(SPIy_Mode == SPI_Mode_Master)
2
  {
3
    /* Confugure MISO pin as Input Floating  */
4
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
5
  }


besser wäre aber meiner Meinung nach:

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

Also Input mit Pullup, anstelle von Floating...

Mag mich aber auch irren, bin neu bei den STM32.

Hast Du denn mal das SPI so ausprobiert ob es geht, oder Dir die Signale 
angeschaut?

Viele Grüße

Daniel

von Alfred (Gast)


Lesenswert?

Füge folgendes ein:
SPI_InitStructure.SPI_CRCPolynomial = 7;
Warum 7 ? weiß ich auch nicht....

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.