Bit-banged 8-port RS-232 communication library

Discussion and questions about programming with Ultibo.
User avatar
Ultibo
Site Admin
Posts: 2291
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: Bit-banged 8-port RS-232 communication library

Postby Ultibo » Fri Aug 16, 2019 12:01 am

ZeroHobby wrote:Course, then I really wouldn't need Ultibo, as Raspbian could handle all the serial ports.

Unless you are also interested in reduced complexity and increased reliability.

We think that a single application immutable binary image like Ultibo will produce a more reliable (and more maintainable) result than the gigabyte plus installation of a general purpose OS. It's no secret that we don't think Linux is a good solution for embedded devices especially given the enormous amount of baggage that it carries from its all things to all people philosophy, something we know from history to be unattainable in almost any field.

PS. We don't want to pollute this thread with a discussion about the merits of one platform over another, we're happy to have that discussion but let's make it a separate thread if there are strong views about it ;)
Ultibo.org | Make something amazing
https://ultibo.org
ZeroHobby
Posts: 7
Joined: Fri Aug 09, 2019 2:11 pm

Re: Bit-banged 8-port RS-232 communication library

Postby ZeroHobby » Fri Aug 16, 2019 1:28 am

Those are all excellent suggestions. I found a used 8-port box for about $39 on eBay. No saying what the power requirements might be, though. However, if cost is figured in, we have a bit of a problem. With a Parallax Propeller, I could easily implement a dozen serial ports with large FIFO buffers and any reasonable baud rate...and it costs $8 (not to mention that I already have two). The point of my thread (and project) is to see how much I can get the Pi Zero to do by itself, seeing as it's far more powerful than any MCUs I've used up to this point. What's at stake is to see whether a $5 Raspberry Pi Zero can do what an $8 Parallax Propeller can do...better ;-)
stevenpostma
Posts: 11
Joined: Thu Jan 18, 2018 5:44 pm

Re: Bit-banged 8-port RS-232 communication library

Postby stevenpostma » Fri Aug 16, 2019 9:32 am

If you want to make a software-only 8x serial port on a rPi, then you need more I/O pins.
I would suggest to use a NXP PCA9701 16-bits SPI port expander that can read and write all 16 bits in a single SPI transfer.
So all rPi hardware you need is 4 pins, a SPI port and one timer interrupt for sampling at 3*9600 or 4*9600 Hz that does a single SPI transfer.
The software part will be more tricky, but doable.

---

Another way to go would be to use I2S with DMA to provide a realtime I/O stream; see
https://github.com/lhartmann/orangepi-i2s-steppers
https://gist.github.com/cnlohr/2b9f8a26 ... 4cff9d04fe

The I2S DMA option would greatly relax the interrupt timing requirement,
for you could process all collected samples in batches of any suitable length.

I don't think anyone has implemented this yet anywhere,
so it would be a great achievement if you could add 8 or more software serial ports this way!
stevenpostma
Posts: 11
Joined: Thu Jan 18, 2018 5:44 pm

Re: Bit-banged 8-port RS-232 communication library

Postby stevenpostma » Fri Aug 16, 2019 10:24 am

DMA-driven SPI mode of the rPi connected to a NXP PCA9701 is another option,
and seems to me to be the most elegant solution.

https://www.raspberrypi.org/forums/view ... p?t=191525 (with interesting comments in the source code)
https://www.raspberrypi.org/forums/view ... p?t=181154

I am very curious about your progress... highly interesting project!
stevenpostma
Posts: 11
Joined: Thu Jan 18, 2018 5:44 pm

Re: Bit-banged 8-port RS-232 communication library

Postby stevenpostma » Fri Aug 16, 2019 12:34 pm

one more useful link for DMA driven IO in written C:
https://github.com/joan2937/pigpio
ZeroHobby
Posts: 7
Joined: Fri Aug 09, 2019 2:11 pm

Re: Bit-banged 8-port RS-232 communication library

Postby ZeroHobby » Fri Aug 16, 2019 3:15 pm

stevenpostma wrote:If you want to make a software-only 8x serial port on a rPi, then you need more I/O pins.
I would suggest to use a NXP PCA9701 16-bits SPI port expander that can read and write all 16 bits in a single SPI transfer.
So all rPi hardware you need is 4 pins, a SPI port and one timer interrupt for sampling at 3*9600 or 4*9600 Hz that does a single SPI transfer.
The software part will be more tricky, but doable.

---

Another way to go would be to use I2S with DMA to provide a realtime I/O stream; see
https://github.com/lhartmann/orangepi-i2s-steppers
https://gist.github.com/cnlohr/2b9f8a26 ... 4cff9d04fe

The I2S DMA option would greatly relax the interrupt timing requirement,
for you could process all collected samples in batches of any suitable length.

I don't think anyone has implemented this yet anywhere,
so it would be a great achievement if you could add 8 or more software serial ports this way!


