【HarmonyOS移植】必备基础知识之002

举报
Yuchuan 发表于 2022/01/14 21:54:53 2022/01/14
【摘要】 移植内核对技术的要求比较全面、比较细致。

1. 基础知识

移植内核对技术的要求比较全面、比较细致。

1.1 单片机相关的知识

  • 栈的作用

  • 加载地址、链接地址

  • 重定位

  • 几个简单的硬件知识

    • 串口
    • 定时器
  • 中断的概念

1.2 Linux操作相关的知识

  • Linux常用命令
  • 简单的脚本:脚本就是把命令写在一个文件里
  • GCC编译命令
  • Kconfig和Makefile

1.3 芯片相关知识

  • 能阅读芯片手册(英文)

    • 移植最小系统时,涉及的手册内容不多
  • 能看懂硬件原理图

    • 移植最小系统时,涉及的原理图内容不多

2. 驱动程序知识

对于只有单片机知识的人来说,怎么去操作硬件?

  • 直接读写寄存器
  • 使用库函数

在RTOS中,本质也是去读写寄存器,但是需要有统一的驱动程序框架。
所以:RTOS驱动 = 驱动框架 + 硬件操作

2.1 以点灯为例

2.1.1 硬件原理

2.1.2 单片机点灯

  • 方法1:直接读写寄存器

  • 方法2:使用厂家的HAL库

2.1.5 Liteos-a/Linux怎么点灯

使用MMU时,一般APP与内核是相互隔离的。APP通过标准的open/read/write等文件操作函数去调用驱动程序。
如下图所示:

为何要多此一举?

  • 它们支持MMU(内存管理单元)
  • 用户程序跟内核是分隔开的,用户程序不能直接读写寄存器
  • 用户程序通过标准接口访问驱动程序
  • 基于这些内核的软件一般都比单片机软件复杂,术业有专攻
  • 不应该让写APP的人去看原理图、写驱动、写寄存器
  • 软件和硬件隔离,硬件再怎么变化,只需要改驱动,APP不需要改

2.2 怎么编写驱动程序

2.2.1 驱动程序的核心

Linux和Liteos-a的驱动程序时类似的,Liteos-a的更加精简。
既然APP使用驱动是调用open/read/write等接口,
那么写驱动程序是最简单的方法就是提供对应的drv_open/drv_read/drv_write等函数。
这些函数放在一个结构体里:Linux对应file_operations结构体,Liteos-a对应file_operations_vfs结构体。

1. Linux

Linux中是定义一个file_operations结构体,如下:

struct file_operations {
	struct module *owner;
	loff_t (*llseek) (struct file *, loff_t, int);
	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
	ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	int (*readdir) (struct file *, void *, filldir_t);
	unsigned int (*poll) (struct file *, struct poll_table_struct *);
	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
	long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
	int (*mmap) (struct file *, struct vm_area_struct *);
	int (*open) (struct inode *, struct file *);
	int (*flush) (struct file *, fl_owner_t id);
	int (*release) (struct inode *, struct file *);
	int (*fsync) (struct file *, loff_t, loff_t, int datasync);
	int (*aio_fsync) (struct kiocb *, int datasync);
	int (*fasync) (int, struct file *, int);
	int (*lock) (struct file *, int, struct file_lock *);
	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
	int (*check_flags)(int);
	int (*flock) (struct file *, int, struct file_lock *);
	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
	ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
	int (*setlease)(struct file *, long, struct file_lock **);
	long (*fallocate)(struct file *file, int mode, loff_t offset,
			  loff_t len);
	int (*show_fdinfo)(struct seq_file *m, struct file *f);
};

2. Liteos-a

Liteos-a中定义了

Liteos-a中定义了一个file_operations_vfs结构体,如下:

struct file_operations_vfs
{
  /* The device driver open method differs from the mountpoint open method */

  int     (*open)(FAR struct file *filep);

  /* The following methods must be identical in signature and position because
   * the struct file_operations and struct mountp_operations are treated like
   * unions.
   */

  int     (*close)(FAR struct file *filep);
  ssize_t (*read)(FAR struct file *filep, FAR char *buffer, size_t buflen);
  ssize_t (*write)(FAR struct file *filep, FAR const char *buffer, size_t buflen);
  off_t   (*seek)(FAR struct file *filep, off_t offset, int whence);
  int     (*ioctl)(FAR struct file *filep, int cmd, unsigned long arg);
  int     (*mmap)(FAR struct file* filep, struct VmMapRegion *region);
  /* The two structures need not be common after this point */

#ifndef CONFIG_DISABLE_POLL
  int     (*poll)(FAR struct file *filep, poll_table *fds);
#endif
  int     (*unlink)(FAR struct inode *inode);
};

2.3 注册驱动程序

1. Linux

static struct file_operations hello_drv = {
	.owner	 = THIS_MODULE,
	.open    = hello_drv_open,
	.read    = hello_drv_read,
	.write   = hello_drv_write,
	.release = hello_drv_close,
};

int major = register_chrdev(0, "hello", &hello_drv);  /* /dev/hello */
static struct class *hello_class = class_create(THIS_MODULE, "hello_class");
device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */

2. Liteos-a

static const struct file_operations_vfs g_helloDevOps = {
    .open   = hello_open,
    .close  = hello_close,
    .read   = hello_read,
    .write  = NULL,
    .seek   = NULL,
    .ioctl  = NULL,
    .mmap   = NULL,
    .unlink = NULL,
};

int ret = register_driver("/dev/hello", &g_helloDevOps, 0666, NULL);

2.4 APP如何使用

Linux和Liteos-a在APP层面都一样:

int main(int argc, char **argv)
{
	int fd;
	char buf[1024];
	int len;
	
	/* 1. 判断参数 */
	if (argc < 2) 
	{
		printf("Usage: %s -w <string>\n", argv[0]);
		printf("       %s -r\n", argv[0]);
		return -1;
	}

	/* 2. 打开文件 */
	fd = open("/dev/hello", O_RDWR);
	if (fd == -1)
	{
		printf("can not open file /dev/hello\n");
		return -1;
	}

	/* 3. 写文件或读文件 */
	if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
	{
		len = strlen(argv[2]) + 1;
		len = len < 1024 ? len : 1024;
		write(fd, argv[2], len);
	}
	else
	{
		len = read(fd, buf, 1024);		
		buf[1023] = '\0';
		printf("APP read : %s\n", buf);
	}
	
	close(fd);
	
	return 0;
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。