Skip to content

Commit

Permalink
firmware: Fix bug in board revision detection
Browse files Browse the repository at this point in the history
Previously we added a fixed value to the ADC reading before
right-shifting. This corrected truncation error, allowing integer
comparison values to match a whole percentage number in the table of
expected ranges. This correction worked when gsg_production was true,
but it was incorrect in the other case.

We now do the math with per mille values rather than percent values, so
it is simpler to eliminate the truncation correction and instead compare
against an expected value between two whole percents. For example, we
compare the per mille value with 225 to determine whether it is closer
to 22 percent or 23 percent.
  • Loading branch information
mossmann committed Jul 31, 2024
1 parent 62f8e11 commit 196bbc8
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions firmware/src/boards/cynthion_d11/board_rev.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ void detect_hardware_revision(void)
reading = hri_adc_read_RESULT_reg(ADC);

// Convert ADC measurement to per mille of the reference voltage.
uint32_t permille = (((uint32_t)reading * 1000) + 20480) >> 12;
if (permille > 510) {
uint32_t permille = ((uint32_t)reading * 1000) >> 12;
if (permille > 515) {
permille = 1000 - permille;
gsg_production = true;
}
Expand All @@ -90,15 +90,15 @@ void detect_hardware_revision(void)
uint16_t version;
uint16_t threshold;
} revisions[] = {
{ CYNTHION_REV_0_6, 10 },
{ CYNTHION_REV_UNKNOWN, 195 },
{ CYNTHION_REV_1_4, 220 },
{ CYNTHION_REV_1_3, 240 },
{ CYNTHION_REV_1_2, 260 },
{ CYNTHION_REV_1_0, 280 },
{ CYNTHION_REV_1_1, 310 },
{ CYNTHION_REV_UNKNOWN, 480 },
{ CYNTHION_REV_0_7, 510 },
{ CYNTHION_REV_0_6, 15 },
{ CYNTHION_REV_UNKNOWN, 200 },
{ CYNTHION_REV_1_4, 225 },
{ CYNTHION_REV_1_3, 245 },
{ CYNTHION_REV_1_2, 265 },
{ CYNTHION_REV_1_0, 285 },
{ CYNTHION_REV_1_1, 315 },
{ CYNTHION_REV_UNKNOWN, 485 },
{ CYNTHION_REV_0_7, 515 },
};

int i = 0;
Expand Down

0 comments on commit 196bbc8

Please sign in to comment.