OK, first off...I am only planning on implementing RX/TX on each port, no flow control. That way, I will only need 8 inputs, and 8 outputs (total of 16 I/Os). According to a pin map I found (https://www.raspberrypi.org/documentation/usage/gpio/), the 40-pin header has at least 26 I/Os; at least for me, there's no lack. Me personally, I could implement 12 serial ports with GPIO (=24 I/Os), as the only other device I need to handle is an I2C touchscreen LCD (=2 I/Os).

One note: the PCA9701 gives 16 INPUTS, not GPIO. It could be used to sample 16 RX lines, but TX lines would have to be handled separately.
Doing bit-banged RS-232 over SPI brings in a slightly more difficult challenge: now all of the data has to get pipelined over a serial bus. If I'm sampling at 4x 9600Hz = 76.8KHz, now ASSUMING we can get all 8 inputs and 8 outputs done over one 16-bit transfer, we have a mathematical absolute minimum SPI clock rate of 1.3MHz (likely much higher). That shouldn't be a problem with a hardware SPI module, but it's something to keep in mind.

I2S definitely would have some possibility; however, at least in my case, that would require external chips--in which case I might as well just buy some SPI-to-UART bridges, or interface a Propeller. Trying to keep it simple and cost effective.

The PigPio project definitely has some interesting links there, including sampling GPIO "between 100,000 and 1,000,000 times per second." Now THAT might be onto something! If I could use DMA to sample the I/Os for UART input, seeing as a PI has a large amount of RAM (at least comparatively speaking to my 2K MCUs!), that could greatly reduce the CPU load for actual sampling, and feasibly permit higher baud rates--though the resulting data would have to be parsed. Output TX rates could be reduced to the baud rate, as we don't need oversampling for TX.

Once I get enough other things off my plate, I'll start poking around with Ultibo and the Pi Zero. Shouldn't be that hard to get the baby steps put together.
@stevenpostma Thanks for your suggestions. Seems that something like this would be of value/interest to you.
stevenpostma
Posts: 11
Joined: Thu Jan 18, 2018 5:44 pm

Re: Bit-banged 8-port RS-232 communication library

Postby stevenpostma » Fri Aug 16, 2019 3:56 pm

indeed I overlooked that the PCA9701 is input-only.
The microchip MCP23S17 is 16-bit I/O, but is a little more complicated to use.

You are right that direct I/O without SPI is far better and easier.
Indeed this project interests me greatly!

Do you need help making a printed circuit board?
stevenpostma
Posts: 11
Joined: Thu Jan 18, 2018 5:44 pm

Re: Bit-banged 8-port RS-232 communication library

Postby stevenpostma » Fri Aug 16, 2019 4:05 pm

by the way, you can use DMA both for input sampling and output updating is you use two DMA buffers.

That the transmission is delayed by one buffer does not harm, it is asynchronous anyway.

So the only interrupt needed is to handle finished blocks of DMA transfer.

If you use Pin-Pong buffers (2x RX, and 2x TX),
the DMA engine can use one buffer while you process the other buffer.

I think that a receive DMA buffer size of just 128 samples (= 32 bits 4x oversampled)
and transmit DMA buffer size of 32 (= 32 bits 1x oversampled) should work fine,
yielding maximum reply delay of 3+3 characters.

The DMA interrupt rate will be about (baudrate / 32) !
ZeroHobby
Posts: 7
Joined: Fri Aug 09, 2019 2:11 pm

Re: Bit-banged 8-port RS-232 communication library

Postby ZeroHobby » Fri Aug 16, 2019 5:15 pm

stevenpostma wrote:indeed I overlooked that the PCA9701 is input-only.
The microchip MCP23S17 is 16-bit I/O, but is a little more complicated to use.

You are right that direct I/O without SPI is far better and easier.
Indeed this project interests me greatly!

Do you need help making a printed circuit board?


Thanks for your offer on making a PCB, but that's no problem for me, as I have a good bit of experience with KiCAD. I actually ordered some custom PCBs from JLCPCB.COM a few weeks ago (for some DC-DC LED drivers), designing the boards with KiCAD. My project board for this will be very simple: a row of four double-stacked serial ports, with one SP3232 (RS-232 level shifter) behind each block of 2 ports, and the Pi Zero connector behind that. (Oh, and a little power supply so it can run on 12vDC, and a connector for my touchscreen LCD.) Very specialized, not a production item. I'm just an electronics hobbyist having a lot of fun ;-).

Using DMA might be a good way to reduce CPU interrupt load, especially for higher baud rates. However, I think that's something I might delve into IF I need higher baud rates...and after I'm more comfortable with the Pi Zero and Ultibo :D . As of right now, I haven't even compiled a "Hello World" program with it...I've been busy debugging PIC projects, and hacking other devices.

Yes, the Microchip expander is more complicated to use, which would increase the minimum SPI clock; fortunately, it handles up to 10MHz. Still, that's more CPU footwork to make it function.
stevenpostma
Posts: 11
Joined: Thu Jan 18, 2018 5:44 pm

Re: Bit-banged 8-port RS-232 communication library

Postby stevenpostma » Mon Aug 19, 2019 8:03 am

Using DMA on an I/O port has the disadvantage that this claims all GPIO output pins for the serial ports
- even precious signals like SPI chip selects for things like LCD screens, ADCs and the like.
So this renders both SPI ports useless as far as I can see.

I think that using SPI DMA still is the most elegant way;
especially when using cheap standard logic (74HC595 and 74HC165) ;
see (use google translate for the German text):
http://www.mikrocontroller.net/topic/215399#2145277
the PDF even shows how to extend this from 8+8
to 16+16, 24+24, 32+32 bits input+output!

The fact that an SPI transfer simultaneously does an IN and an OUT transfer
suits this project idea perfectly!

By the way, I see a lot more useful uses where exact signal timing is important,
such a a software logic analyzer, diphase decoding, generation of pulse trains for
lots of WS2812 LED strings, etc...

Return to “General”

Who is online

Users browsing this forum: No registered users and 0 guests