<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://miosix.org/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Filippo.motti</id>
	<title>Miosix Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://miosix.org/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Filippo.motti"/>
	<link rel="alternate" type="text/html" href="https://miosix.org/wiki/index.php?title=Special:Contributions/Filippo.motti"/>
	<updated>2026-05-17T10:56:05Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.3</generator>
	<entry>
		<id>https://miosix.org/wiki/index.php?title=ST_HAL_Support_in_Miosix&amp;diff=372</id>
		<title>ST HAL Support in Miosix</title>
		<link rel="alternate" type="text/html" href="https://miosix.org/wiki/index.php?title=ST_HAL_Support_in_Miosix&amp;diff=372"/>
		<updated>2026-03-21T10:33:43Z</updated>

		<summary type="html">&lt;p&gt;Filippo.motti: This page contains a guide on how to port code that uses the HAL from ST into Miosix&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;If you are developing or have existing code that uses the HAL from ST Microelectronics, you can port it fairly easily into Miosix by following this guide:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: The content of this page was tested on Miosix v3.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: Miosix uses C++ instead of C&lt;br /&gt;
&lt;br /&gt;
;1. Follow the [[Quick start]] guide to configure the kernel and installing the needed programs&lt;br /&gt;
;2. Include &amp;quot;miosix.h&amp;quot; in the main file of the code (remember that the file main file must be named &amp;quot;main.cpp&amp;quot;);&lt;br /&gt;
;3. Remove the clock configuration: Miosix already handles the clock configuration and keeping it in the code would result in a lock of the board.&lt;br /&gt;
&lt;br /&gt;
:Usually the clock configuration is generated by STM32Cube and it looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
static void SystemClock_Config(void)&lt;br /&gt;
{&lt;br /&gt;
 RCC_ClkInitTypeDef RCC_ClkInitStruct;&lt;br /&gt;
 RCC_OscInitTypeDef RCC_OscInitStruct;&lt;br /&gt;
 &lt;br /&gt;
 /* Enable Power Control clock */&lt;br /&gt;
 __HAL_RCC_PWR_CLK_ENABLE();&lt;br /&gt;
 &lt;br /&gt;
 /* The voltage scaling allows optimizing the power consumption when the device is &lt;br /&gt;
    clocked below the maximum system frequency, to update the voltage scaling value &lt;br /&gt;
    regarding system frequency refer to product datasheet.  */&lt;br /&gt;
 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);&lt;br /&gt;
 &lt;br /&gt;
 /* Enable MSI Oscillator */&lt;br /&gt;
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;&lt;br /&gt;
 RCC_OscInitStruct.MSIState = RCC_MSI_ON;&lt;br /&gt;
 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;&lt;br /&gt;
 RCC_OscInitStruct.MSICalibrationValue=0x00;&lt;br /&gt;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;&lt;br /&gt;
 if(HAL_RCC_OscConfig(&amp;amp;RCC_OscInitStruct) != HAL_OK)&lt;br /&gt;
 {&lt;br /&gt;
   /* Initialization Error */&lt;br /&gt;
   Error_Handler();&lt;br /&gt;
 }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
:If your code contains interrupts refer to the section below.&lt;br /&gt;
&lt;br /&gt;
:Here an example code of the main.cpp for a blinking led in a Nucleo-L053R8 board:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;miosix.h&amp;quot;&lt;br /&gt;
#include &amp;quot;stm32l053xx.h&amp;quot;&lt;br /&gt;
#include &amp;quot;stm32l0xx_hal.h&amp;quot;&lt;br /&gt;
#include &amp;lt;interfaces/bsp.h&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
using namespace miosix;&lt;br /&gt;
&lt;br /&gt;
static void MX_GPIO_Init(void);&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  iprintf(&amp;quot;Hello world, write your application here\n&amp;quot;);&lt;br /&gt;
  HAL_Init();&lt;br /&gt;
&lt;br /&gt;
  MX_GPIO_Init();&lt;br /&gt;
  for (;;) {&lt;br /&gt;
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);&lt;br /&gt;
    HAL_Delay(500);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
