Modify serial buffer library for Teensy 3.1

Status
Not open for further replies.

mick

Active member
Hi there,

Could anyone give me some pointers on how to modify a library so it functions on the Teensy?

Want to adapt this serial buffer library https://github.com/siggiorn/arduino-buffered-serial

it seems in BufferedSerial.h there's a definition thats processor dependent. I'd probably need to include something like this, only I'm not sure what definitions to use.

Code:
#if defined(__MK20DX256__)
	#define UCSRA1	UCSR1A
	#define UCSRA2	UCSR2A
	#define UCSRA3	UCSR3A
#endif


Best and thanks!
 
Others can probably answer this better than I can, but will give you a quick idea of what I might do if I were using this,

Looking at at github, not sure exactly whey they do some of the stuff that they do here, but the main problem I see is they are using AVR hardware registers, in the section of code...
Code:
	// If we should be sending stuff
	if( outgoing_buffer->getSize() > 0 ){
		// If serial port not currently busy
		if( _serial_port == 0 )
			if(((UCSRA) & (1 << UDRE)) ) Serial.write( outgoing_buffer->get() );

#if defined(__AVR_ATmega1280__)
		else if( _serial_port == 1 )
			if(((UCSR1A) & (1 << UDRE1)) ) Serial1.write( outgoing_buffer->get() );
		else if( _serial_port == 2 )
			if(((UCSR2A) & (1 << UDRE2)) ) Serial2.write( outgoing_buffer->get() );
		else if( _serial_port == 3 )
			if(((UCSR3A) & (1 << UDRE3)) ) Serial3.write( outgoing_buffer->get() );
#endif
	}
I would have a tendency to modify this to work with Teensy by doing something like:
Code:
	// If we should be sending stuff
	if( outgoing_buffer->getSize() > 0 ){
		// If serial port not currently busy
		if( _serial_port == 0 )
			if(Serial.availableForWrite()) Serial.write( outgoing_buffer->get() );
		else if( _serial_port == 1 )
			if(Serial1.availableForwrite()) Serial1.write( outgoing_buffer->get() );
		else if( _serial_port == 2 )
			if(Serial2.availableForWrite()) Serial2.write( outgoing_buffer->get() );
		else if( _serial_port == 3 )
			if(Serial3.availableForWrite()) Serial3.write( outgoing_buffer->get() );
	}
Again under ifdef for Teensy... Also note, if I were doing this, I might also change it to write more than one byte at a time. That is you have know the number of bytes you want to write and you know how many bytes you can add to serial buffer without waiting so output the minimum of those two...

Also if I were doing this, I might hold onto a pointer to the hardware serial class so I did not have to do this level of if branching...
something like: if (_pserial->availableForWrite()) _pserial->write(...)

Hope that helps
Kurt
 
Last edited:
Hi Kurt,

Thanks for helping out. On second thought I think you're right, this library has a lot less to offer than one might think. I'd really like to send strings as well. Made a similar protocol recently for i2c, think I'll just try to borrow from this library what is handy and start from scratch. Just wish there was a robust protocol library out there already, things like headers, packages, escaping characters, checksums and so on would be really handy for a lot of projects.

Best,
Mick
 
Status
Not open for further replies.
Back
Top