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

8 REPLIES 8
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.

Farah-Chelly
Associate

S'il vous plaît.. j'ai un code Arduino pour un projet sur esp32 .. (le code représente un bluetooth car controller) 

Je veux le convertir en un langage c pour un STM32F407.

S'il vous plaît, est ce que vous pouvez m'aider.. c'était urgent .

Et merci d'avance.

"Please.. I have an Arduino code for a project on esp32.. (the code represents a bluetooth car controller) 

I want to convert it to a c language for a STM32F407.

Please, can you help me... it was urgent. And thank you in advance."

For do-your-job type "Urgent" perhaps see   https://www.fiverr.com/    http://www.catb.org/~esr/faqs/smart-questions.html#urgent

Hard to know how portable your current code is. The Arduino drivers hide a lot of the detail/complexity and are in C++. You should review the code for the project in it's totality, all the source is within the Arduino environment.

Generally what's in the INO is relatively top-level and superficial, the real complexity will be within the drivers and how it interacts with the hardware. Functionality of the ESP32 may simply not exist in the STM32 library code.

Look at what stm32duino currently offers as platforms.

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