Preparing a project to use the Serial Port in either Release or Debug mode

by Visual Micro 29. August 2013 10:57

This document applies more to boards such as Maple than Arduino but is a useful reference for all

Arduino is quite tollerant but boards such as Maple will crash if we use the SerialUSB.begin() command in our code more than once. Since the debugger, whichever port is it using, issues a SerialUSB.begin() we need our user code to only issue a SerialUSB.begin() when in release mode (non-debug)

The examples below show how to conditionally add code to a project based upon the release/debug (Micro Debug=True) setting using a define called VM_DEBUG.

Another example shown below is the use of a delay when the board first starts. Normally Arduino boards do not need this delay but we found that boards such as maple only work reliably over serial with an initial delay. The debugger automatically enforces this delay so the example shows how to add the delay for normal release mode use of the serial port. The example shows a 2 second delay (2000 milis)

void setup()

{

//     pinMode(BOARD_LED_PIN, OUTPUT);

//if we are not starting in debug mode we can optionally do stuff

//maple gets very upset if both the debugger and user code issue a "begin()" request

//so for maple we let the debugger initialise the port

//for arduino it doesn't matter so much. we can use the project property called RemoteSpeed to force the debugger to use something other than 115200

#if !defined(VM_DEBUG)

//maple uses SerialUSB by default

       SerialUSB.begin();

//for arduino and hardware that requires a baud rate

       //Serial.begin(115200); 

//maple likes this delay for Serial or the first serial messages are lost

       delay(2000);           

//an example

       SerialUSB.println("Starting Maple in release mode");

//for arduino/teensy/msp430

       //Serial.println("Starting Arduino in release mode");

#endif

}

void loop()

{

// Turn the LED from off to on, or on to off   

//toggleLED();          

#if defined(VM_DEBUG)

       SerialUSB.println("Running in debug mode");

#else

       SerialUSB.println("Running in release mode");

#endif

       delay(200);

}

//The example above also demonstrates use of the Visual Micro "Debug Mode" define (VM_DEBUG)