4412 development board Linux System Programming of actual combat - device control character

In linux driver is driving the characters must be mastered, this chapter introduces a character device application program, whether it is learning the knowledge behind the drive to write their own characters, or characters already in the drive, need to be able to write simple applications.

Even in the linux-driven work, written after linux driver, also driven by the need to write a simple application programmers to test.

In addition, with respect to the driving part, fast electronics have a special drive test tutorial learning available to everyone, then we have to go learn these basic underlying knowledge will be very easy.

In the eighth chapter of the manual, we can see these c program also can run on Android below, but there is no graphical interface.

Hardware Tools

Development board PC, 4412; the U-disk or TF card

software tools

Ubuntu12.04.2 and a virtual machine; Arm-2009q3 compiler; Notepad ++ editor; HyperTerminal; Vim editor; minimal linux system

 Preparatory Course

Use basic development tools; commonly used linux commands; minimal linux system; file IO

Video resources

This chapter is supporting the video:

"Control of the video 06_01 character device main function passed parameter"

"The video control apparatus 06_02 led light character"

"Video 06_03 character device control of the buzzer buzzer"

"Video 06_04 character device ADC analog to digital conversion of control"

learning target

Master the main function of usage

Grasp the character device led lamp, buzzer buzzer, ADC analog to digital conversion

A main function parameter entry

In the process of the exchange and the user, although everyone learned C language, but to use C language main function is not very clear. Since the latter part of the experiment need to use this knowledge, here on the occupation of a section, briefly explain the main function.

1.1 main function Introduction

As the main function of the application entry in the header file "#include <stdio.h>" in.

When the main function needs to pass parameters defined as complete

int main(int argc,char **argv)

Parameter argc, indicates the number of parameters

Array parameter ** argv, storing the input character

argv [0] represents the name of the program

argv [1] - argv [n] input parameters

Time does not pass parameter is defined as

int main(void)

The main function return value is int, for determining success or failure of execution

1.2main function routine

Write a simple test file argvc.c main function.

 

 

 

 

 

 

 

As shown above, the input parameters of the first and second converted to type int, assigned to i and j, the final output print. Where argv [0] is the program name, this is back to compiled object file "argvc".

1.3 compile and run tests

In Ubuntu system, as shown below, into the directory created in the previous experiment "/ home / linuxsystemcode /",

Using the command "mkdir charcontrol" New charcontrol folder, argvc.c copied into the source code, into the new folder charcontrol, as shown in FIG.

 

 

 

 

 

Use the command "arm-none-linux-gnueabi-gcc -o argvc argvc.c -static" argvc compiled file, as shown below using the command "ls" see argvc generated executable file.

 

 

 

 

 

 

 

Here are U disk copy code method, it can be compiled into the file system, particularly the method of Reference Experiment 02

The compiled executable file argvc, U copied to disk, starting boards, into the U disk, U disk is loaded, run the program as follows.

 

 

 

 

 

 

As shown above, run successfully, the print:

the Program name is ./mnt/udisk/argvc. Because the program runs is "./mnt/udisk/argvc",

This is the first parameter

The command line has 2 argument:

10, 11. Input parameters are 10 and 11, the corresponding argv [2] and argv [2].

Class 2 character led lamp

In the previously described function of open time, already we mentioned how to turn character class equipment, methods and generally get the handle

Files are the same.

Led lamp device nodes in the / dev directory, as shown in HyperTerminal ls command to find.

 

 

 

 

 

 

由于涉及到硬件知识,这里简单介绍一下硬件原理,如下图所示,led 小灯的硬件原理很简单。

 

 

 

 

 

 

 

如上图所示,给 KP_COL0 和 VDD50_EN 网络高电平,三极管 L9014 就会导通,电源 VSYS 就会将电压加到电阻 R 和 led 小灯上,小灯就会亮。

给 KP_COL0 和 VDD50_EN 网络低电平,三极管 L9014 就会截止,形成断路,小灯灭。

 

