Skip to content
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

ESP32-C3 (RISC-V) port #564

Open
Harvie opened this issue Dec 5, 2020 · 34 comments
Open

ESP32-C3 (RISC-V) port #564

Harvie opened this issue Dec 5, 2020 · 34 comments

Comments

@Harvie
Copy link

Harvie commented Dec 5, 2020

New RISC-V based wifi SoC called ESP32-C3 was announced https://www.espressif.com/en/news/ESP32_C3
Unlike the Bouffalo Lab MCUs ( #559 ) this might actually be easier to support, since it is expected to be supported by ESP-IDF just like the classic ESP32. Biggest change is that RISC-V toolchain will have to be used rather than Xtensa compiler. But that should not affect the C code too much.

I guess this will be nice match for Mongoose OS. As well as ticket to the RISC-V future for all of us.

https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf

image

@Harvie Harvie changed the title ESP32-C3 port ESP32-C3 (RISC-V) port Dec 5, 2020
@rojer
Copy link
Collaborator

rojer commented Dec 6, 2020

interesting. it looks like they reuse peripherals from ESP32, which is good.
i assume we can ignore the "world controller" at least initially, until we figure out what's up with it.
i guess, as long as they provide the toolchain, it shouldn't be too much of effort to support this thing.

@MichiW1
Copy link

MichiW1 commented Jun 17, 2021

Hi all,
I am very interested in using this new chip version. Seems to be a good alternative between esp8266-based and esp32 solutions, while adding BLE in a low price system. There is even a module "ESP32-C3F" available which is directly pin-compatible with common ESP-12F... so not even new PCB-layouting needed :-)
How is the plan of Mongoose to integrate this ESP32-C3 support which is already available in ESP-IDF?
Thanks a lot, Michael

@rojer
Copy link
Collaborator

rojer commented Jun 17, 2021

defintiely should do it, unfortunately it's a pretty big change and i don't have the time right now...

@duncandrennan
Copy link
Contributor

We've been developing on Mongoose is various ways and based on our roadmap use of the -C3 is critical. Is support for the -C3 something that we can help to work towards? I don't really know how the hardware layer of MOS works to support different chips. If you can point me in the right direction I'll do some work to investigate how to support it.

@rojer
Copy link
Collaborator

rojer commented Oct 7, 2021

there isn't really a guide for adding a new platform, yet.
i'll try to put something together.
but the basic steps are: make sure we can flash the thing (changes to mos tool), package SDK and toolchain into a docker container (this may not be required, as we already ship esp-idf and toolchain), then add basic bits into https://github.com/cesanta/mongoose-os/tree/master/platforms these are UART and GPIO drivers, exception handler, clocking, any other init stuff to launch the mos event loop (task). then check various other peripheral drivers, sucha as I2C and SPI - these are libraries, they need platform-specific code.
in the case of C3 most of this will probably be shared with esp32 (emphasis on shared, not copied from!) but careful examintaion of differences is necessary.

we can also discuss contract work to add C3 support, reach out to me personally if interested.

@duncandrennan
Copy link
Contributor

Thank you! I'll start taking a look at this - it will be good to get a better understanding of how MGOS and the ESP-IDF fit together.

@duncandrennan
Copy link
Contributor

I've started working on the mos tool to get it flashing the device. The eFuse structure is different, so efuses.go needs to be different. I guess this means defining an entirely new --platform esp32c3 along with all the necessary bit & pieces?

@rojer
Copy link
Collaborator

rojer commented Oct 22, 2021

yes, i think c3 is a big enough change to warrant a new platform

@duncandrennan
Copy link
Contributor

Where do I look for information about how the stub_flasher's work?

@rojer
Copy link
Collaborator

rojer commented Oct 22, 2021

code is the documentation :)
it's been a while since i touched it and it looks like code has been buried during some repo restructuring, but it can be found here: https://github.com/cesanta/mongoose-os/tree/f626b5113861634e9a4acc96bdb73de29ed8a4d0/common/platforms/esp32/stubs

@rojer
Copy link
Collaborator

rojer commented Oct 22, 2021

there's also a script i used to build the stubs when working on them:
https://github.com/mongoose-os/mos/blob/master/cli/flash/esp32/genstubs.sh

