Skip to content

Commit 6c2dca0

Browse files
jdswensencfriedt
authored andcommitted
counter: ds3231: replace repeated bit manipulation with bcd functions
The driver code for the Maxim DS3231 has repeated code for bit manipulation to transform time data between binary and binary coded decimal. Use the new BCD header functions instead. Signed-off-by: Jake Swensen <[email protected]>
1 parent 67459aa commit 6c2dca0

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

drivers/counter/maxim_ds3231.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,11 @@ static const uint8_t *decode_time(struct tm *tp,
301301
if (with_sec) {
302302
uint8_t reg = *rp++;
303303

304-
tp->tm_sec = 10 * ((reg >> 4) & 0x07) + (reg & 0x0F);
304+
tp->tm_sec = bcd2bin(reg & 0x7F);
305305
}
306306

307307
reg = *rp++;
308-
tp->tm_min = 10 * ((reg >> 4) & 0x07) + (reg & 0x0F);
308+
tp->tm_min = bcd2bin(reg & 0x7F);
309309

310310
reg = *rp++;
311311
tp->tm_hour = (reg & 0x0F);
@@ -353,7 +353,7 @@ static uint8_t decode_alarm(const uint8_t *ap,
353353
tm.tm_mday = (*dp & 0x07);
354354
tm.tm_wday = tm.tm_mday - 1;
355355
} else {
356-
tm.tm_mday = 10 * ((*dp >> 4) & 0x3) + (*dp & 0x0F);
356+
tm.tm_mday = bcd2bin(*dp & 0x3F);
357357
}
358358

359359
/* Walk backwards to extract the alarm mask flags. */
@@ -396,22 +396,22 @@ static int encode_alarm(uint8_t *ap,
396396
if (flags & MAXIM_DS3231_ALARM_FLAGS_IGNSE) {
397397
val = REG_ALARM_IGN;
398398
} else {
399-
val = ((tm.tm_sec / 10) << 4) | (tm.tm_sec % 10);
399+
val = bin2bcd(tm.tm_sec);
400400
}
401401
*ap++ = val;
402402
}
403403

404404
if (flags & MAXIM_DS3231_ALARM_FLAGS_IGNMN) {
405405
val = REG_ALARM_IGN;
406406
} else {
407-
val = ((tm.tm_min / 10) << 4) | (tm.tm_min % 10);
407+
val = bin2bcd(tm.tm_min);
408408
}
409409
*ap++ = val;
410410

411411
if (flags & MAXIM_DS3231_ALARM_FLAGS_IGNHR) {
412412
val = REG_ALARM_IGN;
413413
} else {
414-
val = ((tm.tm_hour / 10) << 4) | (tm.tm_hour % 10);
414+
val = bin2bcd(tm.tm_hour);
415415
}
416416
*ap++ = val;
417417

@@ -420,7 +420,7 @@ static int encode_alarm(uint8_t *ap,
420420
} else if (flags & MAXIM_DS3231_ALARM_FLAGS_DOW) {
421421
val = REG_DAYDATE_DOW | (tm.tm_wday + 1);
422422
} else {
423-
val = ((tm.tm_mday / 10) << 4) | (tm.tm_mday % 10);
423+
val = bin2bcd(tm.tm_mday);
424424
}
425425
*ap++ = val;
426426

@@ -434,10 +434,10 @@ static uint32_t decode_rtc(struct ds3231_data *data)
434434

435435
decode_time(&tm, &rp->sec, true);
436436
tm.tm_wday = (rp->dow & 0x07) - 1;
437-
tm.tm_mday = 10 * ((rp->dom >> 4) & 0x03) + (rp->dom & 0x0F);
437+
tm.tm_mday = bcd2bin(rp->dom & 0x3F);
438438
tm.tm_mon = 10 * (((0xF0 & ~REG_MONCEN_CENTURY) & rp->moncen) >> 4)
439439
+ (rp->moncen & 0x0F) - 1;
440-
tm.tm_year = (10 * (rp->year >> 4)) + (rp->year & 0x0F);
440+
tm.tm_year = bcd2bin(rp->year);
441441
if (REG_MONCEN_CENTURY & rp->moncen) {
442442
tm.tm_year += 100;
443443
}
@@ -896,29 +896,29 @@ static void sync_finish_write(const struct device *dev)
896896
*bp++ = offsetof(struct register_map, sec);
897897

898898
(void)gmtime_r(&when, &tm);
899-
val = ((tm.tm_sec / 10) << 4) | (tm.tm_sec % 10);
899+
val = bin2bcd(tm.tm_sec);
900900
*bp++ = val;
901901

902-
val = ((tm.tm_min / 10) << 4) | (tm.tm_min % 10);
902+
val = bin2bcd(tm.tm_min);
903903
*bp++ = val;
904904

905-
val = ((tm.tm_hour / 10) << 4) | (tm.tm_hour % 10);
905+
val = bin2bcd(tm.tm_hour);
906906
*bp++ = val;
907907

908908
*bp++ = 1 + tm.tm_wday;
909909

910-
val = ((tm.tm_mday / 10) << 4) | (tm.tm_mday % 10);
910+
val = bin2bcd(tm.tm_mday);
911911
*bp++ = val;
912912

913913
tm.tm_mon += 1;
914-
val = ((tm.tm_mon / 10) << 4) | (tm.tm_mon % 10);
914+
val = bin2bcd(tm.tm_mon);
915915
if (tm.tm_year >= 100) {
916916
tm.tm_year -= 100;
917917
val |= REG_MONCEN_CENTURY;
918918
}
919919
*bp++ = val;
920920

921-
val = ((tm.tm_year / 10) << 4) | (tm.tm_year % 10);
921+
val = bin2bcd(tm.tm_year);
922922
*bp++ = val;
923923

924924
uint32_t syncclock = maxim_ds3231_read_syncclock(dev);

0 commit comments

Comments
 (0)