2011-06-09 03:59 AM
Hi,
i am using the code below to try to communicate with a I2C sensor. I have a STM32F207VG with the corresponding peripheral lib v1.0, both lines have 4k7 pull-ups.
The problem has 2 aspects:
- there is no clock signal on the SCL after the start condition
- the acknowledge failure AF is set after the first call to I2C_SendData
I don't know which one is the cause and which one is the consequence, but that AF is certainly nothing which has to do with its �real� purpose, as no data has yet been transmitted, what acknowledge should have failed?
&sharpdefine I2C1_PORT GPIOB
&sharpdefine I2C1_SCL_PIN GPIO_Pin_8
&sharpdefine I2C1_SDA_PIN GPIO_Pin_9
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
/* ----Setup phase---- */
/* Enable I2C1 and I2C1_PORT & Alternate Function clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Reset I2C1 IP */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
/* Release reset signal of I2C1 IP */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
/* I2C1 SCL and SDA pins configuration */
GPIO_InitStructure.GPIO_Pin = I2C1_SCL_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(I2C1_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = I2C1_SDA_PIN;
GPIO_Init(I2C1_PORT, &GPIO_InitStructure);
/* Alternate function
remapping
*/GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
/* I2C
De
-initialize to clean */I2C_DeInit(I2C1);
/* I2C configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 400000;
/* I2C Initialize */
I2C_Init(I2C1, &I2C_InitStructure);
/* I2C ENABLE */
I2C_Cmd(I2C1, ENABLE);
/*----- Transmission Phase -----*/
/* Wait if the bus is busy */
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send sensor address */
I2C_SendData(I2C1,0xF1);
It is step by step register readout that makes me think that the problem is the first call to I2C_SendData. Up to there, all registers show what they should, but as soon as this call takes place, the register SR1 signals the AF. The data register is filled with the correct address but the AF disables the peripheral, the data will not be transmitted. I already tried to do some sw_rst, I2C_ClearITPendingFlags, re-enable peripheral or generate a second start condition � all useless.
On the scope one can see that after the initialization, both lines go high as they should. Then, after about 5(ms), SDA goes down for about a tenth of a ms before returning to high. At the same time, SCL also ''tries'' to go down, but it doesn't even arrive at 0V level before it's pulled high again. I suppose the MCU attempts to generate the start condition here, but should'nt there be a delay between the SDA and SCL transitions? Afterwards, both lines stay high forever and that's it...
Any ideas? Help would really be appreceated!
Don TseTse
#problem-with-i2c #stm32f-i2c #i2c #stm32f2011-12-11 10:34 PM
Hi,
The problem you talked about already exsits in STM32F1xx series MCUs. There is a new method to solve the problem for STM32F2xx series MCUs. You can use DMA to transfer the I2C data. I have done it well onhttp://www.armkits.com/product/devkit1asp
board. I enclose the software example for your reference. ________________ Attachments : main_c.txt : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0qT&d=%2Fa%2F0X0000000beL%2Ffhk3Hj_fqY2b0AfIGWW8MWiYsHjRd_rYzPWa2CGrc.Q&asPdf=falsestm322xg_eval_c.txt : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0oJ&d=%2Fa%2F0X0000000beK%2Ff1to88W2mF0WFpxwaUsr5PpWQlRHvi1.giqxe.WqvfI&asPdf=falsestm322xg_eval_h.txt : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0QJ&d=%2Fa%2F0X0000000beM%2Fa_la2G278sMR_jBfszmZLFWGgyrH1WdPpxdT.NMRzL8&asPdf=falsestm322xg_eval_i2c_ee_c.txt : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0qO&d=%2Fa%2F0X0000000beH%2FthU3281N2U5g5yr5W2CGPkR9009h3OLzf_Nd_7sLLn0&asPdf=falsestm322xg_eval_i2c_ee_h.txt : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0qJ&d=%2Fa%2F0X0000000beI%2Fsoc3bkUh28hF8VnT3.2zFEnWr76dnhlZGz1Lr4dY_Dg&asPdf=false2011-12-11 11:39 PM
2012-03-13 07:49 PM
Dear DonTse Tse
Good day. I also have same problem with you.I have checked my setting, and it is same as yours.I modify the code form DCMI example which is in STM32F2xx_StdPeriph_Lib_V1.0.0.Do you found the problem of this issue? If you have, would please provide me some suggestions to fix it?Thanks and best regards,2012-03-14 06:04 AM
I have not even seen a STM32F2xx but have to ask:
is the horrible, awful, terrible, shitty, crappy I²C interface from the f1's also in the f2's? Erik2012-03-15 12:48 AM
Dear All
I have finished this problem, and it works very well. God!!! It wasted so much time.The below is my setting code, just for you guys reference.PB8 -> SCL(with pull-high resistor 10Kohm), PB9 -> SDA(with pull-high resistor 10Kohm)void I2C1_Config(void){ GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStruct; /* I2C1 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /* GPIOB clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* Connect I2C1 pins to AF4 ************************************************/ GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1); //PB8 -> I2C1_SCL GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1); //PB9 -> I2C1_SDA /* Configure I2C1 GPIOs (Change PB8 and PB9 to AF mode**********************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; // GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure I2C1 ***********************************************************/ I2C_DeInit(I2C1); /* I2C DeInit */ /* Enable the I2C peripheral */ I2C_Cmd(I2C1, ENABLE); /* Set the I2C structure parameters */ I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStruct.I2C_OwnAddress1 = 0xFE; //Master address I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; //7 bit address mode I2C_InitStruct.I2C_ClockSpeed = 30000; // speed = 300K /* Initialize the I2C peripheral w/ selected parameters */ I2C_Init(I2C1, &I2C_InitStruct);}2012-04-03 10:05 AM
Hi ! I read your post about problem with I2C and I think I have the same. After sending adress by I2C, the ADDR flag in SR1 register is not set but at the same time AF Flag is setting up. I do not use std peripherial libs and program on stm32f407 chip. Could you please send me your code or state of your registers taken from debugger ? I would be very grateful.