STM32 I2C Problem

Gast #5509284
Lesenswert?
• ▲
▼
Hallo,

ich versuche mit einem STM32F103RCT6 ein Register von MPU6050 über I2C 
unter Verwendung std-cmsis-lib auszulesen. Es lässt sich problemlos 
beschreiben, nur das auslesen zum zweiten geht nicht mehr.

Hier ist mein Code:
1
#define MPU6050   ((uint8_t) 0x68)
2

3
void RCC_Configuration(void) {
4
    /* GPIOB clock enable */
5
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
6
    /* I2C clock enable */
7
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
8
}
9

10

11
void GPIO_Configuration(void) {
12
    GPIO_InitTypeDef GPIO_InitStructure;
13

14
    /* I2C1 PB6 = SCL, PB7 = SDA Pin Configuration */
15
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
16
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
17
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
18
    GPIO_Init(GPIOB, &GPIO_InitStructure);
19
}
20

21
void I2C_Configuration(void) {
22
    I2C_InitTypeDef I2C_InitStructure;
23

24
    I2C_DeInit(I2C1);
25

26
    I2C_InitStructure.I2C_ClockSpeed = 400000; // 400kHz
27
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
28
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
29
    I2C_InitStructure.I2C_OwnAddress1 = 0x00;
30
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
31
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
32
    I2C_Init(I2C1, &I2C_InitStructure);
33

34
    I2C_Cmd(I2C1, ENABLE);
35
}
36

37

38
void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) {
39
  trace_puts("i2c start\n");
40
  while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
41

42
    I2C_GenerateSTART(I2Cx, ENABLE);
43
    while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
44
    I2C_Send7bitAddress(I2Cx, address, direction);
45

46
    if (direction == I2C_Direction_Transmitter) {
47
        while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
48
    }
49
    else if (direction == I2C_Direction_Receiver) {
50
        while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
51
    }
52
}
53

54
void I2C_write(I2C_TypeDef* I2Cx, uint8_t data) {
55
    trace_puts("i2c write\n");
56
  I2C_SendData(I2Cx, data);
57
    while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
58
}
59

60
uint8_t I2C_read_nack(I2C_TypeDef* I2Cx) {
61
  trace_puts("i2c read nack\n");
62

63
    I2C_AcknowledgeConfig(I2Cx, DISABLE);
64
    while ( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) ) {
65
        trace_printf("i2c read nack stuck\n");
66
    }
67
    uint8_t data = I2C_ReceiveData(I2Cx);
68

69
    return data;
70
}
71

72
void I2C_stop(I2C_TypeDef* I2Cx) {
73
    trace_puts("i2c stop\n");
74

75
    I2C_GenerateSTOP(I2Cx, ENABLE);
76

77
    // Wait until I2C stop condition is finished
78
    while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_STOPF));
79
}
80

81
/* Single-Byte Read Sequence*/
82
uint8_t Read_Reg(uint8_t reg_adr) {
83
    I2C_start(I2C1, MPU6050 << 1, I2C_Direction_Transmitter);
84
    I2C_write(I2C1, reg_adr);
85
    I2C_stop(I2C1);
86

87
    I2C_start(I2C1, MPU6050 << 1, I2C_Direction_Receiver);
88
    uint8_t received_data = I2C_read_nack(I2C1);
89
    I2C_stop(I2C1);
90
    return received_data;
91
}
92

93
int main(void) {
94
  System_Clock_Setup();
95

96
  RCC_Configuration();
97
  GPIO_Configuration();
98
  I2C_Configuration();
99

100
  trace_puts("Hello\n");
101
  Delay(1e5);
102
  while (1) {
103
    trace_puts("loop begin\n");
104

105
    uint8_t addr = Read_Reg(0x75);
106
    trace_printf("addr: %i\n", addr);
107
                Delay(1e4);
108
  }
109
}

Debug Ausgabe:
1
Hello
2
loop begin
3
i2c start
4
i2c write
5
i2c stop
6
i2c start
7
i2c read nack
8
i2c stop
9
addr: 104
10
loop begin
11
i2c start
12
i2c write
13
i2c stop
14
i2c start
15
i2c read nack
16

17
i2c read nack stuck
18
i2c read nack stuck
19
i2c read nack stuck
20
i2c read nack stuck
21
i2c read nack stuck
22
i2c read nack stuck
23
i2c read nack stuck
24
i2c read nack stuck
25
i2c read nack stuck
26
i2c read nack stuck
27
i2c read nack stuck
28
i2c read nack stuck
29
i2c read nack stuck

Weiss jemand zufällig woran es liegt?
Vielen Dank im Voraus.

Antwort schreiben

Hinweis: der ursprüngliche Beitrag ist mehr als 6 Monate alt. Bitte hier nur auf die ursprüngliche Frage antworten, für neue Fragen einen neuen Beitrag erstellen.

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.