树莓派底层IO驱动的编写
发布日期:2021-05-10 01:09:09 浏览次数:21 分类:精选文章

本文共 3967 字,大约阅读时间需要 13 分钟。

��������� BCM2835 ���������������������������������������

BCM2835 ������������

������������������ BCM2835 ������������ARM Cortex-A53 ��������������� 32 ��������������������������������� MMU���������������������������������������������������������������������������������������������������������������

������������������������

BCM2835 ������������������������������Peripheral Unit������������������������ GPIO���UART���SPI ������������������������������������������������������������������������������������������������

GPIO ������

  • ���������������

    • GPFSELx ������������ GPIO ������������������������GPFSEL0���������������������/���������������

    • GPSETx ���������������������������GPSET0 ������ 1 ������������������������������

    • GPCLRx ���������������������������

���������������������

// ������������ 4 ���������
*GPFSEL0 &= ~((1 << 12) | (1 << 13) | (1 << 14));
*GPFSEL0 |= (1 << 12);

��������������������� 12-14 ���������������������������������

������������

���������������������

  • ���������������

    • ������ ioremap ���������������������������������������
    • ������������������������������������
  • ������������������

    • open ������������������������
    • write ������������������������������ IO ���������
  • ������������������������

    • ������ ounmap ���������������������
    • ������ unregister_chrdev ���������������
  • ������������������

    #include 
    #include
    #include
    #include
    #include
    static int major;
    static int minor;
    static char *module_name = "pin4_demo";
    static struct file_operations pin4_fops = {
    .owner = THIS_MODULE,
    .open = pin4_open,
    .write = pin4_write,
    .read = pin4_read,
    };
    static int pin4_init(void) {
    printk(KERN_INFO "pin4 module loaded\n");
    major = register_chrdev(0, module_name, &pin4_fops);
    if (major < 0) {
    printk(KERN_ERR "Failed to register pin4 driver\n");
    return -1;
    }
    // ������������������������������ 0xF0000000��������� 32 ���������
    void *iomem_ptr = ioremap(0xF0000000, 32);
    GPFSEL0 = (volatile unsigned int *)iomem_ptr;
    GPSET0 = (volatile unsigned int *)iomem_ptr + offsetof(struct GPIO, GPSET0);
    GPCLR0 = (volatile unsigned int *)iomem_ptr + offsetof(struct GPIO, GPCLR0);
    return 0;
    }
    void pin4_exit(void) {
    iounmap(GPFSEL0);
    unregister_chrdev(major, module_name);
    printk(KERN_INFO "pin4 module unloaded\n");
    }
    device_t devno;
    static int __init pin4_drv_init(void) {
    int ret;
    ret = pin4_init();
    if (ret) {
    ret = -1;
    }
    devno = MKDEV.major << 16 | major << 8 | minor;
    return ret;
    }
    module_init(pin4_drv_init);
    module_exit(pin4_exit);

    ������������

    ������������������

    #include 
    #include
    #include
    int main() {
    int fd;
    int cmd;
    fd = open("/dev/pin4", O_RDWR);
    if (fd < 0) {
    perror("Failed to open pin4 device");
    return 1;
    }
    printf("Enter 1 to set high, 0 to set low: ");
    scanf("%d", &cmd);
    if (cmd == 1) {
    int data = 1;
    if (write(fd, &data, sizeof(int)) != sizeof(int)) {
    perror("Failed to write to pin4");
    return -1;
    }
    } else if (cmd == 0) {
    int data = 0;
    if (write(fd, &data, sizeof(int)) != sizeof(int)) {
    perror("Failed to write to pin4");
    return -1;
    }
    }
    printf("Successfully wrote %d to pin4\n", cmd);
    return 0;
    }

    ������������

    • ������������

      ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make -j4 modules
    • ���������������������

      insmod pin4_demo.ko
      chmod 666 /dev/pin4
      ./app
    • ���������������

      ������ Kernel ���������������������GPIO������������������

      vitamins ):
      unsigned long __physical_start = 0x80000000;
      unsigned long __virtual_start = 0xC0000000;
      unsigned long __page_size = 4096;
      void *physical =ioremap(__physical_start, 4096);
      occupy(0, 0, 4096);

    ��������������������������������������������������������� BCM2835 ���������������������������������������

    上一篇:设计模式——简易工厂模式
    下一篇:地址相关的概念

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2025年04月06日 09时16分58秒