Skip to content

Commit

Permalink
feat(PeriphDrivers): Return HS frequency from MXC_I2C_GetFrequency wh…
Browse files Browse the repository at this point in the history
…en HS-Mode enabled (#707)
  • Loading branch information
Jacob-Scheiffler authored Aug 14, 2023
1 parent 6cff8ac commit 651393a
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Libraries/PeriphDrivers/Source/I2C/i2c_reva.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ int MXC_I2C_RevA_SetFrequency(mxc_i2c_reva_regs_t *i2c, unsigned int hz)
hsHiClks = ticksTotal / 3 - 1;

// For rounding errors, adjust by 1 clock tick
if (ticksTotal % 1) {
if (ticksTotal % 2) {
hsHiClks++;
}

Expand Down Expand Up @@ -182,7 +182,7 @@ int MXC_I2C_RevA_SetFrequency(mxc_i2c_reva_regs_t *i2c, unsigned int hz)
lowClks = (ticksTotal >> 1) - 1;

// Adjust for rounding errors
if (ticksTotal % 1) {
if (ticksTotal % 2) {
hiClks++;
}

Expand All @@ -200,10 +200,18 @@ int MXC_I2C_RevA_SetFrequency(mxc_i2c_reva_regs_t *i2c, unsigned int hz)

unsigned int MXC_I2C_RevA_GetFrequency(mxc_i2c_reva_regs_t *i2c)
{
unsigned int sclCycles = 0;
unsigned int sclCycles = 2;
// sclCycles Initialized to 2 b/c formula is sclCycles = (lo_clks + 1) + (hi_clks + 1)

// Calculate the speed based on what we've written into registers
sclCycles = (i2c->clklo & MXC_F_I2C_REVA_CLKLO_LO) + (i2c->clkhi & MXC_F_I2C_REVA_CLKHI_HI);
if (i2c->ctrl & MXC_F_I2C_REVA_CTRL_HS_EN) {
// HS-Mode enabled, calculate HS Frequency
sclCycles += (i2c->hsclk & MXC_F_I2C_REVA_HSCLK_LO) >> MXC_F_I2C_REVA_HSCLK_LO_POS;
sclCycles += (i2c->hsclk & MXC_F_I2C_REVA_HSCLK_HI) >> MXC_F_I2C_REVA_HSCLK_HI_POS;
} else {
// HS-Mode not enabled, calculate nominal frequency
sclCycles += (i2c->clklo & MXC_F_I2C_REVA_CLKLO_LO);
sclCycles += (i2c->clkhi & MXC_F_I2C_REVA_CLKHI_HI);
}

return PeripheralClock / sclCycles;
}
Expand Down

0 comments on commit 651393a

Please sign in to comment.