Initialise CAN after startup to prevent runtime error

Linked to this topic: https://os.mbed.com/questions/85763/Initialize-peripherals-before-main/

If I initialise CAN according to the mbed example with it being defined as a CAN object outside of the main process, the mcu fails to boot and throws a runtime fault. This is because I need to initialise the can transceiver first with a digital can enable pin first before the can Tx and Rx lines are initialised to prevent the Rx line from being pulled down and thus causing a runtime error. This is how I would like to do things, but I am unsure how to do it:

#include "mbed.h"
//CAN can1(can1rx, can1tx, 500000);       //cant initialise here without creating runtime error
CAN can2(can2rx, can2tx, 500000);       //second can
DigitalOut led(LED1);
DigitalIn canFault(can1nfault);
DigitalOut canNSTB(can1nstb);
DigitalOut canEN(can1en);

#define repeatTime     100ms

void canInit(){
    wait_us(100);
    canEN = 1;
    wait_us(100);
    canNSTB = 1;
    wait_us(100);
    //Need to initialise CAN here instead
    can_init_freq(&cb1, can1rx, can1tx, 500000);  //Tried to use this function but unsure how to properly call it
}

int main(){
    canInit();  
    while (true) {
        led = !led;
        ThisThread::sleep_for(repeatTime);
        //printf("Hello World\n");
    }
}

Hello,

please be so kind and fill what errors you received, what target you use and what pins you used.
And the can2 object is fine at this place, right?

BR, Jan

Hi, Sorry i forgot those details - I am using a STM32-F446RE.

Can 1 is connected to a TJA1043 transceiver that needs the enable and not standby pins set to certain logic levels before can interface is configured, thats causing problems.

Can2 is connected to a SN65 can transceiver which does not have these additional pins and works fine.

Pins are defines as following:

    #define can1rx PB_8
    #define can1tx PB_9
    #define can1nfault PC_4
    #define can1nstb PC_5
    #define can1en PB_0
    #define can2rx PB_5
    #define can2tx PB_13

And did you tried something like this

#include "mbed.h"

//first default state of pins
DigitalOut canNSTB(can1nstb,1); 
DigitalOut canEN(can1en,1); 

// then CAN
CAN can1(can1rx, can1tx, 500000);       //cant initialise here without creating runtime error
CAN can2(can2rx, can2tx, 500000);       //second can

DigitalOut led(LED1);
DigitalIn canFault(can1nfault);

Or using pointers is a way.

CAN *can1;
//later
can1 = new CAN(can1rx, can1tx, 500000); 

BR, Jan

Thankyou so much, initialising the pins as 1 worked a treat!