S32k342 Using Pit Ip to track milliseconds.

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

S32k342 Using Pit Ip to track milliseconds.

Jump to solution
1,770 Views
pb632146
Contributor V

This is a little bit more of a C question, but I am trying to emulate the functionality of the arduino millis() command which returns how long the program has been on in milliseconds. I figure to do that I would want to use the PIT_IP to increment a global variable every millisecond, but I am having the issue that for a simple delay function where I call a function to return the value to set the starting point like I would with OsIf_GetCounter, the value will update when the timer does, rather than holding the current value of the timer. 

 

As a result the delay function I want to make does not work as well as other functions I have that want to use the current value as a timestamp in a similar function OsIf_GetCounter.

 

pb632146_0-1737692102722.pngpb632146_0-1737692102722.png

 

The interrupt works, I just do not know how to disconnect the variable's value from directly referencing the global.

pb632146_1-1737692160853.pngpb632146_1-1737692160853.png

pb632146_2-1737692167414.pngpb632146_2-1737692167414.png

Are there alternatives to this? I want to avoid OsIf_GetCounter as it only uses 32 bits at the real clock speed which would overflow too often for my needs.

 

0 Kudos
Reply
1 Solution
1,721 Views
pb632146
Contributor V
Turning the PIT timer off and on as a delay function is interesting, I will keep it in my mind for the future.

Strangely the issues seemed to be as a result of the debugger/compiler optimizations, as the issue would change from updating the startTime variable when the global was updated to not updating the global at all with the interrupt presumably not triggering.

But the end goal was for using the counter for timestamping to keep track of on/off times rather than delaying and it seems to work correctly in that situation.

View solution in original post

0 Kudos
Reply
2 Replies
1,735 Views
RomanVR
NXP Employee
NXP Employee

Hi @pb632146.

To assist you more effectively, could you please clarify the specific functionality you need to accomplish? Are you looking for a delay function using the PIT, or are you trying to measure execution time?

If you need to implement a blocking delay with the PIT, I suggest to follow the structure shown in the image below. It was tested for a 1 second delay and it works as expected:

RomanVR_0-1738014764334.pngRomanVR_0-1738014764334.png

The PIT is configured to interrupt every millisecond which updates the "counter" variable. The PitDelay function starts the timer and continuously checks for the "counter" to reach the delay value. Once it does, the variable restarts, and the PIT is stopped.

If you need to measure execution time, you can update a counter variable each time the PIT gets into the interruption. This will provide an accurate estimation of the running time of your program.

Please let me know if this helps.

- RomanVR.

Best Regards!
0 Kudos
Reply
1,722 Views
pb632146
Contributor V
Turning the PIT timer off and on as a delay function is interesting, I will keep it in my mind for the future.

Strangely the issues seemed to be as a result of the debugger/compiler optimizations, as the issue would change from updating the startTime variable when the global was updated to not updating the global at all with the interrupt presumably not triggering.

But the end goal was for using the counter for timestamping to keep track of on/off times rather than delaying and it seems to work correctly in that situation.
0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2033589%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ES32k342%20Using%20Pit%20Ip%20to%20track%20milliseconds.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2033589%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EThis%20is%20a%20little%20bit%20more%20of%20a%20C%20question%2C%20but%20I%20am%20trying%20to%20emulate%20the%20functionality%20of%20the%20arduino%20millis()%20command%20which%20returns%20how%20long%20the%20program%20has%20been%20on%20in%20milliseconds.%20I%20figure%20to%20do%20that%20I%20would%20want%20to%20use%20the%20PIT_IP%20to%20increment%20a%20global%20variable%20every%20millisecond%2C%20but%20I%20am%20having%20the%20issue%20that%20for%20a%20simple%20delay%20function%20where%20I%20call%20a%20function%20to%20return%20the%20value%20to%20set%20the%20starting%20point%20like%20I%20would%20with%20OsIf_GetCounter%2C%20the%20value%20will%20update%20when%20the%20timer%20does%2C%20rather%20than%20holding%20the%20current%20value%20of%20the%20timer.%26nbsp%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EAs%20a%20result%20the%20delay%20function%20I%20want%20to%20make%20does%20not%20work%20as%20well%20as%20other%20functions%20I%20have%20that%20want%20to%20use%20the%20current%20value%20as%20a%20timestamp%20in%20a%20similar%20function%20OsIf_GetCounter.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pb632146_0-1737692102722.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22pb632146_0-1737692102722.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F321166iDF5B9025C7DE8D6A%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22pb632146_0-1737692102722.png%22%20alt%3D%22pb632146_0-1737692102722.png%22%20%2F%3E%3Cspan%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Epb632146_0-1737692102722.png%3C%2Fspan%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThe%20interrupt%20works%2C%20I%20just%20do%20not%20know%20how%20to%20disconnect%20the%20variable's%20value%20from%20directly%20referencing%20the%20global.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pb632146_1-1737692160853.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22pb632146_1-1737692160853.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F321167i2D6DE174A7970782%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22pb632146_1-1737692160853.png%22%20alt%3D%22pb632146_1-1737692160853.png%22%20%2F%3E%3Cspan%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Epb632146_1-1737692160853.png%3C%2Fspan%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pb632146_2-1737692167414.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22pb632146_2-1737692167414.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F321168iDF85D9D39BA685CE%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22pb632146_2-1737692167414.png%22%20alt%3D%22pb632146_2-1737692167414.png%22%20%2F%3E%3Cspan%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Epb632146_2-1737692167414.png%3C%2Fspan%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EAre%20there%20alternatives%20to%20this%3F%20I%20want%20to%20avoid%20OsIf_GetCounter%20as%20it%20only%20uses%2032%20bits%20at%20the%20real%20clock%20speed%20which%20would%20overflow%20too%20often%20for%20my%20needs.%3C%2FP%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2034867%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20S32k342%20Using%20Pit%20Ip%20to%20track%20milliseconds.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2034867%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ETurning%20the%20PIT%20timer%20off%20and%20on%20as%20a%20delay%20function%20is%20interesting%2C%20I%20will%20keep%20it%20in%20my%20mind%20for%20the%20future.%3CBR%20%2F%3E%3CBR%20%2F%3EStrangely%20the%20issues%20seemed%20to%20be%20as%20a%20result%20of%20the%20debugger%2Fcompiler%20optimizations%2C%20as%20the%20issue%20would%20change%20from%20updating%20the%20startTime%20variable%20when%20the%20global%20was%20updated%20to%20not%20updating%20the%20global%20at%20all%20with%20the%20interrupt%20presumably%20not%20triggering.%3CBR%20%2F%3E%3CBR%20%2F%3EBut%20the%20end%20goal%20was%20for%20using%20the%20counter%20for%20timestamping%20to%20keep%20track%20of%20on%2Foff%20times%20rather%20than%20delaying%20and%20it%20seems%20to%20work%20correctly%20in%20that%20situation.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2034798%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20S32k342%20Using%20Pit%20Ip%20to%20track%20milliseconds.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2034798%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F230291%22%20target%3D%22_blank%22%3E%40pb632146%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3ETo%20assist%20you%20more%20effectively%2C%20could%20you%20please%20clarify%26nbsp%3Bthe%20specific%20functionality%20you%20need%20to%20accomplish%3F%26nbsp%3BAre%20you%20looking%20for%20a%20delay%20function%20using%20the%20PIT%2C%20or%20are%20you%20trying%20to%20measure%20execution%20time%3F%3C%2FP%3E%0A%3CP%3EIf%20you%20need%20to%20implement%20a%20blocking%20delay%20with%20the%20PIT%2C%20I%20suggest%20to%20follow%20the%20structure%20shown%20in%20the%20image%20below.%20It%20was%20tested%20for%20a%201%20second%20delay%20and%20it%20works%20as%20expected%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22RomanVR_0-1738014764334.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22RomanVR_0-1738014764334.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F321574i38FBFD6B4A2159C0%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22RomanVR_0-1738014764334.png%22%20alt%3D%22RomanVR_0-1738014764334.png%22%20%2F%3E%3Cspan%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3ERomanVR_0-1738014764334.png%3C%2Fspan%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThe%20PIT%20is%20configured%20to%20interrupt%20every%20millisecond%20which%20updates%20the%26nbsp%3B%22counter%22%20variable.%26nbsp%3BThe%20PitDelay%20function%20starts%20the%20timer%20and%26nbsp%3Bcontinuously%20checks%20for%20the%20%22counter%22%20to%20reach%20the%20delay%20value.%26nbsp%3BOnce%26nbsp%3Bit%20does%2C%20the%20variable%20restarts%2C%20and%20the%20PIT%20is%20stopped.%3C%2FP%3E%0A%3CP%3EIf%20you%20need%20to%20measure%20execution%20time%2C%20you%20can%20update%20a%20counter%20variable%20each%20time%20the%20PIT%20gets%20into%20the%20interruption.%20This%20will%20provide%20an%20accurate%20estimation%20of%20the%20running%20time%20of%20your%20program.%3C%2FP%3E%0A%3CP%3EPlease%20let%20me%20know%20if%20this%20helps.%3C%2FP%3E%0A%3CP%3E-%20RomanVR.%3C%2FP%3E%3C%2FLINGO-BODY%3E