32kHz timer on S32K118 EVB with MCAL Gpt Module

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

32kHz timer on S32K118 EVB with MCAL Gpt Module

Jump to solution
3,690 Views
talha_uyar
Contributor III

Hi,

I am trying to build  basic timer application using the Gpt module provided. I am using MCAL 4.3 on S32K118 EVB and using EB tresos and S32DS.

I have set up the McuClockReferencePoint at LPO_CLK_32K and when the frequency is calculated, it is indeed 32000. Then I have set the GptClockReferencePoint_0 to point to this clock, and created a Gpt channel instance named Sample_Timer. EB tresos also calculates the GptChannelTickFrequency as 32000. 

However, when I set up my main code to test it, the timer was waaay to fast. So I tried some reasonable large values for overflow. Indeed, when I set the timeout period as 40.000.000 it is overflowing with approximately 2 second intervals.

I am guessing the issue is with the EVB 118 hardware. So even though EB Tresos thinks that there is an oscillator with a frequency of 128Khz, it is not there. I think the only two XTAL's available on EVB are at 8MHz and 40MHz. So I guess I cannot test the LPO functionality with this board?

Is it possible to create a timer with 32 kHz frequency without using prescalers etc.? If so, what am I doing wrong?

Thanks in advance,

M. Talha Uyar

0 Kudos
1 Solution
3,319 Views
namnguyenviet
NXP Employee
NXP Employee

Hello talha.uyar@karel.com.tr‌,

I have received the answer from our internal community. Turns out the SOSC in EVB board is 40MHz instead of 8MHz. You can see in the S32K118 EVB schematic:

pastedImage_1.png

This also explained why your timer is five time faster than expectation :-)

Regards,

Nam

View solution in original post

0 Kudos
26 Replies
3,320 Views
namnguyenviet
NXP Employee
NXP Employee

Hello talha.uyar@karel.com.tr‌,

I have received the answer from our internal community. Turns out the SOSC in EVB board is 40MHz instead of 8MHz. You can see in the S32K118 EVB schematic:

pastedImage_1.png

This also explained why your timer is five time faster than expectation :-)

Regards,

Nam

0 Kudos
3,128 Views
namnguyenviet
NXP Employee
NXP Employee

Hello talha.uyar@karel.com.tr‌,

Really appreciate your attempts. Here is my comments:

I really hoped you wouldn't say this was a hardware issue. I can share the .elf file for my main project so that you can test it in your hardware. If you have S32K118 EVB, this code should change the colour of the RGB LED every at every two seconds (Blue -> Green -> White -> Red). If it is also changing its value at every two seconds in your hardware, then it cannot be a hardware issue, right?

 

I will also build a sample project from the SDK and test it as you have suggested.

You've integrated your project with S32DS, am I right? If it's correct, you can send me the whole project, and then I can import to my S32DS to try in my site. Otherwise, you can send me the .elf file.

Also, I have looked at the lpit_periodic_interrupt_s32k118 example and saw that FIRC was used as the clock source for LPIT, so I tried doing that. It works, the frequency of my timer is 24 MHz when I use FIRC as the source instead of SOSC.

 

Is there any particular reason to set SOSC for the LPIT clock source? Is it in any way advantageous to do so? If not, I will just change my clock source to FIRC.

Can you try the lpit_periodic_interrupt_s32k118 with SOSC clock? As I know, there isn't any advantage or recommendation to use SOSC rather than choosing FIRC as LPIT clock source. So you can switch to FIRC.

Nam 

0 Kudos
3,128 Views
talha_uyar
Contributor III

You can download the project files as well as the Tresos files from the following link:

https://drive.google.com/open?id=1tnLKfExgaBMufK6Qb6RSlMMnTnSv6tsK 

The code is badly written but it is very simple. Hopefully the mistake is trivial and you can find it easily. I have used  S32K118 EVB for the hardware. In this one, I have set SOSC as the clock source for LPIT, and used the function Gpt_StartTimer(Sample_Timer, 40000000); to start the timer. The code simply changes the color of the LED every two seconds but it is supposed to change the color every 10 seconds. Indicating that the clock frequency is 20 MHz instead of 4 MHz. 

I will also try using SOSC clock for the lpit_periodic_interrupt_s32k118 project and report the results when I get the chance.

Thanks for all the help Nam, I really appreciate it.

0 Kudos
3,128 Views
talha_uyar
Contributor III

I have set the clock source in lpit_periodic_interrupt_s32k118 to SOSC and it is working fine. Hence, I don't think this is a hardware issue. Something is wrong with my code. 

By the way I have done more accurate measurements for my Gpt timer when the clock source is set to FIRC. There seems to be a 2.5% deviation from the measurements. When I set a pin to be toggled at every 5 milliseconds to generate a square wave with a frequency of 100 Hz, the generated signal has the frequency of 97.5 Hz. This is not that big of a deal, but I thought it was interesting. Is this much deviation within expected levels?

Edit: I realized I have flashed the wrong file to EVB. When I set the clock source to CLK_SRC_SOSC in the lpit_periodic_interrupt_s32k118 example, the LED toggles at every 0.4 seconds instead of the expected 1 second. However, I am not familiar with the SDK codes and I might have done something wrong. I have only changed the parameter in "peripheralClockConfig0" of "clockMan1.c" file:

from

{
.clockName = LPIT0_CLK,
.clkGate = true,
.clkSrc = CLK_SRC_FIRC,
.frac = MULTIPLY_BY_ONE,
.divider = DIVIDE_BY_ONE,
},

to

{
.clockName = LPIT0_CLK,
.clkGate = true,
.clkSrc = CLK_SRC_SOSC,
.frac = MULTIPLY_BY_ONE,
.divider = DIVIDE_BY_ONE,
},

You can test this as well, if the results are not the same, I guess I have a hardware issue.

0 Kudos
3,128 Views
namnguyenviet
NXP Employee
NXP Employee

Hello talha.uyar@karel.com.tr‌,

I tried your code with the EVB board and it's exact what you've described! However, when I tried the same code with my test device (the same silicon masket: PS32K118 LAMLH 0N97V), the generated timer is correct, and that's so weird after all. I supposed the issue is in EVB itself, and I'll ask in our internal community if someone has the same issue. Meanwhile, you can choose FIRC as your LPIT clock source.

Sorry for any inconvenice and I'll back to you once I receive the answer.

Best Regards,

Nam.

0 Kudos
3,128 Views
talha_uyar
Contributor III

We are also designing a custom board for S32K118 for our project, I will make sure to also test both clock sources and report the results here.

Thanks a lot for your help.

0 Kudos
3,134 Views
namnguyenviet
NXP Employee
NXP Employee

Hello,

LPIT is not depended on MCL HW peripheral at all, so I supposed it's not necessary to check the MCL configuration, also not necessary to call Mcl_Init.

Please check the value in LPIT registers after GPT_StartTimer():

LPIT.JPG

If your registers' value is the same, then it should work, unless the MCU clock generation for LPIT has issue (but I've imported your MCU configuration and it worked)

Nam

0 Kudos
3,134 Views
talha_uyar
Contributor III

I meant the Mcu configurations, sorry about that. Ok, I will look at the register values.

0 Kudos
3,134 Views
talha_uyar
Contributor III

Ok TVAL0 -> 0x00030D40 and CVAL0 -> 0x00012629. But my configurations are the same as I posted here.

0 Kudos
3,134 Views
namnguyenviet
NXP Employee
NXP Employee

Hello talha.uyar@karel.com.tr‌,

I have just carefully looked into this thread, and I found the strange thing here:

Muhammet Uyar wrote:

Ok TVAL0 -> 0x00030D40 and CVAL0 -> 0x00012629. But my configurations are the same as I posted here.

After exit GPT_StartTimer, TDVAL0 should be 0x003D0900 (in dec: 4000000), however TDVAL0 is only 0x00030D40 (in dec: 200000) => timer is extremely shorter than it should be. Can you set breakpoint in this line in Gpt_LPit_StartTimer:

