Linux驱动篇之hello

一)环境

a)ubuntu18.04

b)arm-linux-gnueabi-gcc 7.5.0 / gcc 7.5.0

c)FriendlyArm tiny4412 标准版

d)linux-5.5

二)目标

a)hello驱动

b)hello应用

三)步骤

a)hello驱动

1)在drivers目录下新建文件夹tiny4412.

2)在tiny4412目录下新建文件hello.c,并做如下编码:

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of.h>

static int hello_probe(struct platform_device *pdev)
{
  pr_info("enter %s\n", __func__);
  return 0;
}

static int hello_remove(struct platform_device *pdev)
{
  pr_info("enter %s\n", __func__);
  return 0;
}

static void hello_shutdown(struct platform_device *pdev)
{
  pr_info("enter %s\n", __func__);
  return ;
}

static int hello_suspend(struct platform_device *pdev, pm_message_t state)
{
  pr_info("enter %s\n", __func__);
  return 0;
}

static int hello_resume(struct platform_device *pdev)
{
  pr_info("enter %s\n", __func__);
  return 0;
}

static const struct of_device_id tiny4412_hello_dt_match[] = {
  { .compatible = "tiny4412, hello" },
  {},
};

static struct platform_driver tiny4412_hello_driver = {
  .probe          = hello_probe,
  .remove         = hello_remove,
  .shutdown   = hello_shutdown,
  .suspend    = hello_suspend,
  .resume     = hello_resume,
  .driver         = {
    .name   = "hello",
    .of_match_table = tiny4412_hello_dt_match,
  },
};

module_platform_driver(tiny4412_hello_driver);

MODULE_LICENSE("GPL v2");

3)在tiny4412目录下新建文件Makefile,并做如下编码:

obj-$(CONFIG_TINY4412_HELLO)    += hello.o

4)在tiny4412目录下新建文件Kconfig,并做如下编码:

menuconfig TINY4412_DRIVER
  bool "TINY4412 DRIVER Support"
  help
    Select [Y] to enable tiny4412 driver.

if TINY4412_DRIVER
  config TINY4412_HELLO
    tristate "HELLO Support"
    help
      This option enables hello_world in /tiny4412/hello.
      You'll need this to open hello driver.
      It can be built as a module.

endif # TINY4412_DRIVER

4)修改drivers/Makefile,并添加如下编码:

obj-$(CONFIG_TINY4412_DRIVER)  += tiny4412/

5)修改drivers/Kconfig,并添加如下编码:

source "drivers/tiny4412/Kconfig"

6)make menuconfig

6-1)选[Device Drivers]->[TINY4412 DRIVER Support]->[HELLO Support]

7)在exynos4412-tiny4412.dts中,添加如下编码:

hello {
    compatible = "tiny4412, hello";
    status = "okay";
  };

8)make

9)make dtbs

 

b)hello应用

1)新增文件hello.c,做如下编码:

#include <stdio.h>

int main()
{
  printf("hello, linux APP!\n");
  return 0;
}

2)arm-linux-gnueabi-gcc -o hello hello.c, then cp hello into rootfs/usr/bin

3)use arm-linux-gnueabi-readelf to check *.so

4)make libc.so.6 and ld-linux.so.3 in rootfs/lib , then make ramdisk.img

 

c)测试

1)使用上面生成的uImage, dtb, ramdisk.img, 从SD卡启动TINY4412.

2)在TINY4412中运行/usr/bin/hello,即可看到打印"hello, linux APP!".

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值