Hallo Leute,
ich versuche gerade I2C auf dem STM32f4 zum laufen zu bringen. Dafür
habe ich mir eine I2CLib für den Mircocontroller im netz gesucht.
Ich will I2C2 verwenden, und habe dafür GPIOB Pin10/11 für das SDA und
SCL signal verwendet, zudem habe ich auch pullup widerstände an die pins
angebracht.
Wenn ich nun die Pins als OUT konfiguriere kann ich diese auch messbar
ein und ausschalten, d.h. die GPIO konfig müsste soweit ok sein.
Hier die Init funktion für I2C und GPIO:
1 | void I2C_LowLevel_Init(void)
|
2 | {
|
3 | GPIO_InitTypeDef GPIO_InitStructure;
|
4 | I2C_InitTypeDef I2C_InitStructure;
|
5 |
|
6 | //Enable the i2c
|
7 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
|
8 | //Reset the Peripheral
|
9 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
|
10 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
|
11 |
|
12 | //Enable the GPIOs for the SCL/SDA Pins
|
13 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
14 |
|
15 | //Configure and initialize the GPIOs
|
16 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; //Pin 10 SCL, Pin 11 SDA
|
17 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
18 | // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; (I/O test ok with OUT)
|
19 |
|
20 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
21 | GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
|
22 | //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
23 |
|
24 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
25 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
26 |
|
27 | //Connect GPIO pins to peripheral
|
28 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);
|
29 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);
|
30 |
|
31 | //Configure and Initialize the I2C
|
32 | I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
|
33 | I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
|
34 | I2C_InitStructure.I2C_OwnAddress1 = 0x00; //We are the master. We don't need this
|
35 | I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
|
36 | I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
37 | I2C_InitStructure.I2C_ClockSpeed = 50000; //400kHz (Fast Mode) (
|
38 |
|
39 | //Initialize the Peripheral
|
40 | I2C_Init(I2C2, &I2C_InitStructure);
|
41 | // I2C Peripheral Enable
|
42 | I2C_Cmd(I2C2, ENABLE);
|
43 |
|
44 | return;
|
45 | }
|
mit dieser funktion will ich die startcondition setzen und warte dass
das Startbit gesetzt wird, aber es gibt ein timeout:
1 | uint16_t I2C2_WrBufTest(void)
|
2 | {
|
3 | uint32_t TimeOut = HSI_VALUE;
|
4 |
|
5 | //StartCondition generieren
|
6 | I2C2->CR1 |= I2C_CR1_START;
|
7 |
|
8 | // SR1: = U²C Status Register, SB= StartBit
|
9 | while(((I2C2->SR1) & 0x0001) != 0x0001)
|
10 | {
|
11 | if (!(TimeOut--))
|
12 | {
|
13 | //panic(Flags, "I2C Error\n",2);
|
14 | return 1;
|
15 | }
|
16 | }
|
17 | return 0;
|
18 | }
|
Wo könnte das problem sein???? ich verwende ja die stmlib für I2c und
GPIO config, sollte doch "problemlos" funltionieren:) ??
Grüße und danke