REG_WRITE32(LPIT_TVAL_ADDR32((uint32)u8ModuleIdx,(uint32)u8ChannelIdx),u32Value);

and see the exact value was written to TDVAL register?

Nam

0 Kudos
3,134 Views
talha_uyar
Contributor III

These register values are just after the overflow. Sorry for the mistake, in the previous code I did some stuff in the timeout function. These are measured just after the timer overflow. Still the timer overflows much faster than 1 second.Capture.PNG

0 Kudos
3,134 Views
talha_uyar
Contributor III

I have also checked the TVAL value just after 

REG_WRITE32(LPIT_TVAL_ADDR32((uint32)u8ModuleIdx,(uint32)u8ChannelIdx),u32Value);

TVAL0 -> 0x003D0900

0 Kudos
3,134 Views
namnguyenviet
NXP Employee
NXP Employee

Hello talha.uyar@karel.com.tr‌,

Till now, honestly I have no idea what cause the issue. All the registers' value seem to be correct. I assumed there is no issue with SW here, probably it comes from HW.

Could you try with SDK driver to see if the issue still happens? I supposed you might need to use LPIT and Clock driver from SDK. If the issue is still same, then probably your HW has problems, otherwise I will re-check MCAL driver to see any gaps or dependences.

Best Regards,

Nam

0 Kudos
3,134 Views
talha_uyar
Contributor III

I really hoped you wouldn't say this was a hardware issue. I can share the .elf file for my main project so that you can test it in your hardware. If you have S32K118 EVB, this code should change the colour of the RGB LED every at every two seconds (Blue -> Green -> White -> Red). If it is also changing its value at every two seconds in your hardware, then it cannot be a hardware issue, right?

I will also build a sample project from the SDK and test it as you have suggested.

0 Kudos
3,134 Views
talha_uyar
Contributor III

I have built lpit_periodic_interrupt_s32k118 example project and it is working fine. It is overflowing exactly at the expected time. I don't think the issue is with the hardware. 

I am almost certain that the associated clock for the LPIT in my project has the frequency of 20 MHz instead of 4 MHz. I have no idea why but this is almost definitely the source of my problems.

0 Kudos
3,130 Views
talha_uyar
Contributor III

Also, I have looked at the lpit_periodic_interrupt_s32k118 example and saw that FIRC was used as the clock source for LPIT, so I tried doing that. It works, the frequency of my timer is 24 MHz when I use FIRC as the source instead of SOSC.

Is there any particular reason to set SOSC for the LPIT clock source? Is it in any way advantageous to do so? If not, I will just change my clock source to FIRC.

0 Kudos
3,134 Views
namnguyenviet
NXP Employee
NXP Employee

Hello,

Probably the CLK source for LPIT is too much high, leads to CVAL (Counter value) decreases too fast. Please check your PCC and SCG registers:

PCC.JPG

SCG.JPG

Best Regards,

Nam

0 Kudos
3,134 Views
talha_uyar
Contributor III

I think the register values are the same.

Capture.PNG

I also think this about me using the wrong clock source. When I call the function Gpt_StartTimer(Sample_Timer, 24000000) it overflows at 1 second. This kind of indicates that I am using SYS_CLK instead of SOSCDIV2 which is at 24 MHz instead of 4 MHz. I cannot understand why, though.

0 Kudos
3,134 Views
namnguyenviet
NXP Employee
NXP Employee

Hello,

Can you check exact period time the LPIT overflow in case of using SOSC with 4MHz clock source?

Nam

0 Kudos
3,134 Views
talha_uyar
Contributor III

Ok, I don't exactly know how to measure the exact period without manually timing it. I have changed my timer to Gpt_StartTimer(Sample_Timer, 400000000); and added two breakpoints: one just before I started the timer and the other when the timer overflows. The period is about 20 seconds. So I guess the period  of my original Gpt_StartTimer(Sample_Timer, 4000000); is 0.2 seconds.

Again this is not precise considering I have manually measured the time between the timer starts and overflows but it is a very close estimate (definitely between 20 seconds and 21 seconds -> 0.2 seconds to 0.21 seconds for 4 MHz).

0 Kudos