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

samd: Size-optimize the temperature code with an 0.5 to 2.0 degree relative additional error #7045

Merged

Conversation

jepler
Copy link
Member

@jepler jepler commented Oct 12, 2022

Convert most of the math to integer, and don't be too prissy about rounding.

I used a standalone program together with my particular device's calibration values of

const uint32_t NVMCTRL_TEMP_LOG[]={0xfc05511e, 0xcc7ac0f7};

and found the "errors" at 0..60°C, meaning the difference relative to the existing reference code.

At 25°C the first commit introduces an 0.5°C relative change. The second commit introduces a 1°C relative change, while the biggest change over the whole 0°C—60°C range was 2°C.

The total size savings appears to be 268 bytes, based on nm --print-size of the mcu_processor_get_temperature function in the trinket m0 build.

I also tested that the temperature value remains plausible-looking on a PyRuler.

jepler and others added 4 commits October 12, 2022 15:44
Perform most arithmetic with scaled integer values.
For my calibration values
```
const uint32_t NVMCTRL_TEMP_LOG[]={0xfc05511e, 0xcc7ac0f7};
```
the maximum difference between the old and new calculation is 0.50°C.
The difference is smallest (0.13°) at 25.87°C in the old scale.

This reduces mcu_processor_get_temperature from 568 bytes to 348 bytes
(-220 bytes)
Another reduction of -48 bytes can be had if the fine calculation
step is skipped. The worst difference compared to the old reference
code with my calibration values in the 0° to 60° was 2°C,
and the difference at 25°C is 1°C.

The final size decrease for non-full builds like Trinket M0 is 268
bytes.
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

I added some caveat documentation. Thanks for looking at this. I always thought the code size looked large, but was concerned about making it less accurate, which doesn't really matter.

@dhalbert dhalbert merged commit 1569c7e into adafruit:main Oct 13, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants