Friday, May 31, 2013

linux c programming - Makefile

Overview
What is a makefile? Winodws many programmers do not know this thing, because those Windows IDE for you to do this job, but I feel the need to make a good and professional programmers, makefile to understand. It's like there are so many HTML editor, but if you want to become a professional, you still have to know HTML to identify meaning. Especially in the Unix software compiler, you can not write your own makefile, and will not write makefile, from one side shows whether a person has the ability to complete large projects.

Because, makefile relation to the entire project compilation rules. A project's source files do not count, which according to type, function, modules were placed in several directories, makefile defines a set of rules to specify which files need to be compiled, after which files need to compile, which files need to re- compile, even for more complex functions operation because makefile like a Shell scripts, which can execute operating system commands.

makefile benefits is - "Automation compilation", once written, you only need a make command, the entire project is fully automatic compilation, which greatly improves the efficiency of software development. make is a command tool is an instruction explaining the makefile command tools, in general, most of the IDE have this command, for example: Delphi's make, Visual C++ for nmake, Linux under the GNU make. Visible, makefile have become a kind of compilation methods in engineering.

In this document, will be C/C++ source code as our foundation, so inevitably involve some information about C/C++ compiler knowledge related to the content in this area, also please see the compiler documentation. Here are the default compiler is GCC under UNIX and CC. 

About the program is compiled and linked
At this point, I would like to say more about the program compiles some of the norms and methods, in general, whether it is C, C++, or pas, first of all necessary source files compiled into intermediate code file in Windows that is. Obj files, UNIX Next .o files that Object File, this action is called compilation (compile). Then put a lot of Object File synthesis executable file, this action is called link (link).

Compile time, the compiler needs is the correct syntax, functions, and variable declaration is correct. For the latter, usually you need to tell the compiler header file location (should only be declared in the header file, but should be defined in C / C + + files), as long as all the proper syntax compiler can compile the intermediate target files. In general, each source file should correspond to an intermediate object files (O files, or OBJ files).

Link, mainly linked functions and global variables, so we can use these intermediate object files (O files, or OBJ files) to link our application. Linker and regardless of the source file where the function, although the function of an intermediate object files (Object File), most of the time, because too many source files, the compiler generates an intermediate object files too, and in the need to clearly point out the link among target file name, which is very convenient for the compiler, so we have to give the intermediate target pack files, in Windows, this package is called "library" (Library File), that is. lib files under UNIX, is Archive File, that is. a file.

In summary, the source file will first generate intermediate target file, and then from the middle of the target file to generate executable file. At compile time, the compiler detects only program syntax, and functions, whether the variable is declared. If the function is not declared, the compiler will give a warning, but can generate Object File. In the linker, the linker will find all of the Object File in the implementation of the function, if not, it will be reported to the link error code (Linker Error), the VC, this error is usually: Link 2001 error, saying that that the linker can not find the function implementation. You need to specify the function Object File.

Well, get, GNU's make lots of stuff, without further ado, let us begin.

Makefile describes 
make command is executed, you need a Makefile to tell how to make command required to compile and link the program.
First, we use an example to illustrate the Makefile rules of writing. In order to give you a sense of Xing understanding. This example comes from the GNU make manual, in this example, we have eight C project files, header files, and 3, we write a Makefile to tell how to make command to compile and link these files. Our rules are:
    1) If this project is not compiled, then we have all the C files compiled and linked. 
    2) If this works for a few C file is modified, then we only compile C files are modified, and link target 
program
    3) If the project header file is changed, then we need to compile quoted this several header files in C files and link target program.

As long as our Makefile written good enough, all of this, we only use a make command can be completed, make command automatically intelligently according to the current file modification to determine which files need to be recompiled, and thus need to compile program files and link targets.

