Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set I2C Freq: 1MHz~2MHz can't be set #969

Open
TerryWoo123 opened this issue Mar 28, 2024 · 0 comments
Open

Set I2C Freq: 1MHz~2MHz can't be set #969

TerryWoo123 opened this issue Mar 28, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@TerryWoo123
Copy link

I found a bug in MXC_I2C_RevA_SetFrequency() of i2C_reva.c . Once I2C clock is set between 1MHz~2MHz, function will return will error E_BAD_PARAM.

  if (hz > MXC_I2C_REVA_FASTPLUS_SPEED && hz <= MXC_I2C_REVA_HIGH_SPEED) {
        // Enable high speed mode
        int hsLowClks, hsHiClks;

        // Calculate the period of SCL and set up 33% duty cycle
        ticksTotal = PeripheralClock / hz;
        hsLowClks = (ticksTotal * 2) / 3 - 1;
        hsHiClks = ticksTotal / 3 - 1;

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

        // If we're too slow for high speed, bail out
        if ((hsHiClks > 0xF) || (hsLowClks > 0xF)) {
            return E_BAD_PARAM;
        }

if setting Freq is not high enough to enable HS mode, properly value should be set, instead of return with an error.
my suggestion code as below:

        if ((hsHiClks <= 0xF) && (hsLowClks <= 0xF)) 
        {
            hsLowClks = (hsLowClks << MXC_F_I2C_REVA_HSCLK_LO_POS) & MXC_F_I2C_REVA_HSCLK_LO;
            hsHiClks = (hsHiClks << MXC_F_I2C_REVA_HSCLK_HI_POS) & MXC_F_I2C_REVA_HSCLK_HI;

            i2c->hsclk = (hsLowClks | hsHiClks);

            i2c->ctrl |= MXC_F_I2C_REVA_CTRL_HS_EN;

            hz = MXC_I2C_REVA_FAST_SPEED; // High speed preambles will be sent at 400kHz
        }
        else
        {
            // If we're too slow for high speed, bail out
        }
@Jake-Carter Jake-Carter added the bug Something isn't working label Apr 1, 2024
@Jake-Carter Jake-Carter self-assigned this Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants