ltc2949 0.2.0

Client for the battery pack monitor chip LTC2949
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
//! # LTC2949 direct-register client.
//!
//! This module contains the high-level [`Client`] trait, the concrete [`LTC2949`] client,
//! register-oriented configuration types, result readers, and helper constants for
//! host-owned timing.
//!
//! ## Timing
//!
//! The driver never blocks. Operations with mandatory settling or acknowledge time are
//! split into non-blocking phases: the first call starts the bus/device action and returns
//! the number of microseconds the host must wait before issuing the next phase. This mirrors
//! the `CommandTime` pattern used by the LTC681X cell-monitor client while keeping delay,
//! timer, RTOS, or scheduler ownership in the application.
//!
//! Public timing constants are exposed so code can assert, schedule, or budget those waits
//! explicitly:
//!
//! * [`T_BOOT_US`] is returned by [`Client::start_wake_up`] before
//!   [`Client::confirm_wake_up`].
//! * [`T_READY_US`] is returned by [`Client::wake_isospi`] before the next transaction.
//! * [`T_MLCK_US`] is returned by [`Client::request_memory_lock`] before reading a coherent
//!   locked snapshot.
//!
//! ```
//! use embedded_hal::delay::DelayNs;
//! use ltc2949::client::{Client, LTC2949, T_BOOT_US, T_MLCK_US, T_READY_US};
//! use ltc2949::example::{ExampleDelay, ExampleSPIDevice};
//!
//! let spi = ExampleSPIDevice::default();
//! let mut delay = ExampleDelay::default();
//! let mut client = LTC2949::new(spi);
//!
//! let ready_us = client.wake_isospi().unwrap();
//! assert_eq!(T_READY_US, ready_us);
//! delay.delay_us(ready_us);
//!
//! let boot_us = client.start_wake_up().unwrap();
//! assert_eq!(T_BOOT_US, boot_us);
//! delay.delay_us(boot_us);
//! client.confirm_wake_up().unwrap();
//!
//! let lock_us = client.request_memory_lock().unwrap();
//! assert_eq!(T_MLCK_US, lock_us);
//! delay.delay_us(lock_us);
//! let _charge1 = client.read_charge1().unwrap();
//! client.unlock_memory().unwrap();
//!
//! assert_eq!(
//!     u64::from(T_READY_US) + u64::from(T_BOOT_US) + u64::from(T_MLCK_US),
//!     delay.elapsed_us()
//! );
//! ```
//!
//! ## Fast control register
//!
//! [`Client::write_factrl`] selects which current and AUX channels participate in fast
//! conversion. With `FACONV` set, conversions run continuously and place their results in the
//! corresponding FIFOs. Leave `FACONV` clear when an ADCV-style command should trigger a
//! single conversion instead.
//!
//! ```
//! # use ltc2949::client::{Client, FastControlRegister, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let fast_channels = FastControlRegister::default()
//!     .with_faconv(true)
//!     .with_fach1(true)
//!     .with_fach2(true);
//!
//! client.write_factrl(fast_channels).unwrap();
//! ```
//!
//! ## ADC configuration register
//!
//! [`Client::write_adcconf`] configures power-result interpretation, NTC linearisation, and
//! the NTC source used for shunt temperature compensation. The PAGE1 write becomes active
//! only after an `ADJUPD` pulse while the LTC2949 is in STANDBY (`CONT = 0`).
//!
//! ```
//! # use ltc2949::client::{AdcConfiguration, Client, LTC2949, OpsControlRegister};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let adc_configuration = AdcConfiguration::default()
//!     .with_p1asv(true) // P1 contains a voltage result instead of power.
//!     .with_ntc1(true); // SLOT1 contains the NTC1 temperature result.
//!
//! client.write_adcconf(adc_configuration).unwrap();
//! client
//!     .write_opctrl(OpsControlRegister::default().with_adjupd(true))
//!     .unwrap();
//! ```
//!
//! ## NTC coefficients
//!
//! [`Client::write_ntc_coefficients`] stores the reference resistor and Steinhart–Hart
//! coefficients for either NTC channel as Float24 values. The coefficients below are a
//! realistic 10 kΩ NTC configuration. Route the matching SLOT input, enable `NTC1` or `NTC2`
//! in [`AdcConfiguration`], and issue `ADJUPD` before using the temperature result.
//!
//! ```
//! # use ltc2949::client::{Channel, Client, LTC2949, NtcConfig};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let ntc = NtcConfig {
//!     r_ref: 10_000.0,
//!     a: 1.1382e-3,
//!     b: 2.3267e-4,
//!     c: 0.93243e-7,
//! };
//!
//! client.write_ntc_coefficients(Channel::One, &ntc).unwrap();
//! ```
//!
//! ## Sense-resistor temperature compensation
//!
//! [`Client::write_shunt_tc`] programs the linear and quadratic temperature coefficients and
//! the nominal reference temperature for one sense resistor. This copper example uses
//! 3900 ppm/K at 25 °C and no quadratic correction. The values become active after `ADJUPD`
//! and require a configured NTC temperature source.
//!
//! ```
//! # use ltc2949::client::{Channel, Client, LTC2949, ShuntTcConfig};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let copper_shunt = ShuntTcConfig {
//!     tc: 0.0039,
//!     t_ref: 25.0,
//!     tc2: 0.0,
//! };
//!
//! client.write_shunt_tc(Channel::One, &copper_shunt).unwrap();
//! ```
//!
//! ## SLOT multiplexer configuration
//!
//! [`Client::write_slot_mux`] routes a negative and positive input to SLOT1 or SLOT2. A
//! typical NTC divider connects its measurement node to `V1`; selecting `AGND` as `MUXN` and
//! `V1` as `MUXP` measures that node single-ended for NTC1 linearisation.
//!
//! ```
//! # use ltc2949::client::{Channel, Client, LTC2949, MuxInput};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//!
//! client
//!     .write_slot_mux(Channel::One, MuxInput::Agnd, MuxInput::V1)
//!     .unwrap();
//! ```
//!
//! ## GPIO control
//!
//! [`Client::write_gpio_ctrl`] writes `FGPIOCTRL`, which contains four type-safe 2-bit GPIO
//! control fields. In this example GPIO1 is driven high while the remaining GPIOs stay
//! tristated.
//!
//! ```
//! # use ltc2949::client::{Client, GpioControlRegister, GpioCtrl, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let gpio_control = GpioControlRegister::new().with_gpio1_ctrl(GpioCtrl::High);
//!
//! client.write_gpio_ctrl(gpio_control).unwrap();
//! ```
//!
//! [`Client::write_gpio5_config`] writes ´FCURGPIOCTRL´, which controls MUXP & MUXN Current sources and
//! GPIO 5. Changes only tajke effect after ´FGPIOCTRL´, has been written, so [`Client::write_gpio_ctrl`]
//! should be called afterwards.
//!
//! ```
//! # use ltc2949::client::{Client, CurGpioControlRegister, GpioCtrl, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let gpio5_config = CurGpioControlRegister::new().with_gpio5_ctrl(GpioCtrl::Toggle400kHz);
//!
//! client.write_gpio5_config(gpio5_config).unwrap();
//! ```
//!
//! ## Overcurrent configuration
//!
//! [`Client::write_occ_config`] configures both hardware overcurrent comparators. Thresholds
//! select a differential shunt voltage rather than amperes; divide by the shunt resistance
//! to obtain the current limit. With a 100 µΩ shunt, the examples below correspond to +260 A
//! and −520 A limits with 320 µs and 80 µs deglitch times.
//!
//! ```
//! # use ltc2949::client::{Client, LTC2949, OverCurrentConfig};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let positive_limit = OverCurrentConfig {
//!     enable: true,
//!     threshold: 0b001,    // 26 mV / 100 µΩ = 260 A
//!     deglitch_time: 0b10, // 320 µs
//!     polarity: 0b01,      // positive current only
//! };
//! let negative_limit = OverCurrentConfig {
//!     enable: true,
//!     threshold: 0b010,    // 52 mV / 100 µΩ = 520 A
//!     deglitch_time: 0b01, // 80 µs
//!     polarity: 0b10,      // negative current only
//! };
//!
//! client.write_occ_config(positive_limit, negative_limit).unwrap();
//! ```
//!
//! ## ADAX with an LTC681X chain
//!
//! The LTC2949 and the cell-monitor client need separate [`SpiDevice`] handles backed by
//! the same physical isoSPI bus. Sending [`Client::trigger_adax`] through the LTC2949 handle
//! broadcasts ADAX (`0x0460`), so it also starts an AUX conversion on every LTC6813X in the
//! chain.
//!
//! For the LTC2949 to react, `CONT` must be set and `FACONV` must remain clear. The command
//! starts a fast single-shot conversion of the channels selected in [`FastControlRegister`]
//! and clears the LTC2949 FIFOs before conversion.
//!
//! ```
//!# use ltc2949::client::{Client, FastControlRegister, LTC2949, OpsControlRegister};
//!# use ltc2949::example::ExampleSPIDevice;
//!# use ltc681x::ltc6813::LTC6813;
//!# use ltc681x::monitor::{LTC681X, NoPolling};
//!#
//! // On hardware, create both handles from one shared isoSPI bus.
//! let meter_spi = ExampleSPIDevice::default();
//! let chain_spi = ExampleSPIDevice::default();
//!
//! let mut meter = LTC2949::new(meter_spi);
//! let _chain: LTC681X<_, NoPolling, LTC6813, 3> = LTC681X::ltc6813(chain_spi);
//!
//! meter
//!     .write_opctrl(OpsControlRegister::default().with_cont(true))
//!     .unwrap();
//! meter
//!     .write_factrl(FastControlRegister::default().with_facha(true))
//!     .unwrap();
//!
//! // One broadcast starts the LTC2949 fast channels and all LTC6813 AUX conversions.
//! meter.trigger_adax().unwrap();
//! ```
//!
//! ## Status and fault monitoring
//!
//! [`Client::read_status`] returns the decoded `STATUS` register. Its accessors distinguish
//! supply and reset events (`uvloa`, `pora`, `uvlostby`, `uvlod`), a completed result update,
//! and ADC or time-base errors. The SPI transaction can still fail independently, so handle
//! the returned [`Result`] before inspecting individual flags.
//!
//! ```
//! # use ltc2949::client::{Client, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//!
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let status = client.read_status().unwrap();
//!
//! // The example device reports a nominal status with no conversion errors.
//! assert!(!status.adcerr());
//! assert!(!status.tberr());
//! assert!(status.update()); // Result registers contain a new measurement cycle.
//! ```
//!
//! [`Client::read_faults`] decodes the main `FAULTS` register into named hardware,
//! communication, fast-acquisition, self-test, and CRC indicators. Check these flags after
//! start-up and whenever `STATUS` reports an error; [`Client::read_extfaults`] provides the
//! additional memory and fast-channel diagnostics from `EXTFAULTS`.
//!
//! ```
//! # use ltc2949::client::{Client, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//!
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let faults = client.read_faults().unwrap();
//!
//! let configuration_is_valid = !faults.crccfg() && !faults.crcmem();
//! let hardware_self_test_passed = !faults.hwbist();
//! assert!(configuration_is_valid);
//! assert!(hardware_self_test_passed);
//! ```
//!
//! ## Threshold monitoring
//!
//! The alert readers decode the PAGE0 threshold and overflow status registers into named
//! flags. [`Client::read_vt_alerts`] covers battery voltage, temperature, and the two slow
//! AUX slots; [`Client::read_ip_alerts`] covers current and power; and
//! [`Client::read_c_alerts`] covers accumulated charge. [`Client::read_ceof_alerts`],
//! [`Client::read_tb_alerts`], and [`Client::read_vcc_alerts`] report accumulator overflow,
//! time-base, supply, and overcurrent-comparator alerts.
//!
//! Alert bits are sticky read/write flags. Follow the datasheet's memory-lock procedure
//! before clearing alert registers so that an alert arriving during the clear is not lost.
//!
//! ```
//! # use ltc2949::client::{Client, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//!
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let vt_alerts = client.read_vt_alerts().unwrap();
//!
//! // The example device reports a nominal status with no thresholds exceeded.
//! assert!(!vt_alerts.bath());
//! assert!(!vt_alerts.temph());
//! assert!(!vt_alerts.slot1h());
//! ```
//!
//! PAGE1 threshold setters accept physical SI values and round to the nearest register code
//! using the scales in datasheet Tables 26–28. Current, power, charge, and energy setters
//! also take the shunt resistance so callers can use amperes, watts, coulombs, and joules
//! instead of the IC's intermediate V/V² units. Values that do not fit return
//! [`Error::InvalidThreshold`].
//!
//! ```
//! # use ltc2949::client::{AccumulatorClock, ChargeAccumulator, Channel, Client, LTC2949, ThresholdPair};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let shunt_ohms = 100e-6;
//!
//! client
//!     .write_current_thresholds(Channel::One, ThresholdPair::new(-100.0, 100.0), shunt_ohms)
//!     .unwrap();
//! client.write_battery_thresholds(ThresholdPair::new(0.0, 10.0)).unwrap();
//! client
//!     .write_charge_thresholds(
//!         ChargeAccumulator::Charge1,
//!         ThresholdPair::new(-100.0, 100.0),
//!         shunt_ohms as f64,
//!         AccumulatorClock::Internal,
//!     )
//!     .unwrap();
//! ```
//!
//! ## Current measurements
//!
//! [`Client::read_current1`] and [`Client::read_current2`] return the slow-mode voltage
//! measured across the corresponding sense resistor. Divide the decoded voltage by the
//! shunt resistance to obtain amperes. [`Client::read_current1_avg`] and
//! [`Client::read_current2_avg`] expose the moving average of the four preceding measurements
//! with a four-times finer LSB.
//!
//! ```
//! # use ltc2949::client::{Client, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let shunt_ohms = 100e-6_f32;
//!
//! let current1 = client.read_current1().unwrap();
//! let current2 = client.read_current2().unwrap();
//! let current1_avg = client.read_current1_avg().unwrap();
//! let current2_avg = client.read_current2_avg().unwrap();
//!
//! let current1_amps = current1.decode() / shunt_ohms;
//! let current2_amps = current2.decode() / shunt_ohms;
//! let current1_avg_amps = current1_avg.decode() / shunt_ohms;
//! let current2_avg_amps = current2_avg.decode() / shunt_ohms;
//! assert!((current1_amps - 9.5).abs() < 0.001);
//! assert!((current2_amps + 4.75).abs() < 0.001);
//! assert!((current1_avg_amps - 9.5).abs() < 0.001);
//! assert!((current2_avg_amps + 4.75).abs() < 0.001);
//! ```
//!
//! ## Power and battery voltage
//!
//! [`Client::read_power1`] and [`Client::read_power2`] return power-mode results by default.
//! Decode them with the corresponding shunt resistance. If `P1ASV` or `P2ASV` is enabled in
//! [`AdcConfiguration`], the same registers contain voltage instead and must be decoded with
//! [`PowerOrVoltage::decode_voltage`]. [`Client::read_bat`] always returns the pack voltage.
//!
//! ```
//! # use ltc2949::client::{Client, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let shunt_ohms = 100e-6_f32;
//!
//! let power1_watts = client.read_power1().unwrap().decode_power(shunt_ohms);
//! let power2_watts = client.read_power2().unwrap().decode_power(shunt_ohms);
//! let battery = client.read_bat().unwrap();
//!
//! assert!((power1_watts - 0.058_368).abs() < 0.000_001);
//! assert!((power2_watts + 0.029_184).abs() < 0.000_001);
//! assert_eq!(12_345, battery.raw());
//! assert_eq!(4_629_375, (battery.decode() * 1_000_000.0) as i32);
//! ```
//!
//! ## Temperature and supply voltage
//!
//! [`Client::read_temp`] provides the internal die temperature in kelvin or degrees Celsius.
//! [`Client::read_vcc`] reports the shared analog/digital supply voltage. Both result types
//! retain the raw register value and provide unit-aware decoding methods.
//!
//! ```
//! # use ltc2949::client::{Client, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//!
//! let die_temperature = client.read_temp().unwrap();
//! let supply_voltage = client.read_vcc().unwrap();
//!
//! let temperature_celsius = die_temperature.decode_celsius();
//! let vcc_volts = supply_voltage.decode();
//! assert!((temperature_celsius - 25.05).abs() < 0.01);
//! assert!((vcc_volts - 3.2996).abs() < 0.0001);
//! ```
//!
//! ## Charge accumulators
//!
//! [`Client::read_charge1`] and [`Client::read_charge2`] read the signed 48-bit channel
//! accumulators. [`Client::read_charge3`] reads the signed 64-bit weighted channel sum. For
//! the internal clock or a 4 MHz crystal, [`AccumulatedCharge::decode_coulombs`] converts the
//! raw value using the external shunt resistance; a freely configured external clock needs
//! the scale from datasheet Table 27.
//!
//! ```
//! # use ltc2949::client::{Client, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//! let shunt1_ohms = 100e-6_f64;
//! let shunt2_ohms = 100e-6_f64;
//!
//! let charge1 = client.read_charge1().unwrap();
//! let charge2 = client.read_charge2().unwrap();
//! let charge3 = client.read_charge3().unwrap();
//!
//! let channel1_coulombs = charge1.decode_coulombs(shunt1_ohms);
//! let channel2_coulombs = charge2.decode_coulombs(shunt2_ohms);
//! // The weighted C3 sum uses the channel-1 shunt for conversion.
//! let weighted_coulombs = charge3.decode_coulombs(shunt1_ohms);
//! assert!((channel1_coulombs - 37.7887).abs() < 0.0001);
//! assert!((channel2_coulombs + 22.67322).abs() < 0.0001);
//! assert!((weighted_coulombs - 15.11548).abs() < 0.0001);
//! ```
//!
//! ## Time-base accumulators
//!
//! [`Client::read_time1`] through [`Client::read_time4`] return unsigned 32-bit time-base
//! counters. [`AccumulatedTime::decode`] converts them to seconds for the internal clock or a
//! 4 MHz crystal; use the datasheet formula when the external clock is configured freely.
//!
//! ```
//! # use ltc2949::client::{Client, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//!
//! let time1 = client.read_time1().unwrap();
//! let time2 = client.read_time2().unwrap();
//! let time3 = client.read_time3().unwrap();
//! let time4 = client.read_time4().unwrap();
//!
//! assert!((time1.decode() - 3.97777).abs() < 0.00001);
//! assert!((time2.decode() - 4.773324).abs() < 0.00001);
//! assert!((time3.decode() - 5.966655).abs() < 0.00001);
//! assert!((time4.decode() - 7.95554).abs() < 0.00001);
//! ```
//!
//! ## Coherent accumulator snapshots
//!
//! [`Client::read_accumulators1`] and [`Client::read_accumulators2`] each read charge,
//! energy, and time in one 16-byte burst. Because all three values come from one register
//! row, they form a coherent per-channel snapshot without a separate memory-lock handshake.
//!
//! ```
//! # use ltc2949::client::{Client, LTC2949};
//! # use ltc2949::example::ExampleSPIDevice;
//! let mut client = LTC2949::new(ExampleSPIDevice::default());
//!
//! let channel1 = client.read_accumulators1().unwrap();
//! let channel2 = client.read_accumulators2().unwrap();
//!
//! assert_eq!((10_000_000, 10_000, 10_000), (
//!     channel1.charge.raw(),
//!     channel1.energy.raw(),
//!     channel1.time.raw(),
//! ));
//! assert_eq!((-6_000_000, -6_000, 12_000), (
//!     channel2.charge.raw(),
//!     channel2.energy.raw(),
//!     channel2.time.raw(),
//! ));
//! ```
//!
// `modular-bitfield`'s macro expansion emits `pub field: (bool)` etc., which trips the
// `unused_parens` lint on newer rustc. Silence the lint module-wide.
#![allow(unused_parens)]

use crate::float24::Float24;
use crate::pec15::PEC15;
use crate::polling::{NoPolling, PollMethod};
use embedded_hal::spi::{Operation, SpiDevice};
use heapless::Vec;
use modular_bitfield::prelude::*;

/// Worst-case core boot time from SLEEP/power-up to STANDBY (datasheet tBOOT).
/// Returned by [`Client::start_wake_up`].
pub const T_BOOT_US: u32 = 100_000;

/// isoSPI port start-up time after a wake edge (datasheet tREADY, 10 µs; doubled for margin).
/// Returned by [`Client::wake_isospi`].
pub const T_READY_US: u32 = 20;

/// Worst-case memory-lock acknowledge time (datasheet tMLCK, MEASURE mode; 40 ms in
/// STANDBY). Returned by [`Client::request_memory_lock`].
pub const T_MLCK_US: u32 = 130_000;

/// Encoded ID byte carried by direct-command read and write frames (datasheet Table 12).
pub(crate) struct DcmdId {
    /// `true` for a read command (`RW = 1`, `!RW = 0`), `false` for a write command.
    read: bool,
    /// Four-bit PEC-count field; encodes the number of data bytes per PEC as `N - 1`.
    pecc: u8,
}

impl DcmdId {
    pub(crate) fn read(pecc: u8) -> Self {
        Self::new(true, pecc)
    }

    pub(crate) fn write(pecc: u8) -> Self {
        Self::new(false, pecc)
    }

    pub(crate) fn new(read: bool, pecc: u8) -> Self {
        Self {
            read,
            pecc: pecc & 0x0F,
        }
    }
}

impl From<DcmdId> for u8 {
    fn from(id: DcmdId) -> Self {
        let p3 = (id.pecc >> 3) & 1;
        let p2 = (id.pecc >> 2) & 1;
        let p1 = (id.pecc >> 1) & 1;
        let p0 = id.pecc & 1;
        let rw = u8::from(id.read);
        let not_rw = rw ^ 1;
        (rw << 7) | (not_rw << 6) | ((p3 ^ p2) << 5) | (p3 << 4) | (p2 << 3) | ((p1 ^ p0) << 2) | (p1 << 1) | p0
    }
}

/// Max data bytes covered by one PEC group (`PECC + 1`).
const N_PER_PEC: usize = 16;

/// Whole 3-byte FIFO samples that fit one PEC group (`16 / 3 = 5`). Reading several samples
/// per DCMD amortises the ~9-byte header/PEC overhead instead of paying it per sample.
const FIFO_SAMPLES_PER_BURST: usize = N_PER_PEC / 3;

/// Memory page selector. PAGE0 holds measurement results, status and control;
/// PAGE1 holds thresholds and configuration. Selected via the [`RegControlRegister`] register.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Page {
    Page0 = 0,
    Page1 = 1,
}

/// Page-0 register addresses: results, accumulators, status and control/fast-mode
/// registers (datasheet Tables 24, 26-28, 57-64). Discriminant = on-bus `RADDR` byte.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub enum RegAddressP0 {
    Charge1 = 0x00, // 48-bit
    Energy1 = 0x06, // 48-bit
    Time1 = 0x0C,   // 32-bit
    Charge2 = 0x10,
    Energy2 = 0x16,
    Time2 = 0x1C,
    Charge3 = 0x24, // 64-bit
    Time3 = 0x2C,
    Energy4 = 0x34, // 64-bit
    Time4 = 0x3C,
    WkupAck = 0x70,
    Status = 0x80,
    StatVT = 0x81,
    StatIP = 0x82,
    StatC = 0x83,
    StatE = 0x84,
    StatCEOF = 0x85,
    StatTB = 0x86,
    StatVCC = 0x87,
    Current1 = 0x90, // 24-bit signed
    Power1 = 0x93,
    Current2 = 0x96,
    Power2 = 0x99,
    Current1Avg = 0x9C,
    Bat = 0xA0, // 16-bit
    Temp = 0xA2,
    Vcc = 0xA4,
    Slot1 = 0xA6,
    Slot2 = 0xA8,
    Vref = 0xAA,
    Current2Avg = 0xAC,
    Gpio4HbCtrl = 0xE8,
    // Slow-mode auxiliary-MUX slot selection (datasheet Tables 57 & 58). SLOT1/2 each
    // have separate MUXN / MUXP registers, adjacent so a 2-byte burst sets both.
    Slot1MuxN = 0xEB,
    Slot1MuxP = 0xEC,
    Slot2MuxN = 0xED,
    Slot2MuxP = 0xEE,
    ExtFaults = 0xDC,
    Occ1Ctrl = 0xDE,
    Occ2Ctrl = 0xDF,
    Faults = 0xDD,
    OpCtrl = 0xF0,
    FCurGpioCtrl = 0xF1,
    FGpioCtrl = 0xF2,
    FaMuxN = 0xF3,
    FaMuxP = 0xF4,
    FaCtrl = 0xF5,
    FifoI1 = 0xF7,
    FifoI2 = 0xF8,
    FifoBat = 0xF9,
    FifoAux = 0xFA,
    RdcvIAddr = 0xFC,
    RegsCtrl = 0xFF,
}

/// Page-1 register addresses: thresholds, gain correction, ADC config, NTC-linearisation,
/// and sense-resistor TC coefficient blocks (datasheet Tables 67, 69, 71, 72, 76).
/// Discriminant = on-bus `RADDR` byte.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub enum RegAddressP1 {
    // Threshold Registers
    C1Th = 0x00,
    C1Tl = 0x06,
    Tb1Th = 0x0C,
    E1Th = 0x10,
    E1Tl = 0x16,
    C2Th = 0x20,
    C2Tl = 0x26,
    Tb2Th = 0x2C,
    E2Th = 0x30,
    E2Tl = 0x36,
    C3Th = 0x44,
    Tb3Th = 0x4C,
    C3Tl = 0x54,
    E4Th = 0x64,
    Tb4Th = 0x6C,
    E4Tl = 0x74,
    I1Th = 0x80,
    I1Tl = 0x82,
    P1Th = 0x84,
    P1Tl = 0x86,
    I2Th = 0x88,
    I2Tl = 0x8A,
    P2Th = 0x8C,
    P2Tl = 0x8E,
    BatTh = 0x90,
    BatTl = 0x92,
    TempTh = 0x94,
    TempTl = 0x96,
    VccTh = 0x98,
    VccTl = 0x9A,
    Slot1Th = 0xA0,
    Slot1Tl = 0xA2,
    Slot2Th = 0xA4,
    Slot2Tl = 0xA6,
    // 2nd-order sense-resistor TC (3 bytes Float24 each), tucked low on the page.
    Rs1Tc2 = 0x5C,
    Rs2Tc2 = 0x7C,
    // NTC linearisation — datasheet Table 71. All values are Float24 (3 bytes each,
    // MSB at the lowest address). NTC1A..C and NTC2A..C live contiguously, so a single
    // 9-byte burst writes all three coefficients per channel.
    Rref1 = 0xAA,
    Rref2 = 0xAD,
    Ntc1A = 0xD0,
    Ntc1B = 0xD3,
    Ntc1C = 0xD6,
    // Sense-resistor temperature compensation — RSnTC (3 bytes) and RSnT0 (2 bytes,
    // mantissa LSB implicitly 0) are adjacent so they go out in one burst.
    Rs1Tc = 0xD9,
    Rs1T0 = 0xDC,
    AdcConf = 0xDF,
    Ntc2A = 0xE0,
    Ntc2B = 0xE3,
    Ntc2C = 0xE6,
    Rs2Tc = 0xE9,
    Rs2T0 = 0xEC,
    // Gain configuration — Float24 factors and their MUX input assignments (Table 72).
    Rs1Gc = 0xB0,
    Rs2Gc = 0xB3,
    RsRatio = 0xB6,
    BatGc = 0xB9,
    Mux1Gc = 0xC0,
    Mux2Gc = 0xC3,
    Mux3Gc = 0xC6,
    Mux4Gc = 0xC9,
    MuxNset1 = 0xBC,
    MuxPset1 = 0xBD,
    MuxNset2 = 0xBE,
    MuxPset2 = 0xBF,
    MuxNset3 = 0xCC,
    MuxPset3 = 0xCD,
    MuxNset4 = 0xCE,
    MuxPset4 = 0xCF,
}

/// Raw I1/I2 slow-mode current-sense voltage result.
///
/// The LTC2949 stores this as a signed ADC code. [`decode`](Self::decode) converts it to
/// volts at the current sense inputs.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct CurrentSenseVoltage {
    /// Raw signed 24-bit ADC code, sign-extended to `i32`.
    raw: i32,
}

impl CurrentSenseVoltage {
    /// Voltage represented by one raw ADC code.
    pub const LSB_VOLTS: f32 = 950e-9;
    /// Voltage represented by one I1TH/I1TL or I2TH/I2TL threshold code.
    ///
    /// The threshold registers contain the upper 16 bits of the effective 18-bit current
    /// result, so their LSB is four times the result-register LSB (3.8 µV).
    pub const THRESHOLD_LSB_VOLTS: f32 = 4.0 * Self::LSB_VOLTS;

    /// Wraps a raw signed 24-bit ADC code, sign-extended to `i32`.
    pub const fn from_raw(raw: i32) -> Self {
        Self { raw }
    }

    /// Returns the raw signed 24-bit ADC code, sign-extended to `i32`.
    pub fn raw(self) -> i32 {
        self.raw
    }

    /// Decodes the raw ADC code into current-sense voltage in volts.
    pub fn decode(self) -> f32 {
        self.raw as f32 * Self::LSB_VOLTS
    }
}

/// Raw I1AVG/I2AVG moving-average current-sense voltage result.
///
/// The averaged result has a 4x finer LSB than the unaveraged slow-mode current result.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct AveragedCurrentSenseVoltage {
    /// Raw signed 24-bit ADC code, sign-extended to `i32`.
    raw: i32,
}

impl AveragedCurrentSenseVoltage {
    /// Voltage represented by one raw ADC code.
    pub const LSB_VOLTS: f32 = 237.5e-9;

    /// Wraps a raw signed 24-bit ADC code, sign-extended to `i32`.
    pub const fn from_raw(raw: i32) -> Self {
        Self { raw }
    }

    /// Returns the raw signed 24-bit ADC code, sign-extended to `i32`.
    pub fn raw(self) -> i32 {
        self.raw
    }

    /// Decodes the raw ADC code into current-sense voltage in volts.
    pub fn decode(self) -> f32 {
        self.raw as f32 * Self::LSB_VOLTS
    }
}

/// Raw P1/P2 result.
///
/// P1/P2 contain power results by default, or voltage results when `P1ASV`/`P2ASV` is set
/// in [`AdcConfiguration`].
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct PowerOrVoltage {
    /// Raw signed 24-bit ADC code, sign-extended to `i32`.
    raw: i32,
}

impl PowerOrVoltage {
    /// Power-mode scale before applying the external shunt resistance.
    pub const POWER_LSB_VOLT_SQUARED: f32 = 5.8368e-12;
    /// Voltage-mode scale represented by one raw ADC code.
    pub const VOLTAGE_LSB_VOLTS: f32 = 46.875e-6;
    /// Power-mode scale represented by one P1TH/P1TL or P2TH/P2TL threshold code.
    pub const POWER_THRESHOLD_LSB_VOLT_SQUARED: f32 = 4.0 * Self::POWER_LSB_VOLT_SQUARED;
    /// Voltage-mode scale represented by one P1TH/P1TL or P2TH/P2TL threshold code.
    pub const VOLTAGE_THRESHOLD_LSB_VOLTS: f32 = 4.0 * Self::VOLTAGE_LSB_VOLTS;

    /// Wraps a raw signed 24-bit ADC code, sign-extended to `i32`.
    pub const fn from_raw(raw: i32) -> Self {
        Self { raw }
    }

    /// Returns the raw signed 24-bit ADC code, sign-extended to `i32`.
    pub fn raw(self) -> i32 {
        self.raw
    }

    /// Decodes a power-mode P1/P2 result into watts.
    pub fn decode_power(self, shunt_ohms: f32) -> f32 {
        self.raw as f32 * Self::POWER_LSB_VOLT_SQUARED / shunt_ohms
    }

    /// Decodes a voltage-mode P1/P2 result into volts.
    pub fn decode_voltage(self) -> f32 {
        self.raw as f32 * Self::VOLTAGE_LSB_VOLTS
    }
}

/// Raw BAT battery-voltage result.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct BatteryVoltage {
    /// Raw signed 16-bit ADC code.
    raw: i16,
}

impl BatteryVoltage {
    /// Voltage represented by one raw ADC code.
    pub const LSB_VOLTS: f32 = 375e-6;

    /// Wraps a raw signed 16-bit ADC code.
    pub const fn from_raw(raw: i16) -> Self {
        Self { raw }
    }

    /// Returns the raw signed 16-bit ADC code.
    pub fn raw(self) -> i16 {
        self.raw
    }

