This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to configure 16-bit, free-running, 1 MHz timer/counter ?

I would like to configure a timer (TIMER0A) to tick at 1 MHz and simply roll over and keep counting (up).  That is all.

This is generally pretty simple in most microcontrollers but, for some reason, I just can't make much sense of this one.

The data sheet has not been a whole lot of help in figuring out the timer modules.

Can someone provide some insight?

Thank you?

  • Hello Clark

    Please look at the following example in TivaWare Installation. It is exactly meant for what you need.

    TivaWare_C_Series-2.1.2.111\examples\peripherals\timer\periodic_16bit.c

    Regards
    Amit
  • How is generating an interrupt every millisecond the same as running a counter at 1 MHz? That's all I need it to do. I want to be able to read the value at any time and compare it to a previous value to determine elapsed time. Should be easy.
  • Hello Clark

    It is an example for configuring the timer in periodic 16-bit mode. You can change the time base according to your requirements and so can the interrupt be enabled/disabled.

    When you mention that you want the timer to be running at 1 MHz, do you mean that the system clock be 1Mhz or the timer rolls over every 1us.

    Regards
    Amit
  • I mean I want the timer to run at 1MHz.  That means it will roll over every 65536 uS.  The system clock is 80 MHz...

    I'm not using Tivaware.  Registers please.

  • First, use TivaWare. It's generally not a performance or space hog and you get the benefit of clearer, tested code. Also you are more likely to actually get help and get faster help when you do ask. And you will likely need help less.

    Second, given your stated purpose, why not use a 32 bit 80Mhz free running timer? Better resolution, greater range. There's no penalty as far as I can see.

    Robert
  • Agree w Robert - poster notes "complexity" w/in this MCU - and compounds such via, "Registers, please." (love the irony)

    There is a penalty - albeit minor - and that's the (loss) of one 16 bit timer - sacrificed @ the altar of 32 bit version. (I may be confusing this vendor's MCU w/another's...)
  • Hello Clark,

    Using TivaWare (and I strongly agree with Robert and cb1) is more useful that coding the configuration using DRM style macro.

    Reason-1: There are more chances of making mistakes
    Reason-2: When there are issues with the code not working as expected, we would ask you for TivaWare API's as debug is easier with proven functions for us to support you.

    Regards
    Amit
  • Sorry I asked.  I should have known this would devolve into a debate about coding methods.  I am trying to understand these peripherals; plugging into an API does not achieve that.  I do use some Tivaware for initialization and testing.  For those times I created a file of alias function names that actually look like C.  The only thing uglier than camel-case everywhere is Hungarian notation prefixed to camel-case everywhere.

    The reason I don't just use a 32-bit, 80MHz counter is that I want a 1 MHz counter and 16 bits is plenty.

    I would also add that I find Tivaware to be poorly planned and poorly executed.  The level of abstraction makes no sense and only serves to hide the peripherals behind a maze of function calls.   The idea of a single binary library for all targets is moronic and just asking for trouble.

  • Hello Clark,

    I can only advice.

    1. A 80Mhz uC running at 1MHz is not going to give processing benefit when the zero wait state extends up to 40MHz
    2. The benefit of the API's in ROM would be lost and the new API's would only take up flash space.
    3. You can use TivaWare to code the timer module, get it to work and then extract the "REGISTER" access for your understanding.

    Regards
    Amit
  • Don't let the fact that it is easier to use, and provides more than you need prevent you from using it.

    There is no virtue in unnecessary hardship.

    And there are things I don't like about TivaWare myself, chief among them the sloppy typing and I do agree with you about Hungarian notation. In fact I consider it to be actively harmful to good code. Having said that the library is orders of magnitude better than any previous vendor library I've seen and at a useful level of abstraction. A level of abstraction not so high as to eliminate all but a single approach and narrow subset of uses and not so low as to be a simple recapitulation of the registers.

    Robert
  • I just want to know how to configure a timer/counter to tick continuously at 1 MHz. I guess that's asking too much.
  • I have a solution to your problem that I use. I'll see if I can provide it. I just don't have it with me.

    You probably won't like it because it doesn't hold closely to your parameters but it does have the virtue of simplicity. You've already rejected one direct solution but I hold out hope for mine.

    Robert
  • What I'm trying to accomplish is a general purpose "elapsed time" counter to exit otherwise locked-loops. My approach has been to grab the value of a 1MHz counter, stash it, then subtract it from the current value to get elapsed uS. I want the interface to be the same, regardless of whose micro I happen to use. Perhaps I should rethink the methodology before my code base that uses it grows much more. Perhaps, having the "driver" return 'elapsed_uS' when passed the 'init_value' (which was returned by the 'counter_start' function).... that would work.
  • Some may consider this series of (unwarranted) attacks upon vendor & its API, "bit much."

    Perhaps a better use of that "venom" would be to solve the issue at hand - and (just maybe) direct venom at, "self-shortcomings."

  • Clark Leach said:
    Perhaps I should rethink the methodology before my code base that uses it grows much more. Perhaps, having the "driver" return 'elapsed_uS' when passed the 'init_value' (which was returned by the 'counter_start' function).... that would work.

    That's a way to approach it. Particularly if it has to work on multiple platforms. Just think of the TivaWare interface as a simplified register interface.

    Here's what I'm using. First the init

        	/* Enable wide timer 0 to give use two 32 bit timers to act as our base 
    	    clocks. */
        SysCtlPeripheralEnable(SYSCTL_PERIPH_WTIMER0);
        	/* Make sure peripherals are turned on before proceeding further */
        while(!(SysCtlPeripheralReady(SYSCTL_PERIPH_WTIMER0))){
             }
        
        	/* Set WTimer 0 B as a wrap around free running timer.  We will use this
        	   as our hardware clock. 
    	   WTimer 0 A as a wrap aound 1mS timer.  This is our time base for
    	   I/O updates.    */ 
        TimerConfigure(WTIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_PERIODIC | 
        	TIMER_CFG_A_PERIODIC);
        TimerLoadSet(WTIMER0_BASE, TIMER_B, UINT32_MAX);
        TimerLoadSet(WTIMER0_BASE, TIMER_A, (SysCtlClockGet()*1u)/1000u);
        	/* Set up timer interrupt on peripheral and NVIC */
        TimerIntEnable(WTIMER0_BASE, TIMER_TIMA_TIMEOUT);
    

    Second some related defines

    typedef uint32_t TIME_CPU;	/* Fine grained time type. */
    				
    /*** TIMESTAMP - Get fine grained time. 
     *
     * Used when higher resolution is required or when multiple independent time 
     * sources need to be kept in sync.
     * 
     * NOTE!: The timer is a countdown timer.  
     ***/
    #define TIMESTAMP()	((TIME_CPU)TimerValueGet(WTIMER0_BASE,TIMER_B))
    
    	/* Convert from mS to Fine grained time. 53mS max*/
    	/* ToDo: Make so lint can check argument */ 
    #define MS_TO_TIME_CPU(x)	((TIME_CPU)((SysCtlClockGet()*(x))/1000u)) 
    

    Note that to get a uS resolution timestamp just needs (I haven't tested or linted this, typos and logic errors may be present)

    /* No round off as long as the clock speed is a multiple of 1MHz */
    #define CPU_CLK_PER_US ((TIME_CPU)(SysCtlClockGet()/100000u))
    
    #define TIMESTAMP_US() (TIMESTAMP()/(CPU_CLK_PER_US))
    
        /* Order of subtraction reversed to account for countdown. Note do not use TIMESTAMP_US to avoid any possibility of wrap around issues. This also
    * reduces round off errors in the event that the clock is not a 1MHz multiple. */ #define ELPASED_TIMESTAMP_US(first, last) ((((first)-(last))/(CPU_CLK_PER_US)) /* Alternate version of TIMESTAMP_US that wraps on a 16 bit boundary */ #define TIMESTAMP_US_16b() ((TIMESTAMP()/(CPU_CLK_PER_US)) & 0xFFFFu)

    Note if performance is a consideration the SysCtlClockGet can be replaced with a constant. With a little bit of work I think you could also reverse the time stamp result so it appeared to be counting up. I've never found that it mattered.

    Robert

  • Let me chime in here in support of Clark.

    I too prefer to program micros at the register level whenever possible. I strongly dislike using vendor libraries for various reasons, including the fact that they're often bloated, buggy, and abstracted to unreasonable degrees. I've been doing this for thirty years now and have firmly come to the conclusion that my own code is less buggy, smaller, and abstracted only to the degree required. I also only code for the peripheral features I need; vendor libraries tend to throw in everything, including the kitchen sink.

    I've read a number of very disturbing threads on this forum just like this one where someone asks for help with a register-level issue and gets summarily told to use TivaWare as if that's the only way to solve their problem. Why is that? I don't get the same treatment on the STM32 or NXP LPC forums.

    If TI's position is that everyone should be using TivaWare and not programming at the register level, then why do they publish an 1800 page datasheet for the TM4C chips? How do they accommodate companies who have a policy against using any vendor or 3rd party code in their products?
  • I'm an outsider too - sometimes use Register Level (DRM) yet am firmly against such usage when it fails - and then posters land here - and cry for help.

    Users surely have the right to employ their method of choice and operate w/in their comfort level.  

    Yet - does not the forum staff (and out-board helpers) have rights as well - and central in that is the strong push toward the API?   You wrote (harshly & inaccurately I believe) that some are "summarily told to use the API" - and that's untrue.  Almost always - vendor staff & we outsiders - (avoid) the curse of your (summarily) by identifying these overwhelming advantages of the API:

    • API is long tried/true/tested by thousands (perhaps tens of thousands)
    • API is broken into easily viewed & disciplined "Peripheral Groupings"
    • API includes hundreds of well commented & effective, known-good, working examples
    • API source code (almost uniquely) is presented (warts & all) so that users - if they desire - may gain insight or "tweak" to their satisfaction
    • API is far easier & quicker for (both) vendor staff & outsiders to read, comprehend & troubleshoot

    And - often - if the switch to API is rejected and time, effort, & comprehension allows - the Register Methods ARE employed.   ("summarily" proves a gross over-statement (bashing) then - does it not?)

    Thus - from an MCU forum perspective - can there be any doubt that forum's central role is to benefit the many (via the mature API) - rather than the few (those expert enough to employ registers - yet not expert enough to succeed!)

    Both Register-Favoring (demanding?) posters here appear very new to this forum - I'd suspect that w/time they'd come to a far better appreciation of the value, time-savings & safe-guards which the, "time-tested, known good" API provides!

  • As far as the bloated charge goes, I took a look at a current project.

    Total code size 36318

    Breaking down by size

    Application specific hand written 28714
    Driverlib 4786
    Generated code 1616
    C library 1202

    The TivaWare overhead amounts to 13% and is large neither in proportion to the project nor the available space.

    Robert
  • Hello Robert

    Driverlib on flash or driverlib call to ROM from the application presents a smaller memory footprint, does it not?

    Regards
    Amit
  • Mes Amis, (Robert et Amit)

    Descending into "fine detail" is NOT required.

    Both Register favoring posters have failed in their Register efforts. Landed here - insist on help - yet (only) on their (narrow) terms.

    Now - if the earth (or forum) tilted upon its axis - and DRM was to predominate - just (how soon) could each/every (register only) [failing poster] be accommodated? The ability to answer properly & quickly (both wonderfully handled under the API) would instantly be destroyed!

    And this "tilt" is necessitated to satisfy (little more) than the "whims" of those seeking, "personal preference" over a far superior, long-proven, API. That is a clear statement of reality - indisputable!
     
    Had either of our register experts been versed in business - or history - they'd have noted that "LMI" - originator of these MCUs - noted the "stellar" role which the API played in driving this present vendor to its ARM MCU acquisition! (excessive time/effort - spent in "register hunt/searching" - is (well) known for negatively impacting (other) vital investigatory areas...)

  • I would expect so, my figures should represent something of a worst case.

    Robert
  • Had I wanted to ask about Tivaware, I would have. My question was regarding the TM4C, which I thought was the purpose of this forum. This is a place to ask questions, is it not?
  • And your question was very well answered by Amit (among others) - was it not?

    A "Forum Favoring" method was suggested - outside of personal preference - which did not meet your approval...

    Again - if each/every question from posters devolved to, "Answer in my way - and my way only - what then?"

    I wish you well - I'd wager high stakes that your use of the API (and review of the published source code) would quickly, comfortably drive you toward success!
  • Hello Clark,

    Absolutely. It is a forum to ask question for TM4C Hardware and Software.

    While you have rejected TivaWare outright, you have also not considered using TivaWare to extract the DRM approach as suggested earlier. We could have got both to co-exist. Why we do not recommend DRM is

    1. As the code size grows, it makes it difficult to maintain it and debug it (especially for the next person in the chain)

    2. If there is an issue, we would be slightly more inclined to the fact that while there is no errata on the module being used, that could have caused it not to work, it must be a software issue rising from an improperly coded DRM access.

    Regards

    Amit

  • Here, let me make it simpler: I don't want to use the API because I enjoy programming microcontrollers at the register level. I did this as a career for many years and now do it as a hobby, so I've no need for the "better/faster/easier" path that TivaWare may provide.

    Here's an analogy that might explain my position better: say I like to do crossword puzzles. I do them by filling in all the squares myself. You come along and say "hey, there's an easier way--just wait until tomorrow's paper comes out and gives you all the answers. Then you can go fill in all the squares on yesterday's puzzle much faster and quicker." Easier and quicker? Sure. Satisfying? No way.

    I'm really puzzled by the attitude here. I did not, and have not seem others, getting this kind of reception on the STM32 and NXP LPC forums.

    You said in your post above: " sometimes use Register Level (DRM) yet am firmly against such usage when it fails - and then posters land here - and cry for help." In that case, you can certainly exercise your right to just ignore these people. No one's forcing you to reply. But replying and saying "just use the bloody API!" is not helpful and just counterproductive.
  • My friend - you (do) tend to vast over-statement - do you not?

    First you use the word summarily - and then (shrink) from its defense.   And now you (exaggerate [again]) by grossly misquoting, "Just use the bloody API!"   Those words are yours - and yours alone - never/ever thought or uttered here.

    Is this not proof positive that your arguments are (so) empty that you must resort to "over-statement" and clear "misrepresentation" - to make your (flawed) point?

    We note that you "duck & cover" to (evade) the extra time/effort and expanded response time which your (desired) "Register Only" response demands.   You mention the ST forum - I'm active there too - and note the (absence) of vendor staff - which you fail to note...   In stark contrast - this forum is superbly attended - and proper care/consideration must be paid to forum staff.   (vendor's Amit is beyond excellent - & poster Robert provided a very full sample program - only the "most" self-centered would "protest.")

    No one seeks to "deny your programming pleasure" or NY Times x-word challenge - enjoy them.

    The attitude here - if there is one - is to guard & protect the excellent standards w/in this forum - and much credit is due to the powerful API.  

    The initial poster - then you - landing w/in his, "my way or high-way" foot-prints - seek to draw the forum into your highly personal preference - which (unduly) taxes forum staff and we (few) outsiders.   

    We responding have advised that the API (rather uniquely) includes the full source code - you can examine register usage (and sequencing) to your heart's content - and on YOUR OWN TIME/EFFORT!

    Both "Register & Only register" posters have been here ~20 minutes - is that time sufficient to (hack) the forum from its long & successful path?   I very much doubt that.    Personal preference must bow to, "Greatest Good!"    And both "Register ONLY" seeking posters clearly KNOW that!

  • Jerry, I've got similar time in this speciality as you. I share your apprehension of vendor libraries and code. If anything, I have a less flattering view.

    When I first started using this processor I fortunately(?) had a severe time limitation. That made testing the library to see if it worked worth the risk. What I found surprised me. It's built at a reasonable abstraction level and it's fairly robust.

    If it had not existed, what I would have had to write would have needed to be similar but probably less broad.

    As a side note using the library makes it easier to use TDD. The second greatest productivity booster I'm aware of.

    Robert

    Speaking of history, this reminds me of all the complaints about the abandonment of assembly in favour of C. Like that debate there really is no abandonment of one in favour of the other only greater or lesser proportions mixing.

    And TivaWare has lots of flaws, still what it does have is effective.
  • Hello Jerry,

    I could have agreed with the Analogy, only and only if TivaWare was going to be released tomorrow.

    I cannot comment on STM and NXP forums. May be they would prefer users to go through DRM and not improve on their software delivery, Again I cannot comment on why STM and NXP do what they do. There are reasons why we would like to avoid DRM and my last post has given the 2 reasons why. I believe you made the second one in your quest sir.

    EDIT: And what good is a forum if no one responds or suggest within reasonable bound of a debate an alternative with clear reasons. No one responded harshly to anyone and if they would, moderators respond or the poster can raise it up in a concerned forum/ or to TI.

    Regards
    Amit

  • I see this thread now has 481 views. Do you suppose that all these people were looking for a pointless debate about the merits of TivaWare? Probably not. It is more likely that, like myself, they just want to know if a TM4C timer can be configured to free-run at 1 MHz.

    Thanks for hijacking my thread...