Bringing some order in the PSiN PHY layer
As I stated in my last entry, before moving to building the MAC, I have changed the PHY layer a bit. It now uses pullups, and the receive starts when a 0 is detected. This will allow us something that should not be intentionally sought: collisions. But remember, the PSiN is not meant to be good, it's meant to be educative.
Another aspect that the PHY layer did was that the packet length determined when the PHY layer stopped reading. This is fine for a simple protocol, but not very representative of the OSI division of functions. So I had to think of a way to have the PHY layer know when to stop without knowing the packet length. Several ways to do this:
- Special electrical signals indicating packet start and end: as I am using
digitalRead(), using different voltages is out of the question. Another possibility is to use patterns with features shorter than 1 bit. I did not choose this option by the moment because it is slightly more complex, but in the future I may come back to this solution. - Special digital signals marking packet start and end: in essence, I am currently doing this for the packet start. The problem is with the end of the packet: how does the PHY know whether a digital pattern is the end of packet or just coincidentally the same bit sequence? Well, there are several solutions; first, use bit stuffing (introducing one fake bit in the data at sender that breaks the pattern, which will be discarded at the receiver side); and second, making sure that the same pattern cannot be sent in the data. I have chosen the second method, since it will require minimal changes on my code.
Since the idea of PSiN from the very beginning was to transmit simple data, a very fitting idea would be to restrict the data to ASCII characters. ASCII already has characters designed for start (STX) and end (ETX/EOT), and even ACK, which will be nice to have later on. If we limit the data to ASCII characters, we are sure that STX and ETX will not be present in the data, they have a single purpose. At PHY layer, anyhow, we cannot call them characters, but signals. Therefore, the redesign of the PHY layer includes code for detecting start (already there) and end (new, using the CLOSING constant in receive() ).
I have also modified the code to include some useful output over serial, which in the future will be optional. The serial shows the read bits, shows in real time when a packet is being received and summarizes the data in the end. With all these, here is an example of a couple of receptions:

Note that the first byte of the data shows the length of the rest of the packet (2 bytes), which will be processed in the future by the MAC layer, therefore constituting a header of the preliminary protocol. The PHY layer reads 3 bytes (header + data at MAC layer). Also note that the sequence 00000010, which reads 2 in decimal, also matches the STX character, which should not be possible. That is a problem for when we design the MAC layer header.
Furthermore, the code has been improved, commented and simplified ... to complicate it later with MAC.