MediaTek-Labs / mt3620_m4_software

mt3620_m4_driver
Other
32 stars 29 forks source link

Constructor of static variable is not called #22

Closed matsujirushi closed 4 years ago

matsujirushi commented 4 years ago

Constructor of static variable is not called. Don't you know how to fix it?

image

https://github.com/matsujirushi/mt3620_m4_software/tree/matsujirushi-issue-22/MT3620_M4_Sample_Code/FreeRTOS/MT3620_RTApp_FreeRTOS_CPP https://github.com/matsujirushi/mt3620_m4_software/commit/728055c951f6987ee87baf0f5f3ba4182377037e

LawranceLiu commented 4 years ago

Declare class objects as static/global variables would lead to constructor invocation timing uncontrollable. In this case, the constructor seems not invoked. You could try this way:

class DigitalOut
{
private:
    os_hal_gpio_pin _Pin;

public:
    DigitalOut(os_hal_gpio_pin pin);
    DigitalOut(){};
    void Write(int value);
};

DigitalOut::DigitalOut(os_hal_gpio_pin pin)
{
    _Pin = pin;
    mtk_os_hal_gpio_request(_Pin);
    mtk_os_hal_gpio_set_direction(_Pin, OS_HAL_GPIO_DIR_OUTPUT);
}

void DigitalOut::Write(int value)
{
    mtk_os_hal_gpio_set_output(_Pin, value ? OS_HAL_GPIO_DATA_HIGH : OS_HAL_GPIO_DATA_LOW);
}

static DigitalOut led;

static void blink_task(void* pParameters)
{
    led = DigitalOut(gpio_led_green);
        //....
}
matsujirushi commented 4 years ago

Hi Lawrance-san,

In this case, this code looks fine. However, I think the lack of a constructor of static variable call should be addressed in the development environment, not in the code.

LawranceLiu commented 4 years ago

Hi Matsuoka-san, Thank you very much for the solution!