    /// Decodes the raw ADC code into battery voltage in volts.
    pub fn decode(self) -> f32 {
        self.raw as f32 * Self::LSB_VOLTS
    }
}

/// Raw internal die-temperature result.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct DieTemperature {
    /// Raw signed 16-bit ADC code.
    raw: i16,
}

impl DieTemperature {
    /// Temperature represented by one raw ADC code.
    pub const LSB_KELVIN: f32 = 0.2;
    /// Offset between the kelvin and Celsius temperature scales.
    pub const ZERO_CELSIUS_KELVIN: f32 = 273.15;

    /// Wraps a raw signed 16-bit ADC code.
    pub const fn from_raw(raw: i16) -> Self {
        Self { raw }
    }

    /// Returns the raw signed 16-bit ADC code.
    pub fn raw(self) -> i16 {
        self.raw
    }

    /// Decodes the raw ADC code into absolute temperature in kelvin.
    pub fn decode_kelvin(self) -> f32 {
        self.raw as f32 * Self::LSB_KELVIN
    }

    /// Decodes the raw ADC code into temperature in degrees Celsius.
    pub fn decode_celsius(self) -> f32 {
        self.decode_kelvin() - Self::ZERO_CELSIUS_KELVIN
    }
}

/// Raw A/DVCC supply-voltage result.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct SupplyVoltage {
    /// Raw signed 16-bit ADC code.
    raw: i16,
}

impl SupplyVoltage {
    /// Voltage represented by one raw ADC code.
    pub const LSB_VOLTS: f32 = 2.26e-3;

    /// Wraps a raw signed 16-bit ADC code.
    pub const fn from_raw(raw: i16) -> Self {
        Self { raw }
    }

    /// Returns the raw signed 16-bit ADC code.
    pub fn raw(self) -> i16 {
        self.raw
    }

    /// Decodes the raw ADC code into supply voltage in volts.
    pub fn decode(self) -> f32 {
        self.raw as f32 * Self::LSB_VOLTS
    }
}

/// Raw SLOT1/SLOT2 result.
///
/// The value represents either an auxiliary-input voltage or an NTC temperature,
/// depending on the corresponding `NTC1`/`NTC2` bit in [`AdcConfiguration`].
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct SlotValue {
    /// Raw signed 16-bit ADC code.
    raw: i16,
}

impl SlotValue {
    /// Voltage represented by one raw ADC code when NTC conversion is disabled.
    pub const LSB_VOLTS: f32 = 375e-6;
    /// Temperature represented by one raw ADC code when NTC conversion is enabled.
    pub const LSB_DEGREES_CELSIUS: f32 = 0.2;

    /// Wraps a raw signed 16-bit SLOT ADC code.
    pub const fn from_raw(raw: i16) -> Self {
        Self { raw }
    }

    /// Returns the raw signed 16-bit ADC code.
    pub fn raw(self) -> i16 {
        self.raw
    }

    /// Decodes a voltage-mode SLOT result into volts.
    pub fn decode_voltage(self) -> f32 {
        self.raw as f32 * Self::LSB_VOLTS
    }

    /// Decodes an NTC-mode SLOT result into degrees Celsius.
    pub fn decode_temperature(self) -> f32 {
        self.raw as f32 * Self::LSB_DEGREES_CELSIUS
    }
}

/// Raw accumulated charge result from C1, C2 or C3.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct AccumulatedCharge {
    /// Raw signed accumulator code, sign-extended to `i64` for 48-bit registers.
    raw: i64,
}

impl AccumulatedCharge {
    /// Volt-seconds represented by one raw code with the internal clock or a 4 MHz crystal.
    pub const LSB_VOLT_SECONDS: f64 = 377.887e-12;

    /// Wraps a raw signed 48-bit or 64-bit accumulator code.
    pub const fn from_raw(raw: i64) -> Self {
        Self { raw }
    }

    /// Returns the raw signed accumulator code.
    pub fn raw(self) -> i64 {
        self.raw
    }

    /// Decodes the result into volt-seconds for the internal clock or a 4 MHz crystal.
    ///
    /// With a freely configured external clock, use the LSB formula from datasheet Table 27
    /// instead because the scale depends on `fEXT`, `PRE` and `DIV`.
    pub fn decode(self) -> f64 {
        self.raw as f64 * Self::LSB_VOLT_SECONDS
    }

    /// Decodes the result into coulombs using the external shunt resistance.
    ///
    /// Pass the channel's shunt for C1/C2 and the channel-1 shunt for the weighted C3 sum.
    /// This scale applies to the internal clock or a 4 MHz crystal.
    pub fn decode_coulombs(self, shunt_ohms: f64) -> f64 {
        self.decode() / shunt_ohms
    }
}

/// Raw accumulated energy result from E1, E2 or E4.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct AccumulatedEnergy {
    /// Raw signed accumulator code, sign-extended to `i64` for 48-bit registers.
    raw: i64,
}

impl AccumulatedEnergy {
    /// Volt-squared-seconds represented by one raw code with the internal clock or a 4 MHz crystal.
    pub const LSB_VOLT_SQUARED_SECONDS: f64 = 2.32175e-9;

    /// Wraps a raw signed 48-bit or 64-bit accumulator code.
    pub const fn from_raw(raw: i64) -> Self {
        Self { raw }
    }

    /// Returns the raw signed accumulator code.
    pub fn raw(self) -> i64 {
        self.raw
    }

    /// Decodes the result into volt-squared-seconds for the internal clock or a 4 MHz crystal.
    ///
    /// With a freely configured external clock, use the LSB formula from datasheet Table 27
    /// instead because the scale depends on `fEXT`, `PRE` and `DIV`.
    pub fn decode(self) -> f64 {
        self.raw as f64 * Self::LSB_VOLT_SQUARED_SECONDS
    }

    /// Decodes the result into joules using the external shunt resistance.
    ///
    /// Pass the channel's shunt for E1/E2 and the channel-1 shunt for the weighted E4 sum.
    /// This scale applies to the internal clock or a 4 MHz crystal.
    pub fn decode_joules(self, shunt_ohms: f64) -> f64 {
        self.decode() / shunt_ohms
    }
}

/// Raw accumulated time-base result from TB1 through TB4.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct AccumulatedTime {
    /// Raw unsigned 32-bit accumulator code.
    raw: u32,
}

/// Charge accumulator selected for a PAGE1 threshold write.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum ChargeAccumulator {
    Charge1,
    Charge2,
    /// Weighted sum of channels 1 and 2; its SI conversion uses the channel-1 shunt.
    Charge3,
}

/// Energy accumulator selected for a PAGE1 threshold write.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum EnergyAccumulator {
    Energy1,
    Energy2,
    /// Weighted sum of channels 1 and 2; its SI conversion uses the channel-1 shunt.
    Energy4,
}

/// Time-base accumulator selected for a PAGE1 threshold write.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum TimeBase {
    Time1,
    Time2,
    Time3,
    Time4,
}

/// A single threshold expressed in the caller's physical unit.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct Threshold<T> {
    /// Threshold in the physical unit expected by the caller.
    pub value: T,
}

impl<T> Threshold<T> {
    /// Creates a single physical threshold.
    pub const fn new(value: T) -> Self {
        Self { value }
    }
}

/// Low and high thresholds expressed in the caller's physical unit.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct ThresholdPair<T> {
    /// Low threshold in the physical unit expected by the caller.
    pub low: T,
    /// High threshold in the physical unit expected by the caller.
    pub high: T,
}

impl<T> ThresholdPair<T> {
    /// Creates an ordered low/high physical threshold pair.
    ///
    /// Ordering is validated when the pair is quantized or passed to a client method.
    pub const fn new(low: T, high: T) -> Self {
        Self { low, high }
    }
}

impl ThresholdPair<f64> {
    fn quantize_signed_code(value: f64, lsb: f64, minimum: f64, maximum: f64) -> Result<i64, ThresholdConversionError> {
        if !value.is_finite() {
            return Err(ThresholdConversionError::NonFiniteValue);
        }

        if !lsb.is_finite() || lsb <= 0.0 {
            return Err(ThresholdConversionError::InvalidScale);
        }

        let scaled = value / lsb;
        if scaled <= minimum - 0.5 || scaled >= maximum + 0.5 {
            return Err(ThresholdConversionError::OutOfRange);
        }

        Ok(if scaled >= 0.0 {
            (scaled + 0.5) as i64
        } else {
            (scaled - 0.5) as i64
        })
    }
}

/// Reason a physical threshold could not be converted to its register representation.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum ThresholdConversionError {
    /// A threshold value was NaN or infinite.
    NonFiniteValue,
    /// The LSB was zero, negative, NaN, or infinite.
    InvalidScale,
    /// The low threshold was greater than the high threshold.
    LowAboveHigh,
    /// The rounded value does not fit in the target register format.
    OutOfRange,
}

/// Converts a threshold in physical units into a scaled LTC2949 register value.
///
/// `lsb` is the amount of the physical unit represented by one register code. Conversion
/// rounds to the nearest code, with half-way values rounded away from zero.
pub trait Quantize<Output> {
    /// Quantizes this value using the supplied physical-unit LSB.
    fn quantize(self, lsb: f64) -> Result<Output, ThresholdConversionError>;
}

/// Encoded signed 16-bit high/low threshold pair used by the non-accumulated measurements.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct Signed16Thresholds {
    high: i16,
    low: i16,
}

impl Signed16Thresholds {
    /// Returns the quantized high threshold.
    pub const fn high(self) -> i16 {
        self.high
    }

    /// Returns the quantized low threshold.
    pub const fn low(self) -> i16 {
        self.low
    }

    /// Encodes the high value followed by the low value, most-significant byte first.
    pub fn to_be_bytes(self) -> [u8; 4] {
        let mut bytes = [0u8; 4];
        bytes[..2].copy_from_slice(&self.high.to_be_bytes());
        bytes[2..].copy_from_slice(&self.low.to_be_bytes());
        bytes
    }
}

/// Encoded signed 48-bit high/low accumulator threshold pair.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct Signed48Thresholds {
    high: i64,
    low: i64,
}

impl Signed48Thresholds {
    /// Returns the quantized high threshold as a sign-extended `i64`.
    pub const fn high(self) -> i64 {
        self.high
    }

    /// Returns the quantized low threshold as a sign-extended `i64`.
    pub const fn low(self) -> i64 {
        self.low
    }

    /// Encodes the high value followed by the low value as two big-endian 48-bit values.
    pub fn to_be_bytes(self) -> [u8; 12] {
        let high = self.high.to_be_bytes();
        let low = self.low.to_be_bytes();
        let mut bytes = [0u8; 12];
        bytes[..6].copy_from_slice(&high[2..]);
        bytes[6..].copy_from_slice(&low[2..]);
        bytes
    }
}

/// Encoded signed 64-bit high/low accumulator threshold pair.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct Signed64Thresholds {
    high: i64,
    low: i64,
}

impl Signed64Thresholds {
    /// Returns the quantized high threshold.
    pub const fn high(self) -> i64 {
        self.high
    }

    /// Returns the quantized low threshold.
    pub const fn low(self) -> i64 {
        self.low
    }

    /// Encodes the high threshold, most-significant byte first.
    pub const fn high_to_be_bytes(self) -> [u8; 8] {
        self.high.to_be_bytes()
    }

    /// Encodes the low threshold, most-significant byte first.
    pub const fn low_to_be_bytes(self) -> [u8; 8] {
        self.low.to_be_bytes()
    }
}

/// Encoded unsigned 32-bit time-base threshold.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct Unsigned32Threshold {
    raw: u32,
}

impl Unsigned32Threshold {
    /// Returns the quantized register code.
    pub const fn raw(self) -> u32 {
        self.raw
    }

    /// Encodes the threshold, most-significant byte first.
    pub const fn to_be_bytes(self) -> [u8; 4] {
        self.raw.to_be_bytes()
    }
}

impl Quantize<Signed16Thresholds> for ThresholdPair<f64> {
    fn quantize(self, lsb: f64) -> Result<Signed16Thresholds, ThresholdConversionError> {
        if self.low > self.high {
            return Err(ThresholdConversionError::LowAboveHigh);
        }
        Ok(Signed16Thresholds {
            high: Self::quantize_signed_code(self.high, lsb, f64::from(i16::MIN), f64::from(i16::MAX))? as i16,
            low: Self::quantize_signed_code(self.low, lsb, f64::from(i16::MIN), f64::from(i16::MAX))? as i16,
        })
    }
}

impl Quantize<Signed16Thresholds> for ThresholdPair<f32> {
    fn quantize(self, lsb: f64) -> Result<Signed16Thresholds, ThresholdConversionError> {
        ThresholdPair::new(f64::from(self.low), f64::from(self.high)).quantize(lsb)
    }
}

impl Quantize<Signed48Thresholds> for ThresholdPair<f64> {
    fn quantize(self, lsb: f64) -> Result<Signed48Thresholds, ThresholdConversionError> {
        const MIN: f64 = -((1u64 << 47) as f64);
        const MAX: f64 = ((1u64 << 47) - 1) as f64;
        if self.low > self.high {
            return Err(ThresholdConversionError::LowAboveHigh);
        }
        Ok(Signed48Thresholds {
            high: Self::quantize_signed_code(self.high, lsb, MIN, MAX)?,
            low: Self::quantize_signed_code(self.low, lsb, MIN, MAX)?,
        })
    }
}

impl Quantize<Signed64Thresholds> for ThresholdPair<f64> {
    fn quantize(self, lsb: f64) -> Result<Signed64Thresholds, ThresholdConversionError> {
        if self.low > self.high {
            return Err(ThresholdConversionError::LowAboveHigh);
        }
        if !lsb.is_finite() || lsb <= 0.0 {
            return Err(ThresholdConversionError::InvalidScale);
        }
        if !self.low.is_finite() || !self.high.is_finite() {
            return Err(ThresholdConversionError::NonFiniteValue);
        }

        fn quantize(value: f64, lsb: f64) -> Result<i64, ThresholdConversionError> {
            // `i64::MAX as f64` rounds up to 2^63, so use an exclusive upper bound.
            let scaled = value / lsb;
            if !(-9_223_372_036_854_775_808.0..9_223_372_036_854_775_808.0).contains(&scaled) {
                return Err(ThresholdConversionError::OutOfRange);
            }
            Ok(if scaled >= 0.0 {
                (scaled + 0.5) as i64
            } else {
                (scaled - 0.5) as i64
            })
        }

        Ok(Signed64Thresholds {
            high: quantize(self.high, lsb)?,
            low: quantize(self.low, lsb)?,
        })
    }
}

impl Quantize<Unsigned32Threshold> for Threshold<f64> {
    fn quantize(self, lsb: f64) -> Result<Unsigned32Threshold, ThresholdConversionError> {
        if !self.value.is_finite() {
            return Err(ThresholdConversionError::NonFiniteValue);
        }
        if !lsb.is_finite() || lsb <= 0.0 {
            return Err(ThresholdConversionError::InvalidScale);
        }
        let scaled = self.value / lsb;
        if self.value < 0.0 || scaled >= f64::from(u32::MAX) + 0.5 {
            return Err(ThresholdConversionError::OutOfRange);
        }
        Ok(Unsigned32Threshold {
            raw: (scaled + 0.5) as u32,
        })
    }
}

/// Output mode encoded by each two-bit `GPIOxCTRL` field (datasheet Tables 62 and 63).
#[derive(Specifier, Copy, Clone, Eq, PartialEq, Debug)]
#[bits = 2]
pub enum GpioCtrl {
    /// High-impedance input.
    Tristate = 0b00,
    /// Drive low (DGND).
    Low = 0b01,
    /// Toggle at 400 kHz.
    Toggle400kHz = 0b10,
    /// Drive high (DVCC).
    High = 0b11,
}

/// Clock configuration used to scale accumulated charge, energy, and time thresholds.
///
/// [`Internal`](Self::Internal) also covers a 4 MHz crystal with the datasheet's `PRE = 2`,
/// `DIV = 30` settings (Table 26). The external-clock formula follows Table 27.
#[derive(Copy, Clone, PartialEq, Debug, Default)]
pub enum AccumulatorClock {
    #[default]
    Internal,
    External {
        /// External clock frequency in hertz.
        frequency_hz: f64,
        /// PRE field value (0–7).
        pre: u8,
        /// DIV field value (0–31).
        div: u8,
    },
}

/// Errors returned when an [`AccumulatorClock`] cannot be converted into accumulator LSBs.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum AccumulatorClockError {
    /// The clock configuration is invalid for the datasheet formula.
    InvalidConfiguration,
}

impl AccumulatorClock {
    /// Returns the charge, energy, and time LSBs for this clock configuration.
    pub fn lsbs(self) -> Result<(f64, f64, f64), AccumulatorClockError> {
        match self {
            AccumulatorClock::Internal => Ok((
                AccumulatedCharge::LSB_VOLT_SECONDS,
                AccumulatedEnergy::LSB_VOLT_SQUARED_SECONDS,
                AccumulatedTime::LSB_SECONDS,
            )),
            AccumulatorClock::External { frequency_hz, pre, div } => {
                if !frequency_hz.is_finite() || frequency_hz <= 0.0 || pre > 7 || div > 31 {
                    return Err(AccumulatorClockError::InvalidConfiguration);
                }
                let clock_factor = f64::from(1u16 << pre) * (f64::from(div) + 1.0) / frequency_hz;
                Ok((
                    1.21899e-5 * clock_factor,
                    7.4895e-5 * clock_factor,
                    12.8315 * clock_factor,
                ))
            }
        }
    }
}

impl AccumulatedTime {
    /// Seconds represented by one raw code with the internal clock or a 4 MHz crystal.
    pub const LSB_SECONDS: f64 = 397.777e-6;

    /// Wraps a raw unsigned 32-bit accumulator code.
    pub const fn from_raw(raw: u32) -> Self {
        Self { raw }
    }

    /// Returns the raw unsigned 32-bit accumulator code.
    pub fn raw(self) -> u32 {
        self.raw
    }

    /// Decodes the result into seconds for the internal clock or a 4 MHz crystal.
    ///
    /// With a freely configured external clock, use the LSB formula from datasheet Table 27
    /// instead because the scale depends on `fEXT`, `PRE` and `DIV`.
    pub fn decode(self) -> f64 {
        self.raw as f64 * Self::LSB_SECONDS
    }
}

/// A device register resolving to a memory [`Page`] and an on-bus address byte, so the
/// framing helpers take a register and derive the page rather than threading both.
trait Register: Copy {
    const PAGE: Page;
    fn addr(self) -> u8;
}

impl Register for RegAddressP0 {
    const PAGE: Page = Page::Page0;
    fn addr(self) -> u8 {
        self as u8
    }
}

impl Register for RegAddressP1 {
    const PAGE: Page = Page::Page1;
    fn addr(self) -> u8 {
        self as u8
    }
}

/// GPIO4 heartbeat control register (`GPIO4HBCTRL`, PAGE0 0xE8; datasheet Table 61).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct Gpio4HeartbeatControlRegister {
    /// Enable the GPIO4 heartbeat output.
    pub gpio4_hb_enable: bool,
    // Reserved bits 7:1. Write as zero.
    #[skip]
    __: B7,
}

/// Current-source and GPIO5 control register (`FCURGPIOCTRL`, PAGE0 0xF1;
/// datasheet Table 62).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct CurGpioControlRegister {
    /// GPIO5 output mode.
    #[bits = 2]
    pub gpio5_ctrl: GpioCtrl,
    // Reserved bits 3:2. Write as zero.
    #[skip]
    __: B2,
    /// MUXP current-source polarity.
    pub mux_p_cur_pol: bool,
    /// Enable the MUXP current source.
    pub mux_p_cur_en: bool,
    /// MUXN current-source polarity.
    pub mux_n_cur_pol: bool,
    /// Enable the MUXN current source.
    pub mux_n_cur_en: bool,
}

/// GPIO1 through GPIO4 control register (`FGPIOCTRL`, PAGE0 0xF2;
/// datasheet Table 63).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct GpioControlRegister {
    /// GPIO1 output mode (bits 1:0).
    #[bits = 2]
    pub gpio1_ctrl: GpioCtrl,
    /// GPIO2 output mode (bits 3:2).
    #[bits = 2]
    pub gpio2_ctrl: GpioCtrl,
    /// GPIO3 output mode (bits 5:4).
    #[bits = 2]
    pub gpio3_ctrl: GpioCtrl,
    /// GPIO4 output mode (bits 7:6).
    #[bits = 2]
    pub gpio4_ctrl: GpioCtrl,
}

/// Operation Control register (PAGE0, 0xF0) — datasheet Table 24. `clr`, `sshot`, `adjupd`
/// and `rst` are set-only; the device clears them once done (poll to observe completion).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct OpsControlRegister {
    /// SLEEP: `0` = normal operation, `1` = enter SLEEP. The part exits SLEEP on CSB low
    /// in SPI mode or the datasheet wake-up pulse sequence in isoSPI mode.
    pub sleep: bool,
    /// CLR, set-only: clear accumulation and tracking registers, including charge, energy,
    /// time-base, max/min measurement, temperature, VCC and SLOT tracking registers.
    pub clr: bool,
    /// SSHOT, set-only: start one measurement set, update result registers, then return to
    /// STANDBY. If CONT is set, the current conversion cycle completes first.
    pub sshot: bool,
    /// CONT: enable continuous measurement. Charge, energy and time accumulation are active
    /// only while continuous mode is enabled.
    pub cont: bool,
    // Reserved bit 4. Write as 0.
    #[skip]
    __: B1,
    /// ADJUPD, set-only: request an update of page-1 configuration registers except
    /// thresholds. Issue in STANDBY; the device clears the bit when the update is done.
    pub adjupd: bool,
    // Reserved bit 6. Write as 0.
    #[skip]
    __: B1,
    /// RST, set-only: request software reset. The reset function is locked by default and
    /// requires the RSTUNLCK sequence before this bit has an effect.
    pub rst: bool,
}

/// Fast Control register (PAGE0, 0xF5) — datasheet Table 60.
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct FastControlRegister {
    /// FACONV: enable continuous fast conversions. When this is set and at least one of
    /// `FACHA`, `FACH1` or `FACH2` is set, fast conversion results are written to the
    /// corresponding FIFO.
    pub faconv: bool,
    /// FACHA: include the auxiliary channel in fast mode. AUX fast conversions start after
    /// a FACONV rising edge or when an ADCV-style command is issued.
    pub facha: bool,
    /// FACH1: include channel 1 in fast mode. CH1 fast conversions start after a FACONV
    /// rising edge or when an ADCV-style command is issued.
    pub fach1: bool,
    /// FACH2: include channel 2 in fast mode. CH2 fast conversions start after a FACONV
    /// rising edge or when an ADCV-style command is issued.
    pub fach2: bool,
    // Reserved bits [7:4]. Write as 0.
    #[skip]
    __: B4,
}

/// ADC Configuration register (PAGE1, 0xDF) — datasheet Table 69. `p1asv`/`p2asv` set
/// power ADCs to voltage mode; `ntc1`/`ntc2` linearise the SLOT; `ntcslot1` ties CH2 TC to NTC1.
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct AdcConfiguration {
    /// P1ASV: configure P1ADC as voltage instead of power. `0` = power mode,
    /// `1` = voltage mode.
    pub p1asv: bool,
    /// P2ASV: configure P2ADC as voltage instead of power. `0` = power mode,
    /// `1` = voltage mode.
    pub p2asv: bool,
    // Reserved bit 2. Write as 0.
    #[skip]
    __: B1,
    /// NTC1: SLOT1 result mode. `0` = voltage measurement with 375 uV LSB,
    /// `1` = NTC temperature measurement with 0.2 deg C LSB.
    pub ntc1: bool,
    /// NTC2: SLOT2 result mode. `0` = voltage measurement with 375 uV LSB,
    /// `1` = NTC temperature measurement with 0.2 deg C LSB.
    pub ntc2: bool,
    // Reserved bit 5. Write as 0.
    #[skip]
    __: B1,
    /// NTCSLOT1: shunt temperature-compensation source selection. `0` links I1 TC to
    /// SLOT1 and I2 TC to SLOT2; `1` links both I1 and I2 TC compensation to SLOT1.
    pub ntcslot1: bool,
    // Reserved bit 7. Write as 0.
    #[skip]
    __: B1,
}

/// Register Control register (common to both pages, 0xFF) — datasheet Table 23. `mlk` is
/// the 2-bit memory-lock handshake (`0b01` request, `0b10` device-acknowledged).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct RegControlRegister {
    /// PAGE: active memory-map page. `0` selects PAGE0 result/control registers;
    /// `1` selects PAGE1 threshold/configuration registers.
    pub page: bool,
    // Reserved bit 1. Write as 0.
    #[skip]
    __: B1,
    /// BCREN: broadcast read enable. Keep cleared in the parallel-to-daisy-chain topology
    /// so the LTC2949 does not respond to broadcast reads and collide with cell monitors.
    pub bcren: bool,
    // Reserved bit 3. Write as 0.
    #[skip]
    __: B1,
    /// MLK\[1:0\]: memory-lock handshake. `0b00` = unlocked, `0b01` = lock requested by
    /// master, `0b10` = memory locked/acknowledged by the LTC2949.
    pub mlk: B2,
    // Reserved bit 6. Write as 0.
    #[skip]
    __: B1,
    /// RDCVCONF: RDCV readout mode. `0` = indirect memory access from RDCVIADDR (0xFC);
    /// `1` = RDCV reports the latest fast-channel conversion results.
    pub rdcvconf: bool,
}

impl Default for OpsControlRegister {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for Gpio4HeartbeatControlRegister {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for CurGpioControlRegister {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for GpioControlRegister {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for FastControlRegister {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for AdcConfiguration {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for RegControlRegister {
    fn default() -> Self {
        Self::new()
    }
}

/// Status register (PAGE0, 0x80; datasheet Table 26).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct StatusRegister {
    /// UVLOA: analog supply undervoltage lockout occurred.
    pub uvloa: bool,
    /// PORA: analog supply power-on reset occurred.
    pub pora: bool,
    /// UVLOSTBY: standby regulator undervoltage lockout occurred.
    pub uvlostby: bool,
    /// UVLOD: digital supply undervoltage lockout occurred.
    pub uvlod: bool,
    /// UPDATE: result registers have been updated.
    pub update: bool,
    /// ADCERR: ADC error occurred.
    pub adcerr: bool,
    /// TBERR: time-base error occurred.
    pub tberr: bool,
    // Reserved bit 7.
    #[skip]
    __: B1,
}

/// Voltage, Temperature Threshold Alerts register (PAGE0, 0x81; datasheet Table 35).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct VTAlerts {
    /// BATH: voltage (VBATP – VBATM) high threshold exceeded.
    pub bath: bool,
    /// BATL: voltage (VBATP – VBATM) low threshold exceeded.
    pub batl: bool,
    /// TEMPH: temperature high threshold exceeded.
    pub temph: bool,
    /// TEMPL: temperature low threshold exceeded.
    pub templ: bool,
    /// SLOT1H: SLOT1 high threshold exceeded.
    pub slot1h: bool,
    /// SLOT1L: SLOT1 low threshold exceeded.
    pub slot1l: bool,
    /// SLOT2H: SLOT2 high threshold exceeded.
    pub slot2h: bool,
    /// SLOT2L: SLOT2 low threshold exceeded.
    pub slot2l: bool,
}

/// Current and power threshold alerts (STATIP, PAGE0, 0x82; datasheet Table 36).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct IPAlerts {
    /// I1H: Current1 high threshold exceeded.
    pub i1h: bool,
    /// I1L: Current1 low threshold exceeded.
    pub i1l: bool,
    /// P1H: Power1 high threshold exceeded.
    pub p1h: bool,
    /// P1L: Power1 low threshold exceeded.
    pub p1l: bool,
    /// I2H: Current2 high threshold exceeded.
    pub i2h: bool,
    /// I2L: Current2 low threshold exceeded.
    pub i2l: bool,
    /// P2H: Power2 high threshold exceeded.
    pub p2h: bool,
    /// P2L: Power2 low threshold exceeded.
    pub p2l: bool,
}

/// Charge threshold alerts (STATC, PAGE0, 0x83; datasheet Table 37).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct CAlerts {
    /// C1H: Charge1 high threshold exceeded.
    pub c1h: bool,
    /// C1L: Charge1 low threshold exceeded.
    pub c1l: bool,
    /// C2H: Charge2 high threshold exceeded.
    pub c2h: bool,
    /// C2L: Charge2 low threshold exceeded.
    pub c2l: bool,
    /// C3H: Charge3 high threshold exceeded.
    pub c3h: bool,
    /// C3L: Charge3 low threshold exceeded.
    pub c3l: bool,
    // Reserved bits 6–7.
    #[skip]
    __: B2,
}

/// Energy threshold alerts (STATE, PAGE0, 0x84; datasheet Table 38).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct EAlerts {
    /// E1H: Energy1 high threshold exceeded.
    pub e1h: bool,
    /// E1L: Energy1 low threshold exceeded.
    pub e1l: bool,
    /// E2H: Energy2 high threshold exceeded.
    pub e2h: bool,
    /// E2L: Energy2 low threshold exceeded.
    pub e2l: bool,
    // Reserved bits 4–5.
    #[skip]
    __: B2,
    /// E4H: Energy4 high threshold exceeded.
    pub e4h: bool,
    /// E4L: Energy4 low threshold exceeded.
    pub e4l: bool,
}

/// Charge and energy overflow alerts (STATCEOF, PAGE0, 0x85; datasheet Table 39).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct CEOFAlerts {
    /// C1OVF: Charge1 accumulator overflowed.
    pub c1ovf: bool,
    /// C2OVF: Charge2 accumulator overflowed.
    pub c2ovf: bool,
    /// C3OVF: Charge3 accumulator overflowed.
    pub c3ovf: bool,
    // Reserved bit 3.
    #[skip]
    __: B1,
    /// E1OVF: Energy1 accumulator overflowed.
    pub e1ovf: bool,
    /// E2OVF: Energy2 accumulator overflowed.
    pub e2ovf: bool,
    // Reserved bit 6.
    #[skip]
    __: B1,
    /// E4OVF: Energy4 accumulator overflowed.
    pub e4ovf: bool,
}

/// Time-base threshold and overflow alerts (STATTB, PAGE0, 0x86; datasheet Table 40).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct TBAlerts {
    /// T1TH: Time1 threshold exceeded.
    pub t1th: bool,
    /// T2TH: Time2 threshold exceeded.
    pub t2th: bool,
    /// T3TH: Time3 threshold exceeded.
    pub t3th: bool,
    /// T4TH: Time4 threshold exceeded.
    pub t4th: bool,
    /// T1OVF: Time1 overflowed.
    pub t1ovf: bool,
    /// T2OVF: Time2 overflowed.
    pub t2ovf: bool,
    /// T3OVF: Time3 overflowed.
    pub t3ovf: bool,
    /// T4OVF: Time4 overflowed.
    pub t4ovf: bool,
}

/// VCC and overcurrent-comparator alerts (STATVCC, PAGE0, 0x87; datasheet Table 41).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct VCCAlerts {
    /// VCCH: VCC high threshold exceeded.
    pub vcch: bool,
    /// VCCL: VCC low threshold exceeded.
    pub vccl: bool,
    /// OCC1H: Current1 remained above the OCC1 threshold for longer than its deglitch time.
    pub occ1h: bool,
    /// OCC2H: Current2 remained above the OCC2 threshold for longer than its deglitch time.
    pub occ2h: bool,
    // Reserved bits 4–7.
    #[skip]
    __: B4,
}

/// Fault register (PAGE0, 0xDD; datasheet Table 28).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct FaultsRegister {
    /// PROMERR: PROM read error occurred.
    pub promerr: bool,
    /// TSD: thermal shutdown occurred.
    pub tsd: bool,
    /// INTCOMMERR: internal communication error occurred.
    pub intcommerr: bool,
    /// EXTCOMMERR: external communication error occurred.
    pub extcommerr: bool,
    /// FAERR: fast-acquisition error occurred.
    pub faerr: bool,
    /// HWBIST: hardware built-in self-test failed.
    pub hwbist: bool,
    /// CRCCFG: configuration CRC error occurred.
    pub crccfg: bool,
    /// CRCMEM: memory CRC error occurred.
    pub crcmem: bool,
}

/// Extended fault register (PAGE0, 0xDC; datasheet Table 33).
#[bitfield(bits = 8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub struct ExtFaultsRegister {
    /// HD1BITERR: Hamming decoder 1-bit error occurred.
    pub hd1biterr: bool,
    /// ROMERR: ROM CRC error occurred.
    pub romerr: bool,
    /// MEMERR: memory error occurred.
    pub memerr: bool,
    /// FCAERR: fast-channel error occurred.
    pub fcaerr: bool,
    /// XRAMERR: XRAM error occurred.
    pub xramerr: bool,
    /// IRAMERR: IRAM error occurred.
    pub iramerr: bool,
    // Reserved bit 6.
    #[skip]
    __: B1,
    /// HWMBISTEXEC: memory BIST was executed.
    pub hwmbistexec: bool,
}

/// FIFO sample tag values (datasheet Table 30).
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum FifoTag {
    /// New, valid sample.
    Ok,
    /// Sample was already read; no new data since last drain.
    ReadOverrun,
    /// FIFO filled and at least one sample was overwritten.
    WriteOverrun,
    /// Anything else returned by the device.
    Unknown(u8),
}

impl FifoTag {
    fn from_byte(b: u8) -> Self {
        match b {
            0x00 => FifoTag::Ok,
            0x55 => FifoTag::ReadOverrun,
            0xAA => FifoTag::WriteOverrun,
            other => FifoTag::Unknown(other),
        }
    }
}

/// One fast-mode FIFO sample.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct FifoSample {
    /// Raw 16-bit two's-complement ADC reading. Multiply by the channel's LSB
    /// (7.60371 µV for I1/I2, 375.183 µV for BAT/AUX) to get a physical value.
    pub raw: i16,
    pub tag: FifoTag,
}

/// Coherent snapshot of one channel's charge / energy / time-base accumulators. All three
/// share a 16-byte row, so one burst reads them from the same `CONT` cycle (no lock needed).
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct Accumulators {
    /// Charge (48-bit two's-complement).
    pub charge: AccumulatedCharge,
    /// Energy (48-bit two's-complement).
    pub energy: AccumulatedEnergy,
    /// Time base (32-bit unsigned).
    pub time: AccumulatedTime,
}

/// Overcurrent control register payload for OCC1CTRL/OCC2CTRL (datasheet Tables 50 & 51).
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct OverCurrentConfig {
    /// OCCEN: enables the overcurrent comparator for the channel.
    pub enable: bool,
    /// OCCxDAC\[2:0\]: differential shunt-voltage threshold selector. Values map to
    /// 0, 26, 52, 78, 103, 155, 207 and 310 mV; convert to current with
    /// `I_limit = V_threshold / R_shunt`.
    pub threshold: u8,
    /// OCCxDGL\[1:0\]: deglitch time selector. Values map to off, 80 us, 320 us and
    /// 1280 us before the comparator event is accepted.
    pub deglitch_time: u8,
    /// OCCxPOL\[1:0\]: comparator polarity selector. `0b00` checks both current directions;
    /// `0b01` positive only; `0b10` negative only.
    pub polarity: u8,
}

/// One of the two LTC2949 measurement channels — each pairs a current/power ADC, a SLOT,
/// an NTC lineariser and a sense-resistor TC entry (suffix 1 vs. 2).
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Channel {
    One,
    Two,
}

/// Steinhart–Hart parameters for one NTC channel: `1/T = a + b·ln(R) + c·ln(R)³`, with `R`
/// inferred from `r_ref`. Stored on-chip as Float24 (driver handles `f32 → Float24`).
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct NtcConfig {
    /// Series reference resistor `R_ref` in ohms.
    pub r_ref: f32,
    /// Coefficient A (units: 1/K).
    pub a: f32,
    /// Coefficient B (units: 1/K).
    pub b: f32,
    /// Coefficient C (units: 1/K).
    pub c: f32,
}

/// Sense-resistor temperature-drift compensation: `R(T) = R0·[1 + tc·(T-t_ref) + tc2·(T-t_ref)²]`,
/// `T` being the linearised NTC reading. Copper ≈ `0.0039 /K`; low-TC alloys can stay uncompensated.
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct ShuntTcConfig {
    /// First-order temperature coefficient (1/K). Datasheet `RSnTC`.
    pub tc: f32,
    /// Reference temperature `T0` in °C where the resistor is nominal (datasheet `RSnT0`,
    /// stored as 16-bit truncated Float24).
    pub t_ref: f32,
    /// Second-order temperature coefficient (1/K²). Datasheet `RSnTC2`. Set to
    /// `0.0` to disable the quadratic term — fine for copper.
    pub tc2: f32,
}

/// Inputs the AUX multiplexer can route to the SLOT pair (`MUXP`/`MUXN`) or the fast-mode
/// `FAMUX` registers. Discriminants match the 5-bit encoding in datasheet Table 57.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
pub enum MuxInput {
    Agnd = 0,
    V1 = 1,
    V2 = 2,
    V3 = 3,
    V4 = 4,
    V5 = 5,
    V6 = 6,
    V7 = 7,
    V8 = 8,
    V9 = 9,
    V10 = 10,
    V11 = 11,
    V12 = 12,
    /// Overcurrent comparator test input (negative side).
    Imt = 13,
    /// Overcurrent comparator test input (positive side).
    Ipt = 14,
    VbatM = 15,
    VbatP = 16,
    Cf2M = 17,
    Cf2P = 18,
    Cf1M = 19,
    Cf1P = 20,
    /// Internal 2.39 V redundant reference.
    Vref2 = 22,
    /// `VREF2` routed through an internal 250 kΩ — useful for self-test of the
    /// AUX MUX current sources.
    Vref2Via250k = 23,
}

/// One of the four programmable AUX-MUX gain-correction settings in Table 72.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum MuxGainSlot {
    One,
    Two,
    Three,
    Four,
}

/// Gain correction applied to one current-sense channel (`RS1GC` or `RS2GC`).
///
/// Construct it either from an already calculated dimensionless correction factor or from
/// the shunt's nominal and measured resistance. In the latter case the programmed factor is
/// `nominal_ohms / actual_ohms`, as specified below datasheet Table 72.
///
/// Input validation happens in [`Client::write_shunt_gain_correction`], allowing both
/// constructors to remain infallible and convenient in configuration structures.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ShuntGainCorrection {
    /// An already calculated, dimensionless correction factor.
    CorrectionFactor(f32),
    /// The nominal and measured shunt resistances used to calculate the correction factor.
    Resistances { nominal_ohms: f32, actual_ohms: f32 },
}

impl ShuntGainCorrection {
    /// Uses an already calculated, dimensionless correction factor.
    pub const fn from_correction_factor(correction_factor: f32) -> Self {
        Self::CorrectionFactor(correction_factor)
    }

    /// Calculates the correction factor from the nominal and measured shunt resistances.
    pub const fn from_resistances(nominal_ohms: f32, actual_ohms: f32) -> Self {
        Self::Resistances {
            nominal_ohms,
            actual_ohms,
        }
    }

    /// Returns the dimensionless factor that will be written to the gain-correction register.
    ///
    /// This is the supplied factor or `nominal_ohms / actual_ohms`. The write method rejects
    /// non-positive or non-finite inputs before accessing the bus.
    pub fn correction_factor(self) -> f32 {
        match self {
            Self::CorrectionFactor(correction_factor) => correction_factor,
            Self::Resistances {
                nominal_ohms,
                actual_ohms,
            } => nominal_ohms / actual_ohms,
        }
    }
}

/// A strictly positive, finite ratio.
///
/// This wraps the `numerator / denominator` check used for shunt calibration and shunt-ratio
/// writes so callers can validate it once and reuse the resulting value.
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct PositiveRatio(f32);

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum PositiveRatioError {
    NonPositiveOrNonFinite,
}

impl PositiveRatio {
    /// Constructs a ratio from an already calculated dimensionless value.
    pub fn try_from_ratio(ratio: f32) -> Result<Self, PositiveRatioError> {
        if !ratio.is_finite() || ratio <= 0.0 {
            return Err(PositiveRatioError::NonPositiveOrNonFinite);
        }
        Ok(Self(ratio))
    }

    /// Constructs a ratio from `numerator / denominator`.
    pub fn try_from_parts(numerator: f32, denominator: f32) -> Result<Self, PositiveRatioError> {
        if !numerator.is_finite() || numerator <= 0.0 || !denominator.is_finite() || denominator <= 0.0 {
            return Err(PositiveRatioError::NonPositiveOrNonFinite);
        }
        Self::try_from_ratio(numerator / denominator)
    }

    /// Returns the stored ratio.
    pub const fn value(self) -> f32 {
        self.0
    }
}

impl From<PositiveRatio> for f32 {
    fn from(ratio: PositiveRatio) -> Self {
        ratio.value()
    }
}

/// Errors that can occur talking to an LTC2949.
pub enum Error<B: SpiDevice<u8>> {
    /// Underlying SPI transaction failed.
    BusError(B::Error),
    /// A returned PEC did not match the calculated value.
    ChecksumMismatch,
    /// A threshold was non-finite, used an invalid clock/shunt configuration, had its low
    /// bound above its high bound, or did not fit in the corresponding Table 67 register.
    InvalidThreshold,
    /// A gain factor or resistance used to derive one was zero, negative, NaN, or infinite.
    InvalidGainCorrection,
}

impl<B: SpiDevice<u8>> core::fmt::Debug for Error<B> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Error::BusError(_) => f.debug_struct("BusError").finish(),
            Error::ChecksumMismatch => f.debug_struct("ChecksumMismatch").finish(),
            Error::InvalidThreshold => f.debug_struct("InvalidThreshold").finish(),
            Error::InvalidGainCorrection => f.debug_struct("InvalidGainCorrection").finish(),
        }
    }
}

impl<B: SpiDevice<u8>> From<ThresholdConversionError> for Error<B> {
    fn from(_: ThresholdConversionError) -> Self {
        Self::InvalidThreshold
    }
}

/// High-level LTC2949 operation set, the dependency-injection seam for hosts (mirrors
/// [LTC681X client](https://docs.rs/ltc681x/0.6.2/ltc681x/monitor/trait.LTC681XClient.html).
/// FIFO drains stay on [`LTC2949`].
pub trait Client {
    type Error;

    /// Starts wake-up (datasheet Figure 20): pulses the isoSPI edge and invalidates the page
    /// cache. Returns the wait ([`T_BOOT_US`]) to observe before [`confirm_wake_up`](Self::confirm_wake_up).
    fn start_wake_up(&mut self) -> Result<u32, Self::Error>;

    /// Completes wake-up by writing WKUPACK so the device does not auto-sleep after `tACKN`
    /// (1 s). Call once the [`start_wake_up`](Self::start_wake_up) wait has elapsed.
    fn confirm_wake_up(&mut self) -> Result<(), Self::Error>;

    /// Re-wakes only the isoSPI port (idles after `tIDLE` = 6.4 ms), leaving the core alone.
    /// Returns the wait ([`T_READY_US`]) before the next transaction. Cell monitors share this.
    fn wake_isospi(&mut self) -> Result<u32, Self::Error>;

    /// Writes the Operation Control register (PAGE0, 0xF0).
    fn write_opctrl(&mut self, value: OpsControlRegister) -> Result<(), Self::Error>;

    /// Reads the Operation Control register.
    fn read_opctrl(&mut self) -> Result<OpsControlRegister, Self::Error>;

    /// Writes the Fast Control register (PAGE0, 0xF5).
    fn write_factrl(&mut self, value: FastControlRegister) -> Result<(), Self::Error>;

    /// Writes the ADC Configuration register (PAGE1, 0xDF). Takes effect only after an
    /// ADJUPD pulse on OPCTRL while the core is in STANDBY.
    fn write_adcconf(&mut self, value: AdcConfiguration) -> Result<(), Self::Error>;

    /// Writes the Float24 gain correction for sense resistor 1 or 2 (`RS1GC` or `RS2GC`,
    /// Table 72). Construct `correction` from either a dimensionless factor or the nominal
    /// and actual shunt resistances. Apply the change with an `ADJUPD` pulse while the device
    /// is in STANDBY.
    fn write_shunt_gain_correction(
        &mut self,
        channel: Channel,
        correction: ShuntGainCorrection,
    ) -> Result<(), Self::Error>;

    /// Calculates and writes `RSRATIO = RS1 / RS2` as Float24. This factor is used when
    /// channel 2 contributes to the combined C3 and E4 accumulators.
    fn write_shunt_ratio(&mut self, rs1_ohms: f32, rs2_ohms: f32) -> Result<(), Self::Error>;

    /// Writes the dimensionless battery-divider gain-correction factor (`BATGC`) as Float24.
    fn write_battery_gain_correction(&mut self, correction_factor: f32) -> Result<(), Self::Error>;

    /// Configures one of the four AUX-MUX gain corrections. Writes its Float24 `MUXnGC`
    /// factor and the matching `MUXNSETn`/`MUXPSETn` input codes from Table 57.
    /// Matching is polarity-independent in the LTC2949.
    fn write_mux_gain_correction(
        &mut self,
        slot: MuxGainSlot,
        correction_factor: f32,
        negative: MuxInput,
        positive: MuxInput,
    ) -> Result<(), Self::Error>;

    /// Writes the low and high threshold for C1, C2, or C3 (Table 67), converting coulombs
    /// with `shunt_ohms` and the selected accumulator clock. C3 uses the channel-1 shunt.
    fn write_charge_thresholds(
        &mut self,
        accumulator: ChargeAccumulator,
        thresholds: ThresholdPair<f64>,
        shunt_ohms: f64,
        clock: AccumulatorClock,
    ) -> Result<(), Self::Error>;

    /// Writes the low and high threshold for E1, E2, or E4 (Table 67), converting joules
    /// with `shunt_ohms` and the selected accumulator clock. E4 uses the channel-1 shunt.
    fn write_energy_thresholds(
        &mut self,
        accumulator: EnergyAccumulator,
        thresholds: ThresholdPair<f64>,
        shunt_ohms: f64,
        clock: AccumulatorClock,
    ) -> Result<(), Self::Error>;

    /// Writes a TB1–TB4 high threshold in seconds (Table 67).
    fn write_time_threshold(
        &mut self,
        time_base: TimeBase,
        threshold: Threshold<f64>,
        clock: AccumulatorClock,
    ) -> Result<(), Self::Error>;

    /// Writes an I1/I2 low/high threshold pair in amperes. `shunt_ohms` converts the
    /// requested current to differential shunt voltage. The 16-bit threshold LSB is 3.8 µV
    /// (four times the 18-bit I1/I2 result LSB).
    fn write_current_thresholds(
        &mut self,
        channel: Channel,
        thresholds: ThresholdPair<f32>,
        shunt_ohms: f32,
    ) -> Result<(), Self::Error>;

    /// Writes a power-mode P1/P2 low/high threshold pair in watts. Use this when the
    /// corresponding `PxASV` bit is clear. The 16-bit threshold LSB is four times the
    /// P1/P2 result-register LSB.
    fn write_power_thresholds(
        &mut self,
        channel: Channel,
        thresholds: ThresholdPair<f32>,
        shunt_ohms: f32,
    ) -> Result<(), Self::Error>;

    /// Writes a voltage-mode P1/P2 low/high threshold pair in volts. Use this when the
    /// corresponding `PxASV` bit is set. The 16-bit threshold LSB is 187.5 µV.
    fn write_power_as_voltage_thresholds(
        &mut self,
        channel: Channel,
        thresholds: ThresholdPair<f32>,
    ) -> Result<(), Self::Error>;

    /// Writes the BAT low/high threshold pair in volts.
    fn write_battery_thresholds(&mut self, thresholds: ThresholdPair<f32>) -> Result<(), Self::Error>;

    /// Writes the die-temperature low/high threshold pair in degrees Celsius.
    fn write_temperature_thresholds(&mut self, thresholds: ThresholdPair<f32>) -> Result<(), Self::Error>;

    /// Writes the A/DVCC low/high threshold pair in volts.
    fn write_vcc_thresholds(&mut self, thresholds: ThresholdPair<f32>) -> Result<(), Self::Error>;

    /// Writes a SLOT1/SLOT2 low/high threshold pair in volts (corresponding `NTCx` clear).
    fn write_slot_voltage_thresholds(
        &mut self,
        slot: Channel,
        thresholds: ThresholdPair<f32>,
    ) -> Result<(), Self::Error>;

    /// Writes a SLOT1/SLOT2 low/high threshold pair in degrees Celsius
    /// (corresponding `NTCx` set).
    fn write_slot_temperature_thresholds(
        &mut self,
        slot: Channel,
        thresholds: ThresholdPair<f32>,
    ) -> Result<(), Self::Error>;

    /// Writes the Fast AUX mux selection (FAMUXP, FAMUXN).
    fn write_fast_aux_mux(&mut self, mux_n: u8, mux_p: u8) -> Result<(), Self::Error>;

