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
//! I2C (Inter-Integrated Circuit) HAL for RP2350
//!
//! This module provides a safe interface for the RP2350 I2C peripherals.
//! The RP2350 has two I2C controllers (I2C0 and I2C1).
//!
//! # Features
//!
//! - Master and slave modes
//! - Standard (100 kHz), Fast (400 kHz), and Fast Mode Plus (1 MHz)
//! - 7-bit and 10-bit addressing
//! - 16-byte TX and RX FIFOs
//! - **Type-safe pin ownership**: Pins are consumed by I2C, preventing misuse
//!
//! # GPIO Mapping
//!
//! I2C0:
//! - SDA: GPIO 0, 4, 8, 12, 16, 20, 24, 28
//! - SCL: GPIO 1, 5, 9, 13, 17, 21, 25, 29
//!
//! I2C1:
//! - SDA: GPIO 2, 6, 10, 14, 18, 22, 26
//! - SCL: GPIO 3, 7, 11, 15, 19, 23, 27
//!
//! # Example (Master Mode with Pin Ownership)
//!
//! ```ignore
//! use rp2350::gpio::Pins;
//! use rp2350::i2c::I2c0;
//! use rp2350::Peripherals;
//!
//! let dp = Peripherals::take().unwrap();
//! let pins = Pins::init(dp.IO_BANK0, dp.PADS_BANK0);
//!
//! // Initialize I2C0 at 400 kHz, consuming pins p4 (SDA) and p5 (SCL)
//! let i2c = I2c0::init(
//! dp.I2C0,
//! pins.pin::<4>(), // SDA pin - ownership transferred
//! pins.pin::<5>(), // SCL pin - ownership transferred
//! 400_000, // 400 kHz
//! &clocks,
//! );
//!
//! // Write to a device
//! i2c.write(0x50, &[0x00, 0x01, 0x02]).unwrap();
//!
//! // Read from a device
//! let mut buf = [0u8; 4];
//! i2c.read(0x50, &mut buf).unwrap();
//!
//! // Compile error: pins.pin::<4>() already moved!
//! // pins.pin::<4>().into_output();
//! ```
//!
//! # Example (Slave Mode)
//!
//! ```ignore
//! use rp2350::gpio::Pins;
//! use rp2350::i2c::I2c0Slave;
//! use rp2350::Peripherals;
//!
//! let dp = Peripherals::take().unwrap();
//! let pins = Pins::init(dp.IO_BANK0, dp.PADS_BANK0);
//!
//! // Initialize I2C0 as slave with address 0x55
//! let i2c = I2c0Slave::init(
//! dp.I2C0,
//! pins.pin::<4>(),
//! pins.pin::<5>(),
//! 0x55,
//! 150_000_000,
//! );
//!
//! // Wait for and read data from master
//! let mut buf = [0u8; 4];
//! let count = i2c.read(&mut buf).unwrap();
//!
//! // Write data for master to read
//! i2c.write(&[0x01, 0x02, 0x03]).unwrap();
//! ```
use pastey::paste;
use crate::clocks::Clocks;
use crate::gpio::{I2c0SclPin, I2c0SdaPin, I2c1SclPin, I2c1SdaPin};
use crate::reset::Resettable;
/// I2C speed mode
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u32)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Speed {
/// Standard mode (100 kHz)
Standard = 1,
/// Fast mode (400 kHz) and Fast mode plus (1 MHz)
/// Both use the same IC_CON speed setting, frequency is determined by clock timing
Fast = 2,
}
/// I2C error types
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
/// Arbitration lost
ArbitrationLoss,
/// No acknowledgement received
NoAcknowledge,
/// TX FIFO overflow
TxOverflow,
/// RX FIFO overflow
RxOverflow,
/// RX FIFO underflow
RxUnderflow,
/// Abort condition
Abort,
/// Timeout
Timeout,
}
/// I2C mode
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Mode {
/// Master mode
Master,
/// Slave mode
Slave,
}
// ============================================================================
// Macro for generating I2C master implementations
// ============================================================================
macro_rules! impl_i2c {
($I2cName:ident, $i2c_num:literal, $I2cType:ty, $i2c_mod:ident, $SdaPin:ident, $SclPin:ident) => {
paste! {
/// I2C master peripheral wrapper with type-safe pin ownership.
///
/// This struct owns both the I2C peripheral and the GPIO pins used for
/// SDA and SCL. The pins are configured automatically when the I2C
/// instance is created, and their ownership prevents them from being
/// used for other purposes.
///
#[doc = concat!("# I2C", $i2c_num, " Master Pin Mapping")]
///
#[doc = concat!("See [`", stringify!($SdaPin), "`] for valid SDA pins.")]
///
#[doc = concat!("See [`", stringify!($SclPin), "`] for valid SCL pins.")]
///
/// For slave mode, use
#[doc = concat!("[`", stringify!($I2cName), "Slave`].")]
pub struct $I2cName<SDA, SCL>
where
SDA: $SdaPin,
SCL: $SclPin,
{
i2c: $I2cType,
_sda: SDA,
_scl: SCL,
}
impl<SDA, SCL> $I2cName<SDA, SCL>
where
SDA: $SdaPin,
SCL: $SclPin,
{
/// I2C peripheral number
pub const NUM: u8 = $i2c_num;
// ================================================================
// Initialization (Master Mode)
// ================================================================
/// Initialize I2C in master mode.
///
/// This method consumes the I2C peripheral and GPIO pins, configuring
/// the pins for I2C function automatically (including pull-up resistors).
///
/// # Arguments
///
/// * `i2c` - The I2C peripheral from PAC
/// * `sda` - SDA pin (must implement the appropriate SDA trait)
/// * `scl` - SCL pin (must implement the appropriate SCL trait)
/// * `baudrate` - Desired baudrate in Hz (e.g., 100_000 for 100 kHz)
/// * `clocks` - System clocks
///
/// # Type Safety
///
/// Only pins that implement the correct trait can be used:
#[doc = concat!("- SDA must implement [`", stringify!($SdaPin), "`]")]
#[doc = concat!("- SCL must implement [`", stringify!($SclPin), "`]")]
///
/// # Example
///
/// ```ignore
#[doc = concat!("let i2c = ", stringify!($I2cName), "::init(")]
#[doc = concat!(" dp.I2C", $i2c_num, ",")]
/// pins.pin::<4>(), // SDA
/// pins.pin::<5>(), // SCL
/// 400_000, // 400 kHz
/// &clocks,
/// );
/// ```
pub fn init(i2c: $I2cType, mut sda: SDA, mut scl: SCL, baudrate: u32, clocks: &Clocks) -> Self {
// Configure pins for I2C function (includes pull-up)
sda.[<configure_i2c $i2c_num _sda>]();
scl.[<configure_i2c $i2c_num _scl>]();
// Reset peripheral to ensure clean register state
<$I2cType as Resettable>::init();
let mut i2c_dev = Self {
i2c,
_sda: sda,
_scl: scl,
};
// Disable I2C to configure
i2c_dev.i2c.ic_enable.clr_enable();
// Disable all interrupts and clear pending
i2c_dev.i2c.ic_intr_mask.write_bits(0);
let _ = i2c_dev.i2c.ic_clr_intr.read_bits();
// Configure as master: clear all, then set required bits
i2c_dev.i2c.ic_con.write_bits(0);
i2c_dev.i2c.ic_con.set_master_mode();
i2c_dev.i2c.ic_con.set_ic_restart_en();
i2c_dev.i2c.ic_con.set_ic_slave_disable();
i2c_dev.i2c.ic_con.set_tx_empty_ctrl();
// Set baudrate (also configures speed mode in IC_CON)
i2c_dev.configure_baudrate(baudrate, clocks.sys_freq());
// Set FIFO thresholds
i2c_dev.i2c.ic_tx_tl.write_tx_tl(0);
i2c_dev.i2c.ic_rx_tl.write_rx_tl(0);
// Enable I2C
i2c_dev.i2c.ic_enable.set_enable();
i2c_dev
}
/// Obtain a pin-erased I2C handle without ownership.
///
/// Returns `$I2cName<ErasedPin, ErasedPin>` — a handle that can access
/// all I2C registers without carrying pin type parameters.
/// This is useful for ISR context or global storage where the
/// concrete pin types are inconvenient.
///
/// # Safety
///
/// The caller must ensure:
/// 1. The I2C peripheral has already been initialised (pins
/// configured, baud rate set, peripheral taken out of reset).
/// 2. No data races: the returned handle must not be used
/// concurrently with the owning instance without proper
/// synchronisation (e.g. `Mutex`).
///
/// # Example
///
/// ```ignore
#[doc = concat!("// In init code: create the full I2C with pins")]
#[doc = concat!("let i2c = ", stringify!($I2cName), "::init(")]
#[doc = concat!(" dp.I2C", $i2c_num, ", sda_pin, scl_pin, 400_000, &clocks,")]
/// );
///
/// // Later, in ISR or another context:
#[doc = concat!("let i2c = unsafe { ", stringify!($I2cName), "::<ErasedPin, ErasedPin>::steal() };")]
/// let mut buf = [0u8; 2];
/// i2c.read(0x50, &mut buf).unwrap();
/// ```
#[inline]
pub unsafe fn steal() -> crate::Steal<$I2cName<crate::gpio::pins::ErasedPin, crate::gpio::pins::ErasedPin>> {
crate::Steal($I2cName {
i2c: unsafe { <$I2cType>::steal() },
_sda: crate::gpio::pins::ErasedPin,
_scl: crate::gpio::pins::ErasedPin,
})
}
/// Get reference to the underlying register block.
///
/// # Safety
/// Direct register access can bypass the HAL's safety guarantees.
/// The caller must ensure operations do not conflict with the HAL state.
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::$i2c_mod::RegisterBlock {
&self.i2c
}
/// Release the I2C peripheral and pins.
///
/// Returns a tuple of (I2C peripheral, SDA pin, SCL pin).
/// The pins can then be reconfigured for other uses.
#[inline]
pub fn release(self) -> ($I2cType, SDA, SCL) {
(self.i2c, self._sda, self._scl)
}
// ================================================================
// Configuration
// ================================================================
/// Configure the I2C baudrate (internal helper)
fn configure_baudrate(&mut self, baudrate: u32, sys_clk: u32) {
// Determine speed mode
let speed = if baudrate >= 400_000 {
Speed::Fast
} else {
Speed::Standard
};
// Update speed in IC_CON
self.i2c.ic_con.write_speed(speed as u32);
// Calculate clock counts
let period = sys_clk / baudrate;
let (lcnt, hcnt) = match speed {
Speed::Standard => {
let hcnt = period * 4 / 10;
let lcnt = period - hcnt;
(lcnt, hcnt)
}
Speed::Fast => {
let hcnt = period * 3 / 10;
let lcnt = period - hcnt;
(lcnt, hcnt)
}
};
// Apply minimum values and subtract IC internal delays
let hcnt = hcnt.saturating_sub(8).max(6);
let lcnt = lcnt.saturating_sub(1).max(8);
// Write SCL timing registers
match speed {
Speed::Standard => {
self.i2c.ic_ss_scl_hcnt.write_ic_ss_scl_hcnt(hcnt);
self.i2c.ic_ss_scl_lcnt.write_ic_ss_scl_lcnt(lcnt);
}
Speed::Fast => {
self.i2c.ic_fs_scl_hcnt.write_ic_fs_scl_hcnt(hcnt);
self.i2c.ic_fs_scl_lcnt.write_ic_fs_scl_lcnt(lcnt);
}
}
// Set spike suppression
let spklen = if baudrate >= 1_000_000 { 1 } else { 7 };
self.i2c.ic_fs_spklen.write_ic_fs_spklen(spklen);
// Set SDA hold time
let sda_hold = (sys_clk / 1_000_000 / 3).max(1);
self.i2c.ic_sda_hold.write_ic_sda_tx_hold(sda_hold);
}
/// Set the I2C baudrate (master mode only).
pub fn set_baudrate(&mut self, baudrate: u32, sys_clk: u32) {
self.i2c.ic_enable.clr_enable();
self.configure_baudrate(baudrate, sys_clk);
self.i2c.ic_enable.set_enable();
}
// ================================================================
// Control
// ================================================================
/// Enable the I2C peripheral.
#[inline]
pub fn enable(&self) {
self.i2c.ic_enable.set_enable();
}
/// Disable the I2C peripheral.
#[inline]
pub fn disable(&self) {
self.i2c.ic_enable.clr_enable();
}
/// Check if I2C peripheral is enabled.
#[inline]
pub fn is_enabled(&self) -> bool {
self.i2c.ic_enable.enable()
}
/// Set the target address for master mode transactions.
///
/// This is a no-op when the requested address (and 10-bit flag)
/// already matches the hardware, so repeated calls from
/// consecutive transactions don't pay the disable → configure →
/// enable cost on every bus access.
#[inline]
pub fn set_target_addr(&self, addr: u16, addr_10bit: bool) {
if self.i2c.ic_tar.ic_tar() == addr as u32
&& self.i2c.ic_con.ic_10bitaddr_master() == addr_10bit
{
return;
}
// Disable to change address
self.i2c.ic_enable.clr_enable();
// Update 10-bit addressing in IC_CON
if addr_10bit {
self.i2c.ic_con.set_ic_10bitaddr_master();
} else {
self.i2c.ic_con.clr_ic_10bitaddr_master();
}
// Set target address
self.i2c.ic_tar.write_ic_tar(addr as u32);
// Re-enable
self.i2c.ic_enable.set_enable();
}
// ================================================================
// Master Mode Operations
// ================================================================
/// Write data to an I2C device (master mode).
///
/// Writing an empty slice performs an **address probe**: it issues
/// START + address + STOP and reports whether a device acknowledged
/// its address (see [`Error::NoAcknowledge`]).
///
/// # Arguments
/// * `addr` - 7-bit I2C address
/// * `data` - Data bytes to write
pub fn write(&self, addr: u8, data: &[u8]) -> Result<(), Error> {
use rp2350_pac::$i2c_mod::IcDataCmd;
self.set_target_addr(addr as u16, false);
if data.is_empty() {
// Address probe: send a single zero byte with STOP. If the
// target NACKs its address, the wait below reports it via
// `check_abort`.
self.wait_cond(|| self.i2c.ic_status.tfnf())?;
self.i2c.ic_data_cmd.write_bits(IcDataCmd::STOP);
} else {
for (i, &byte) in data.iter().enumerate() {
let last = i == data.len() - 1;
// Wait for TX FIFO not full (bounded)
self.wait_cond(|| self.i2c.ic_status.tfnf())?;
// Write data command
let cmd = (byte as u32) | if last { IcDataCmd::STOP } else { 0 };
self.i2c.ic_data_cmd.write_bits(cmd);
}
}
// Wait for TX FIFO to empty (bounded)
self.wait_cond(|| self.i2c.ic_status.tfe())?;
// Wait for not busy (bounded)
self.wait_cond(|| !self.i2c.ic_status.mst_activity())?;
// Both waits above return immediately when their condition
// already holds, so an abort (e.g. an address NACK) that
// ended the transfer would go unnoticed. Check the abort
// status explicitly before reporting success.
self.check_abort()?;
Ok(())
}
/// Read data from an I2C device (master mode).
///
/// # Arguments
/// * `addr` - 7-bit I2C address
/// * `buffer` - Buffer to store received data
pub fn read(&self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> {
use rp2350_pac::$i2c_mod::IcDataCmd;
if buffer.is_empty() {
return Ok(());
}
self.set_target_addr(addr as u16, false);
let mut tx_idx = 0;
let mut rx_idx = 0;
let len = buffer.len();
while rx_idx < len {
// Send read commands while TX FIFO has space
while tx_idx < len && self.i2c.ic_status.tfnf() {
let last = tx_idx == len - 1;
let cmd = IcDataCmd::CMD | if last { IcDataCmd::STOP } else { 0 };
self.i2c.ic_data_cmd.write_bits(cmd);
tx_idx += 1;
}
// Read from RX FIFO
while rx_idx < len && self.i2c.ic_status.rfne() {
buffer[rx_idx] = self.i2c.ic_data_cmd.dat() as u8;
rx_idx += 1;
}
// Wait (bounded) for RX data or TX FIFO space so the
// loop can make progress.
self.wait_cond(|| {
self.i2c.ic_status.rfne()
|| (tx_idx < len && self.i2c.ic_status.tfnf())
})?;
}
// Wait for the STOP to finish and the bus to idle, so a
// back-to-back transaction cannot race it.
self.wait_cond(|| !self.i2c.ic_status.mst_activity())?;
// A NACK on the address byte or on the last received byte
// is only reported through the abort status.
self.check_abort()?;
Ok(())
}
/// Write then read (combined transaction with repeated start).
///
/// # Arguments
/// * `addr` - 7-bit I2C address
/// * `write_data` - Data to write
/// * `read_buffer` - Buffer for data to read
pub fn write_read(
&self,
addr: u8,
write_data: &[u8],
read_buffer: &mut [u8],
) -> Result<(), Error> {
use rp2350_pac::$i2c_mod::IcDataCmd;
if write_data.is_empty() && read_buffer.is_empty() {
return Ok(());
}
// A write with no read phase is just a plain write; delegate
// so the transaction is terminated with a STOP.
if read_buffer.is_empty() {
return self.write(addr, write_data);
}
self.set_target_addr(addr as u16, false);
// Write phase (no STOP — a repeated START follows)
for &byte in write_data {
self.wait_cond(|| self.i2c.ic_status.tfnf())?;
self.i2c.ic_data_cmd.write_dat(byte as u32);
}
// Read phase
let mut tx_idx = 0;
let mut rx_idx = 0;
let len = read_buffer.len();
while rx_idx < len {
// Send read commands
while tx_idx < len && self.i2c.ic_status.tfnf() {
let last = tx_idx == len - 1;
let cmd = IcDataCmd::CMD | if last { IcDataCmd::STOP } else { 0 };
self.i2c.ic_data_cmd.write_bits(cmd);
tx_idx += 1;
}
// Read from RX FIFO
while rx_idx < len && self.i2c.ic_status.rfne() {
read_buffer[rx_idx] = self.i2c.ic_data_cmd.dat() as u8;
rx_idx += 1;
}
// Wait (bounded) for RX data or TX FIFO space so the
// loop can make progress.
self.wait_cond(|| {
self.i2c.ic_status.rfne()
|| (tx_idx < len && self.i2c.ic_status.tfnf())
})?;
}
// Wait for the STOP to finish and the bus to idle.
self.wait_cond(|| !self.i2c.ic_status.mst_activity())?;
// Both waits above can succeed without ever evaluating a
// condition, so check the abort status explicitly.
self.check_abort()?;
Ok(())
}
// ================================================================
// Status and Errors
// ================================================================
/// Check for abort conditions.
fn check_abort(&self) -> Result<(), Error> {
if self.i2c.ic_raw_intr_stat.tx_abrt() {
let source = self.i2c.ic_tx_abrt_source.read_bits();
// Clear abort flag
let _ = self.i2c.ic_clr_tx_abrt.read_bits();
if source & (1 << 0) != 0 {
return Err(Error::NoAcknowledge);
}
if source & (1 << 12) != 0 {
return Err(Error::ArbitrationLoss);
}
return Err(Error::Abort);
}
Ok(())
}
/// Spin (bounded) until `cond` is true, checking for I2C abort
/// each iteration. Returns [`Error::Timeout`] if the hardware
/// does not make progress within
/// [`crate::spin::SPIN_TIMEOUT`] iterations.
fn wait_cond(&self, cond: impl Fn() -> bool) -> Result<(), Error> {
let mut spins = 0u32;
while !cond() {
self.check_abort()?;
core::hint::spin_loop();
spins += 1;
if spins >= crate::spin::SPIN_TIMEOUT {
return Err(Error::Timeout);
}
}
Ok(())
}
/// Get the TX FIFO level.
#[inline]
pub fn tx_fifo_level(&self) -> u8 {
self.i2c.ic_txflr.txflr() as u8
}
/// Get the RX FIFO level.
#[inline]
pub fn rx_fifo_level(&self) -> u8 {
self.i2c.ic_rxflr.rxflr() as u8
}
/// Check if TX FIFO is empty.
#[inline]
pub fn is_tx_empty(&self) -> bool {
self.i2c.ic_status.tfe()
}
/// Check if RX FIFO is not empty.
#[inline]
pub fn is_rx_not_empty(&self) -> bool {
self.i2c.ic_status.rfne()
}
/// Check if this master is actively transferring data.
#[inline]
pub fn is_busy(&self) -> bool {
self.i2c.ic_status.mst_activity()
}
/// Get the current baudrate by reading hardware registers.
///
/// # Arguments
/// * `sys_clk` - System clock frequency in Hz
#[inline]
pub fn baudrate(&self, sys_clk: u32) -> u32 {
let speed = self.i2c.ic_con.speed();
let (hcnt, lcnt) = if speed >= Speed::Fast as u32 {
(
self.i2c.ic_fs_scl_hcnt.ic_fs_scl_hcnt(),
self.i2c.ic_fs_scl_lcnt.ic_fs_scl_lcnt(),
)
} else {
(
self.i2c.ic_ss_scl_hcnt.ic_ss_scl_hcnt(),
self.i2c.ic_ss_scl_lcnt.ic_ss_scl_lcnt(),
)
};
let total = hcnt + 8 + lcnt + 1;
if total == 0 {
return 0;
}
sys_clk / total
}
/// Get the current mode by reading the IC_CON register.
#[inline]
pub fn mode(&self) -> Mode {
if self.i2c.ic_con.master_mode() {
Mode::Master
} else {
Mode::Slave
}
}
// ================================================================
// Interrupts
// ================================================================
// ---- Mask operations (enable/disable) ----
/// Enable specific interrupts using mask.
pub fn enable_interrupts(&self, mask: u32) {
self.i2c.ic_intr_mask.set_bits(mask);
}
/// Disable specific interrupts using mask.
pub fn disable_interrupts(&self, mask: u32) {
self.i2c.ic_intr_mask.clr_bits(mask);
}
/// Disable all interrupts.
#[inline]
pub fn disable_all_interrupts(&self) {
self.i2c.ic_intr_mask.write_bits(0);
}
// ---- RX FULL interrupt ----
/// Enable RX FULL interrupt (RX FIFO reaches or exceeds threshold).
#[inline]
pub fn enable_rx_full_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_rx_full();
}
/// Disable RX FULL interrupt.
#[inline]
pub fn disable_rx_full_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_rx_full();
}
/// Check if RX FULL interrupt is enabled.
#[inline]
pub fn is_rx_full_interrupt_enabled(&self) -> bool {
self.i2c.ic_intr_mask.m_rx_full()
}
// ---- TX EMPTY interrupt ----
/// Enable TX EMPTY interrupt (TX FIFO at or below threshold).
#[inline]
pub fn enable_tx_empty_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_tx_empty();
}
/// Disable TX EMPTY interrupt.
#[inline]
pub fn disable_tx_empty_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_tx_empty();
}
/// Check if TX EMPTY interrupt is enabled.
#[inline]
pub fn is_tx_empty_interrupt_enabled(&self) -> bool {
self.i2c.ic_intr_mask.m_tx_empty()
}
// ---- TX ABRT interrupt ----
/// Enable TX abort interrupt.
#[inline]
pub fn enable_tx_abort_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_tx_abrt();
}
/// Disable TX abort interrupt.
#[inline]
pub fn disable_tx_abort_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_tx_abrt();
}
// ---- STOP DET interrupt ----
/// Enable stop detected interrupt.
#[inline]
pub fn enable_stop_det_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_stop_det();
}
/// Disable stop detected interrupt.
#[inline]
pub fn disable_stop_det_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_stop_det();
}
// ---- START DET interrupt ----
/// Enable start detected interrupt.
#[inline]
pub fn enable_start_det_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_start_det();
}
/// Disable start detected interrupt.
#[inline]
pub fn disable_start_det_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_start_det();
}
// ---- ACTIVITY interrupt ----
/// Enable activity interrupt.
#[inline]
pub fn enable_activity_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_activity();
}
/// Disable activity interrupt.
#[inline]
pub fn disable_activity_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_activity();
}
// ---- Error interrupts ----
/// Enable RX overflow interrupt.
#[inline]
pub fn enable_rx_over_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_rx_over();
}
/// Disable RX overflow interrupt.
#[inline]
pub fn disable_rx_over_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_rx_over();
}
/// Enable RX underflow interrupt.
#[inline]
pub fn enable_rx_under_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_rx_under();
}
/// Disable RX underflow interrupt.
#[inline]
pub fn disable_rx_under_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_rx_under();
}
/// Enable TX overflow interrupt.
#[inline]
pub fn enable_tx_over_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_tx_over();
}
/// Disable TX overflow interrupt.
#[inline]
pub fn disable_tx_over_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_tx_over();
}
// ---- Status reading ----
/// Get raw interrupt status.
#[inline]
pub fn raw_interrupt_status(&self) -> u32 {
self.i2c.ic_raw_intr_stat.read_bits()
}
/// Get masked interrupt status.
#[inline]
pub fn interrupt_status(&self) -> u32 {
self.i2c.ic_intr_stat.read_bits()
}
// ---- Clear interrupts ----
/// Clear all interrupts.
#[inline]
pub fn clear_all_interrupts(&self) {
let _ = self.i2c.ic_clr_intr.read_bits();
}
/// Clear RX UNDER interrupt.
#[inline]
pub fn clear_rx_under_interrupt(&self) {
let _ = self.i2c.ic_clr_rx_under.read_bits();
}
/// Clear RX OVER interrupt.
#[inline]
pub fn clear_rx_over_interrupt(&self) {
let _ = self.i2c.ic_clr_rx_over.read_bits();
}
/// Clear TX OVER interrupt.
#[inline]
pub fn clear_tx_over_interrupt(&self) {
let _ = self.i2c.ic_clr_tx_over.read_bits();
}
/// Clear TX ABRT interrupt.
#[inline]
pub fn clear_tx_abort_interrupt(&self) {
let _ = self.i2c.ic_clr_tx_abrt.read_bits();
}
/// Clear ACTIVITY interrupt.
#[inline]
pub fn clear_activity_interrupt(&self) {
let _ = self.i2c.ic_clr_activity.read_bits();
}
/// Clear STOP DET interrupt.
#[inline]
pub fn clear_stop_det_interrupt(&self) {
let _ = self.i2c.ic_clr_stop_det.read_bits();
}
/// Clear START DET interrupt.
#[inline]
pub fn clear_start_det_interrupt(&self) {
let _ = self.i2c.ic_clr_start_det.read_bits();
}
/// Clear GEN CALL interrupt.
#[inline]
pub fn clear_gen_call_interrupt(&self) {
let _ = self.i2c.ic_clr_gen_call.read_bits();
}
/// Clear RESTART DET interrupt.
#[inline]
pub fn clear_restart_det_interrupt(&self) {
let _ = self.i2c.ic_clr_restart_det.read_bits();
}
// ================================================================
// DMA Support
// ================================================================
/// DREQ selection for transmitting data via DMA.
pub const TX_DREQ: crate::dma::TreqSel = crate::dma::TreqSel::[<I2c $i2c_num Tx>];
/// DREQ selection for receiving data via DMA.
pub const RX_DREQ: crate::dma::TreqSel = crate::dma::TreqSel::[<I2c $i2c_num Rx>];
/// Get the I2C data/command register (IC_DATA_CMD) address for DMA transfers.
///
/// IC_DATA_CMD is at offset `0x10` from the I2C base address.
#[inline]
pub fn data_register_address(&self) -> u32 {
<$I2cType>::PTR as u32 + 0x10
}
/// Enable DMA requests for the transmit FIFO.
#[inline]
pub fn enable_dma_tx(&self) {
self.i2c.ic_dma_cr.set_tdmae();
}
/// Disable DMA requests for the transmit FIFO.
#[inline]
pub fn disable_dma_tx(&self) {
self.i2c.ic_dma_cr.clr_tdmae();
}
/// Enable DMA requests for the receive FIFO.
#[inline]
pub fn enable_dma_rx(&self) {
self.i2c.ic_dma_cr.set_rdmae();
}
/// Disable DMA requests for the receive FIFO.
#[inline]
pub fn disable_dma_rx(&self) {
self.i2c.ic_dma_cr.clr_rdmae();
}
/// Set the DMA transmit data level.
///
/// Controls the level at which a DMA request is generated for TX.
/// The DMA TX request fires when the number of entries in the
/// TX FIFO is at or below this threshold.
#[inline]
pub fn set_dma_tx_data_level(&self, level: u8) {
self.i2c.ic_dma_tdlr.write_dmatdl(level as u32);
}
/// Set the DMA receive data level.
///
/// Controls the level at which a DMA request is generated for RX.
/// The DMA RX request fires when the number of entries in the
/// RX FIFO is at or above this threshold + 1.
#[inline]
pub fn set_dma_rx_data_level(&self, level: u8) {
self.i2c.ic_dma_rdlr.write_dmardl(level as u32);
}
}
}
};
}
// ============================================================================
// Macro for generating I2C slave implementations
// ============================================================================
macro_rules! impl_i2c_slave {
($I2cName:ident, $i2c_num:literal, $I2cType:ty, $i2c_mod:ident, $SdaPin:ident, $SclPin:ident) => {
paste! {
/// I2C slave peripheral wrapper with type-safe pin ownership.
///
/// This struct owns both the I2C peripheral and the GPIO pins used for
/// SDA and SCL. The pins are configured automatically when the I2C
/// instance is created, and their ownership prevents them from being
/// used for other purposes.
///
#[doc = concat!("# I2C", $i2c_num, " Slave Pin Mapping")]
///
#[doc = concat!("See [`", stringify!($SdaPin), "`] for valid SDA pins.")]
///
#[doc = concat!("See [`", stringify!($SclPin), "`] for valid SCL pins.")]
pub struct $I2cName<SDA, SCL>
where
SDA: $SdaPin,
SCL: $SclPin,
{
i2c: $I2cType,
_sda: SDA,
_scl: SCL,
}
impl<SDA, SCL> $I2cName<SDA, SCL>
where
SDA: $SdaPin,
SCL: $SclPin,
{
/// I2C peripheral number
pub const NUM: u8 = $i2c_num;
// ================================================================
// Initialization (Slave Mode)
// ================================================================
/// Initialize I2C in slave mode with 7-bit address.
///
/// # Arguments
///
/// * `i2c` - The I2C peripheral from PAC
/// * `sda` - SDA pin (must implement the appropriate SDA trait)
/// * `scl` - SCL pin (must implement the appropriate SCL trait)
/// * `addr` - 7-bit slave address
/// * `sys_clk` - System clock frequency in Hz
///
/// # Example
///
/// ```ignore
#[doc = concat!("let i2c = ", stringify!($I2cName), "::init(")]
#[doc = concat!(" dp.I2C", $i2c_num, ",")]
/// pins.pin::<4>(), // SDA
/// pins.pin::<5>(), // SCL
/// 0x55, // Slave address
/// 150_000_000, // System clock
/// );
/// ```
pub fn init(i2c: $I2cType, sda: SDA, scl: SCL, addr: u8, sys_clk: u32) -> Self {
Self::init_impl(i2c, sda, scl, addr as u16, false, sys_clk)
}
/// Initialize I2C in slave mode with 10-bit address.
///
/// # Arguments
///
/// * `i2c` - The I2C peripheral from PAC
/// * `sda` - SDA pin (must implement the appropriate SDA trait)
/// * `scl` - SCL pin (must implement the appropriate SCL trait)
/// * `addr` - 10-bit slave address
/// * `sys_clk` - System clock frequency in Hz
pub fn init_10bit(i2c: $I2cType, sda: SDA, scl: SCL, addr: u16, sys_clk: u32) -> Self {
Self::init_impl(i2c, sda, scl, addr, true, sys_clk)
}
/// Shared slave-mode initialization.
fn init_impl(i2c: $I2cType, mut sda: SDA, mut scl: SCL, addr: u16, addr_10bit: bool, sys_clk: u32) -> Self {
// Configure pins for I2C function
sda.[<configure_i2c $i2c_num _sda>]();
scl.[<configure_i2c $i2c_num _scl>]();
// Reset peripheral to ensure clean register state
<$I2cType as Resettable>::init();
let i2c_dev = Self {
i2c,
_sda: sda,
_scl: scl,
};
// Disable I2C to configure
i2c_dev.i2c.ic_enable.clr_enable();
// Disable all interrupts and clear pending
i2c_dev.i2c.ic_intr_mask.write_bits(0);
let _ = i2c_dev.i2c.ic_clr_intr.read_bits();
// Configure as slave: clear all, then set required bits
i2c_dev.i2c.ic_con.write_bits(0);
i2c_dev.i2c.ic_con.write_speed(Speed::Fast as u32);
if addr_10bit {
i2c_dev.i2c.ic_con.set_ic_10bitaddr_slave();
}
i2c_dev.i2c.ic_con.set_stop_det_ifaddressed();
i2c_dev.i2c.ic_con.set_rx_fifo_full_hld_ctrl();
i2c_dev.i2c.ic_con.set_tx_empty_ctrl();
// Set slave address
i2c_dev.i2c.ic_sar.write_ic_sar(addr as u32);
// Set FIFO thresholds
i2c_dev.i2c.ic_tx_tl.write_tx_tl(0);
i2c_dev.i2c.ic_rx_tl.write_rx_tl(0);
// Configure timing for slave (use sensible defaults)
let sda_hold = (sys_clk / 1_000_000 / 3).max(1);
i2c_dev.i2c.ic_sda_hold.write_ic_sda_tx_hold(sda_hold);
// Enable I2C
i2c_dev.i2c.ic_enable.set_enable();
i2c_dev
}
// ================================================================
// Steal / Release
// ================================================================
/// Obtain a pin-erased I2C slave handle without ownership.
///
/// # Safety
///
/// The caller must ensure:
/// 1. The I2C peripheral has already been initialised (pins
/// configured, slave address set, peripheral taken out of reset).
/// 2. No data races: the returned handle must not be used
/// concurrently with the owning instance without proper
/// synchronisation (e.g. `Mutex`).
#[inline]
pub unsafe fn steal() -> crate::Steal<$I2cName<crate::gpio::pins::ErasedPin, crate::gpio::pins::ErasedPin>> {
crate::Steal($I2cName {
i2c: unsafe { <$I2cType>::steal() },
_sda: crate::gpio::pins::ErasedPin,
_scl: crate::gpio::pins::ErasedPin,
})
}
/// Get reference to the underlying register block.
///
/// # Safety
/// Direct register access can bypass the HAL's safety guarantees.
/// The caller must ensure operations do not conflict with the HAL state.
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::$i2c_mod::RegisterBlock {
&self.i2c
}
/// Release the I2C peripheral and pins.
///
/// Returns a tuple of (I2C peripheral, SDA pin, SCL pin).
/// The pins can then be reconfigured for other uses.
#[inline]
pub fn release(self) -> ($I2cType, SDA, SCL) {
(self.i2c, self._sda, self._scl)
}
// ================================================================
// Slave Mode Operations
// ================================================================
/// Read data sent by master (slave mode).
///
/// Returns the number of bytes read. Blocks until STOP condition.
pub fn read(&self, buffer: &mut [u8]) -> Result<usize, Error> {
let mut count = 0;
// Clear any pending interrupts
let _ = self.i2c.ic_clr_intr.read_bits();
// Wait for data or STOP
loop {
// Check for RX data
while self.i2c.ic_status.rfne() && count < buffer.len() {
buffer[count] = self.i2c.ic_data_cmd.dat() as u8;
count += 1;
}
// Check for STOP condition
if self.i2c.ic_raw_intr_stat.stop_det() {
// Read any remaining data
while self.i2c.ic_status.rfne() && count < buffer.len() {
buffer[count] = self.i2c.ic_data_cmd.dat() as u8;
count += 1;
}
// Clear STOP flag
let _ = self.i2c.ic_clr_stop_det.read_bits();
break;
}
// Check for errors
if self.i2c.ic_raw_intr_stat.rx_over() {
let _ = self.i2c.ic_clr_rx_over.read_bits();
return Err(Error::RxOverflow);
}
}
Ok(count)
}
/// Read data sent by master (slave mode, non-blocking).
///
/// Returns the number of bytes read.
pub fn read_available(&self, buffer: &mut [u8]) -> usize {
let mut count = 0;
while self.i2c.ic_status.rfne() && count < buffer.len() {
buffer[count] = self.i2c.ic_data_cmd.dat() as u8;
count += 1;
}
count
}
/// Write data for master to read (slave mode).
///
/// This writes data to the TX FIFO when master requests a read.
/// Call this when you detect a read request (RD_REQ interrupt).
pub fn write(&self, data: &[u8]) -> Result<(), Error> {
for &byte in data {
// Wait for TX FIFO space
while !self.i2c.ic_status.tfnf() {
if self.i2c.ic_raw_intr_stat.tx_over() {
let _ = self.i2c.ic_clr_tx_over.read_bits();
return Err(Error::TxOverflow);
}
}
// Write data
self.i2c.ic_data_cmd.write_dat(byte as u32);
}
// Clear read request flag
let _ = self.i2c.ic_clr_rd_req.read_bits();
Ok(())
}
/// Check if master is requesting a read (slave mode).
#[inline]
pub fn read_requested(&self) -> bool {
self.i2c.ic_raw_intr_stat.rd_req()
}
/// Clear read request flag (slave mode).
#[inline]
pub fn clear_read_request(&self) {
let _ = self.i2c.ic_clr_rd_req.read_bits();
}
/// Check if a STOP condition was detected (slave mode).
#[inline]
pub fn stop_detected(&self) -> bool {
self.i2c.ic_raw_intr_stat.stop_det()
}
/// Clear STOP detection flag (slave mode).
#[inline]
pub fn clear_stop(&self) {
let _ = self.i2c.ic_clr_stop_det.read_bits();
}
/// Check if a START condition was detected (slave mode).
#[inline]
pub fn start_detected(&self) -> bool {
self.i2c.ic_raw_intr_stat.start_det()
}
/// Clear START detection flag (slave mode).
#[inline]
pub fn clear_start(&self) {
let _ = self.i2c.ic_clr_start_det.read_bits();
}
// ================================================================
// Control
// ================================================================
/// Enable the I2C peripheral.
#[inline]
pub fn enable(&self) {
self.i2c.ic_enable.set_enable();
}
/// Disable the I2C peripheral.
#[inline]
pub fn disable(&self) {
self.i2c.ic_enable.clr_enable();
}
/// Check if I2C peripheral is enabled.
#[inline]
pub fn is_enabled(&self) -> bool {
self.i2c.ic_enable.enable()
}
// ================================================================
// Status
// ================================================================
/// Get the TX FIFO level.
#[inline]
pub fn tx_fifo_level(&self) -> u8 {
self.i2c.ic_txflr.txflr() as u8
}
/// Get the RX FIFO level.
#[inline]
pub fn rx_fifo_level(&self) -> u8 {
self.i2c.ic_rxflr.rxflr() as u8
}
/// Check if TX FIFO is empty.
#[inline]
pub fn is_tx_empty(&self) -> bool {
self.i2c.ic_status.tfe()
}
/// Check if RX FIFO is not empty.
#[inline]
pub fn is_rx_not_empty(&self) -> bool {
self.i2c.ic_status.rfne()
}
/// Check if I2C is busy.
#[inline]
pub fn is_busy(&self) -> bool {
self.i2c.ic_status.activity()
}
/// Get the current mode.
#[inline]
pub fn mode(&self) -> Mode {
Mode::Slave
}
// ================================================================
// Interrupts
// ================================================================
// ---- Mask operations (enable/disable) ----
/// Enable specific interrupts using mask.
pub fn enable_interrupts(&self, mask: u32) {
self.i2c.ic_intr_mask.set_bits(mask);
}
/// Disable specific interrupts using mask.
pub fn disable_interrupts(&self, mask: u32) {
self.i2c.ic_intr_mask.clr_bits(mask);
}
/// Disable all interrupts.
#[inline]
pub fn disable_all_interrupts(&self) {
self.i2c.ic_intr_mask.write_bits(0);
}
// ---- RX FULL interrupt ----
/// Enable RX FULL interrupt (RX FIFO reaches or exceeds threshold).
#[inline]
pub fn enable_rx_full_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_rx_full();
}
/// Disable RX FULL interrupt.
#[inline]
pub fn disable_rx_full_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_rx_full();
}
/// Check if RX FULL interrupt is enabled.
#[inline]
pub fn is_rx_full_interrupt_enabled(&self) -> bool {
self.i2c.ic_intr_mask.m_rx_full()
}
// ---- TX EMPTY interrupt ----
/// Enable TX EMPTY interrupt (TX FIFO at or below threshold).
#[inline]
pub fn enable_tx_empty_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_tx_empty();
}
/// Disable TX EMPTY interrupt.
#[inline]
pub fn disable_tx_empty_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_tx_empty();
}
/// Check if TX EMPTY interrupt is enabled.
#[inline]
pub fn is_tx_empty_interrupt_enabled(&self) -> bool {
self.i2c.ic_intr_mask.m_tx_empty()
}
// ---- TX ABRT interrupt ----
/// Enable TX abort interrupt.
#[inline]
pub fn enable_tx_abort_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_tx_abrt();
}
/// Disable TX abort interrupt.
#[inline]
pub fn disable_tx_abort_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_tx_abrt();
}
// ---- RD REQ interrupt ----
/// Enable read request interrupt (master wants to read).
#[inline]
pub fn enable_rd_req_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_rd_req();
}
/// Disable read request interrupt.
#[inline]
pub fn disable_rd_req_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_rd_req();
}
// ---- STOP DET interrupt ----
/// Enable stop detected interrupt.
#[inline]
pub fn enable_stop_det_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_stop_det();
}
/// Disable stop detected interrupt.
#[inline]
pub fn disable_stop_det_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_stop_det();
}
// ---- START DET interrupt ----
/// Enable start detected interrupt.
#[inline]
pub fn enable_start_det_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_start_det();
}
/// Disable start detected interrupt.
#[inline]
pub fn disable_start_det_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_start_det();
}
// ---- ACTIVITY interrupt ----
/// Enable activity interrupt.
#[inline]
pub fn enable_activity_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_activity();
}
/// Disable activity interrupt.
#[inline]
pub fn disable_activity_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_activity();
}
// ---- Error interrupts ----
/// Enable RX overflow interrupt.
#[inline]
pub fn enable_rx_over_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_rx_over();
}
/// Disable RX overflow interrupt.
#[inline]
pub fn disable_rx_over_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_rx_over();
}
/// Enable RX underflow interrupt.
#[inline]
pub fn enable_rx_under_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_rx_under();
}
/// Disable RX underflow interrupt.
#[inline]
pub fn disable_rx_under_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_rx_under();
}
/// Enable TX overflow interrupt.
#[inline]
pub fn enable_tx_over_interrupt(&self) {
self.i2c.ic_intr_mask.set_m_tx_over();
}
/// Disable TX overflow interrupt.
#[inline]
pub fn disable_tx_over_interrupt(&self) {
self.i2c.ic_intr_mask.clr_m_tx_over();
}
// ---- Status reading ----
/// Get raw interrupt status.
#[inline]
pub fn raw_interrupt_status(&self) -> u32 {
self.i2c.ic_raw_intr_stat.read_bits()
}
/// Get masked interrupt status.
#[inline]
pub fn interrupt_status(&self) -> u32 {
self.i2c.ic_intr_stat.read_bits()
}
// ---- Clear interrupts ----
/// Clear all interrupts.
#[inline]
pub fn clear_all_interrupts(&self) {
let _ = self.i2c.ic_clr_intr.read_bits();
}
/// Clear RX UNDER interrupt.
#[inline]
pub fn clear_rx_under_interrupt(&self) {
let _ = self.i2c.ic_clr_rx_under.read_bits();
}
/// Clear RX OVER interrupt.
#[inline]
pub fn clear_rx_over_interrupt(&self) {
let _ = self.i2c.ic_clr_rx_over.read_bits();
}
/// Clear TX OVER interrupt.
#[inline]
pub fn clear_tx_over_interrupt(&self) {
let _ = self.i2c.ic_clr_tx_over.read_bits();
}
/// Clear TX ABRT interrupt.
#[inline]
pub fn clear_tx_abort_interrupt(&self) {
let _ = self.i2c.ic_clr_tx_abrt.read_bits();
}
/// Clear ACTIVITY interrupt.
#[inline]
pub fn clear_activity_interrupt(&self) {
let _ = self.i2c.ic_clr_activity.read_bits();
}
/// Clear STOP DET interrupt.
#[inline]
pub fn clear_stop_det_interrupt(&self) {
let _ = self.i2c.ic_clr_stop_det.read_bits();
}
/// Clear START DET interrupt.
#[inline]
pub fn clear_start_det_interrupt(&self) {
let _ = self.i2c.ic_clr_start_det.read_bits();
}
/// Clear GEN CALL interrupt.
#[inline]
pub fn clear_gen_call_interrupt(&self) {
let _ = self.i2c.ic_clr_gen_call.read_bits();
}
/// Clear RESTART DET interrupt.
#[inline]
pub fn clear_restart_det_interrupt(&self) {
let _ = self.i2c.ic_clr_restart_det.read_bits();
}
// ================================================================
// DMA Support
// ================================================================
/// DREQ selection for transmitting data via DMA.
pub const TX_DREQ: crate::dma::TreqSel = crate::dma::TreqSel::[<I2c $i2c_num Tx>];
/// DREQ selection for receiving data via DMA.
pub const RX_DREQ: crate::dma::TreqSel = crate::dma::TreqSel::[<I2c $i2c_num Rx>];
/// Get the I2C data/command register (IC_DATA_CMD) address for DMA transfers.
///
/// IC_DATA_CMD is at offset `0x10` from the I2C base address.
#[inline]
pub fn data_register_address(&self) -> u32 {
<$I2cType>::PTR as u32 + 0x10
}
/// Enable DMA requests for the transmit FIFO.
#[inline]
pub fn enable_dma_tx(&self) {
self.i2c.ic_dma_cr.set_tdmae();
}
/// Disable DMA requests for the transmit FIFO.
#[inline]
pub fn disable_dma_tx(&self) {
self.i2c.ic_dma_cr.clr_tdmae();
}
/// Enable DMA requests for the receive FIFO.
#[inline]
pub fn enable_dma_rx(&self) {
self.i2c.ic_dma_cr.set_rdmae();
}
/// Disable DMA requests for the receive FIFO.
#[inline]
pub fn disable_dma_rx(&self) {
self.i2c.ic_dma_cr.clr_rdmae();
}
/// Set the DMA transmit data level.
///
/// Controls the level at which a DMA request is generated for TX.
/// The DMA TX request fires when the number of entries in the
/// TX FIFO is at or below this threshold.
#[inline]
pub fn set_dma_tx_data_level(&self, level: u8) {
self.i2c.ic_dma_tdlr.write_dmatdl(level as u32);
}
/// Set the DMA receive data level.
///
/// Controls the level at which a DMA request is generated for RX.
/// The DMA RX request fires when the number of entries in the
/// RX FIFO is at or above this threshold + 1.
#[inline]
pub fn set_dma_rx_data_level(&self, level: u8) {
self.i2c.ic_dma_rdlr.write_dmardl(level as u32);
}
}
}
};
}
// ============================================================================
// Generate I2C0 and I2C1 (Master)
// ============================================================================
impl_i2c!(I2c0, 0, rp2350_pac::I2C0, i2c0, I2c0SdaPin, I2c0SclPin);
impl_i2c!(I2c1, 1, rp2350_pac::I2C1, i2c1, I2c1SdaPin, I2c1SclPin);
// ============================================================================
// Generate I2C0Slave and I2C1Slave
// ============================================================================
impl_i2c_slave!(I2c0Slave, 0, rp2350_pac::I2C0, i2c0, I2c0SdaPin, I2c0SclPin);
impl_i2c_slave!(I2c1Slave, 1, rp2350_pac::I2C1, i2c1, I2c1SdaPin, I2c1SclPin);