An alternative Serial Library for Arduino 1.0

For receive parity errors it would be difficult to flag each character since I buffer 8-bits. I could set a flag indicating a parity error occurred in some character and the user could check and clear it. Is that adequate?

Any step forward is OK :slight_smile:

The avr USART is capable of character sizes of 5, 6, 7, 8, and 9 bits. Nine data bits would be difficult since I use an 8-bit data type for ring buffers.

Don't lnow of any device using 9 databits (never used it either) so don't care about that one. The 5,6,7 are still interesting.

The # stopbits support sounds good too! Do you have a link to a PDF describing the USART?
Stopbits come from a time where devices needed time to process the incoming data (think mechanical teletypes), and to be sure to give them enough time. Zero stopbits came in when local buffering removed that need.

I am familiar with your (great!) highspeed SD work so now I understand your 16 bit int choice better (add this rationale in the readme file ?)

You can't access the ring buffer when there is no buffer. It is possible that the ring buffer doesn't exist if BUFFERED_RX is zero.

You're right, I missed that in my quick review

New code looks better imho, well done!

Atmel has a number or app notes but I just look in one of the processor datasheets for USART info. The 328 data sheet is here http://www.atmel.com/dyn/resources/prod_documents/doc8161.pdf. Chapter 19 is the USART.

I did some research on stop bits and there must be at least one stop bit. I maintained terminal concentrators in the early 1970s and 1, 1.5, and 2 stop bits were common with 1.5 and 2 used to accommodate mechanical printers like teletypes.

You still need one stop bit for async serial. This is necessary to re-sync the receive clock.

TTL uses logic High (+5 V) for a mark, and logic low (0 V) for a space (hope I remember this correctly).

Since the start bit is space or logic low (0) and the stop bit is mark or logic high (1), there is always a clear demarcation between the previous character and the next one. If there were no stop bit and all data bits were low there would be no way to frame the characters. A sequence of zeros would just hold the line low.

I plan to have a version of begin with a second parameter for options. It will just be bits to set in UCSRC. This will allow even, odd, and no parity. One or two stop bits and 5, 6, 7, or 8 bit characters.

I will save any receive error bits. This will include ring-buffer overrun, USART receive overrun, parity error, and framing error. I will or these bits into a variable that can be read or cleared by new API functions. I think I will only do this for buffered receive in the ISR. I mainly did unbuffered receive for the case where you only want to do output. I need to put the error variable in the ring buffer so the ISR can access it.

I've been playing with this for most of the afternoon and think I've found a problem. I'm accessing the serial port code via Stream* so that I don't know whether it's HardwareSerial or SerialPort. However the Stream overrides in SerialPort aren't marked virtual.

I'm not seeing any buffering here when I write to the serial port.

Checking out the code I see you've gone for inlining everywhere you can. Unfortunately inlining with virtual functions can cause problems, actually taking more code space than when not inlining.

Iain

I didn't mark the overrides as virtual since I didn't want someone to override my functions in a class derived from SerialPort since SerialPort is a template.

I understand the inline problem but this code is a beta prototype to test the API and use of templates for buffer size. It's a pain to move functions out of a template to make them non-inline at this stage. I will optimize flash use later.

I have already totally changed the internal structure by making SerialRingBuffer and SerialRegisters classes and moving most of the code to these classes which don't have inline functions plus it reduces duplicated code for RX and TX. That reduced flash use a lot.

Write is now just these two lines

      // wait for TX ISR if buffer is full
      while (!txbuf_->put(b)) {}
      // enable interrupts
      usart_->ucsrb |= M_UDRIE;

I tested the input buffering buffering in the MegaTest program. It couldn't work if there was no input buffering.

This sketch tests output buffering:

#include <SerialPort.h>
SerialPort<0, 63, 63> NewSerial;

// use macro to substitute for Serial
#define Serial NewSerial

void setup() {
  Serial.begin(9600);
  uint32_t t = micros();
  Serial.println("12345678901234567890");
  t = micros() - t;
  Serial.print("micros: ");
  Serial.println(t);
}
void loop() {}