    /// Writes the Float24 Steinhart–Hart coefficients and reference resistor for an NTC
    /// channel. Activate via [`write_slot_mux`](Self::write_slot_mux), [`AdcConfiguration::ntc1`] and an ADJUPD pulse.
    fn write_ntc_coefficients(&mut self, channel: Channel, params: &NtcConfig) -> Result<(), Self::Error>;

    /// Writes the sense-resistor temperature-compensation parameters for one channel. Active
    /// only after an `ADJUPD` pulse on `OpCtrl` while the core is in STANDBY.
    fn write_shunt_tc(&mut self, channel: Channel, config: &ShuntTcConfig) -> Result<(), Self::Error>;

    /// Routes `negative`/`positive` to the SLOT's `MUXN`/`MUXP` for differential AUX-ADC
    /// reads (datasheet Tables 57 & 58). NTCs typically use `(Vx, Agnd)`.
    fn write_slot_mux(&mut self, slot: Channel, negative: MuxInput, positive: MuxInput) -> Result<(), Self::Error>;

    /// Writes GPIO1 through GPIO4 control (`FGPIOCTRL`, datasheet Table 63).
    ///
    /// This write also transfers the previously written `FCURGPIOCTRL` value into the
    /// device's active configuration. Keep at least 1 ms between writes to `FGPIOCTRL`.
    fn write_gpio_ctrl(&mut self, gpio: GpioControlRegister) -> Result<(), Self::Error>;

    /// Writes GPIO5 and MUX current-source control (`FCURGPIOCTRL`, datasheet Table 62).
    ///
    /// The value does not become active until `FGPIOCTRL` is subsequently written. Prefer
    /// [`write_gpio_controls`](Self::write_gpio_controls) when configuring both registers.
    fn write_gpio5_config(&mut self, config: CurGpioControlRegister) -> Result<(), Self::Error>;

    /// Writes `FCURGPIOCTRL` and `FGPIOCTRL` in one burst, as recommended by the datasheet.
    ///
    /// Writing the second byte makes both configurations active. Keep at least 1 ms between
    /// calls that write `FGPIOCTRL`.
    fn write_gpio_controls(
        &mut self,
        gpio5_and_current: CurGpioControlRegister,
        gpio1_to_4: GpioControlRegister,
    ) -> Result<(), Self::Error>;

    /// Enables or disables the GPIO4 heartbeat (`GPIO4HBCTRL`, datasheet Table 61).
    fn write_gpio4_hb(&mut self, hb: bool) -> Result<(), Self::Error>;

    /// Writes both overcurrent-comparator control registers (`OCC1CTRL`/`OCC2CTRL`) in
    /// one PAGE0 burst. `config1` applies to channel 1; `config2` applies to channel 2.
    fn write_occ_config(&mut self, config1: OverCurrentConfig, config2: OverCurrentConfig) -> Result<(), Self::Error>;

    /// Reads and decodes the STATUS register (PAGE0, 0x80; datasheet Table 26).
    fn read_status(&mut self) -> Result<StatusRegister, Self::Error>;

    /// Reads voltage, temperature, and SLOT threshold alerts (STATVT, PAGE0, 0x81;
    /// datasheet Table 35). Set bits indicate that the corresponding high or low threshold
    /// was exceeded; the flags are sticky read/write bits.
    fn read_vt_alerts(&mut self) -> Result<VTAlerts, Self::Error>;

    /// Reads current and power threshold alerts (STATIP, PAGE0, 0x82; datasheet Table 36).
    fn read_ip_alerts(&mut self) -> Result<IPAlerts, Self::Error>;

    /// Reads accumulated-charge threshold alerts (STATC, PAGE0, 0x83; datasheet Table 37).
    fn read_c_alerts(&mut self) -> Result<CAlerts, Self::Error>;

    fn read_e_alerts(&mut self) -> Result<EAlerts, Self::Error>;

    /// Reads charge and energy accumulator overflow alerts (STATCEOF, PAGE0, 0x85;
    /// datasheet Table 39).
    fn read_ceof_alerts(&mut self) -> Result<CEOFAlerts, Self::Error>;

    /// Reads time-base threshold and overflow alerts (STATTB, PAGE0, 0x86;
    /// datasheet Table 40).
    fn read_tb_alerts(&mut self) -> Result<TBAlerts, Self::Error>;

    /// Reads VCC threshold and overcurrent-comparator alerts (STATVCC, PAGE0, 0x87;
    /// datasheet Table 41).
    fn read_vcc_alerts(&mut self) -> Result<VCCAlerts, Self::Error>;

    /// Reads and decodes the FAULTS register (PAGE0, 0xDD; datasheet Table 28).
    fn read_faults(&mut self) -> Result<FaultsRegister, Self::Error>;

    /// Reads and decodes the EXTFAULTS register (PAGE0, 0xDC; datasheet Table 33).
    fn read_extfaults(&mut self) -> Result<ExtFaultsRegister, Self::Error>;

    /// Requests the memory lock (datasheet Figure 19, `MLK = 0b01`) for a cross-register
    /// snapshot; returns the wait ([`T_MLCK_US`]). Stay on one page, then [`unlock_memory`](Self::unlock_memory).
    fn request_memory_lock(&mut self) -> Result<u32, Self::Error>;

    /// Releases the memory lock (`MLK = 0b00`), letting the register map update again.
    fn unlock_memory(&mut self) -> Result<(), Self::Error>;

    /// Reads I1 (slow-mode current 1) as a raw signed 24-bit current-sense voltage result.
    /// LSB = 950 nV.
    fn read_current1(&mut self) -> Result<CurrentSenseVoltage, Self::Error>;

    /// Reads I2 (slow-mode current 2). LSB = 950 nV.
    fn read_current2(&mut self) -> Result<CurrentSenseVoltage, Self::Error>;

    /// Reads I1AVG (moving average of the four preceding current 1 measurements).
    /// LSB = 237.5 nV.
    fn read_current1_avg(&mut self) -> Result<AveragedCurrentSenseVoltage, Self::Error>;

    /// Reads I2AVG (moving average of the four preceding current 2 measurements).
    /// LSB = 237.5 nV.
    fn read_current2_avg(&mut self) -> Result<AveragedCurrentSenseVoltage, Self::Error>;

    /// Reads P1 (power 1 or voltage if P1ASV is set). LSB = 5.8368 µV²/Ω (power) or
    /// 46.875 µV (voltage).
    fn read_power1(&mut self) -> Result<PowerOrVoltage, Self::Error>;

    /// Reads P2 (power 2 or voltage if P2ASV is set). LSB = 5.8368 µV²/Ω (power) or
    /// 46.875 µV (voltage).
    fn read_power2(&mut self) -> Result<PowerOrVoltage, Self::Error>;

    /// Reads BAT (battery voltage). LSB = 375 µV.
    fn read_bat(&mut self) -> Result<BatteryVoltage, Self::Error>;

    /// Reads internal die temperature. LSB = 0.2 °C, full-scale 819.2 K.
    fn read_temp(&mut self) -> Result<DieTemperature, Self::Error>;

    /// Reads A/DVCC supply voltage. LSB = 2.26 mV.
    fn read_vcc(&mut self) -> Result<SupplyVoltage, Self::Error>;

    /// Reads SLOT1 as a [`SlotValue`] — voltage or temperature depending on NTC1 in
    /// ADCCONF.
    fn read_slot1(&mut self) -> Result<SlotValue, Self::Error>;

    /// Reads SLOT2 as a [`SlotValue`] — voltage or temperature depending on NTC2 in
    /// ADCCONF.
    fn read_slot2(&mut self) -> Result<SlotValue, Self::Error>;

    /// Reads Charge1 (48-bit two's-complement) as [`AccumulatedCharge`].
    fn read_charge1(&mut self) -> Result<AccumulatedCharge, Self::Error>;

    /// Reads Charge2 (48-bit two's-complement) as [`AccumulatedCharge`].
    fn read_charge2(&mut self) -> Result<AccumulatedCharge, Self::Error>;

    /// Reads Charge3 — the weighted channel-1/channel-2 sum (64-bit two's-complement).
    fn read_charge3(&mut self) -> Result<AccumulatedCharge, Self::Error>;

    /// Reads Energy1 (48-bit two's-complement) as [`AccumulatedEnergy`].
    fn read_energy1(&mut self) -> Result<AccumulatedEnergy, Self::Error>;

    /// Reads Energy2 (48-bit two's-complement) as [`AccumulatedEnergy`].
    fn read_energy2(&mut self) -> Result<AccumulatedEnergy, Self::Error>;

    /// Reads Energy4 — the weighted channel-1/channel-2 sum (64-bit two's-complement).
    fn read_energy4(&mut self) -> Result<AccumulatedEnergy, Self::Error>;

    /// Reads time-base 1 (32-bit unsigned) as [`AccumulatedTime`].
    fn read_time1(&mut self) -> Result<AccumulatedTime, Self::Error>;

    /// Reads time-base 2 as [`AccumulatedTime`].
    fn read_time2(&mut self) -> Result<AccumulatedTime, Self::Error>;

    /// Reads time-base 3 as [`AccumulatedTime`].
    fn read_time3(&mut self) -> Result<AccumulatedTime, Self::Error>;

    /// Reads time-base 4 as [`AccumulatedTime`].
    fn read_time4(&mut self) -> Result<AccumulatedTime, Self::Error>;

    /// Reads channel 1's charge, energy and time-base ([`Accumulators`]) in one coherent
    /// 16-byte burst — prefer this for SoC integration over separate charge/time reads.
    fn read_accumulators1(&mut self) -> Result<Accumulators, Self::Error>;

    /// Reads channel 2's charge, energy and time-base in a single coherent 16-byte burst
    /// (row `0x10–0x1F`). See [`read_accumulators1`](Self::read_accumulators1).
    fn read_accumulators2(&mut self) -> Result<Accumulators, Self::Error>;

    /// Broadcast ADCV (0x0260): synchronous fast conversion on the LTC2949 and every cell
    /// monitor. **Hazard:** also restarts the chain, so don't call it from a separate meter task.
    fn trigger_adcv_broadcast(&mut self) -> Result<(), Self::Error>;

    /// Broadcasts ADAX (`0x0460`) on the shared bus.
    ///
    /// LTC2949 treats ADAX like every other ADCV-style command: with `CONT = 1` and
    /// `FACONV = 0`, it starts a fast single-shot conversion of the channels enabled in
    /// [`FastControlRegister`]. It does not specifically start a slow SLOT1/SLOT2 conversion.
    ///
    /// The broadcast also starts AUX conversions on attached LTC68xx cell monitors and clears
    /// the LTC2949 FIFOs before conversion. With `FACONV = 1`, LTC2949 ignores the command.
    fn trigger_adax(&mut self) -> Result<(), Self::Error>;
}

/// LTC2949 client for the parallel-to-daisy-chain topology (datasheet Figure 12(B)),
/// reached directly via `DCMD`. High-level ops are on the [`Client`] trait.
pub struct LTC2949<B, P>
where
    B: SpiDevice<u8>,
    P: PollMethod<B>,
{
    bus: B,
    poll_method: P,
    /// Tracks the currently selected page so `select_page` is a no-op when redundant.
    current_page: Option<Page>,
}

impl<B> LTC2949<B, NoPolling>
where
    B: SpiDevice<u8>,
{
    /// Constructs an LTC2949 client, addressed directly via `DCMD` (alone on the bus or in
    /// parallel with a cell-monitor chain).
    pub fn new(bus: B) -> Self {
        Self {
            bus,
            poll_method: NoPolling {},
            current_page: None,
        }
    }
}

