Digistump Forums

The Digispark => Digispark (Original) Support => Topic started by: atkulp on March 20, 2013, 12:44:40 pm

Title: Software Serial
Post by: atkulp on March 20, 2013, 12:44:40 pm
Is there a replacement for the SoftwareSerial.h and associated library for Digispark?  I don't need SPI support -- just a simple serial TX and RX pin.  I have a Bluetooth module with V+, GND, TX, and RX pins.  I hoped that it would be easy to use, but I don't know how to set baud rate, error correction, etc. to emulate a serial port.  Any ideas?
Title: Re: Software Serial
Post by: Bluebie on March 20, 2013, 04:52:10 pm

SoftwareSerial works fine on the Digispark except for the 16.5MHz clock speed. If you can wait a couple of days for @Digistump to release the next version of the digispark arduino app (or if you're on a mac, use my beta build (http://digistump.com/board/index.php/topic,694.0.html)) you'll be able to select either the 16.0mhz or 8mhz digispark "board" from the Tools > Board menu. Both of these speeds compile fine with SoftwareSerial. I haven't tested it properly though and because of it's dependance on PCINT0, the timing maybe slightly off anyway. You might have more luck with lower speeds than higher ones.


These alernative clock speeds aren't compatible with using USB libraries like DigiUSB or DigiKeyboard, but SoftwareSerial conflicts with those by taking over the Pin Change Interrupt as well and it's nontrivial to resolve that issue.


The real solution would be for someone to spend the time rebuilding the table[] and XMIT_START_ADJUSTMENT = 6; for the 16.5mhz clock speed and possibly tweaking the other speed tables to take in to account the few instruction delay caused by the digispark bootloader on pcint0. Nicer solutions include using a timer to sample the pin instead of pinchange interrupts, which wouldn't conflict with usb libraries, but would mess with the frequency of PWM channels, or someone could implement HardwareSerial on digispark seeing as that seems to be what most people really want anyway. There's no reason the USI on the digispark can't implement hardware serial.


See also this thread (http://digistump.com/board/index.php/topic,212.0.html)
Title: Re: Software Serial
Post by: digistump on March 20, 2013, 05:52:09 pm
Also checkout the softserial library in the newest software release - contributed by RC Navy here on the forums.


Newest release is out - tons of updates/improvements - get it here: http://digistump.com/wiki/digispark/tutorials/connecting
Title: Re: Software Serial
Post by: atkulp on March 20, 2013, 06:00:50 pm
Awesome!  I actually ended up finding a forum post with a modified timing table.  It's working now!  It will be nice having it in the download rather than making the manual change though.  I have the JY-MCU Bluetooth module (paid $9 on DX.com!!) and it works beautifully with it.
Title: Re: Software Serial
Post by: fraguada on May 21, 2013, 12:24:54 am
@atkulp

I have the same module and am trying to get some communication going with the digispark.  Could you point me to the place where you found the necessary modifications?

At the moment I am struggling a bit with the SoftSerial Lib.  I can get this to work on several other boards with a serial monitor (Uno, Leonardo, 32u4 BO, etc).  I suppose one of the issues is that my BT module is set to 56700 baud rate, and I as understand, this rate is not available to the SoftSerial lib.  So my first step would be to modify this on an UNO and then see if works back on the digispark.
Title: Re: Software Serial
Post by: atkulp on May 21, 2013, 11:12:41 am
Sure.  It was a bit tricky for me to find this stuff.  In AppData\Roaming\Digispark Ready - Arduino 1.03\libraries\SoftwareSerial\SoftwareSerial.cpp (version number of Arduino in the path might vary), search for "const int XMIT_START_ADJUSTMENT = 6;" and add the following block immediately after:


Code: [Select]
#elif F_CPU == 16500000
#warning 16.5MHz proc detected


// DigiSpark support taken from:
// http://digistump.com/board/index.php/topic,212.0.html


static const DELAY_TABLE PROGMEM table[] =
{
  //  baud    rxcenter   rxintra    rxstop    tx
  { 115200,   1,         16,        16,       12,    },
  { 57600,    10,        38,        38,       34,    },
  { 38400,    26,        59,        59,       56,    },
  { 31250,    32,        72,        72,       70,    },
  { 28800,    35,        79,        79,       76,    },
  { 19200,    56,        121,       121,      118,   },
  { 14400,    76,        161,       161,      158,   },
  { 9600,     118,       243,       243,      240,   },
  { 4800,     240,       489,       489,      486,   },
  { 2400,     486,       980,       980,      977,   },
  { 1200,     977,       1961,      1961,     1958,  },
  { 600,      1961,      3923,      3923,     3919,  },
  { 300,      3923,      7855,      7855,     7852,  },
};


const int XMIT_START_ADJUSTMENT = 5;

The next line after this block will be "#else".  That's it.  You might want to back up the cpp file first in case something goes wrong.  I found the info here: http://digistump.com/board/index.php/topic,212.0.html (http://digistump.com/board/index.php/topic,212.0.html).  I haven't tested all the settings, but I can confirm that I now have a serial port available on my Digispark!
Title: Re: Software Serial
Post by: digistump on May 21, 2013, 01:09:54 pm
Worth noting - the newest versions of the software already have this change integrated. Glad you got it working!
Title: Re: Software Serial
Post by: fraguada on May 21, 2013, 02:02:24 pm
Is there any particular reason that 57600 baud rate is not supported?  I see the code in TinyDebugSerial.h where the classes such as: TinyDebugSerialWriter_16_115200 are declared, so I am wondering if it would be too much trouble to add 57600 baud?  At the moment I can work around it.

Error Compiling

In member function 'void TinyDebugSerial::begin(long int)',
    inlined from 'void setup()' at SoftwareSerialExample.ino:35:
C:\Users\wiso\Documents\arduino\DigisparkArduino-Win32\Digispark-Arduino-1.0.4\hardware\digispark\cores\tiny/TinyDebugSerial.h:699: error: call to 'TinyDebugSerialBadBaud' declared with attribute error: Serial (TinyDebugSerial) supports three baud rates: 9600, 38400, or 115200.
Title: Re: Software Serial
Post by: digistump on May 21, 2013, 02:33:10 pm
SoftwareSerial library was modified specifically to work with the Digispark and should support all standard baud rates.


TinyDebugSerial is included with the tiny core we built off of, and we have only added support for 16.5Mhz and haven't tried to do anything else with it - I imagine 57600 baud could be added pretty easily, but I haven't (until now) ever seen a need to support it.
Title: Re: Software Serial
Post by: fraguada on May 21, 2013, 02:42:48 pm
I think I can work around it.  Am currently testing if it is fine, but I see no reason to be tied to 57600.

Thanks!
Title: Re: Software Serial
Post by: fraguada on May 22, 2013, 08:13:08 am
p.s. this is working great on 38400 ;)
Title: Re: Software Serial
Post by: jaghvi on June 21, 2013, 01:33:10 am
 how does the attiny communicates with inbuilt usb port?How is  the serial-usb feature implemented ?
Title: Re: Software Serial
Post by: digistump on June 21, 2013, 10:12:53 am
jm- I believe bluebie gave a detailed answer to this question when you asked it in another thread. Software serial should work with most serial devices - this is attiny to device communication to communicate that data to the computer you would need to additionally use the DigiUSB, DigiKeyboard, or DigiJoystick libraries.
Title: Re: Software Serial
Post by: jaghvi on June 23, 2013, 10:20:17 pm
 :o  thanks