It writes 20 characters in 220 microseconds so output buffering is working. Here is the output:

12345678901234567890
micros: 220

But not if you access it via Stream*.

Iain

I take that back. I've just written a simple test program to access via Stream* and buffering still happens.

I'd assumed the slowdown in my original app (when I used SerialPort) was due to a lack of buffering. That's not the case so I'll have a good look at my code.

Iain

I will save any receive error bits. This will include ring-buffer overrun, USART receive overrun, parity error, and framing error. I will or these bits into a variable that can be read or cleared by new API functions. I think I will only do this for buffered receive in the ISR. I mainly did unbuffered receive for the case where you only want to do output. I need to put the error variable in the ring buffer so the ISR can access it.

One possible useful function of analyzing the receive error bits is to be able to pass on the rather obscure 'break condition' to a users sketch. The break condition (both sending it and detecting it) is seldom seen or used in the micro-controller world but was used in mainframe and minicomputer systems in the past as a way a remote receiver could 'interrupt' a incoming serial stream by generating the break 'signal' on it's transmit line to the host. When the sending host detected the break condition on it's receive input it would stop transmission and take whatever action the agreed protocol was. I believe the break condition is defined as any space condition lasting longer then a valid frame time length? That is longer then a valid complete character length, but possibly longer?

In the old teletype days a continuous break condition would cause the machine to 'run open' making a very distinctive and noticeable sound that would be noticed by the operators and corrective action taken as it normally meant a broken communications channel.

Lefty

There could very well be bugs so let me know if you find a slowdown problem. I only put two days into this so far. I haven't done much testing.

I put this code out so I could get comments while I am early in development. I really appreciate suggestions like robtillaart's suggestion to add parity, character size, and stop-bit options. Also to return error information for parity, framing, receiver overruns, and buffer overruns.

retrolefty,

I don't see a way to detect break in the avr USART. It probably would give a framing error.

It probably would give a framing error.

Yep, according to the 328 data sheet a 0 stop bit is a framing error.


Rob

fat16lib:
retrolefty,

I don't see a way to detect break in the avr USART. It probably would give a framing error.

Yes, framing error would be the only applicable detection from the existing error codes i believe. So how would one differentiate a purposely sent break condition from a borked character error? Probably can't and maybe one reason one doesn't see the break feature used much anymore. Just wanted to throw out my fuzzy memory of the 'break condition' subject and don't propose that it is all that useful a feature anymore. It was more commonly used in half-duplex comm links as a poor mans 'back channel' method.

Yes, I'm old but my lawn is in good shape so stay off it. :wink:

Lefty

fat16lib:
There could very well be bugs so let me know if you find a slowdown problem. I only put two days into this so far. I haven't done much testing.

I put this code out so I could get comments while I am early in development. I really appreciate suggestions like robtillaart's suggestion to add parity, character size, and stop-bit options. Also to return error information for parity, framing, receiver overruns, and buffer overruns.

OK. It's taken a while to isolate the problem. It seems that creating the second serial port is causing all the problems. This simple program demonstrates the problem. I've no idea what's causing it but if you don't add the SerialPort<1, 16, 16> but it's fine. No idea if the parameters are important. Each loop should report either 0us or 4us. However after the first loop, it reports 156us each time. I'm guessing something in an interrupt is eating all my cycles.

#include <SerialPort.h>

static SerialPort<0, 0, 256> serialPort;
static SerialPort<1, 16, 16> serialPort1;

void setup(void)
{
	SerialPort<0, 0, 256> serialPort;

	serialPort.begin(115200);
	serialPort1.begin(115200);
	for (int route = 0; route < 11; route++)
	{
		uint32_t start, stop;

		start = micros();
		stop = micros();
		serialPort.print(stop - start, DEC);
		serialPort.println("us");
		delay(400);
	}
}

void loop(void)
{
}

Hope that helps.

Iain

Looks like I didn't clean up the code correctly. Remove the SerialPort<> declaration inside setup().

Here's the cleaner version:

#include <SerialPort.h>

static SerialPort<0, 0, 256> serialPort;
static SerialPort<1, 16, 16> serialPort1;

