platformio / platform-atmelavr

Atmel AVR: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/atmelavr
Apache License 2.0
136 stars 104 forks source link

USB-CDC disabled on Atmega32U4 #261

Open NaokiS28 opened 2 years ago

NaokiS28 commented 2 years ago

Hi there,

Trying to write a project using both hardware Serial1 and USB Serial but the CDC port gets disabled when using Platform IO to program my Arduino Pro Micro and requires using the reset button to upload again.. But as the CDC is disabled, Serial doesn't work and I can't connect to it. It doesn't show in other terminals either. Here's the code I'm using:

platform.ini:

platform = atmelavr
board = micro
framework = arduino
lib_deps = mheironimus/Joystick@^2.0.7

build_flags = 
    ; enable USB serial
    -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
    -D USBCON
; QUIRK: without setting this, no  
; data will be received on the serial USB port
; https://github.com/stm32duino/Arduino_Core_STM32/issues/1193
monitor_dtr = 1
; optional: set COM port to monitor here if there are multiple
;monitor_port= COM27

main.cpp:

#include <Joystick.h>

#define JVS_VERBOSE   // Use Serial for debuggin

#include <jvs.h>

#define LED_10      10

#define JVS_SENSE_PIN 3
#define RTS_PIN 2

JVS jvs(Serial1, JVS_SENSE_PIN, RTS_PIN);

Joystick_ pad(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
  20, 2, false, false, false, false, false, false, false, false, false, false, false);

// 2 HATs: Menu controllers for P1/P2
// 20 buttons: 2xUDLR for stage, 2x Start for panel, Test, Service, 1 or 2 coin

enum sextetFeilds {_cabinet, 
_p1panel, _p1extra, _p1pad1, _p1pad2, _p1pad3, _p1pad4, 
_p2panel, _p2extra, _p2pad1, _p2pad2, _p2pad3, _p2pad4
};

#define VALID_CMDS 3
const char validCommands[VALID_CMDS][16] = {
  {"INIT;"},
  {"ID;"},
  {"TEST;"}
};

void setup() {
  // put your setup code here, to run once:
 Serial.begin(115200);
  while (!Serial);
  pad.begin();  // Start USB HID and auto update true
  jvs.begin();

  pinMode(LED_10, OUTPUT);
}

void loop() {
  // readInputs();
  jvs.update();  // USB Serial for debugging

  // Direct drive output
    //static bool receivedPacket = false;
    //static byte packet = 0;
      if(Serial.available()){

      }
    //digitalWrite(13, bitRead(lightData[0],4));
}
NaokiS28 commented 2 years ago

This is what happens after programming, monitor opens on the CDC after programming, then the main program loads and CDC is disconnected:

> Executing task in folder LIOn: platformio device monitor <

--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at http://bit.ly/pio-monitor-filters
--- Miniterm on /dev/cu.usbmodemHIDLE1  9600,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
Exception in thread rx:
Traceback (most recent call last):
  File "/Users/naoki/.platformio/penv/lib/python3.8/site-packages/serial/serialposix.py", line 575, in read

--- exit ---
    buf = os.read(self.fd, size - len(read))
OSError: [Errno 6] Device not configured

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/naoki/.platformio/python3/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/Users/naoki/.platformio/python3/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/naoki/.platformio/penv/lib/python3.8/site-packages/serial/tools/miniterm.py", line 499, in reader
    data = self.serial.read(self.serial.in_waiting or 1)
  File "/Users/naoki/.platformio/penv/lib/python3.8/site-packages/serial/serialposix.py", line 581, in read
    raise SerialException('read failed: {}'.format(e))
serial.serialutil.SerialException: read failed: [Errno 6] Device not configured

Terminal will be reused by tasks, press any key to close it.

> Executing task in folder LIOn: platformio device monitor <

--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at http://bit.ly/pio-monitor-filters

--- Available ports:
---  1: /dev/cu.Bluetooth-Incoming-Port 'n/a'
--- Enter port index or full name: