Skip to content

Commit 066923b

Browse files
authored
New version that is better tested, still test version
This version has more testing and some refinements for handling of timeout It still has some printouts and comments that might be useful for others review and testing When reviewed and tested, I will remove this I have also packaged the same files as an independent library that is easy to download and install as a library This can be found at: https://github.com/HanzHager/Wire
1 parent 7a5325d commit 066923b

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

libraries/Wire/Wire.cpp

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ void TwoWire::WireSCIMasterCallback(i2c_master_callback_args_t *arg) {
7272
ptr->setBusStatus(WIRE_STATUS_TX_COMPLETED);
7373
}
7474
}
75-
7675
}
7776

7877
/* -------------------------------------------------------------------------- */
7978
void TwoWire::WireMasterCallback(i2c_master_callback_args_t *arg) {
8079
/* -------------------------------------------------------------------------- */
8180
/* +++++ MASTER I2C not SCI Callback ++++++ */
81+
8282
i2c_master_cfg_t *cfg = (i2c_master_cfg_t *)arg->p_context;
8383

8484
TwoWire *ptr = nullptr;
@@ -212,8 +212,6 @@ TwoWire::TwoWire(int scl, int sda, WireAddressMode_t am /*= ADDRESS_MODE_7_BITS*
212212
s_i2c_cfg.txi_irq = FSP_INVALID_VECTOR;
213213
s_i2c_cfg.tei_irq = FSP_INVALID_VECTOR;
214214
s_i2c_cfg.tei_irq = FSP_INVALID_VECTOR;
215-
216-
217215
}
218216

219217
/* -------------------------------------------------------------------------- */
@@ -336,7 +334,7 @@ void TwoWire::_begin(void) {
336334
m_i2c_cfg.p_callback = WireMasterCallback;
337335

338336
m_i2c_extend.timeout_mode = IIC_MASTER_TIMEOUT_MODE_SHORT;
339-
m_i2c_extend.timeout_scl_low = IIC_MASTER_TIMEOUT_SCL_LOW_DISABLED;
337+
m_i2c_extend.timeout_scl_low = IIC_MASTER_TIMEOUT_SCL_LOW_ENABLED;
340338
}
341339

342340
m_i2c_cfg.channel = channel;
@@ -483,7 +481,7 @@ uint8_t TwoWire::read_from(uint8_t address, uint8_t* data, uint8_t length, uint3
483481
}
484482
if(err == FSP_SUCCESS) {
485483
if(m_read != nullptr) {
486-
bus_status = WIRE_STATUS_UNSET;
484+
setBusStatus(WIRE_STATUS_UNSET);
487485
err = m_read(&m_i2c_ctrl,data,length,!sendStop);
488486
}
489487
}
@@ -501,7 +499,7 @@ uint8_t TwoWire::read_from(uint8_t address, uint8_t* data, uint8_t length, uint3
501499
if(bus_status == WIRE_STATUS_RX_COMPLETED) {
502500
return length;
503501
}
504-
502+
505503
return 0; /* ???????? return value ??????? */
506504
}
507505

@@ -512,15 +510,30 @@ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, uint32
512510
fsp_err_t err = FSP_ERR_ASSERTION;
513511
if(init_ok) {
514512
if(m_setSlaveAdd != nullptr) {
513+
setBusStatus(WIRE_STATUS_UNSET);
515514
err = m_setSlaveAdd(&m_i2c_ctrl, address, m_i2c_cfg.addr_mode);
516515
}
517-
if(err == FSP_SUCCESS) {
516+
if((err == FSP_SUCCESS) && (bus_status != WIRE_STATUS_TRANSACTION_ABORTED)) {
517+
518518
if(m_write != nullptr) {
519-
bus_status = WIRE_STATUS_UNSET;
519+
setBusStatus(WIRE_STATUS_UNSET);
520520
err = m_write(&m_i2c_ctrl,data,length,!sendStop);
521521
}
522-
}
522+
if (err==FSP_ERR_INVALID_SIZE) {
523+
rv = END_TX_DATA_TOO_LONG;
524+
Serial.println(F("Invalid size when trying to write"));
525+
}
526+
527+
} else // No FSP_SUCCESS in m_setSlaveAdd or WIRE_STATUS_TRANSACTION_ABORTED
528+
if (err == FSP_ERR_IN_USE) {
529+
Serial.println(F("An I2C Transaction is in progress. when setting slave address"));
530+
} else
531+
if (bus_status == WIRE_STATUS_TRANSACTION_ABORTED){
532+
rv = END_TX_NACK_ON_ADD;
533+
Serial.println(F("Transaction aborted -> NACK on ADDR"));
534+
}
523535

536+
// If FSP_SUCCESS, wait for change in bus_status or timeout
524537
uint32_t const start = micros();
525538
while (((timeout_us == 0ul) || ((micros() - start) < timeout_us)) &&
526539
bus_status == WIRE_STATUS_UNSET && err == FSP_SUCCESS) {
@@ -531,6 +544,7 @@ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, uint32
531544
}
532545
else if(data_too_long) {
533546
rv = END_TX_DATA_TOO_LONG;
547+
Serial.println(F("Trying to write more than II2C_BUFFER_LENGTH, buffer truncated before written"));
534548
}
535549
else if(bus_status == WIRE_STATUS_UNSET) {
536550
rv = END_TX_TIMEOUT;
@@ -539,7 +553,8 @@ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, uint32
539553
/* as far as I know is impossible to distinguish between NACK on ADDRESS and
540554
NACK on DATA */
541555
else if(bus_status == WIRE_STATUS_TRANSACTION_ABORTED) {
542-
rv = END_TX_NACK_ON_ADD;
556+
if (length==0) rv = END_TX_NACK_ON_ADD;
557+
if (rv != END_TX_NACK_ON_ADD) rv = END_TX_NACK_ON_DATA;
543558
}
544559
}
545560
else {
@@ -593,13 +608,13 @@ void TwoWire::setClock(uint32_t freq) {
593608
m_i2c_extend.clock_settings.brh_value = 15;
594609
m_i2c_extend.clock_settings.cks_value = 0 + clock_divisor;
595610
break;
596-
#if BSP_FEATURE_IIC_FAST_MODE_PLUS
597611
case I2C_MASTER_RATE_FASTPLUS:
612+
#if BSP_FEATURE_IIC_FAST_MODE_PLUS
598613
m_i2c_extend.clock_settings.brl_value = 6;
599614
m_i2c_extend.clock_settings.brh_value = 5;
600615
m_i2c_extend.clock_settings.cks_value = 0;
601-
break;
602616
#endif
617+
break;
603618
}
604619
}
605620
}
@@ -640,9 +655,7 @@ void TwoWire::setClock(uint32_t freq) {
640655
* @param timeout a timeout value in microseconds, if zero then timeout checking is disabled
641656
* @param reset_with_timeout if true then I2C interface will be automatically reset on timeout
642657
* if false then I2C interface will not be reset on timeout
643-
644658
*/
645-
646659
/* -------------------------------------------------------------------------- */
647660
void TwoWire::setWireTimeout(uint32_t timeout, bool reset_with_timeout){
648661
/* -------------------------------------------------------------------------- */
@@ -680,28 +693,31 @@ void TwoWire::clearWireTimeoutFlag(void){
680693
void TwoWire::handleTimeout(bool reset){
681694
/* -------------------------------------------------------------------------- */
682695
timed_out_flag = true;
696+
Serial.println(F("Handling TwoWire::handleTimeout()"));
683697

684698
if (reset) { //TBD; What do we do here? like fixHungWire()?
685699
// TBD, Is this the way to go to reset the bus?
686700
// Do we need more to handle devices that hangs the bus?
701+
Serial.print(F("with reset Abort result: "));
687702
if(m_abort != nullptr) {
688-
bus_status = WIRE_STATUS_UNSET;
703+
//setBusStatus(WIRE_STATUS_TRANSACTION_ABORTED);
689704
fsp_err_t err = m_abort(&m_i2c_ctrl);
705+
Serial.println(err);
690706
}
691707
// TDB, Is this the right way to get back after reset?
692-
//if(m_open != nullptr) {
693-
// fsp_err_t err = m_open(&m_i2c_ctrl,&m_i2c_cfg);
694-
// if(FSP_SUCCESS == err) {
695-
// init_ok &= true;
696-
// }
697-
//}
708+
if(m_open != nullptr) {
709+
fsp_err_t err = m_open(&m_i2c_ctrl,&m_i2c_cfg);
710+
if(FSP_SUCCESS == err) {
711+
init_ok &= true;
712+
}
713+
Serial.print(F(" Open result: "));
714+
Serial.println(err);
715+
}
716+
// Is it neccesarry to do the open after the abort?
717+
// Is that more to be done after the abort to get back to same settings
698718
}
699719
}
700720

701-
702-
703-
704-
705721
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
706722
* TRANSMISSION BEGIN
707723
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

0 commit comments

Comments
 (0)