资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,#,日程,从嵌入式系统谈起,让,C+,为嵌入式系统服务,C+,的应用案例研究,Question?,C+keyword:volatile,volatile,UINT1*ptr=(UINT1*)0 x1234;,/Wait for register to become non-zero.,while(*ptr=0),;/pooling,/Do something else.,嵌入式系统,IEEE,(国际电气和电子工程师协会)的定义,Devices used to control,monitor or assist the operation of equipment,machinery or plants.,对定义的理解:,以应用为中心,以计算机技术为基础,并且软硬件是可裁剪的,适用于对功能、可靠性、成本、体积、功耗等有严格要求的,专用计算机系统,。,嵌入式系统的特点,嵌入性:由于是嵌入到对象系统中,必须满足对象系统的环境要求,如物理环境(小型)、电气,/,气氛环境(可靠)、成本(价廉)等要求。,专用性:软、硬件的裁剪性;满足对象要求的最小软、硬件配置等。(欧洲:,Dedicated System,),计算机系统:嵌入式系统必须是能满足对象系统控制要求的计算机系统。与上两个特点相呼应,这样的计算机必须配置有与对象系统相适应的接口电路。,嵌入式体系结构,嵌入式操作系统,嵌入式应用程序,嵌入式硬件平台,嵌入式处理器,嵌入式外围设备,Mobile Phones,45%,Converged Devices,3%,Compressed Audio,13%,Cable/DSL,5%,Automotive,5%,DSC,3%,DVD,3%,Printers,2%,DTV,2%,Set top box,2%,PVR,1%,POS,1%,Thin Clients,1%,Digital Camera,1%,IP Phones,0%,WLAN Access,0%,Res Gateway,0%,Others,13%,近年来嵌入式设备市场机会,1.6 Billion units,嵌入式机遇,2004,年,4,月 中国工程院院士 倪光南:,我国嵌入式应用的规模排在,世界第三,。,2002,年,我国嵌入式软件市场销售额为,118.56,亿元,占到了同年我国软件总产值,1100,亿元的,10%,,而今天已经占到了,软件产业的,40%,,按,2003,年我国软件市场实现销售额,399.6,亿元计算,约为,13.6,亿元。尽管如此,仍不容乐观。全球嵌入式软件市场在,2002,年时就超过了,317,亿美元。伴随着国内信息家电、手机、汽车电子等行业需求的加剧,嵌入式软件将扮演着越来越重要的角色。,传统的,IT,设备应该逐渐转变为嵌入设备,,这是一个大趋势。在这个大趋势中,软件的地位越来越重要。因此,嵌入式软件在提升,IT,制造业的竞争力、优化产业结构的过程中起到了非常重要的作用,发展嵌入式软件势在必行。,我们在嵌入式软件技术方面与国外的差距不大,。因为嵌入式软件是以应用为前提的,,没有垄断的可能,。谁掌握了应用,谁就能做得更好。,硬件的发展,嵌入式微处理器,X86,工控领域。发展方向:小型、低功耗,ARM,速度、功能扩展,解决方案,手机、,PDA,、数码相机、,MP3,、,MP4,、游戏机、,DVD,嵌入式软件开发人员,嵌入式领域,电子技术工程师,软件工程师,熟悉硬件技术。对软件技术与工程不太了解,熟悉软件工程思想与应用程序开发。对硬件设计,硬件原理不熟悉,嵌入式编程的一些特点,资源受限,处理器速度:算法的效率,内存少:所以必须非常注意内存的泄漏,I/O,及外设,手机:显示屏幕,没有鼠标,外设的多样性,编程语言,嵌入式,系统,Assembly Language,Java/.NET,C/C+,8-bit,16-bit,单片机,J2ME,.NET CF,应用范围最广泛,Assembly Language,优点,灵活,操作硬件,运行高效,体积小,缺点,代码冗长、复杂、容易出错,开发效率低,与硬件绑定,应用领域,低端嵌入式系统,系统的,Startup,代码,对运行效率要求比较高的地方,Java/.NET/,脚本语言,优点,RAD,开发,简单易用,缺点,运行效率低,消耗系统资源大,应用领域,应用程序开发,图形界面,/,游戏开发,C,语言,Linux/UNIX,世界的系统编程语言,已经被广泛应用于嵌入式系统编程,越来越多的取代汇编语言,如何选择,开发效率,运行效率,汇编语言,虚拟机,/,脚本语言,C,C+,ThenWhy C+?,Bjarne Stroustrup,如是说,C+s emphasis on general features(notably classes)has been its main strength,C+will not be a“Windows language”or a“web language”or even an“embedded systems language.”It will be a general-purpose language that supports those applications areas using a common set of facilities.,C+,包含的一些思想,Better C,抽象数据类型,ADT,面向对象的思想,OOD,OOP,范型编程,GP,template,案例分析:失败的手机项目,AndWhy NOT C+?,一些质疑,From,comp.lang.c+,&,comp.arch.embedded,C+is slow,C+generates bloated code,C+is complicated,C+doesnt offer much to systems programmers,Advanced C+features are unsuitable for systems work,C+code isnt portable,一些精彩对话,This is a great topic that usually generates a lot of controversy.,“Procedural code in C+is essentially the same as in C,C+yields bigger and slower code because:,Unnecessary class&object in code,Has a lot of subtleties that are invisible to the newbie(copy constructors,inline expansion,etc.),C+_can_ be equal in speed and code size as asm or plain old C.Used improperly,all of these language can create a mess:-),一些精彩对话(续),The right tool for the right job,thats my motto.You can get a lot more work done in C+than you can in C(and a hell of a lot more than in assembly)if you know what youre doing.If you need assembly,use it.,YOU DONT HAVE TO USE THE PARTS OF THE LANGUAGE THAT YOU DONT NEED.,Even the new/advanced features of C+,such as templates,exceptions,and run-time type information(RTTI)are designed to meet the 0-overhead rule.,一些精彩对话(续),it depends,sometimes time critical means the time to market and not the timing of your ISR.,Most C+features have no impact on code size or on speed.,A _good_ C+programmer knows,and I have seen device drivers written in C+.For the _average_ programmer,C+and real-time usually dont mix.,一些精彩对话(续),Dynamic Memory Allocation!STL makes use of dynamic memory allocation.For systems that have to run for long periods of time or that are mission critical,they can not tolerate running out of memory or having the heap fragmented to point where objects of size N can no longer be allocated.,We moved to,C+,for 80C186 development over 5 years ago and havent regretted it.Each project has allowed us to refine our system of objects.Typical system:256kB RAM,512kB ROM(Flash,now),20MHz instruction clock,一些精彩对话(续),C+,is not intrinsically more reusable.You have to design with reusability in mind.,C+,is not intrinsically safer.You have to design with safety in mind.,C+,disadvantages:,Bloat:,C+,can be a RAM and ROM hog.,Speed:We have yet to make a fast IO class.We dont even try anymore.A couple of levels of indirection can end up costing 20 x a simple outp machine instruction.,C+,advantages:,Objects allow us to add functionality and re-arrange interfaces easily,quickly,and with little fear of side-effects.,Summary,All the mantras of good software development apply.and everything else in Code Complete or other good texts is mandatory.,C+,will not make a programmer better or worse.It*can*help you take your design to a higher level.,Embedded software is a rare art.,C+,(and especially STL)was not designed for embedded work.You may have to beat the compiler into submission,but I think its worth the fight.,在面向对象中的重要程度,实现的开销(内存,速度,复杂性),Instance,Class,Single Inheritance,Function Overload,Default Argument,Inline Function,Reference Type,Polymorphism,Exception,Constructor/Destructor,Template,Multiple Inheritance,Copy Constructor,对标准化的努力,Embedded C+Technical Committee,成立于上世纪,90,年代,定制,EC+,规范,标准,C+,的一个子集,P.J.Plauger,撰文,OS,内存管理,图形系统,Windows CE GWES,Direct FB,Symbian,Symbian OS is an operating system optimized for,mobile terminals,such as communicators and phones,and it is the underlying system upon which the Series 60 Platform has been built.,Symbian OS is thoroughly object-oriented;all the system objects from applications to interrupt handlers are defined as C+classes.,class Base,public:,Base(),m_pArr=new int1024;,if(!m_pArr),/what should we do?,int*m_pArr;,;,构造函数没有返回值,class Base,public:,Base(bool&ret),m_pArr=new int1024;,if(!m_pArr),ret=false;,ret=true;,int*m_pArr;,;,void main(),bool bRet;,Base b(bRet);,if(bRet=false),cout construct failed endl;,class Base,public:,Base(bool&ret),m_pArr=new int0 x10000000;,if(!m_pArr),ret=false;,ret=true;,Base(),cout destructor LeavingFunctionL();,CleanupStack:PopAndDestroy(object);,E,fficient exception handling rules:Continued,Rule 3:All the codes that might fail,should be taken out of the constructor,and put into a constructL()function.This will be called after the constructor has completed.This is,two-phase construction,.,/Phase#1,MyObject:MyObject(),/Phase#2,void MyObject:ConstructL(),MyMemberData*data=new(ELeave)MyMemberData();,/Put both phases together in one function.,MyObject*MyObject:NewL(),MyObject*self=new(ELeave)MyObject();,CleanupStack:PushL(self);,self-ConstructL();,CleanupStack:Pop(self);,return self;,图形系统,人机交互的发展:,CUI-GUI-NUI(?),包含的内容:图形,窗口,事件,嵌入式图形系统必须有以下几个特点:,占用资源少,高性能,高可靠性,可配置,Windows CE,的图形系统,桌面,Windows USER32.dll,,,GDI32.dll,的一个子集,使用,Win32 API,编程,/,得到窗口,DC,HDC dc=GetDC(hWnd);,/,创建红色,虚线画笔,HPEN hpen=CreatePen(PS_DASH,1,RGB(255,0,0);,/,设置,DC,的画笔为新创建的画笔,同时保留旧的画笔句柄,HPEN hOld=(HPEN)SelectObject(dc,hpen);,/,在窗口的,DC,上画线,LineTo(dc,100,100);,/,把,DC,的画笔属性重新设置为旧的画笔,SelectObject(dc,hOld);,/,删除画笔,DeleteObject(hpen);,/,释放窗口,DC,ReleaseDC(hWnd,dc);,/,得到窗口,DC,HDC dc=GetDC(hWnd);,/,创建红色,虚线画笔,HPEN hpen=CreatePen(PS_DASH,1,RGB(255,0,0);,/,在窗口的,DC,上画线,LineTo(dc,100,100);,HDC GetDC(HWND hWnd),CWindow*This=(CWindow*)hWnd;,This-GetDC();,CWindow:GetDC(),return this-m_hDC;,/,得到窗口,DC,HDC dc=GetDC(hWnd);,/,创建红色,虚线画笔,HPEN hpen=CreatePen(PS_DASH,1,RGB(255,0,0);,/,在窗口的,DC,上画线,LineTo(dc,100,100);,HPEN CreatePen(),CPen*pRet=new CPen();,if(pRet),return(HPEN)pRet;,/,得到窗口,DC,HDC dc=GetDC(hWnd);,/,创建红色,虚线画笔,HPEN hpen=CreatePen(PS_DASH,1,RGB(255,0,0);,/,在窗口的,DC,上画线,LineTo(dc,100,100);,LineTo(HDC,int,int),DC*pDC=(DC*)HDC;,pDC-LineTo();,DC:LineTo(),/perform drawing task,GDI Object Model,Open source project supported by,convergence Germany,Licensed under GNU GPL,Provides graphic API set and integrated window system,Works on a frame buffer device,and provides the mechanism to,use the hardware acceleration,effectively.,www.directfb.org/,Architecture,DirectFB consists of the followings:,Core API Module,Generic GFX Driver,GFX Drivers for Specific Hardware,To bring out the best performance on a specific graphics hardware GFX Drivers for the hardware should be written.,Generic GFX Driver checks whether the hardware acceleration by a GFX driver is available,If yes,it handovers to the GFX driver,If not it uses software rendering engine,Hardware,User Level,Device Drivers,DirectFB,Application,DirectFB,DirectFB Core API Module,GFX drivers,Frame Buffer,Driver,Display Unit,2D Graphics,Hardware,Generic,GFX Driver,IDirectFB*dfb=NULL;,IDirectFBSurface*primary=NULL;,int screen_width =0,screen_height=0;,int main(int argc,char*argv),/A surface description is needed to create a surface.,DFBSurfaceDescription dsc;,/Initialize DirectFB passing argc and argv,DirectFBInit,(,/Create the super interface.,DirectFBCreate,(,/go fullscreen,dfb-SetCooperativeLevel(dfb,DFSCL_FULLSCREEN);,/create primary surface,dsc.flags=DSDESC_CAPS;,dsc.caps =DSCAPS_PRIMARY|DSCAPS_FLIPPING;,dfb-,CreateSurface,(dfb,&dsc,&,primary,);,primary-GetSize(primary,/Clear the screen by filling a rectangle,primary-,FillRectangle,(primary,0,0,screen_width,screen_height);,/Draw a horizontal line in the middle of the screen.,primary-,DrawLine,(primary,screen_height/2,screen_width-1,screen_height/2);,/Now flip the whole surface to make things visible.,primary-,Flip,(primary,NULL,0);,primary-,Release,(primary),dfb-,Release,(dfb);,DirectFB Interface,#define,DECLARE_INTERFACE,(IFACE)typedef struct _#IFACE IFACE;,#define,DEFINE_INTERFACE,(IFACE,IDATA.),struct,_#IFACE ,void *priv;/,接口数据,int magic;/Magic Number,DFBResult(*AddRef)(IFACE*thiz);,DFBResult(*Release)(IFACE*thiz);,IDATA ;,DEFINE_INTERFACE,(IDirectFB,DFBResult(*SetCooperativeLevel)(,IDirectFB *thiz,DFBCooperativeLevel level,);,DFBResult(*CreateSurface)(,IDirectFB *thiz,const DFBSurfaceDescription *desc,IDirectFBSurface *ret_interface,);,Compare:COM Object Model,QueryInterface,AddRef,Release,Read,QueryInterface,AddRef,Release,Read,Implementation,Implementation,Implementation,Implementation,CStream,BYTE*data,INT count,IStreamVtbl,IStream,IStreamVtbl,Compare:DirectFB Object Model,IDirectFB,void*priv,Int magic,AddRef,Release,SetVideoMode,Implementation,Implementation,Implementation,Implementation,CreateSurface,IDirectFB_data,int ref;,CoreDFB*core;,面向接口编程,而不是针对实现编程。,舍其“形”而取其“神”,案例总结,Symbian,案例,体验,C+,独特的语言魅力,可控性、自由度、系统编程、,Win CE GWES&DirectFB,C+,语言对嵌入式软件的改进,面向对象的思想,总结,嵌入式软件开发者,是否可以用,C+,重构您的系统?,桌面,/,企业应用开发者,是否对嵌入式软件开发有所了解?,谢谢!希望能与各位交流,E-Mail:hezongjian,
展开阅读全文