I2C Problems
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-10-11 6:24 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-10-11 6:59 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-10-13 10:23 AM
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?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-10-15 9:15 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-10-26 1:30 PM
I programm under linux with eclipse.
