Hallo,
vielleicht kann mir ja jemand helfen:
Ich benutze einen STM32F415RGT6 und will I2C nutzen. Hier ein
Minimalbeispiel wie ich das Modul aktiviere:
1 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
2 |
|
3 | GPIO_InitTypeDef gpioInit;
|
4 |
|
5 | GPIO_StructInit(&gpioInit);
|
6 |
|
7 | gpioInit.GPIO_Mode = GPIO_Mode_AF;
|
8 |
|
9 | gpioInit.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
|
10 |
|
11 | gpioInit.GPIO_PuPd = GPIO_PuPd_UP;
|
12 |
|
13 | gpioInit.GPIO_Speed = GPIO_Speed_25MHz;
|
14 |
|
15 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
|
16 |
|
17 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
|
18 |
|
19 | GPIO_Init(GPIOB, &gpioInit);
|
20 |
|
21 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
|
22 |
|
23 | I2C_DeInit(I2C1);
|
24 |
|
25 | I2C_InitTypeDef I2C_InitStructure;
|
26 |
|
27 | I2C_StructInit(&I2C_InitStructure);
|
28 |
|
29 |
|
30 | /* I2C configuration */
|
31 |
|
32 | I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
|
33 |
|
34 | I2C_InitStructure.I2C_ClockSpeed = 100000;
|
35 |
|
36 | I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
|
37 |
|
38 | I2C_InitStructure.I2C_OwnAddress1 = 0x01;
|
39 |
|
40 | I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
|
41 |
|
42 | I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
43 |
|
44 | I2C_Init(I2C1, &I2C_InitStructure);
|
45 |
|
46 | I2C_Cmd(I2C1, ENABLE);
|
47 |
|
48 |
|
49 | while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
|
50 |
|
51 |
|
52 | /* Generate Start, send the address and wait for ACK */
|
53 |
|
54 | I2C_GenerateSTART(I2C1, ENABLE);
|
55 |
|
56 | while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
|
57 |
|
58 | I2C_Send7bitAddress(I2C1, 0xE0, I2C_Direction_Transmitter);
|
59 |
|
60 |
|
61 | while (!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
|
Der Mikrocontroller hängt jedes mal bei der letzten Abfrage. Die
Statusregister des I2C sind nach dem Senden der Adresse wie folgt
gesetzt:
I2C1 -> SR1 = 1024
I2C1 -> SR2 = 3
das bedeutet, dass das Bit für Acknowledge Failure (AF) dauerhaft
gesetzt ist, der Master also nicht erkennt, dass der Slave ein ACK
gesendet hat. Das ganze ist unabhängig vom Slave, vom I2C-Peripheral und
von der Busgeschwindigkeit. Der Slave setzt das ACK jedoch (Siehe Anhang
f415.png)
Lade ich den selben Code (lediglich auf 400 khz, das ist aber
irrelevant) auf ein STM32F407-Disco Board hoch, funktioniert alles
einwandfrei. (Siehe Anhang f407.png)
Was mache ich falsch? Oder ist das Board einfach im Eimer? Alle anderen
Peripherals funktionieren einwandfrei.
Vielen Dank!