Libraries, constructors, and softwareSerial...

38 views
Skip to first unread message

William Westfield

unread,
Apr 26, 2015, 4:39:12 AM4/26/15
to Arduino Developers
In general, library code (and sketch code) should not call high-level Arduino functions like pinMode() from constructors, because constructors get called before init(), right?

SoftwareSerial does this :-(

BillW / WestfW

bob cousins

unread,
Apr 26, 2015, 5:22:44 AM4/26/15
to devel...@arduino.cc
An awful lot of libraries do the same thing!

The Arduino core has a special bodge to init the API before constructors are called, but this is a bad habit to encourage and is a pain if using third-party cores which do a more normal initialization.

In general, a constructor should not have dependencies on any external code.

William Westfield

unread,
Apr 26, 2015, 8:05:41 PM4/26/15
to devel...@arduino.cc

> The Arduino core has a special bodge to init the API before constructors are called

It does? Where is that?

BillW

bob cousins

unread,
Apr 27, 2015, 3:33:31 PM4/27/15
to devel...@arduino.cc

I should rephrase that to say the Due core has a bodge:

In file https://github.com/arduino/Arduino/blob/master/hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/source/gcc/startup_sam3xa.c#L198

    /* Initialize the C library */

    // Arduino: we must setup hardware before doing this
    //__libc_init_array();

    /* Branch to main function */
    main();

    /* Infinite loop */
    while (1);

main() does setup then calls libc_init_array().

I have never looked at the AVR startup code, but I assumed it would be similar - maybe not. I'm not even sure where the AVR startup code is, I guess it is baked into toolchain already.
Reply all
Reply to author
Forward
0 new messages