MySensors Debug print @ 5.3Mbit/sec freeing Hardware Serial Port

MySensors provides a lot of useful debugging information on the serial port when MY_DEBUG is defined, I need to use the HardwareSerial for another purpose. but would still like to have the option to see the debug prints. which could look something like this

 __  __       ____
|  \/  |_   _/ ___|  ___ _ __  ___  ___  _ __ ___
| |\/| | | | \___ \ / _ \ `_ \/ __|/ _ \| `__/ __|
| |  | | |_| |___| |  __/ | | \__ \  _  | |  \__ \
|_|  |_|\__, |____/ \___|_| |_|___/\___/|_|  |___/
        |___/                      2.3.1

1 MCO:BGN:INIT NODE,CP=RSNNA---,REL=255,VER=2.3.1
1 MCO:BGN:BFR
before
ShuntCtrl
to clear EEPROM pulldown d13 now:  321
4411 TSM:INIT
4412 TSF:WUR:MS=1000
4412 TSM:INIT:TSP OK
4413 TSM:INIT:STATID=123
4413 TSF:SID:OK,ID=123
4413 TSM:FPAR
4430 TSF:MSG:SEND,123-123-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
5412 MCO:BGN:STP
setup

In Debugging @ 5.3Mbit/sec (5333333 Baud) on Arduino and other Embedded systems I demonstrate how to implement a fast and low overhead method of getting debug prints out on a normal i/o pin, moving the buffering complexity to a USB-Serial-port.

Only output is important so the above solution would be adequate for the debug prints from MySensors. Hence the way forward is to create an Arduino Library, we will call it DebugSerial, which contains two files

libraries/DebugSerial/DebugSerial.h

/* DebugSerial -- bitbang serial print @ 5.3Mbit/sec
 * http://storepeter.dk/embedded/dprint
 * Copyright (c) 2018 peter@lorenzen.us BeerWare
 * need the these defines to used D13 for bitbang serial
 */ 
 #ifndef DebugSerial_h
 #define DebugSerial_h
 
 #include "Arduino.h"

 #define DPRINT_PIN 13
 // FIXME we should be able to generate the lines below automagically
 #define DPRINT_TX_PORT PORTB
 #define DPRINT_TX_DDR  DDRB
 #define DPRINT_TX_BIT  5

 class DebugSerial : public Stream {
 public:
     DebugSerial(uint8_t level) { begin(level); }
     void begin(uint8_t level);
     int read() { return 0; }
     int available() { return 0; }
     int peek() { return 0; }
     size_t write(uint8_t byte);
     using Print::write;
 };
 
 extern DebugSerial dprint_serial;
 #endif

libraries/DebugSerial/DebugSerial.cpp

/* DebugSerial -- bitbang print @ 5.3Mbit/sec
 * http://storepeter.dk/embedded/dprint
 * Copyright (c) 2018 peter@lorenzen.us BeerWare
 */ 
 #include 
 #define DPRINT_TX_PORT_ADDR _SFR_IO_ADDR(DPRINT_TX_PORT)
 uint8_t debug_level;
 void DebugSerial::begin(uint8_t level)
 {
         DPRINT_TX_DDR &= ~_BV(DPRINT_TX_BIT);   // set tx_bit as input
         DPRINT_TX_PORT |= _BV(DPRINT_TX_BIT);   // stop bit and weak pull-up ~ 20kohm
         if ((DPRINT_TX_PORT & _BV(DPRINT_TX_BIT)) == 0) {
                 level = 0; // pull the tx_bit down by a 2k2 resitor to disable debug prints
         }
         DPRINT_TX_DDR |= _BV(DPRINT_TX_BIT);    // set tx_pin as output
         debug_level = level;
 }
