How to learn embedded linux?

Transferred from Mr. Wei Dongshan. Check it out from time to time to motivate yourself to learn linux well.

As a newcomer, how to learn embedded Linux? I've been asked so many times, so I wrote this article to answer it.
   Before learning embedded Linux, there must be a foundation of C language. It doesn't matter whether the assembly foundation is or not (there are just a few assembly instructions, you can use them at a glance).
How much do you need to learn the C language? Of course, the more familiar you are, the better. If you are unfamiliar, you must also have basic skills. For example, write an array sorting, input numbers and so on.
The only way to learn C language is to write more programs and practice more. It doesn't matter if there is a compilation error, you can solve it yourself; In the past, I used
VC to practice C language, and I often tried to write some C language competition topics. They are pure C, pure math, and pure logic topics, and they don't involve interfaces. They
are very suitable for tempering your programming skills.     Back to the topic, first of all we have to understand what your purpose is. Generally speaking, the so-called embedded Linux can be divided into two parts: the underlying system and application development.     If you want to do application development, then you should learn C language, data structure, JAVA and so on. There is nothing special to pay attention to in embedded application development and application development on PC . Maybe you say some optimizations are to be done on the embedded, yes, there are optimizations, but unoptimized programs are no different than program development on a PC. Plus, when you have the ability to optimize, you don't have to ask this question anymore. Specific to a certain example, for example, to develop the interface, we use VC on the PC; in embedded Linux, we may use QT or Android. At this time, you should learn QT and Android programming. But the foundation is still C or JAVA, and on this basis, you are familiar with their interfaces. If you have studied VC, you should spend time to understand those classes and controls.   If your purpose is to learn the underlying system, this is my specialty, but I can say something.   Before answering this question, let me answer: Many people ask me, should I learn to drive or learn to apply? 
    









  I can only say that it is based on interest, and the driver and the application are not completely separated. 
1. The driver we are talking about is not limited to the operation of the hardware, but also the principle of the operating system, the sleep and wake-up scheduling of the process and other concepts. 
   If you want to write a good application and better solve the problems encountered by the application, you should understand this knowledge 
. 2. The threshold for making applications is low, especially the current ANDROID, pure JAVA. Personally think that the development path of making applications is business proficiency.
   For example, in the communication industry, IPTV industry, and mobile phone industry, you understand the needs of the industry. Therefore, as a leader, most of them are applied.
3. To be a driver, in fact, I don't want to call it "doing a driver", but want to call it "doing a low-level system". If you do it well, it will kill all industries. I have worked for several years and have
   done mobile phones, IPTV, and conference TV, but these products are no different to me because I only do the bottom layer. Their business has nothing to do with me.
   When there is a problem with the application, and they can't solve it, I will give them ideas from the kernel perspective and provide them with tools. 
   To do the development direction of the bottom, I personally think that it is a technical expert. 
4. In fact, there is no boundary between doing the bottom layer or doing the application. If you have the bottom layer experience, and then do the application, you will feel very at ease.
   With business experience, you can understand the bottom layer again, and you can form a team soon.    Back to the question of how to learn. What does the embedded Linux underlying system contain? Don't worry, just give an example and you'll know. 1. When the computer is turned on, who displayed those interfaces? Is the BIOS, what does it do? Some self-tests, then read into windows from the hard drive, and boot it.    Similarly, this BIOS corresponds to the bootloader in Embedded Linux. This bootloader is going to read the Linux kernel from Flash and start it.
   



2. What is the purpose of starting Windows? Of course it's online chat or something. Where are these Internet and chat tools?
   on the C drive and D drive. Therefore, Windows must first identify the C drive and D drive. In Linux we call it the root file system.