1. Makefile rules
In about this Makefile before, let us first take a cursory look at the Makefile rules.

    target ...: Prerequisites ... 
            command 
            ... 
            ...

    target that is a target file, which can be Object File, it can be executed files. Can also be a label (Label), the label of this feature, in the subsequent "pseudo target" will be described in the chapter.

    prerequisites that is, to generate the required files that target or targets.

    command is the make command to be executed. (Arbitrary Shell Command)

This is a file dependencies, ie, target the one or more target file depends on prerequisites in the document, and its generation rules defined in the command. White said that, prerequisites, if there is more than one file is newer than the target file, then, command defined commands will be executed. This is the Makefile rules. That is the core content of the Makefile.

In the final analysis, Makefile is such a thing that seems to me also that the end of this document. Oh. Not really, this is the main line of the Makefile and the core, but not enough to write a Makefile, I will back little by little work experience combined with my coming to you slowly. Content still living. :)

2. Sample
As previously mentioned, if a project has three header files, and eight C files, we described earlier in order to complete the three rules, we Makefile would look like this below.

    edit: main.o kbd.o command.o display.o / 
           insert.o search.o files.o utils.o 
            CC-O edit main.o kbd.o command.o display.o / 
                       insert.o search.o files.o utils.o

    main.o: main.c defs.h 
            CC-c main.c 
    kbd.o: kbd.c defs.h command.h 
            CC-c kbd.c 
    command.o: COMMAND.C defs.h command.h 
            CC - c COMMAND.C 
    display.o: display.c defs.h buffer.h 
            CC-c display.c 
    insert.o: insert.c defs.h buffer.h 
            CC-c insert.c 
    search.o: search.c defs . h buffer.h 
            CC-c search.c 
    files.o: modifier for defs.h buffer.h command.h 
            CC-c modifier for 
    utils.o: utils.c defs.h 
            CC-c utils.c 
    clean : 
            RM edit main.o kbd.o command.o display.o / 
               insert.o search.o files.o utils.o

Slash (/) is a newline mean. This is more convenient to read the Makefile. We can put this content saved in the file as "Makefile" file, then in the directory, simply type the command "make" you can generate executable file edit. If you want to delete the executable file and all the intermediate object files, then simply execute it "make clean" on it.

In this makefile, the target file (target) contains: executable file edit and intermediate object files (*. O), dependencies (prerequisites) that those behind the colon. C files and. H files. Every one. O file has a set of dependent files, and these. O file is executable file edit the dependent files. Dependencies essentially shows which files from the target file is generated, in other words, the target file is which files updated.

After defining dependencies, subsequent line that defines how to generate the target file of the operating system commands, be sure to start with a Tab key as. Remember, make and regardless of how the command works, he just execute the defined commands. make would be more targets files and prerequisites file modification date, file date if the prerequisites file date than targets to be new, or target does not exist, then, make follow-defined command is executed.

Here we must point out that, clean is not a file, it is merely an action name, a bit like the C language lable, like its nothing after the colon, then, make it will not automatically go to file dependencies, it does not automatically execute the commands defined later. Subsequent commands to be executed, we must significantly after the make command must say this lable name. This method is useful, we can not be defined in a makefile compiler and compiler-independent or commands, such as packaged programs, backup procedures, and so on.

Linux system boot process

1.BIOS Start:
When the system is powered up, the computer will first from CMOS ( Complementary Metal Oxide Semiconductor ) chip load BIOS , and the detection system hardware basic information, such as memory, hard drives, processors, etc.. Then BIOS to find whether there is the system boot disk , and if not, the system will find the master boot MBR ( Main Boot Record ) and run record MBR on the program. Usually this program is the operating system of the Loader program (called embedded systems Bootloader ) , whose main function is to instruct the system to be loaded at start up and loading system. For example, in MBR running on Linux the Loader program ( GRUB and LILO ), you can load the Linux kernel. BIOS called firmware. In computer systems, BIOS program is stored in the motherboard chip, format the hard disk does not have any impact on this information; except with special tools can be erase, its content is always there.

2.GRUB Loading system:
GRUB is a system running Loader program, under normal circumstances, the installation of LINUX operating system, start the system when the first program that is GRUB . This program is used to load the operating system kernel (if there are multiple operating systems, this program is used to select the boot multiple operating systems).
In the LINUX operating system, and start the associated application is located in /boot folder. Run GRUB reads Applications for /boot/grub/grub.conf file, edit this file to load the operating system, you can set the startup waiting time (waiting time exceeded, the system will start the default boot operating system), start menu, default boot operating system and operating system kernel image filename and location.

3. System startup files:
In the system loads Linux kernel, the system kernel in order to run the following procedure. The first system is running a kernel file as an /sbin/init , this program will create a system of one of a process, and this process as a whole system after the application of the parent process, and then run /etc/rc.d/rc.sysinit initialize the system. /etc/inittab is the system running the first three major program, this program is mainly used to select the starting levels, different levels start to run different programs and commands, these files are in /etc/rcX.d folder read. After completion of the above content execution, the system loads the user specific information.

4. Initialize the user file:

  •  /etc/rc.d/rc.local is a script file, complete the above initialization, the user can load the file folder system startup program to run. Start automatically when the system starts a program that can be added in this file, the equivalent of a batch program startup.
  •  /etc/profile file. Reads the /etc/rc.d/rc.local , the system will set the environment variable, which is the system's main SHELL configuration files, but also the first to detect when the user logs system files. Including PATH , USER , LOGNAME , MAIL , HOSTNAME , etc.
  •  /home/$USERNAME/./bash_profile file. After reading /etc/profile file, each user will read the documents with their own unique, except for root users, all users will read /home/$USERNAME/.bash_profile file and set their own environment variables.

Execution might finish above documents, the user can log in Linux operating system 

Recommended: Embedded Linux kernel learning books

Many embedded Linux novice, for their own learning process is very confused and do not know where to learn? Learning process needs to see what books? Before writing an article on how to get started embedded development, and how deep? The article I hope for your help, the next step is to recommend some embedded Linux novices to learn about embedded Linux core books.

Linux Essentials
  1. "Linux and Unix Shell Programming Guide"
C programming language
  1. "C Primer Plus, 5th Edition "[U.S.] Stephen Prata
  2. "The C Programming Language, 2nd Edition" [U.S.] Brian W. Kernighan David M. Rithie ( K & R )
  3. "Advanced Programming in the UNIX Environment, 2nd Edition" (APUE)
Linux kernel
  1. "Understanding the Linux Kernel" (third edition)
  2. "UNIX Network Programming" (UNP)
  3. "TCP / IP Elaborates"
  4. "Linux kernel programming"
  5. "Linux device driver development" (LDD)
Hardware foundation
  1, "ARM architecture and programming"
  2, S3C2410 Datasheet
Basic English
  1. "Computer and Communication English"
System Tutorial
  1. "Embedded Systems - Architecture, Programming and Design"
  2. "Building Embedded Linux Systems"
Theoretical foundation
  1. "Introduction to Algorithms"
  2. "data structure (C language version)."
  3. "Computer Organization and Architecture? Performance Analysis"
  4. "In-depth understanding of computer systems" [U.S.] Randal E. Bryant David O "Hallaron the
  5. "Operating Systems: Internals and Design Principles"
  6. "Compiler Principle"
  7. "Data Communications and Computer Networks"
  8. "Data Compression Principles and Applications"


Individual learning these things started to rely on self-study, to be honest this is too long journey. Embedded real still relatively high barriers to entry, before learning should be fully prepared mind, into the training institutions is also necessary to learn, I have discovered during the training, the same training, the same teaching, some people can learn well, some people have learned very poor. In fact, the saying goes: the master took the way, but by individuals.

Operating system some of the important concepts

