1, Introduction to Linux application development
Three directions of Linux learning:
- Operation and maintenance
- application development
- Bottom development
For application development engineers, the skills they need to master are
- API of Linux platform, including IO operation, interprocess communication, linux environment and network communication
- Scripting languages, including Shell, Python and databases, such as MySQL and Sqlite.
2, Embedded Linux application development example
The source code is from the driving test example of punctual atom. This chapter introduces the composition of application source code and the process of developing an application with an example. In an application, there are two parts: header file and main function. Header files contain the functions of the kernel. The functions of these header files will be introduced later; The main function uses the IO operation of Linux to read ir, als and ps of ap3216c.
#include "stdio.h" #include "unistd.h" #include "sys/types.h" #include "sys/stat.h" #include "sys/ioctl.h" #include "fcntl.h" #include "stdlib.h" #include "string.h" #include <poll.h> #include <sys/select.h> #include <sys/time.h> #include <signal.h> #include <fcntl.h> /*************************************************************** Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved. file name : ap3216cApp.c author : Zuo Zhongkai edition : V1.0 describe : ap3216c equipment test APP. other : nothing usage method : ./ ap3216cApp /dev/ap3216c Forum : www.openedv.com journal : First version v1.0 created by Zuo Zhongkai on September 20, 2019 ***************************************************************/ /* * @description : main main program * @param - argc : argv Number of array elements * @param - argv : Specific parameters * @return : 0 success; Other failures */ int main(int argc, char *argv[]) { int fd; char *filename; unsigned short databuf[3]; unsigned short ir, als, ps; int ret = 0; if (argc != 2) { printf("Error Usage!\r\n"); return -1; } filename = argv[1]; fd = open(filename, O_RDWR); if(fd < 0) { printf("can't open file %s\r\n", filename); return -1; } while (1) { ret = read(fd, databuf, sizeof(databuf)); if(ret == 0) { /* Data read successfully */ ir = databuf[0]; /* ir Sensor data */ als = databuf[1]; /* als Sensor data */ ps = databuf[2]; /* ps Sensor data */ printf("ir = %d, als = %d, ps = %d\r\n", ir, als, ps); } usleep(200000); /*100ms */ } close(fd); /* Close file */ return 0; }
2.1 header file
Common header files for Linux Driver Development The functions of each header file are clearly explained.
There are a total of 32. h header files in the header file directory. There are 13 in the main directory, 4 in the asm subdirectory, 10 in the linux subdirectory and 5 in the sys subdirectory. The functions of these header files are as follows:
Files in the home directory
<a.out.h>: a.out Header file that defines a.out Execute file formats and some macros. <const.h>: Constant symbol header file, currently only defined i In node i_mode Each flag bit of the field. <ctype.h>: Character type header file, which defines some macros related to character type judgment and conversion. <errno.h>: Error header file, including various error numbers in the system.(Linus from minix Introduced in). <fcntl.h>: The file control header file is used to define the operation control constant symbol of the file and its descriptor. <signal.h>: The signal header file defines the signal symbol constant, signal structure and signal operation function prototype. <stdarg.h>: The standard parameter header file defines the variable parameter list in the form of macro. It mainly describes one type( va_list)And 3 macros( va_start, va_arg and va_end),be used for vsprintf,vprintf,vfprintf Function. <stddef.h>: The standard definition header file defines NULL, offsetof(TYPE, MEMBER). <string.h>: String header file, which mainly defines some embedded functions related to string operation. <termios.h>: The terminal input / output function header file mainly defines the terminal interface controlling the asynchronous communication port. <time.h>: Time type header file, which mainly defines tm Structure and some primitive functions related to time. <unistd.h>: Linux The standard header file defines various symbolic constants and types, and declares various functions. For example, it defines__LIBRARY__,It also includes system call number and embedded assembly_syscall0()Wait. <utime.h>: User time header file, which defines the access and modification time structure and utime()Prototype.
File at linux-2.6.29/include/linux
<linux/config.h>: The kernel configuration header file defines the keyboard language and hard disk type( HD_TYPE)Optional. <linux/fdreg.h>: Floppy disk header file, which contains some definitions of floppy disk controller parameters. <linux/fs.h>: File system header file, defining file table structure( file,buffer_head,m_inode Etc.). <linux/hdreg.h>: The hard disk parameter header file defines information such as access to hard disk register port, status code and partition table. <linux/head.h>: head Header file, which defines the simple structure of segment descriptor and several selector constants. <linux/kernel.h>: The kernel header file contains the prototype definitions of some common kernel functions. <linux/mm.h>: Memory management header file, including page size definition and some page release function prototypes. <linux/sched.h>: The scheduler header file defines the task structure task_struct,The data of the initial task 0, as well as some embedded assembly function macro statements about descriptor parameter setting and acquisition. <linux/sys.h>: The system call header file contains 72 system calls C Function handler,with"sys_"start. <linux/tty.h>: tty Header file that defines the tty_io,Parameters and constants of serial communication.
File at linux-2.6.29/arch/arm/include/asm
<asm/io.h>: I/O Header file, which defines the pair in the form of macro embedded assembler I/O Function of port operation. <asm/memory.h>: Memory copy header file containing memcpy()Embedded assembly macro functions. <asm/segment.h>: The segment operation header file defines the embedded assembly function for segment register operation. <asm/system.h>: System header file that defines the setting or modification descriptor/Embedded assembly macros such as interrupt gates.
File located in include/sys
<sys/stat.h>: A file status header file that contains a file or file system status structure stat{}And constants. <sys/times.h>: Defines the runtime structure in the process tms as well as times()Function prototype. <sys/types.h>: Type header file, which defines the basic system data types. <sys/utsname.h>: System name structure header file. <sys/wait.h>: Wait for the call header file to define the system call wait()and waitpid()And related constant symbols.
2.2 main function
First, judge whether the parameters are correct through argc and argv of main, open the ap3216c device through open, and read the sensor data of ap3216c through read.
/* * @description : main main program * @param - argc : argv Number of array elements * @param - argv : Specific parameters * @return : 0 Success; other failures */ int main(int argc, char *argv[]) { int fd; char *filename; unsigned short databuf[3]; unsigned short ir, als, ps; int ret = 0; if (argc != 2) { printf("Error Usage!\r\n"); return -1; } filename = argv[1]; fd = open(filename, O_RDWR); if(fd < 0) { printf("can't open file %s\r\n", filename); return -1; } while (1) { ret = read(fd, databuf, sizeof(databuf)); if(ret == 0) { /* Data read successfully */ ir = databuf[0]; /* ir Sensor data */ als = databuf[1]; /* als Sensor data */ ps = databuf[2]; /* ps Sensor data */ printf("ir = %d, als = %d, ps = %d\r\n", ir, als, ps); } usleep(200000); /*100ms */ } close(fd); /* Close file */ return 0; }
3, Supplementary knowledge
3.1 input parameters argc and argv of main function
reference resources Arguments argc and argv of main function , argc's full name is arguments count, which means the number of parameters passed in from the command line to the program, including the program name. When argc is 1, it means that there are no other parameters in the command line except the program name. The full name of char *argv [] is arguments value, that is, the parameter value. The input of the command line is a string. The program can process the parameters passed in from the command line accordingly.