PlatformIO Community

DigiSpark compilation failure using CDC library

I have a very simple DigiSpark project that’s attempting to simulate a serial port. The code is as follows:

#include <DigiCDC.h>

void setup() {
  SerialUSB.begin(115200);
}

void loop() {
  SerialUSB.println("Hello world");
  SerialUSB.delay(1000);
}

And platformio.ini as follows:

[env:digispark-tiny]
platform = atmelavr
board = digispark-tiny
framework = arduino

Why I try to compile either with the IDE or running pio run, I get the following compilation error:

In file included from /Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/cores/dtiny/Stream.h:24:0,
                 from /Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/DigiCDC.h:15,
                 from /Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/DigiCDC.cpp:10:
/Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/cores/dtiny/Print.h:37:0: warning: "BIN" redefined
 #define BIN 2
 ^
In file included from /Users/krupagj/.platformio/packages/toolchain-atmelavr/avr/include/avr/iotn85.h:38:0,
                 from /Users/krupagj/.platformio/packages/toolchain-atmelavr/avr/include/avr/io.h:428,
                 from /Users/krupagj/.platformio/packages/toolchain-atmelavr/avr/include/avr/interrupt.h:38,
                 from /Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/osccal.h:25,
                 from /Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/usbconfig.h:240,
                 from /Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/usbdrv.h:13,
                 from /Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/DigiCDC.h:11,
                 from /Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/DigiCDC.cpp:10:
/Users/krupagj/.platformio/packages/toolchain-atmelavr/avr/include/avr/iotnx5.h:55:0: note: this is the location of the previous definition
 #define BIN     7
 ^
avr-as: unrecognized option `-x'
/Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/DigiCDC.cpp:294:1: error: narrowing conversion of '128' from 'int' to 'char' inside { } [-Wnarrowing]
 };
 ^
/Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/DigiCDC.cpp:294:1: error: narrowing conversion of '131' from 'int' to 'char' inside { } [-Wnarrowing]
/Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/DigiCDC.cpp:294:1: error: narrowing conversion of '255' from 'int' to 'char' inside { } [-Wnarrowing]
/Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/DigiCDC.cpp:294:1: error: narrowing conversion of '129' from 'int' to 'char' inside { } [-Wnarrowing]
*** [.pio/build/digispark-tiny/lib35c/DigisparkCDC/usbdrvasm.asm.o] Error 1
*** [.pio/build/digispark-tiny/lib35c/DigisparkCDC/DigiCDC.cpp.o] Error 1

I’m running on MacOS 10.15.2 and have fully updated platformio.

Here’s how to get it to work.

  • edit /Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/DigiCDC.cpp line 211, change char into unsigned char (fixes "narrowing int to [signed] char error)
  • edit /Users/krupagj/.platformio/packages/framework-arduino-avr-digistump/libraries/DigisparkCDC/usbdrvasm.asm and remove or comment out the end statement
  • fix your main.cpp code by replacing it with this
#include <DigiCDC.h>

extern "C" void setup() {
  SerialUSB.begin();
}

 extern "C" void loop() {
  SerialUSB.println("Hello world");
  SerialUSB.delay(1000);
}
  • because setup() and loop() need to use C-linkage to be able to be found by the startup.c code. also the header file for DigiCDC does declare the existence of a begin(unsigned long) function, but it’s not implemented, so calling it will results in a function not defined error. (Also, a baudrate for a USB CDC is pointless)

You should then see

Building .pio\build\digispark-tiny\firmware.hex
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [====      ]  43.0% (used 220 bytes from 512 bytes)
Flash: [=====     ]  54.1% (used 3252 bytes from 6012 bytes)

There are still however a lot of warnings regarding the re-definition of the BIN macro (e.g. used in Serial.println(value, BIN); by the original Atmel device header. So I’m not sure if that’s going to work and you shouldn’t use it probably.

The USB D+ and D- pins seem to be port B pins 4 and 3 if I read DigiCDCDevice::usbBegin() correctly. Since I don’t have the board I have no idea if it’s going to work.

2 Likes

So I’am trying to use the same library :smiley: latest macOS, latest PIO etc.

Sadly I also get these errors and the compilation fails:

|-- <DigisparkCDC>
Building in release mode
Compiling .pio/build/digispark-tiny/lib165/DigisparkCDC/DigiCDC.cpp.o
Compiling .pio/build/digispark-tiny/lib165/DigisparkCDC/usbdrvasm.asm.o
In file included from /Users/majuss/.platformio/packages/framework-arduino-avr-digistump/cores/dtiny/Stream.h:24:0,
                 from lib/DigisparkCDC/DigiCDC.h:15,
                 from lib/DigisparkCDC/DigiCDC.cpp:10:
/Users/majuss/.platformio/packages/framework-arduino-avr-digistump/cores/dtiny/Print.h:37:0: warning: "BIN" redefined
 #define BIN 2
 ^
In file included from /Users/majuss/.platformio/packages/toolchain-atmelavr/avr/include/avr/iotn85.h:38:0,
                 from /Users/majuss/.platformio/packages/toolchain-atmelavr/avr/include/avr/io.h:428,
                 from /Users/majuss/.platformio/packages/toolchain-atmelavr/avr/include/avr/interrupt.h:38,
                 from lib/DigisparkCDC/osccal.h:25,
                 from lib/DigisparkCDC/usbconfig.h:240,
                 from lib/DigisparkCDC/usbdrv.h:13,
                 from lib/DigisparkCDC/DigiCDC.h:11,
                 from lib/DigisparkCDC/DigiCDC.cpp:10:
/Users/majuss/.platformio/packages/toolchain-atmelavr/avr/include/avr/iotnx5.h:55:0: note: this is the location of the previous definition
 #define BIN     7
 ^
avr-as: unrecognized option `-x'
Compiling .pio/build/digispark-tiny/FrameworkArduino/Tone.cpp.o
*** [.pio/build/digispark-tiny/lib165/DigisparkCDC/usbdrvasm.asm.o] Error 1
Compiling .pio/build/digispark-tiny/FrameworkArduino/WInterrupts.c.o
In file included from /Users/majuss/.platformio/packages/framework-arduino-avr-digistump/cores/dtiny/Tone.cpp:41:0:
/Users/majuss/.platformio/packages/framework-arduino-avr-digistump/cores/dtiny/Tone.cpp: In function 'void tone(uint8_t, unsigned int, long unsigned int)':
/Users/majuss/.platformio/packages/framework-arduino-avr-digistump/cores/dtiny/pins_arduino.h:80:95: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
 #define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_byte( port_to_output_PGM + (P))) )
                                                                                               ^
/Users/majuss/.platformio/packages/framework-arduino-avr-digistump/cores/dtiny/Tone.cpp:232:33: note: in expansion of macro 'portOutputRegister'
       tone_timer_pin_register = portOutputRegister( digitalPinToPort( _pin ) );
                                 ^
In file included from /Users/majuss/.platformio/packages/framework-arduino-avr-digistump/cores/dtiny/wiring_private.h:32:0,
                 from /Users/majuss/.platformio/packages/framework-arduino-avr-digistump/cores/dtiny/WInterrupts.c:37:
/Users/majuss/.platformio/packages/toolchain-atmelavr/avr/include/avr/delay.h:36:2: warning: #warning "This file has been moved to <util/delay.h>." [-Wcpp]
 #warning "This file has been moved to <util/delay.h>."
  ^