Skip to content

esp32: Switch to IDF v5.0 #11528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 23, 2023
Merged

esp32: Switch to IDF v5.0 #11528

merged 12 commits into from
Jun 23, 2023

Conversation

dpgeorge
Copy link
Member

@dpgeorge dpgeorge commented May 17, 2023

For the esp32 port, MicroPython currently supports IDF v4.x (from 4.0.2 up to the latest 4.4.4). There is now a new major version of IDF v5.x, with a release v5.0.1.

IDF 5 is needed for some of the newer esp32 SoCs to work. It also cleans up a lot of the inconsistencies between existing SoCs (eg S2, S3, C3).

I propose to switch entirely to IDF v5, ie drop support for IDF v4. Mainly because it's a lot of effort to maintain both at the same time, and that effort is better spent elsewhere.

This PR gets basic IDF v5. support working. Current status:

ESP32 ESP32-S2 ESP32-S3 ESP32-C3
builds ✔️ ✔️ ✔️ ✔️
SPIRAM ✔️ ✔️ ✔️ N/A
REPL (UART) ✔️ ✔️ ✔️ ✔️
REPL (USB) N/A ✔️ ✔️ N/A
filesystem ✔️ ✔️ ✔️ ✔️
GPIO ✔️ ✔️ ✔️ ✔️
SPI ✔️ ✔️ ✔️ ✔️
I2C ✔️ ✔️ ✔️ ✔️
PWM ✔️ ✔️ ✔️ ✔️
ADC ✔️ ✔️ ✔️ ✔️
WiFi STA ✔️ ✔️ ✔️ ✔️
WiFi AP ✔️ ✔️ ✔️ ✔️
BLE ✔️ N/A ✔️ ✔️
ETH ✔️
PPP ✔️ ✔️ ✔️
sockets ✔️ ✔️ ✔️ ✔️
SSL ✔️ ENOMEM ✔️ ✔️
RMT ✔️ ✔️ ✔️ ✔️
NeoPixel ✔️ ✔️ ✔️ ✔️
I2S ✔️ ✔️ ✔️ N/A
ESPNow ✔️ ✔️ ✔️ ✔️
ULP-FSM ✔️ ✔️ ✔️ N/A
SDCard ✔️ N/A N/A ✔️
WDT ✔️ ✔️ ✔️ ✔️

@dpgeorge dpgeorge marked this pull request as draft May 17, 2023 03:11
@dpgeorge dpgeorge added this to the release-1.21.0 milestone May 17, 2023
@github-actions
Copy link

github-actions bot commented May 17, 2023

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% PICO
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@codecov-commenter
Copy link

codecov-commenter commented May 17, 2023

Codecov Report

Merging #11528 (1cb22e7) into master (41c9142) will not change coverage.
The diff coverage is n/a.

❗ Current head 1cb22e7 differs from pull request most recent head 6a9db52. Consider uploading reports for the commit 6a9db52 to get more accurate results

@@           Coverage Diff           @@
##           master   #11528   +/-   ##
=======================================
  Coverage   98.40%   98.40%           
=======================================
  Files         155      155           
  Lines       20543    20543           
=======================================
  Hits        20215    20215           
  Misses        328      328           
Impacted Files Coverage Δ
extmod/modbtree.c 96.98% <ø> (ø)
extmod/modhashlib.c 100.00% <ø> (ø)
extmod/modssl_mbedtls.c 89.84% <ø> (ø)

@wnienhaus
Copy link
Contributor

For the table above: I just tested this branch on both the ESP32-S2 and the ESP32-S3 and the ULP-FSM works correctly.

