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.

PROCESSOR-SDK-AM335X: How to make a accurate timer with 1 ms

Part Number: PROCESSOR-SDK-AM335X
Other Parts Discussed in Thread: AM3352

Hi Team,

Could you tell me how to implement an accurate timer with 1 ms?

As I know, create a thread and call usleep() function is not a good way and it is also not accurate.

 SDK Version: Linux-RT Processor SDK for AM335x 08.02.00.24

But I am not sure about the frequency of the MCU due to RT SDK does not support the feature "scaling_cur_freq".

  • Hello Woody,

    Please describe your usecase. What do you want to do with the 1ms timer?

    Regards,

    Nick

  • Hi Nick,

    My case would be I have a counter and increase it every 100 ms in user space.

    I found the timer is not really accurate If I use usleep() to count it.

    Do you have any suggestion?

    Besides, the SDK I use is Linux-RT Processor SDK for AM335x 08.02.00.24, so I don;t really know what the frequency is running now on AM3352.

  • Hello Woody,

    What is the purpose of the counter that increments every 100ms?

    There are multiple ways to keep track of time, trigger events, etc. And different AM335x part numbers will have different options on them. So my suggestion would change depending on what you are trying to do in your design.

    Regards,

    Nick

  • Hi Nick,

    For example, I would like to use accumulate the time of power-on time.

    So I have a thread to be a timer and accumulate seconds, minutes, hours and days.

    For another example, GPIO interrupt, I would like to receive a GPIO interrupt and wait about 50 milliseconds then to do something.

    Thanks.

  • Hello Woody,

    Measuring processor runtime

    Will your application modify the Linux system time? If not, you can just use that for measuring the processor runtime. Just grab a timestamp immediately after you boot, and then you can just subtract that from the current time to get the total runtime.

    Keep in mind that you will want to use 64 bit Linux time instead of 32 bit time (assuming you want your application to continue running after 2038). Support for 64 bit time on 32 bit processors (like AM335x) was added in Linux kernel 5.6, so support will be there in SDK 8.2. Search the web for more resources on how to use 64 bit time on 32 bit Linux processors (that's a generic Linux question, not a question specifically about TI software, so I cannot support it on the forums).

    Responding to signals on a pin 

    Linux is NOT a real time OS (even "real time" Linux is not truely real time. For more details, see https://e2e.ti.com/support/processors-group/processors/f/processors-forum/1085663/faq-sitara-multicore-system-design-how-to-ensure-computations-occur-within-a-set-cycle-time ).

    If you don't actually care about precise timing on your GPIO signals, go ahead and use Linux to control the GPIO, receive the interrupt, and sleep until you need it to do something.

    If you need precise timing on the GPI / GPO signals, please take a look at an AM335x part number that includes the PRU subsystem. The PRU is a baremetal core, placed physically close to the dedicated PRU GPI / PRU GPO pins, with a direct signal connection from the PRU's internal registers to the pins. It is 100% deterministic, and designed for precise "bitbanging" (i.e., reading signals and toggling signals high and low) - the AM335x PRU is so precise and low latency that it can implement 100Mbit Ethernet, standard protocols like UART, custom protocols, etc.

    Regards,

    Nick