cortext m0 PRIu8 expands to hhu and is not usable

Bug #1836698 reported by Phil Bolduc on 2019-07-16
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
Undecided
Unassigned

Bug Description

Package: toolchain-gccarmnoneeabi - gcc-arm-embedded
Version: 1.70201.0
Toolchain: binary package provided with Platform IO in VS Code
Host Machine: Windows 10 x64
Symptom: when using PRIu8 from toolchain-gccarmnoneeabi/arm-none-eabi/include/inttypes.h it expands to hhu on Cortex M0+ processors. This macro is used to format types in printf style functions. On Cortext M0, an integer value is not correctly formatted.

I am using Platform IO inside VS Code on Windows 10. I am compiling a library, MySensors, that uses the PRIu8 macro to format various values. I am using a Moteino M0. I have also checked compiled output for a adafruit feather m0. On these Cortex M0+ processors, the PRIu8 expands to hhu.

When I got to the definition, the the following path using Platform IO's "Got yo Definition"

  C:\Users\username\.platformio\packages\toolchain-gccarmnoneeabi\arm-none-eabi\include\inttypes.h

  #define __PRI8(x) __INT8 __STRINGIFY(x)
  #define PRIu8 __PRI8(u)

The macro expands using the __INT8 macro defined in toolchain-gccarmnoneeabi\arm-none-eabi\include\sys\_intsup.h

#if (__INT8_TYPE__ == 0)
#define __INT8 "hh"
#elif (__INT8_TYPE__ == 1 || __INT8_TYPE__ == 3)
#define __INT8 "h"
#elif (__INT8_TYPE__ == 2)
#define __INT8
#elif (__INT8_TYPE__ == 4 || __INT8_TYPE__ == 6)
#define __INT8 "l"
#elif (__INT8_TYPE__ == 8 || __INT8_TYPE__ == 10)
#define __INT8 "ll"
#endif

These are common predefined macros as stated here https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html. I am not not really sure where to point the finger on the cause.

Platform IO defines is the SAMD boards here https://github.com/platformio/platformio-pkg-framework-arduinosam

Is there a compiler flag that Platform IO should be using? I just not sure if it is a Platform IO bug or the toolchain-gccarmnoneeabi - gcc-arm-embedded bug.

-------------------------------------------------------------------
Example Program:

#include <Arduino.h>

#ifdef MOTEINO_M0
#define Serial SerialUSB
#endif

void setup() {
  Serial.begin(9600);
  while (!Serial);

  char buffer[512];
  uint8_t value = 123;
  snprintf(buffer, sizeof(buffer), "value=%" PRIu8 "\n", value);

  Serial.print(buffer);

  Serial.print("PRIu8=");
  Serial.print(PRIu8);
}

void loop() {
}

// end of example

-------------------------------------------------------------------
- platformio.ini
-------------------------------------------------------------------

[env:moteino_zero]
platform = atmelsam
board = moteino_zero
framework = arduino

-------------------------------------------------------------------
Output:

value=hu
PRIu8=hhu

-------------------------------------------------------------------
Expected Output:
value=123
PRIu8=hu

Phil Bolduc (pbolduc) wrote :

When adding build_flag = -E, the setup function in generated main.cpp.o file looks like this

void setup() {
  SerialUSB.begin(9600);
  while (!SerialUSB);

  char buffer[512];
  uint8_t value = 123;
  snprintf(buffer, sizeof(buffer), "value=%"
# 18 "src\\main.cpp" 3 4
                                            "hh"
# 18 "src\\main.cpp"
                                            "u" "\n", value);

  SerialUSB.print(buffer);

  SerialUSB.print("PRIu8=");
  SerialUSB.print(
# 23 "src\\main.cpp" 3 4
              "hh"
# 23 "src\\main.cpp"
              "u");
}

What is your full compile command? Are you using newlib_nano by any chance?

Phil Bolduc (pbolduc) wrote :