3. Windows can recognize the C drive and D drive, so it must be able to read and write the hard disk. This involves something called a driver. Of course, not only the hard disk, but also the network card, USB and so on.
   Embedded Linux can read and execute application programs from Flash, and there must be a driver for Flash, of course, not only Flash.    Let's talk about it first, the embedded LINUX contains four large blocks: bootloader, kernel, driver, and root file system. 1. Bootloader:     It is a slightly complicated bare-board program. But it is not easy to understand and write this bare board program well. The useful tools under Windows weaken our programming ability. Many people use ADS and KEIL when they play embedded. Can you answer these questions? 1. Once powered on, where does the CPU fetch instructions for execution?    Answer: Generally, the command is from Flash. 2. But Flash can only be read and cannot be written directly. If I use global variables, where are these global variables?    Answer: Global variables should be in memory 3. So who puts global variables in memory?    A: Friends who have used ADS and KEIL for a long time, can you answer them? This requires "relocation". In ADS or KEIL, the relocation code is written for you by the company that makes the tools.        Have you ever read it? 4. With such a large memory, how do I know to read "the content originally stored in the Flash" to "where to go" in the memory?
      












   答:这个地址用"链接脚本"决定,在ADS里有scatter文件,KEIL里也有类似的文件。但是,你去研究过吗?
5. 你说重定位是把程序从Flash复制到内存,那么这个程序可以读Flash啊?
   答:是的,要能操作Flash。当然不仅仅是这些,还有设置时钟让系统运行得更快等等。
   
   先自问自答到这里吧,bootloader这一个裸板程序,其实有3部分要点:
1. 对硬件的操作
2. 对ARM体系处理器的了解
3. 程序的基本概念:重定位、栈、代码段数据段BSS段什么的。
   
    对硬件的操作,需要看原理图、芯片手册。这需要一定的硬件知识,不求你能设计硬件,但是至少能看懂; 不求能看懂模拟电路,
但是要能看懂数字电路。这方面的能力我是在学校里学到的,微机原理、数字电路这2本书(书名忘了)就足够了。但是我怀疑你有无耐
心把这2本书看完。我不知道现在有没有更快捷的书。想速成的话,就先放掉这块吧,不懂就问GOOGLE、发贴。
另外,芯片手册是肯定要读的,别去找中文的,就看英文的。开始是非常痛苦,以后就会发现那些语法、词汇一旦熟悉后,
读任何芯片手册都很容易。
对ARM体系处理器的了解, 看杜春蕾的<ARM体系架构与编程>吧,里面讲有汇编指令,有异常模式、MMU等。也就这3块内容需要你了解。
程序的基本概念,王道当然是去看编译原理了。可惜,这类书绝对是天书级别的。劝你若非超级天才还是别去看了。就看我写的
<嵌入式Linux应用开发完全手册>和第1期视频吧,别担心,不用花钱。照着视频把硬件相关的实验做了,这些概念就清楚了。我还没有
发现第2套讲这些概念的书或视频,允许我盲目吹嘘一回。

对于bootloader,我学习时是先看了<ARM体系架构与编程>,然后自己写程序把各个硬件的实验都做了一遍,比如GPIO、时钟、
SDRAM、UART、NAND。把它们都弄清楚了,组台在一起就很容易看懂u-boot了
总结一下,看懂硬件原理图、看芯片手册,这需要你自己去找资料。剩下的,就按<嵌入式Linux应用开发完全手册>和第1期视频的章
节目录去学习吧。


二、内核:
想速成的人,先跨过内核的学习,直接学习怎么写驱动。
想成为高手,内核必须深刻了解。注意,我说的是了解,我没奢望去写出一个内核。
要对里面的调度机制、内存管理机制、文件管理机制等等有所了解。
推荐两本书:
1. 通读<linux内核完全注释>,请看薄的那本(浮燥的社会讲求速度, 呵), 
2. 选读<Linux内核情景分析>, 想了解哪一块就读哪一节


三、驱动:
驱动包含两部分:硬件本身的操作、驱动程序的框架。
又是硬件,还是要看得懂原理图、读得懂芯片手册,多练吧。

说到驱动框架,有一些书介绍一下。LDD3,即<Linux设备驱动>,老外写的那本,里面介绍了不少概念,值得一读。但是,它的作用
也就限于介绍概念了。我基本上是入门之前用它来熟悉一下概念,入门后就扔掉了。
驱动方面比较全的介绍,应该是宋宝华的<linux设备驱动开发详解>了,老实说我只看过目录,有不少人说好,这里推荐一下。
要想深入了解某一块,<Linux内核情景分析>绝对是超5星级推荐。你别指望把它读完,1800多页,上下两册呢。我是某一块不清楚
时,就去翻一下它。任何一部分,这书都可以讲上2、3百页,非常详细。并且是以某个目标来带你分析内核源码。它以linux 2.4为例,
但是原理相通,同样适用于其它版本的linux。

还有没有其他介绍?呵呵,当然有了,韦东山Linux视频第2期。<嵌入式Linux应用开发完全手册>里对驱动讲得不多,不够深入。
于是我录制了这期视频。不仅仅教你怎么写怎么改驱动,还教你为什么这样写这样改驱动。
每一个驱动都是现场编写: 
1. 用绘图板画图讲解──相当于学校里老师在黑板上画图讲解,很直观
   绝对不是对着PPT念。
2. 用source insight当场写程序,从第1行开始写,每一课都是这样。我讲了20多个驱动,就写了20多个程序。
3. 写完就编译、测试。
4. 很全面,字符设备驱动、块设备、网卡驱动3大类齐全,硬件介绍、驱动框架分析、测试3大类齐全。
    培训机构里教的内容,远不及这期视频丰富。我在多个培训机构讲过课,从没看到哪个老师敢每一课都当场讲解当场编写代码
当场测试,除我之外!也没看到哪个培训机构讲完这些内容──因为时间不够,讲完起码要一个月,但是这部分基本只有2周授课时间。

把你手上的开发板所涉及的硬件,都去尝试写一个驱动吧。有问题就先"痛苦地思考",思考的过程中你会把很多不相关的知识
串联起来,最终贯通。


四、根文件系统:
大家有没有想过这2个问题:
1. 对于Linux做出来的产品,有些用作监控、有些做手机、有些做平板。那么内核启动后,挂载根文件系统后,应该启动哪一个应用程序呢?
   答:内核不知道也不管应该启动哪一个用户程序。它只启动init这一个应用程序,它对应/sbin/init。
       显然,这个应用程序就要读取配置文件,根据配置文件去启动用户程序(监控、手册界面、平板界面等等)
       这个问题提示我们,文件系统的内容是有一些约定的,比如要有/sbin/init,要有配置文件
2. 你写的hello,world程序,有没有想过里面用到的printf是谁实现的?
   答:这个函数不是你实现的,是库函数实现的。它运行时,得找到库。
       这个问题提示我们,文件系统里还要有库。
       
       简单的自问自答到这里,要想深入了解,可以看一下busybox的init.c,就可以知道init进程做的事情了。
       当然,也可以看<嵌入式Linux应用开发完全手册>里构建根文件系统那章。


说一下我的学习经历吧。
1. 我在学校时读的是物理电子专业,其实课程里没有教怎么设计电路,只是教了些电子电路方面的知识。PCB的设计
   是在实验室里自学的,只设计过2层板,现在忘记得差不多了。但是保留了看原理图、看芯片手册的能力。
2. 选修了软件学位,对软件设计挺感兴趣,但是也只是学了C语言、数据库而已。凭着兴趣做了不少竞赛题。没能力去
   参加竞赛,但是把C语言练得很扎实。
3. 在实验室、在第1家公司,就是设计些简单的PCI卡,写一下windows的驱动程序
4. 在第2家公司,用51单片机做车载电话,开始走上纯软件的道路。
5. 开始感到单片机的不足,辞职半年闭门学Linux,从red hat怎么操作开始。步骤就是先看<ARM体系架构与编程>,
   再自己写裸板程序操作硬件,接着到分析u-boot。同时看<linux内核完全注释>,对LINUX框架有所了解。
   在写裸板时,建议各位加强对中断的理解,内核就是用中断来完成各种功能的。
6. 分析完u-boot,就开始进行简单的驱动编程了,这时候,能力还很弱。
7. 开始去中兴上班,工作2年,编写各类驱动、解决各类问题(驱动问题、帮助定位应用问题),能力得到煅炼。

    
    总结一下:
1. 硬件方面的书: 微机原理、数字电路,高校里的教材。毕业多年,忘名了。
2. Linux方面的书:
    <ARM体系架构与编程>
       <嵌入式Linux应用开发完全手册>
    <Linux设备驱动>,老外写的那本
    <linux设备驱动开发详解>
    <linux内核完全注释>
    <Linux内核情景分析>
3. 视频:
韦东山Linux视频第1期(基于S3C2440录制): ARM实验,u-boot,文件系统,初级驱动
韦东山Linux视频第1期(基于S3C6410录制): 裸板程序
韦东山Linux视频第2期: 高级驱动

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325479005&siteId=291194637