
C++在嵌入式系统中的运用.ppt
60页C++技术大会C++在嵌入式系统中的运用何宗键同济大学软件学院 嵌入式系统研究中心 2005 . 11日程• 从嵌入式系统谈起• 让C++为嵌入式系统服务• C++的应用案例研究Question?• C++ keyword : volatilevolatile UINT1 * ptr = (UINT1 *) 0x1234; // 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%Mobile Phones, 45%Converged Devices, 3%Converged Devices, 3%Compressed Audio, 13%Compressed Audio, 13% Cable/DSL, 5%Cable/DSL, 5%Automotive, 5%Automotive, 5%DSC, 3%DSC, 3%DVD, 3%DVD, 3%Printers, 2%Printers, 2%DTV, 2%DTV, 2%Set top box, 2%Set top box, 2%PVR, 1%PVR, 1%POS, 1%POS, 1%Thin Clients, 1%Thin Clients, 1%Digital Camera, 1%Digital Camera, 1%IP Phones, 0%IP Phones, 0%WLAN Access, 0%WLAN Access, 0%ResRes Gateway, 0% Gateway, 0%Others , 13%Others , 13%近年来嵌入式设备市场机会近年来嵌入式设备市场机会1.6 Billion units1.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 LanguageJava / .NETC / C++8-bit, 16-bit单片机J2ME, .NET CF应用范围最广泛Assembly Language• 优点 – 灵活,操作硬件 – 运行高效,体积小• 缺点 – 代码冗长、复杂、容易出错 – 开发效率低 – 与硬件绑定• 应用领域 – 低端嵌入式系统 – 系统的Startup代码 – 对运行效率要求比较高的地方Java / .NET / 脚本语言• 优点 – RAD开发 – 简单易用• 缺点 – 运行效率低 – 消耗系统资源大• 应用领域 – 应用程序开发 – 图形界面 / 游戏开发C语言• Linux / UNIX世界的系统编程语言• 已经被广泛应用于嵌入式系统编程• 越来越多的取代汇编语言如何选择开发效率运行效率汇编语言虚拟机 / 脚本语言C C++Then…Why 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案例分析:失败的项目And…Why NOT C++??一些质疑• From comp.lang.c++ all the system objects from applications to interrupt handlers are defined as C++ classes. class Base { public:Base(){m_pArr = new int[1024];if ( ! m_pArr){// what should we do??}}int * m_pArr; };构造函数没有返回值class Base { public:Base(bool if ( ! m_pArr){ret = false;}ret = true;}int * m_pArr; }; void main() {bool bRet;Base b(bRet);if (bRet == false){cout = KArraySize){User::Leave(KErrArgument);}else{return iArray[aIndex];} } // 使用这个函数: TInt x, err; TRAP(err, x = GetL(5);) if(err != KErrNone) { // 输出错误信息等等 }int Get(int aIndex) {if(aIndex = KArraySize){throw new Exception();}else{return iArray[aIndex];} } // 使用这个函数: 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_leaveLLL();CleanupStack::PopAndDestroy(3);} Efficient exception handling rules• Rule 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 memory 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: 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。
