2011-04-12 04:49 AM
Dear all,
My name is Gregory and I live in France (so excuse my poor english). I am a newbie in STM32 world, but I come from the Arduino world and have some good basis in C programing.
I try to use a DS1307 chip with my discovery board but I'm facing a problem for a few days now that I can't overcome.
Here below is the code of my main.c file, I try to read values in the RTC DS1307 chip, I sniff the I2C bus with a logic analyzer and I can clearly see the start command followed by the slave adresse + the direction bit and then a NACK, the chip is working (tested on my arduino board) and I can't understand why I get a NACK (I expect the slave to ACK and then send the bytes from the eeprom). Moreover, I never get the event EV6
I will be great if you can help me.
Code :
&sharpinclude ''stm32f10x.h''
&sharpdefine I2C_SPEED 100000 /*!< I2C Speed */
&sharpdefine I2C1_SLAVE_ADDRESS7 0b11010000
&sharpdefine I2C_TIMEOUT 100000
GPIO_InitTypeDef GPIO_InitStructureOutput; // Structure for Output Definitions
GPIO_InitTypeDef GPIO_InitStructureI2C; // Structure for I2C Definitions
I2C_InitTypeDef I2C_InitStructure; // Structure for I2
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval : None
*/
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
int TimeOut;
/* GPIOB Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* I2C1 and I2C2 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* Configure I2C1 pins: SCL and SDA ----------------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; // Open Drain, I2C bus pulled high externally
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Enable I2C1 -------------------------------------------------------------*/
I2C_DeInit(I2C1);
I2C_Cmd(I2C1, ENABLE);
/* I2C1 configuration ------------------------------------------------------*/
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x039;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;
I2C_Init(I2C1, &I2C_InitStructure);
/*----- Transmission Phase -----*/
/* Send I2C1 START condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on I2C1 EV5 and clear it */
TimeOut = I2C_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
{
TimeOut--;
if (TimeOut == 0) return 1;
}
I2C_SendData(I2C1, 0x00); // Send Year register address
/* Send DS1307 slave Address for write */
I2C_Send7bitAddress(I2C1, I2C1_SLAVE_ADDRESS7, I2C_Direction_Transmitter);
//Test on I2C1 EV6 and clear it
TimeOut = I2C_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
TimeOut--;
if (TimeOut == 0){
// Send I2C1 STOP Condition
I2C_GenerateSTOP(I2C1, ENABLE);
return 2;
}
}
return 1;
}
#i2c-ds1307