running an esptool command from the IDE [SOLVED]

For any reason I have one type of ESP32(with external flash) which needs to get an esptool-erase_region command before uploading the code from the IDE, otherwise I'll get a MD-checksum Error and the programming is aborted.

I can send this command from a shell besides the IDE, but it would be more convenient to integrate this in the build process. Any ideas how to handle this?

Thank in advance.

You would do this in the platform.txt of the ESP32 hardware core. The Arduino IDE does support pre and post build hooks, but unfortunately there are no upload hooks. So you'll need to figure out how to incorporate your command into tools.esptool_py.upload.pattern, tools.esptool_py.upload.pattern.linux, and maybe tools.esptool_py.upload.network_pattern too?

Oh that's a good hint, thank you very much.
I'll get to accomplish this an mark this topic as RESOLVED if I'm successful.

You're welcome. Let me know if you need any help finding the platform.txt file. You might check whether you can use the platform.local.txt feature:

I couldn't remember whether that overrides the properties in platform.txt, but from the documentation it sounds like it does. The benefit of platform.local.txt is that you can make your customizations in that file instead of needing to modify platform.txt, then save a copy of your platform.local.txt. That way, when you update to a new version of the ESP32 core and your modifications are lost you can just copy your platform.local.txt back into the folder.

Note that you need to restart the Arduino IDE before any changes you make to platform.txt or platform.local.txt will take effect.

Well I tried to write it in the platform.local.txt but it wasn't evaluated even I restarted the IDE. So I modified the platform.txt directly and it worked like a charm. For my case it isn't necessary to perform the esptool task immediately before uploading it's good enough to do it before compilation.
THIS WAS THE FASTEST HELP in a forum EVER,
Again thank you very much!

I'm glad to hear it's working! The reason I was not liking the idea of using the build hooks is because they run even if you are just doing a compile, and not uploading. I'm not very familiar with esptool, so I didn't know whether that command would work without a board connected, but I guess that's no problem.

It's a shame platform.local.txt didn't work for this. The next step up from there would be to make your own custom hardware package that references the core, tools, and variants of the ESP32 core. That would only require a custom boards.txt and platform.txt. The advantage of that is it wouldn't be affected by updates to the ESP32 core.

pert:
I'm glad to hear it's working! The reason I was not liking the idea of using the build hooks is because they run even if you are just doing a compile, and not uploading. I'm not very familiar with esptool, so I didn't know whether that command would work without a board connected, but I guess that's no problem.

It's a shame platform.local.txt didn't work for this. The next step up from there would be to make your own custom hardware package that references the core, tools, and variants of the ESP32 core. That would only require a custom boards.txt and platform.txt. The advantage of that is it wouldn't be affected by updates to the ESP32 core.

Per, the esp8266 and esp32 packages don't work as referenced packages. problem is the tools folder

Wow, I didn't know that. That's really unfortunate. Thanks for your work to get that fixed!