Process synchronization (direct constraints):
Synchronization refers to the system of some processes need to cooperate together to complete a task . Specifically, a process to run to a certain point, the process for it to require the other partners to provide information without the prior messages, the process waits to get the message into the ready state after awakening. Synchronization means on the basis of mutually exclusive (in most cases), through other mechanisms visitor ordered access to the resource. In most cases, the mutex synchronization has been achieved, in particular all resources must be written to mutually exclusive. Few cases is one that can allow visitors to simultaneously access multiple resources.

Process mutex (indirect constraints) Mutual Exclusion:
Since each process requires the sharing of resources, while some resources require exclusive use, so competition between processes use these resources, the process of this relationship is mutually exclusive processes. A resource while allowing only be accessed by a visitor, unique and exclusive. But can not restrict visitors exclusive access to resources in order that access is disordered.

Related concepts:
Mutually exclusive: that multiple processes can not simultaneously use the same resources.
Deadlock: refers to the number of processes to each other, are not enough resources.
Hunger: refers to a process has been lack of resources (other processes may in turn consume resources).
Critical resource: the system allows only certain resources used by a process, known as the critical resources such resources or resources or shared variables mutually exclusive.
Critical area: the process of accessing critical resources section of code.

Critical Areas:

  • Critical region (Critical Section): The process to access critical resources section of code.
  • Entry zone (entry Section): Before entering the critical section, check the possibility of entering the critical section of code. If you can enter the critical region, usually set the corresponding " critical region is being accessed " flag
  • Exit area (Exit Section): Used to " critical region is being accessed " flag is cleared.
  • Residual area (remainder Section): The rest of the code.



Use critical areas should follow the guidelines
Available let into: When no process in the critical zone, any right to use the process to enter the critical section.
No empty wait: Do not allow more than two processes simultaneously enter the critical region.
Choose one of many: When there is no progress in the critical areas, while there are multiple processes required to enter the critical region, only let one of them to enter the critical section, other processes must wait.
Finite Waiting: Any request to enter the critical section should be met within a limited time.
Let right wait: in a wait state process should give up occupation of CPU.
Equal competition: no right to stop any process other processes running processes relative speed between no hard and fast rules.

 Linux under the process contains the following key elements:
There is a executable program;
A dedicated system stack space;
Kernel has its control block (process control block), describing the resources occupied by the process, so that the process can accept the kernel scheduler;
Has a separate storage space
Processes and threads sometimes not fully distinguish between, often depending on the context to understand its meaning.

Thursday, May 23, 2013

How to become a qualified software engineer for embedded systems

Embedded systems software engineers generally refers to those engaged in the field of embedded system software and master the basic knowledge. On an embedded system software engineers, the employer will pay particular attention to those in the field you have specific vocational skills, such as you are good driver software for embedded systems development, or embedded system application development, or embedded system testing; mastered you specifically what kind of operating system, whether there is a certain structure of an embedded system for embedded systems software development skills. So, people often follow the operating system or hardware architecture to subdivide embedded systems software engineers, such as embedded systems ( Linux ) engineers, embedded systems ( WINCE ) engineers, embedded systems ( ARM ) Engineer, Embedded Systems ( MIPS ) engineers.
    Many want to enter the field of embedded systems students will always experience a thought process, that is, at first did not know what needs to learn the knowledge, then just do not know why you want to learn this knowledge, then there does not know how to learn this knowledge, to the last is I do not know how to use that knowledge
    Simply put, to become an embedded systems software engineer, the basic requirements are:

  1. Have basic knowledge of computer hardware and software
  2. Master a programming language embedded systems
  3. Master a set of embedded system development tools
  4. Master an embedded operating system
  5. Master an embedded system architecture
  6. Mastered the field of embedded system applications industry basics

    This is the embedded systems software engineer minimum professional skill requirements.Specifically listed below