在前面介绍过,如果要给文件进行写操作,那么使用的是 write 函数。对于 led 小灯的操作,使用写函数,理论上也是可以的。但是对于 IO 口(这里的 IO 口指的是硬件上的 IO 口, 不是指 IO 文件)的操作,Linux 专门设计了一个高效的函数 ioctl。

这个函数在头文件#include<unistd.h>中。

int ioctl( int fd, int request, int cmd);

参数 fd,函数 open 返回的句柄。

参数 request 和参数 cmd,由内核驱动决定具体操作,例如 request 可以代表那个 IO 口, cmd 代表对 IO 进行什么样的操作,也可以反过来。具体的含义由驱动工程师在驱动中 switch决定。

返回值:返回 0 成功;返回-1,出错。

2.1小灯测试例程

编写简单的 leds.c 文件测试小灯。

首先添加头文件,如下图所示。

通过 main 参数传过来的参数是 char 字符格式的,如果要传递给 ioctl 函数,需要用到数

值转化函数 atoi,添加了头文件#include <stdlib.h>。

接着由于小灯的数量和命令都是 2,所以对小灯数量和操作数进行宏定义#define LED_NUM 2 #define LED_C 2。

 

 

 

 

 

然后 main 函数如下图所示。

 

 

 

 

 

 

 

如上图所示。

第 33 行,调用了 ioctl 函数,将 main 函数的第一个和第二个参数传入驱动。

第 19 行,解释那个参数具体代表什么含义,"argv1 is cmd;argv2 is io”,参数 1 对应命

令,参数 2 对应具体那个 led 灯。

第 36 行,将打开的设备节点"/dev/leds"关闭。

2.2 编译运行测试

在 Ubuntu 系统下,如下图所示,进入前面实验创建的目录

“/home/linuxsystemcode/charcontrol”,将源码 leds.c 拷贝进去,如下图所示。

 

 

 

 

 

 

 

使用命令“arm-none-linux-gnueabi-gcc -o leds leds.c -static”编译 leds 文件,如下

图所示,使用命令“ls”可以看到生成了 leds 可执行文件。

 

 

 

 

 

 

这里介绍 U 盘拷贝代码的方法,也可以编译进文件系统,具体方法参考实验 02

将编译成的可执行文件 open,拷贝到 U 盘,启动开发板,插入 U 盘,加载 U 盘,运行程序如下。

如下图所示,如果不加参数会有提示,然后报错。

 

 

 

 

 

 

 

如下图所示,使用命令“./mnt/udisk/leds 0 0”运行,可以看到靠近蜂鸣器的小灯灭了。

 

 

 

 

 

 

所有参数对小灯的控制如下:

0 0 靠近蜂鸣器的小灯灭;

0 1 靠近按键的小灯灭;

1 0 靠近蜂鸣器的小灯亮;

1 1 靠近按键的小灯亮。

用户可以自行测试一下。

3 字符类 Buzzer 蜂鸣器

和 led 灯类似,蜂鸣器的设备节点也是在/dev 目录下,如下图所示。

 

 

 

 

 

 

蜂鸣器的硬件和 led 灯类似,如下图所示。

 

 

 

 

 

 

如上图所示。

原理图很容易理解,如果网络 MOTOR_PWM 为高电平,则 L9014 导通,蜂鸣器响,如

果网络 MOTOR_PWM 为低电平,则 L9014 截止,蜂鸣器则不响。

操作方式和 led 小灯类似。

3.1蜂鸣器测试例程

编写简单的 buzzertest.c 文件测试蜂鸣器。

首先添加头文件,如下图所示,下面新加了几个库文件,一般常用的就是下面几个,写代

码的时候,为了方便,可以直接都添加上。

 

 

 

 

 

 

 

然后 main 函数如下图所示。

 

 

 

 

 

 

如上图代码所示。

由于只有一个 IO,底层没有做第三个参数的判断,所以无效

第 16-19 行,对参数 argv[1]有个简单的判断,命令只能是 0 或者 1。

第 21-24 行,open 函数打开蜂鸣器设备节点

