| 79 | } |
| 80 | |
| 81 | error_t bm8563_set_datetime(Device* device, const Bm8563DateTime* dt) { |
| 82 | if (dt->year < 2000 || dt->year > 2199 || |
| 83 | dt->month < 1 || dt->month > 12 || |
| 84 | dt->day < 1 || dt->day > 31 || |
| 85 | dt->hour > 23 || dt->minute > 59 || dt->second > 59) { |
| 86 | return ERROR_INVALID_ARGUMENT; |
| 87 | } |
| 88 | |
| 89 | auto* i2c_controller = device_get_parent(device); |
| 90 | auto address = GET_CONFIG(device)->address; |
| 91 | |
| 92 | bool century = (dt->year >= 2100); |
| 93 | uint8_t y = static_cast<uint8_t>(century ? dt->year - 2100 : dt->year - 2000); |
| 94 | |
| 95 | uint8_t buf[7] = {}; |
| 96 | buf[0] = dec_to_bcd(dt->second); |
| 97 | buf[1] = dec_to_bcd(dt->minute); |
| 98 | buf[2] = dec_to_bcd(dt->hour); |
| 99 | buf[3] = dec_to_bcd(dt->day); |
| 100 | buf[4] = 0; // weekday — leave as Sunday (unused) |
| 101 | buf[5] = static_cast<uint8_t>(dec_to_bcd(dt->month) | (century ? 0x80u : 0x00u)); |
| 102 | buf[6] = dec_to_bcd(y); |
| 103 | |
| 104 | return i2c_controller_write_register(i2c_controller, address, REG_SECONDS, buf, sizeof(buf), I2C_TIMEOUT_TICKS); |
| 105 | } |
| 106 | |
| 107 | Driver bm8563_driver = { |
| 108 | .name = "bm8563", |
nothing calls this directly
no test coverage detected