| 53 | extern "C" { |
| 54 | |
| 55 | error_t bm8563_get_datetime(Device* device, Bm8563DateTime* dt) { |
| 56 | auto* i2c_controller = device_get_parent(device); |
| 57 | auto address = GET_CONFIG(device)->address; |
| 58 | |
| 59 | // Burst-read 7 registers starting at 0x02: |
| 60 | // [0]=seconds [1]=minutes [2]=hours [3]=days [4]=weekdays [5]=months [6]=years |
| 61 | uint8_t buf[7] = {}; |
| 62 | error_t error = i2c_controller_read_register(i2c_controller, address, REG_SECONDS, buf, sizeof(buf), I2C_TIMEOUT_TICKS); |
| 63 | if (error != ERROR_NONE) return error; |
| 64 | |
| 65 | if (buf[0] & 0x80u) { |
| 66 | LOG_E(TAG, "Clock integrity compromised (VL flag set) — data unreliable"); |
| 67 | return ERROR_INVALID_STATE; |
| 68 | } |
| 69 | dt->second = bcd_to_dec(buf[0] & 0x7Fu); // mask VL flag |
| 70 | dt->minute = bcd_to_dec(buf[1] & 0x7Fu); |
| 71 | dt->hour = bcd_to_dec(buf[2] & 0x3Fu); |
| 72 | dt->day = bcd_to_dec(buf[3] & 0x3Fu); |
| 73 | // buf[4] = weekday — ignored |
| 74 | dt->month = bcd_to_dec(buf[5] & 0x1Fu); |
| 75 | bool century = (buf[5] & 0x80u) != 0; |
| 76 | dt->year = static_cast<uint16_t>(2000 + bcd_to_dec(buf[6]) + (century ? 100 : 0)); |
| 77 | |
| 78 | return ERROR_NONE; |
| 79 | } |
| 80 | |
| 81 | error_t bm8563_set_datetime(Device* device, const Bm8563DateTime* dt) { |
| 82 | if (dt->year < 2000 || dt->year > 2199 || |
nothing calls this directly
no test coverage detected