STM32F4 I2C Receiver Problem

OP #2646536
Hallo,

ich spiele zurzeit mit STM332F4Discovery I2C rum. Als Slave habe ich 
einen Arduino Duemilanove verwendet. (mit Receive und Request Events)
1
#include <Wire.h>
2

3
void setup()
4
{
5
  Wire.begin(0xAA);                // join i2c bus with address #4
6
  Wire.onReceive(receiveEvent); // register event
7
  Wire.onRequest(requestEvent);
8
  Serial.begin(115200);           // start serial for output
9
}
10

11
void loop()
12
{
13
  delay(100);
14
}
15

16
void receiveEvent(int howMany)
17
{
18
  while(1 < Wire.available()) // loop through all but the last
19
  {
20
    char c = Wire.read(); // receive byte as a character
21
    Serial.print(c);         // print the character
22
  }
23
  char x = Wire.read();    // receive byte as an integer
24
  Serial.println(x);         // print the integer
25
}
26

27
void requestEvent()
28
{
29
  Wire.write("hello"); // respond with message of 6 bytes
30
                       // as expected by master
31
}

Das Senden von Zeichen an den Arduino geht auch, aber wenn ich danach 
vom Arduino lesen möchte, (mit Wire.write()), bleibt der Stm32 an dieser 
Stelle in der while Schleife hängen: I2C2 an PB10/11; Atollic Studio 3.0
1
I2C_Send7bitAddress(I2C2, 0xAA >> 1, I2C_Direction_Receiver);
2
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == SET) {
3
    ;
4
  }

Hier einmal der Code, das ist nur ein reiner Test:
1
#include "stm32f4xx.h"
2

3
volatile unsigned int counter = 0;
4
volatile uint16_t arr[500];
5

6
/* Private macro */
7
/* Private variables */
8
void Delay(__IO uint32_t nCount);
9
/* Private function prototypes */
10
void setup_gps(void);
11
void setup_pc(void);
12
void setup_led(void);
13
void setup_exti_line0(void);
14

15
void setup_i2c();
16
void write_LSM303DLH(uint8_t adr, uint8_t * data, uint8_t lenght,
17
    uint8_t offset);
18
void read_LSM303DLH(uint8_t adr, uint8_t * data, uint8_t lenght, uint8_t index);
19
/* Private functions */
20

21
/**
22
 **===========================================================================
23
 **
24
 **  Abstract: main program
25
 **
26
 **===========================================================================
27
 */
28
int main(void) {
29
  SystemInit();
30

31
  int i = 0;
32

33
  uint64_t adr = 0x080E0000;
34

35
  /*uint64_t def =  *((uint64_t*)0x1FFF7A10);
36

37
   uint64_t read_data = *((uint64_t*)adr);
38

39
   *((uint64_t*)adr) = 0x00000000;
40

41
   uint64_t read_data2 = *((uint64_t*)adr);*/
42
  setup_gps();
43
  setup_pc();
44
  setup_led();
45
  setup_exti_line0();
46
  setup_i2c();
47

48
  Delay(0x3FFFFF);
49

50
  ///////////////////////////////////////////////////////////////////////////////
51
  /*uint8_t t[] = { 0x02, 0x00 };
52
   write_LSM303DLH(MAG_ADDRESS, t, 2, 0);
53
   t[0] = 0x00;
54
   t[1] = (1 << 3) | (1 << 4);
55
   write_LSM303DLH(MAG_ADDRESS, t, 2, 0);
56
   Delay(0x3FFFFF);
57
   t[0] = 0x20;
58
   t[1] = 0x27;
59
   write_LSM303DLH(ACC_ADDRESS, t, 2, 0);
60
   Delay(0x3FFFFF);
61
   uint8_t out[] = { 0, 0, 0, 0, 0, 0, 0 };
62
   read_LSM303DLH(MAG_ADDRESS, out, 6, 0x03);
63
   int x = 0, y = 0, z = 0;
64
   x = (out[0] << 8 | out[1]);
65
   y = (out[2] << 8 | out[3]);
66
   z = (out[4] << 8 | out[5]);*/
67

68
  while (I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) == SET) {
69
    ;
70
  }
71

