Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic set up GDB for Arduino Mega2560 to work with existing Serial code (Read 1933 times)
JFR
Newbies
*
Offline


Posts: 7
Joined: Jan 4th, 2023
set up GDB for Arduino Mega2560 to work with existing Serial code
Jan 5th, 2023 at 6:21pm
Print Post  
vMicro version 2.2022.1128
minor version 24
Visual Studio 2022 Community
Windows 10

Hi All

I have existing Arduino code executing on the Mega2560 platform utilizing multiple layers of Serial messages.  Of the 4 available serial ports on the Mega, I am using 0 (Serial) for communicating both with my console, as well as displaying various debugging and other state messages, and 1 (Serial1) for communicating with servos attached to the board.  Serial2, and Serial3 are not in use at this time.

What is the most efficient way to set up GDB debugging to not interfere with the existing serial use while allowing debugging functionality (without having to rewrite a significant amount of code!).

I am currently experiencing the following error when enabling GDB-stub hardware debugging for this code:

Error linking for board ATmega2560 (Mega 2560) (Arduino Mega) (mega_atmega2560)
HardwareSerial0.cpp.o (symbol from plugin)*: In function Serial
(.text+0x0)*: multiple definition of __vector_25
Build failed for project 'TestHarnessServoTranslate'
avr8-stub.c.o (symbol from plugin)*: (.text+0x0): first defined here

Any insights are appreciated

my setup loop:

void setup()
{
  debug_init();
  pinMode(ledPin, OUTPUT); 
   
  // for interfacing with SERVOs 
  Serial1.begin(1000000);
  while (!Serial1) {
    ; // wait for serial port to connect. Needed for Native USB only
  }
  // for serial debug
    Serial.begin(115200); 
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
}
 
 
  SERVO.pSerial = &Serial1;  // assign Serial 1 to servos
  delay(500);

  cLog* pLog = cLog::getInstance(); // using singleton pattern  - user Serial (0) by default
   
  pServo = cServo::getInstance(); // using singleton pattern
  pServo->Start(&SERVO);      
  SERVO.EnableTorque(254, 1);

  pHexi = cHexapod::getInstance();
  pHexi->Start();
  digitalWrite(ledPin, HIGH);
}


----

// log.h

#ifndef _LOG_h
#define _LOG_h

#if defined(ARDUINO) && ARDUINO >= 100
  #include "arduino.h"
#else
  #include "WProgram.h"
#endif
//#include "configuration.h"

#define _Trace   5
#define _Debug   4
#define _Info    3
#define _Warn    2
#define _Error   1
#define _GDB     0

extern byte level_log;

class cLog { 
  public:   
    void writeLog(String msg, int code);
    void writeLog(String msg, int attribute, int code);
    void writeLog(String msg, short attribute, int code);
    void writeLog(String msg, double attribute, int code);
    static cLog* getInstance(); // public interface to constructor
    ~cLog(); // destructor
private:
    cLog(); // private constructor
    static cLog* instance; // singleton instance pointer
    int instanceCount;

    void printMsg(int code);
    void printMsg(String msg, int code);
    void printMsg(String msg, int attribute, int code);
    void printMsg(String msg, double attribute, int code);
    
    char* StringToChar(String msg);
};
#endif


---

#ifndef _cSERVO_h
#define _cSERVO_h

#if defined(ARDUINO) && ARDUINO >= 100
  #include "arduino.h"
#else
  #include "WProgram.h"
#endif

#include <SCSCL.h>
#include "log.h"


#define UPPERLIMIT 800  // MAX raw servo position (0-1023)
#define LOWERLIMIT 100  //MIN raw servo position (0-1023)

class cServo 
{
public:
    static cServo* getInstance(); // public singleton class access 
    SCSCL* diagnosticSERVOPointer(); // use RAW servo libraries
    void Start(SCSCL* pServo);
    int ReadPos(int ID);
    int RegWritePos(uint8_t address, uint16_t position, uint16_t time, uint16_t speed = 0); // asynch single servo write: address, position, time, speed = 0 & validate and convert from degrees
    void RegWriteAction(void); // commit writes
    void SyncWritePos(u8 ID[], int moveTransactionsCount, u16 Position[], int Time, u16 Speed[]);
    bool ReadMove(int ID);
    ~cServo();
private:
    static cServo* instance;  // to store singleton instance pointer
    int instanceCount;
    cServo(); // constructor - private to force use of public getInstance()
    SCSCL *pSERVO;
    cLog* pLog; // singleton
    int calcDelay(u16 currentPos, u16 newPos, u16 velocity);
}; //end of class cServo


class cSharedUtils
{
  public:
    static int calcDelay(u16 currentPos, u16 newPos, u16 velocity); // in milliseconds
    static u16 degreesToServoPosition(int degrees);
    static u16 validateUpperLower(u16 newValue);
    static int servoPositionToDegrees(u16 position);
};

#endif
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12163
Location: United Kingdom
Joined: Apr 10th, 2010
Re: set up GDB for Arduino Mega2560 to work with existing Serial code
Reply #1 - Jan 5th, 2023 at 7:30pm
Print Post  
The avr-debugger library page on github should answer the qestion. If it supports 2560 and other serial ports then you will expect the debug_init() to pass the serial port. 

As it is, the debug_init() will take over the Serial port, then you will see errors resulting from your code that uses it.

You will also see similar errors if you turn optimization off or the arduino avr core. The core is designed in a way that does not allow optimization to be switched off.

If you don't find a solution on github then you can either use the visual micro serial debugger specifying serial3/4 or buy an ICE debugger to connect to the 2560.



  
Back to top
IP Logged
 
JFR
Newbies
*
Offline


Posts: 7
Joined: Jan 4th, 2023
Re: set up GDB for Arduino Mega2560 to work with existing Serial code
Reply #2 - Jan 6th, 2023 at 2:58pm
Print Post  
Thanks Tim Smiley
  
Back to top
 
IP Logged
 
vmuser
Newbies
*
Offline


Posts: 2
Joined: May 18th, 2018
Re: set up GDB for Arduino Mega2560 to work with existing Serial code
Reply #3 - Feb 23rd, 2023 at 6:48pm
Print Post  
I have encountered a problem with GDB stub debugging. VS2022 Community + VM 2.2022.1128 min10.  When debugger is started with no active breakpoints it is impossible to pause execution or place a breakpoint. Clicking pause/stop button results in IDE hanging up, and the only way to "unhang" is to take the board out of USB port, then IDE reports connection failure and can be manipulated further.

I suppose that it is due to incorrect launch config. GDB is launched by default with the following args:
Code
Select All
{
...
"MIDebuggerPath": "toolchain-path/avr-gdb.exe",
  "MIDebuggerArgs": "\"gdb_stub_test.ino.elf\" -ex \"set serial baud 115200\" -ex \"target remote \\\\.\\COM9\"",
...
}
   


I found that to work correctly COM port must be placed into "MIDebuggerServerAddress" variable, elf is also can be taken from "program" parameter. So successful config in my case is:
Code
Select All
"MIDebuggerServerAddress": "\\\\.\\COM9",
  "MIDebuggerPath": "e:\\INSTALL\\CODING\\avr-gcc-7.3.0\\bin\\avr-gdb.exe",
  "MIDebuggerArgs": "--interpreter=mi -b 115200",
  "program": "$(program)", 


I ended up with 'Manual/Custom' debugger setting instead of 'GDB Stub' and local 'debugger_launch.json' which is attached below. Hope this help someone to save a few hours.
« Last Edit: Feb 23rd, 2023 at 6:50pm by vmuser »  

Please Register or Login to the Forum to see File Attachments
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2441
Joined: Feb 13th, 2019
Re: set up GDB for Arduino Mega2560 to work with existing Serial code
Reply #4 - Feb 23rd, 2023 at 7:13pm
Print Post  
Thanks for the information around this, and the workaround.

We had not experienced this issue before with the existing config, however this may be due to us always including the "breakpoint()" line of code at the top of the loop() function when using the GDBStub.

We will review our configurations and amend them to ensure this is resolved for all users.
  
Back to top
IP Logged
 
vmuser
Newbies
*
Offline


Posts: 2
Joined: May 18th, 2018
Re: set up GDB for Arduino Mega2560 to work with existing Serial code
Reply #5 - Feb 23rd, 2023 at 7:35pm
Print Post  
Do you plan to add a debug window with AVR registries like in AVR Studio or VisualGDB?  Or may be it is already there and I've missed something. Of course we can explore them through watch window, i.e. PORTx, DDRx etc., but separate window would be useful anyway.
  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2441
Joined: Feb 13th, 2019
Re: set up GDB for Arduino Mega2560 to work with existing Serial code
Reply #6 - Feb 24th, 2023 at 10:22am
Print Post  
We are looking to implement something similar using the Peripheral Viewer and SVD file functionality in Visual Studio, however this is still being worked on currently, we will update this thread when there is further information available.
  
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint