Visual Studio for Arduino development and STM32F4 support
I've been having some fun with Arduino boards over the last few weeks. I've been mainly using the UNO R3 board. There are three main drawbacks on using these devices, two tied to the microcontrollers they use, and one on the IDE:
- they are slow;
- they have very few memory available in both the RAM and Flash departments;
- Arduino IDE is, for lack of better word, "not very good".
The project
I've been working on a small project to build a multi-sensor "environment monitoring" device, with an SPL meter and a temperature and humidity sensor. The goal is to add a few other sensors as well, depending on which other things can be easily monitored. The output of these sensors will be displayed on a nice TFT screen.
Problem #1: performance
It seems like a simple and small project, right? Well, even that simple of a project stretched the limits of an humble Uno R3 board. Basic TFT screen tests, although successful, were not quite what I expected. Nearly one full second to paint the screen one solid color is atrocious, but that's what 16MHz gives you.
Problem #2: memory (or lack thereof)
Next step for the project is to add the DHT sensor library. Built, deployed, and... Nothing™. The TFT screen libraries are apparently so big, that adding anything else to the project causes the application to run out of RAM. 2KB of RAM means you can't do a lot with the thing. To be honest, the libraries I use are probably a bit bloated (pun intended), but it's not supposed to break that easily.
Solving problem #1 and #2
I went shopping and found an Arduino Mega2560, which has a whooping 8KB of RAM, but still only has a 16MHz clock speed. My test project with all the libraries loaded works now, but the board is gigantic. Even the Uno R3 is slightly too big for my needs.
That's when I found the STM32F422CEU6-based "Black Pill" board. This development board has 512KB of Flash memory, 128KB of RAM, and a 100MHz ARM Cortex M-4 core, which is plenty more resources than I could possibly need for that particular application. The only problem with these boards is that they are not exactly Arduino boards, and can't be programmed the same way.
Luckily, the good people at STMicroelectronics provided a set of board drivers and libraries for use with Arduino IDE! But the whole "uploading stuff to the board" is not quite working. You still need to have the STM32CubeProgrammer software installed and it's an overall mess that the Arduino IDE is not equipped to handle.
Problem #3: the IDE
With the "hardware performance" problem solved, this leads to my third problem: Arduino IDE. Don't get me wrong. It's an AWESOME IDE for new developers who will only use Arduino-branded (or the fully-compatible) hardware. It's relatively clean and simple to use which make it perfect for beginners. It does have a nice library manager feature, very similar to NPM or Nuget, but system-wide instead of project specific. Once the library is installed, you can simply add the necessary #include statements and you're good to go. The things that I don't like*?
- Projects are unmanageable. You cannot easily add/rename/delete files.
- Corollary: subfolder are not a thing in projects.
- It absolutely REQUIRES a communication port to be specified to build and upload a project even when the uploading part is done through a separate process configured at IDE level. This means you can't really use it for STM32 development.
- It is horribly memory-hungry.
- Does it really need to spawn 11 processes when it starts?!? Look at this crap! That's 508.06MB just to show the IDE.
- Performances degrade the more you use it. You have to restart it every few hours, otherwise memory and CPU usage get out of hand (memory jumps from 400MB to 2.6GB in seconds, then gets freed, then gets reserved again, on, and on, and on, all while consuming a steady 25-35% of CPU resources).
- It's unstable and hangs often for no reason.
- Try to save a new sketch and the screen will blank out. Did your code save or not? Who knows!
- Code "intelligence" is practically non-existing. It can't figure out symbols correctly and the parser sometimes just... gives up? But it still displays errors from code that has long since changed.
- Building code is extremely, horribly, painfully slow, particularly if you use not-technically-Arduino boards.
- I don't think it caches the boards libraries it compiles and recompiles them for every project.
- IDE configuration is minimal/non-existent and not very flexible.
*Observations made on Arduino IDE 2.0RC6.
Does it work? Yes, most of the times. Is it easy to use? I guess so... once you you get used to how clumsy the interface feels. Is it good for the weekend warrior? Absolutely. Would I recommend it to someone with any programming experience? Not at all.
Visual Studio to the rescue!
I'm not exactly sure why it's not that widely discussed in Arduino forums, but you can use Visual Studio (a 100% tried-and-true professional IDE) to develop code for the Arduino platform. There's this wonderful extension available to add Arduino support called Arduino IDE for Visual Studio made by Visual Micro. Visual Studio Community is free, but the extension is not. It's priced at 49$ for students and hobbyist, and 200$ for a commercial license. It is well-worth the price.
You'll need the Arduino IDE installed still (for the libraries it provides), but you will not have to launch it since the extension provides it's own library and board manager that fetches from the same sources.
This extension gives you access to new Arduino-specific projects types and all the things you might need from the original Arduino IDE, all from within Visual Studio. This is really the best of both worlds.
Visual Studio's IntelliSense and coding tools are so much better than those from Arduino IDE. Its configuration is unparalleled since you can change pretty much every single thing in its behaviour and UI. This is a big plus for seasoned developers who might want to retain control on most of their environment's behaviour and look-and-feel.
Probably the best thing this extension has going for it though, is that deployment. just. work. Out of the box. As long as the board libraries are installed and the settings are correct, it will work. I wasted 4 hours trying to get Arduino IDE to deploy to my Black Pill board and only got pain and sadness in return for my efforts. With Visual Studio and Visual Micro's extension, I got the Blink sample built and deployed in less than 2 minutes on each board I tried it with. Overall build times are quite consistent as well. Once common libraries are built, they are cached and ready to be reused in any project, not just the one they've been built for. The extension also has a serial port monitor for boards that supports it (so most Arduino-compatible boards), and it works quite well.
Certainly, an advanced, professional IDE such as Visual Studio takes up a lot of resources, right? Yes and no. It depends of how it's set up, the features you are using, and the installed extensions. Currently, the instance I have running is sitting at 549.93MB with a project loaded, built, and deployed a few times.
A new, empty instance will probably consume between 200 and 280MB. Again this depends of configuration and active extensions. Your mileage may vary, but it has a similar memory use with a project loaded to what Arduino IDE consumes after only starting up and displaying an empty window.
In terms of CPU usage patterns, unless IntelliSence is doing something or your code is compiling/deploying, Visual Studio is not really CPU-hungry. It's very stable in my experience.
Summary
Arduino boards are really good and fun to work with. They are, however, very limited in resources and that can hamper your projects (but that is expected with devices like these). Other devices, like ESP32- and STM32F4-based boards, provide more RAM, Flash, and processing power compared to some Atmel microcontrollers used in Arduino devices, making them ideal for more complex and-or resource-intensive projects. You might be limited by the number of available pins, though...
Developing applications for Arduino (or similar) devices can be done in many ways. Arduino IDE is one of them, but it's not the best. It's good for beginners or hobbyists to develop simple project and get the hang of software development, but Arduino IDE is not equipped or made to handle complex project structures. It tries really hard, but it's falling short. Visual Studio with Visual Micro's Arduino IDE for Visual studio extension is your best bet to add Arduino support to a professional IDE and get the best of both worlds.
Comments
Post a Comment