Here is the first part of the build log with the arm-none-eabi-g++ command. I have attached the full log.

> Executing task: C:\Users\phil\.platformio\penv\Scripts\platformio.exe run --verbose --environment adafruit_feather_m0 <

Processing adafruit_feather_m0 (platform: atmelsam; board: adafruit_feather_m0; framework: arduino)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONFIGURATION: https://docs.platformio.org/page/boards/atmelsam/adafruit_feather_m0.html
PLATFORM: Atmel SAM 3.8.0 > Adafruit Feather M0
HARDWARE: SAMD21G18A 48MHz, 32KB RAM, 256KB Flash
DEBUG: Current (atmel-ice) External (atmel-ice, blackmagic, jlink)
PACKAGES: toolchain-gccarmnoneeabi 1.70201.0 (7.2.1), framework-arduinosam 4.3.190711
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 20 compatible libraries
Scanning dependencies...
No dependencies
arm-none-eabi-g++ -o .pio\build\adafruit_feather_m0\src\main.cpp.o -c -fno-rtti -fno-exceptions -std=gnu++11 -fno-threadsafe-statics -Os -ffunction-sections -fdata-sections -Wall -mthumb -nostdlib --param max-inline-insns-single=500 -mcpu=cortex-m0plus -DPLATFORMIO=40000 -DARDUINO_ARCH_SAMD -DARDUINO_SAMD_ZERO -DARDUINO_SAMD_FEATHER_M0 -DARM_MATH_CM0PLUS -D__SAMD21G18A__ -DF_CPU=48000000L -DUSBCON -DUSE_TINYUSB -DARDUINO=10805 -DUSB_VID=0x239A -DUSB_PID=0x800B "-DUSB_PRODUCT=\"Adafruit Feather M0\"" -DUSB_MANUFACTURER=\"Adafruit\" -Iinclude -Isrc -IC:\users\phil\.platformio\packages\framework-arduinosam\cores\adafruit -IC:\users\phil\.platformio\packages\framework-arduinosam\system\samd\CMSIS\CMSIS\Include -IC:\users\phil\.platformio\packages\framework-arduinosam\system\samd\CMSIS-Atmel\CMSIS\Device\ATMEL -IC:\users\phil\.platformio\packages\framework-arduinosam\cores\adafruit\Adafruit_TinyUSB_Core -IC:\users\phil\.platformio\packages\framework-arduinosam\cores\adafruit\Adafruit_TinyUSB_Core\tinyusb\src -IC:\users\phil\.platformio\packages\framework-arduinosam\variants\feather_m0 src\main.cpp

Phil Bolduc (pbolduc) wrote :

I have created a corresponding issue on the platform io atmelsam repository, https://github.com/platformio/platform-atmelsam/issues/70

Phil Bolduc (pbolduc) wrote :

I ran the same sketch with Arduino IDE 1.8.9 and I got the same output,

value=hu
PRIu8=hhu

Where would the problem be? board definition? is there a compiler option to force newlib_nano? How can I check if it is using newlib_nano?

I have attached the build log from Arduino IDE 1.8.9

john (jkovach) wrote :

Must be newlib_nano. I had a similar problem:
https://bugs.launchpad.net/gcc-arm-embedded/+bug/1598122
The library is selected at link stage. Look for linker command line.

john (jkovach) wrote :

By the way, the full newlib would pull a lot of code. I think it would be wise to accept the limitations of newlib_nano and modify your code like the following:
uint8_t value = 123;
snprintf(buffer, sizeof(buffer), "value=%d\n", (int)value);

Phil Bolduc (pbolduc) wrote :

Thank you for your feedback. This does provide the answer to the original issue. I have submitted a issue on the original library's repository. Since the issue is caused in the linking stage, it could be difficult to at compilation time detect. I would have to know it is the gcc arm embedded toolchain and if running on a micro-controller.

Anyways, I appreciate the feed back and pointing me in the right direction. I think this issue can be closed.

Changed in gcc-arm-embedded:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.