Pages: [1] 2   Go Down
Author Topic: AltSoftSerial Library  (Read 4191 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
God Member
*****
Karma: 26
Posts: 626
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I released AltSoftSerial today.

http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

Basically, it's an alternative to SoftwareSerial, which uses a hardware timer to interfere much less with interrupts from other libraries.  It is also capable of simultaneously transmitting and receiving, using buffers for both.

For cases where NewSoftSerial or SoftSerial lose data or interfere with other libraries, AltSoftSerial offers an alternative way to emulate an additional serial port.  In some cases, it may perform better, in others is may offer no improvement (while consuming a timer).

No SoftwareSerial approach can rival true HardwareSerial with a real UART, but when no extra serial port is available, hopefully this alternative library can help in cases where the existing libraries have issues.
« Last Edit: February 11, 2012, 05:14:52 pm by Paul Stoffregen » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, could be a lifesaver. Couldn't get it to work on Uno, though -- I hook up a GPS receiver on pins 8&9 and no data arrives, even if I exchange the RX/TX pins in config/known_boards.h.

It works with NewSoftSerial but the interrupt delays are killing the Serial.
« Last Edit: February 26, 2012, 11:10:25 pm by andr » Logged

0
Offline Offline
God Member
*****
Karma: 26
Posts: 626
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Do NOT change config/known_boards.h!  (unless you're adding another board)

On Uno, pin 8 is always receive, because that is the pin with timer1's input capture signal.  Pin 9 is always transmit, because that is timer1's compare A waveform generator pin.

The definitions in known_boards.h are merely for documentation, and for the library to configure the correct pins using pinMode (so receive becomes and input and transmit becomes an output).  The actual use of the pins when the library is running is due to way the timer hardware is physically connected within the chip.  The numbers in known_boards.h have no effect on the actual pins used, since the library directly controls the timer and the timer connects to specific pins.  If you change known_boards.h, the only effect is the library will use pinMode on the wrong pins.  It will still use the same pins while running, but they may not be set to the correct modes if you edited the .h file.

Why AltSoftSerial is not working for you, I do not know?  I can't see your hardware.  At this point, I would highly recommend you download a fresh copy of AltSoftSerial.  Do not modify known_boards.h.  
« Last Edit: February 27, 2012, 08:03:31 am by Paul Stoffregen » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the explanation.

The hardware is Uno R2 with Libelium GPS shield

Here's how I use NewSoftSerial:
Code:
#include <NewSoftSerial.h>
#define RXPIN 9
#define TXPIN 8
NewSoftSerial nss(RXPIN, TXPIN);
nss.begin(4800);

Here's how I tried AltSoftSerial:
Code:
#include <AltSoftSerial.h>
AltSoftSerial nss;
nss.begin(4800);
Logged

0
Offline Offline
God Member
*****
Karma: 26
Posts: 626
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

#define RXPIN 9
#define TXPIN 8

That's backwards for AltSoftSerial.

http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

Please refer to the table in the "Hardware Requirements" section (below the photo).  AltSoftSerial transmits on pin 9 and receives on pin 8.

You MUST physically swap the wires.  There is no software solution to swap the signals.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ouch. As you can see, the shield is meant to be inserted just one way and fits nicely together with the GPRS shield. Will try putting it backwards nevertheless, just to make sure.

Is it that some deep code hacking is needed to reverse the pins or is it impossible at all because of wiring reasons?
Logged

0
Offline Offline
God Member
*****
Karma: 26
Posts: 626
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ouch. As you can see, the shield is meant to be inserted just one way and fits nicely together with the GPRS shield. Will try putting it backwards nevertheless, just to make sure.

You will probably damage the shield if you reverse ALL the pins.  Only pins 8 and 9 should be swapped.

It's unfortunate this shield was designed with the pins swapped.  AltSoftSerial did not exist 2 years ago when it was made.  I wrote AltSoftSerial only a few weeks ago.

Quote
Is it that some deep code hacking is needed to reverse the pins or is it impossible at all because of wiring reasons?

AltSoftSerial only works with the pins as documented.  On boards like Uno with only one 16 bit timer, there are absolutely no other options.

As explained above, it uses timer1's special features, and those hardware features are hard-wired to those specific pins.  It is absolutely impossible from software to make any pin other than 8 on Uno act as a 16 bit timer input capture.

AltSoftSerial will only work if you connect the signals as documented.  It provides a lot of benefit, but the limitation is only specific pins can be used.
« Last Edit: February 27, 2012, 12:07:18 pm by Paul Stoffregen » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You will probably damage the shield if you reverse ALL the pins.  Only pins 8 and 9 should be swapped.
What makes you think I'll try to bend all the pins? smiley Thanks for the explanation and all the work. Will look for other workarounds.
Logged

0
Offline Offline
God Member
*****
Karma: 26
Posts: 626
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If "will try putting it backwards" means only swapping pins 8 and 9, then good.

If it means attempting plug the whole shield in some strange way (perhaps hanging off the edge of the Uno) which reverses 8 and 9, but also causes other pins to line of differently... well, don't do that!
Logged

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It is absolutely impossible from software to make any pin other than 8 on Uno act as a 16 bit timer input capture.
Actually you could use any one of the analog pins as an input for the 16 bit timer. 
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

0
Offline Offline
God Member
*****
Karma: 26
Posts: 626
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It is absolutely impossible from software to make any pin other than 8 on Uno act as a 16 bit timer input capture.
Actually you could use any one of the analog pins as an input for the 16 bit timer. 

Wow, yes, you're right.  The AN1 pin can too.  I didn't notice that before.

The signal will become inverted, so the library would need to handle reverse polarity (on my to-do list), and of course code would need to enable the analog comparator and ADC mux.

Good eye on the hardware's seldom used features!
Logged

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm going to have to give this a try now.  My current project uses NSS and as luck would have it I just happened to route those serial signals to 8 and 9 (what are the odds?). 
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey Paul,
I haven't tried this alternative yet but I just wanted to be sure on a couple of things before I implement this library.  I need to use pin change interrupts on most of my IO pins but unfortunately NewSoftSerial ties up all the pin change interrupt vectors even if they're not used so I get multiple vector define errors when I compile.  I could rem out ports C and D in NSS but I still lose PCI for the rest of the IO pins on Port B.

From what I can tell your library does not use pin change interrupts, can you confirm this? 

Also you mention that PWM on pin D10 can't be used.  I assume I can still use D10 for other functions just not PWM?

How does code size compare to NSS?

Thanks again for this library, it will probably save me a lot of time!
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

0
Offline Offline
God Member
*****
Karma: 26
Posts: 626
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

From what I can tell your library does not use pin change interrupts, can you confirm this?

Confirmed, pin change interrupts are not used.

Quote
Also you mention that PWM on pin D10 can't be used.  I assume I can still use D10 for other functions just not PWM?

Yes.  I've added some text to the web page to clarify this point.

Quote
How does code size compare to NSS?

I didn't compare.  Maybe you can tell me?

Just click "Verify", make a note of the size, then change just a couple lines your code to switch to AltSoftSerial, and click Verify again.  Subtract the numbers.... pretty easy!
Logged

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Paul.  So far so good, it saved me 900 bytes. 
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

Pages: [1] 2   Go Up
Jump to: