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

Possible simplication (or error) of sx127x/sx126x frequency to pll step calculation #87

Open
plaes opened this issue Sep 3, 2024 · 1 comment

Comments

@plaes
Copy link

plaes commented Sep 3, 2024

When staring at the sx127x method of calculation PLL step from frequency, I spotted that resulting value for steps_fraq will always be 0:

// Get integer and fractional parts of the frequency computed with a PLL step scaled value
steps_int = freq_in_hz / SX127X_PLL_STEP_SCALED;
steps_frac = freq_in_hz - ( steps_int * SX127X_PLL_STEP_SCALED );

Proof:

steps_fraq = freq_in_hz - ( steps_int * SX127X_PLL_STEP_SCALED );
 // substitute steps_int with its expression:
steps_freq = freq_in_hz - ((freq_in_hz / SX127X_PLL_STEP_SCALED) * SX127X_PLL_STEP_SCALED);
// (x / SX127X_PLL_STEP_SCALED) * SX127X_PLL_STEP_SCALED == x
steps_freq = freq_in_hz - freq_in_hz;
steps_freq = 0;

This would also allow simplifying the return expression to

return (steps_int << SX127X_PLL_STEP_SHIFT_AMOUNT) +  (( SX127X_PLL_STEP_SCALED >> 1 ) / SX127X_PLL_STEP_SCALED );

And after some tests we can drop off the second term which is 0 ending up with just steps_int << SX127X_PLL_STEP_SHIFT_AMOUNT

Update: Same simplification can be applied in the sx126x driver as well:

// Get integer and fractional parts of the frequency computed with a PLL step scaled value
steps_int = freq_in_hz / SX126X_PLL_STEP_SCALED;
steps_frac = freq_in_hz - ( steps_int * SX126X_PLL_STEP_SCALED );
// Apply the scaling factor to retrieve a frequency in Hz (+ ceiling)
return ( steps_int << SX126X_PLL_STEP_SHIFT_AMOUNT ) +
( ( ( steps_frac << SX126X_PLL_STEP_SHIFT_AMOUNT ) + ( SX126X_PLL_STEP_SCALED >> 1 ) ) /
SX126X_PLL_STEP_SCALED );
}

@plaes plaes changed the title Possible simplication (or error) of sx127x frequency to pll step calculation Possible simplication (or error) of sx127x/sx126x frequency to pll step calculation Sep 6, 2024
@opeyrard
Copy link

Hi,
(freq_in_hz / SX126X_PLL_STEP_SCALED) x SX126X_PLL_STEP_SCALED is not equal to freq_in_hz in integer in C.
Many thanks,
Best regards,

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

2 participants