# coretex-m4(STM32F407VG discovery) Questions about implementing vTaskDelay and Delay

**URL:** <https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975>\
**Category:** Kernel\
**Created:** [January 12, 2018, 1:40am UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975 "2018-01-12T01:40:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 12, 2018, 1:40am UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975/1 "2018-01-12T01:40:28Z")

</div>

**[jonginkk](https://sourceforge.net/u/jonginkk/)** wrote on [Friday, January 12, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee2acb9/#c7c8):

Hello. I am new to FreeRTOS.  
I have analyzed and understood the Scheduling method using Task.  
I want to run the PID control source through FreeRTOS scheduling.

I included several functions in the FreeRTOS, such as the libraries tm\_stm32f4\_delay.h and tm\_stm32f4\_delay.c, which were imported from outside, and found that SysTick\_Handler duplicated.  
tm\_stm32f4\_delay.c defines the millis and delay functions we need to do the PID.

To solve this problem, I commented out the SysTick\_Handler in the section of tm\_stm32f4\_delay.c and solved the compilation problem. I understood that [[#define xPortSysTickHandler SysTick\_Handler]], which is embedded in FreeRTOS, is used for interrupts and is absolutely necessary.

After all, we need to implement the Delay function needed for the PID. The tool I am using is IAR.

The above is a description of the situation I experienced, and the questions are as follows.

1. I want to use the function defined in the header file included in TaskFunction via vTaskDelay. Can this header file serve as a delay through a function called vTaskDelay?

2. I read the contents of [https://www.freertos.org/FreeRTOS\_Support\_Forum\_Archive/November\_2013/freertos\_configTICK\_RATE\_HZ\_8fc54ac4j.html](https://www.freertos.org/FreeRTOS_Support_Forum_Archive/November_2013/freertos_configTICK_RATE_HZ_8fc54ac4j.html), but I do not understand.  
We implemented a delay using the vTaskDelay function, but this did not allow accurate ms Delay.  
I set it to #define configTICK\_RATE\_HZ ((TickType\_t) 1000) and I wanted a delay of 500ms, but when I measured it with an oscilloscope, it took about 2 seconds of delay.  
There is only one external\_LED\_TASK task to operate.

[//delay.h](https://delay.h) function  
void external\_delay\_test (void)  
{  
// const TickType\_t xDelay = 500 / portTICK\_PERIOD\_MS;

```
unsigned int n_iCnt = 0;
for (;;)
{

```

// vTaskList (ptrTaskList);  
// printf (“ptrTaskList \ n”);  
if ((n\_iCnt% 2) == 0)  
{  
GPIO\_SetBits (GPIOD, GPIO\_Pin\_4);  
}  
else if ((n\_iCnt% 2) == 1)  
{  
GPIO\_ResetBits (GPIOD, GPIO\_Pin\_4);  
}  
// puts (“LED2”);  
printf (“PD4 ::::% d \ n”, n\_iCnt);

```
    vTaskDelay (500);
    n_iCnt ++;
}

```

}

[//main.c](https://main.c) Taskfunction  
void external\_LED\_TASK (void \* pvParameter)  
{  
external\_delay\_test ();  
}

I have implemented a for (;😉 loop in external\_delay\_test, which is not implemented as a loop in TaskFunction itself.  
We have confirmed that it works well when scheduling multiple TaskFunctions.

1. Is the structure I think is wrong? If so, can you use vTaskDelay to delay for exactly one second? Or can I re-implement Delay?

I need help.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [January 12, 2018, 4:55am UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975/2 "2018-01-12T04:55:55Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, January 12, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee2acb9/#c7c8/de58):

> I included several functions in the FreeRTOS, such as the libraries  
> tm\_stm32f4\_delay.h and tm\_stm32f4\_delay.c,

These are ST files that call FreeRTOS functions - rather than FreeRTOS  
files.

> which were imported from  
> outside, and found that SysTick\_Handler duplicated.

If you were to create the FreeRTOS project yourself it shouldn’t be  
duplicated. Likewise, if you were to create the project using STM32Cube  
it shouldn’t be duplicated. Note however that if you are using a  
project generated by STM32Cube then it will install the ST HAL systick  
handler, and call the FreeRTOS systick handler from that.

> 1. 
> 
> ```
> I want to use the function defined in the header file included in
> TaskFunction via vTaskDelay. Can this header file serve as a delay
> through a function called vTaskDelay?
> 
> ```

Sorry - I don’t understand the question. If you want to use  
vTaskDelay() then #include FreeRTOS.h and task.h (in that order), and  
just call vTaskDelay(). The delay period is in ‘ticks’, so if you want  
to specify it in ms then use the pdTICK\_TO\_MS() macro.

> 1. 
> 
> ```
> I read the contents of
> https://www.freertos.org/FreeRTOS_Support_Forum_Archive/November_2013/freertos_configTICK_RATE_HZ_8fc54ac4j.html,
> but I do not understand.
> 
> ```

That is a rather old and somewhat random page to reference. Better to  
reference the API documentation and the free to download book.

> ```
> We implemented a delay using the vTaskDelay function, but this did
> not allow accurate ms Delay.
> I set it to #define configTICK_RATE_HZ ((TickType_t) 1000) and I
> wanted a delay of 500ms, but when I measured it with an
> oscilloscope, it took about 2 seconds of delay.
> 
> ```

I’m guessing the value of configCPU\_CLOCK\_HZ is wrong.  
configTICK\_RATE\_HZ is derived from configCPU\_CLOCK\_HZ so if the later is  
wrong then the former will be calculated incorrectly.

configCPU\_CLOCK\_HZ should be set to the frequency of the clock that is  
feeding the SysTick timer.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 12, 2018, 5:38am UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975/3 "2018-01-12T05:38:50Z")

</div>

**[jonginkk](https://sourceforge.net/u/jonginkk/)** wrote on [Friday, January 12, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee2acb9/#8a7e):

Thank you for your reply. I am a beginner, so the question would be insufficient.

I include FreeRTOS.h and task.h in global.h and global.h in the source definition file.  
vTaskDelay () works. However, accurate time calculation is not possible.  
My board is stm32f407, which has a clock of 168 MHz.

#if defined (STM32F40\_41xxx)  
uint32\_t SystemCoreClock = 168000000;

#define configCPU\_CLOCK\_HZ (SystemCoreClock)

If so, where is the macro called pdTICK\_TO\_MS?

Is it possible to use the pdTICK\_TO\_MS to make the exact ms calculation possible with vTaskDelay?  
I have checked #define portTICK\_RATE\_MS portTICK\_PERIOD\_MS similar to FreeRTOS.h. pdTICK\_TO\_MS is invisible.  
If possible, please give an example of how to use this.  
I need a delay function that can calculate the exact ms or s and give a delay.

please help me. thanks.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [January 12, 2018, 5:54am UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975/4 "2018-01-12T05:54:44Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, January 12, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee2acb9/#8a7e/f90f):

> uint32\_t SystemCoreClock = 168000000;
> 
> define configCPU\_CLOCK\_HZ (SystemCoreClock)

Setting the variable SystemCoreClock is not enough to actually program  
the clocks on the MCU so that the actual clock frequency matches  
SystmeCoreClock. That is normally done by a function called something  
like (but not actually) SystemInit(). Search the source files using  
grep or something similar for every instance of SystemCoreClock to see  
where it is used, and if it is used in a function that sets up the  
actual clocks, make sure the function is being used.

> If so, where is the macro called pdTICK\_TO\_MS?

Sorry - I had that the wrong way around. It is pdMS\_TO\_TICKS(). You  
specify a time in ms and get back the equivalent time in ticks. So  
pdMS\_TO\_TICKS( 10 ) gives you the number of ticks in 10ms.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 12, 2018, 6:27am UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975/5 "2018-01-12T06:27:12Z")

</div>

**[jonginkk](https://sourceforge.net/u/jonginkk/)** wrote on [Friday, January 12, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee2acb9/#6efa):

grep, or SystemCoreClock to find the source file and use the actual clock  
When used in a function to set the function to use  
Make sure you’re on.

- I did not understand this part. SystemCloreClock is defined in system\_stm32f4xx.c, and SystemCoreClock is also used.  
If so, can you suggest how to set up SystemCoreClock to do the right thing?

With configTIC\_RATE\_HZ = 1000,  
vTaskDelay (100); Was measured at 321ms.

As your reply  
vTaskDelay (pdMS\_TO\_TICKS (100))Was measured at 160ms.  
vTaskDelay (pdMS\_TO\_TICKS (200))Was measured at 176ms…

I do not know what problem this is caused by.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [January 12, 2018, 5:00pm UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975/6 "2018-01-12T17:00:59Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, January 12, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee2acb9/#6efa/7142):

> grep, or SystemCoreClock to find the source file and use the actual clock  
> When used in a function to set the function to use  
> Make sure you’re on.
> 
> - I did not understand this part. SystemCloreClock is defined in  
> system\_stm32f4xx.c, and SystemCoreClock is also used.  
> If so, can you suggest how to set up SystemCoreClock to do the right  
> thing?

SystemCoreClock will not be used in many places within the source files

- just look in the source files to see every place it is used. Once of  
the places it will be used is in a function that configures the MCU  
clocks. SystemCoreClock holds the desired clock frequency, so the  
function will use SystemCoreClock to set the actual clock frequency to  
the desired clock frequency. Once you find the function that sets the  
actual clock frequency then make sure the function is actually being  
called. If the function is not called then it is likely the MCU clocks  
are just left at their default power up values, so your timing will not  
be accurate.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 14, 2018, 2:30am UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975/7 "2018-01-14T02:30:55Z")

</div>

**[jonginkk](https://sourceforge.net/u/jonginkk/)** wrote on [Sunday, January 14, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee2acb9/#224f):

Thank you for your answer.

I have analyzed HSE\_VALUE in the stm32f4xx.h file for your answer.  
This variable appears to have a constant value of 25MHz. Is this really the true external clock value, which means XTAL?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 14, 2018, 2:30am UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975/8 "2018-01-14T02:30:59Z")

</div>

**[jonginkk](https://sourceforge.net/u/jonginkk/)** wrote on [Sunday, January 14, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee2acb9/#0b9f):

Thank you for your answer.

I have analyzed HSE\_VALUE in the stm32f4xx.h file for your answer.  
This variable appears to have a constant value of 25MHz. Is this really the true external clock value, which means XTAL?

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [January 14, 2018, 4:11am UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975/9 "2018-01-14T04:11:09Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Sunday, January 14, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee2acb9/#2622):

Although your question about frequencies is outside the scope of FreeRTOS, I will try to answer it shortly:

The clock starts with a High-Speed External **oscillator** (HSE), which has a frequency of e.g. 8, 12, or 25 Mhz (between 4 and 26 MHz).

This clock rate will go through a PLL: a unit that can both divide and multiply clock rates.  
Every STM32F part has its own optimum working frequency (`SystemCoreClock`). This frequency has nothing to do with the oscillator but with the part itself.

Here is an example:

OSC (HSE) = 25 MHz

```auto
    #define HSE_VALUE ((uint32_t)25000000)

```

`PLL_M`: The Oscillator frequency will first be divided by `PLL_M` to get 1 MHz. So for a 25 MHz oscillator, PLL\_M is set to 25.

`PLL_N`: Main PLL (PLL) multiplication factor to get VCO (Voltage Controlled Oscillator). If `PLL_N` equals 336, then 1 MHz will turn into 336 MHz. This is a very high frequency which will be divided again to get workable frequencies: both for the CPU itself, as well as for some peripherals (e.g. USB).

`PLL_P`: Main PLL (PLL) division factor for main system clock

```auto
    00: PLLP = 2 // 336/2 = 168 Mhz
    01: PLLP = 4 // 336/4 = 84 MHz
    10: PLLP = 6
    11: PLLP = 8

```

`PLL_Q`: Main PLL (PLL) division factor for USB OTG FS

`PLL_Q` is often set at 7 to get : 336/7 = 48 MHz, which is the working speed for USB.

In the STM32F reference you will find a “clock tree”, which explains every detail.

Personally, I found the HAL drivers and example projects confusing. I would have liked to see a set of obligatory defines for the above values. These defines depend both on the part (maximum speed), as well as on the hardware (oscillator).

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 15, 2018, 4:48am UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975/10 "2018-01-15T04:48:16Z")

</div>

**[jonginkk](https://sourceforge.net/u/jonginkk/)** wrote on [Monday, January 15, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee2acb9/#8690):

Thank you. I changed PPL\_M = 25 to PPL\_M = 8,  
We changed the HSE\_VALUE = 2500000 to HSE\_VALUE = 800000 to get the correct clock value. The external clock settings on my board were set differently.  
I have solved this problem with your help. I learned a lot.  
If you use printf with freeRTOS, can you explain why the delays increase and explain the solution?

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [January 15, 2018, 5:12am UTC](https://forums.freertos.org/t/coretex-m4-stm32f407vg-discovery-questions-about-implementing-vtaskdelay-and-delay/6975/11 "2018-01-15T05:12:00Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Monday, January 15, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/6ee2acb9/#3dca):

I’m not sure how your `printf()` is implemented, most probably it will send the bytes to a serial port in a polling way. If so, try to set the baudrate at 115200 baud, which will give a throughput of about 11 KByte/sec. Still you will see delays of a few msec.  
There are other ways of using the serial port: either interrupt-driven and/or with DMA. Please refer the [ST’s site](http://www.st.com/content/st_com/en.html) and [forum](https://community.st.com/community/stm32-community/stm32-forum/content) to get more information about logging and `printf()`.  
And further: `printf()` is probably not thread-aware, and I think that you may never call it from within an ISR (Interrupt Service Routine).  
If you want to log a time-critical process, I think you better just put the information into an array of strings, and inspect that when the process is ready.
