cancel
Showing results for 
Search instead for 
Did you mean: 

Delay function for STM32F4

theanswer
Associate II
Posted on July 08, 2015 at 13:50

Does anyone know of a good delay function I could use with my STM32F4. I need delay in miliseconds and microseconds.

#nano-delay() #ns-delay
20 REPLIES 20
Posted on July 08, 2015 at 20:40

Yes, but you can't do the comparison that way. The counter is 32-bit, and it wraps. Every 25.5 seconds at 168 MHz.

You do the delta measurement to compute the elapsed time, and exit when it exceeds that.

What units is ''length'' expressed in?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 08, 2015 at 20:44

*DWT_CYCCNT is the equivalent to millis(), but it's unit of counting is several nano-seconds. If you want milliseconds configure a timer with a 1 MHz time base and use TIM2->CNT in place of millis().

unsigned int ontime = (SystemCoreClock/1000)*(unsigned int)length; // length in units of milliseconds

Computing endtime and waiting for that is broken logic. It's a common coding error that breaks a lot of systems.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 08, 2015 at 20:51

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/%5bbug%20report%5d%20Bug%20in%20HAL%20%28SPL%29%20delay%20function&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=1421]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2F%5Bbug%20report%5D%20Bug%20in%20HAL%20%28SPL%29%20delay%20function&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=1421

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
theanswer
Associate II
Posted on July 09, 2015 at 12:07

Sorry if this is a stupid question but how do you configure such timer. I have a problem with ARR and Prescaler values.

kyu
Associate II
Posted on September 23, 2015 at 22:44

Hi Clive1,

In my configuration

SystemCoreClock for STM32F4Discovery is 168Mhz.

SystemCoreClock/1000000 = 168 (1 us)

SystemCoreClock/1000 = 168000 (1 ms)

SystemCoreClock/ X = Y (Z ns)

What is the maximum number for X? Y=76 is the best number for best ns accuracy.   Based on my experiment Z is not linear (with X and Y).

I am using AHB1 (150MHz) for GPIO Port B using HAL lib.

How does SystemCoreClock, cystic_config(), and ABH1 (GPIO) work together to measure the duration of a logic ''1'' pulse on GPIO Port B:0?

How does difference between AHB1 and SystemCoreClock impact the accuracy in timing?

My plan is to use 

HAL_GPIO_ReadPin() but not sure how how the different timing will work together.

Thank you!

kyu
Associate II
Posted on September 23, 2015 at 22:46

Hi Clive1,

In my configuration

SystemCoreClock for STM32F4Discovery is 168Mhz.

SystemCoreClock/1000000 = 168 (1 us)

SystemCoreClock/1000 = 168000 (1 ms)

SystemCoreClock/ X = Y (Z ns)

What is the maximum number for X? Y=76 is the best number for best ns accuracy.   Based on my experiment Z is not linear (with X and Y).

I am using AHB1 (150MHz) for GPIO Port B using HAL lib.

How does SystemCoreClock, cystic_config(), and ABH1 (GPIO) work together to measure the duration of a logic ''1'' pulse on GPIO Port B:0?

How does difference between AHB1 and SystemCoreClock impact the accuracy in timing?

My plan is to use 

HAL_GPIO_ReadPin() but not sure how how the different timing will work together.

Thank you!

kyu
Associate II
Posted on September 23, 2015 at 22:49

Hi Clive1,

In my configuration....

SystemCoreClock for STM32F4Discovery is 168Mhz.

SystemCoreClock/1000000 = 168 (1 us)

SystemCoreClock/1000 = 168000 (1 ms)

SystemCoreClock/ X = Y (Z ns)

What is the maximum number for X?

Y=76 is the best number I found for best ns accuracy.   Based on my experiment Z is not linear (with X and Y).  How do I achieve Z (in ns, us, ms)?

I am using AHB1 (150MHz) for GPIO Port B using HAL lib.

How does SystemCoreClock, cystic_config(), and ABH1 (GPIO) work together to measure the duration of a logic ''1'' pulse on GPIO Port B:0?

How does difference between AHB1 and SystemCoreClock impact the accuracy in timing?

My plan is to use 

HAL_GPIO_ReadPin() but not sure how how the different timing will work together.

Thank you!

kyu
Associate II
Posted on September 23, 2015 at 22:50

Hi Clive1,

In my configuration....

SystemCoreClock for STM32F4Discovery is 168Mhz.

SystemCoreClock/1000000 = 168 (1 us)

SystemCoreClock/1000 = 168000 (1 ms)

SystemCoreClock/ X = Y (Z ns)

What is the maximum number for X?

Y=76 is the best number I found for best ns accuracy.   Based on my experiment Z is not linear (with X and Y).  How do I achieve Z (in ns, us, ms)?

I am using AHB1 (150MHz) for GPIO Port B using HAL lib.

How does SystemCoreClock, cystic_config(), and ABH1 (GPIO) work together to measure the duration of a logic ''1'' pulse on GPIO Port B:0?

How does difference between AHB1 and SystemCoreClock impact the accuracy in timing?

My plan is to use 

HAL_GPIO_ReadPin() but not sure how how the different timing will work together.

Thank you!

kyu
Associate II
Posted on September 23, 2015 at 22:51

Hi Clive1,

In my configuration....

SystemCoreClock for STM32F4Discovery is 168Mhz.

SystemCoreClock/1000000 = 168 (1 us)

SystemCoreClock/1000 = 168000 (1 ms)

SystemCoreClock/ X = Y (Z ns)

What is the maximum number for X? 

Y=76 is the best number I found for best ns accuracy.   Based on my experiment Z is not linear (with X and Y).  How do I achieve Z (in ns, us, ms)?

I am using AHB1 (150MHz) for GPIO Port B using HAL lib.

How does SystemCoreClock, cystic_config(), and ABH1 (GPIO) work together to measure the duration of a logic ''1'' pulse on GPIO Port B:0?

How does difference between AHB1 and SystemCoreClock impact the accuracy in timing?

My plan is to use 

HAL_GPIO_ReadPin() but not sure how how the different timing will work together.

Thank you!

Posted on September 23, 2015 at 23:09

The forum is throwing all kinds of errors when posting, double check if your post actually succeeded by opening the forum in another tab.

Well you have to watch for it exceeding 32-bits, and doing the math in a way that preserves some of the precision. Could be rounded too, but the loop is going to take a few extra cycles.

ticks = (SystemCoreClock  * nano) / 1000000000;

Not sure what you precise question is, I'm not using HAL, and you can't interrupt at MHz rates.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..