uboot i2c 操作函数记录

时间:2024-04-14 23:10:27
  • I2C 在 u-boot 上面,有直接操作 I2C 的函数

    // drivers/i2c/i2c_core.c
    
    // 设置在哪个 I2C bus 上工作
    276 int i2c_set_bus_num(unsigned int bus)
    277 {
    278 int max;
    279
    280 if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
    281 return 0;
    282
    283 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
    284 if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
    285 return -1;
    286 #endif
    287
    288 max = ll_entry_count(struct i2c_adapter, i2c);
    289 if (I2C_ADAPTER(bus) >= max) {
    290 printf("Error, wrong i2c adapter %d max %d possible\n",
    291 I2C_ADAPTER(bus), max);
    292 return -2;
    293 }
    294
    295 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
    296 i2c_mux_disconnect_all();
    297 #endif
    298
    299 gd->cur_i2c_bus = bus;
    300 if (I2C_ADAP->init_done == 0)
    301 i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
    302
    303 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
    304 i2c_mux_set_all();
    305 #endif
    306 return 0;
    307 }
    //etc i2c_set_bus_num(1); // i2c slave 匹配
    313 int i2c_probe(uint8_t chip)
    314 {
    315 return I2C_ADAP->probe(I2C_ADAP, chip);
    316 }
    // etc i2c_probe(0x68)
    // 返回是否成功, 0 表示成功 , 非 0 表示失败 // i2c 读写 写
    330 int i2c_read(uint8_t chip, unsigned int addr, int alen,
    331 uint8_t *buffer, int len)
    332 {
    333 return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
    334 }
    335
    336 int i2c_write(uint8_t chip, unsigned int addr, int alen,
    337 uint8_t *buffer, int len)
    338 {
    339 return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
    340 } //etc
    uchar wri_data = 0x3f, red_data;
    90 if (i2c_write(0x68, 1, 1, (uchar *)&wri_data, sizeof(int)))
    91 puts("write error\n");
    92
    93 if (i2c_read(0x68, 1, 1, (uchar *)&red_data,
    94 sizeof(int))) {
    95 puts("Could not read the MCU; something fundamentally"
    96 " wrong on the I2C bus.\n");
    97 return -EIO;
    98 }