Adding a serial port using the SC16IS740


#1

I added a library, SC16IS740RK, that makes it possible to use up to 4 SC16IS740 I2C UARTs off the I2C bus (D0 and D1). The chips support from 50 to 115200 baud and can go higher with different oscillators.

Basically, just create the object and call begin() and it works like a regular UART like Serial1:

#include "SC16IS740RK.h"

// Pick a debug level from one of these two:
SerialLogHandler logHandler;
// SerialLogHandler logHandler(LOG_LEVEL_TRACE);

SC16IS740 extSerial(Wire, 0);

char out = ' ';

void setup() {
	Serial.begin(9600);

	delay(5000);

	extSerial.begin(9600);
}

void loop() {
	while(extSerial.available()) {
		int c = extSerial.read();
		Log.info("received %d", c);
	}

	extSerial.print(out);
	if (++out >= 127) {
		out = ' ';
	}
	delay(100);
}

Tested with both the Photon and Electron and 0.7.0:

On the Github there’s also the Eagle CAD files for making the handy breakout board in the picture and the BOM.

This is just the first version and I’ll eventually add interrupt support, flow control, and possibly SPI interface support.


Ethernet FeatherWing with Xenon - Serial 2
How to use 3 serial ports at a time?
Two software serial port using ParticleSoftSerial library
#2

@rickkas7, nice job on the library! Having interrupts and SPI would make those things fly!


#4

OK, I just designed a SPI with hardware flow control board and sent it off to fab, so I’ll write the software for those features when I get the boards back.


#5

I’m trying to use your library for the SC16IS740RK over SPI on an Argon with 0.8.0-rc.27.

Printing a single character with extSerial.print('a') works fine but printing a string with extSerial.print("ab") doesn’t work and the Argon flashes SOS 10 / Assertion failure.

Minimal code:

#include "SC16IS740RK.h"

SC16IS740SPI extSerial(SPI, A2);

void setup() {
	extSerial.begin(9600);
	extSerial.print("ab");
}

void loop() { }

I can receive the single character at the output, so the hardware setup seems to be fine.

Is this a bug in the library or in the particle firmware or am I doing something wrong?


#6

Can you try extSerial.write("ab", 3);?


#7

Same problem with extSerial.write((uint8_t*)"ab", 3)

Splitting in

	extSerial.print('a');
	extSerial.print('b');
	extSerial.print('\0');

works fine.


#8

I know there were (are) some issues with SPI and rc.27 (at least in the implementation of SPI.beginTransaction()) so it might well be an issue with the implementation of SPI on the mesh devices.

So until Rick can report back, you could copy/paste the sources for that library and add some Serial.print() statements in the SC16IS740SPI::writeInternal() and SC16IS740Base::write() functions to home in on the offending instruction.


#9

I can reproduce the problem in SPI mode but not in I2C mode. It could be a SPI DMA issue, but I’ll have to dig into it further later.


#10

AFAICT you are not using DMA in your implementation

	spi.transfer(const_cast<uint8_t *>(buffer), NULL, size, NULL);

#11

I released version 0.0.3 of the library. On mesh devices, I temporarily disabled the multi-byte SPI transfer mode since that appears to be the source of the SOS fault. It does multiple single-byte SPI transfers, which seems to work fine.


#12

Wow, that was fast!
0.0.3 fixed the issue, thank you very much! This library is perfect for me as I need three serial ports on a mesh device. It makes using these ICs really easy.

Also thank you to @ScruffR for your assistance.