Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ to write embedded code

2025-03-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use C++ to write embedded code". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The startup code of embedded programs, that is, startup code, is generally written in assembly, and the code is efficient, but it is not convenient to integrate with C++ code. For example, if you want to define the interrupt function directly in the cpp file, you need asm statements.

Here is the startup process of the avr chip:

The startup of avr is divided into several stages, which are shown in the form of .init segments. N is called from 0 to 9 in turn. This process can be referred to the documentation of avr-libc.

Define a function to initialize the stack and R1 in the .init2 section, as follows:

_ _ attribute__ ((section (".init2"), naked)) void _ _ init_stack ();... void _ _ init_stack () {CPU.SP = 0x10FF; / / not 0x1100 asm volatile ("clr R1");}

Define the main function:

_ _ attribute__ ((section (".init9")) void main ();... void main () {/ / code here}

Declare interruption

Set link parameters: add the option-nostartfiles-nodefaultlibs-nostdlib when connecting, the purpose is not to let the connector link the default startup code and interrupt vector, but your code may need the default library in gcc, then you also need to add the library option-lavr-stl-lc-lm-latmega128a-lprintf_flt-lscanf_flt-lgcc, note: there are sequence requirements.

This is the end of the introduction of "how to write embedded code using C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report