impl<B, P> Client for LTC2949<B, P>
where
    B: SpiDevice<u8>,
    P: PollMethod<B>,
{
    type Error = Error<B>;

    fn start_wake_up(&mut self) -> Result<u32, Error<B>> {
        // A reset / SLEEP clears the device's page selection — drop the cache so the next
        // access re-issues REGSCTRL rather than trusting a stale page.
        self.current_page = None;

        // Two dummy bytes — content irrelevant, they just provide the isoSPI wake edge.
        self.bus.write(&[0x00]).map_err(Error::BusError)?;
        self.bus.write(&[0x00]).map_err(Error::BusError)?;

        // If the core was in SLEEP or just powered up it needs up to tBOOT to reach
        // STANDBY before it will accept the wake-up confirmation. The host owns the wait.
        Ok(T_BOOT_US)
    }

    fn confirm_wake_up(&mut self) -> Result<(), Error<B>> {
        // Confirm wake-up by writing 0x00 to WKUPACK (within tACKN = 1 s of STANDBY).
        self.write_bytes(RegAddressP0::WkupAck, &[0x00])
    }

    fn wake_isospi(&mut self) -> Result<u32, Error<B>> {
        // One dummy byte provides the differential edge; the port is ready after tREADY.
        // The core state is untouched. The host owns the wait.
        self.bus.write(&[0x00]).map_err(Error::BusError)?;
        Ok(T_READY_US)
    }

    fn write_opctrl(&mut self, value: OpsControlRegister) -> Result<(), Error<B>> {
        self.write_bytes(RegAddressP0::OpCtrl, &value.into_bytes())
    }

    fn read_opctrl(&mut self) -> Result<OpsControlRegister, Error<B>> {
        let mut buf = [0u8; 1];
        self.read_bytes(RegAddressP0::OpCtrl, &mut buf)?;
        Ok(OpsControlRegister::from_bytes(buf))
    }

    fn write_factrl(&mut self, value: FastControlRegister) -> Result<(), Error<B>> {
        self.write_bytes(RegAddressP0::FaCtrl, &value.into_bytes())
    }

    fn write_adcconf(&mut self, value: AdcConfiguration) -> Result<(), Error<B>> {
        self.write_bytes(RegAddressP1::AdcConf, &value.into_bytes())
    }

    fn write_shunt_gain_correction(
        &mut self,
        channel: Channel,
        correction: ShuntGainCorrection,
    ) -> Result<(), Error<B>> {
        let address = match channel {
            Channel::One => RegAddressP1::Rs1Gc,
            Channel::Two => RegAddressP1::Rs2Gc,
        };

        let correction_factor = match correction {
            ShuntGainCorrection::CorrectionFactor(correction_factor) => correction_factor,
            ShuntGainCorrection::Resistances {
                nominal_ohms,
                actual_ohms,
            } => PositiveRatio::try_from_parts(nominal_ohms, actual_ohms)
                .map_err(|_| Error::InvalidGainCorrection)?
                .value(),
        };

        self.write_gain_factor(address, correction_factor)
    }

    fn write_shunt_ratio(&mut self, rs1_ohms: f32, rs2_ohms: f32) -> Result<(), Error<B>> {
        let ratio = PositiveRatio::try_from_parts(rs1_ohms, rs2_ohms)
            .map_err(|_| Error::InvalidGainCorrection)?
            .value();
        self.write_gain_factor(RegAddressP1::RsRatio, ratio)
    }

    fn write_battery_gain_correction(&mut self, correction_factor: f32) -> Result<(), Error<B>> {
        self.write_gain_factor(RegAddressP1::BatGc, correction_factor)
    }

    fn write_mux_gain_correction(
        &mut self,
        slot: MuxGainSlot,
        correction_factor: f32,
        negative: MuxInput,
        positive: MuxInput,
    ) -> Result<(), Error<B>> {
        let (gain_address, mux_set_address) = match slot {
            MuxGainSlot::One => (RegAddressP1::Mux1Gc, RegAddressP1::MuxNset1),
            MuxGainSlot::Two => (RegAddressP1::Mux2Gc, RegAddressP1::MuxNset2),
            MuxGainSlot::Three => (RegAddressP1::Mux3Gc, RegAddressP1::MuxNset3),
            MuxGainSlot::Four => (RegAddressP1::Mux4Gc, RegAddressP1::MuxNset4),
        };
        let encoded = Self::encode_gain_factor(correction_factor)?;
        self.write_bytes(gain_address, &encoded)?;
        self.write_bytes(mux_set_address, &[negative as u8, positive as u8])
    }

    fn write_charge_thresholds(
        &mut self,
        accumulator: ChargeAccumulator,
        thresholds: ThresholdPair<f64>,
        shunt_ohms: f64,
        clock: AccumulatorClock,
    ) -> Result<(), Error<B>> {
        let (charge_lsb, _, _) = clock.lsbs().map_err(|_| Error::InvalidThreshold)?;
        let lsb_coulombs = Self::divide_lsb_by_shunt(charge_lsb, shunt_ohms)?;
        match accumulator {
            ChargeAccumulator::Charge1 | ChargeAccumulator::Charge2 => {
                let address = match accumulator {
                    ChargeAccumulator::Charge1 => RegAddressP1::C1Th,
                    ChargeAccumulator::Charge2 => RegAddressP1::C2Th,
                    ChargeAccumulator::Charge3 => unreachable!(),
                };
                let encoded: Signed48Thresholds = thresholds.quantize(lsb_coulombs)?;
                self.write_bytes(address, &encoded.to_be_bytes())
            }
            ChargeAccumulator::Charge3 => {
                let encoded: Signed64Thresholds = thresholds.quantize(lsb_coulombs)?;
                self.write_bytes(RegAddressP1::C3Th, &encoded.high_to_be_bytes())?;
                self.write_bytes(RegAddressP1::C3Tl, &encoded.low_to_be_bytes())
            }
        }
    }

    fn write_energy_thresholds(
        &mut self,
        accumulator: EnergyAccumulator,
        thresholds: ThresholdPair<f64>,
        shunt_ohms: f64,
        clock: AccumulatorClock,
    ) -> Result<(), Error<B>> {
        let (_, energy_lsb, _) = clock.lsbs().map_err(|_| Error::InvalidThreshold)?;
        let lsb_joules = Self::divide_lsb_by_shunt(energy_lsb, shunt_ohms)?;
        match accumulator {
            EnergyAccumulator::Energy1 | EnergyAccumulator::Energy2 => {
                let address = match accumulator {
                    EnergyAccumulator::Energy1 => RegAddressP1::E1Th,
                    EnergyAccumulator::Energy2 => RegAddressP1::E2Th,
                    EnergyAccumulator::Energy4 => unreachable!(),
                };
                let encoded: Signed48Thresholds = thresholds.quantize(lsb_joules)?;
                self.write_bytes(address, &encoded.to_be_bytes())
            }
            EnergyAccumulator::Energy4 => {
                let encoded: Signed64Thresholds = thresholds.quantize(lsb_joules)?;
                self.write_bytes(RegAddressP1::E4Th, &encoded.high_to_be_bytes())?;
                self.write_bytes(RegAddressP1::E4Tl, &encoded.low_to_be_bytes())
            }
        }
    }

    fn write_time_threshold(
        &mut self,
        time_base: TimeBase,
        threshold: Threshold<f64>,
        clock: AccumulatorClock,
    ) -> Result<(), Error<B>> {
        let (_, _, time_lsb) = clock.lsbs().map_err(|_| Error::InvalidThreshold)?;
        let encoded: Unsigned32Threshold = threshold.quantize(time_lsb)?;
        let address = match time_base {
            TimeBase::Time1 => RegAddressP1::Tb1Th,
            TimeBase::Time2 => RegAddressP1::Tb2Th,
            TimeBase::Time3 => RegAddressP1::Tb3Th,
            TimeBase::Time4 => RegAddressP1::Tb4Th,
        };
        self.write_bytes(address, &encoded.to_be_bytes())
    }

    fn write_current_thresholds(
        &mut self,
        channel: Channel,
        thresholds: ThresholdPair<f32>,
        shunt_ohms: f32,
    ) -> Result<(), Error<B>> {
        let lsb_amperes =
            Self::divide_lsb_by_shunt(CurrentSenseVoltage::THRESHOLD_LSB_VOLTS as f64, shunt_ohms as f64)?;
        let address = match channel {
            Channel::One => RegAddressP1::I1Th,
            Channel::Two => RegAddressP1::I2Th,
        };
        let encoded: Signed16Thresholds = thresholds.quantize(lsb_amperes)?;
        self.write_bytes(address, &encoded.to_be_bytes())
    }

    fn write_power_thresholds(
        &mut self,
        channel: Channel,
        thresholds: ThresholdPair<f32>,
        shunt_ohms: f32,
    ) -> Result<(), Error<B>> {
        let lsb_watts = Self::divide_lsb_by_shunt(
            PowerOrVoltage::POWER_THRESHOLD_LSB_VOLT_SQUARED as f64,
            shunt_ohms as f64,
        )?;
        let address = match channel {
            Channel::One => RegAddressP1::P1Th,
            Channel::Two => RegAddressP1::P2Th,
        };
        let encoded: Signed16Thresholds = thresholds.quantize(lsb_watts)?;
        self.write_bytes(address, &encoded.to_be_bytes())
    }

    fn write_power_as_voltage_thresholds(
        &mut self,
        channel: Channel,
        thresholds: ThresholdPair<f32>,
    ) -> Result<(), Error<B>> {
        let address = match channel {
            Channel::One => RegAddressP1::P1Th,
            Channel::Two => RegAddressP1::P2Th,
        };
        let encoded: Signed16Thresholds = thresholds.quantize(PowerOrVoltage::VOLTAGE_THRESHOLD_LSB_VOLTS as f64)?;
        self.write_bytes(address, &encoded.to_be_bytes())
    }

    fn write_battery_thresholds(&mut self, thresholds: ThresholdPair<f32>) -> Result<(), Error<B>> {
        let encoded: Signed16Thresholds = thresholds.quantize(BatteryVoltage::LSB_VOLTS as f64)?;
        self.write_bytes(RegAddressP1::BatTh, &encoded.to_be_bytes())
    }

    fn write_temperature_thresholds(&mut self, thresholds: ThresholdPair<f32>) -> Result<(), Error<B>> {
        let kelvin = ThresholdPair::new(
            thresholds.low + DieTemperature::ZERO_CELSIUS_KELVIN,
            thresholds.high + DieTemperature::ZERO_CELSIUS_KELVIN,
        );
        let encoded: Signed16Thresholds = kelvin.quantize(DieTemperature::LSB_KELVIN as f64)?;
        self.write_bytes(RegAddressP1::TempTh, &encoded.to_be_bytes())
    }

    fn write_vcc_thresholds(&mut self, thresholds: ThresholdPair<f32>) -> Result<(), Error<B>> {
        let encoded: Signed16Thresholds = thresholds.quantize(SupplyVoltage::LSB_VOLTS as f64)?;
        self.write_bytes(RegAddressP1::VccTh, &encoded.to_be_bytes())
    }

    fn write_slot_voltage_thresholds(&mut self, slot: Channel, thresholds: ThresholdPair<f32>) -> Result<(), Error<B>> {
        let address = match slot {
            Channel::One => RegAddressP1::Slot1Th,
            Channel::Two => RegAddressP1::Slot2Th,
        };
        let encoded: Signed16Thresholds = thresholds.quantize(SlotValue::LSB_VOLTS as f64)?;
        self.write_bytes(address, &encoded.to_be_bytes())
    }

    fn write_slot_temperature_thresholds(
        &mut self,
        slot: Channel,
        thresholds: ThresholdPair<f32>,
    ) -> Result<(), Error<B>> {
        let address = match slot {
            Channel::One => RegAddressP1::Slot1Th,
            Channel::Two => RegAddressP1::Slot2Th,
        };
        let encoded: Signed16Thresholds = thresholds.quantize(SlotValue::LSB_DEGREES_CELSIUS as f64)?;
        self.write_bytes(address, &encoded.to_be_bytes())
    }

    fn write_fast_aux_mux(&mut self, mux_n: u8, mux_p: u8) -> Result<(), Error<B>> {
        self.write_bytes(RegAddressP0::FaMuxN, &[mux_n, mux_p])
    }

    fn write_ntc_coefficients(&mut self, channel: Channel, params: &NtcConfig) -> Result<(), Error<B>> {
        let (rref_addr, abc_addr) = match channel {
            Channel::One => (RegAddressP1::Rref1, RegAddressP1::Ntc1A),
            Channel::Two => (RegAddressP1::Rref2, RegAddressP1::Ntc2A),
        };

        let rref = Float24::new(params.r_ref).encode();
        self.write_bytes(rref_addr, &rref)?;

        let mut abc = [0u8; 9];
        abc[0..3].copy_from_slice(&Float24::new(params.a).encode());
        abc[3..6].copy_from_slice(&Float24::new(params.b).encode());
        abc[6..9].copy_from_slice(&Float24::new(params.c).encode());
        self.write_bytes(abc_addr, &abc)?;

        Ok(())
    }

    fn write_shunt_tc(&mut self, channel: Channel, config: &ShuntTcConfig) -> Result<(), Error<B>> {
        let (tc_addr, tc2_addr) = match channel {
            Channel::One => (RegAddressP1::Rs1Tc, RegAddressP1::Rs1Tc2),
            Channel::Two => (RegAddressP1::Rs2Tc, RegAddressP1::Rs2Tc2),
        };

        // RSnTC (3 bytes Float24) + RSnT0 (2 bytes truncated Float24).
        let mut tc_burst = [0u8; 5];
        tc_burst[0..3].copy_from_slice(&Float24::new(config.tc).encode());
        tc_burst[3..5].copy_from_slice(&Float24::new(config.t_ref).encode_high());
        self.write_bytes(tc_addr, &tc_burst)?;

        // RSnTC2 lives elsewhere on the page.
        let tc2 = Float24::new(config.tc2).encode();
        self.write_bytes(tc2_addr, &tc2)?;

        Ok(())
    }

    fn write_slot_mux(&mut self, slot: Channel, negative: MuxInput, positive: MuxInput) -> Result<(), Error<B>> {
        let addr_n = match slot {
            Channel::One => RegAddressP0::Slot1MuxN,
            Channel::Two => RegAddressP0::Slot2MuxN,
        };
        // MUXN and MUXP are adjacent (0xEB/0xEC for SLOT1, 0xED/0xEE for SLOT2)
        // so a single 2-byte burst configures both.
        self.write_bytes(addr_n, &[negative as u8, positive as u8])
    }

    fn write_gpio_ctrl(&mut self, gpio: GpioControlRegister) -> Result<(), Self::Error> {
        self.write_bytes(RegAddressP0::FGpioCtrl, &gpio.into_bytes())
    }

    fn write_gpio5_config(&mut self, config: CurGpioControlRegister) -> Result<(), Self::Error> {
        self.write_bytes(RegAddressP0::FCurGpioCtrl, &config.into_bytes())
    }

    fn write_gpio_controls(
        &mut self,
        gpio5_and_current: CurGpioControlRegister,
        gpio1_to_4: GpioControlRegister,
    ) -> Result<(), Self::Error> {
        let bytes = [gpio5_and_current.into_bytes()[0], gpio1_to_4.into_bytes()[0]];
        self.write_bytes(RegAddressP0::FCurGpioCtrl, &bytes)
    }

    fn write_gpio4_hb(&mut self, hb: bool) -> Result<(), Self::Error> {
        let config = Gpio4HeartbeatControlRegister::new().with_gpio4_hb_enable(hb);
        self.write_bytes(RegAddressP0::Gpio4HbCtrl, &config.into_bytes())
    }

    fn write_occ_config(&mut self, config1: OverCurrentConfig, config2: OverCurrentConfig) -> Result<(), Self::Error> {
        let b =
            (config1.enable as u8) | (config1.threshold << 1) | (config1.deglitch_time << 4) | (config1.polarity << 6);
        self.write_bytes(RegAddressP0::Occ1Ctrl, &[b])?;

        let b2 =
            (config2.enable as u8) | (config2.threshold << 1) | (config2.deglitch_time << 4) | (config2.polarity << 6);
        self.write_bytes(RegAddressP0::Occ2Ctrl, &[b2])
    }

    fn read_status(&mut self) -> Result<StatusRegister, Error<B>> {
        let mut buf = [0u8; 1];
        self.read_bytes(RegAddressP0::Status, &mut buf)?;
        Ok(StatusRegister::from_bytes(buf))
    }

    fn read_vt_alerts(&mut self) -> Result<VTAlerts, Self::Error> {
        let mut buf = [0u8; 1];
        self.read_bytes(RegAddressP0::StatVT, &mut buf)?;
        Ok(VTAlerts::from_bytes(buf))
    }

    fn read_ip_alerts(&mut self) -> Result<IPAlerts, Self::Error> {
        let mut buf = [0u8; 1];
        self.read_bytes(RegAddressP0::StatIP, &mut buf)?;
        Ok(IPAlerts::from_bytes(buf))
    }

    fn read_c_alerts(&mut self) -> Result<CAlerts, Self::Error> {
        let mut buf = [0u8; 1];
        self.read_bytes(RegAddressP0::StatC, &mut buf)?;
        Ok(CAlerts::from_bytes(buf))
    }

    fn read_e_alerts(&mut self) -> Result<EAlerts, Self::Error> {
        let mut buf = [0u8; 1];
        self.read_bytes(RegAddressP0::StatE, &mut buf)?;
        Ok(EAlerts::from_bytes(buf))
    }

    fn read_ceof_alerts(&mut self) -> Result<CEOFAlerts, Self::Error> {
        let mut buf = [0u8; 1];
        self.read_bytes(RegAddressP0::StatCEOF, &mut buf)?;
        Ok(CEOFAlerts::from_bytes(buf))
    }

    fn read_tb_alerts(&mut self) -> Result<TBAlerts, Self::Error> {
        let mut buf = [0u8; 1];
        self.read_bytes(RegAddressP0::StatTB, &mut buf)?;
        Ok(TBAlerts::from_bytes(buf))
    }

    fn read_vcc_alerts(&mut self) -> Result<VCCAlerts, Self::Error> {
        let mut buf = [0u8; 1];
        self.read_bytes(RegAddressP0::StatVCC, &mut buf)?;
        Ok(VCCAlerts::from_bytes(buf))
    }

    fn read_faults(&mut self) -> Result<FaultsRegister, Error<B>> {
        let mut buf = [0u8; 1];
        self.read_bytes(RegAddressP0::Faults, &mut buf)?;
        Ok(FaultsRegister::from_bytes(buf))
    }

    fn read_extfaults(&mut self) -> Result<ExtFaultsRegister, Error<B>> {
        let mut buf = [0u8; 1];
        self.read_bytes(RegAddressP0::ExtFaults, &mut buf)?;
        Ok(ExtFaultsRegister::from_bytes(buf))
    }

    // -- Memory lock (coherent multi-register snapshots) -------------------

    fn request_memory_lock(&mut self) -> Result<u32, Error<B>> {
        // REGSCTRL is page-independent; preserve the current page bit so the lock request
        // does not also switch pages. MLK = 0b01.
        let page = self.current_page.unwrap_or(Page::Page0);
        let value = self.regsctrl_base().with_mlk(0b01);
        self.dcmd_write(RegAddressP0::RegsCtrl.addr(), &value.into_bytes())?;
        // The lock request also pins the page. Record it so that same-page reads inside the
        // lock are cache hits and do not rewrite REGSCTRL — which would clear MLK and
        // release the lock. (This is why the caller must stay on one page while locked.)
        self.current_page = Some(page);
        // The host waits the worst-case acknowledge time (tMLCK,M) before relying on the
        // snapshot. Polling REGSCTRL.MLK == 0b10 is the faster alternative but needs a
        // readback loop; the fixed wait is simpler and guaranteed.
        Ok(T_MLCK_US)
    }

    fn unlock_memory(&mut self) -> Result<(), Error<B>> {
        // MLK back to 0b00, same page preserved.
        let value = self.regsctrl_base();
        self.dcmd_write(RegAddressP0::RegsCtrl.addr(), &value.into_bytes())
    }

    fn read_current1(&mut self) -> Result<CurrentSenseVoltage, Error<B>> {
        self.read_signed_24(RegAddressP0::Current1).map(CurrentSenseVoltage::from_raw)
    }

    fn read_current2(&mut self) -> Result<CurrentSenseVoltage, Error<B>> {
        self.read_signed_24(RegAddressP0::Current2).map(CurrentSenseVoltage::from_raw)
    }

    fn read_current1_avg(&mut self) -> Result<AveragedCurrentSenseVoltage, Error<B>> {
        self.read_signed_24(RegAddressP0::Current1Avg)
            .map(AveragedCurrentSenseVoltage::from_raw)
    }

    fn read_current2_avg(&mut self) -> Result<AveragedCurrentSenseVoltage, Error<B>> {
        self.read_signed_24(RegAddressP0::Current2Avg)
            .map(AveragedCurrentSenseVoltage::from_raw)
    }

    fn read_power1(&mut self) -> Result<PowerOrVoltage, Error<B>> {
        self.read_signed_24(RegAddressP0::Power1).map(PowerOrVoltage::from_raw)
    }

    fn read_power2(&mut self) -> Result<PowerOrVoltage, Error<B>> {
        self.read_signed_24(RegAddressP0::Power2).map(PowerOrVoltage::from_raw)
    }

    fn read_bat(&mut self) -> Result<BatteryVoltage, Error<B>> {
        self.read_signed_16(RegAddressP0::Bat).map(BatteryVoltage::from_raw)
    }

    fn read_temp(&mut self) -> Result<DieTemperature, Error<B>> {
        self.read_signed_16(RegAddressP0::Temp).map(DieTemperature::from_raw)
    }

    fn read_vcc(&mut self) -> Result<SupplyVoltage, Error<B>> {
        self.read_signed_16(RegAddressP0::Vcc).map(SupplyVoltage::from_raw)
    }

    fn read_slot1(&mut self) -> Result<SlotValue, Error<B>> {
        self.read_signed_16(RegAddressP0::Slot1).map(SlotValue::from_raw)
    }

    fn read_slot2(&mut self) -> Result<SlotValue, Error<B>> {
        self.read_signed_16(RegAddressP0::Slot2).map(SlotValue::from_raw)
    }

    fn read_charge1(&mut self) -> Result<AccumulatedCharge, Error<B>> {
        self.read_signed_48(RegAddressP0::Charge1).map(AccumulatedCharge::from_raw)
    }

    fn read_charge2(&mut self) -> Result<AccumulatedCharge, Error<B>> {
        self.read_signed_48(RegAddressP0::Charge2).map(AccumulatedCharge::from_raw)
    }

    fn read_charge3(&mut self) -> Result<AccumulatedCharge, Error<B>> {
        self.read_signed_64(RegAddressP0::Charge3).map(AccumulatedCharge::from_raw)
    }

    fn read_energy1(&mut self) -> Result<AccumulatedEnergy, Error<B>> {
        self.read_signed_48(RegAddressP0::Energy1).map(AccumulatedEnergy::from_raw)
    }

    fn read_energy2(&mut self) -> Result<AccumulatedEnergy, Error<B>> {
        self.read_signed_48(RegAddressP0::Energy2).map(AccumulatedEnergy::from_raw)
    }

    fn read_energy4(&mut self) -> Result<AccumulatedEnergy, Error<B>> {
        self.read_signed_64(RegAddressP0::Energy4).map(AccumulatedEnergy::from_raw)
    }

    fn read_time1(&mut self) -> Result<AccumulatedTime, Error<B>> {
        self.read_unsigned_32(RegAddressP0::Time1).map(AccumulatedTime::from_raw)
    }

    fn read_time2(&mut self) -> Result<AccumulatedTime, Error<B>> {
        self.read_unsigned_32(RegAddressP0::Time2).map(AccumulatedTime::from_raw)
    }

    fn read_time3(&mut self) -> Result<AccumulatedTime, Error<B>> {
        self.read_unsigned_32(RegAddressP0::Time3).map(AccumulatedTime::from_raw)
    }

    fn read_time4(&mut self) -> Result<AccumulatedTime, Error<B>> {
        self.read_unsigned_32(RegAddressP0::Time4).map(AccumulatedTime::from_raw)
    }

    fn read_accumulators1(&mut self) -> Result<Accumulators, Error<B>> {
        self.read_accumulator_row(RegAddressP0::Charge1)
    }

    fn read_accumulators2(&mut self) -> Result<Accumulators, Error<B>> {
        self.read_accumulator_row(RegAddressP0::Charge2)
    }

    fn trigger_adcv_broadcast(&mut self) -> Result<(), Error<B>> {
        // ADCV broadcast: CMD0=0b00000_010, CMD1=0b01100000 = 0x0260 (datasheet Table 17).
        // For LTC2949 the exact bitmap variant (Normal mode, all cells) is don't-care; it
        // simply triggers the fast channels selected by FACTRL.
        self.send_cmd16(0x0260)
    }

    fn trigger_adax(&mut self) -> Result<(), Error<B>> {
        self.send_cmd16(0x0460)
    }
}