1. Become a qualified embedded system software engineers need to have the condition.
A clear direction of their development, and to develop a viable career plans total uncertainty of the future may be a lot of students do, so for professional learning simply do not plan or analysis, but holding the attitude of take things one step. This idea will tend to go through the subconscious hint yourself, leading eventually you become idle, mediocrity. If you are from now on, to determine their own direction of development, develop career plans and pay the actual action, then wherever you go in the future is technology, management direction, or start their own businesses, or even whether you are still in the future implementation of the plans you are now, You will receive capabilities.

2. With both theory and practice, the quality of both Mind and Body 
 A qualified embedded systems software engineer, is to really understand the nature of the embedded system software products and software product development spirit and essence of man. Grasp the embedded system software development language, applied linguistics tools to solve specific problems at work to complete the objectives and tasks of embedded systems software engineer's work. However, embedded systems software engineer this perspective, this is only the external things are not important, the nature of work for. Learning and the acquisition of embedded system software product development of theoretical knowledge, software development methodologies, and in practice comprehension, application software product analysis, design, implementation ideas to solve specific problems embedded system software product development, embedded real System Software Engineer necessary literacy.

3. In-depth understanding of the concept of embedded systems

  • From hardware to understand. Embedded system hardware is based on the traditional CPU peripheral devices, integrated into the CPU chip. Such as relatively high-end ARM class Intel Xscale architecture under the IXP network processor CPU integrates a PCI controller also integrates three months NPE network processor engine.
  • From software to understand. Embedded system is to customize the operating system kernel will be selected into the application, compile the kernel will be downloaded to the ROM medium. In the custom operating system kernel selected application group member is to complete the software " embedded " . For example WinCE kernel customization, there will be the appropriate choice, which is to Wordpad , PDF , MediaPlay so choose. If we choose, and in the CE starts, you can find these things in the interface.
  • To understand from the implementation. The software kernel or application to write to the file system and other things embedded system hardware platform ROM, on the realization of a truly "embedded " .

4. The embedded system, hierarchical manner known 
    Embedded system is divided into four layers, hardware layer, the driver layer, operating system layer and application layer:

  • Hardware layer: the entire embedded system simply, if you are right Microcontroller and very familiar with the interface piece and can C language and assembly language programming, then the hardware layer from embedded systems is relatively easy to do it. Embedded systems hardware layer is the foundation, but still rely on value-added software embedded systems.
  • Driver layer: To be able to read circuit diagrams should be able to very proficient in the operating system kernel, so that drivers written in a system call, the operating system does not monopolize the time slice, and lead to other tasks can not run. Of course, do not understand the operating system kernel architecture and real-time scheduling resistance, without a good driver programming style, just follow Introduction to add drivers, a lot of people can do, but this is just a junior embedded systems Driver Engineer lowest level.
  • Operating System layer: In the main work of the operating system layer or transplantation, and very few people have been written from the operating system.
  • Application layer: need to know programming languages, development tools, know that in Linux / Windows and other operating system how to program, function calls, master compiler and development environment.

5. Master operating systems, programming tools and programming environments
    Proficient in programming embedded systems is the most important requirement because programming embedded systems software engineers spend most of the time working, so the embedded system software engineer must be familiar with the operating system and embedded hardware architecture, able to understand such the contents of hardware registers a class, master-based board-level assembly and C programming, to understand the hardware interface circuit design, master environment based on embedded operating system applications and tool chain, such as Linux under the ARM tool chain and so on.

6. Skilled use of embedded system development platform 
    Embedded system development environment is that you learn how to develop an experimental platform for embedded systems projects. In the training, you can contact an experiment carried out with considerable practical effect of the teaching experiment platform, this teaching experiment platform must add specific development modules to form an actual development system. Having an experimental platform for embedded systems, embedded systems can greatly help beginners become familiar with and master the software embedded systems. Familiar with an actual embedded system development platform, and able to engage in the development of a specific project, is to get the actual conduct of embedded system software development experience to pass.

If you meet the above these requirements, you can become a qualified embedded systems software engineer. Of course, the really good software engineer for embedded systems is to rely on the accumulation of work can be formed, and this is also used to measure the actual working time of.