Get Flexcom clock (i.e., FXCOM0_clock) in my program?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Get Flexcom clock (i.e., FXCOM0_clock) in my program?

ソリューションへジャンプ
5,342件の閲覧回数
danielholala
Senior Contributor II

Hi there,

for using the serial interface, I configured Flexcom 0 clock to 23.97 MHz, see here:

danielholala_0-1638210856490.png

 

Now to initialize USART0, I think I should call 

USART_Init ( USART_Type * base,
const usart_config_t * config,
uint32_t srcClock_Hz
) ;

The last parameter is "USART clock source frequency in HZ." (see docs).

I guess I should pass FXCOMCLK0 (in Hz) to correctly initialize the interface.

You don't want me to type this value in, do you? That would be 🤦‍

So how do I programatically retrieve this value? 

I tried 

CLOCK_GetFlexCommClkFreq(0); 

but this resulted in 330 033 which is clearly not the value I need (what is this function returning, anyway?).

 

Cheers
Dan

0 件の賞賛
返信
1 解決策
3,222件の閲覧回数
danielholala
Senior Contributor II

Hi,

just installed MCUXpresso IDE 11.7.0 and SDK 2.13.0 (for LPC5528).

This SDK version seems to have fixed this issue although using different code to retrieve the Flexcom clock:

 

/* Get FLEXCOMM Clk */
uint32_t CLOCK_GetFlexCommClkFreq(uint32_t id)
{
    uint32_t freq   = 0U;
    uint32_t frgMul = 0U;
    uint32_t frgDiv = 0U;

    freq   = CLOCK_GetFlexCommInputClock(id);
    frgMul = (SYSCON->FLEXFRGXCTRL[id] & SYSCON_FLEXFRG0CTRL_MULT_MASK) >> 8U;
    frgDiv = SYSCON->FLEXFRGXCTRL[id] & SYSCON_FLEXFRG0CTRL_DIV_MASK;
    return (uint32_t)(((uint64_t)freq * ((uint64_t)frgDiv + 1ULL)) / (frgMul + frgDiv + 1UL));
}

 

I tested this code with the following Clock Tools configuration: main clock at 150 MHz (derived from 16 MHz XTAL via PLL0) and Flexcom0 clock derived from main clock divided by 2 (PLL0DIV), multiplied by 256 (FRGCTROL0_MUL), divided by 400 (FRGCTRL0_DIV)  = 48 MHz. This function gets the correct result. I prefer this code over the code suggested by @ZhangJennie as it does not employ floating point arithmetic.

元の投稿で解決策を見る

タグ(1)
0 件の賞賛
返信
13 返答(返信)
5,329件の閲覧回数
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi Daniel

What's your chip part number?

Thanks,

Jun Zhang

0 件の賞賛
返信
5,318件の閲覧回数
danielholala
Senior Contributor II

It's a microcontroller from the LPC55xx family, currently LPC55S26JBD64.

Thanks.

Daniel

0 件の賞賛
返信
5,309件の閲覧回数
ZhangJennie
NXP TechSupport
NXP TechSupport

Daniel

For example, if you use Flexcomm0 USART0, USART0 is clocked by Flexcomm0 clock by default.

You can change the clock source to 32K RTC clock by setting MODE32K as below.

ZhangJennie_0-1638263442038.png

 

Regards

Jun Zhang

 

 

0 件の賞賛
返信
5,307件の閲覧回数
danielholala
Senior Contributor II

Dear ZhangJennie,

Thank you for your reply, however, I don't see how your answer relates to my question. You basically suggest that I scrap the tool and do the clock configuration by registers.

That's bewildering.

I assume that NXP has invested a lot of resources (money, people) to provide the community with MCUXpresso Config Tools. Of course, there will be different opinions on using tools for configuring a microcontroller. I guess that some engineers prefer to setup all registers on their on just using the datasheet and some basic structs. Other engineers use the SDK and embrace the availability of configuration tools provided through the manufacturer.

With that said, I gave it a try. I used the clocks tools as I think it nicely visualizes the setup and is self-sufficient (quite independent from the remaining configuration and code). I configured the FXCOM0_clock as reproduced in my first post.

Now, how do I continue from there?

Thanks.

Best regards
Daniel

0 件の賞賛
返信
5,293件の閲覧回数
ZhangJennie
NXP TechSupport
NXP TechSupport

In my previous reply. I tried to tell you USART clock source selection from register, root level.

If configure tool, it is easy, Just select FXCOM0 for clock source.

ZhangJennie_0-1638325026263.png

Jun Zhang

 

0 件の賞賛
返信
5,285件の閲覧回数
danielholala
Senior Contributor II

Dear Jun Zhang,

Thank you for your response.

From you response I take it that there's no way to retrieve the Flexcom0 clock frequency that has been configured using the Clocks tool.

Instead, you suggest to configure the Flexcom/USART component using Peripherals tools. The Peripheral tools obviously retrieves the correct clock frequency from Clocks tool. Peripherals tool also adds code to initialize USART. The code is based on "fsl_usart_cmsis.h". It's based on ARM's CMSIS API.

How do I continue from there with the NXP SDK API?

0 件の賞賛
返信
5,251件の閲覧回数
ZhangJennie
NXP TechSupport
NXP TechSupport

HI

There is defect in clock get frequency functions. With below code, change temp type as float.

uint32_t CLOCK_GetFlexCommClkFreq(uint32_t id)
{
uint32_t freq = 0U;
float temp;

freq = CLOCK_GetFlexCommInputClock(id);
temp = (SYSCON->FLEXFRGXCTRL[id] & SYSCON_FLEXFRG0CTRL_MULT_MASK) >> 8;
return (uint32_t)(freq / (1U + ((temp) / ((SYSCON->FLEXFRGXCTRL[id] & SYSCON_FLEXFRG0CTRL_DIV_MASK) + 1U))));
}

You will also find in peripheral.h, FLEXCOMM0_CLOCK_SOURCE  automatically changes to the right flexcomm0 clock source after code generation. 

/* Definition of the clock source frequency */
#define FLEXCOMM0_CLOCK_SOURCE 47407407UL

 

Have a nice day,

Jun Zhang

3,223件の閲覧回数
danielholala
Senior Contributor II

Hi,

just installed MCUXpresso IDE 11.7.0 and SDK 2.13.0 (for LPC5528).

This SDK version seems to have fixed this issue although using different code to retrieve the Flexcom clock:

 

/* Get FLEXCOMM Clk */
uint32_t CLOCK_GetFlexCommClkFreq(uint32_t id)
{
    uint32_t freq   = 0U;
    uint32_t frgMul = 0U;
    uint32_t frgDiv = 0U;

    freq   = CLOCK_GetFlexCommInputClock(id);
    frgMul = (SYSCON->FLEXFRGXCTRL[id] & SYSCON_FLEXFRG0CTRL_MULT_MASK) >> 8U;
    frgDiv = SYSCON->FLEXFRGXCTRL[id] & SYSCON_FLEXFRG0CTRL_DIV_MASK;
    return (uint32_t)(((uint64_t)freq * ((uint64_t)frgDiv + 1ULL)) / (frgMul + frgDiv + 1UL));
}

 

I tested this code with the following Clock Tools configuration: main clock at 150 MHz (derived from 16 MHz XTAL via PLL0) and Flexcom0 clock derived from main clock divided by 2 (PLL0DIV), multiplied by 256 (FRGCTROL0_MUL), divided by 400 (FRGCTRL0_DIV)  = 48 MHz. This function gets the correct result. I prefer this code over the code suggested by @ZhangJennie as it does not employ floating point arithmetic.

タグ(1)
0 件の賞賛
返信
5,247件の閲覧回数
danielholala
Senior Contributor II

That code change fixed CLOCK_GetFlexCommClkFreq() and thus my issue. Thank you for your support.

 

Just a quick note regarding your comment regarding "FLEXCOMM0_CLOCK_SOURCE". I could not find this definition in any of my projects. What I found was "FLEXCOMM0_CLOCK_SOURCE_FREQ"

However, this definition is only available after one uses the Peripherals tools to configure, e.g., USART0, see screenshot. In other words, if one does not use Peripherals tools this definition is not available. Just a thought, maybe the clocks tool should add this definition instead of the Peripherals tools. 

danielholala_0-1638522409000.png

 

 

0 件の賞賛
返信
4,803件の閲覧回数
marek_neuzil
NXP Employee
NXP Employee

Hello,

I have reviewed your issue and the problem is in the implementation of the following CLOCK_GetFlexCommClkFreq() function:

uint32_t CLOCK_GetFlexCommClkFreq(uint32_t id)
{
uint32_t freq = 0U;
uint32_t temp;

freq = CLOCK_GetFlexCommInputClock(id);
temp = SYSCON->FLEXFRGXCTRL[id] & SYSCON_FLEXFRG0CTRL_MULT_MASK;
return freq / (1U + (temp) / ((SYSCON->FLEXFRGXCTRL[id] & SYSCON_FLEXFRG0CTRL_DIV_MASK) + 1U));
}

The calculation of return expression is based on integer arithmetic and therefore the fractional divider is always 1. The implementation of the function must be fixed. I have already reported the issue.

The definitions of the clock frequency of all top level clock outputs of the clock model have been already supported for latest i.MX RT MCUs. We are going to support this feature for other MCUs in a next release.

Best Regards,

Marek Neuzil

 

 

5,241件の閲覧回数
ZhangJennie
NXP TechSupport
NXP TechSupport

Maybe we use different versions. anyway, I think they are same thing despite the name. this is generated by peripheral tools

ZhangJennie_0-1638527648048.png

 

0 件の賞賛
返信
5,236件の閲覧回数
danielholala
Senior Contributor II

Thanks for confirming that the definition for clock source frequency is only available after using Peripheral tools.

The name of the definition may well vary over tool version. My versions are as follows: 

  •  MCUXpresso IDE v11.4.1
  • Clocks Tool Version: 8.0.0.202109031454
  • Peripherals Tool Version: 10.0.0.202109031454
  • Pins Tool Version: 10.0.0.202109031454

(extracted from Help > About MCUXpresso IDE)

0 件の賞賛
返信
5,221件の閲覧回数
ZhangJennie
NXP TechSupport
NXP TechSupport

Ok. my version is older.

0 件の賞賛
返信