0
点赞
收藏
分享

微信扫一扫

开发板教程-20220404

春意暖洋洋 2022-04-04 阅读 56
linux

买的 MX6U 板子今天到货,用四个月到五个月玩这个板子。

每天写几篇文章更新一下,自己的进步。

Linux 硬件平台下的软件开发来说。大可将编程分为三种,分别为裸机编程、 Linux 驱
动编程以及 Linux 应用编程。
首先对于裸机编程这个概念来说很好理解,一般把没有操作系统支持的编程环
境称为裸机编程环境,譬如单片机上的编程开发,编写直接在硬件上运行的程序,没有操作系统支持; 狭义上 Linux 驱动编程指的是基于内核驱动框架开发驱动程序, 驱动开发工程师通过调用 Linux 内核提供的接口完成设备驱动的注册, 驱动程序负责底层硬件操作相关逻辑, 如果学习过 Linux 驱动开发的读者,想必对此并不陌生; 而 Linux 应用编程(系统编程)则指的是基于 Linux 操作系统的应用编程,在应用程序中通过调用系统调用 API 完成应用程序的功能和逻辑, 应用程序运行于操作系统之上。

应用开发程序

static void led_on(void)
{

/* 点亮 LED 硬件操作代码 */

}
static void led_off(void)
{
/* 熄灭 LED 硬件操作代码 */
}

int main(void)
{
/* 用户逻辑 */
for ( ; ; ) {

led_on();
delay();
led_off();
delay();
//点亮 LED
//延时
//熄灭 LED
//延时

}
}

Linux 系统下的 LED 驱动程序示例代码
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/delay.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
 

static void led_on(void)
{
/* 点亮 LED 硬件操作代码 */
}
static void led_off(void)
{
/* 熄灭 LED 硬件操作代码 */
}
static int led_open(struct inode *inode, struct file *filp)
{
/* 打开设备时需要做的事情 */
}
static ssize_t led_write(struct file *filp, const char __user *buf,
size_t size, loff_t *offt)
{
int flag;
/* 获取应用层 write 的数据,存放在 flag 变量 */
if (copy_from_user(&flag, buf, size))
return -EFAULT;
/* 判断用户写入的数据,如果是 0 则熄灭 LED,如果是非 0 则点亮 LED */
if (flag)
led_on();
else
led_off();
return 0;
}
static int led_release(struct inode *inode, struct file *filp)
{
/* 关闭设备时需要做的事情 */
}
static struct file_operations led_fops = {

.owner
.open
.write
.release
= THIS_MODULE,
= led_open,
= led_write,
= led_release,

};
static int led_probe(struct platform_device *pdev)
{
/* 驱动加载时需要做的事情 */
}
static int led_remove(struct platform_device *pdev)
{
/* 驱动卸载时需要做的事情 */
}
static const struct of_device_id led_of_match[] = {

{ .compatible = "alientek,led", },

{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, led_of_match);
static struct platform_driver led_driver = {
.driver = {

.owner
.name
.of_match_table
= THIS_MODULE,
= "led",
= led_of_match,
},
.probe= led_probe,

.remove = led_remove,
};
module_platform_driver(led_driver);
MODULE_DESCRIPTION("LED Driver");
MODULE_LICENSE("GPL");

led_fops 对象中提供了 open、write、 release 方法,当应用程序调用 open 系统调用打开此 LED 设备时会执行到 led_open 函数,当调用 close系统调用关闭 LED 设备时会执行到 led_release 函数,而调用 write 系统调用时会执行到 led_write 函数,此驱动程序的设定是当应用层调用 write 写入 0 时熄灭 LED, write 写入非 0 时点亮 LED。

应用程序对设备文件的操作

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd;
int data;
fd = open("/dev/led", O_WRONLY);//打开 LED 设备(假定 LED 的设备文件为/dev/led)
if (0 > fd)
return -1;
for ( ; ; ) {

data = 1;
write(fd, &data, sizeof(data)); //写 1 点亮 LED
sleep(1);//延时 1 秒

data = 0;
write(fd, &data, sizeof(data)); //写 0 熄灭 LED
sleep(1); //延时 1 秒
}
close(fd);
return 0;
}

ulimit 命令来查看进程可打开的最大文件数

ulimit -n

 

举报

相关推荐

0 条评论