cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Problems

martin239955
Associate II
Posted on October 11, 2015 at 15:24

Hello,

I tried to test the I2C interface on a STM32:


#include <stdio.h>

#include <stdlib.h>

#include ''diag/Trace.h''

#include ''stm32f4xx_hal.h''

#include ''stdint.h''



int

main(
int
argc, 
char
* argv[])

{

GPIO_InitTypeDef init;

I2C_HandleTypeDef i2c1Handle;

uint8_t data[5] = {1,2,3,4,5};



/* CLKs enable only if needed */

__HAL_RCC_GPIOB_CLK_ENABLE();

__HAL_RCC_I2C1_CLK_ENABLE();


// SDL Port B8

init.Pin = GPIO_PIN_8;

init.Mode = GPIO_MODE_AF_OD;

init.Alternate = GPIO_AF4_I2C1;

init.Pull = GPIO_NOPULL;



HAL_GPIO_Init(GPIOB, &init);


init.Pin = GPIO_PIN_9;

HAL_GPIO_Init(GPIOB, &init);



i2c1Handle.Instance = I2C1;

i2c1Handle.Init.ClockSpeed = 100000; 
// 100kHz

i2c1Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

i2c1Handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;

i2c1Handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;

i2c1Handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;

i2c1Handle.Init.OwnAddress1 = 0x56;

HAL_I2C_Init(&i2c1Handle);


//HAL_I2C_Master_Transmit(&i2c1Handle, 0x3C, data, 1, 10000);

// Infinite loop

while
(1)

{

// Add your code here.

int
i = 0;

}

}

I found out, that when I init the speed variable, the program works. But now my question: Does the variables don't have an init value, because the registers have init values?
4 REPLIES 4
srdjan
Associate II
Posted on October 11, 2015 at 15:59

Timing register is initialized to 0x0 upon reset. That's why you have to setup proper clock speed parameters based on your CPU clock in order for i2c to work properly.

martin239955
Associate II
Posted on October 13, 2015 at 19:23

Yes, but the gpio speed register was initialized with low speed.

But in the hal_gpio_init(...) is a assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));

this assert crashes every time until I added the speed parameter. But why these variables don't have init values?

Amel NASRI
ST Employee
Posted on October 15, 2015 at 18:15

Hi marmsoler.martinn,

This seems to be your own code, not a CubeMX generated one...

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

martin239955
Associate II
Posted on October 26, 2015 at 21:30

I programm under linux with eclipse.