Skip to content

Commit

Permalink
Merge pull request #139 from O-H-M2/i2c_master_verification
Browse files Browse the repository at this point in the history
I2c_master driver validation and enhancement
  • Loading branch information
OctopusZ authored Sep 24, 2023
2 parents eb5219c + 499b423 commit 3d6105d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
42 changes: 39 additions & 3 deletions qmk_porting/platforms/ch58x/i2c_master.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@

static uint8_t i2c_address;

static void i2c_power_toggle(bool state)
{
if (state) {
while (R8_SLP_CLK_OFF1 & RB_SLP_CLK_I2C) {
sys_safe_access_enable();
R8_SLP_CLK_OFF1 &= ~RB_SLP_CLK_I2C;
}
sys_safe_access_disable();
} else {
while (!(R8_SLP_CLK_OFF1 & RB_SLP_CLK_I2C)) {
sys_safe_access_enable();
R8_SLP_CLK_OFF1 |= RB_SLP_CLK_I2C;
}
sys_safe_access_disable();
}
}

void i2c_init()
{
#ifdef I2C_IO_REMAPPING
Expand All @@ -45,10 +62,15 @@ void i2c_init()
while (I2C_GetFlagStatus(I2C_FLAG_BUSY)) {
__nop();
}

wait_us(200);
i2c_power_toggle(false);
}

i2c_status_t i2c_start(uint8_t address, uint16_t timeout)
{
i2c_power_toggle(true);

uint16_t timeout_timer = timer_read();

i2c_address = address;
Expand All @@ -59,6 +81,7 @@ i2c_status_t i2c_start(uint8_t address, uint16_t timeout)
return I2C_STATUS_TIMEOUT;
}
}

return I2C_STATUS_SUCCESS;
}

Expand Down Expand Up @@ -98,12 +121,15 @@ i2c_status_t i2c_transmit(uint8_t address, const uint8_t *data, uint16_t length,
return I2C_STATUS_TIMEOUT;
}
}

i2c_stop();

wait_us(200);
i2c_power_toggle(false);

return I2C_STATUS_SUCCESS;
}

// TODO: verify this
i2c_status_t i2c_receive(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout)
{
i2c_status_t status = i2c_start(address, timeout);
Expand Down Expand Up @@ -135,6 +161,10 @@ i2c_status_t i2c_receive(uint8_t address, uint8_t *data, uint16_t length, uint16
}

I2C_AcknowledgeConfig(ENABLE);

wait_us(200);
i2c_power_toggle(false);

return I2C_STATUS_SUCCESS;
}

Expand Down Expand Up @@ -163,7 +193,6 @@ i2c_status_t i2c_writeReg16(uint8_t devaddr, uint16_t regaddr, const uint8_t *da
return i2c_transmit(devaddr, (const uint8_t *)buffer, length + 2, timeout);
}

// TODO: verify this
i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t *data, uint16_t length, uint16_t timeout)
{
i2c_status_t status = i2c_start(devaddr, timeout);
Expand Down Expand Up @@ -222,10 +251,13 @@ i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t *data, uint16
}

I2C_AcknowledgeConfig(ENABLE);

wait_us(200);
i2c_power_toggle(false);

return I2C_STATUS_SUCCESS;
}

// TODO: verify this
i2c_status_t i2c_readReg16(uint8_t devaddr, uint16_t regaddr, uint8_t *data, uint16_t length, uint16_t timeout)
{
i2c_status_t status = i2c_start(devaddr, timeout);
Expand Down Expand Up @@ -290,5 +322,9 @@ i2c_status_t i2c_readReg16(uint8_t devaddr, uint16_t regaddr, uint8_t *data, uin
}

I2C_AcknowledgeConfig(ENABLE);

wait_us(200);
i2c_power_toggle(false);

return I2C_STATUS_SUCCESS;
}
2 changes: 1 addition & 1 deletion qmk_porting/protocol/pre_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#if defined BLE_ENABLE || (defined ESB_ENABLE && ESB_ENABLE == 1)
#define NO_USB_STARTUP_CHECK
#ifndef BATTERY_MEASURE_PIN
#error "Battery measure pin undefined."
#warning "Battery measure pin undefined."
#else
#endif
#ifndef POWER_DETECT_PIN
Expand Down

0 comments on commit 3d6105d

Please sign in to comment.