2012-04-13 10:09 AM
ATTENTION! The following code do not work for STM32 I2C while looking good:
void I2C_Ini(void){ I2C_InitTypeDef I2C_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; // I2C RCC RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE); // I2C GPIO RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_PinRemapConfig(GPIO_Remap_I2C1,ENABLE);// SCL=B8,SDA=B9 PININI(SCL,GPIO_Mode_AF_OD,GPIO_Speed_50MHz);PINLOCK(SCL); //
macros w/o errors
PININI(SDA,GPIO_Mode_AF_OD,GPIO_Speed_50MHz);PINLOCK(SDA);// macros w/o errors // I2C config I2C_InitStructure.I2C_ClockSpeed = 100000; I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0xAA; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init(I2C1,&I2C_InitStructure); I2C_Cmd(I2C1,ENABLE); // I2C NVIC NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn; NVIC_Init(&NVIC_InitStructure); I2C_ITConfig(I2C1,I2C_IT_EVT,ENABLE); // enable event IRQ I2C_ITConfig(I2C1,I2C_IT_ERR,ENABLE); // enable error IRQ I2C_ITConfig(I2C1,I2C_IT_BUF,ENABLE); // enable buffer IRQ } As a result of this init the BUSY bit is set and I2C can't work! The ref manual don't provide any info about this. #stm32f100c8 #i2c2012-04-13 10:39 AM
I think it's nuanced, All examples use I2C_Cmd(I2Cx,ENABLE); prior to I2C_Init()
2012-04-13 3:16 PM
It's may be not good idea to enable peripheral which is't properly init.
In my code above if we enable I2C1 clocks AFTER GPIO init - all will be working.. I think, it's strange behaviour for I2C.2012-04-14 2:33 AM
Hi,
I am having problem with I2C on STM32F100C8 . I'm not sure if its my code or the I2C peripheral giving the problem.After initialization the code is getting stuck in test on EV6 where I am getting i2c last event as 0x30400(I2C->SR1=0x400 and I2C->SR2=3 and the event I'm expecting is 0x70082(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED). On the oscilloscope I see that SCL and SDA are high initially and go low after start condition and then SDA goes high and remains high while SCL continues to remain low when I send the device address. Could someone help me to debug this better,Thanks Here is my code snippet,void
MMA8453Q_I2C_InitBusPins(
void
)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable I2C and GPIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE);
/* Configure I2C pins: SCL and SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(ACC_I2C_Port, &GPIO_InitStructure);
/* 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 = 100000;
/* I2C Peripheral Enable */
I2C_Cmd(I2C1, ENABLE);
/* Apply I2C configuration after enabling it */
I2C_Init(I2C1, &I2C_InitStructure);
}
void
MMA8453Q_I2C_BufferRead(u8 slAddr, u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{
/* While 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 MMA8453Q_Magn address for write */
I2C_Send7bitAddress(I2C1, ACC_I2C_ADDRESS_WRITE, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while
(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//code stuck here
}
unsigned
char
MMA8453Q_I2C_Init(
void
){
unsigned
char
i;
MMA8453Q_I2C_InitBusPins();
MMA8453Q_I2C_BufferRead(ACC_I2C_ADDRESS,&i,ACC_WHO_AM_I,1);
if
(i != 0x1a)
return
(0);
return
(1);
}
2012-04-17 12:56 AM
Hi Again,
Any views please ?Thanks2012-04-17 8:22 AM
I am having problem with I2C on STM32F100C8 .
I'm not sure if its my code or the I2C peripheral giving the problem.After initialization the code is getting stuck in test on EV6
read the errata
2012-04-17 11:06 PM
Hi,
Thanks for the info,I found there were some problems with the hardware connection on my side.I'm using bit banging for I2C for now ,its working fine.We’re moving the ST Community to a new platform to give you a better and more reliable community experience.