Clock tact between USB CDC and I2C

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Clock tact between USB CDC and I2C

Jump to solution
1,874 Views
itr1718
Contributor III

Hello,

 

I already asked about a sensor probing and I tried also changed clock setting. I guess now, it depends on clock tact between USB and I2C.

My boot clock frequency is 48MHz and I2C baud rate is 400k. If the baud rate is lower, usb don't also run.

I red document about KL27 family. According to this, standard I2C rates are 100kbit/s in VLPR mode and my sensor with IIC runs in VLPR and also normal clock setting which frequency is 48MHz with Lirc mode.

But USB runs only in Hirc mode. 

 

My question is, how can I set clock frequency or something others for two communication, USB and I2C?

0 Kudos
Reply
1 Solution
1,844 Views
myke_predko
Senior Contributor III

@itr1718 

Can you see your clocks using the MCUXpresso Clock Wizard?  

Can we bring this down to one thread?  I think there are the same root causes to your problems.  

Let's move this into the "don't probe a sensor after applying usb driver" thread.  

myke

View solution in original post

0 Kudos
Reply
5 Replies
1,868 Views
myke_predko
Senior Contributor III

Hiya @itr1718 

Same response as previously - could you describe your set up a bit better?  

For this question, you need to describe where you're getting your code (ie from the SDK or from?) and the second one is how are you setting up your clocks?  

myke

0 Kudos
Reply
1,864 Views
itr1718
Contributor III

Hi @myke_predko ,

First of all, I got the demo project from SDK for FRDM-K27L, because I am using MKL27Z256 microcontroller. the other codes for usb are also from the SDK. I just added some lines for printf using in terminal program.

Second, I applied clock setting same as the demo code, clock_config. 

Core clock frequency is 48000000Hz for normal mode and 2000000Hz for VLPR.

 

As my experience, HIRC source is enabled for normal mode. If it is changed LIRC mode, the sensor runs well.

 

I add part of the detail code below. 

For clock_config,

const mcglite_config_t mcgliteConfig_BOARD_BootClockRUN =
{
.outsrc=kMCGLITE_ClkSrcHirc, /* MCGOUTCLK source is HIRC */
.irclkEnableMode = kMCGLITE_IrclkEnable, /* MCGIRCLK enabled, MCGIRCLK disabled in STOP mode */
.ircs = kMCGLITE_Lirc8M, /* Slow internal reference (LIRC) 8 MHz clock selected */
.fcrdiv = kMCGLITE_LircDivBy1, /* Low-frequency Internal Reference Clock Divider: divided by 1 */
.lircDiv2 = kMCGLITE_LircDivBy1, /* Second Low-frequency Internal Reference Clock Divider: divided by 1 */
.hircEnableInNotHircMode = true, /* HIRC source is enabled */
};
const sim_clock_config_t simConfig_BOARD_BootClockRUN =
{
.er32ksrc=SIM_OSC32KSEL_OSC32KCLK_CLK, /* OSC32KSEL select: OSC32KCLK clock */
.clkdiv1 = 0x10000U, /* SIM_CLKDIV1 - OUTDIV1: /1, OUTDIV4: /2 */
};
const osc_config_t oscConfig_BOARD_BootClockRUN =
{
.freq = 0U, /* Oscillator frequency: 0Hz */
.capLoad = (kOSC_Cap4P | kOSC_Cap8P), /* Oscillator capacity load: 12pF */
.workMode = kOSC_ModeOscLowPower, /* Oscillator low power */
.oscerConfig =
{
.enableMode = kOSC_ErClkEnable, /* Enable external reference clock, disable external reference clock in STOP mode */
}
};

void BOARD_BootClockRUN(void)
{
/* Set the system clock dividers in SIM to safe value. */
CLOCK_SetSimSafeDivs();
/* Set MCG to HIRC mode. */
CLOCK_SetMcgliteConfig(&mcgliteConfig_BOARD_BootClockRUN);
/* Set the clock configuration in SIM module. */
CLOCK_SetSimConfig(&simConfig_BOARD_BootClockRUN);
/* Set SystemCoreClock variable. */
SystemCoreClock = BOARD_BOOTCLOCKRUN_CORE_CLOCK;
}

 

For I2C Initialisation,

I2C_MasterGetDefaultConfig(&i2c_Mconfig);

i2c_Mconfig.baudRate_Bps = 400000U;
i2c_Mconfig.enableMaster = ON;
i2c_Mconfig.enableStopHold = OFF;
i2c_Mconfig.glitchFilterWidth = 0;

int16_t srcclock = CLOCK_GetFreq(I2C0_CLK_SRC);
I2C_MasterInit(I2C0, &i2c_Mconfig, srcclock);

 

itr

0 Kudos
Reply
1,852 Views
myke_predko
Senior Contributor III

HI @itr1718 

So, your copying the Clock Configuratino code from the SDK? 

Could I recommend that you use the MCUXpresso Clock Wizard to set up your clocks?  I know it's basically user hostile and you will need to have a pretty good understanding of clocking, but if you can get things set up there, I think you're just about guaranteed to having everything working as expected.  

I'm saying this because it looks like you're using the I2C API for your initialization and I'm not sure what is going to be happening for I2C0_CLK_SRC.  It *should* be "BUS_CLK" but I don't know what it is nor do I know what speed BUS_CLK is running at.  

Can I suggest that you try to get the Clock Wizard working and then you can post a screen shot here for people to review?  It's a lot easier for people to review than to go through code.  

myke

0 Kudos
Reply
1,847 Views
itr1718
Contributor III

Hi @myke_predko ,

yes, I had usb connection problem before and copied clock_config from SDK.

I am using Kinetis design studio, but I try to use MCUXpresso Clock Wizard.

I2C0_CLK_SRC is defined SYS_CLOCK.

 

Should I check and set both clocks of USB and I2C?

0 Kudos
Reply
1,845 Views
myke_predko
Senior Contributor III

@itr1718 

Can you see your clocks using the MCUXpresso Clock Wizard?  

Can we bring this down to one thread?  I think there are the same root causes to your problems.  

Let's move this into the "don't probe a sensor after applying usb driver" thread.  

myke

0 Kudos
Reply