static void writeByte(uint8_t ch)  // irq disabled for 33 instruction that is 2usec
{
         asm volatile (
         "   in r25,SREG\n\t"                // get interrupt state
         "   cli\n\t"                            // disable irq
         "   in tmp_reg,%[tx_port]\n\t"      // read the full port
         "   clt\n\t"                            // T=0
         "   bld tmp_reg,%[tx_bit]\n\t"      // set tx_bit=T
         "   out %[tx_port],tmp_reg\n\t"     // sending Startbit
         "0: bst %[ch],0\n\t"                    // T=lsb
         "   bld tmp_reg,%[tx_bit]\n\t"
         "   out %[tx_port],tmp_reg\n\t"     // sending lsb
         "1: bst %[ch],1\n\t"
         "   bld tmp_reg,%[tx_bit]\n\t"
         "   out %[tx_port],tmp_reg\n\t"
         "2: bst %[ch],2\n\t"
         "   bld tmp_reg,%[tx_bit]\n\t"
         "   out %[tx_port],tmp_reg\n\t"
         "3: bst %[ch],3\n\t"
         "   bld tmp_reg,%[tx_bit]\n\t"
         "   out %[tx_port],tmp_reg\n\t"
         "4: bst %[ch],4\n\t"
         "   bld tmp_reg,%[tx_bit]\n\t"
         "   out %[tx_port],tmp_reg\n\t"
         "5: bst %[ch],5\n\t"
         "   bld tmp_reg,%[tx_bit]\n\t"
         "   out %[tx_port],tmp_reg\n\t"
         "6: bst %[ch],6\n\t"
         "   bld tmp_reg,%[tx_bit]\n\t"
         "   out %[tx_port],tmp_reg\n\t"
         "7: bst %[ch],7\n\t"
         "   bld tmp_reg,%[tx_bit]\n\t"
         "   out %[tx_port],tmp_reg\n\t"     // sending msb
         "8: set\n\t"                            // T=1
         "   bld tmp_reg,%[tx_bit]\n\t"
         "   out %[tx_port],tmp_reg\n\t"     // sending Stopbit
         "   out SREG,r25\n\t"               // restore irq
         :
           [ch] "+r" (ch)
         :
           [tx_port] "M" (DPRINT_TX_PORT_ADDR),
           [tx_bit] "M" (DPRINT_TX_BIT)
         :
           "r25"
         );
}
size_t DebugSerial::write(uint8_t byte)
{
           writeByte(byte);
           return 1;
}

Finally we need to make MySensors aware of the new library, applying this patch to MySensors

diff --git a/hal/architecture/AVR/MyHwAVR.h b/hal/architecture/AVR/MyHwAVR.h
 index 14e340e2..8212c86d 100644
 --- a/hal/architecture/AVR/MyHwAVR.h
 +++ b/hal/architecture/AVR/MyHwAVR.h
 @@ -41,6 +41,8 @@
 #ifndef MY_DEBUGDEVICE
  #define MY_DEBUGDEVICE MY_SERIALDEVICE
 +#else
 +#include        // 5.3Mbit bitbang tx only, using pl2303 USB serial, to change pin, edit the this file
  #endif

To redirect the standard MySensors debug print to digital pin 13 add this to your MySensors sketch

#include 
#define MY_DEBUG        // need to be very first
#define MY_SERIALDEVICE dprint_serial
#define MY_DEBUGDEVICE dprint_serial
DebugSerial dprint_serial(1);

Thati is it: connect a PL2303 USB-serial-port to pin D13 and you will be able to follow the debug stream from MySensors.

You need to define both MY_SERIALDEVICE and MY_DEBUGDEVICE otherwise hardware serial port code is still included and initialized. If you use MY_DISABLED_SERIAL MySesnsors debugprints will be disabled – not what I had expected with MY_DEBUGDEVICE defined

Print Friendly, PDF & Email
This entry was posted in Arduino, Embedded, MySensors. Bookmark the permalink.

One Response to MySensors Debug print @ 5.3Mbit/sec freeing Hardware Serial Port

  1. Pingback: Debugging @ 5.3Mbit/sec (5333333 Baud) on Arduino and other Embedded systems | Peter Lorenzen

Comments are closed.