C++在嵌入式系统中的运用_第1页
C++在嵌入式系统中的运用_第2页
C++在嵌入式系统中的运用_第3页
C++在嵌入式系统中的运用_第4页
C++在嵌入式系统中的运用_第5页
已阅读5页,还剩55页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C+技术大会C+在嵌入式系统中的运用何宗键同济大学软件学院嵌入式系统研究中心2005 . 11日程从嵌入式系统谈起让C+为嵌入式系统服务C+的应用案例研究Question?C+ keyword : volatilevolatile 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 ass

2、ist the operation of equipment ,machinery or plants.对定义的理解:以应用为中心,以计算机技术为基础,并且软硬件是可裁剪的,适用于对功能、可靠性、成本、体积、功耗等有严格要求的专用计算机系统。 嵌入式系统的特点嵌入性:由于是嵌入到对象系统中,必须满足对象系统的环境要求,如物理环境(小型)、电气/气氛环境(可靠)、成本(价廉)等要求。专用性:软、硬件的裁剪性;满足对象要求的最小软、硬件配置等。(欧洲:Dedicated System)计算机系统:嵌入式系统必须是能满足对象系统控制要求的计算机系统。与上两个特点相呼应,这样的计算机必须配置有与对象系

3、统相适应的接口电路。嵌入式体系结构嵌入式操作系统嵌入式应用程序嵌入式硬件平台嵌入式处理器嵌入式外围设备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%近年来嵌入式设备市场机

4、会1.6 Billion units嵌入式机遇2004年4月 中国工程院院士 倪光南:我国嵌入式应用的规模排在世界第三。2002年,我国嵌入式软件市场销售额为118.56亿元,占到了同年我国软件总产值1100亿元的10%,而今天已经占到了软件产业的40%,按2003年我国软件市场实现销售额399.6亿元计算,约为13.6亿元。尽管如此,仍不容乐观。全球嵌入式软件市场在2002年时就超过了317亿美元。伴随着国内信息家电、手机、汽车电子等行业需求的加剧,嵌入式软件将扮演着越来越重要的角色。传统的IT设备应该逐渐转变为嵌入设备,这是一个大趋势。在这个大趋势中,软件的地位越来越重要。因此,嵌入式软件

5、在提升IT制造业的竞争力、优化产业结构的过程中起到了非常重要的作用,发展嵌入式软件势在必行。 我们在嵌入式软件技术方面与国外的差距不大。因为嵌入式软件是以应用为前提的,没有垄断的可能。谁掌握了应用,谁就能做得更好。硬件的发展嵌入式微处理器X86工控领域。发展方向:小型、低功耗ARM速度、功能扩展解决方案手机、PDA、数码相机、MP3、MP4、游戏机、DVD嵌入式软件开发人员嵌入式领域电子技术工程师软件工程师熟悉硬件技术。对软件技术与工程不太了解熟悉软件工程思想与应用程序开发。对硬件设计,硬件原理不熟悉嵌入式编程的一些特点资源受限处理器速度:算法的效率内存少:所以必须非常注意内存的泄漏I/O及外

6、设手机:显示屏幕,没有鼠标外设的多样性编程语言嵌入式系统Assembly LanguageJava / .NETC / C+8-bit, 16-bit单片机J2ME, .NET CF应用范围最广泛Assembly Language优点灵活,操作硬件运行高效,体积小缺点代码冗长、复杂、容易出错开发效率低与硬件绑定应用领域低端嵌入式系统系统的Startup代码对运行效率要求比较高的地方Java / .NET / 脚本语言优点RAD开发简单易用缺点运行效率低消耗系统资源大应用领域应用程序开发图形界面 / 游戏开发C语言Linux / UNIX世界的系统编程语言已经被广泛应用于嵌入式系统编程越来越多的

7、取代汇编语言如何选择开发效率运行效率汇编语言虚拟机 / 脚本语言CC+ThenWhy C+?Bjarne Stroustrup如是说C+s emphasis on general features (notably classes) has been its main strengthC+ 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 app

8、lications areas using a common set of facilities.C+包含的一些思想Better C抽象数据类型ADT面向对象的思想OODOOP范型编程 GPtemplate案例分析:失败的手机项目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 unsu

9、itable 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 CC+ yields bigger and slower code because:Unnecessary class & object in codeHas a lot of subtleties that are invisible to the ne

