Linux i2c 经典驱动模型 和必要的注释 (rtc驱动为例)

本文详细介绍了Linux环境下RTC(实时时钟)的i2c驱动模型,包括软复位、设置日期时间、读取日期时间、设置闹钟等功能的实现代码,涉及i2c通信、设备注册和操作等关键步骤。
摘要由CSDN通过智能技术生成

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/time.h>
#include <linux/types.h>
#include <linux/seq_file.h>
#include <linux/list.h>
#include <linux/proc_fs.h> 
#include <linux/delay.h>

#include <mach/mt_gpio.h>
#include <mach/mt_reg_base.h>
#include <mach/mt_typedefs.h>

#include <mach/board.h>
#include <mach/irqs.h>
#include <mach/eint.h>

#include <asm/uaccess.h>

#include <asm/io.h>
#include <linux/platform_device.h>
#include <linux/autoconf.h>
#include <linux/kobject.h>
#include <linux/earlysuspend.h>

#include <mach/upmu_common.h>
#include <linux/regulator/consumer.h>

#include <linux/types.h>
#include <mach/mt_typedefs.h>
#include <mach/mt_pm_ldo.h>


#include "rtc.h"
#include "cust_gpio_usage.h"

//定义设备名
#define RTC_NAME "rtc1"

//日期数据的长度

#define D_RTC_DATETIME_LENGTH 0x07

//闹钟数据的长度
#define D_RTC_DATETIME_ALARM_LENGTH 0x05

//i2c address 是A2>>1  0x51
#define D_RTC_DEVICE_ADDR 0x51 //0xA2
#define D_RTC_REGISTER_LENGTH 0x12

//RTC 寄存器宏定义

#define RTC_REG_CTRL_1 0x00
#define RTC_REG_CTRL_2 0x01 
#define RTC_REG_OFFSET 0x02
#define RTC_REG_RAM_BYTE 0x03
#define RTC_REG_SECONDS 0x04
#define RTC_REG_MINUTES 0x05
#define RTC_REG_HOURS 0x06
#define RTC_REG_DAYS 0x07
#define RTC_REG_WEEKDAYS 0x08
#define RTC_REG_MONTHS 0x09
#define RTC_REG_YEARS 0x0a
#define RTC_REG_SECOND_ALARM 0x0b
#define RTC_REG_MINUTE_ALARM 0x0c
#define RTC_REG_HOUR_ALARM 0x0d
#define RTC_REG_DAY_ALARM 0x0e
#define RTC_REG_WEEKDAYS_ALARM 0x0f
#define RTC_REG_TIMER_VALUE 0x10
#define RTC_REG_TIMER_MODE 0x11

 

static struct i2c_client *rtc_client = NULL;

// 16进制转成BCD码(4个二机制位表示一位10进制)
static void HexToBcd(uint8_t *cHex, uint8_t length);

//BCD码转成16进制码
static void BcdToHex(uint8_t *cBcd, uint8_t length);

 

//rtc 向寄存器reg中写writedata[]数据

//I2c 设备写函数

static int RTC_WriteByte(u8 reg, u8 *writedata)

// msleep(5);
u8 databuf[2] = {0};
int ret = 0;
databuf[0] = reg;
databuf[1] = *writedata;
// 向I2C设备发送数据
ret = i2c_master_send(rtc_client, databuf, 0x2);
if(ret < 0)
{
printk("rtc : rtc_i2c_write send data failed !\n");
return -1;
}

return ret;
}

//读寄存器reg的数据,保存到readdata[]

//12c 设备读函数,譬如要读取设备寄存器A里面的值,得先由主机向设备写A地址,然后从A中读取A里面的值,故有写读操作

static int RTC_ReadByte(u8 reg, u8 *readdata)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值