Rockchip-linux驱动 --- IIC

文章目录

一、IIC子系统编程

(一)分配并初始化对象

c 复制代码
struct i2c_driver {
	//入口函数
	int (*probe)(struct i2c_client *client, const struct i2c_device_id *id);
	//出口函数
	void (*remove)(struct i2c_client *client);
	//关闭设备执行的函数
	void (*shutdown)(struct i2c_client *client);
	struct device_driver driver;
};

(二)IIC驱动框架

c 复制代码
//入口函数
int myiic_probe(struct i2c_client *client, const struct i2c_device_id *id){
	return 0;
}

//出口函数
void myiic_remove(struct i2c_client *client){}

static struct i2c_driver myiic_driver = {
	.probe = myiic_probe,
	.remove = myiic_remove,
	.shutdown = myiic_shutdown,
	.driver = {
		.name = "myiic",
		.of_match_table = myiic_dt_ids,
	},
};

module_i2c_driver(myiic_driver);
MODULE_LICENSE("GPL v2");

(三)IIC的regmap函数

1. 初始化IIC的寄存器映射

c 复制代码
#define devm_regmap_init_i2c(i2c, config) __regmap_lockdep_wrapper(__devm_regmap_init_i2c, #config, i2c, config)
功能:初始化一个专用于IIC设备的寄存器映射
参数:
	@i2c:指向 struct i2c_client 的指针,表示要与之交互的 I2C 设备
	@config:指向remap_config结构体
返回值:
	成功,返回一个指向regmap的指针
	失败,返回一个错误码指针
备注:可以直接使用regmap的API,而无需直接操作IIC接口
c 复制代码
寄存器映射配置信息结构体
struct regmap_config {
	int reg_bits;
	int val_bits;
	const struct regmap_access_table *volatile_table;
	enum regcache_type cache_type;
	...
}

结构体成员 --- volatile_table
指定哪些寄存器是易变的

结构体成员 --- cache_type
	enum regcache_type {
		REGCACHE_NONE,
		REGCACHE_RBTREE,
		REGCACHE_COMPRESSED,
		REGCACHE_FLAT,
	};

2. 写入寄存器

c 复制代码
int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
功能:将指定的值写入到硬件设备的寄存器
参数:
	@map:remap实例的指针
	@reg:要写入的寄存器的地址
	@val:要写入的寄存器的值
返回值:
	成功,返回0
	失败,返回错误码
备注:
	需要先初始化regmap实例

3.读取寄存器

c 复制代码
int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
功能:读取指定寄存器的值
参数:
	@map:指向regmap实例的指针
	@reg:要读的寄存器地址
	@val:指向变量的指针,存储从寄存器读取的值
返回值:
	成功,返回0
	失败,返回错误码

(二)打印调试信息

c 复制代码
dev_emerg(dev, fmt, ...) 
dev_crit(dev, fmt, ...)
dev_alert(dev, fmt, ...)
dev_err(dev, fmt, ...) 
dev_warn(dev, fmt, ...)
dev_notice(dev, fmt, ...) 
dev_info(dev, fmt, ...) 
功能:打印日志信息,日志级别从上到下优先级递减
参数:
	@dev:指向 struct device 的指针,用于指定日志消息关联的设备。
	@fmt:格式化字符串,遵循 printf 风格的格式。
	@...:可变参数列表,用于 fmt 字符串中的格式化。
相关推荐
一个平凡而乐于分享的小比特9 天前
TPA620 芯片功能详解
iic·adc·tpa620
一个平凡而乐于分享的小比特11 天前
SMBus(System Management Bus,系统管理总线)
iic·smbus
ttkwzyttk22 天前
Linux内核中模块定义宏机制解析
linux驱动
小渔村的拉线工1 个月前
20.IIC通信上拉电阻的计算
嵌入式硬件·iic·硬件知识·上拉电阻
BlueBirdssh1 个月前
时钟相位差
linux驱动
大熊背1 个月前
sensor IIC有误时,写入曝光参数异常出现的常见问题
iic·自动曝光
蓝天居士2 个月前
Linux设备驱动之gpio-keys(2)
linux驱动·gpio-keys
GUET_一路向前2 个月前
STM32_I2C Timing参数计算方法(I2C speed:120k/240k/400k)
stm32·单片机·嵌入式硬件·iic
喜喜安2 个月前
串口、IIC、SPI通信协议
uart·iic·spi
时光の尘2 个月前
嵌入式面试八股文(十九)·裸机开发与RTOS开发的区别
linux·stm32·单片机·iic·rtos·spi