Example LPC845_I2C_accel_led: I2C_MasterInit()

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

Example LPC845_I2C_accel_led: I2C_MasterInit()

2,047 Views
1234567890
Contributor IV

I don't understand the driver, please explain it to me:

The example above from LPC845BRK_Examples.zip has a

#define I2C_MASTER_CLOCK_FREQUENCY (12000000)

In clocks tool I can see a FRO of 30 MHz is selected, resulting in clock_config.c.

In I2C_MasterInit() the function I2CMasterSetBaudrate() is called with I2C_MASTER_CLOCK_FREQUENCY as argument for srcClock_Hz (I2C functional clock frequency in Hertz).

When I hop along the calls I think it must be 30 MHz instead of 12 MHz in the example. So where went I wrong or missed the change? It's a serious question for me to understand the driver.

Labels (1)
0 Kudos
Reply
12 Replies

1,724 Views
An_C
Contributor I

Hello,

Just to say that the changes described in this post have not been updated in the I2C driver and it still does not work without making the changes manually. I used the LPC845 Breakout board.

Cheers

0 Kudos
Reply

1,785 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Customer,

    #define I2C_MASTER_CLOCK_FREQUENCY (12000000) is the I2C module clock source frequent, not the core clock.

  Please check this information:

pastedImage_1.png

So, you need to check SYSCON I2CnCLKSEL, which clock you are choose, you can debug your code and check it.

 If you are choosing the FRO, it is 12Mhz.

But if you are choosing main_clk, and your main_clk is 30Mhz, then I am afraid it is your code problem.

Wish it helps you!

If you still have question about it, please kindly let me know.


Have a great day,
Kerry

 

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply

1,785 Views
1234567890
Contributor IV

Hi,

thanks for your advise. Unfortunately I don't have the MMA8652 break-out board and can just load the example and debug the LPC845. I assume that initialization of the master (LPC845) is completely done before starting communication with the MMA8652 break-out board. So I upload the example to the LPC845 and hit F8. This will lead to line 524 in fsl_i2c.h, which is right after the function init_i2c_master() in main.

When I then look at FCLKSEL5 (0x4004 80A4) I see 0x7, which is the reset value and means 'none'. So no clock is selected for I2C0? And I can't identify any clock configuration in main after that point.

And as I stated above, in mex-file 30 MHz is selected. So having 12 MHz as peripheral clock for I2C0 is a little bit difficult from my point of view (at the moment, and that is my problem).

0 Kudos
Reply

1,785 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Customer,

  I think you code may have problems if it is not from the official side.

  Please download the LPC845 sdk code from this link:

   Welcome | MCUXpresso SDK Builder 

  Choose the chip as LPC845, generate the code and download it.

  You can refer to the offical sdk I2C sample code:

SDK_2.4.2_LPCXpresso845MAX\boards\lpcxpresso845max\driver_examples\i2c\polling_b2b_transfer\master

  I debug my I2C master code,  this project also define the I2C master clock as 12Mhz, but after debug, we can find the I2C module clock is from main, and the main clock is from IRC, and as you know, the IRC is 12Mhz.

pastedImage_2.png

pastedImage_3.png

pastedImage_4.png

So, I highly recommend you refer to the NXP official code.

Wish it helps you!

If you still have question about it, please kindly let me know.


Have a great day,
Kerry

 

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply

1,785 Views
1234567890
Contributor IV

Hi, thanks for your reply.The example I mean is from an official NXP site as I wrote above.

I checked your example. One of the differences is, that

/* Select the main clock as source clock of I2C0. */
CLOCK_Select(kI2C0_Clk_From_MainClk);

is called in the code. So here the clock source is chosen.

So I think the example code from LPC845-BRK is wrong.

But o.k., let's continue with your example: The next line in your debugger screenshot above is

BOARD_BootClockFRO30M();

which is

void BOARD_BootClockFRO30M(void)
{
    /*!< Set up the clock sources */
    /*!< Set up FRO */
    POWER_DisablePD(kPDRUNCFG_PD_FRO_OUT);      /*!< Ensure FRO is on  */
    POWER_DisablePD(kPDRUNCFG_PD_FRO);          /*!< Ensure FRO is on  */
    CLOCK_SetFroOscFreq(kCLOCK_FroOscOut30M);   /*!< Set up FRO freq */
    CLOCK_SetFroOutClkSrc(kCLOCK_FroSrcFroOsc); /*!< Set FRO clock source */
    POWER_DisablePD(kPDRUNCFG_PD_SYSOSC);       /*!< Ensure Main osc is on */
    CLOCK_InitSysOsc(12000000U);                /*!< Set main osc freq */
    CLOCK_SetMainClkSrc(kCLOCK_MainClkSrcFro);  /*!< select fro for main clock */
    CLOCK_SetCoreSysClkDiv(1U);
    /*!< Set SystemCoreClock variable. */
    SystemCoreClock = BOARD_BOOTCLOCKFRO30M_CORE_CLOCK;
}

