LowPowerLab Forum

Hardware support => Moteino M0 => Topic started by: frankleonrose on July 20, 2018, 11:17:19 PM

Title: Building library that expects USB Serial (PlatformIO)
Post by: frankleonrose on July 20, 2018, 11:17:19 PM
Hi. My project has a dependency that uses `Serial.dtr()`. Works fine when Serial is actually USB. Should build fine with MoteinoM0 since it supports SerialUSB.

However, moteino_zero/variant.cpp defines

  Uart Serial( &sercom5, PIN_SERIAL_RX, PIN_SERIAL_TX, PAD_SERIAL_RX, PAD_SERIAL_TX ) ;

If I try to redefine `Serial` for the project to be `SerialUSB` (-DSerial=SerialUSB), I get a linker error stating that SerialUSB has been defined twice, because the line in variant.cpp has become

  Uart SerialUSB( &sercom5, PIN_SERIAL_RX, PIN_SERIAL_TX, PAD_SERIAL_RX, PAD_SERIAL_TX ) ;

Is there a recommended way to build an entire project (source and dependencies) with a consistent definition of `Serial`?

If this is just broken, it seems like the right thing would be for variant.cpp to define this Uart as Serial5 (aligns with sercom5). Then `Serial` is available to be redefined as necessary. It could have a default #define Serial Serial5 so as not to break anybody. The adafruit/feather_m0 variant does this.

Thanks!
Title: Re: Building library that expects USB Serial (PlatformIO)
Post by: Felix on July 23, 2018, 11:51:37 AM
Hi frankleonrose,

I have not tried to do something like this via linker, I usually redefine Serial as SerialUSB with this directive, in the sketch itself:

#if defined(MOTEINO_M0) && defined(SERIAL_PORT_USBVIRTUAL)
  #define Serial SERIAL_PORT_USBVIRTUAL // Required for Serial on Zero based boards
#endif


You could try to compile the same way in Arduino and see what you get.

The core follows the Arduino Zero (same MCU) pattern in terms of peripheral definitions.
I do not want to stray away from that standard since people expect it to work very similar to Arduino Zero.
My primary concern is being able to support this on the Arduino platform. Platformio is a niche that sometimes requires tweaking. So .. if Arduino Zero fails in the same manner, then I would raise an issue with Platform.io to look into it. If that escalates to be an Arduino core problem then Arduino should  be flagged and if there is a resolution we can merge it in this M0 core.

Title: Re: Building library that expects USB Serial (PlatformIO)
Post by: frankleonrose on July 30, 2018, 09:47:18 AM
Thanks, @felix.

I contributed a board specification (https://github.com/platformio/platform-atmelsam/commit/3c3c4ae8b39516b766b5cb1f81e7fac46702e40f) that should help other PlatformIO+MoteinoM0 users get started.

With respect to Serial, given the Arduino Zero standard and the fact that Arduino IDE doesn't offer project-wide defines (-DSOMETHING), the only solution is to avoid libraries that use Serial explicitly.
Title: Re: Building library that expects USB Serial (PlatformIO)
Post by: Felix on July 30, 2018, 10:08:09 AM
Thanks for the explanation and for making that contribution, I will follow that commit!