Xunwei 4412 development board Linux character device control

Character device drivers must be mastered in Linux drivers. This chapter mainly introduces character device application programs. Whether it is a character driver written by yourself after learning the following knowledge, or an existing character driver, you need to be able to write some simple applications . Even if you are engaged in Linux driver work, after the Linux driver is written, it is necessary for the driver programmer to write a simple application for testing.
In addition, regarding the driving part, Xunwei Electronics has a special driving experiment tutorial for everyone to learn. After everyone has these foundations, it will be easy to learn the underlying knowledge.
In Chapter 10.22 of this manual, you can see that these C programs can also be run under Android, but there is no graphical interface.
The supporting videos for this chapter are:
"Video 06_01 Character Device Controlling Main Function Passing Parameters"
"Video 06_02 Character Device Controlled LED Lights"
"Video 06_03 Character Device Controlling Buzzer Buzzer"
"Video 06_04 Character Device Controlling ADC Analog-to-Digital Conversion" "
parameter 17.1 entrance main function of
the process and the user exchanges, although everyone learned C language, but to use C language main function is not very clear. Since this part of the knowledge is needed in the subsequent experiments, I will take up a section here. First, let's briefly introduce the main function.
Introduction to the
main function The main function is the entry point of the application and is in the header file "#include<stdio.h>". When the main function needs to pass parameters, the complete definition is
int main (int argc, char **argv)
parameter argc, which represents the number of parameters
Parameter **argv, an array that stores the input characters, argv[0] represents the name of the program, argv[1]——argv[n] input parameter
is defined as
int main(void) when the parameter is not passed
, the return value of the function main The type is int, which is used to judge the success or failure of the program execution.
Main function routine
Write a simple argvc.c file to test the main function.

As shown in the figure above, the first and second input parameters are converted to int type, assigned to i and j, and finally output and printed.
Among them, argv[0] is the name of the program, and here is the object file "argvc" to be compiled later.
Compile and run the test
under the Ubuntu system, as shown in the figure below, enter the directory "/home/linuxsystemcode/" created in the previous experiment, use the command "mkdircharcontrol" to create a new charcontrol folder, copy the source code argvc.c into it, and enter the newly created folder charcontrol, as shown in the figure below.

Use the command "arm-none-linux-gnueabi-gcc -o argvc argvc.c -static" to compile the argvc file, as shown in the figure below, use the command "ls" to see that the argvc executable file is generated.

Here is the method of copying code from U disk, which can also be compiled into the file system. For the specific method, refer to section 10.3.5. Copy the compiled executable file argvc to a U disk, start the development board, insert the U disk, load the U disk, and run the program as follows.

As shown in the figure above, the program runs successfully and prints:
the Program name is ./mnt/udisk/argvc.
Because the running program is "./mnt/udisk/argvc", this is the first parameter
The command line has 2 argument:
10,11.

The input parameters are 10 and 11, corresponding to argv[2] and argv[2].
17.2 Character type led lights
When the open function was introduced earlier, how to open a character type device has been mentioned, and the method of obtaining a handle is the same as that of a general file.
The device node of the LED lamp is in the /dev directory, as shown in the figure below, you can use the ls command to find it in the HyperTerminal.

As it involves hardware knowledge, here is a brief introduction to the hardware principle. As shown in the figure below, the hardware principle of the led light is very simple.

As shown in the figure above, if the KP_COL0 and VDD50_EN network are high, the transistor L9014 will be turned on, and the power supply VSYS will apply the voltage to the resistor R and the small led light, and the small light will light up.
Give the KP_COL0 and VDD50_EN network low level, the transistor L9014 will be cut off, forming an open circuit, and the light will go out.
As mentioned earlier, if you want to write to a file, you use the write function. For the operation of small led lights, it is theoretically possible to use the write function. But for the IO port (here the IO port refers to the IO port on the hardware, not the IO file)

For operation, Linux has specially designed an efficient function ioctl.
This function is in the header file #include<unistd.h>.
int ioctl(int fd, int request, int cmd); The
parameter fd, the handle returned by the function open.
The parameters request and cmd are determined by the kernel driver. For example, request can represent the IO port, and cmd can represent what operation is performed on the IO, or vice versa. The specific meaning is determined by the drive engineer's switch in the drive.
Return value: return 0 for success; return -1 for error.


Small lamp test routine
Write a simple leds.c file to test the small lamp. First add the header file, as shown in the figure below.
The parameters passed through the main parameter are in char format. If you want to pass to the ioctl function, you need to use the value conversion function atoi, and add the header file #include <stdlib.h>.
Then, since the number of small lights and commands are both 2, the number of small lights and the operands are macro-defined
#define LED_NUM 2
#define LED_C 2.

Then the main function is shown in the figure below.

As shown in FIG.
In line 33, the ioctl function is called, and the first and second parameters of the main function are passed to the driver.
In line 19, explain the meaning of the parameter, "argv1 is cmd; argv2 is io", parameter 1 corresponds to the command, and parameter 2 corresponds to the specific led light.
On line 36, close the opened device node "/dev/leds".


Compile and run the test
In the Ubuntu system, as shown in the figure below, enter the directory "/home/linuxsystemcode/charcontrol" created in the previous experiment, and copy the source code leds.c into it, as shown in the figure below.

Use the command "arm-none-linux-gnueabi-gcc -o leds leds.c -static" to compile the leds file, as shown in the figure below, use the command "ls" to see that the leds executable file is generated.

Here is how to copy the code from the USB flash drive, which can also be compiled into the file system. For the specific method, refer to Experiment 02.
Open the compiled executable file, copy it to the USB flash drive, start the development board, insert the USB flash drive, load the USB flash drive, and run the program as follows .
As shown in the figure below, if you do not add parameters, there will be a prompt, and then an error will be reported.

As shown in the figure below, use the command "./mnt/udisk/leds 0 0" to run, you can see that the small light near the buzzer is off.

All parameters control the small lights as follows:
0 0 The small light close to the buzzer is off;
0 1 The small light close to the button is off;
1 0 The small light close to the buzzer is on;
1 1 The small light close to the button is on. Users can test it by themselves.

 

Guess you like

Origin blog.csdn.net/mucheni/article/details/114367110