buttley
I had some free time so I thought I would have a look and see if I could get my ESP32-C6 working.
I’m going to go through what I did and what the problems were in as much detail as I can.
I compared the ESP32-C6 against my ESP32-Devkit which is supported by OpenPlc,
 
The first step was to make sure my copy of OpenPlc was up to date. I’m only mentioning this because I was surprised to see when I selected help then about that the date release 2024-08-28. I thought on an earlier version I seen a September date.
 
I needed to download the drive USB to UART bridge as windows put the little triangle in for it. If anyone else needs that driver it can be found.
 
 
The next step was to modify the hal.jons file.
 
  "ESP32-C6-DevKitC-1 V1.2": {
    "core": "esp32:esp32",
    "default_ain": "0, 1, 2, 3",
    "default_aout": "18, 19, 20, 21",
    "default_din": "4, 5, 10, 11",
    "default_dout": "15, 9, 22, 23, 39",
    "define": "BOARD_ESP32",
    "last_update": 0,4
    "platform": "esp32:esp32:esp32c6",
    "source": "esp32.cpp",
    "version": "3.0.4"
 
That I/O configuration seemed to work not bad for me as it gave 4 analog in, 4 analog out, 4 digital in and 4 digital out with what I call a virtual pin. I added a pin 39 but there is no real pin 39. When looking at some Arduino examples I found that this board has a built in RBG LED. If you don’t want to do anything fancy with it by toggling virtual pin 39 you can flash the RBG LED with the white color. This was handy for me as it gave me a good visual indication that microcontroller was alive and executing code.
 
I was able to download and execute code with no problems.
 
The first problem I encountered was that when I used the debug mode with modbus RTU serial. It actually connected to the board fine and was able monitor my code running in real time. Then when I hit the stop icon to quit the debug mode the code would stop executing on my microcontroller. The board will not respond to the reset at that time. You can get it to restart by cycling the power. The other interesting thing is that actually if I went back online by hitting the debug icon, the board would start executing the code again. This problem is repeatable.
 
I went back and looked at my ESP32-Devkit board. It does not seem to have the same problem when hitting the stop icon. I did note that with this board that sometimes going into debug mode it would be unsuccessful. It seems sometimes going to debug mode it would stop execution of the board. This problem was not consistent I would say about 25% of the time it would stop the board going to debug mode.
 
Anyway I went on with my testing of the C6 board. I had no issues with the debug mode using modbus TCP. I had no trouble getting to connect wifi to my router.
 
Next I tested the digital I/O that I had assigned. The inputs were more consistent using a 10K pull down resistor. Other than all the digital I/O was fine.
 
Next I went to the analog I/O. Analog inputs first. Basically I had a pot connected to the 3.3V supply going to an analog input. What I found was my input variable which was of UINT data type would roll over (65384) with about 1V input. Then it would roll over again at 2V and again 3V. I went to my ESP32-Devkit board and found it suffers from the same problem.
 
I went on to test the analog outputs on my C6 board.
 
I found that it had a carrier frequency of 1KHZ also I found that it seemed to be configured for 8 bit. By forcing it I found at 16384 it would be at 100% duty cycle and roll over above that. At 32767 it would again be at 100% duty cycle.
 
I went back to my ESP32-Devkit board once again. Now this board is bit different in that it has two real DAC’s on it. I reconfigure the board and took input 23 and added as an analog output giving me to real DAC’s and one PWM analog output. Testing it I found the PWM analog output worked exactly as it did on the C6 board. So it had the same problem. Next I had a look at the real DAC’s.
 
 I found with DVM the linearity of these DAC’s was awful. I decided to have a look with the scope.
 
I found there seemed to be a glitch in the output.
 
[203128%2F8984242%2Fmceclip0]
 
Next I lowered my sweep rate on my scope to see what the frequency of the glitch was.
 
[203128%2F8984243%2Fmceclip0]
 
Kind of interesting you can see at the top left that the frequency is 50HZ. What’s interesting here is that my scan time is at 20 Msec. 1/scantime is 50HZ. The glitch happens once per scan.
 
Now that I convinced myself that these problems are software related I though I would have a look and see if could identify what was causing them.
 
I had a look at ESP32.CPP as this seemed to be the place where this was handled. I first looked for the analog input problem where it rolls over at about 1V.
 
At about line 79 I found this line
 
        if (int_input[i] != NULL)
            *int_input[i] = (analogRead(pinMask_AIN[i]) * 64);
 
I didn’t see and code changing the configuration of the analog channels so it should be set up for 12 bit 11db gain. It seems to me that the *64 in that line should have been *16.
I changed it and tested both boards and the analog inputs now seem to work. The C6 documentation says don’t exceed 3.3 V and that 3.9 V gives 12 bits. So if I have 3.2/3.9 X 4095 that gives 3360 then the above line multiples by 16 giving 52760 and that is almost exactly what I got. The ESP32-Devkit board seems to give closer to 4095 at 3.2V or about 65520 counts. I can live with that.
 
Next I had a look at the glitch problem in the real DAC’s on the ESP32-Devkit board. Near the end of the program I found the following.
 
            analogWrite(pinMask_AOUT[i], (*int_output[i] / 64));
#if SOC_DAC_SUPPORTED
            dacWrite(pinMask_AOUT[i], (*int_output[i] / 256));
//#else
//           ledcWrite(pinMask_AOUT[i], (*int_output[i] / 16));
#endif
 
It appears that the code is writing to as a PWM output (firs line) then two lines down it writes to the same I/O point as DAC. In my mind this is clearly causing the glitch in the out. Now the solution to the PWM analog outs being low resolution. Low carrier frequency and rolling over at 16384 sort was the code but remarked out.
What was there didn’t seem to work quite the way I thought it should so I reworked it a bit.
 
There is a bit of added complication with some boards having real DAC’s and some not. Also on the boards that do I thought you should be able to have added analog channels as PWM.
 
In re-working it I made a couple of assumptions. First on the boards that have real DAC’s there are only two channels. I had a look and all the boards I seen that had real DAC’s only had two channels. The next thing I assumed was that if you had real DAC’s that they had to be the first two analog out pins in the HAL fille or the I/O configuration in the IDE.
 
In the hardware init part of the code I added the following
 
#if (SOC_DAC_SUPPORTED)
              if (i < 2)
{
pinMode(pinMask_AOUT[i], OUTPUT);
 
else     {
ledcAttach(pinMask_AOUT[i], PWM_ANALOG_FREQ, PWM_RESOLUTION);
 
}
#else
 
              ledcAttach(pinMask_AOUT[i], PWM_ANALOG_FREQ, PWM_RESOLUTION);
#endif
 
Essentially if the board has no DAC its analog channels are configured as PWM. IF there are DAC the first (array index 0 and 1) are set for output and the others are set for PWM. Then near the bottom of the file I added.
 
 
#if (SOC_DAC_SUPPORTED )
if (i < 2)
{
dacWrite(pinMask_AOUT[i], (*int_output[i] / 256));
}
else     {
ledcWrite(pinMask_AOUT[i], (*int_output[i] / 16));
}
 
#else
               ledcWrite(pinMask_AOUT[i], (*int_output[i] / 16));
#endif
 
Again if no DAC’s the ourputs are written as PWM. If there DAC’s the first two are done with the dacWrite the rest are done as PWM.
 
I tested these changes on both boards.
 
 
[203128%2F8984244%2Fmceclip0]
 
I now get a 4 KHZ carrier frequency and 65520 gives 100% duty cycle. The real DAC also seem to work ok no glitches.
 
I went back to my blackpill board and made a couple of changes in the stm32_f411ce.CPP file as it also had analog problems.
 
To summarize.
 
The ESP32-C6 still has a problem where it stops executing code when hitting the debug stop icon.
 
In the process of my testing I tried the ESP32 WROVER board option and got the following error.
Error during build: Invalid FQBN: getting build properties for board esp32:esp32:esp32wrover: invalid option 'PSRAM'
$? = 1
 
COMPILATION FAILED!
It seems there is a parameter problem in the HAL file. I did not look at this.
 
I think there are other boards that have similar analog I/O problems someone should re-look at these.
 
I’m going to upload the files I changed. I did try and put comments indicating what I changed. If someone can review what I did to see if it makes sense.
 
A couple of things I noticed about forcing. The first was that it would not let me force a variable of the type UINT it would not let me force it above 32767.
The second was if you try to force a variable of the type TIME it crashes OpenPLC. I didn’t include a bug report but that one easy to duplicate.
 
 
I’m kind of hoping the entire community can benefit from the work I did.
 
Thanks for the help
 
Reply 0 0
thiagoralves
Awesome work @buttley!! With so many boards being supported on OpenPLC, sometimes it is hard to track these little glitches. Would you be so kind to convert these changes into a Github PR (Pull Request)? This gives us full visibility of what has changed and allows us to incorporate all changes with just one click. Creating a Pull Request is very simple on GitHub. You just need to fork the editor source code (click on the Fork button at the top right) and then modify the files on your own copy. You can modify the files directly on GitHub web editor without having to install anything on your computer. Once you're done modifying everything just hit submit pull request to send those changes back to us.
Reply 0 0
buttley
I gave it try hopefully you get the changes. Thanks
Reply 0 0
thiagoralves
Thanks @buttley! I got the PR, will check it today and merge if everything is good with it.
Reply 0 0
Balmazo
Hello.
Im trying to use the ESP32C (SEEED XIAO ESP32C6) but Im getting this problem (In a loop):
 
Modbus Read Input Registers failed on MB device SEEED: Connection timed out
Modbus Write Holding Registers failed on MB device SEEED: Bad file descriptor
Device SEEED is disconnected. Attempting to reconnect...
Connected to MB device SEEED
 
I tryied to use your files but I still get the problem. I have an ESP32S3 and Im using the same program (I just change the ESP32S3 or ESP32C6 when Im dowloading the program) and its working fine.
 
Do you have any idea why Im having this error?
Reply 0 0
thiagoralves
You haven't given us much detail about your setup, so it is hard to help. Are you trying to add your ESP32 board as a slave of a Linux or Windows OpenPLC Runtime? What program are you loading on the ESP32 boards? Are you enabling Modbus TCP or Modbus RTU? What does your configuration for the slave device on the OpenPLC Runtime looks like?
Reply 0 0
Balmazo
hat program are you loading on the ESP32 boards? Are you enablin
 
You haven't given us much detail about your setup, so it is hard to help. Are you trying to add your ESP32 board as a slave of a Linux or Windows OpenPLC Runtime? What program are you loading on the ESP32 boards? Are you enabling Modbus TCP or Modbus RTU? What does your configuration for the slave device on the OpenPLC Runtime looks like?
Thanks for the quickly answer. Im sorry.
 
Im trying to add my ESP32 as a TCP Modbus Slave (I have a Raspberry Pi running as "Master"). I have already configured the ESP32 as a slave on the Raspberry slave devices (I tried using both "Generic TCP Device" and "ESP32"). But for loading the program in the ESP32 Im using Windows.
[203128%2F9283106%2Fmceclip0]
 

Im just loading a test program to see if it works.
[203128%2F9283099%2Fmceclip0]
 
Yes, Im enabling the Modbus TCP.
[203128%2F9283104%2Fmceclip0]
 
And Im doing exactly the same for the S3 and for the C6 (Just changing the device, obviously and they are not turned on in the same time, then I dont have IP problems).
[203128%2F9283107%2Fblobid0]
 
The S3 works fine but the C6 doenst.
Reply 0 0
thiagoralves
I believe your problem will be fixed if you update your editor. You're running on a very old version. Just hit file->check for updates to update. If the problem is still happening after the update, let us know.
Reply 0 0
Balmazo
I tried that, but unfortunately it didnt worked:
Still exactly the same. If I try with the S3, it works, but with the C6, it doesnt.
 
[203128%2F9283418%2Fmceclip0]
 
[203128%2F9283419%2Fmceclip0]
 
[203128%2F9283420%2Fmceclip0]
Reply 0 0
thiagoralves
Then it seems that the Modbus driver is broken on the C6. Have you tried using a different Modbus client to see if it works? Something like Modbus Poll or Radzio Modbus
Reply 0 0
Reply