An alternative Serial Library for Arduino 1.0

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

It should be possible to speed up write(const char* str).

I now use the default implementation in Print which is not very efficient. Here is the default version:

    size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); }

I also use the default version of write(const uint8_t*, size_t)

size_t Print::write(const uint8_t *buffer, size_t size)
{
  size_t n = 0;
  while (size--) {
    n += write(*buffer++);
  }
  return n;
}

If I override write(const uint8_t*, size_t) with an efficient version that just copies the bytes into the ring buffer it should speed up. I will try this but it may be a few days.

Thanks for suggesting this. I now must redesign/rewrite the internals to implement all the good suggestions people have made.

An efficient copy of the write methods would be good. I've also discovered that accessing the SerialPort class via Stream* is bad for performance. The message which took 168us before, takes 244us via Stream*. I'm guessing that's due to the extra indirection caused by virtual functions. So having efficient write methods in SerialPort would be excellent.

Iain

After a few seconds thought, I realized that an efficient read would also be good.

int read(uint8_t* buf, size_t size);

would return as much input data as possible. I use int for the return type in case I need -1 for an error return. I guess input buffer overrun might be such a condition.

My to-do list is growing really fast.