gpt4 book ai didi

c - 使用 writel 将 4 位写入 ioremap 内存地址

转载 作者:行者123 更新时间:2023-12-02 09:20:19 24 4
gpt4 key购买 nike

我是内核编程新手,现在尝试将一些值写入设备驱动程序中的 32 位 GPIO 寄存器。 I/O 为ioremap() -ed 到内存地址。问题是,我不知道怎么办writel()/writeb()/writew()将位写入地址。

供应商文件显示寄存器位于 0xE5200000 。我必须写入的位是 [0:3]位并将剩余 28 位([4:31] 位)保留为零。

这是我迄今为止编写的设备驱动程序中的代码的一部分:

#define TCON_ADDR 0xE250000 // The address as provided by the vendor
static void *TIMER_CON_ADDR;
// I have to map and write to the address when the device is opened
static int on_dev_open(struct inode *inode, struct file *file) {
unsigned int data;
TIMER_CON_ADDR = ioremap(TCON_ADDR, 4); // Map the [0:4] bits to TIMER_CON_ADDR
data = 4; // 0100 in binary
writel(data, TIMER_CON_ADDR); // Write 0100 to TIMER_CON_ADDR
return 0;
}

上面的代码对你们来说可能完全是胡言乱语,但我不熟悉 write(l|w|b)ioremap() .

所以我的问题是:

  1. 我是否映射了 [0:4]位到 TIMER_CON_ADDR 是否正确?
  2. 如果没有,我该如何正确映射它们?
  3. 正确映射 4 位后,如何使用 write(1|w|b) 中的任何一个函数以正确的顺序将位 ( 0100 ) 写入 TIMER_CON_ADDR?
  4. write(l|w|b) 是什么意思?在幕后写位吗?
  5. 是否有任何我遗漏/错误的信息?

感谢您提前提供的所有帮助。

最佳答案

  1. Did I map the [0:4] bits to TIMER_CON_ADDR correctly?

不,你写32位,writel写4个字节,4 * 8 = 32位

  1. If not, how do I map them correctly?

无法映射 4 位,最小 8 位 = 1 个字节,但如果您使用 32 位注册您需要映射 32 位 = 4 字节。也不要忘记检查和处理错误。

  1. After I have correctly mapped the 4 bits, how do I use any of the write(1|w|b) functions to write bits (0100) to TIMER_CON_ADDR in the correct order?

您需要使用 readl,内核中充满了示例,只需在 Linux 内核源代码树的 drivers 子目录中运行 grep 即可。读/写总体思路:

u32 reg = readl(TIMER_CON_ADDR);
reg &= ~0xfu;
reg |= 4;
writel(reg, TIMER_CON_ADDR);
  1. What does write(l|w|b) do under the hood to write bits?

看源代码,它只是简单的C函数,例如:

static inline void __raw_writel(u32 value, volatile void __iomem *addr)
{
*(volatile u32 __force *)addr = value;
}

主要思想是告诉编译器它不应该删除你的内存读/写

  1. Is there any information I've missed / got wrong?

看过类似驱动的源码,已经包含了差不多了这些简单驱动程序的所有解决方案。

关于c - 使用 writel 将 4 位写入 ioremap 内存地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43164988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com