this is dead code right now and needs to be revived.
i think C source should be checked in under https://github.com/mongoose-os/mos/tree/master/cli/flash/esp

@rojer
Copy link
Collaborator

rojer commented Oct 24, 2021

i dug up the sources and checked them into the repo - mongoose-os/mos@7ec8632

you can now develop C stub code, then rebuild and test it with a single command.

@duncandrennan
Copy link
Contributor

How do the mgos docker containers work? I see, for example, that https://hub.docker.com/r/mgos/esp32-build/tags contains what I assume is ESP-IDF v4.2 (with various releases).

ESP32-C3 requires ESP-IDF v4.3, but I assume the docker container specifically needs to sets up all the necessary flags/etc. to build for ESP32-C3 (e.g. to use the riscv32-esp-elf-gcc compiler).

I haven't worked with Docker (other than using it blindly) so it is a bit of a mystery as to how the build system works. I think I understand it conceptually, but not in an practical way that allows me to configure a new docker container for esp32c3 builds. Maybe the esp32 container is sufficient as long as it has ESP-IDF v4.3?

@duncandrennan
Copy link
Contributor

cesanta/mongoose-os/#584 is my first step towards this - a docker container for ESP32-C3 builds with the riscv32-elf-gcc compiler.

I'm busy working on getting the bootloader stubs compiled - there are some things to resolve around the SPI flash API in the stubs.

@duncandrennan
Copy link
Contributor

duncandrennan commented Nov 14, 2021

@rojer I'm busy trying to get the bootloader stub (in the mos tool) to compile for ESP32-C3. I think I've made some decent progress but I'm stumbling a bit with regards to the assembler in main().

I've pushed code to duncandrennan/mos@1aefe5b

I've worked around the ccount() calls to use the correct assembler for the riscv processor, but I'm not quite sure what is actually happening in main() with regards to the __asm volatile("movi a1, stack_end\n"); - are you able to give me a kick in the right direction?

@rojer
Copy link
Collaborator

rojer commented Nov 15, 2021

assuming you refer to this.
stub_main clears the bss segment and sets a stack pointer to where the stack will be.
in xtensa abi, a1 register is the stack pointer, so we point it at an appropritaely sized area that will be our stack.
since we don't plan to return from stub_main, we don't bother preserving the previous value.
since the stack grows down, we need to point at the end of the memory region.
stack is the actual stack space and stack_end is a variable at the end of it.
it's a bit sloppy of us to expect that stack_end will always follow stack in memory as linker is under no such pbligation, but in practice this seems to hold.
back then my asm-fu was not as good, if i were writing this today i'd be more careful.

so, bootom line is: you need to figure out how stack works on RISC-V and what it takes to relocate it to the location of the stack variable.

@rojer
Copy link
Collaborator

rojer commented Nov 15, 2021

this is the more correct variant.

@duncandrennan
Copy link
Contributor

With regards to cesanta/mongoose-os/#584 what are the rom.bin/elf files and where do they come from (or how are they generated)?

@rojer
Copy link
Collaborator

rojer commented Nov 16, 2021

rom.bin is the dump of ROM.
rom.elf is that same dump in ELF format, so it can be loaded into debugger.
these are just nice-to-haves for core dump debugging, don't worry about them for now.
if you don't have a rom dump for C3, delete both files for now.

@duncandrennan
Copy link
Contributor

Espressif offers an ELF format file here, https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-guides/core_dump.html#rom-functions-in-backtraces but that .elf file is significantly bigger than the ESP32 rom.elf, and if I convert that to a BIN file with riscv32-esp-elf-objcopy it is 3.5MB versus the 0.5MB of the ESP32 rom.bin

@rojer
Copy link
Collaborator

rojer commented Nov 17, 2021

as i said, don't worry about it for now.

@duncandrennan
Copy link
Contributor

duncandrennan commented Nov 22, 2021

mongoose-os/mos#70 is the initial draft of the bootloader stub. It compiles, but definitely needs some checking

@duncandrennan
Copy link
Contributor

I've made progress on getting this to read and write the flash correctly. Reading is working but I've struggled quite a bit with the writing. When I try to write to flash the device stops communicating. I've been able to hard code it to flash some data, but something with regards to the UART ISR for receiving the flash data is going wrong. I've had inconsistent behaviour over recompiles, so we're also double checking the placement in memory and stack requirements. Working on getting this resolved so that we can start working on getting the actual OS code compiled. I haven't had a proper debugging environment set up, so that is the next step for me. Hopefully I can do a PR with something that actually works shortly.

@duncandrennan
Copy link
Contributor

@rojer Are you able to give me some pointers as to how to debug the bootloader code? I'm not quite sure whether to load the bootloader via gdb and then skip the mos stub loading steps, or let mos load the stub, then try to attach to it from gdb. If you have some guidance, that would be very helpful. Thank you!

@rojer
Copy link
Collaborator

rojer commented Mar 11, 2022

hey Duncan, i plan to work on C3 port myself soon, i'll have a look myself at that point.

@duncandrennan
Copy link
Contributor

@rojer got the flash reading & writing working on the ESP32-C3's

@rojer
Copy link
Collaborator

rojer commented Jun 14, 2022

yes, and i intend to continue working on the port from here

rojer added a commit that referenced this issue Jun 26, 2022
A prerequisite for supporting targets other than the original ESP32.

#564
rojer added a commit to mongoose-os/mos that referenced this issue Jul 26, 2022
rojer added a commit to mongoose-os-libs/core that referenced this issue Jul 30, 2022
@rojer
Copy link
Collaborator

rojer commented Jul 30, 2022

a8db502

rojer added a commit to mongoose-os-libs/freertos that referenced this issue Jul 30, 2022
rojer added a commit to mongoose-os-libs/mbedtls that referenced this issue Jul 30, 2022
rojer added a commit to mongoose-os-libs/vfs-common that referenced this issue Jul 30, 2022
Rename `esp32_*` to `esp32xx_*`.

cesanta/mongoose-os#564
rojer added a commit to mongoose-os-libs/vfs-fs-spiffs that referenced this issue Jul 30, 2022
rojer added a commit to mongoose-os-libs/core that referenced this issue Jul 30, 2022
rojer added a commit to mongoose-os-libs/lwip that referenced this issue Aug 6, 2022
rojer added a commit to mongoose-os-libs/wifi that referenced this issue Aug 6, 2022
rojer added a commit that referenced this issue Aug 6, 2022
rojer added a commit that referenced this issue Aug 6, 2022
rojer added a commit to mongoose-os-libs/bt-common that referenced this issue Aug 8, 2022
rojer added a commit to mongoose-os-libs/boards that referenced this issue Aug 8, 2022
rojer added a commit to mongoose-os-libs/dns-sd that referenced this issue Aug 8, 2022
@rojer
Copy link
Collaborator

rojer commented Aug 8, 2022

initial support is done. still TBD: eFuse and flash encryption support

@duncandrennan
Copy link
Contributor

Wow! Thanks @rojer

@duncandrennan
Copy link
Contributor

@rojer I tried to build our firmware using this with the "latest" mos and libraries, but it fails complaining that it doens't know about the esp32c3, only [esp32]. I looked through the mos code as AFAICT it scans through the modules/mongoose-os/platforms looking for sdk.version files then adds the directory name to the supported platforms. I do see the esp32 and esp32c3 directories in the platforms directory, so I'm not really sure why this is failing. Got the same result from a local or a remote build. I haven't tried rebuilding mos locally and using that. Exact error log (from remote build) is

rror: /src/cli/build_local.go:225: can't build for the platform esp32c3; supported platforms are: [esp32] (use --no-platform-check to override)
/src/cli/build.go:278:
/src/cli/build.go:143:
/src/cli/main.go:194: build failed
Error: /src/cli/build_remote.go:359: build failed
/src/cli/build.go:278:
/src/cli/build.go:221:
/src/cli/main.go:194: build failed

@duncandrennan
Copy link
Contributor

Ok, figured out the issue in my particular case was the mongoose-os-libs/i2c library which does not yet support the -C3. I tried adding it, but see there are currently compile issues with the code.

@rojer
Copy link
Collaborator

rojer commented Aug 12, 2022

yeah, that message is about lib compatibility. god point about i2c, it needs to be updated

@duncandrennan
Copy link
Contributor

Noting the following also need updates (after trying some of our other code and bumping up against them):

  • mongoose-os-libs/adc
  • mongoose-os-libs/pwm

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

No branches or pull requests

4 participants