(To test, I used this sample code: #11761 (comment)).

I still want to test the same on the original ESP32 and will then let you know my result.

@wnienhaus
Copy link
Contributor

Now I tested this branch on the original ESP32 too and the ULP-FSM works correctly.

@dpgeorge
Copy link
Member Author

Thank you @wnienhaus ! I have updated the table.

@dpgeorge dpgeorge force-pushed the esp32-idf-50 branch 2 times, most recently from c65d0d5 to a70fb7a Compare June 19, 2023 06:22
@bwhitman
Copy link

I am using this on ESP32-S3 and can confirm SPIRAM works great there (gc.mem_free() shows me 4MB heap and it works), and UART REPL is fine. I have also tested WIFI STA and it works. It's a huge help for my project, so thank you!

@dpgeorge
Copy link
Member Author

@bwhitman did you delete a comment about LFS2 partitions not working? I just want to make sure everything is OK with that (it would be a show-stopper if it didn't work).

@bwhitman
Copy link

bwhitman commented Jun 21, 2023

@bwhitman did you delete a comment about LFS2 partitions not working? I just want to make sure everything is OK with that (it would be a show-stopper if it didn't work).

I did. I thought it was due to the new MP or IDF, but it was because the new IDF gave me a new env where I re-installed littlefs-python, which got me a brand new version that made a LFS partition version 2.1, which was too new for MP / IDF 5.0.2. (It will return ENOENT if it's over 2.0. I eventually found it by turning on LFS debugging.) I've made an issue for that over there.. Apologies, I should have updated my comment instead of deleting it.

@dpgeorge
Copy link
Member Author

Apologies, I should have updated my comment instead of deleting it.

No worries. But, yes, definitely best to update your comment instead of deleting 😄

@jimmo
Copy link
Member

jimmo commented Jun 21, 2023

Tested: GPIO, I2C, SPI, RMT, NeoPixel, ADC, PWM across OG,S2,S3,C3. Also added REPL UART+USB on S2,S3, and Ethernet (IP101) on OG.

@dpgeorge
Copy link
Member Author

SD card code has been updated for IDF v5, and tested working on ESP32.

@jimmo
Copy link
Member

jimmo commented Jun 21, 2023

Tested PPP on OG/S3/C3, SDCard on S3.

@ThinkTransit
Copy link
Contributor

@ThinkTransit if you programmed with a UF2 file then it's likely that the partition table is different to what's expected by the MicroPython firmware.

Can you run:

from esp32 import Partition
print(Partition.find(Partition.TYPE_DATA, label="vfs"))
print(Partition.find(Partition.TYPE_DATA, label="ffat", block_size=512))

?

If the first line prints None and the second line prints something else then you might be able to fix it by manually creating the filesystem:

os.VfsFat.mkfs(bdev)

Thanks @dpgeorge , creating the filesystem manually works.

>>> from esp32 import Partition
>>> print(Partition.find(Partition.TYPE_DATA, label="vfs"))
[]
>>> import os
>>> os.VfsFat.mkfs(bdev)
>>> 
MPY: soft reboot
MicroPython v1.20.0-239-g1cb22e7be on 2023-06-22; FeatherS2 with ESP32-S2
Type "help()" for more information.
>>> 

@dpgeorge
Copy link
Member Author

creating the filesystem manually works

In this case (programming with a UF2) it's assumed that the previous firmware has created the FAT filesystem.

To improve this situation we need to modify inisetup.py to detect if it should create a LFS or FAT filesystem, based on the Partition label.

@ThinkTransit
Copy link
Contributor

creating the filesystem manually works

In this case (programming with a UF2) it's assumed that the previous firmware has created the FAT filesystem.

To improve this situation we need to modify inisetup.py to detect if it should create a LFS or FAT filesystem, based on the Partition label.

Ok makes sense thanks.

I just tested this board without the bootloader.

esptool.py --chip esp32s2 --port /dev/ttyACM0 erase_flash
esptool.py --chip esp32s2 --port /dev/ttyACM0 write_flash -z 0x1000 ports/esp32/build-UM_FEATHERS2/micropython.bin

again probably doing something wrong but the usb doesn't enumerate and a loadstore error is repeated on uart0. Could it be related?

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3f000020,len:0x364a4
Panic at 4001126a
Fatal exception (3): LoadStoreError
epc1=0x4000ff4b, epc2=0x00000000, epc3=0x00000000, excvaddr=0x3f000020, depc=0x00000000
ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x7 (TG0WDT_SYS_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x4000f254
SPIWP:0xee
mode:DIO, clock div:1
load:0x3f000020,len:0x364a4
Panic at 4001126a
Fatal exception (3): LoadStoreError
epc1=0x4000ff4b, epc2=0x00000000, epc3=0x00000000, excvaddr=0x3f000020, depc=0x00000000

@wnienhaus
Copy link
Contributor

wnienhaus commented Jun 22, 2023

I had this exact same error (LoadStoreError), when I used the wrong offset for the micropython.bin. I believe it should be 0x10000 (4 zeros) instead of 0x1000 (3 zeros). Now you would also have to write bootloader.bin to 0x1000 again, to restore a working bootloader, and likely partition-table.bin to 0x8000.

@AmirHmZz
Copy link
Contributor

AmirHmZz commented Jun 23, 2023

I've run our main project on this PR built with idf5.0.2 and everything worked correctly!

  • Tested components : GPIO SPI SD Card I2C PWM WIFI STA Socket ESPNow BTree
  • Tested on : ESP32

dpgeorge and others added 12 commits June 23, 2023 15:22
Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
To prevent warnings when building with ESP IDF v5.

Signed-off-by: Damien George <damien@micropython.org>
Allows registering UART interrupt again.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
This function was made private/static in IDF commit
c67f4c2, so it add back here.

Signed-off-by: Damien George <damien@micropython.org>
The hall sensor is no longer supported by IDF v5.

Signed-off-by: Damien George <damien@micropython.org>
This commit updates the esp32 port to work exclusively with ESP-IDF v5.
IDF v5 is needed for some of the newer ESP32 SoCs to work, and it also
cleans up a lot of the inconsistencies between existing SoCs (eg S2, S3,
and C3).

Support for IDF v4 is dropped because it's a lot of effort to maintain both
versions at the same time.

The following components have been verified to work on the various SoCs:

                ESP32     ESP32-S2  ESP32-S3  ESP32-C3
    build       pass      pass      pass      pass
    SPIRAM      pass      pass      pass      N/A
    REPL (UART) pass      pass      pass      pass
    REPL (USB)  N/A       pass      pass      N/A
    filesystem  pass      pass      pass      pass
    GPIO        pass      pass      pass      pass
    SPI         pass      pass      pass      pass
    I2C         pass      pass      pass      pass
    PWM         pass      pass      pass      pass
    ADC         pass      pass      pass      pass
    WiFi STA    pass      pass      pass      pass
    WiFi AP     pass      pass      pass      pass
    BLE         pass      N/A       pass      pass
    ETH         pass      --        --        --
    PPP         pass      pass      pass      --
    sockets     pass      pass      pass      pass
    SSL         pass      ENOMEM    pass      pass
    RMT         pass      pass      pass      pass
    NeoPixel    pass      pass      pass      pass
    I2S         pass      pass      pass      N/A
    ESPNow      pass      pass      pass      pass
    ULP-FSM     pass      pass      pass      N/A
    SDCard      pass      N/A       N/A       pass
    WDT         pass      pass      pass      pass

Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
IDF v5.0 provides access to rssi value for received espnow packets via
recv_info arg to recv_cb().

Signed-off-by: Glenn Moloney <glenn.moloney@gmail.com>
Signed-off-by: Damien George <damien@micropython.org>
And change erase_flash to erase-flash, because the former is deprecated
since IDF 4.4.

Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge
Copy link
Member Author

I've run our main project on this PR built with idf5.0.2 and everything worked correctly!

Very good, thanks for the additional testing!

I have also tested machine.I2S on ESP32, ESP32S2 and ESP32S3.

@dpgeorge dpgeorge merged commit 6a9db52 into micropython:master Jun 23, 2023
@dpgeorge
Copy link
Member Author

Merged!

@dpgeorge dpgeorge deleted the esp32-idf-50 branch June 23, 2023 06:05
@robert-hh
Copy link
Contributor

Thanks. That was surely a lot of work. Time for a relaxed weekend.

@Carglglz
Copy link
Contributor

Carglglz commented Jun 24, 2023

@dpgeorge I've managed to rebase #8968 to include these changes and everything seems to be working but I've noticed that mDNS support and esp32_mbedtls_errors.c were left behind, is there any reason for this? So far I've managed to add mDNS support again and I will look into mbedtls_errors.c next, also now that the esp32 port is at mbedtls 3.3.0 maybe is time to revisit #8988 ? 👀

@Wind-stormger
Copy link
Contributor

To improve this situation we need to modify inisetup.py to detect if it should create a LFS or FAT filesystem, based on the Partition label.

https://github.com/orgs/micropython/discussions/9961#discussioncomment-6227871

I've put my code up for this.

Would I submit a PR?

@dpgeorge
Copy link
Member Author

I've noticed that mDNS support and esp32_mbedtls_errors.c were left behind

I thought I left mDNS as is... but maybe I missed something there.

For the custom mbedtls errors, yes, I did not update that. The only downside there is that the error messages will be longer/take up more flash (and be different to other mbedtls ports...). For example what used to be:

OSError(-29184, 'MBEDTLS_ERR_SSL_INVALID_RECORD')

Is now (on esp32 with IDF 5):

OSError(-29184, 'SSL - An invalid SSL record was received')

I would like to eventually fix this (feel free to try!) but it's not a high priority at the moment.

also now that the esp32 port is at mbedtls 3.3.0 maybe is time to revisit #8988

Maybe... you already did update to v2.28.3 in 61b8e1b and it would be good to have that in the next release which is scheduled for the end of this month (!). Then we can update to v3.3.0 after the release.

@Carglglz
Copy link
Contributor

I thought I left mDNS as is... but maybe I missed something there.

Yes, for some reason Espressif decided to move mDNS outside of the esp-idf repo, is now here esp-protocols, I had to clone it and copy the mdns component to esp-idf/components, add it to
esp32_common.cmake and include mdns.h in network_wlan.c and modsocket.c, after that it worked again as expected, I'll do a PR if necessary. 👍🏼

I would like to eventually fix this (feel free to try!) but it's not a high priority at the moment.

I agree

Then we can update to v3.3.0 after the release.

Yes, this is probably the best option 🤔

@dpgeorge
Copy link
Member Author

I'll do a PR if necessary

If you have time, that would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.