# Porting STM32L4 vs STM32L5

**URL:** <https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522>\
**Category:** Kernel\
**Created:** [August 24, 2020, 6:52am UTC](https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522 "2020-08-24T06:52:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![smartin](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@smartin](https://forums.freertos.org/u/smartin)\
**Post date:** [August 24, 2020, 6:52am UTC](https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522/1 "2020-08-24T06:52:18Z")

</div>

Hi all,

Until now I had been working with FreeRTOS with a STM32L4, now I am going to start working with an STM32L5.

My first problem has been the different implementation of Porting regarding the SystickHandler.

With the STM32L4 the Systick\_Handler was used in the stm32l4xx\_it.c and in it the TickCounter of the HALs could be increased with the HAL\_IncTick() function and call the porting function xPortSysTickHandler() for the FreeRTOS tick increment,  
but now in the porting of STM32L5 this is not possible since the Systick\_Handler() is implemented directly inthe port.c file.

Why the change? Is there a way to continue using it as before without modifying the Porting?

For now what I have done has been in the port.c file, change the Systick\_Handler() to xPortSysTickHandler() and inplement my Systick\_Handler() in the same way as in STM32L4. But I don’t know if this could have other implications.

Any comments are welcome

Best Regards,

Sergio

---

<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:** [August 24, 2020, 7:28am UTC](https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522/2 "2020-08-24T07:28:11Z")

</div>

Sergio, this is indeed a bit confusing.

In a stand-alone ( bare metal ) project, the HAL library uses the system tick to count the time.

```auto
void SysTick_Handler(void)
{
  HAL_IncTick();
}

```

Now when you use FreeRTOS, it wants to use the same interrupt for its clock tick.

What you can do is the following, change the HAL function:

```auto
void SysTick_Handler(void)
{
  HAL_IncTick();
  xPortSysTickHandler();
}

```

and make sure that your FreeRTOSConfig.h does not not define `xPortSysTickHandler`.

I would recommend not to change any of the FreeRTOS kernel code, like port.c That could cause troubles when you upgrade the kernel.

If you decide to give the interrupt directly to the kernel, your `FreeRTOSConfig.h` will have this define:

```auto
    #define xPortSysTickHandler SysTick_Handler

```

The HAL function `HAL_GetTick()` is declared weak, so you can override it:

```auto
HAL_GetTick(void)
{
  return xTaskGetTickCount();
}

```

and make sure that the HAL version of `SysTick_Handler()` is not compiled ( set between `/* */` )

---

<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:** [August 24, 2020, 8:04am UTC](https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522/3 "2020-08-24T08:04:56Z")

</div>

I wrote:

```auto
uint32_t HAL_GetTick(void)
{
  return xTaskGetTickCount();
}

```

Mind you, some of the HAL init functions have loops with a time-out, as measured by `HAL_GetTick()`. As long as the kernel is not running, those loops become endless.

This could be a solution:

```auto
uint32_t HAL_GetTick(void)
{
uint32_t ulReturn;
	if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
	{
		ulReturn = ( uint32_t ) xTaskGetTickCount();
	}
	else
	{
		static uint32_t ulCounter = 0U;
		static uint32_t ulTicks = 0U;

		ulCounter++;
		if( ulCounter >= 1000U )
		{
			/* Increment the tick counter every 1000 calls. */
			ulCounter = 0U;
			ulTicks++;
		}
		ulReturn = ulTicks;
	}
	return ulReturn;
}

```

---

<div class="post-metadata">

**Author:** ![smartin](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@smartin](https://forums.freertos.org/u/smartin)\
**Post date:** [August 24, 2020, 8:56am UTC](https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522/4 "2020-08-24T08:56:04Z")

</div>

Hi,

Thank you very much, in fact that was my problem that many HAL functions use the HAL\_GetTick() before launching the scheduler, and with the redefiniton of the HAL\_GetTick function I can use it, but not with the precision of the timeout.

Best Regards,

Sergio

---

<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:** [August 24, 2020, 9:15am UTC](https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522/5 "2020-08-24T09:15:07Z")

</div>

If you want `HAL_GetTick()` to return a precise tick value, I would use the systick handler of HAL and define:

```auto
void SysTick_Handler(void)
{
  HAL_IncTick();
  if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
  {
    xPortSysTickHandler();
  }
}

```

in which case `xPortSysTickHandler` shall not be #defined.

---

<div class="post-metadata">

**Author:** ![smartin](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@smartin](https://forums.freertos.org/u/smartin)\
**Post date:** [August 24, 2020, 10:09am UTC](https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522/6 "2020-08-24T10:09:27Z")

</div>

Hi,

This is what I had to do, but in order to do it I had to modify the port.c, because in the STM32L5 porting, is defined Systick\_Handler() in this file, not xPortSysTickHandler()

Best Regards,

Sergio

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [August 24, 2020, 3:42pm UTC](https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522/7 "2020-08-24T15:42:43Z")

</div>

Hi Sergio,

Instead of changing port.c, you could put the HAL tick on another timer (other than systick). In fact this method is recommended by ST now. CubeMX generates a warning if you try to put the FreeRTOS tick and the HAL tick both on the systick timer.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [August 24, 2020, 3:55pm UTC](https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522/8 "2020-08-24T15:55:06Z")

</div>

I agree with @jefftenney that changing the HAL to use a different timer than SysTick is the right solution. The following image shows how to change HAL timer in STM32Cube IDE or CubeMX:

 ![Changing_HAL_Tick_STM32](https://d2v6vdsk2p900z.cloudfront.net/original/1X/48302d3af993dd06ad1b1c2196ac4713b317405a.jpeg)

Thanks.

---

<div class="post-metadata">

**Author:** ![smartin](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@smartin](https://forums.freertos.org/u/smartin)\
**Post date:** [August 25, 2020, 5:10am UTC](https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522/9 "2020-08-25T05:10:13Z")

</div>

Hi,

Thank you very much for yours comments @jefftenney@aggarg

Best Regards

Sergio

---

<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:** [August 25, 2020, 7:00am UTC](https://forums.freertos.org/t/porting-stm32l4-vs-stm32l5/10522/10 "2020-08-25T07:00:09Z")

</div>

> Instead of changing port.c, you could put the HAL  
> tick on another timer (other than systick).

Yes, very much agreed, that is a perfect solution.
