简体   繁体   English

从直接调用的静态方法内部获取类对象指针

[英]Get class object pointer from inside a static method called directly

I have the following class, for example in a header: 我有以下类,例如在标题中:

class Uart
  {
  public:
    Uart (int ch, int bd = 9600, bool doinit = false);
    ......
    static void isr (void);
   }

The idea is this class represents USART hardware, the same way as SPI, RTC etc and I set the address of static member isr as interrupt vector routine during runtime. 想法是此类表示USART硬件,与SPI,RTC等相同,我在运行时将静态成员isr的地址设置为中断向量例程。
For example like this 像这样

extern "C"
{
  void
  Uart::isr (void)
  {
    if ( USART1->SR & USART_SR_RXNE) //receive
      {
    short c = USART2->DR;
    USART1->DR = c;
    USART1->SR &= ~USART_SR_RXNE;
    ;
      }
    else if ( USART1->SR & USART_SR_TC) //transfer
      {
    USART1->SR &= ~USART_SR_TC;
      }
  }
}

And set it as a interrupt vector, for example 并将其设置为中断向量,例如

_vectors_[USART1_IRQn + IRQ0_EX] = (word) &dbgout.isr;

So each time this "callback" routine is called by CPU I want to get access to it's "parent" object to save and/or manipulate the received data in userfriendly manner. 因此,每次CPU调用此“回调”例程时,我都希望访问它的“父”对象,以便以用户友好的方式保存和/或操作接收到的数据。
Is it possible at all? 有可能吗? Maybe somehow organize the class or whatever. 也许以某种方式组织班级或其他活动。
The architecture is strictly 32bit (ARM, gcc) 架构严格为32位(ARM,gcc)

Static methods know nothing about the object. 静态方法对对象一无所知。

You need a different approach: 您需要一种不同的方法:

// Create interrupt handler method (non-static!)
void Uart::inthandler() {
    // whatever is needed here
}

// Create object
Uart* p = new Uart(...);

// Create interrupt handler function
void inthandler() {
    if (p != NULL) {
        p->inthandler();
    }
}

// Install the interrupt handler function
InstallIntHandler(IRQ, inthandler);

It's just a principle that has to be adapted to your specific environment. 这只是必须适应您的特定环境的原则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM