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.

CC2340R5: [Timer] I want to achieve a microsecond level delay

Part Number: CC2340R5

Tool/software:

I want to achieve a microsecond level delay, such as delay (1us) or delay (5us).

But in the example code, I can only find sleep() for seconds, usleep() for ms, and nanosleep() that I don't know how to use.

I would like to know if there are other available functions that can achieve microsecond level delay, such as the __delay_cycles() function in msp430.

  • Hi, 

    usleep() will gives you sleeps in 1 micro-second step. 

    Regards, 

  • But I have tried to use usleep, but it is actually delaying in milliseconds. 

    The values I actually input can only be in units of 1k. For example, usleep(2000), usleep(3000).

    #define configTICK_RATE_HZ (1000UL)
    #define TICK_PERIOD_USECS (1000000L / configTICK_RATE_HZ)

    int usleep(useconds_t usec)
    {
    TickType_t xDelay;

    /* usec must be less than 1000000 */
    if (usec >= 1000000)
    {
    errno = EINVAL;
    return (-1);
    }

    /* take the ceiling */
    xDelay = (usec + TICK_PERIOD_USECS - 1) / TICK_PERIOD_USECS;

    /* must add one tick to ensure a full duration of xDelay ticks */
    vTaskDelay(xDelay + 1);

    return (0);
    }

  • Hi, 

    Thank you for the additional details you have provided. 

    Using ClockP_usleep() should do the trick. Can you please test it for me? 

    Best regards, 

  • Hi Clément,

    Thanks for your help. ClockP_usleep() can work successfully.

    I just have one more question that i want to delay 1us and then switch the voltage of one pin, while the time spent is about 1.5 us. I used to heard that GPIO_toggle() will cost some time about 0.5 us. Is this the reason why 0.5us is redundant?  

    Best regards,
    Redmond.