static void MX_GPIO_Init(void) {&lt;br /&gt;
  __HAL_RCC_GPIOC_CLK_ENABLE();&lt;br /&gt;
  __HAL_RCC_GPIOA_CLK_ENABLE();&lt;br /&gt;
&lt;br /&gt;
  GPIO_InitTypeDef GPIO_InitStruct = {0};&lt;br /&gt;
&lt;br /&gt;
  // Configure Led pin&lt;br /&gt;
  GPIO_InitStruct.Pin = GPIO_PIN_5;&lt;br /&gt;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;&lt;br /&gt;
  GPIO_InitStruct.Pull = GPIO_NOPULL;&lt;br /&gt;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;&lt;br /&gt;
&lt;br /&gt;
  HAL_GPIO_Init(GPIOA, &amp;amp;GPIO_InitStruct);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
;4. Include in the Makefile all the libraries and all the file .c needed to compile the program&lt;br /&gt;
Remember to include the HAL libraries and .c files used&lt;br /&gt;
&lt;br /&gt;
;5. add a CFLAGS and CXXFLAGS that defines the type of board that you are using in the Makefile &lt;br /&gt;
:(ex: CFLAGS += -DSTM32L053xx and CXXFLAGS += -DSTM32L053xx  for a stm32l053-R8).&lt;br /&gt;
:If you want, you can instead uncomment the type of the board in the Miosix folder (ex: for an stm32l053-R8, you need to uncomment &amp;quot;#define STM32L053xx&amp;quot; in &amp;quot;miosix/arch/common/CMSIS/Device/ST/STM32L0xx/Include/stm32l0xx.h&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
;5b. You need to do that also for any Shield that you are using.&lt;br /&gt;
&lt;br /&gt;
;6. Include also the file wrapper.cpp that handles the function HAL_Delay() for Miosix, since Miosix handles the clock&lt;br /&gt;
&lt;br /&gt;
:Here the part that you need to modify in the example of the Makefile for the previous example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Prevoius code&lt;br /&gt;
SRC :=main.cpp \&lt;br /&gt;
    STM32CubeL0/Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal.c \&lt;br /&gt;
    STM32CubeL0/Drivers/BSP/STM32L0xx_Nucleo/stm32l0xx_nucleo.c \&lt;br /&gt;
	STM32CubeL0/Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_gpio.h \&lt;br /&gt;
	STM32CubeL0/Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_cortex.c \&lt;br /&gt;
	wrapper.cpp&lt;br /&gt;
&lt;br /&gt;
INCLUDE_DIRS := -ISTM32CubeL0/Drivers/STM32L0xx_HAL_Driver/Inc \&lt;br /&gt;
	-ISTM32CubeL0/Drivers/BSP/STM32L0xx_Nucleo \&lt;br /&gt;
	-Imiosix/arch/common/CMSIS/Device/ST/STM32L0xx/Include/ \&lt;br /&gt;
	-ISTM32CubeL0/Projects/NUCLEO-L053R8/Examples/GPIO/GPIO_IOToggle/Inc&lt;br /&gt;
&lt;br /&gt;
#Other code&lt;br /&gt;
&lt;br /&gt;
CFLAGS += -DSTM32L053xx&lt;br /&gt;
&lt;br /&gt;
CXXFLAGS += -DSTM32L053xx&lt;br /&gt;
&lt;br /&gt;
#Other code&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
;7. Compile and flash the program. This is done by using the command &amp;quot;make&amp;quot; and &amp;quot;make program&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== Interrupt ===&lt;br /&gt;
&lt;br /&gt;
Miosix handles interrupts differently than ST and thus the code needs to be adapted.&lt;br /&gt;
&lt;br /&gt;
In Miosix interrupts need to be registered in order to be called. This is done by creating a lock and registering the IRQ like this example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  GlobalIrqLock lock; //disable interrupts on the core that acquired the lock (all in single core)&lt;br /&gt;
  IRQregisterIrq(lock, SPI2_IRQn, &amp;amp;SPIx_IRQHandler); //setup for SPI2, SPIx_IRQHandler is the handler called when the SPI finished sending a message&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This needs to be done for each interrupt that you have to register.&lt;br /&gt;
&lt;br /&gt;
Keep in mind that the lock is made using a RAII style, so the lock exist as long the variable exists and the lock, thus encapsulating in brackets will deallocate it automatically when no longer needed.&lt;/div&gt;</summary>
		<author><name>Filippo.motti</name></author>
	</entry>
</feed>