OpenPLC_Editor icon indicating copy to clipboard operation
OpenPLC_Editor copied to clipboard

WIP: Add support for ESP32S3

Open rhysperry111 opened this issue 2 years ago • 2 comments

Starting to try and add support for the ESP32S3. Main issues with this was the board does not have a DAC so PWM has to be used as a crude substitute.

While modifying the ESP32 file I also made the PWM_CONTROLLER implementation more flexible, moving away from FastPWM which seems to be abandoned over to the more standard LEDC.

Would appreciate some help as in order to use the required LEDC channel initiation function, esp32:esp32 core >3.0.0 must be used. I have made an attempt to switch over to the development library sources, but I'm not just getting a bunch of undefined reference errors when compiling.

Changes made:

  • Remove fastPWM dependency as it seems unmaintained and has issues with duty cycle 0% and 100% (at least on my board). Switch to the LEDC library which is bundled with the ESP32 core.
    • This also allowed for static channel<->pin<->timer mappings in esp32.cpp to be removed. Now the PWM_CONTROLLER block will simply take the board pin as the channel, and will auto assign a timer from those available. (BREAKING)
    • Move to dev version of ESP32 core as current version doesn't support auto assigning timers. (see espressif/arduino-esp32#8796)
  • Add the ESP32-S3 board
    • Use PWM to emulate analog outputs (at 4khz) on boards without a DAC

rhysperry111 avatar Mar 15 '24 14:03 rhysperry111

Any update on inclusion of this feature?

OliverB21 avatar Mar 20 '24 13:03 OliverB21

AFAIK it should be all good to be merged, however I have been unable to test everything as a whole because of compile errors when using the dev ESP32 core. I have no idea how to fix the issues with compilation in the current state, however you can track the progress of the 3.0.0 core release using the milestone on espressif/arduino-esp32.

You shouldn't need the specific hardware to reproduce the compilation error, so help from anyone with Arduino compiler knowledge would be appreciated.

rhysperry111 avatar Mar 20 '24 13:03 rhysperry111

@me-no-dev helped fix the compilation over in an issue in the espressif repo. Will add a commit to change some flags and then after some testing this should be ready for merge.

rhysperry111 avatar Apr 24 '24 18:04 rhysperry111

Seems to be working - although I've had to do a hacky workaround because subprocess.Popen really doesn't seem to like the idea of having an argument to a command with a space in it. Will see what I can do tomorrow

note for tomorrow me: some basic "$@" arg debugging seems to suggest getting rid of quotes might help

rhysperry111 avatar Apr 24 '24 21:04 rhysperry111

Looks all good from my side although I haven't been able to test anything on an OS other than Linux. I ended up removing the quotes from the compile args as they seemed to be messing with things.

rhysperry111 avatar Apr 30 '24 08:04 rhysperry111

@rhysperry111 Thank you so much for all your changes. I took a look at them and they look good, however I have a main (huge) concern. If I understood it correctly, you're replacing the ESP32 board file source with another source from Espressif that is basically a dev branch. Is that right? I'm worried about making that change and in one way or another break all the other ESP32 boards that are currently working fine on OpenPLC. Could we make that change exclusive for the ESP32S3?

thiagoralves avatar Apr 30 '24 14:04 thiagoralves

If I understood it correctly, you're replacing the ESP32 board file source with another source from Espressif that is basically a dev branch. Is that right?

Yeah that's correct. It's because the new esp32.cpp file uses functions which are only available in the 3.0.0 release which is currently in rc2. If it eases your conscience the dev sources aren't nightly but just the release candidates.

I'm worried about making that change and in one way or another break all the other ESP32 boards that are currently working fine on OpenPLC. Could we make that change exclusive for the ESP32S3?

Not really an easy way to do this without splitting off esp32.cpp as well so I'd rather not. I don't see any reason why other boards would break (although there is a breaking change in that PWM_CONTROLLER now maps channels to pins differently).

I'm happy to wait until 3.0.0 is in a stable release if it's a huge problem.

rhysperry111 avatar Apr 30 '24 16:04 rhysperry111

Yeah, I believe waiting for a final release would be better. I'm just thinking about a million of other things that can go wrong, especially some crucial things like Serial that directly affects live monitoring.

thiagoralves avatar Apr 30 '24 16:04 thiagoralves

3.0.0 has just been released upstream - not sure if there's any testing of other areas of code (serial, WiFi) that you want to do. I'll send in a commit switching back to stable sources soon.

rhysperry111 avatar May 27 '24 14:05 rhysperry111

Awesome! If you can, just make sure that a simple program compiles for the ESP32 and that Modbus serial and TCP work

thiagoralves avatar May 28 '24 22:05 thiagoralves

It seems that ESP32 core 3.0.0 is broken. I updated my core and it won’t compile the blink example. See https://openplc.discussion.community/post/esp32-compilation-failed-13378321?pid=1339594513

thiagoralves avatar May 29 '24 10:05 thiagoralves

3.0.0 is not broken. There is something wrong on your end @thiagoralves

me-no-dev avatar May 29 '24 10:05 me-no-dev

Probably due to the overriding of compile flags. One of the commits in this PR fixes that

rhysperry111 avatar May 29 '24 11:05 rhysperry111

@rhysperry111 is this ready to merge or do you still need to change the board source file back to the original?

thiagoralves avatar May 29 '24 19:05 thiagoralves

Already changed the board file back with the force push two days ago. I've not done any testing to verify modbus over RTU, however I had modbus over TCP working.

Probably worth merging so at least ESP32 will compile again and then fixing anything that crops up with another PR. More than happy to help with that.

rhysperry111 avatar May 29 '24 19:05 rhysperry111

Awesome! Merging now. Thanks again!

thiagoralves avatar May 29 '24 20:05 thiagoralves

If you plan on doing a release with this in, it's worth mentioning that existing ESP32 projects that use PWM_CONTROLLER blocks will need to be tweaked slightly.

The pin input to the block now directly maps to the output PWM pin rather than being an index to an arbitrary mapping

rhysperry111 avatar May 29 '24 20:05 rhysperry111