From 5ab734afde2630e0ae0315d906b8fe20cadf3efc Mon Sep 17 00:00:00 2001 From: maidnl Date: Fri, 10 Nov 2023 11:43:08 +0100 Subject: [PATCH 1/5] fix for switching role (master <-> slave) in I2C communication on the same device (Fix to be cleaned up --> too many debug comment) --- cores/arduino/IRQManager.cpp | 51 ++++++++++-- cores/arduino/IRQManager.h | 4 + libraries/Wire/Wire.cpp | 152 ++++++++++++++++++++++++----------- libraries/Wire/Wire.h | 5 +- 4 files changed, 161 insertions(+), 51 deletions(-) diff --git a/cores/arduino/IRQManager.cpp b/cores/arduino/IRQManager.cpp index 73036ffe..9212e78f 100644 --- a/cores/arduino/IRQManager.cpp +++ b/cores/arduino/IRQManager.cpp @@ -25,8 +25,11 @@ #define CANFD_PRIORITY 12 #define FIRST_INT_SLOT_FREE 0 -IRQManager::IRQManager() : last_interrupt_index{0} { - +IRQManager::IRQManager() : last_interrupt_index{0} , + rcfg_I2C_interrupt_index_txi(-1), + rcfg_I2C_interrupt_index_rxi(-1), + rcfg_I2C_interrupt_index_tei(-1), + rcfg_I2C_interrupt_index_eri(-1) { } IRQManager::~IRQManager() { @@ -562,35 +565,54 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) { i2c_master_cfg_t *mcfg = (i2c_master_cfg_t *)p_cfg->cfg; uint8_t hw_channel = p_cfg->hw_channel; mcfg->ipl = I2C_MASTER_PRIORITY; - if (mcfg->txi_irq == FSP_INVALID_VECTOR) { + if (rcfg_I2C_interrupt_index_txi == -1) { /* TX interrupt */ mcfg->txi_irq = (IRQn_Type)last_interrupt_index; *(irq_ptr + last_interrupt_index) = (uint32_t)iic_master_txi_isr; set_iic_tx_link_event(last_interrupt_index, hw_channel); R_BSP_IrqCfg((IRQn_Type)last_interrupt_index, I2C_MASTER_PRIORITY, mcfg); + rcfg_I2C_interrupt_index_txi = last_interrupt_index; last_interrupt_index++; + } else { + *(irq_ptr + rcfg_I2C_interrupt_index_txi) = (uint32_t)iic_master_txi_isr; + } + if (rcfg_I2C_interrupt_index_rxi == -1) { /* RX interrupt */ mcfg->rxi_irq = (IRQn_Type)last_interrupt_index; *(irq_ptr + last_interrupt_index) = (uint32_t)iic_master_rxi_isr; set_iic_rx_link_event(last_interrupt_index, hw_channel); R_BSP_IrqCfg((IRQn_Type)last_interrupt_index, I2C_MASTER_PRIORITY, mcfg); + rcfg_I2C_interrupt_index_rxi = last_interrupt_index; last_interrupt_index++; + } else { + *(irq_ptr + rcfg_I2C_interrupt_index_rxi) = (uint32_t)iic_master_rxi_isr; + } + if (rcfg_I2C_interrupt_index_tei == -1) { /* TX ERROR interrupt */ mcfg->tei_irq = (IRQn_Type)last_interrupt_index; *(irq_ptr + last_interrupt_index) = (uint32_t)iic_master_tei_isr; set_iic_tei_link_event(last_interrupt_index, hw_channel); R_BSP_IrqCfg((IRQn_Type)last_interrupt_index, I2C_MASTER_PRIORITY, mcfg); + rcfg_I2C_interrupt_index_tei = last_interrupt_index; last_interrupt_index++; + } else { + *(irq_ptr + rcfg_I2C_interrupt_index_tei) = (uint32_t)iic_master_tei_isr; + } + if (rcfg_I2C_interrupt_index_eri == -1) { /* RX ERROR interrupt */ mcfg->eri_irq = (IRQn_Type)last_interrupt_index; *(irq_ptr + last_interrupt_index) = (uint32_t)iic_master_eri_isr; set_iic_eri_link_event(last_interrupt_index, hw_channel); R_BSP_IrqCfg((IRQn_Type)last_interrupt_index, I2C_MASTER_PRIORITY, mcfg); + rcfg_I2C_interrupt_index_eri = last_interrupt_index; last_interrupt_index++; + } else { + *(irq_ptr + rcfg_I2C_interrupt_index_eri) = (uint32_t)iic_master_eri_isr; } + R_BSP_IrqEnable (mcfg->txi_irq); R_BSP_IrqEnable (mcfg->rxi_irq); R_BSP_IrqEnable (mcfg->tei_irq); @@ -644,31 +666,50 @@ bool IRQManager::addPeripheral(Peripheral_t p, void *cfg) { i2c_slave_cfg_t *p_cfg = (i2c_slave_cfg_t *)cfg; p_cfg->ipl = I2C_SLAVE_PRIORITY; p_cfg->eri_ipl = I2C_SLAVE_PRIORITY; - if (p_cfg->txi_irq == FSP_INVALID_VECTOR) { + if (rcfg_I2C_interrupt_index_txi == -1) { /* TX interrupt */ p_cfg->txi_irq = (IRQn_Type)last_interrupt_index; *(irq_ptr + last_interrupt_index) = (uint32_t)iic_slave_txi_isr; set_iic_tx_link_event(last_interrupt_index, p_cfg->channel); + rcfg_I2C_interrupt_index_txi = last_interrupt_index; last_interrupt_index++; - + } else { + *(irq_ptr + rcfg_I2C_interrupt_index_txi) = (uint32_t)iic_slave_txi_isr; + } + + if (rcfg_I2C_interrupt_index_rxi == -1) { /* RX interrupt */ p_cfg->rxi_irq = (IRQn_Type)last_interrupt_index; *(irq_ptr + last_interrupt_index) = (uint32_t)iic_slave_rxi_isr; set_iic_rx_link_event(last_interrupt_index, p_cfg->channel); + rcfg_I2C_interrupt_index_rxi = last_interrupt_index; last_interrupt_index++; + } else { + *(irq_ptr + rcfg_I2C_interrupt_index_rxi) = (uint32_t)iic_slave_rxi_isr; + } + if (rcfg_I2C_interrupt_index_tei == -1) { /* TEI interrupt */ p_cfg->tei_irq = (IRQn_Type)last_interrupt_index; *(irq_ptr + last_interrupt_index) = (uint32_t)iic_slave_tei_isr; set_iic_tei_link_event(last_interrupt_index, p_cfg->channel); + rcfg_I2C_interrupt_index_tei = last_interrupt_index; last_interrupt_index++; + } else { + *(irq_ptr + rcfg_I2C_interrupt_index_tei) = (uint32_t)iic_slave_tei_isr; + } + if (rcfg_I2C_interrupt_index_eri == -1) { /* ERI interrupt */ p_cfg->eri_irq = (IRQn_Type)last_interrupt_index; *(irq_ptr + last_interrupt_index) = (uint32_t)iic_slave_eri_isr; set_iic_eri_link_event(last_interrupt_index, p_cfg->channel); + rcfg_I2C_interrupt_index_eri = last_interrupt_index; last_interrupt_index++; + } else { + *(irq_ptr + rcfg_I2C_interrupt_index_eri) = (uint32_t)iic_slave_eri_isr; } + R_BSP_IrqEnable (p_cfg->txi_irq); R_BSP_IrqEnable (p_cfg->rxi_irq); R_BSP_IrqEnable (p_cfg->tei_irq); diff --git a/cores/arduino/IRQManager.h b/cores/arduino/IRQManager.h index 6d7dff38..2f0f5925 100644 --- a/cores/arduino/IRQManager.h +++ b/cores/arduino/IRQManager.h @@ -223,6 +223,10 @@ class IRQManager { ~IRQManager(); private: + int rcfg_I2C_interrupt_index_txi; + int rcfg_I2C_interrupt_index_rxi; + int rcfg_I2C_interrupt_index_tei; + int rcfg_I2C_interrupt_index_eri; size_t last_interrupt_index; bool set_adc_end_link_event(int li, int ch); bool set_adc_end_b_link_event(int li, int ch); diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index 778f0a93..e1b25c15 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -22,6 +22,7 @@ Version 2022 for Renesas RA4 by Daniele Aimo (d.aimo@arduino.cc) */ +#include "r_iic_slave.h" extern "C" { #include #include @@ -252,8 +253,9 @@ bool TwoWire::cfg_pins(int max_index) { ioport_sda = USE_SCI_EVEN_CFG(cfg_sda) ? IOPORT_PERIPHERAL_SCI0_2_4_6_8 : IOPORT_PERIPHERAL_SCI1_3_5_7_9; ioport_scl = USE_SCI_EVEN_CFG(cfg_scl) ? IOPORT_PERIPHERAL_SCI0_2_4_6_8 : IOPORT_PERIPHERAL_SCI1_3_5_7_9; - R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[sda_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_PERIPHERAL_PIN | ioport_sda)); - R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[scl_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_PERIPHERAL_PIN | ioport_scl)); + fsp_err_t err = R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[sda_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_PERIPHERAL_PIN | ioport_sda)); + + err = R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[scl_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_PERIPHERAL_PIN | ioport_scl)); } else { @@ -264,28 +266,42 @@ bool TwoWire::cfg_pins(int max_index) { ioport_sda = IOPORT_PERIPHERAL_IIC; ioport_scl = IOPORT_PERIPHERAL_IIC; - R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[sda_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_DRIVE_MID | IOPORT_CFG_PERIPHERAL_PIN | ioport_sda)); - R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[scl_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_DRIVE_MID | IOPORT_CFG_PERIPHERAL_PIN | ioport_scl)); + fsp_err_t err = R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[sda_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_DRIVE_MID | IOPORT_CFG_PERIPHERAL_PIN | ioport_sda)); + Serial.print("pin sda "); + Serial.println(err); + + err = R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[scl_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_DRIVE_MID | IOPORT_CFG_PERIPHERAL_PIN | ioport_scl)); + Serial.print("pin scl "); + Serial.println(err); } return true; } +void TwoWire::begin() { + Serial.println("MASTER BEGIN"); + is_master = true; + _begin(); +} + + /* -------------------------------------------------------------------------- */ -void TwoWire::begin(void) { +void TwoWire::_begin(void) { /* -------------------------------------------------------------------------- */ init_ok = true; int max_index = PINS_COUNT; init_ok &= cfg_pins(max_index); + Serial.println("intenal BEGIN"); + if(init_ok) { /* ----------------------------------- ->>>>> MASTER initialization * ----------------------------------- */ if(is_master) { - + Serial.println("intenal BEGIN as MASTER"); setClock(I2C_MASTER_RATE_STANDARD); if(is_sci) { @@ -304,6 +320,7 @@ void TwoWire::begin(void) { m_i2c_cfg.p_callback = WireSCIMasterCallback; } else { + Serial.println("intenal BEGIN as MASTER"); TwoWire::g_I2CWires[channel] = this; m_open = R_IIC_MASTER_Open; @@ -339,9 +356,11 @@ void TwoWire::begin(void) { else { /* a slave device cannot be instatiated on SCI peripheral */ if(is_sci) { + Serial.println("intenal BEGIN !!!! Slave SCI !!!!"); init_ok = false; return; } + Serial.println("intenal BEGIN as SLAVE"); TwoWire::g_I2CWires[channel] = this; s_open = R_IIC_SLAVE_Open; @@ -367,37 +386,71 @@ void TwoWire::begin(void) { init_ok = false; return; } - + if(is_master) { - I2CIrqMasterReq_t irq_req; - irq_req.ctrl = &m_i2c_ctrl; - irq_req.cfg = &m_i2c_cfg; - /* see note in the cfg_pins - the IRQ manager need to know the HW channel that in case of SCI - peripheral is not the one in the cfg structure but the one in - the Wire channel, so copy it in the request */ - irq_req.hw_channel = channel; - if(is_sci) { - init_ok &= IRQManager::getInstance().addPeripheral(IRQ_SCI_I2C_MASTER,&irq_req); - } - else { - init_ok &= IRQManager::getInstance().addPeripheral(IRQ_I2C_MASTER,&irq_req); - } - if(FSP_SUCCESS == m_open(&m_i2c_ctrl,&m_i2c_cfg)) { - init_ok &= true; - } - else { - init_ok = false; - } - } - else { - init_ok &= IRQManager::getInstance().addPeripheral(IRQ_I2C_SLAVE,&s_i2c_cfg); - if(FSP_SUCCESS == s_open(&s_i2c_ctrl,&s_i2c_cfg)) { - init_ok &= true; - } - else { - init_ok = false; - } + /* master but IRQ has already been configured as slave */ + if(slave_irq_added) { + //s_i2c_ctrl.p_reg->ICCR1 &= ~(1 << 7); /* Performing manual initial reset if the peripheral is already initialized */ + //s_i2c_ctrl.p_reg->ICCR1 |= (1 << 6); /* Performing manual initial reset if the peripheral is already initialized */ + //s_i2c_ctrl.p_reg->ICCR1 |= (1 << 7); /* Performing manual initial reset if the peripheral is already initialized */ + Serial.println("COPY MASTER IRQ from SLAVE"); + m_i2c_cfg.txi_irq = s_i2c_cfg.txi_irq; + m_i2c_cfg.rxi_irq = s_i2c_cfg.rxi_irq; + m_i2c_cfg.tei_irq = s_i2c_cfg.tei_irq; + m_i2c_cfg.eri_irq = s_i2c_cfg.eri_irq; + } + + + I2CIrqMasterReq_t irq_req; + irq_req.ctrl = &m_i2c_ctrl; + irq_req.cfg = &m_i2c_cfg; + /* see note in the cfg_pins + the IRQ manager need to know the HW channel that in case of SCI + peripheral is not the one in the cfg structure but the one in + the Wire channel, so copy it in the request */ + irq_req.hw_channel = channel; + if(is_sci) { + Serial.println("CONFIGURED MASTER IRQ as SCI"); + init_ok &= IRQManager::getInstance().addPeripheral(IRQ_SCI_I2C_MASTER,&irq_req); + } + else { + Serial.println("CONFIGURED MASTER IRQ as IIC"); + init_ok &= IRQManager::getInstance().addPeripheral(IRQ_I2C_MASTER,&irq_req); + } + master_irq_added = true; + + + if(FSP_SUCCESS == m_open(&m_i2c_ctrl,&m_i2c_cfg)) { + Serial.println("I2C master open OK"); + init_ok &= true; + } else { + Serial.println("I2C master open WRONG"); + init_ok = false; + } + + } else { + if(master_irq_added) { + Serial.println("COPY SLAVE IRQ from MASTER"); + //m_i2c_ctrl.p_reg->ICCR1 &= ~(1 << 7); /* Performing manual initial reset if the peripheral is already initialized */ + //m_i2c_ctrl.p_reg->ICCR1 |= (1 << 6); /* Performing manual initial reset if the peripheral is already initialized */ + //m_i2c_ctrl.p_reg->ICCR1 |= (1 << 7); /* Performing manual initial reset if the peripheral is already initialized */ + s_i2c_cfg.txi_irq = m_i2c_cfg.txi_irq; + s_i2c_cfg.rxi_irq = m_i2c_cfg.rxi_irq; + s_i2c_cfg.tei_irq = m_i2c_cfg.tei_irq; + s_i2c_cfg.eri_irq = m_i2c_cfg.eri_irq; + } + + Serial.println("CONFIGURING SLAVE IRQ"); + init_ok &= IRQManager::getInstance().addPeripheral(IRQ_I2C_SLAVE,&s_i2c_cfg); + slave_irq_added = true; + + if(FSP_SUCCESS == s_open(&s_i2c_ctrl,&s_i2c_cfg)) { + Serial.println("I2C slave open OK"); + init_ok &= true; + } else { + Serial.println("I2C slave open WRONG"); + init_ok = false; + } } } @@ -407,7 +460,8 @@ void TwoWire::begin(uint16_t address) { is_master = false; slave_address = address; /* Address is set inside begin() using slave_address member variable */ - begin(); + Serial.println("SLAVE BEGIN"); + _begin(); } @@ -431,15 +485,24 @@ void TwoWire::begin(uint8_t address) { void TwoWire::end(void) { /* -------------------------------------------------------------------------- */ - if(init_ok) { - if(is_master) { - if(m_close != nullptr) { - m_close(&m_i2c_ctrl); + if (init_ok) { + if (is_master) { + if (m_close != nullptr) { + m_close(&m_i2c_ctrl); + Serial.println("--- DISABLING MASTER IRQ"); + R_BSP_IrqDisable(m_i2c_cfg.txi_irq); + R_BSP_IrqDisable(m_i2c_cfg.rxi_irq); + R_BSP_IrqDisable(m_i2c_cfg.tei_irq); + R_BSP_IrqDisable(m_i2c_cfg.eri_irq); } - } - else { - if(s_close != nullptr) { + } else { + if (s_close != nullptr) { s_close(&s_i2c_ctrl); + Serial.println("--- DISABLING SLAVE IRQ"); + R_BSP_IrqDisable(s_i2c_cfg.txi_irq); + R_BSP_IrqDisable(s_i2c_cfg.rxi_irq); + R_BSP_IrqDisable(s_i2c_cfg.tei_irq); + R_BSP_IrqDisable(s_i2c_cfg.eri_irq); } } } @@ -447,7 +510,6 @@ void TwoWire::end(void) { } - /* -------------------------------------------------------------------------- */ uint8_t TwoWire::read_from(uint8_t address, uint8_t* data, uint8_t length, unsigned int timeout_ms, bool sendStop) { /* -------------------------------------------------------------------------- */ diff --git a/libraries/Wire/Wire.h b/libraries/Wire/Wire.h index 6b4eb562..be1d8e0c 100644 --- a/libraries/Wire/Wire.h +++ b/libraries/Wire/Wire.h @@ -151,6 +151,9 @@ class TwoWire : public arduino::HardwareI2C { } private: + bool master_irq_added; + bool slave_irq_added; + void _begin(); static TwoWire *g_SCIWires[TWOWIRE_MAX_I2C_CHANNELS]; static TwoWire *g_I2CWires[TWOWIRE_MAX_SCI_CHANNELS]; @@ -233,4 +236,4 @@ extern TwoWire Wire3; #endif #endif -#endif \ No newline at end of file +#endif From 6075e89ba7398ce7cfee71942ab82cbaa9b6033c Mon Sep 17 00:00:00 2001 From: maidnl Date: Fri, 10 Nov 2023 11:46:16 +0100 Subject: [PATCH 2/5] updated gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 216b0ba9..bef9554f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ .idea +compile_commands.json +.clangd +.cache/ cores/arduino/mydebug.cpp libraries/Storage/.development cores/arduino/mydebug.cpp.donotuse From 20e866dc9b3fe5f3a27b743e20d9a010a597ac64 Mon Sep 17 00:00:00 2001 From: maidnl Date: Fri, 10 Nov 2023 12:33:41 +0100 Subject: [PATCH 3/5] cleaned up code from debug logs --- libraries/Wire/Wire.cpp | 92 ++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 60 deletions(-) diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index e1b25c15..76fd4280 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -267,24 +267,22 @@ bool TwoWire::cfg_pins(int max_index) { ioport_scl = IOPORT_PERIPHERAL_IIC; fsp_err_t err = R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[sda_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_DRIVE_MID | IOPORT_CFG_PERIPHERAL_PIN | ioport_sda)); - Serial.print("pin sda "); - Serial.println(err); err = R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[scl_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_DRIVE_MID | IOPORT_CFG_PERIPHERAL_PIN | ioport_scl)); - Serial.print("pin scl "); - Serial.println(err); } return true; } +/* -------------------------------------------------------------------------- */ void TwoWire::begin() { - Serial.println("MASTER BEGIN"); +/* -------------------------------------------------------------------------- */ is_master = true; _begin(); } - +/* private version of begin so that it can be called both for slave and master + * initialization */ /* -------------------------------------------------------------------------- */ void TwoWire::_begin(void) { /* -------------------------------------------------------------------------- */ @@ -293,15 +291,12 @@ void TwoWire::_begin(void) { init_ok &= cfg_pins(max_index); - Serial.println("intenal BEGIN"); - if(init_ok) { /* ----------------------------------- ->>>>> MASTER initialization * ----------------------------------- */ if(is_master) { - Serial.println("intenal BEGIN as MASTER"); setClock(I2C_MASTER_RATE_STANDARD); if(is_sci) { @@ -320,7 +315,6 @@ void TwoWire::_begin(void) { m_i2c_cfg.p_callback = WireSCIMasterCallback; } else { - Serial.println("intenal BEGIN as MASTER"); TwoWire::g_I2CWires[channel] = this; m_open = R_IIC_MASTER_Open; @@ -356,11 +350,10 @@ void TwoWire::_begin(void) { else { /* a slave device cannot be instatiated on SCI peripheral */ if(is_sci) { - Serial.println("intenal BEGIN !!!! Slave SCI !!!!"); init_ok = false; return; } - Serial.println("intenal BEGIN as SLAVE"); + TwoWire::g_I2CWires[channel] = this; s_open = R_IIC_SLAVE_Open; @@ -390,17 +383,12 @@ void TwoWire::_begin(void) { if(is_master) { /* master but IRQ has already been configured as slave */ if(slave_irq_added) { - //s_i2c_ctrl.p_reg->ICCR1 &= ~(1 << 7); /* Performing manual initial reset if the peripheral is already initialized */ - //s_i2c_ctrl.p_reg->ICCR1 |= (1 << 6); /* Performing manual initial reset if the peripheral is already initialized */ - //s_i2c_ctrl.p_reg->ICCR1 |= (1 << 7); /* Performing manual initial reset if the peripheral is already initialized */ - Serial.println("COPY MASTER IRQ from SLAVE"); m_i2c_cfg.txi_irq = s_i2c_cfg.txi_irq; m_i2c_cfg.rxi_irq = s_i2c_cfg.rxi_irq; m_i2c_cfg.tei_irq = s_i2c_cfg.tei_irq; m_i2c_cfg.eri_irq = s_i2c_cfg.eri_irq; } - I2CIrqMasterReq_t irq_req; irq_req.ctrl = &m_i2c_ctrl; irq_req.cfg = &m_i2c_cfg; @@ -410,45 +398,34 @@ void TwoWire::_begin(void) { the Wire channel, so copy it in the request */ irq_req.hw_channel = channel; if(is_sci) { - Serial.println("CONFIGURED MASTER IRQ as SCI"); init_ok &= IRQManager::getInstance().addPeripheral(IRQ_SCI_I2C_MASTER,&irq_req); } else { - Serial.println("CONFIGURED MASTER IRQ as IIC"); init_ok &= IRQManager::getInstance().addPeripheral(IRQ_I2C_MASTER,&irq_req); } master_irq_added = true; if(FSP_SUCCESS == m_open(&m_i2c_ctrl,&m_i2c_cfg)) { - Serial.println("I2C master open OK"); init_ok &= true; } else { - Serial.println("I2C master open WRONG"); init_ok = false; } } else { if(master_irq_added) { - Serial.println("COPY SLAVE IRQ from MASTER"); - //m_i2c_ctrl.p_reg->ICCR1 &= ~(1 << 7); /* Performing manual initial reset if the peripheral is already initialized */ - //m_i2c_ctrl.p_reg->ICCR1 |= (1 << 6); /* Performing manual initial reset if the peripheral is already initialized */ - //m_i2c_ctrl.p_reg->ICCR1 |= (1 << 7); /* Performing manual initial reset if the peripheral is already initialized */ - s_i2c_cfg.txi_irq = m_i2c_cfg.txi_irq; + s_i2c_cfg.txi_irq = m_i2c_cfg.txi_irq; s_i2c_cfg.rxi_irq = m_i2c_cfg.rxi_irq; s_i2c_cfg.tei_irq = m_i2c_cfg.tei_irq; - s_i2c_cfg.eri_irq = m_i2c_cfg.eri_irq; + s_i2c_cfg.eri_irq = m_i2c_cfg.eri_irq; } - Serial.println("CONFIGURING SLAVE IRQ"); init_ok &= IRQManager::getInstance().addPeripheral(IRQ_I2C_SLAVE,&s_i2c_cfg); slave_irq_added = true; if(FSP_SUCCESS == s_open(&s_i2c_ctrl,&s_i2c_cfg)) { - Serial.println("I2C slave open OK"); init_ok &= true; } else { - Serial.println("I2C slave open WRONG"); init_ok = false; } } @@ -456,18 +433,17 @@ void TwoWire::_begin(void) { /* -------------------------------------------------------------------------- */ void TwoWire::begin(uint16_t address) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ is_master = false; slave_address = address; /* Address is set inside begin() using slave_address member variable */ - Serial.println("SLAVE BEGIN"); _begin(); } /* -------------------------------------------------------------------------- */ void TwoWire::begin(int address) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ is_master = false; slave_address = (uint16_t)address; begin((uint16_t)address); @@ -475,7 +451,7 @@ void TwoWire::begin(int address) { /* -------------------------------------------------------------------------- */ void TwoWire::begin(uint8_t address) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ is_master = false; slave_address = (uint16_t)address; begin((uint16_t)address); @@ -483,13 +459,12 @@ void TwoWire::begin(uint8_t address) { /* -------------------------------------------------------------------------- */ void TwoWire::end(void) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ if (init_ok) { if (is_master) { if (m_close != nullptr) { m_close(&m_i2c_ctrl); - Serial.println("--- DISABLING MASTER IRQ"); R_BSP_IrqDisable(m_i2c_cfg.txi_irq); R_BSP_IrqDisable(m_i2c_cfg.rxi_irq); R_BSP_IrqDisable(m_i2c_cfg.tei_irq); @@ -498,7 +473,6 @@ void TwoWire::end(void) { } else { if (s_close != nullptr) { s_close(&s_i2c_ctrl); - Serial.println("--- DISABLING SLAVE IRQ"); R_BSP_IrqDisable(s_i2c_cfg.txi_irq); R_BSP_IrqDisable(s_i2c_cfg.rxi_irq); R_BSP_IrqDisable(s_i2c_cfg.tei_irq); @@ -512,7 +486,7 @@ void TwoWire::end(void) { /* -------------------------------------------------------------------------- */ uint8_t TwoWire::read_from(uint8_t address, uint8_t* data, uint8_t length, unsigned int timeout_ms, bool sendStop) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ /* ??? does this function make sense only for MASTER ???? */ fsp_err_t err = FSP_ERR_ASSERTION; @@ -539,9 +513,9 @@ uint8_t TwoWire::read_from(uint8_t address, uint8_t* data, uint8_t length, unsig return 0; /* ???????? return value ??????? */ } -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, unsigned int timeout_ms, bool sendStop) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ uint8_t rv = END_TX_OK; fsp_err_t err = FSP_ERR_ASSERTION; if(init_ok) { @@ -582,9 +556,9 @@ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, unsign return rv; } -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ void TwoWire::setClock(uint32_t freq) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ if(init_ok && is_master) { if(m_close != nullptr) { m_close(&m_i2c_ctrl); @@ -654,7 +628,7 @@ void TwoWire::setClock(uint32_t freq) { /* -------------------------------------------------------------------------- */ void TwoWire::beginTransmission(uint32_t address) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ if (init_ok) { data_too_long = false; master_tx_address = address; @@ -671,13 +645,13 @@ void TwoWire::beginTransmission(uint16_t address) { /* -------------------------------------------------------------------------- */ void TwoWire::beginTransmission(uint8_t address){ -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ beginTransmission((uint32_t)address); } /* -------------------------------------------------------------------------- */ void TwoWire::beginTransmission(int address) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ beginTransmission((uint32_t)address); } @@ -687,7 +661,7 @@ void TwoWire::beginTransmission(int address) { /* -------------------------------------------------------------------------- */ uint8_t TwoWire::endTransmission(bool sendStop) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ uint8_t ret = write_to(master_tx_address, tx_buffer, tx_index, timeout, sendStop); transmission_begun = false; return ret; @@ -695,7 +669,7 @@ uint8_t TwoWire::endTransmission(bool sendStop) { /* -------------------------------------------------------------------------- */ uint8_t TwoWire::endTransmission(void) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ return endTransmission(true); } @@ -705,7 +679,7 @@ uint8_t TwoWire::endTransmission(void) { /* -------------------------------------------------------------------------- */ size_t TwoWire::requestFrom(uint8_t address, size_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ if(init_ok) { if (isize > 0) { @@ -747,13 +721,13 @@ size_t TwoWire::requestFrom(uint8_t address, size_t quantity, uint32_t iaddress, /* -------------------------------------------------------------------------- */ size_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool sendStop) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ return requestFrom((uint8_t)address, quantity, (uint32_t)0, (uint8_t)0, sendStop); } /* -------------------------------------------------------------------------- */ size_t TwoWire::requestFrom(uint8_t address, size_t quantity) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ return requestFrom((uint8_t)address, quantity, true); } @@ -766,7 +740,7 @@ size_t TwoWire::requestFrom(uint8_t address, size_t quantity) { // or after beginTransmission(address) /* -------------------------------------------------------------------------- */ size_t TwoWire::write(uint8_t data) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ if(init_ok) { if(is_master) { if(transmission_begun) { @@ -795,7 +769,7 @@ size_t TwoWire::write(uint8_t data) { // or after beginTransmission(address) /* -------------------------------------------------------------------------- */ size_t TwoWire::write(const uint8_t *data, size_t quantity) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ if(init_ok) { if(is_master) { // in master transmitter mode @@ -819,14 +793,14 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity) { // sets function called on slave write /* -------------------------------------------------------------------------- */ void TwoWire::onReceive( I2C_onRxCallback_f f ) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ rx_callback = f; } // sets function called on slave read /* -------------------------------------------------------------------------- */ void TwoWire::onRequest( I2C_onTxCallback_f f ) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ tx_callback = f; } @@ -838,7 +812,7 @@ void TwoWire::onRequest( I2C_onTxCallback_f f ) { /* -------------------------------------------------------------------------- */ int TwoWire::available(void) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ return rx_index - rx_extract_index; } @@ -847,7 +821,7 @@ int TwoWire::available(void) { // or after requestFrom(address, numBytes) /* -------------------------------------------------------------------------- */ int TwoWire::read(void) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ int rv = -1; // get each successive byte on each call @@ -864,7 +838,7 @@ int TwoWire::read(void) { // or after requestFrom(address, numBytes) /* -------------------------------------------------------------------------- */ int TwoWire::peek(void) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ int rv = -1; // get each successive byte on each call @@ -877,13 +851,11 @@ int TwoWire::peek(void) { /* -------------------------------------------------------------------------- */ void TwoWire::flush(void) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ while(bus_status != WIRE_STATUS_TX_COMPLETED && bus_status != WIRE_STATUS_TRANSACTION_ABORTED) {} } - - #if WIRE_HOWMANY > 0 TwoWire Wire(WIRE_SCL_PIN, WIRE_SDA_PIN); #endif From 2915015750a02a98350e67f348aba18bbb5c6d43 Mon Sep 17 00:00:00 2001 From: maidnl Date: Fri, 10 Nov 2023 12:40:15 +0100 Subject: [PATCH 4/5] Change to avoid the need to call end() when switching role --- libraries/Wire/Wire.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index 76fd4280..a089b100 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -277,6 +277,7 @@ bool TwoWire::cfg_pins(int max_index) { /* -------------------------------------------------------------------------- */ void TwoWire::begin() { /* -------------------------------------------------------------------------- */ + end(); is_master = true; _begin(); } @@ -434,6 +435,7 @@ void TwoWire::_begin(void) { /* -------------------------------------------------------------------------- */ void TwoWire::begin(uint16_t address) { /* -------------------------------------------------------------------------- */ + end(); is_master = false; slave_address = address; /* Address is set inside begin() using slave_address member variable */ @@ -444,6 +446,7 @@ void TwoWire::begin(uint16_t address) { /* -------------------------------------------------------------------------- */ void TwoWire::begin(int address) { /* -------------------------------------------------------------------------- */ + end(); is_master = false; slave_address = (uint16_t)address; begin((uint16_t)address); @@ -452,6 +455,7 @@ void TwoWire::begin(int address) { /* -------------------------------------------------------------------------- */ void TwoWire::begin(uint8_t address) { /* -------------------------------------------------------------------------- */ + end(); is_master = false; slave_address = (uint16_t)address; begin((uint16_t)address); From 081bc2406076d2b85cb9bb13b0f30e56327a1bea Mon Sep 17 00:00:00 2001 From: maidnl Date: Mon, 13 Nov 2023 11:16:47 +0100 Subject: [PATCH 5/5] removing unused variable (left over from debug session) --- libraries/Wire/Wire.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index a089b100..5fd8a84b 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -265,10 +265,8 @@ bool TwoWire::cfg_pins(int max_index) { is_sci = false; ioport_sda = IOPORT_PERIPHERAL_IIC; ioport_scl = IOPORT_PERIPHERAL_IIC; - - fsp_err_t err = R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[sda_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_DRIVE_MID | IOPORT_CFG_PERIPHERAL_PIN | ioport_sda)); - - err = R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[scl_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_DRIVE_MID | IOPORT_CFG_PERIPHERAL_PIN | ioport_scl)); + R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[sda_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_DRIVE_MID | IOPORT_CFG_PERIPHERAL_PIN | ioport_sda)); + R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[scl_pin].pin, (uint32_t) (IOPORT_CFG_PULLUP_ENABLE | IOPORT_CFG_DRIVE_MID | IOPORT_CFG_PERIPHERAL_PIN | ioport_scl)); } return true;