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

Friendly Register Assignments #5

Open
KevinMechler opened this issue Nov 26, 2021 · 0 comments
Open

Friendly Register Assignments #5

KevinMechler opened this issue Nov 26, 2021 · 0 comments

Comments

@KevinMechler
Copy link

Hi again. I can't help but notice that the way you assign values to registers is not friendly or... safe?

void icm20948_clock_source(uint8_t source)
{
	uint8_t new_val = read_single_icm20948_reg(ub_0, B0_PWR_MGMT_1);
	new_val |= source;

	write_single_icm20948_reg(ub_0, B0_PWR_MGMT_1, new_val);
}

In this snippet, you |= two values, but if I used a number like 0x41 instead of 0x01 here, I could theoretically put the chip into sleep instead of correctly setting the clock source.

Now, in the event that I wanted to set the clock source as 0x07, which I think is valid, and then later change it back to 0x01, I could not with your code.

Consider redoing your assignments to something that clears the bits for the field in the register before OR-ing them.

new_val = (new_val & 0xF8) | (source & 0x07);

would be a much safer way of preventing accidental changes to registers/configs, I think. Nearly every register config function should probably be updated like this.

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