#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/i2c.h>
unsigned char mac_read(unsigned char command)
{
int ret;
unsigned char buf;
struct i2c_adapter *a = i2c_get_adapter(1);
struct i2c_msg msg[2] =
{
{
.addr = 0x50, .flags = 0, .buf = &command, .len = 1
},
{
.addr = 0x50, .flags = I2C_M_RD, .buf = &buf, .len = 1
}
};
ret = i2c_transfer(a, msg, 2);
if (ret < 0) {
printk("[mac_write] i2c_transfer error\n");
}
return buf;
}
EXPORT_SYMBOL(mac_read);
int mac_write(unsigned char reg, unsigned char data)
{
int ret;
unsigned char buf [] = { reg, data };
struct i2c_adapter *a = i2c_get_adapter(1);
struct i2c_msg msg[1] =
{
{
.addr = 0x50, .flags = 0, .buf = buf, .len = 2
}
};
ret = i2c_transfer(a, msg, 1);
if (ret < 0) {
printk("[mac_write] i2c_transfer error\n");
}
return ret;
}
EXPORT_SYMBOL(mac_write);
写这个程序之前我在思考是网卡先驱动获取mac先呢,还是i2c子系统的i2c适配器先初始化好呢?
加了些打印语句判断之后,证明我的板子是i2c子系统的i2c适配器先初始化好,于是可以堂而皇之的用i2c接口的api
如果不是就得用__raw_writew,__raw_readw这些底层寄存器操作函数,像单片机一样来读取mac地址,
不然网卡获取不到固定mac,ip在dhcp获取中就会变动,
写入不着急可以等到i2c子系统初始化好再说
先修改makefile添加该c文件的依赖,让它编译进内核
(这里我调试板子使用nfs的,内核存放于sd卡中,正常逻辑我每次改内核就得把sd拿出来拷贝一次,相当的麻烦
我把sd卡挂载在/media/card中,内核编译后uImage自动复制一份到nfs目录中,超级终端中利用cp命令将unfs目录中的Image复制到挂载的/media/ca