/// Inherent helpers: FIFO drains (generic over `N`, so off the trait) plus private
/// framing/decoding internals.
impl<B, P> LTC2949<B, P>
where
    B: SpiDevice<u8>,
    P: PollMethod<B>,
{
    fn encode_gain_factor(correction_factor: f32) -> Result<[u8; 3], Error<B>> {
        if !correction_factor.is_finite() || correction_factor <= 0.0 {
            return Err(Error::InvalidGainCorrection);
        }
        Ok(Float24::new(correction_factor).encode())
    }

    fn write_gain_factor(&mut self, address: RegAddressP1, correction_factor: f32) -> Result<(), Error<B>> {
        let encoded = Self::encode_gain_factor(correction_factor)?;
        self.write_bytes(address, &encoded)
    }

    fn divide_lsb_by_shunt(lsb: f64, shunt_ohms: f64) -> Result<f64, Error<B>> {
        if !shunt_ohms.is_finite() || shunt_ohms <= 0.0 {
            return Err(Error::InvalidThreshold);
        }
        Ok(lsb / shunt_ohms)
    }

    /// Drains up to `N` I1 FIFO samples (3 bytes each: MSB, LSB, TAG). Stops at the first
    /// non-`Ok` sample, which is included as the terminator so the caller can read its `tag`.
    pub fn read_fifo_i1<const N: usize>(&mut self) -> Result<Vec<FifoSample, N>, Error<B>> {
        self.read_fifo::<N>(RegAddressP0::FifoI1)
    }

    /// Drains up to `N` samples from the I2 FIFO.
    pub fn read_fifo_i2<const N: usize>(&mut self) -> Result<Vec<FifoSample, N>, Error<B>> {
        self.read_fifo::<N>(RegAddressP0::FifoI2)
    }

    /// Drains up to `N` samples from the BAT (P1/P2 voltage-mode) FIFO.
    pub fn read_fifo_bat<const N: usize>(&mut self) -> Result<Vec<FifoSample, N>, Error<B>> {
        self.read_fifo::<N>(RegAddressP0::FifoBat)
    }

    /// Drains up to `N` samples from the AUX FIFO.
    pub fn read_fifo_aux<const N: usize>(&mut self) -> Result<Vec<FifoSample, N>, Error<B>> {
        self.read_fifo::<N>(RegAddressP0::FifoAux)
    }

    fn read_fifo<const N: usize>(&mut self, reg: RegAddressP0) -> Result<Vec<FifoSample, N>, Error<B>> {
        // The FIFO register is non-incrementing: each 3-byte group within a burst pops the
        // next sample (datasheet "Reading the FIFOs"), so one DCMD read of 3·k bytes yields
        // k samples. Drain in bursts and stop at the first non-`Ok` tag (kept as terminator).
        let mut samples: Vec<FifoSample, N> = Vec::new();
        let mut buf = [0u8; FIFO_SAMPLES_PER_BURST * 3];
        let mut remaining = N;
        while remaining > 0 {
            let batch = remaining.min(FIFO_SAMPLES_PER_BURST);
            let bytes = &mut buf[..batch * 3];
            self.read_bytes(reg, bytes)?;
            for sample in bytes.as_chunks::<3>().0 {
                let raw = ((sample[0] as u16) << 8 | sample[1] as u16) as i16;
                let tag = FifoTag::from_byte(sample[2]);
                let stop = !matches!(tag, FifoTag::Ok);
                let _ = samples.push(FifoSample { raw, tag });
                if stop {
                    return Ok(samples);
                }
            }
            remaining -= batch;
        }
        Ok(samples)
    }

    fn read_signed_16(&mut self, reg: RegAddressP0) -> Result<i16, Error<B>> {
        let mut buf = [0u8; 2];
        self.read_bytes(reg, &mut buf)?;
        Ok(i16::from_be_bytes(buf))
    }

    fn read_signed_24(&mut self, reg: RegAddressP0) -> Result<i32, Error<B>> {
        let mut buf = [0u8; 3];
        self.read_bytes(reg, &mut buf)?;
        // Sign-extend 24 -> 32.
        let raw = (u32::from(buf[0]) << 16) | (u32::from(buf[1]) << 8) | u32::from(buf[2]);
        let extended = if raw & 0x0080_0000 != 0 { raw | 0xFF00_0000 } else { raw };
        Ok(extended as i32)
    }

    fn read_unsigned_32(&mut self, reg: RegAddressP0) -> Result<u32, Error<B>> {
        let mut buf = [0u8; 4];
        self.read_bytes(reg, &mut buf)?;
        Ok(u32::from_be_bytes(buf))
    }

    fn read_signed_48(&mut self, reg: RegAddressP0) -> Result<i64, Error<B>> {
        let mut buf = [0u8; 6];
        self.read_bytes(reg, &mut buf)?;
        Ok(Self::sign_extend_48(&buf))
    }

    /// Reads a full 16-byte accumulator row (charge, energy, time) in one coherent burst,
    /// starting at the row's charge address (`0x00` for channel 1, `0x10` for channel 2).
    fn read_accumulator_row(&mut self, reg: RegAddressP0) -> Result<Accumulators, Error<B>> {
        let mut buf = [0u8; 16];
        self.read_bytes(reg, &mut buf)?;
        Ok(Accumulators {
            charge: AccumulatedCharge::from_raw(Self::sign_extend_48(&buf[0..6])),
            energy: AccumulatedEnergy::from_raw(Self::sign_extend_48(&buf[6..12])),
            time: AccumulatedTime::from_raw(u32::from_be_bytes([buf[12], buf[13], buf[14], buf[15]])),
        })
    }

    fn read_signed_64(&mut self, reg: RegAddressP0) -> Result<i64, Error<B>> {
        let mut buf = [0u8; 8];
        self.read_bytes(reg, &mut buf)?;
        Ok(i64::from_be_bytes(buf))
    }

    /// REGSCTRL for the current page with RDCVCONF=1, BCREN=0, MLK=00. Lets the memory-lock
    /// helpers rewrite REGSCTRL without changing the selected page.
    fn regsctrl_base(&self) -> RegControlRegister {
        let page1 = matches!(self.current_page.unwrap_or(Page::Page0), Page::Page1);
        RegControlRegister::default().with_rdcvconf(true).with_page(page1)
    }

    fn select_page(&mut self, page: Page) -> Result<(), Error<B>> {
        if self.current_page == Some(page) {
            return Ok(());
        }
        let value = RegControlRegister::default()
            .with_rdcvconf(true)
            .with_page(matches!(page, Page::Page1));
        self.dcmd_write(RegAddressP0::RegsCtrl.addr(), &value.into_bytes())?;
        self.current_page = Some(page);
        Ok(())
    }

    /// Reads `buf.len()` bytes from `reg` via a direct `DCMD` read (no shift-register
    /// prefix in the parallel topology), selecting the page first if needed.
    fn read_bytes<R: Register>(&mut self, reg: R, buf: &mut [u8]) -> Result<(), Error<B>> {
        self.select_page(R::PAGE)?;
        self.dcmd_read(reg.addr(), buf)
    }

    /// Writes `data` to `reg` via `DCMD`. The cell monitors ignore command 0xFE.
    fn write_bytes<R: Register>(&mut self, reg: R, data: &[u8]) -> Result<(), Error<B>> {
        // REGSCTRL writes are themselves the page-switch mechanism; avoid recursion.
        if reg.addr() != RegAddressP0::RegsCtrl.addr() {
            self.select_page(R::PAGE)?;
        }
        self.dcmd_write(reg.addr(), data)
    }

    /// Sends a DCMD write transaction. `data` must fit one PEC group (≤ 16 bytes); every
    /// caller already stays within that (the 9-byte NTC burst is the largest).
    /// DCMD frame layout (datasheet Table 11):
    ///
    ///   [0xFE, RADDR, PEC0, PEC1, ID, D0..D(N-1), PEC0, PEC1, D(N)..D(2N-1), PEC0, PEC1, ...]
    ///
    /// The PEC on bytes 2..4 is computed over [0xFE, RADDR]. Each subsequent PEC covers the
    /// preceding `N` data bytes. `N` is encoded in the ID byte's PECC field
    /// (`PECC = N-1`, range 0..=15). The ID byte itself carries redundancy so it is not
    /// covered by a PEC.
    fn dcmd_write(&mut self, addr: u8, data: &[u8]) -> Result<(), Error<B>> {
        // Frame: 4 (header+PEC) + 1 (ID) + ≤16 (data) + 2 (PEC) = 23 bytes max.
        assert!(
            (1..=N_PER_PEC).contains(&data.len()),
            "DCMD write requires 1 to 16 data bytes"
        );

        let mut frame = [0u8; 23];
        frame[0] = 0xFE;
        frame[1] = addr;
        let header_pec = PEC15::calc(&frame[0..2]);
        frame[2] = header_pec[0];
        frame[3] = header_pec[1];
        let n = data.len();
        frame[4] = DcmdId::write((n - 1) as u8).into();
        frame[5..5 + n].copy_from_slice(data);
        let data_pec = PEC15::calc(&frame[5..5 + n]);
        frame[5 + n] = data_pec[0];
        frame[6 + n] = data_pec[1];

        let total = 5 + n + 2;
        self.bus.write(&frame[..total]).map_err(Error::BusError)?;
        self.poll_method.end_sync_command(&mut self.bus).map_err(Error::BusError)?;
        Ok(())
    }

    // -- DCMD direct read -------------------------------------------------

    /// Direct `DCMD` read: data appears on MISO after the 5-byte header, then the PEC.
    /// `buf` must fit one PEC group (≤ 16 bytes); the 16-byte accumulator row is the largest.
    fn dcmd_read(&mut self, addr: u8, buf: &mut [u8]) -> Result<(), Error<B>> {
        assert!(
            (1..=N_PER_PEC).contains(&buf.len()),
            "DCMD read requires 1 to 16 data bytes"
        );

        let n = buf.len();
        // MOSI: header (4) + ID (1) + n dummy data bytes + 2 dummy PEC bytes.
        // MISO: 5 don't-care (command echo region) + n data + 2 PEC.
        let mut mosi = [0xFFu8; 23];
        let mut miso = [0x00u8; 23];

        mosi[0] = 0xFE;
        mosi[1] = addr;
        let header_pec = PEC15::calc(&mosi[0..2]);
        mosi[2] = header_pec[0];
        mosi[3] = header_pec[1];
        mosi[4] = DcmdId::read((n - 1) as u8).into();
        // bytes 5..5+n and the trailing PEC bytes stay 0xFF (don't-care on MOSI).

        let total = 5 + n + 2;
        self.bus
            .transaction(&mut [Operation::Transfer(&mut miso[..total], &mosi[..total])])
            .map_err(Error::BusError)?;

        buf.copy_from_slice(&miso[5..5 + n]);

        let pec = PEC15::calc(&miso[5..5 + n]);
        if pec[0] != miso[5 + n] || pec[1] != miso[6 + n] {
            // A bad PEC can mean the device reset (e.g. unexpected SLEEP/POR), in which
            // case its page selection is back to PAGE0. Drop the cache so the next access
            // re-issues REGSCTRL rather than trusting a possibly-stale page.
            self.current_page = None;
            return Err(Error::ChecksumMismatch);
        }

        self.poll_method.end_sync_command(&mut self.bus).map_err(Error::BusError)?;
        Ok(())
    }

    fn send_cmd16(&mut self, cmd: u16) -> Result<(), Error<B>> {
        let frame = Self::build_cmd16(cmd);
        self.bus.write(&frame).map_err(Error::BusError)?;
        self.poll_method.end_sync_command(&mut self.bus).map_err(Error::BusError)?;
        Ok(())
    }

    /// Sign-extends a big-endian 48-bit two's-complement value (`bytes[0..6]`) to `i64`.
    fn sign_extend_48(bytes: &[u8]) -> i64 {
        let mut raw: u64 = 0;
        for &b in &bytes[..6] {
            raw = (raw << 8) | u64::from(b);
        }
        if raw & 0x0000_8000_0000_0000 != 0 {
            raw |= 0xFFFF_0000_0000_0000;
        }
        raw as i64
    }

    /// Builds a 4-byte LTC681X-style 16-bit command (CMD0, CMD1, PEC0, PEC1).
    fn build_cmd16(cmd: u16) -> [u8; 4] {
        let mut frame = [(cmd >> 8) as u8, cmd as u8, 0, 0];
        let pec = PEC15::calc(&frame[..2]);
        frame[2] = pec[0];
        frame[3] = pec[1];
        frame
    }
}