Skip to content
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

Sequential Register Writing/Reading #30

Open
bruno-f-cruz opened this issue Dec 21, 2023 Discussed in #27 · 0 comments
Open

Sequential Register Writing/Reading #30

bruno-f-cruz opened this issue Dec 21, 2023 Discussed in #27 · 0 comments

Comments

@bruno-f-cruz
Copy link
Member

Discussed in #27

Originally posted by Poofjunior May 18, 2023
Hi everyone,

I'd like to propose a minor feature tweak to the existing (and future) harp protocol.

For register reads and writes, I propose that if we read/write more than the specified number of bytes, we simply continue reading/writing sequentially from the next register.

The main benefit is that we can read/write groups of sequential registers. For future harp devices, we can then group related registers together, which enables us to read/write structs.

In C++, this is straightforward to implement if the register data is stored in a packed struct.

// Example register values stored contiguously in memory with a packed struct (i.e no padding).
#pragma pack(push, 1)
struct RegValues
{
    const int16_t R_POSITION_X;
    const int16_t R_POSITION_Y;
    const int16_t R_POSITION_Z;
    const uint8_t R_RESET_POSITION;
};
#pragma pack(pop)

Suppose we want to read out the x,y, and z position as a single harp read request, instead of 3 register read requests. Then, on the client side, we simply ask for 6 bytes. Since they're stored contiguously in memory with no padding, the embedded device can just memcpy it into the outgoing packet. In fact, if the data on the harp device is already stored contiguously in memory, then little-to-no changes on the embedded device need to be adjusted.

Here's some "client-side" pseudocode.

dev = harp_device("3D_JOYSTICK")
x = dev.read_request(reg=R_POSITION_X, bytes=2).payload.unpack('<l')
y = dev.read_request(reg=R_POSITION_Y, bytes=2).payload.unpack('<l')
z = dev.read_request(reg=R_POSITION_Z, bytes=2).payload.unpack('<l')

# versus

position_xyz = dev.read_request(reg=R_POSITION_X, bytes=6).payload.unpack('<lll')  # Returns a payload of 6 bytes, aka: 3-little-endian ints

For reference, this is a common way of interacting with a number of ASICs with a register map on serial interfaces like i2c or SPI. Examples include sensors from Bosch like the BMA180, BNO085, etc.

Overall, I think this small tweak lets you basically send/receive groups of data in of mixed payload sizes that can be then reinterpreted from the client side as a struct. This feature is also backward-compatible with existing systems without necessitating any changes. They can simply ignore this feature.

LMK what y'all think!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant