rt_err_t at91_i2c_init(void) { struct rt_i2c_bus_device *bus; bus = rt_malloc(sizeof(struct rt_i2c_bus_device)); if (bus == RT_NULL) { rt_kprintf("rt_malloc failed\n"); return -RT_ENOMEM; } rt_memset((void *)bus, 0, sizeof(struct rt_i2c_bus_device)); bus->priv = (void *)&bit_ops; at91_i2c_gpio_init(); rt_i2c_bit_add_bus(bus, "i2c0"); return RT_EOK; }
void rt_hw_i2c_init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_I2C_SCL | RCC_I2C_SDA , ENABLE ); GPIO_InitStructure.GPIO_Pin = PIN_I2C_SCL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(GPIO_PORT_I2C_SCL, &GPIO_InitStructure); GPIO_SetBits(GPIO_PORT_I2C_SCL, PIN_I2C_SCL); GPIO_InitStructure.GPIO_Pin = PIN_I2C_SDA; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(GPIO_PORT_I2C_SDA, &GPIO_InitStructure); GPIO_SetBits(GPIO_PORT_I2C_SDA, PIN_I2C_SDA); rt_memset((void *)&i2c_device, 0, sizeof(struct rt_i2c_bus_device)); i2c_device.priv = (void *)&bit_ops; rt_i2c_bit_add_bus(&i2c_device, "i2c1"); }
int rt_hw_i2c_init(void) { GPIO_InitTypeDef GPIO_InitStructure; #ifdef RT_USING_I2C_BITOPS RCC_AHB1PeriphClockCmd(RCC_I2C_SCL | RCC_I2C_SDA, ENABLE); /* config SCL PIN */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Pin = PIN_I2C_SCL; GPIO_SetBits(GPIO_PORT_I2C_SCL, PIN_I2C_SCL); GPIO_Init(GPIO_PORT_I2C_SCL, &GPIO_InitStructure); /* config SDA PIN */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Pin = PIN_I2C_SDA; GPIO_SetBits(GPIO_PORT_I2C_SDA, PIN_I2C_SDA); GPIO_Init(GPIO_PORT_I2C_SDA, &GPIO_InitStructure); rt_memset((void *)&i2c_device, 0, sizeof(struct rt_i2c_bus_device)); i2c_device.priv = (void *)&bit_ops; rt_i2c_bit_add_bus(&i2c_device, "i2c1"); #else I2C_InitTypeDef I2C_InitStructure; /*!< sEE_I2C Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /*!< sEE_I2C_SCL_GPIO_CLK and sEE_I2C_SDA_GPIO_CLK Periph clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /* Reset sEE_I2C IP */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); /* Release reset signal of sEE_I2C IP */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); /*!< GPIO configuration */ /*!< Configure sEE_I2C pins: SCL */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; 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_UP; GPIO_Init(GPIOB, &GPIO_InitStructure); /*!< Configure sEE_I2C pins: SDA */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Connect PXx to I2C_SCL*/ GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); /* Connect PXx to I2C_SDA*/ GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); /*!< I2C configuration */ /* sEE_I2C configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; // I2C_InitStructure.I2C_OwnAddress1 = SLA_ADDRESS; I2C_InitStructure.I2C_OwnAddress1 = 0x18; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 400000; /* Apply sEE_I2C configuration after enabling it */ I2C_Init(I2C1, &I2C_InitStructure); /* sEE_I2C Peripheral Enable */ I2C_Cmd(I2C1, ENABLE); rt_memset((void *)&stm32_i2c1, 0, sizeof(struct rt_i2c_bus_device)); stm32_i2c1.ops = &i2c1_ops; rt_i2c_bus_device_register(&stm32_i2c1, "i2c1"); #endif return 0; }