cancel
Showing results for 
Search instead for 
Did you mean: 

Convert Arduino code to STM32 IDE code

Good B
Associate II

Hi,

I would like to convert Arduino code to STM32 IDE code.

Could you please suggest for me ?

Thank you

6 REPLIES 6
turboscrew
Senior III

If it's just a test code, have you considered STM32Duino?

The conversion is not that simple. I think you need to recode the program manually.

http://wiki.stm32duino.com/index.php?title=Main_Page

Good B
Associate II

Hi

Thank you very much for your suggestion.

I tried to learn many IDEs and compare its function such as Arduino, CooCox, IAR, Keil, Mikroelektronika, System Workbench for STM32, TrueSTUDIO and etc.

But I would like to use STM32 IDE. Because it is free and great IDE for me.

>I tried to learn many IDEs ...

An IDE is just the human interface (HMI) to the toolchain, i.e. compiler/linker (or interpreter).

Most of the named IDEs are based on a C/C++ toolchain, so the best option is to come to grips with that language.

AFAIK there exists no "translator" between Arduino and C/C++. I think you would need to rewrite your code.

vitorengcruz
Associate II

Hi @Community member​ ,

Are you create your project with STM32CubeMX? I highly recommend, it's a piece of cake to configure and generate all peripherals code!

You can use advanced macros to swap arduino standard routines with STM32CubeMX, like this:

// Define the GPIO pin and port, that was originally connected arduino Digital 10 and 11, in the STM32.

#define D10_Pin GPIO_PIN_4

#define D10_GPIO_Port GPIOC

#define D11_Pin GPIO_PIN_5

#define D11_GPIO_Port GPIOC

// Advanced macro to swap routine names and parameters.

#define digitalWrite(PIN_NAME,PIN_STATE)   HAL_GPIO_WritePin(D##PIN_NAME##_GPIO_Port,D##PIN_NAME##_Pin,GPIO_PIN_##PIN_STATE)

#define digitalRead(PIN_NAME)   HAL_GPIO_ReadPin(D##PIN_NAME##_GPIO_Port,D##PIN_NAME##_Pin)

// Simple test of this implementation. Should work like a champ!

void test (){

  if( digitalRead(11) )

  digitalWrite(10,SET);

else

digitalWrite(10,RESET);

}

You can put this code in another C file, I recommend create it in the Middlewares folder, like this: "Middlewares/MyCode/source".

In the main.c file, add the prototypes:

void setup ();

void loop ();

Between USER_CODE labels.

And in the main routine, add setup and loop calls:

int main () {

[...] // STM32CumeMX code Generated.

/* Infinite loop */

 /* USER CODE BEGIN WHILE */

setup();

 while (1)

 {

   /* USER CODE END WHILE */

   /* USER CODE BEGIN 3 */

  loop();

}

  /* USER CODE END 3 */

}

Be aware of the USER CODE labels, as the STM32CubeMX will only leave user code between this labels! Otherwise It will be deleted every time you generate code!!

More about macros here:

https://www.cprogramming.com/tutorial/cpreprocessor.html

Or you can simply refactor code using "Refactor->Rename... Alt+Shift+R" tool in every routine and pin names.

Best regards.

I just wanna say that thank you !!

your explanation is great and clearly

Good B
Associate II

Thank you very much for your help.