void setup(void)
{
	serialPort.begin(115200);
	serialPort1.begin(115200);
	for (int route = 0; route < 11; route++)
	{
		uint32_t start, stop;

		start = micros();
		stop = micros();
		serialPort.print(stop - start, DEC);
		serialPort.println("us");
		delay(400);
	}
}

void loop(void)
{
}

Iain

I run your example with the version SerialPortBeta20111230.zip on a Mega and get this??

4us
0us
0us
4us
4us
4us
4us
0us
4us
0us
4us

I've run it on two different Mega's and that's not what I get. :-((

Just run it again and it's not going wrong here either. I'll have another look in the morning.

Iain

Do you have anything connected to port1? If nothing is connected, you could get really high speed framing errors on port1 from noise.

I put a wire in RX1 and get your result and the RX1 buffer is full of junk.

You could try connecting RX1 to 5V so it appears to be connected to an idle port. Serial ports idle HIGH.

Edit: There is a bug when the RX1 buffer is full. I don't clear the interrupt when I can't buffer the character.

Here is a fix for the receive ISR:

//------------------------------------------------------------------------------
static  void rx_isr(volatile uint8_t* udr, SerialRingBuffer* pb) {
  uint8_t b = *udr;
  uint16_t tmp = pb->head + 1;
  if (tmp >= pb->size) tmp = 0;
  if (tmp != pb->tail) {
    pb->buf[pb->head] = b;
    pb->head = tmp;
  }
}

I posted the fixed version as SerialPortBeta20111231.zip here Google Code Archive - Long-term storage for Google Code Project Hosting..

fat16lib:
Do you have anything connected to port1? If nothing is connected, you could get really high speed framing errors on port1 from noise.

I put a wire in RX1 and get your result and the RX1 buffer is full of junk.

You could try connecting RX1 to 5V so it appears to be connected to an idle port. Serial ports idle HIGH.

Edit: There is a bug when the RX1 buffer is full. I don't clear the interrupt when I can't buffer the character.

That would explain my issue. Another Mega is connected to serial port 1 and it streams data continuously. So when this Mega starts up, before I can do anything the port will be full. I'll check out the fix when I power up the complete system and report back.

Thanks for your efforts. Glad you could figure it out from my incomplete ramblings. I hadn't realised the importance of my system since I was seeing timing issues on Serial0, and only discovered that Serial1 was required well after midnight.

Iain

Thanks for the fix. It's all working correctly now.

Iain

I'd like to know if it's possible to have a faster method to upload a string to the the transmit buffer.

In my system I have a maximum processing time of 390us. This all works well but when debugging, the amount of text output causes the timing to be exceeded. I was hoping that a larger output buffer would help. It's definitely an improvement over HardwareSerial.

I've been testing with a 29 character message and HardwareSerial took 892us to send the message. Using SerialPort with a transmit buffer size of 32 bytes it's down to 168us. However just doing a memory copy takes 16us.

So I've been wondering if there's a way to allow the SerialPort class to have a fast copy into the transmit buffer?

#include <SerialPort.h>

SerialPort<0, 0, 32> port;

void setup(void)
{
	port.begin(115200);
	for (int route = 0; route < 11; route++)
	{
		uint32_t start;

		start = micros();
		DebugPrintLine("Selecting passenger route x");
		ShowMessageTime(start, micros());
		start = micros();
		DebugPrintLine2("Selecting passenger route x");
		ShowMessageTime(start, micros());
	}
}

void loop(void)
{
}

void DebugPrintLine(const char* message)
{
	while (*message != '\0')
	{
		port.write(*message++);
	}

	port.write('\r');
	port.write('\n');
}

void DebugPrintLine2(const char* message)
{
	char buffer[50];
	char* dest = buffer;

	while (*dest++ = *message++)
		;

	*dest++ = '\r';
	*dest++ = '\n';
}

void ShowMessageTime(uint32_t start, uint32_t stop)
{
	port.print("Message time:");
	port.print(stop - start, DEC);
	port.println("us");
	delay(400);
}

Iain