Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use C++ to write interrupts and events

Shulou Source: shulou.com Published: 2022-06-01 18:45:42 09月26日 Update

This article mainly explains "how to use C++ to write interruptions and events". The content in the article is simple and clear, and it is easy to learn and understand. let's study and learn "how to use C++ to write interruptions and events".

In general, when you use an object-oriented language, you will naturally think of encapsulation for some concepts, and the portability of the encapsulated class library will be more convenient. The ISR function has some particularities that can not be simply defined in C++.

1. Without changing the startup code:

If you do not want to change the startup code, the interrupt function cannot be defined directly in the C++ code, and you need to extend asm ("interrupt function name") in the language provided by GCC, for example:

Class Interrupt {/ * * External Interrupt Request 0 * / static inline void INT0_IRQHandler () asm ("_ _ vector_1"); / / other interrupts.}

Then you can define it in the cpp file: void Interrupt::INT0_IRQHandler () {/ code /}

two。 Write their own startup code, this is compared to NB, in general, some manufacturers will provide C startup code, you can refer to its rewritten C++ code. In addition to writing code for related functions such as data initialization and BSS initialization, the main task is to define interrupt vectors. There are two styles of interrupt vectors:

2-1. Interrupt vectors with jump statements, such as AVR, are defined as follows:

Typedef void ISR (void); struct ISR_Type {uint8_t code [2]; ISR* isr;}; _ _ attribute__ ((section (".isr _ vectors")) static const vectors [] = {{0x0C 0x94}, Interrupt::Reset}, {{0x0C 0x94}, Interrupt::INT0_IRQHandler}, / * other interrupt vectors * /}

2-2. Interrupt vector in the form of address, ARM series

_ _ attribute__ ((section (".isr _ vector"), used)) static IRQ_Handler* isrVector [] {_ estack, reset, it::NMI_Handler, / * other functions * /}

3. Define ISR in the H file (excluding interrupt functions using the asm () instruction) to increase compilation speed, but you need to precede each function with inline.

Thank you for your reading. the above is the content of "how to use C++ to write interrupts and events". After the study of this article, I believe you have a deeper understanding of the problem of how to use C++ to write interruptions and events. the specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Code function custom vector event situation learning content that is form file language encapsulation special manufacturer manufacturer address object idea instruction Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information Microsoft Huawei Xiaomi