Question
Nucleo-F103RB and L3G4200D.
Posted on August 29, 2014 at 12:21
Hello
I have a Nucleo-F103RB board and a EVK of L3G4200D. I am try connect this via I2C I have this code for config I2Cvoid Inicializacion_uC(void)
{
RCC_Configuration_IO();
Inicializacion_GPIO();
Inicializacion_GPIO_UART1();
Inicializacion_GPIO_UART2();
Inicializacion_GPIO_UART3();
Inicializacion_GPIO_I2C1();
Configuration_Interrupcion_NVIC_UART1();
Configuration_Interrupcion_NVIC_UART2();
Configuration_Interrupcion_NVIC_UART3();
Inicializacion_UART1();
Inicializacion_UART2();
Inicializacion_UART3();
Inicializacion_I2C1();
}
void RCC_Configuration_IO(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* Enable DMA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* USART clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* I2C clock enable */
RCC_APB2PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); // creo que sobra
/* GPIOA clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
}
void Inicializacion_GPIO_I2C1(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
#ifdef REGION /* I2C1 GPIO Configuration
PB5 --> I2C1_SMBA (IN)
PB6 --> I2C1_SCL (OUT)
PB7 --> I2C1_SDA (IN/OUT)
*/
/*Configure GPIO pin : PB */
/*
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStruct);
*/
/*Configure GPIO pin : PB */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStruct);
#endif
}
void Inicializacion_I2C1(void)
{
I2C_InitTypeDef I2C_InitStructure;
/* I2C configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; //I2C_Mode_SMBusHost;
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 = I2CxSpeed;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
I have this code for config
void InicializaSensores(void)
{
/* Gyroscope initialization */
L3GInit L3GInitStructure;
L3GInitStructure.xPowerMode = L3G_NORMAL_SLEEP_MODE;
L3GInitStructure.xOutputDataRate = L3G_ODR_95_HZ_CUTOFF_12_5;
L3GInitStructure.xEnabledAxes = L3G_ALL_AXES_EN;
L3GInitStructure.xFullScale = L3G_FS_500_DPS;
L3GInitStructure.xDataUpdate = L3G_BLOCK_UPDATE;
L3GInitStructure.xEndianness = L3G_BIG_ENDIAN;
ConfigL3gx(&L3GInitStructure);
}
void ConfigL3gx(L3GInit *pxL3GInitStruct)
{
uint8_t CTRL1 = 0x00, CTRL4 = 0x00;
CTRL1 |= ((uint8_t)pxL3GInitStruct->xPowerMode | (uint8_t)pxL3GInitStruct->xOutputDataRate | (uint8_t)pxL3GInitStruct->xEnabledAxes);
CTRL4 |= ((uint8_t)pxL3GInitStruct->xFullScale | (uint8_t)pxL3GInitStruct->xDataUpdate | (uint8_t)pxL3GInitStruct->xEndianness);
//I2CWriteString(I2C1 , uint8_t cAddr, uint8_t* pcBuffer , uint8_t cWriteAddr , uint8_t cNumByteToWrite)
I2CWriteString(I2C1 ,Add_Gyr, &CTRL1, L3G_CTRL_REG1 , 1);
I2CWriteString(I2C1 ,Add_Gyr, &CTRL4, L3G_CTRL_REG4 , 1);
}
void I2CWriteString(I2C_TypeDef* I2Cx, uint8_t I2C_Addr, uint8_t* Data, uint8_t WriteAddr, uint8_t NumByteToWrite)
{
#ifdef FORCE_CRITICAL_SEC
__disable_irq();
#endif
I2C_GenerateSTART(I2Cx, ENABLE); // Enviamos condicion de Inicion / Envia START condition
// Esperamos a que el Bus este libre
while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Transmitter); // Enviamos direcion para escrivir
// Comoprobamos si es Maestro esta en modo Tx / check master is now in Tx mode
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2Cx, WriteAddr); // Enviamos direcion del registro a escrivir / Send the register address to write
// Esperamos a recivir ACK
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// enviamos los datos
for(uint8_t i=0 ; i < NumByteToWrite ; i++)
{
// Enviamos los dato
I2C_SendData(I2Cx, Data[i]);
//Esperamos el ACK
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
// Eviamos Stop al sistema
I2C_GenerateSTOP(I2Cx, ENABLE);
/*stop bit flag*/
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF));
#ifdef FORCE_CRITICAL_SEC
__enable_irq();
#endif
}
I have this code for configure the device
I don't know because this don't work.
The code stop in
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I have a oscilloscope and i measure the SCL and SDA line and this is always to 0V
Someone can help me?
Regards
#nucleo #f103rb #nucleo-f103rb #i2c #i2c