cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo F0: Problem with I2C

mfkfx
Associate II
Posted on August 01, 2014 at 15:59


 I2C_HandleTypeDef hi2c1; 
int main() 
{ 
HAL_Init(); 
SystemClock_Config(); 
MX_GPIO_Init(); 
MX_I2C1_Init(); 
HAL_I2C_Init(&hi2c1);
 if(!(HAL_OK == HAL_I2C_IsDeviceReady(&hi2c1,0x39,1,1000)))
 __ASM(''NOP'');//Breakpoint

} 
void MX_I2C1_Init(void) 
{ 
hi2c1.Instance = I2C1; 
hi2c1.Init.Timing = 0x1042C3C7; //value calculated as described in Ref. Man. 
hi2c1.Init.OwnAddress1 = 0x21; 
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; 
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; 
hi2c1.Init.OwnAddress2 = 0; 
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; 
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; 
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; 
HAL_I2C_Init(&hi2c1); 
HAL_I2CEx_AnalogFilter_Config(&hi2c1, I2C_ANALOGFILTER_DISABLED); 
}

Hello, I'm trying to communicate with a slave device (Adafruit TSL2561, I²C-Address: 0x39). I made a fast project using STM32 CubeMX and adapted it. However, the output of the Pins is not correct, see the attached screenshot (Addr. 0x38 instead of 0x39). Can anyone tell me what I did wrong? Thank you in advance!
5 REPLIES 5
Posted on August 01, 2014 at 16:33

Bit 0 normally denotes Read/Write and is not an Address, perhaps if you're using a 7-bit address you need to left shift it by 1

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mfkfx
Associate II
Posted on August 01, 2014 at 16:52

Thank you for your immediate response.

Unfortunately the device is still not responding.

Meanwhile I tried another device with slave address 0x60, which

did also not respond (tried with and without shift).

Is there something wrong with I2C initialization?

Thank you!

Posted on August 01, 2014 at 17:30

Is there something wrong with I2C initialization?

No idea, I'm not supporting HAL/CUBE code. I don't see any pin configuration code, you'd need to configure the pins correctly in OD (Open Drain) mode, and you'd need an appropriate external interface with pull-up resistors. Your analyzer output looks promising, but with I2C the lack of an ACK to an output address suggests the part you're talking too isn't hearing you. Check your supply and grounds, and SCL/SDA connectivity to the correct pins, and any address select pins on the I2C device.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mfkfx
Associate II
Posted on August 01, 2014 at 17:44

Hello again,

the sensor is wired up correctly. In the meantime I tried another code, but stillno responsefrom both sensors (with and without shifting the address):

void I2C_init(void) 
{ 
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); 
RCC_I2CCLKConfig(RCC_I2C1CLK_HSI); 
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1); 
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1); 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; 
GPIO_Init(GPIOB, &GPIO_InitStructure); 
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; 
I2C_InitStructure.I2C_DigitalFilter = 0x00; 
I2C_InitStructure.I2C_OwnAddress1 = 0x12; 
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; 
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; 
I2C_InitStructure.I2C_Timing = 0x1042C3C7; 
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; 
I2C_Init(I2C1, &I2C_InitStructure); 
I2C_Cmd(I2C1, ENABLE); 
} 
int main(void) 
{ 
I2C_init(); 
while(I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET); 
I2C_TransferHandling(I2C1, 0x39, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); 
}

Posted on August 01, 2014 at 19:47

0x39 *2 = 0x72

0x29 *2 = 0x52

0x49 *2 = 0x92
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..