You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**TODO:** explain something about interrupts? but don't be weird about it, I guess...
272
270
273
271
---
274
272
275
273
276
-
## Using Sio
274
+
### A little protocol
275
+
276
+
Before diving into implementation, let's take a minute to describe a *protocol*.
277
+
A protocol is a set of rules that govern communication.
278
+
279
+
The most critical communications are those that support the application's features, which we'll call *messages*.
280
+
281
+
/// Transmission errors: do not want. Transmission errors: cannot be eliminated.
282
+
/// Lots of possible ways to deal with damaged message packets.
283
+
/// Need to *detect* errors before you can deal with them.
284
+
285
+
There's always a possibility that a message will be damaged in transmission or even due to a bug.
286
+
The most important step to take in dealing with this reality is *detection* -- the application needs to know if a message was delivered successfully (or not).
287
+
To check that a message arrived intact, we'll use checksums.
288
+
Every packet sent will include a checksum of itself.
289
+
At the receiving end, the checksum can be computed again and checked against the one sent with the packet.
290
+
291
+
:::tip Checksums, a checksummary
292
+
293
+
A checksum is a computed value that depends on the value of some *input data*.
294
+
In our case, the input data is all the bytes that make up a packet.
295
+
In other words, every byte of the packet influences the sum.
296
+
297
+
<!-- A checksum of a packet can be sent alongside the packet, which the receiver can use to check if the packet arrived intact. -->
298
+
The packet includes a field for such a checksum, which is initialised to `0`.
299
+
The checksum is computed using the whole packet -- including the zero -- and the result is written to the checksum field.
300
+
When the packet checksum is recomputed now, the result will be zero!
301
+
This is a common feature of popular checksums because it makes checking if data is intact so simple.
302
+
303
+
:::
304
+
305
+
Checking the packet checksum will indicate if the message was damaged, but only the receiver will have this information.
306
+
To inform the sender we'll make a rule that every message transfer must be followed by a delivery *report*.
307
+
In terms of information, a report is a boolean value -- either the message was received intact, or not.
308
+
309
+
Because reports are so simple -- but very important -- we'll employ a simple technique to deliver them reliably.
310
+
Define two magic numbers -- one to send when the packet checksum matched and another for if it didn't.
311
+
For this tutorial we'll use `DEF STATUS_OK EQU $11` for *success* and flip every bit, giving `DEF STATUS_ERROR EQU $EE` to mean *failed*.
312
+
313
+
To increase the likelihood of the report getting interpreted correctly, we'll simply repeat the value multiple times.
314
+
At the receiving end, check each received byte -- finding just one byte equal to `STATUS_OK` will be interpreted as *success*.
315
+
316
+
:::tip
317
+
318
+
The binary values used here should be far apart in terms of [*hamming distance*](https://en.wikipedia.org/wiki/Hamming_distance).
319
+
In essence, either value should be very hard to confuse for the other, even if some bits were randomly changed.
320
+
321
+
:::
322
+
323
+
<!-- PROTOCOL RULES
324
+
- two communication "channels": application (messages) & meta (reports)
325
+
- message packet includes a checksum of itself to be validated by receiver
326
+
- every message packet is followed by a delivery report
327
+
- message begins with 'MessageType' code -->
277
328
278
-
---
279
329
280
-
**TODO:**
330
+
### SioPacket
331
+
We'll implement some functions to facilitate constructing, sending, receiving, and checking packets.
332
+
The packet functions will operate on the existing serial data buffers.
281
333
282
-
/// building serial link test program, separate to unbricked main.asm?
334
+
The packets follow a simple structure: starting with a header containing a magic number and the packet checksum, followed by the payload data.
335
+
The magic number is a constant that marks the start of a packet.
0 commit comments