我正在研究 LPC3131(来自 NXP 的 arm9),我正在尝试在 linux 内核中制作我的第一个驱动程序,但我遇到了一些麻烦。
事实上,我想读/写微控制器寄存器中的值,就像我在那里发现的那样
我在驱动程序的初始化函数中做了以下事情
static int parr_init(void)
{
int result;
dev_t my_dev;
my_cdev = cdev_alloc();
cdev_init(my_cdev, &parr_port_fops);
printk(KERN_ALERT "parr_driver : Hello world \n");
result = alloc_chrdev_region(&my_dev, 0, 0, "parr_driver");
if(result < 0)
{
printk(KERN_WARNING "parr_driver : can't get a major number \n");
}
else
{
printk(KERN_ALERT "parr_driver : major number : %i \n", MAJOR(my_dev));
printk(KERN_ALERT "parr_driver : minor number : %i \n", MINOR(my_dev));
}
result = cdev_add(my_cdev, my_dev, 1);
if(result < 0)
{
printk(KERN_ALERT "parr_driver : failed to register driver \n");
}
else
{
printk(KERN_ALERT "parr_driver : driver is now ready to be use \n");
}
uint32_t volatile* reg = (uint32_t volatile *) 0x13009000;
printk("reg adress %p \n", reg);
printk("reg value %X \n", *reg);
return 0;
}
第一个 printk 工作正常,它打印我的收银机地址。但是第二个没有给出预期的结果,我遇到了分段错误。您可以在下面找到我的控制台日志:
/ # insmod home/parr_driver.ko
parr_driver : Hello world
parr_driver : major number : 254
parr_driver : minor number : 0
parr_driver : driver is now ready to be use
reg adress 13009000
Unable to handle kernel paging request at virtual address 13009000
pgd = c1798000
[13009000] *pgd=00000000
Internal error: Oops: 5 [#1]
last sysfs file: /sys/devices/platform/pnx-i2c.1/i2c-1/i2c-dev/i2c-1/dev
Modules linked in: parr_driver(+)
CPU: 0 Not tainted (2.6.33.7 #125)
PC is at parr_init+0xa4/0xec [parr_driver]
LR is at parr_init+0xa4/0xec [parr_driver]
pc : [<bf0000f0>] lr : [<bf0000f0>] psr: 60000013
sp : c1763f48 ip : c075ffe4 fp : 00000002
r10: bf00004c r9 : 00000000 r8 : c0659f68
r7 : 00131fd8 r6 : c0827fcc r5 : bf00033c r4 : 13009000
r3 : 60000013 r2 : c081d184 r1 : 000044c0 r0 : 00000018
Flags: nZCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user
Control: 0005317f Table: 31798000 DAC: 00000015
Process insmod (pid: 260, stack limit = 0xc1762270)
Stack: (0xc1763f48 to 0xc1764000)
3f40: 00000000 0fe00000 00000000 c0659350 00000000 00000000
3f60: 00000e19 bf00033c 00000000 bf00033c 00000000 00131fd8 c0659f68 c1762000
3f80: 00000000 c069581c c1c1aec0 c1768ec0 00000003 00000000 befc0e78 00000000
3fa0: 00000080 c0659dc0 00000000 befc0e78 00131fd8 00000e19 00102520 00000000
3fc0: 00000000 befc0e78 00000000 00000080 befc0e78 00102520 00000000 00000002
3fe0: 00000069 befc07e4 00015608 00009434 60000010 00131fd8 30880031 30880431
[<bf0000f0>] (parr_init+0xa4/0xec [parr_driver]) from [<c0659350>] (do_one_initcall+0x58/0x1a4)
[<c0659350>] (do_one_initcall+0x58/0x1a4) from [<c069581c>] (sys_init_module+0xc0/0x1f0)
[<c069581c>] (sys_init_module+0xc0/0x1f0) from [<c0659dc0>] (ret_fast_syscall+0x0/0x28)
Code: eb59cf48 e1a01004 e59f0040 eb59cf45 (e5941000)
---[ end trace e5bb78b790365d97 ]---
Segmentation fault
我的代码有什么问题?
正如下面评论中的建议,我尝试使用 ioremap 而不是直接读取。
这是我的新代码
if(request_mem_region(0x13009000, 4, "parr_driver") != NULL)
{
map = ioremap(0x13009000, 4);
if(map != NULL)
{
printk("reg value %X \n", ioread32(map));
iowrite32(0x0000FFFF, map);
printk("reg value %X \n", ioread32(map));
}
else
{
printk("ioremap FAIL \n");
}
}
控制台日志
parr_driver : minor number : 0
parr_driver : driver is now ready to be use
reg value DFB
reg value DFB
它现在不再崩溃,读取似乎工作,但我仍然无法写入我的硬件寄存器(第二次 DFB 而不是 FFFF)
任何的想法 ?