2011-08-17 02:40 AM
Dear collegue,
I have lunched the example project about I2C simple communication with interrupt that we can find in the UM0427 (user manual about firmware lib v2.0.3).
The simple application is based on the main showed below.
IWhen the program execute I2C1 Start Condition, a Bus Error occour and the cpu go in the BusFault_Handler.
Why there is this bus error?...How can I do to delete this error?
int main(void)
{
&sharpifdef DEBUG
debug();
&sharpendif
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* NVIC configuration ------------------------------------------------------*/
NVIC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
/* Enable I2C1 and I2C2 ----------------------------------------------------*/
I2C_Cmd(I2C1, ENABLE);
I2C_Cmd(I2C2, ENABLE);
/* I2C1 configuration ------------------------------------------------------*/
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
I2C_Init(I2C1, &I2C_InitStructure);
/* I2C2 configuration ------------------------------------------------------*/
I2C_InitStructure.I2C_OwnAddress1 = I2C2_SLAVE_ADDRESS7;
I2C_Init(I2C2, &I2C_InitStructure);
I2C_CalculatePEC(I2C1, ENABLE);
I2C_CalculatePEC(I2C2, ENABLE);
/* Enable I2C1 event and buffer interrupts */
I2C_ITConfig(I2C1, I2C_IT_EVT | I2C_IT_BUF, ENABLE);
/* Enable I2C1 event and buffer interrupts */
I2C_ITConfig(I2C2, I2C_IT_EVT | I2C_IT_BUF, ENABLE);
/*----- Transmission Phase -------------------------------------------------*/
/* Set data direction to transmitter */
Direction = Transmitter;
/* Send I2C1 START condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Wait until all data and the PEC value are received */
/* I2C2_Buffer_Rx buffer will contain the data plus the PEC value */
while(Rx2_Idx < Tx1BufferSize)
{
}
/* Check the corectness of the I2C1 transmitted data */
TransferStatus1 = Buffercmp(I2C1_Buffer_Tx, I2C2_Buffer_Rx, Tx1BufferSize);
/* TransferStatus1 = PASSED, if the transmitted and received data
are equal */
/* TransferStatus1 = FAILED, if the transmitted and received data
are different */
/*----- Reception Phase --------------------------------------------------*/
/* Re-configure and enable I2C1 event interrupt to have the higher priority */
NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Wait until end of Slave transmission */
while(Rx1_Idx < Tx2BufferSize)
{
}
/* Check the corectness of the I2C1 received data */
TransferStatus2 = Buffercmp(I2C2_Buffer_Tx, I2C1_Buffer_Rx, Tx2BufferSize);
/* TransferStatus2 = PASSED, if the transmitted and received data
are equal */
/* TransferStatus2 = FAILED, if the transmitted and received data
are different */
while(1)
{
}
}
#i2c-lib-v2.0.3-stm32f103-bus-err2011-08-17 05:08 AM
You could add some debugging output to figure out where.
You could create a hard fault handler to identify the faulting location/instructions. At a quick glance, you probably want to make sure you set the NVIC up for both interrupts you are using, before you enable them, and you also need to check what your interrupt routines are doing, and how they are clearing the interrupts/events.