Skip to content

Dev/sync time validity #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions SynchronizationClock.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ The `Harp Synchronization Clock` is a dedicated bus that disseminates the curren

* The packet is composed of 6 bytes (`header[2]` and `timestamp_s[4]`):
- `header[2] = {0xAA, 0xAF)`
- `timestamp_s` is of type U32, little-endian, and contains the current second.
- `timestamp_s` is of type U32, little-endian, and contains the previous elapsed second.

> **Important**
>
> To avoid unexpected behaviors, only one bit at a time should be written to register `R_RESET_DEV`.
A sample logic trace is shown below:
!["SynchClockLogicAnalyzer](./assets/SyncLogicTrace.png)

## Example code

Example of a microcontroller C code:
Example of a microcontroller C code dispatching the serialized data:

```C

Expand All @@ -49,18 +48,58 @@ ISR(TCD0_OVF_vect, ISR_NAKED)
case 7:
USARTD1_DATA = *timestamp_byte2;
break;
// The final byte is dispatched much later than the previous 5.
case 1998:
USARTD1_DATA = *timestamp_byte3;
break;
}
}
```

Example of a microcontroller C++ code for converting the four received encoded bytes to the timestamp:
````C
#define HARP_SYNC_OFFSET_US (672)

// Assume 4 bytes of timestamp data (without header) have been written to this array.
alignas(uint32_t) volatile uint8_t sync_data_[4];

// reinterpret 4-byte sequence as a little-endian uint32_t.
uint32_t encoded_sec = *(reinterpret_cast<uint32_t*>(self->sync_data_));
// Convert received timestamp to the current time in microseconds.
// Add 1[s] per protocol spec since 4-byte sequence encodes the **previous** second.
uint64_t curr_us = ((static_cast<uint64_t>(encoded_sec) + 1) * 1e6) - HARP_SYNC_OFFSET_US;
````

A full example demonstrating a state machine receiving the 6-byte sequence can be found in the [Pico Core](https://github.com/AllenNeuralDynamics/harp.core.rp2040/blob/main/firmware/src/harp_synchronizer.cpp).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A full example demonstrating a state machine receiving the 6-byte sequence can be found in the [Pico Core](https://github.com/AllenNeuralDynamics/harp.core.rp2040/blob/main/firmware/src/harp_synchronizer.cpp).
A full example demonstrating a state machine receiving the 6-byte sequence can be found in the [Pico Core](https://github.com/harp-tech/core.pico/blob/main/firmware/src/harp_synchronizer.cpp).

@Poofjunior These changes look great, thanks for the greatly improved documentation! If you think we are close to transferring the pico repository I would adapt the URL of this example as suggested above.

You will notice I am implicitly suggesting a naming convention for the core repos which should be consistent with yours, but I have opened a discussion for us to review it together: https://github.com/orgs/harp-tech/discussions/120


---


## Physical connection

The physical connection is made by a simple audio cable. In the same folder of this file, you can find an [example](./PhysicalConnector.pdf) of the sender and the receiver.
The physical connection is made by a simple 3.5mm audio cable.

The connector pinout for a device *receiving* the timestamp is shown below:

!["SynchReceiverSchematic](./assets/HarpClockSyncReceiver.png)

A TVS diode is also suggested for ESD protection.

> [!IMPORTANT]
> The device receiving the timestamp must provide 3.3-5V (~10mA) on the audio jack **R** pin.

The schematic snippet for a device *sending* the timestamp is shown below:

!["SynchSenderSchematic](./assets/HarpClockSyncSender.png)

> [!NOTE]
> The device *sending* the timestamp isolates each clock output port, preventing ground loops from forming when connecting the audio jack between sender and receiver.

A supplementary PDF [example](./assets/PhysicalConnector/PhysicalConnector.pdf) of the sender and the receiver is also available.
The connector used is from `Switchcraft Inc.` with PartNo. `35RASMT2BHNTRX`.

A KiCAD schematic template for creating a Harp device based on the [RP2040](https://www.raspberrypi.com/products/rp2040/) microcontroller with circuitry for receiving the timestamp is provided through the [Pico Template](https://github.com/AllenNeuralDynamics/harp.device.pico-template).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A KiCAD schematic template for creating a Harp device based on the [RP2040](https://www.raspberrypi.com/products/rp2040/) microcontroller with circuitry for receiving the timestamp is provided through the [Pico Template](https://github.com/AllenNeuralDynamics/harp.device.pico-template).
A KiCAD schematic template for creating a Harp device based on the [RP2040](https://www.raspberrypi.com/products/rp2040/) microcontroller with circuitry for receiving the timestamp is provided through the [Pico Template](https://github.com/harp-tech/device.pico-template).

Similar to above, it might be useful to transfer the pico template repository to harp-tech. Not sure whether it should be named device or whether we should have a different convention for templates. I am happy to keep the device prefix but just leaving it out there in case anyone has any concerns.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea!

Per Kicad-9 improvements, I may make some changes and add a secondary hardware library to this template, but I haven't settled on that just yet.

The big change in V9 was the ability to create schematic "snippets" called Design Blocks and reuse them within and across projects. I paste snippets across projects manually for various power supply designs, so I may do further reading on how to tease them into a repository.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short term, I think the docs changes are usable and an improvement as-is, so I'd motion for pico hardware template repo migration later.


## Release Notes

- v1.0
Expand All @@ -73,4 +112,4 @@ The connector used is from `Switchcraft Inc.` with PartNo. `35RASMT2BHNTRX`.
* Adopt semantic versioning.

- v1.1.1
* Remove table of contents to avoid redundancy with doc generators.
* Remove table of contents to avoid redundancy with doc generators.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a missing version increment here? We can discuss what is our convention for incrementing standard document versions going forward.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might have to fill me in what you mean here. Do I need to bump a version somewhere?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a release notes section at the bottom of the document where for other updates we have been bumping release versions.

That said, this feels like it is becoming increasingly legacy, and I don't feel these really add anything on top of the versioning git provides. If the proposal in https://github.com/orgs/harp-tech/discussions/129#discussioncomment-12664412 is accepted, then I would actually motion for us to stop adding manual release notes in the files, and actually have all notes in PRs which can then be used to automatically generate notes for new releases.

If necessary we could generate back tags and create releases for older versions to keep track of when which bit was introduced, but going forward I feel it would be much simpler for everyone if there was a single version of everything protocol related.

Binary file added assets/HarpClockSyncReceiver.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/HarpClockSyncSender.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
Binary file added assets/SyncLogicTrace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.