72
  I2C_GenerateSTART(I2C2, ENABLE);
73
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT) == SET) {
74
    ;
75
  }
76

77
  I2C_Send7bitAddress(I2C2, 0xAA >> 1, I2C_Direction_Transmitter);
78
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)
79
      == SET) {
80
    ;
81
  }
82

83
  I2C_SendData(I2C2, 'H');
84
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == SET) {
85
    ;
86
  }
87

88
  I2C_SendData(I2C2, 'E');
89
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == SET) {
90
    ;
91
  }
92
  I2C_GenerateSTOP(I2C2, ENABLE);
93

94
  /////////////////////////////////////////////////////////////////////////////////////////////
95

96
  while (I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) == SET) {
97
    ;
98
  }
99

100
  I2C_GenerateSTART(I2C2, ENABLE);
101
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT) == SET) {
102
    ;
103
  }
104

105
  I2C_Send7bitAddress(I2C2, 0xAA >> 1, I2C_Direction_Receiver);
106
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == SET) {
107
    ;
108
  }
109

110
  char abc[5];
111

112
  abc[0] = I2C_ReceiveData(I2C2);
113
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED) == SET) {
114
    ;
115
  }
116

117
  abc[1] = I2C_ReceiveData(I2C2);
118
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED) == SET) {
119
    ;
120
  }
121

122
  abc[2] = I2C_ReceiveData(I2C2);
123
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED) == SET) {
124
    ;
125
  }
126

127
  abc[3] = I2C_ReceiveData(I2C2);
128
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED) == SET) {
129
    ;
130
  }
131

132
  abc[4] = I2C_ReceiveData(I2C2);
133
  while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED) == SET) {
134
    ;
135
  }
136

137
  I2C_GenerateSTOP(I2C2, ENABLE);
138

139

140
  USART_SendData(USART2,abc[0]);
141
  USART_SendData(USART2,abc[1]);
142
  USART_SendData(USART2,abc[2]);
143
  USART_SendData(USART2,abc[3]);
144
  USART_SendData(USART2,abc[4]);
145
  /////////////////////////////////////////////////////////////////
146

147
  while (1) {
148

149
  }
150
}
151

152
void setup_i2c() {
153

154
  /*  GPIO Setup  */
155
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
156
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
157

158
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);
159
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);
160

161
  GPIO_InitTypeDef GPIO_I2C;
162
  GPIO_I2C.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
163
  GPIO_I2C.GPIO_Mode = GPIO_Mode_AF;
164
  GPIO_I2C.GPIO_OType = GPIO_OType_OD;
165
  GPIO_I2C.GPIO_Speed = GPIO_Speed_50MHz;
166
  GPIO_I2C.GPIO_PuPd = GPIO_PuPd_NOPULL;
167
  GPIO_Init(GPIOB, &GPIO_I2C);
168

169
  /*  I2C Setup  */
170
  I2C_InitTypeDef I2C_init;
171
  I2C_init.I2C_Mode = I2C_Mode_I2C;
172
  I2C_init.I2C_ClockSpeed = 400000;
173
  I2C_init.I2C_DutyCycle = I2C_DutyCycle_16_9;
174
  I2C_init.I2C_OwnAddress1 = 0x00;
175
  I2C_init.I2C_Ack = I2C_Ack_Enable;
176
  I2C_init.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
177
  I2C_Init(I2C2, &I2C_init);
178

179
  I2C_Cmd(I2C2, ENABLE);
180
}

Hab ein paar Initfunktionen für USART usw. rausgelassen, damits 
übersichtlicher ist.


Eine andere Frage noch.

Ich muss am Anfang diese Funktion aufrufen, sonst passt das Timing des 
Prozessors nicht.
SystemInit();
Aber normalerweise wird das doch automatisch in der startup Datei 
gemacht?

MfG
Philipp

Antwort schreiben

Eine eigene Frage? Dieser Thread ist über 14 Jahre alt. Stell sie in einem eigenen Thread – dort lesen mehr Leute mit, und sie wird öfter und schneller beantwortet.

Neuen Thread starten

Eine Lösung, Ergänzung oder Rückmeldung zu diesem Thread? Dann antworte hier.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.