鸿蒙系统开发教程_韦东山 2-2必备基础知识

在线课堂:https://www.100ask.net/index(课程观看)
论  坛:http://bbs.100ask.net/(学术答疑)
开 发 板:https://100ask.taobao.com/ (淘宝)
     https://weidongshan.tmall.com/(天猫)
交流群一:QQ群:869222007(鸿蒙开发/Linux/嵌入式/驱动/资料下载)
交流群二:QQ群:536785813(单片机-嵌入式)
公 众 号:百问科技


版本 日期 作者 说明
V1 2020 韦东山 韦东山鸿蒙系统开发教程

>>>视频教程在线观看<<<:

https://www.100ask.net/detail/p_5fcf586ae4b04db7c0939c82/8

1. 基础知识

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

1.1 单片机相关的知识

  • 栈的作用

  • 加载地址、链接地址

  • 重定位

  • 几个简单的硬件知识

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

1.2 Linux操作相关的知识

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

1.3 芯片相关知识

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

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

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

2. 驱动程序知识

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

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

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

扫描二维码关注公众号,回复: 12120839 查看本文章

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中定义了一个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;
}

en = 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;

}


猜你喜欢

转载自blog.csdn.net/thisway_diy/article/details/110919590
今日推荐