-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWire.cpp
More file actions
1012 lines (912 loc) · 33.9 KB
/
Copy pathWire.cpp
File metadata and controls
1012 lines (912 loc) · 33.9 KB
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
/*
* TWI/I2C library for the MFL Arduino Core
* Copyright (c) 2025 Arduino LLC. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Arduino.h>
#include <PinOpsMap.hpp>
#include <PinOps.hpp>
#include "Wire.h"
namespace arduino {
inline constexpr uint8_t BUFFER_LENGTH = WIRE_BUFFER_LENGTH;
inline constexpr uint8_t OWNER_ADDRESS = 0x33U;
inline constexpr uint32_t DEFAULT_SPEED = 100'000U;
inline constexpr uint32_t I2C_TIMEOUT_DEFAULT = 0xF0000U;
auto TwoWire::get_instance(i2c::I2C_Base Base, pin_size_t sdaPin, pin_size_t sclPin) -> TwoWire& {
switch (Base) {
case i2c::I2C_Base::I2C0_BASE: {
static TwoWire WI2C0(i2c::I2C_Base::I2C0_BASE, sdaPin, sclPin);
return WI2C0;
}
case i2c::I2C_Base::I2C1_BASE: {
static TwoWire WI2C1(i2c::I2C_Base::I2C1_BASE, sdaPin, sclPin);
return WI2C1;
}
case i2c::I2C_Base::INVALID:
default:
#ifdef CORE_DEBUG
core_debug("Invalid I2C instance!");
__builtin_trap();
#endif
static TwoWire dummy(i2c::I2C_Base::INVALID, sdaPin, sclPin);
return dummy;
}
}
TwoWire::TwoWire(i2c::I2C_Base Base, pin_size_t sdaPin, pin_size_t sclPin) :
rxBuffer_(),
txBuffer_(),
i2c_(i2c::I2C::get_instance(Base).value()),
customSdaPin_(sdaPin),
customSclPin_(sclPin),
base_(Base),
ownerAddress_(0),
txAddress_(0),
transmitting_(false)
{
}
/**
* @brief Enables the I2C interface with the default address.
*
* This function enables the I2C interface with the default address
* (0x33) and configures the pins as open-drain outputs.
*
* @note The I2C interface is disabled when this function is called.
* @note The I2C interface is enabled when the function completes.
*
* @see begin(uint8_t)
* @see end()
*/
void TwoWire::begin() {
ownerAddress_ = OWNER_ADDRESS << 1U;
i2c_.set_address_format(ownerAddress_, i2c::Address_Format::FORMAT_7BITS, i2c::Bus_Mode::I2C);
i2c_.set_enable(true);
i2c_.set_ack_enable(true);
configurePins();
}
/**
* @brief Enables the I2C interface with the specified address.
*
* This function enables the I2C interface with the specified address and
* configures the pins as open-drain outputs.
*
* @param address The address to use for the I2C interface.
* The address must be a 7-bit address and will be shifted left one bit
* to allow for the read bit to be added.
*
* @note The I2C interface is disabled when this function is called.
* @note The I2C interface is enabled when the function completes.
*
* @see begin()
* @see end()
*/
void TwoWire::begin(uint8_t address) {
ownerAddress_ = address << 1U;
i2c_.set_address_format(ownerAddress_, i2c::Address_Format::FORMAT_7BITS, i2c::Bus_Mode::I2C);
i2c_.set_enable(true);
i2c_.set_ack_enable(true);
configurePins();
setSlaveInterruptEnable();
}
void TwoWire::end() {
flush();
i2c_.set_enable(false);
}
/**
* @brief Sets the clock speed of the I2C interface.
*
* This function sets the clock speed of the I2C interface to the given speed.
* The clock speed can only be changed while the I2C interface is disabled.
*
* @param speed The desired clock speed of the I2C interface in Hz.
*
* @note This function is not thread-safe and should only be called from a single
* thread.
*/
void TwoWire::setClock(uint32_t speed) {
// Clock can only be changed while the I2C is disabled
i2c_.set_enable(false);
i2c_.set_clock_speed_duty(speed, i2c::Duty_Cycle::DTCY_2);
i2c_.set_enable(true);
}
/**
* @brief Begin a transmission to a slave device.
*
* This function begins a transmission to the slave device with the given
* address. The address must be a 7-bit address and will be shifted left one
* bit to allow for the read bit to be added.
*
* @param address The address of the slave device to transmit to.
*
* @note This function is not thread-safe and should only be called from a single
* thread.
*
* @see endTransmission()
*/
void TwoWire::beginTransmission(uint8_t address) {
// Save address of target and clear buffer
transmitting_ = true;
txAddress_ = address << 1;
txBuffer_.clear();
}
/**
* @brief Completes a transmission to a slave device.
*
* This function completes a transmission to a slave device that was started
* with beginTransmission(). The function transmits the data in the transmit
* buffer in a single transmission. If the stopBit argument is false, the
* function does not generate a stop condition after transmission and is
* suitable for sending a stream of data to a slave device.
*
* @param stopBit If true, generate a stop condition after transmission.
*
* @return 0 on success, 1 on data too long, 2 on slave device NACKs address,
* 3 on slave device NACKs data, 4 on other error.
*
* @note This function is not thread-safe and should only be called from a single
* thread.
*
* @see beginTransmission()
*/
uint8_t TwoWire::endTransmission(bool stopBit) {
uint8_t txLength = txBuffer_.available();
if (txLength == 0) {
// Nothing to transmit
transmitting_ = false;
return 0U;
}
// Transmit all bytes in the buffer
i2c::I2C_Error_Type result = masterTransmit(txAddress_, txLength, stopBit);
// Clear the buffer and reset transmitting flag
txBuffer_.clear();
transmitting_ = false;
// Map error codes to Arduino Wire error codes
switch (result) {
case i2c::I2C_Error_Type::OK: return 0U;
case i2c::I2C_Error_Type::DATA_SIZE_ERROR: return 1U;
case i2c::I2C_Error_Type::NACK_ADDRESS: return 2U;
case i2c::I2C_Error_Type::NACK_DATA: return 3U;
default: return 4U;
}
}
/**
* @brief Completes a transmission to a slave device with a stop condition.
*
* This function is a convenience function for endTransmission(true).
*
* @return 0 on success, 1 on data too long, 2 on slave device NACKs address,
* 3 on slave device NACKs data, 4 on other error.
*
* @see endTransmission(bool)
*/
uint8_t TwoWire::endTransmission() {
return endTransmission(true);
}
/**
* @brief Request data from a slave device.
*
* This function initiates a data request from a specified slave device on the
* I2C bus. It sends a request to the device with the given address and attempts
* to receive the specified number of bytes. The received data is stored in the
* internal receive buffer, and the function returns the number of bytes successfully
* received. If the requested length exceeds the buffer size, it will be clamped to
* the buffer size.
*
* @param address The 7-bit address of the slave device.
* @param len The number of bytes to request.
* @param stopBit If true, a stop condition will be sent after the data is received.
*
* @return The number of bytes successfully received.
*/
size_t TwoWire::requestFrom(uint8_t address, size_t len, bool stopBit) {
// Check for valid length
if (len == 0U) {
return 0;
}
// Clamp length to buffer size
if (len > BUFFER_LENGTH) {
len = BUFFER_LENGTH;
}
// Receive data from the specified address
if (masterReceive(address << 1U, len, stopBit) == i2c::I2C_Error_Type::OK) {
return len;
}
return 0;
}
/**
* @brief Request data from a slave device.
*
* This function is a wrapper around requestFrom(address, len, true) and is
* provided for convenience. It will send a stop condition after the data is
* received.
*
* @param address The 7-bit address of the slave device.
* @param len The number of bytes to request.
*
* @return The number of bytes successfully received.
*/
size_t TwoWire::requestFrom(uint8_t address, size_t len) {
return requestFrom(address, len, true);
}
/**
* @brief Send a single byte of data to a slave device.
*
* @param[in] data The byte of data to send.
* @return Number of bytes sent.
*
* This function sends a single byte of data to a slave device. If the
* internal transmit buffer is full, the function returns 0. If the slave
* device is in receive mode, the function returns 0. If the data byte is
* sent successfully, the function returns 1.
*/
size_t TwoWire::write(uint8_t data) {
if (transmitting_) {
// Master mode - store data in transmit buffer
if (txBuffer_.availableForStore()) {
txBuffer_.store_char(data);
return 1;
}
// Buffer is full
return 0;
} else {
// Slave mode - directly write to slave buffer
return (writeSlaveBuffer(&data, 1U) == i2c::I2C_Error_Type::OK) ? 1U : 0U;
}
}
/**
* @brief Send a sequence of bytes to a slave device.
*
* @param[in] buffer The sequence of bytes to send.
* @param[in] len The number of bytes to send.
* @return Number of bytes sent.
*
* This function sends a sequence of bytes to a slave device. If the
* internal transmit buffer is full, the function returns the number of
* bytes sent so far. If the slave device is in receive mode, the
* function returns 0. If the data bytes are sent successfully, the
* function returns the number of bytes sent.
*/
size_t TwoWire::write(const uint8_t* buffer, size_t len) {
// Check for null buffer or zero length
if (buffer == nullptr || len == 0) {
return 0;
}
if (transmitting_) {
// Master mode - store data in transmit buffer
size_t i;
for (i = 0U; i < len; ++i) {
if (!txBuffer_.availableForStore()) {
// Buffer is full, return number of bytes stored so far
return i;
}
txBuffer_.store_char(buffer[i]);
}
return i; // Return number of bytes stored
} else {
// Slave mode - directly write to slave buffer
return (writeSlaveBuffer(buffer, len) == i2c::I2C_Error_Type::OK) ? len : 0;
}
}
/**
* @brief Get the number of bytes available to read from the receive buffer.
*
* @return Number of bytes available to read.
*
* This function returns the number of bytes available to read from the
* receive buffer. If the receive buffer is empty, the function returns 0.
*/
int TwoWire::available() {
return rxBuffer_.available();
}
/**
* @brief Read a byte from the receive buffer.
*
* @return The byte read from the receive buffer, or -1 if the buffer is empty.
*
* This function reads a byte from the receive buffer. If the receive buffer
* is empty, the function returns -1. If data is available, the function returns
* the byte read from the receive buffer.
*/
int TwoWire::read() {
return rxBuffer_.read_char();
}
/**
* @brief Peek at the next byte in the receive buffer without removing it.
*
* @return The next byte in the buffer, or -1 if the buffer is empty.
*
* This function returns the next byte in the receive buffer without removing
* it. If the receive buffer is empty, the function returns -1. If data is
* available, the function returns the byte peeked from the receive buffer.
*/
int TwoWire::peek() {
return rxBuffer_.peek();
}
/**
* @brief Wait for all data to be sent.
*
* Waits for all data in the transmit buffer to be sent.
*/
void TwoWire::flush() {
while (txBuffer_.available() > 0) {
}
}
/**
* @brief Registers a callback function for receiving data from the I2C bus.
*
* This function sets the callback function that will be called whenever
* data is received from the I2C bus. The callback function must be
* declared as void function(int) and must take a single int argument
* indicating the number of bytes available in the receive buffer.
*
* @param function Pointer to the callback function to be registered.
*/
void TwoWire::onReceive(void(*function)(int)) {
onReceiveCallback = function;
}
/**
* @brief Registers a callback function for sending data to the I2C bus.
*
* This function sets the callback function that will be called whenever
* the I2C master requests data from the slave device. The callback function
* must be declared as void function(void) with no arguments.
*
* @param function Pointer to the callback function to be registered.
*/
void TwoWire::onRequest(void(*function)()) {
onRequestCallback = function;
}
/**
* @brief Transmit data to a slave device as master.
*
* This function transmits a sequence of bytes to a slave device as master.
* If the internal transmit buffer is full, the function returns the number of
* bytes sent so far. If the slave device NACKs the address or data, the
* function returns the corresponding error value. If the data bytes are sent
* successfully, the function returns i2c::I2C_Error_Type::OK.
*
* @param address The address of the slave device to transmit to.
* @param buffer The sequence of bytes to transmit.
* @param len The number of bytes to transmit.
* @param stopBit If true, generate a stop condition after transmitting the data.
* @return i2c::I2C_Error_Type::OK if transmission was successful, otherwise an error value.
*/
i2c::I2C_Error_Type TwoWire::masterTransmit(uint8_t address, uint8_t len, bool stopBit) {
// Special case for zero-length transmissions
if (len == 0U) {
return waitForReadyState(address);
}
// Check if bus is busy before starting transmission
if (checkBusyState() == i2c::I2C_Error_Type::BUSY) {
return i2c::I2C_Error_Type::BUSY;
}
// Generate start condition and wait for it to be sent
i2c_.generate_start_condition();
uint32_t timeout = I2C_TIMEOUT_DEFAULT;
while (!i2c_.get_flag(i2c::Status_Flags::FLAG_SBSEND)) {
if (--timeout == 0U) {
return i2c::I2C_Error_Type::TIMEOUT;
}
}
// Set direction and address, then wait for address to be sent
i2c_.set_direction_address(i2c::Transfer_Direction::TRANSMIT, address);
timeout = I2C_TIMEOUT_DEFAULT;
while (!i2c_.get_flag(i2c::Status_Flags::FLAG_ADDSEND)) {
if (--timeout == 0U) {
return i2c::I2C_Error_Type::NACK_ADDRESS;
}
}
// Clear address sent flag
i2c_.clear_flag(i2c::Clear_Flags::FLAG_ADDSEND);
// Transmit specified number of bytes from the buffer
for (uint8_t i = 0U; i < len; i++) {
if (txBuffer_.available() == 0) {
// No more data in buffer
break;
}
uint8_t data = txBuffer_.read_char();
if (writeByte(data) != i2c::I2C_Error_Type::OK) {
// Generate stop condition if requested
if (stopBit) {
stop();
}
return i2c::I2C_Error_Type::NACK_DATA;
}
}
// Generate stop condition if requested
if (stopBit) {
i2c::I2C_Error_Type stopResult = stop();
if (stopResult != i2c::I2C_Error_Type::OK) {
return stopResult;
}
}
return i2c::I2C_Error_Type::OK;
}
/**
* @brief Master receives data from slave device.
*
* This function sends a start condition, transmits the slave address with the
* read bit set, and then receives the specified amount of data from the slave
* device. If the stopBit parameter is set to true, a stop condition is sent at
* the end of the transmission.
*
* @param address The address of the slave device.
* @param len The number of bytes to receive.
* @param stopBit Whether a stop condition should be sent at the end of the
* transmission.
*
* @return i2c::I2C_Error_Type::OK on success, an error code otherwise.
*/
i2c::I2C_Error_Type TwoWire::masterReceive(uint8_t address, uint8_t len, bool stopBit) {
// Check if bus is busy before starting reception
if (checkBusyState() == i2c::I2C_Error_Type::BUSY) {
return i2c::I2C_Error_Type::BUSY;
}
// Clear the buffer before receiving new data
rxBuffer_.clear();
// Check if there's enough space in the buffer
if (rxBuffer_.availableForStore() < len) {
return i2c::I2C_Error_Type::DATA_SIZE_ERROR;
}
// Configure ACK behavior based on number of bytes to receive
if (len == 1U) {
// For single byte, disable ACK before reception
i2c_.set_ack_enable(false);
} else if (len == 2U) {
// For two bytes, set ACK position to next and disable ACK
i2c_.set_ack_position(i2c::ACK_Select::NEXT);
i2c_.set_ack_enable(false);
} else {
// For more than two bytes, enable ACK
i2c_.set_ack_enable(true);
}
// Generate start condition and wait for it to be sent
i2c_.generate_start_condition();
uint32_t timeout = I2C_TIMEOUT_DEFAULT;
while (!i2c_.get_flag(i2c::Status_Flags::FLAG_SBSEND)) {
if (--timeout == 0U) {
return i2c::I2C_Error_Type::TIMEOUT;
}
}
// Set direction to receive and send address
i2c_.set_direction_address(i2c::Transfer_Direction::RECEIVE, address);
// Wait for address to be sent
timeout = I2C_TIMEOUT_DEFAULT;
while (!i2c_.get_flag(i2c::Status_Flags::FLAG_ADDSEND)) {
if (--timeout == 0U) {
return i2c::I2C_Error_Type::NACK_ADDRESS;
}
}
// Clear address sent flag
i2c_.clear_flag(i2c::Clear_Flags::FLAG_ADDSEND);
// Receive all bytes
for (uint32_t i = 0U; i < len; i++) {
// Special handling for multi-byte transfers
if (len > 2U && i == static_cast<uint32_t>(len) - 3U) {
// For transfers > 2 bytes, wait for byte transfer complete before
// disabling ACK on the third-to-last byte
timeout = I2C_TIMEOUT_DEFAULT;
while (!i2c_.get_flag(i2c::Status_Flags::FLAG_BTC)) {
if (--timeout == 0U) {
if (stopBit) {
stop();
}
return i2c::I2C_Error_Type::NACK_DATA;
}
}
i2c_.set_ack_enable(false);
} else if (len == 2U && i == 0U) {
// For 2-byte transfers, wait for byte transfer complete after first byte
timeout = I2C_TIMEOUT_DEFAULT;
while (!i2c_.get_flag(i2c::Status_Flags::FLAG_BTC)) {
if (--timeout == 0U) {
if (stopBit) {
stop();
}
return i2c::I2C_Error_Type::NACK_DATA;
}
}
}
// Wait for receive buffer not empty
timeout = I2C_TIMEOUT_DEFAULT;
while (!i2c_.get_flag(i2c::Status_Flags::FLAG_RBNE)) {
if (--timeout == 0U) {
if (stopBit) {
stop();
}
return i2c::I2C_Error_Type::NACK_DATA;
}
}
// Read data from receive buffer and store in ring buffer
uint8_t receivedByte = i2c_.receive_data();
rxBuffer_.store_char(receivedByte);
}
// Generate stop condition if requested
if (stopBit) {
i2c::I2C_Error_Type stopResult = stop();
if (stopResult != i2c::I2C_Error_Type::OK) {
return stopResult;
}
}
return i2c::I2C_Error_Type::OK;
}
/**
* @brief Transmit a single byte to the I2C bus as master.
*
* This function will transmit a single byte to the I2C bus as master.
* If the transmission is successful, the function will return i2c::I2C_Error_Type::OK.
* If the transmission times out, the function will return i2c::I2C_Error_Type::TIMEOUT.
*
* @param data The byte of data to be transmitted.
* @return i2c::I2C_Error_Type::OK if transmission was successful, otherwise i2c::I2C_Error_Type::TIMEOUT.
*/
i2c::I2C_Error_Type TwoWire::writeByte(uint8_t data) {
// Transmit the data byte
i2c_.transmit_data(data);
// Wait for either transmit buffer empty or byte transfer complete
uint32_t timeout = I2C_TIMEOUT_DEFAULT;
while (true) {
// Check if either flag is set
if (i2c_.get_flag(i2c::Status_Flags::FLAG_TBE) ||
i2c_.get_flag(i2c::Status_Flags::FLAG_BTC)) {
break;
}
// Check for timeout
if (--timeout == 0U) {
return i2c::I2C_Error_Type::TIMEOUT;
}
}
return i2c::I2C_Error_Type::OK;
}
/**
* @brief Store data in the internal transmit buffer for transmission to a slave device.
*
* This function will store the specified amount of data from the given buffer
* in the internal transmit buffer. If the buffer is full, the function will
* return i2c::I2C_Error_Type::DATA_SIZE_ERROR. If the data is successfully stored,
* the function will return i2c::I2C_Error_Type::OK.
*
* @param buffer The buffer containing the data to be transmitted.
* @param len The number of bytes to transmit.
* @return i2c::I2C_Error_Type::OK if data was stored successfully, otherwise i2c::I2C_Error_Type::DATA_SIZE_ERROR.
*/
i2c::I2C_Error_Type TwoWire::writeSlaveBuffer(const uint8_t* buffer, uint8_t len) {
// Check if there's enough space in the ring buffer
if (txBuffer_.availableForStore() >= len) {
for (uint8_t i = 0U; i < len; i++) {
txBuffer_.store_char(buffer[i]);
}
return i2c::I2C_Error_Type::OK;
}
return i2c::I2C_Error_Type::DATA_SIZE_ERROR;
}
/**
* @brief Generate a stop condition on the I2C bus.
*
* This function generates a stop condition on the I2C bus by setting the
* STOP bit in the control register (CTL0). The function will block until the
* stop bit is reset by hardware or a timeout occurs.
*
* @return i2c::I2C_Error_Type::OK if the stop condition was generated successfully, otherwise i2c::I2C_Error_Type::TIMEOUT.
*/
i2c::I2C_Error_Type TwoWire::stop() {
i2c_.generate_stop_condition();
// wait for stop bit reset with timeout
uint32_t timeout = I2C_TIMEOUT_DEFAULT;
while (i2c_.get_stop_condition()) {
if (--timeout == 0U) {
return i2c::I2C_Error_Type::TIMEOUT;
}
}
return i2c::I2C_Error_Type::OK;
}
/**
* @brief Waits for the bus to be ready to transmit data to a slave device.
*
* This function waits for the bus to be ready to transmit data to a slave
* device. If the bus is busy, the function will return i2c::I2C_Error_Type::BUSY.
* If the bus is ready, the function will return i2c::I2C_Error_Type::OK.
*
* @param address The address of the slave device to wait for.
* @return i2c::I2C_Error_Type::OK if the bus is ready, otherwise i2c::I2C_Error_Type::BUSY or i2c::I2C_Error_Type::TIMEOUT.
*/
i2c::I2C_Error_Type TwoWire::waitForReadyState(uint8_t address) {
// Check if bus is busy
if (checkBusyState() == i2c::I2C_Error_Type::BUSY) {
return i2c::I2C_Error_Type::BUSY;
}
// Generate start condition
i2c_.generate_start_condition();
// Wait for start bit to be sent
uint32_t timeout = I2C_TIMEOUT_DEFAULT;
while (!i2c_.get_flag(i2c::Status_Flags::FLAG_SBSEND)) {
if (--timeout == 0U) {
return i2c::I2C_Error_Type::TIMEOUT;
}
}
// Set direction and address
i2c_.set_direction_address(i2c::Transfer_Direction::TRANSMIT, address);
// Wait for address to be sent or error
timeout = I2C_TIMEOUT_DEFAULT;
bool addsend = false;
bool aerr = false;
do {
addsend = i2c_.get_flag(i2c::Status_Flags::FLAG_ADDSEND);
aerr = i2c_.get_flag(i2c::Status_Flags::FLAG_AERR);
if (--timeout == 0U) {
// Generate stop condition
i2c_.generate_stop_condition();
// Wait for stop condition with timeout
uint32_t stopTimeout = I2C_TIMEOUT_DEFAULT;
while (i2c_.get_stop_condition() != 0U) {
if (--stopTimeout == 0U) {
return i2c::I2C_Error_Type::TIMEOUT;
}
}
return i2c::I2C_Error_Type::TIMEOUT;
}
} while (!(addsend || aerr)); // Continue until either flag is set
// Handle result based on which flag was set
if (addsend) {
i2c_.clear_flag(i2c::Clear_Flags::FLAG_ADDSEND);
i2c_.generate_stop_condition();
// Wait for stop condition with timeout
timeout = I2C_TIMEOUT_DEFAULT;
while (i2c_.get_stop_condition() != 0U) {
if (--timeout == 0U) {
return i2c::I2C_Error_Type::TIMEOUT;
}
}
return i2c::I2C_Error_Type::OK;
} else {
// Must be aerr
i2c_.clear_flag(i2c::Clear_Flags::FLAG_AERR);
i2c_.generate_stop_condition();
// Wait for stop condition with timeout
timeout = I2C_TIMEOUT_DEFAULT;
while (i2c_.get_stop_condition() != 0U) {
if (--timeout == 0U) {
return i2c::I2C_Error_Type::TIMEOUT;
}
}
return i2c::I2C_Error_Type::NACK_ADDRESS;
}
}
/**
* @brief Configures the SDA and SCL pins for I2C communication.
*
* If a custom pin is specified for SDA or SCL, the function will configure that
* pin. Otherwise, it will use the pin mapping defined in the Variant.h file.
*
* The function will set the pin mode and speed according to the pin operations
* defined for the I2C peripheral and the pin.
*
* The function will also check if a remap is required for the pin and apply it
* if necessary.
*/
void TwoWire::configurePins() {
// SDA pin
if (customSdaPin_ != NO_PIN) {
pinOpsPinout(I2C_SDA_PinOps, customSdaPin_);
} else {
auto sdaPinOps = getPinOpsByPeripheral(I2C_SDA_PinOps, base_);
if (sdaPinOps == invalidPinOps) {
return;
}
auto sdaMode = getPackedPinMode(sdaPinOps.packedPinOps);
auto sdaSpeed = getPackedPinSpeed(sdaPinOps.packedPinOps);
// Initialize pin
auto& sdaPort = gpio::GPIO::get_instance(sdaPinOps.port).value();
sdaPort.set_pin_mode(sdaPinOps.pin, sdaMode, sdaSpeed);
// Set remap
auto sdaRemap = getPackedPinRemap(sdaPinOps.packedPinOps);
AFIO_I.set_remap(sdaRemap);
}
// SCL pin
if (customSclPin_ != NO_PIN) {
pinOpsPinout(I2C_SCL_PinOps, customSclPin_);
} else {
auto sclPinOps = getPinOpsByPeripheral(I2C_SCL_PinOps, base_);
if (sclPinOps == invalidPinOps) {
return;
}
auto sclMode = getPackedPinMode(sclPinOps.packedPinOps);
auto sclSpeed = getPackedPinSpeed(sclPinOps.packedPinOps);
// Initialize pin
auto& sclPort = gpio::GPIO::get_instance(sclPinOps.port).value();
sclPort.set_pin_mode(sclPinOps.pin, sclMode, sclSpeed);
// Set remap
auto sclRemap = getPackedPinRemap(sclPinOps.packedPinOps);
AFIO_I.set_remap(sclRemap);
}
}
/**
* @brief Check if the I2C bus is busy.
*
* This function checks the I2C bus busy flag and returns i2c::I2C_Error_Type::BUSY if the bus is busy.
* If the bus is not busy, the function returns i2c::I2C_Error_Type::OK. If the timeout elapses before the bus
* is not busy, the function returns i2c::I2C_Error_Type::TIMEOUT.
*
* @return i2c::I2C_Error_Type::OK if the bus is not busy, otherwise i2c::I2C_Error_Type::BUSY or i2c::I2C_Error_Type::TIMEOUT.
*/
i2c::I2C_Error_Type TwoWire::checkBusyState() {
// Wait until the I2C bus is not busy or timeout occurs
uint32_t timeout = I2C_TIMEOUT_DEFAULT;
while (i2c_.get_flag(i2c::Status_Flags::FLAG_I2CBSY)) {
if (--timeout == 0U) {
return i2c::I2C_Error_Type::BUSY;
}
}
return i2c::I2C_Error_Type::OK;
}
/**
* @brief Enables interrupts for the slave device.
*
* This function enables interrupts for the slave device in the NVIC and
* in the I2C peripheral. The interrupts enabled are the event interrupts,
* error interrupts, and buffer interrupts.
*
* @note This function should be called before calling the begin() function.
*
* @see begin()
*/
void TwoWire::setSlaveInterruptEnable() {
// Configure NVIC priorities and enable interrupts based on I2C instance
switch (base_) {
case i2c::I2C_Base::I2C0_BASE:
// Set priorities for I2C0 event and error interrupts
CORTEX_I.set_nvic_priority(I2C0_EV_IRQn, 2U, 3U);
CORTEX_I.set_nvic_priority(I2C0_ER_IRQn, 2U, 2U);
// Enable I2C0 event and error interrupts
NVIC_EnableIRQ(I2C0_EV_IRQn);
NVIC_EnableIRQ(I2C0_ER_IRQn);
break;
case i2c::I2C_Base::I2C1_BASE:
// Set priorities for I2C1 event and error interrupts
CORTEX_I.set_nvic_priority(I2C1_EV_IRQn, 2U, 3U);
CORTEX_I.set_nvic_priority(I2C1_ER_IRQn, 2U, 2U);
// Enable I2C1 event and error interrupts
NVIC_EnableIRQ(I2C1_EV_IRQn);
NVIC_EnableIRQ(I2C1_ER_IRQn);
break;
default:
// Invalid I2C base, do nothing
return;
}
// Enable I2C peripheral interrupts
i2c_.set_interrupt_enable(i2c::Interrupt_Type::INTR_ERR, true);
i2c_.set_interrupt_enable(i2c::Interrupt_Type::INTR_EV, true);
i2c_.set_interrupt_enable(i2c::Interrupt_Type::INTR_BUF, true);
}
/**
* @brief Error handler for I2C events.
*
* This function is called when an error occurs on the I2C bus. It checks
* the interrupt flags and clears them if set.
*/
void TwoWire::errorHandler() {
if (i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_AERR)) {
i2c_.clear_interrupt_flag(i2c::Clear_Flags::FLAG_AERR);
}
// SMBus alert
if (i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_SMBALT)) {
i2c_.clear_interrupt_flag(i2c::Clear_Flags::FLAG_SMBALT);
}
// SMBus mode bus timeout
if (i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_SMBTO)) {
i2c_.clear_interrupt_flag(i2c::Clear_Flags::FLAG_SMBTO);
}
// Overrun or underrun when SCL stretch is disabled
if (i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_OUERR)) {
i2c_.clear_interrupt_flag(i2c::Clear_Flags::FLAG_OUERR);
}
// Arbitration lost
if (i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_LOSTARB)) {
i2c_.clear_interrupt_flag(i2c::Clear_Flags::FLAG_LOSTARB);
}
// Bus error
if (i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_BERR)) {
i2c_.clear_interrupt_flag(i2c::Clear_Flags::FLAG_BERR);
}
// CRC mismatch
if (i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_PECERR)) {
i2c_.clear_interrupt_flag(i2c::Clear_Flags::FLAG_PECERR);
}
}
/**
* Interrupt handler for the I2C bus.
*
* This function is called when an interrupt occurs for the I2C bus. The
* interrupt can be triggered by the following events:
*
* - A slave address has been sent and matched (ADDSEND)
* - The transmit buffer is empty (TBE)
* - The receive buffer is not empty (RBNE)
* - A stop condition has been detected (STPDET)
*
* The function will clear the interrupt flag and perform the necessary actions
* to handle the interrupt. If the interrupt is caused by a slave address match,
* the function will reset the RX count and check if the bus is in transmit mode.
* If it is, it will clear the TX buffer and call the registered transmit callback.
*
* If the interrupt is caused by the transmit buffer being empty, the function
* will check if there is data available in the TX buffer and send it if so.
*
* If the interrupt is caused by the receive buffer not being empty, the function
* will read the data from the bus and store it in the RX buffer.
*
* If the interrupt is caused by a stop condition being detected, the function
* will call the registered slave receive callback and clear the RX buffer.
*/
void TwoWire::interruptHandler() {
if (i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_ADDSEND)) {
i2c_.clear_interrupt_flag(i2c::Clear_Flags::FLAG_ADDSEND);
// Reset RX count and check transmit mode
rxBuffer_.clear();
if (i2c_.get_flag(i2c::Status_Flags::FLAG_TRS)) {
txBuffer_.clear();
if (onRequestCallback) {
onRequestCallback(); // Call the registered transmit callback
}
}
} else if (i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_TBE) &&
!i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_AERR)) {
// Transmit Buffer Empty: Send next byte if available
if (txBuffer_.available() > 0) {
i2c_.transmit_data(txBuffer_.read_char());
}
} else if (i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_RBNE)) {
// Receive Buffer Not Empty: Read data and store in RX buffer
if (rxBuffer_.availableForStore() > 0) {
rxBuffer_.store_char(i2c_.receive_data());
} else {
// RX buffer too small
}
} else if (i2c_.get_interrupt_flag(i2c::Interrupt_Flags::INTR_FLAG_STPDET)) {
i2c_.set_enable(true);
// On stop detection, call the slave receive callback
if (!i2c_.get_flag(i2c::Status_Flags::FLAG_TRS)) {
if (onReceiveCallback) {
onReceiveCallback(rxBuffer_.available());
}
}
rxBuffer_.clear();
}
}
} // namespace arduino
#ifdef BOARD_USE_I2C0
arduino::TwoWire& Wire = arduino::TwoWire::get_instance(i2c::I2C_Base::I2C0_BASE);
#endif
#ifdef BOARD_USE_I2C1
arduino::TwoWire& Wire1 = arduino::TwoWire::get_instance(i2c::I2C_Base::I2C1_BASE);
#endif
extern "C" {
void I2C0_EV_IRQHandler(void) {
auto& instance = arduino::TwoWire::get_instance(i2c::I2C_Base::I2C0_BASE);
instance.interruptHandler();
}
void I2C0_ER_IRQHandler(void) {
auto& instance = arduino::TwoWire::get_instance(i2c::I2C_Base::I2C0_BASE);