10、wbie (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

11、 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-overhea

12、d 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 do

13、nt 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 allo

14、cated. 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 reus

15、ability 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 instructi

16、on. C+ advantages: Objects allow us to add functionality and re-arrange interfaces easily, quickly, and with little fear of side-effects. SummaryAll 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 be

17、tter 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. 在面向对象中的重要程度实现的开销(内存,速度,复杂性)InstanceClassSingle Inherit

18、ance Function Overload Default ArgumentInline FunctionReference TypePolymorphismExceptionConstructor / DestructorTemplateMultiple Inheritance Copy Constructor对标准化的努力Embedded C+ Technical Committee成立于上世纪90年代,定制EC+规范标准C+的一个子集P. J. Plauger撰文/ec2plus如何让C+为嵌入式服务合适的场合,合适的工具扬长避短,发挥C+语言的优势为嵌入式服务规范 / 工具 / 产业

19、日程从嵌入式系统谈起让C+为嵌入式系统服务C+的应用案例研究C+在嵌入式中的应用实例Symbian OS内存管理图形系统Windows CE GWESDirect FBSymbianSymbian 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 ob

20、ject-oriented; all the system objects from applications to interrupt handlers are defined as C+ classes. class Basepublic: Base() m_pArr = new int1024; if ( ! m_pArr) / what should we do? int * m_pArr;构造函数没有返回值class Basepublic: Base(bool &ret) m_pArr = new int1024; if ( ! m_pArr) ret = false; ret =

21、true; int * m_pArr;void main() bool bRet; Base b(bRet); if (bRet = false) cout construct failed endl; class Basepublic: Base(bool &ret) m_pArr = new int0 x10000000; if ( ! m_pArr) ret = false; ret = true; Base() cout destructor endl; int * m_pArr;共识:对象的构造失败的唯一方法是在构造函数中抛出异常 但是异常处理会显著的增加代码的体积。统计数字:5%

22、- 20%Symbian中的方案:关闭编译器的异常处理开关Symbian中构造函数失败怎么办?Symbian 异常处理TInt GetL(TInt aIndex) if(aIndex = KArraySize) User:Leave(KErrArgument); else return iArrayaIndex; / 使用这个函数:TInt x, err;TRAP(err, x = GetL(5);)if(err != KErrNone)/ 输出错误信息等等int Get(int aIndex) if(aIndex = KArraySize) throw new Exception(); el

23、se return iArrayaIndex; / 使用这个函数:try x = GetL(5);catch(Exception e)/输出错误信息等等清理栈Cleanup Stackvoid FunL() HBufC * wbuf = HBufC:NewL(10); CleanupStack:PushL(wbuf); HBufC * wbuf2 = HBufC:NewL(20); CleanupStack:PushL(wbuf2); RFs fs; User:LeaveIfEror( fs.Connect(); CleanupClosePushL(fs); do_sth_maybe_leav

24、eLLL(); CleanupStack:PopAndDestroy(3); Efficient exception handling rulesRule 1: Functions that can leave should have a letter L at the end of their names.void MyFunctionL() iMember = new (ELeave) CMember; iValue = AnotherFunctionL(); User:LeaveIfError(iSession.Connect(); Rule 2: While allocating me

25、mory on the heap, if the pointer is an automatic variable, it should be pushed on the cleanup stack.CObject* object = CObject:NewL(); CleanupStack:PushL(object); object-LeavingFunctionL(); CleanupStack:PopAndDestroy(object); Efficient exception handling rules: ContinuedRule 3: All the codes that mig

26、ht 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

27、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.dl

28、l,GDI32.dll的一个子集使用Win32 API编程/ 得到窗口DCHDC 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);/ 释放窗口DCRelease

29、DC(hWnd, dc);/ 得到窗口DCHDC 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;/ 得到窗口DCHDC dc = GetDC(hWnd);/ 创建红色,虚线画笔HPEN hpen = CreatePen(PS_

30、DASH, 1, RGB(255, 0, 0);/ 在窗口的DC上画线LineTo(dc, 100, 100);HPEN CreatePen() CPen * pRet = new CPen(); if (pRet) return (HPEN)pRet;/ 得到窗口DCHDC 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-Line

31、To();DC:LineTo() / perform drawing taskGDI Object ModelOpen source project supported by convergence GermanyLicensed under GNU GPLProvides graphic API set and integrated window systemWorks on a frame buffer device and provides the mechanism to use the hardware acceleration effectively.http:/Architect

32、ureDirectFB consists of the followings:Core API ModuleGeneric GFX DriverGFX Drivers for Specific HardwareTo 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 ava

33、ilableIf yes, it handovers to the GFX driverIf not it uses software rendering engineHardwareUser LevelDevice DriversDirectFBApplicationDirectFBDirectFB Core API ModuleGFX driversFrame BufferDriverDisplay Unit2D GraphicsHardwareGenericGFX DriverIDirectFB *dfb = NULL;IDirectFBSurface *primary = NULL;i

34、nt 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 argvDirectFBInit (&argc, &argv);/ Create the super interface.DirectFBCreate (&dfb);/ go fullscreendfb-SetCoop

35、erativeLevel (dfb, DFSCL_FULLSCREEN);/ create primary surfacedsc.flags = DSDESC_CAPS;dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING;dfb-CreateSurface( dfb, &dsc, &primary );primary-GetSize (primary, &screen_width, &screen_height);/ Clear the screen by filling a rectangleprimary-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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论