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.

clock source to generate time in micrisecond

Hi,

i am new to bear metal concepts, i am trying to get the clock source to generate clock function to get time in microsecond.

I need any source file or example which demonstrate  time in microsecond  return from the clock() function using system clock or any other source. Please help

best regards

Subhash 

  • I am using RM57 eval board and trying to start with timer first. i get some compiler library to get the clock() function but need system clock source to get my clock function work. please help
  • Hi Subhash,

     I will suggest you use the RTI (Real Time Interrupt) module to setup precise timer. The example for RTI can be found if you go to HalCoGen->Help->Help Topics->Examples->example_rtiBlinky.c.

  • Hi Charles,

    thanks for the quick updated. I refereed your link and able to generate the microsecond time.
    below are my changes for your reference


    void rtiInit(void)
    {
    /* USER CODE BEGIN (2) */
    /* USER CODE END */
    /** @b Initialize @b RTI1: */

    /** - Setup NTU source, debug options and disable both counter blocks */
    rtiREG1->GCTRL = (uint32)((uint32)0x5U << 16U) | 0x00008000U;

    /** - Setup timebase for free running counter 0 */
    rtiREG1->TBCTRL = 0x00000000U;

    /** - Enable/Disable capture event sources for both counter blocks */
    rtiREG1->CAPCTRL = 0U | 0U;

    /** - Setup input source compare 0-3 */
    rtiREG1->COMPCTRL = 0x00001000U | 0x00000100U | 0x00000000U | 0x00000000U;

    /** - Reset up counter 0 */
    rtiREG1->CNT[0U].UCx = 0x00000000U;

    /** - Reset free running counter 0 */
    rtiREG1->CNT[0U].FRCx = 0x00000000U;

    /** - Setup up counter 0 compare value
    * - 0x00000000: Divide by 2^32
    * - 0x00000001-0xFFFFFFFF: Divide by (CPUC0 + 1)
    */
    rtiREG1->CNT[0U].CPUCx = 74U;

    /** - Reset up counter 1 */
    rtiREG1->CNT[1U].UCx = 0x00000000U;

    /** - Reset free running counter 1 */
    rtiREG1->CNT[1U].FRCx = 0x00000000U;

    /** - Setup up counter 1 compare value
    * - 0x00000000: Divide by 2^32
    * - 0x00000001-0xFFFFFFFF: Divide by (CPUC1 + 1)
    */
    rtiREG1->CNT[1U].CPUCx = 74U;

    /** - Setup compare 0 value. This value is compared with selected free running counter. */
    rtiREG1->CMP[0U].COMPx = 9375U;

    /** - Setup update compare 0 value. This value is added to the compare 0 value on each compare match. */
    rtiREG1->CMP[0U].UDCPx = 9375U;

    /** - Setup compare 1 value. This value is compared with selected free running counter. */
    rtiREG1->CMP[1U].COMPx = 46875U;

    /** - Setup update compare 1 value. This value is added to the compare 1 value on each compare match. */
    rtiREG1->CMP[1U].UDCPx = 46875U;

    /** - Setup compare 2 value. This value is compared with selected free running counter. */
    rtiREG1->CMP[2U].COMPx = 75000U;

    /** - Setup update compare 2 value. This value is added to the compare 2 value on each compare match. */
    rtiREG1->CMP[2U].UDCPx = 75000U;

    /** - Setup compare 3 value. This value is compared with selected free running counter. */
    rtiREG1->CMP[3U].COMPx = 93750U;

    /** - Setup update compare 3 value. This value is added to the compare 3 value on each compare match. */
    rtiREG1->CMP[3U].UDCPx = 93750U;

    /** - Clear all pending interrupts */
    rtiREG1->INTFLAG = 0x0007000FU;

    /** - Disable all interrupts */
    rtiREG1->CLEARINTENA = 0x00070F0FU;

    /** @note This function has to be called before the driver can be used.\n
    * This function has to be executed in privileged mode.\n
    * This function does not start the counters.
    */

    /* USER CODE BEGIN (3) */
    rtiStartCounter(rtiREG1, rtiCOUNTER_BLOCK0);
    rtiStartCounter(rtiREG1, rtiCOUNTER_BLOCK1);
    /* USER CODE END */
    }
    uint32
    getMicroSeconds(void)
    {
    return rtiREG1->CNT[0].FRCx;
    }



    please suggest if i am going in right direction
  • Hi Subhash,

    I don't really see anything wrong. I don't know what frequency you have configured for RTICLK. But it looks like you are enabling 4 different compares. I will suggest that you call rtiStartCounter() in the main() instead of inside the rtiInit() to keep it clean.

    Normally the way to use RTI module is for the module to generate a periodic interrupt according to your configured timebase. For example, you can configure the RTI to generate an interrupt every 1ms. CPU responds to the periodic interrupt to perform the task you want to execute every 1ms.