Page 2 of 2

Re: Send 64 bit from one Raspberry PI 3 B to another

Posted: Tue Jun 12, 2018 9:17 pm
by DougieLawson

Re: Send 64 bit from one Raspberry PI 3 B to another

Posted: Tue Jun 12, 2018 11:32 pm
by procount
Hi @steve041264
What other posters are failing to point out to you is why your program has not got a chance of working. You are correct in that it is lacking synchronization, but it is much deeper than that.

Your transmit program is blindly wiggling a GPIO pin up and down according to the value of the next bit in your bit stream. Your receive program is blindly looping round reading the state of a GPIO pin and inserting it into an array of bits but you have no synchronisation at all between these programs.
How fast is the transmit loop? How long is the GPIO pin in each bit state (i.e. how wide is a bit period)?
How fast is the receive loop? Is it the same as the transmit loop? Even if it is, how do you know it is reading in the middle of the bit and not at the edge of a bit where the data might be an indeterminate value? What if the timing of the receive loop drifts relative to the transmit loop? Too fast and you read the same bit twice. Too slow and it misses a bit. What if your RPis are at different speeds e.g. a 3B+ vs a PiZero?

I see you have put start and stop message markers (9999 and 10000) at the message level in your bit stream, but you have not considered how to send one bit reliably and accurately between the 2 systems.
Having decided that, you need to consider how to send one byte. Then how to combine those bytes into larger 16, 32 or 64 bit values.
Finally how to group those values into a larger messages that the receiver can understand and parse.
Finally all good communication protocols include some form of error detection at the bit, byte and/or message level. Some also include error correction.

Most of these decisions are normally handled for you by the hardware interfaces and communication protocols that the other posters are steering you towards (I2C, SPI, RS232 etc). If you cannot use these, then you will need a software library that implements them, or you need to roll your own implementation like @TElmed has suggested. You can bet that the 433MHz transceivers you talk about use a complicated communication protocol between the GPIO pin and the transceiver - they don't just send the bits out one by one as fast as they can.

If you insist to only use 1 signal wire and gnd for your interface and not use a separate clock signal, then you will have to make sure that both systems generate their own clocks that operate at roughly the same speed, then you will need to synchronise the receive clock to the transmit clock by waiting for the first start bit of the message. So start by setting the TX GPIO pin high for many seconds, start the RX program and get it to wait until the RX GPIO pin goes low. Then when the TX GPIO pin goes low it signals the start of the transmission. The RX program should wait one and a half bit periods then sample the RX GPIO Pin in the middle of bit 1. then wait 1 bit period to sample in the middle of bit 2 and so on. Since the clocks will drift, it can only do this for a limited time, say 1 byte. Then there will need to be another gap (where the GPIO pins should be set high) and the Receiver must wait for another start bit for the next byte and so on. This is basically how RS232 serial links work. You can get more fancy by adding parity and stop bits for better reliability.

You then need to start thinking about a message protocol. i.e. how the receiver interprets the stream of bytes as a message. You already added start and stop words (9999 and 1000) to delineate the messages. that's a start. You may want to add checksums to make sure no errors have occurred in the message. Communication protocols also add additional fields to describe the data rather than relying on each end of the line knowing what the format of the message is beforehand - this makes it more flexible. (This is mostly repeating what @@TElmed has already said).

By far the hardest part of rolling your own serial interface is the timing. That's why the posters are pointing you at ready-made hardware and software solutions. I would take their advice.

Hope that helps.

Re: Send 64 bit from one Raspberry PI 3 B to another

Posted: Wed Jun 13, 2018 6:31 am
by Arjan
DougieLawson wrote:
Tue Jun 12, 2018 9:17 pm
Page 160 in ... herals.pdf
The BSC interface can be used as either a Broadcom Serial Controller (BSC) or a Serial Peripheral Interface (SPI) controller.
The SPI controller implements 3 wire serial protocol variously called Serial Peripheral Interface (SPI) or Synchronous Serial Protocol (SSP). BSC and SPI controllers do not have DMA connected, hence DMA is not supported.

This tells me that it can work. Can SPI Slave work with this SoC?

Re: Send 64 bit from one Raspberry PI 3 B to another

Posted: Wed Jun 20, 2018 9:34 am
by procount

I thought this youtube video might be helpful as it explains the synchronization problems pretty well, although he solves it by adding a separate clock signal to make it synchronous, rather than by adding start/stop bits to keep it asynchronous. Although he uses an Arduino rather than a Raspberry Pi, the principles still apply.

Re: Send 64 bit from one Raspberry PI 3 B to another

Posted: Wed Jun 20, 2018 7:02 pm
by jdb
Absent any sensible reason why the UART, or SPI is unsuitable...

Use a data pin and a strobe pin. The transmitter has an active-high strobe and asserts it for some period of time V over a total time T (where V < T, ideally V = 0.5T). It changes the value of the data pin when you command V to be de-asserted (i.e. 1->0).

Your receiver checks for rising edges on the strobe by comparing the last read value with the current one, and immediately after reading a 0->1 transition you sample the data GPIO. You ignore the data GPIO otherwise. On top of that, build a synchronisation protocol such as used in SPDIF (BMC with illegal symbols that denote the start of data words).

Then all you do is turn the transmit speed down (increasing T) until you get reliable communication at both ends. The speed will be terrible.

Re: Send 64 bit from one Raspberry PI 3 B to another

Posted: Wed Jun 20, 2018 7:05 pm
by Arjan
jdb wrote:
Wed Jun 20, 2018 7:02 pm
Absent any sensible reason why the UART, or SPI is unsuitable...
Are you saying that the SPI Slave should work? If so, then please can you provide a pointer to code?