Here the system oscillator is configured to 12 MHz. I don't know what it is good for, but doesn't matter here.

The FRO is configured to 30 MHz and chosen as main clock source. Even SystemCoreClock is updated to 30 MHz. So from my point of view the result of all is: I2C0 clock is 30 MHz, not 12 MHz.

Sorry, please show me where I went off the track.

EDIT/ADDED: I loaded your example without slave attached and let the program run to line 101 in main (i2c_polling_b2b_transfer_master.c). I think the master should be completely configured because the next line starts communication with the slave.pastedImage_2.pngSo for me it looks like I stated above: I2C0 runs from FRO, previously configured for 30 MHz. Because of I2C0->DIV (DIVVAL = 9, means divided by 10) the effective clock for I2C is 3 MHz.

0 Kudos
Reply

1,785 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi customer,

  Thank you for your updated information.

  Please give me your test project link, then I can help you to check it on my side.

Have a great day,
Kerry

 

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply

1,785 Views
1234567890
Contributor IV

Hi, from SDK_2.x_LPC845 (version 2.5.0, manifest version 3.4.0) I imported driver_examples/i2c/lpc_i2c_polling_b2b_transfer_master (via 'Import SDK example(s)...' from the Quickstart Panel).

0 Kudos
Reply

1,785 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Customer,

   Thanks a lot for your contribution.

   You are right, after the detail checking, the SDK have bugs about the I2C code.

  I have tested the main clock, it is really 30Mhz, I output the main clock to PIO0_6, with the following code:

1.  pin mux.c

 Add:

       // CLKOUT to P0_6  kSWM_CLKOUT kerry add
    SWM_SetMovablePinSelect(SWM0, kSWM_CLKOUT, kSWM_PortPin_P0_6);

Above  CLOCK_DisableClock(kCLOCK_Swm); in void BOARD_InitPins(void).

2.  in main code:

   //========clkout P0_6 choose main clock
    SYSCON->CLKOUTSEL = 1;
    SYSCON->CLKOUTDIV = 1;

Then we can find the main clock in the PIO0_6, it is :

pastedImage_1.png

So,  please modify the I2C master clock to 30Mhz, instead of 12Mhz.

#define I2C_MASTER_CLOCK_FREQUENCY (30000000)//(12000000)

I will report this bug to our according department.

Then when you test the I2C, you will find the I2C clock is 100Khz.

pastedImage_2.png

Thanks a lot for your question post, it is really very useful to our SDK improvement.

  Wish it helps you!

Have a great day,
Kerry

 

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply

1,785 Views
1234567890
Contributor IV

Thanks for your staying power.

Please also remind the initial issue: For the example 'LPC845_I2C_accel_led' I wrote in addition to the 30 MHZ issue:

When I then look at FCLKSEL5 (0x4004 80A4) I see 0x7, which is the reset value and means 'none'. So no clock is selected for I2C0?

And this order doesn't make sense to me as well:

masterConfig.enableTimeout = true;
/* Initialize and set default configuration: clock, freq for I2C master device */
I2C_MasterGetDefaultConfig(&masterConfig);
I2C_MasterInit(BOARD_ACCEL_I2C_BASEADDR, &masterConfig, I2C_MASTER_CLOCK_FREQUENCY);

enableTimeout is set to true. After that the default config is loaded where enableTimeout is false.

=> The default config should be loaded first, then enableTimeout should be overwritten and then peripheral should be initialised with the config (now differing from default).

0 Kudos
Reply

1,785 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Customer,

   About the FCLKSEL5 (0x4004 80A4)=0x7, because you didn't call the code to select the I2C module clock:

       CLOCK_Select(kI2C0_Clk_From_MainClk);

  Actually, the SDK code already have it, when you write your own code, you need to add the module clock selection at first.

  pastedImage_1.png

pastedImage_2.png

  About the example LPC845_I2C_accel_led, where you get it? I have checked the SDK_2.5.0_LPCXpresso845MAX\boards\lpcxpresso845max\driver_examples\i2c, didn't find it .

Have a great day,
Kerry

 

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply

1,785 Views
1234567890
Contributor IV

Hi, it's not my code. Like I wrote in the second sentence of this thread: The example above from LPC845BRK_Examples.zip

https://www.nxp.com/downloads/en/software/LPC845BRK_Examples.zip 

0 Kudos
Reply

1,785 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Customer,

   Thank you for your updated information.

   The lpc845BRK board is the new board, I even don't have it. So, that code, at least I didn't test it before.

   After checking the code, you are totally right, these three points need to modify:

1. #define I2C_MASTER_CLOCK_FREQUENCY (12000000) , modified to

 #define I2C_MASTER_CLOCK_FREQUENCY (30000000)

2. Add the enable I2C module clock code:

 CLOCK_Select(kI2C0_Clk_From_MainClk);

3.  change the code order in init_i2c_master about the I2C_MasterGetDefaultConfig and masterConfig configuration.

I will report this code bug to our according department.


Have a great day,
Kerry

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply