Skip to content

Instantly share code, notes, and snippets.

@ctring
Last active February 15, 2024 11:36
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctring/7f12d812fb594eecc493 to your computer and use it in GitHub Desktop.
Save ctring/7f12d812fb594eecc493 to your computer and use it in GitHub Desktop.
Convert from SysCtlDelay in TivaWare library to milliseconds and microseconds delay.
/*
* delay.c - Delay in millisecond and microsecond functions
*
* Created on: Jul 7, 2014
* Author: Cuong T. Nguyen
*/
#include <stdint.h>
#include <stdbool.h>
#include "driverlib/sysctl.h"
void delayMs(uint32_t ui32Ms) {
// 1 clock cycle = 1 / SysCtlClockGet() second
// 1 SysCtlDelay = 3 clock cycle = 3 / SysCtlClockGet() second
// 1 second = SysCtlClockGet() / 3
// 0.001 second = 1 ms = SysCtlClockGet() / 3 / 1000
SysCtlDelay(ui32Ms * (SysCtlClockGet() / 3 / 1000));
}
void delayUs(uint32_t ui32Us) {
SysCtlDelay(ui32Us * (SysCtlClockGet() / 3 / 1000000));
}
@anhtu91
Copy link

anhtu91 commented Feb 13, 2021

Hi, bro. How do you know 1 SysCtlDelay = 3 clock cycle? It is not mentioned in reference manual.

@ctring
Copy link
Author

ctring commented Feb 13, 2021

It was 7 years ago so I'm not exactly sure where I got that number. A Google search for SysCtlDelay yields this document (which looks very familiar to me so highly likely I got the number from it). On page 77, it says:

SysCtlDelay() is a loop timer provided in TivaWare. The count parameter is the loop count, not the actual delay in clock cycles. Each loop is 3 CPU cycles

@OmarRamadan1
Copy link

Hi, bro. How do you know 1 SysCtlDelay = 3 clock cycle? It is not mentioned in reference manual.

It is actually mentioned in the SysCtlDelay description in the api manual as the following "This function provides a means of generating a delay by executing a simple 3 instruction cycle
loop a given number of times. It is written in assembly to keep the loop instruction count
consistent across tool chains."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment