Kendryte K210 在freertos上的i2c的使用

首先修改project_cfg.h文件,将硬件定义好的i2c引脚定义好,其中13和14表示IO13和IO14,如下:

const fpioa_cfg_t g_fpioa_cfg =
{
    .version = PIN_CFG_VERSION,
    .functions_count = 2,
    .functions =
    {
        {14, FUNC_I2C0_SCLK},
        {13, FUNC_I2C0_SDA},

    }
};

然后在主函数里的如下:

//打开i2c0
handle_t i2c = io_open ("/dev/i2c0");

//获取设备地址
handle_t dev0 = i2c_get_device (i2c , 0x21 , 7);

//设置频率
i2c_dev_set_clock_rate (dev0 , 200000);


uint8_t reg = 0x26;
uint8_t data_buf [2] = { 0x26 ,0x02 };
data_buf [0] = reg;

//寄存器0x26写入值为0x02
io_write (dev0 , data_buf , 2);

//从0x26中读出一个字节并保存到data_buf里
i2c_dev_transfer_sequential (dev0 , &reg , 1, data_buf , 1);

//对比两个值是否相同
printf("%x %x\r\n",data_buf[0],data_buf[1]);

猜你喜欢

转载自blog.csdn.net/smile_5me/article/details/106790018