第 26 行,使用 ioctl 函数操作蜂鸣器。

第 27 行,使用 close 函数关闭设备节点。

3.2 编译运行测试

在 Ubuntu 系统下,如下图所示,进入前面实验创建的目录

“/home/linuxsystemcode/charcontrol”,将源码 buzzertest.c 拷贝进去,如下图所示。

 

 

 

 

 

 

使用命令“arm-none-linux-gnueabi-gcc -o buzzertest buzzertest.c -static”编译 buzzertest 文件,如下图所示,使用命令“ls”可以看到生成了 buzzertest 可执行文件。

 

 

 

 

 

这里介绍 U 盘拷贝代码的方法,也可以编译进文件系统,具体方法参考实验 02

将编译成的可执行文件 buzzertest,拷贝到 U 盘,启动开发板,插入 U 盘,加载 U 盘, 运行程序如下。 使用参数 1 和 0,蜂鸣器会响。第二个参数实际上并不起作用。

 

 

 

 

 

如下图所示,使用参数 0 和 0,蜂鸣器会停止响。

 

 

 

 

 

4字符类 ADC 模数转换

和 led 灯类似,数模转换的设备节点也是在/dev 目录下,如下图所示。

 

 

 

 

 

模数转换的硬件部分如下图所示。

 

 

 

 

 

 

 

如上图所示。

XadcAIN0 网络可以读取到当前输入电压,滑动变阻器 R 移动的时候,1 和 2 之间的电阻 R12 改变,滑动变阻器最大电阻为 R13,然后电压 Vadc=R12*VDD1V8_EXT/R13 上面公式中 Vadc 可以通过 4412 读取出来,VDD1V8 和 R13 已知,那么就很容易求出 R12 的电阻。如下图所示,在 4412datasheet 中 ADC 章节中有真实的电阻和电压曲线图。

 

 

 

 

这里将数值做一个简单的换算,

1.8V 对应的是 10K 欧姆,对应的寄存器数值为 0xfff;

0V 对应的是 0 欧姆,对应的寄存器数值为 0x0。

这样做一个简单公式,将读取的数值 r 转化为电阻值 R。

R = r*10000/0xfff,即 R = r*10000/4095。

这个小公式在后面的代码中将会使用到。

4.1模数转换例程

编写简单的 ADC.c 文件测试 adc 的驱动。

首先添加头文件,如下图所示。

 

 

 

 

然后 main 函数如下图所示。

 

 

 

 

如上图代码所示。

第 14 行,设备节点为 char *adc = "/dev/adc"。

第 21 行,打开设备节点文件。

第 26 行,使用 read 函数,将读取数字赋予 buffer。

第 30 和 31 行,做个简单地换算,将读取的数值转化为电阻值。

4.2编译运行测试

在 Ubuntu 系统下,如下图所示,进入前面实验创建的目录

“/home/linuxsystemcode/charcontrol/”将源码 ADC.c 拷贝进去,如下图所示。

 

 

 

 

使用命令“arm-none-linux-gnueabi-gcc -o ADC ADC.c -static”编译 ADC 文件,如 下图所示,使用命令“ls”可以看到生成了 ADC 可执行文件。

 

 

 

 

这里介绍 U 盘拷贝代码的方法,也可以编译进文件系统,具体方法参考实验 02

将编译成的可执行文件 open,拷贝到 U 盘,启动开发板,插入 U 盘,加载 U 盘,运行程 序。

如下图所示,使用命令“./mnt/udisk/ADC”即可检测当前电阻值,中间的大段打印参数 是多次打印寄存器的数值,在驱动实验中再去介绍。

 

 

 

 

 

调整滑动电阻器之后,再次使用测试程序,如下图所示,输出数值会有变化。

 

 

 

 

 

 

 

滑动变阻器向顺时针旋转,阻值会减小,最小为 0;

滑动变阻器向逆时针旋转,阻值会增大,最大为 10K。

http://topeetboard.com

Guess you like

Origin www.cnblogs.com/yueliang17/p/11996852.html