cpq / mdk

A bare metal SDK for the ESP32 & ESP32C3
MIT License
188 stars 26 forks source link

examples/blinky fails on ESP32-C3-DevKitM-1U #13

Open bartgrantham opened 1 year ago

bartgrantham commented 1 year ago

The code builds ok but it appears to crash immediately and esptool monitor throws an odd error:

podman run -it --rm -v /Users/bart/Code/riscv/mdk/examples/blinky:/Users/bart/Code/riscv/mdk/examples/blinky -v /Users/bart/Code/riscv/mdk:/Users/bart/Code/riscv/mdk -w /Users/bart/Code/riscv/mdk/examples/blinky localhost/gcc-riscv riscv-none-elf-gcc  -W -Wall -Wextra -Werror -Wundef -Wshadow -pedantic -Wdouble-promotion -fno-common -Wconversion -march=rv32imc -mabi=ilp32 -Os -ffunction-sections -fdata-sections -I. -I/Users/bart/Code/riscv/mdk/esp32c3  /Users/bart/Code/riscv/mdk/esp32c3/boot.c main.c -T/Users/bart/Code/riscv/mdk/esp32c3/link.ld -nostdlib -nostartfiles -Wl,--gc-sections  -o firmware.elf
/Users/bart/Code/riscv/mdk/esputil/esputil mkbin firmware.elf firmware.bin
/Users/bart/Code/riscv/mdk/esputil/esputil flash 0   firmware.bin
Using flash params 0x22f
Written firmware.bin, 464 bytes @ 0
/Users/bart/Code/riscv/mdk/esputil/esputil monitor
ESP-ROM:esp32c3-api1-20210207
0,len:0x144
SHA-256 comparison failed:
Calculated: 8d0a8732737dba9127ad398dcaab95b080ef25aad59193ac92b6b66ad65e89�J)�)��5ͯ�ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x7 (TG0WDT_SYS_RST),boot:0xc (SPI_FAST_FLASH_BOOT)
Saved PC:0x4038053e
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fc88000,len:0x60
load:0x40380400,len:0x144
SHA-256 comparison failed:
Calculated: 8d0a8732737dba9127ad398dcaab95b080ef25aad59193ac92b6b66ad65e89d2
Expected: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Attempting to boot anyway...
entry 0x40380400
LED: 0
ESP-ROM:esp32c3-api1-20210207
...

And it repeats the last 15 lines indefinitely. The gibberish is masking debugging output from expect, I think?

examples/ws2812 works fine. FWIW, this is on macOS Ventura 13.1, running podman instead of docker, and I built the image from Dockerfile example in the README.md. examples/button also works. examples/bme280 does not, but I am not sure it was expected to.

NilsBe commented 1 year ago

try this:

diff --git a/examples/blinky/main.c b/examples/blinky/main.c index f4766c8..660e7db 100644 --- a/examples/blinky/main.c +++ b/examples/blinky/main.c @@ -4,6 +4,7 @@ static int led_pin = LED1; // To override: make EXTRA_CFLAGS=-DLED1=5 static int led_state = 0;

int main(void) {

stacksmith commented 1 year ago

I spent many hours now, making bins and flashing them. As mentioned elsewhere, esputil does not work for me at all, and I've been using esptool.

I can flash arduino crap with it, but every attempt to flash blinky is generally just ignored, and the device continues to run old code in higher memory (since blinky is so small...). Otherwise it just keeps resetting and printing repeating crap.

OK, crashing for hours in slightly different ways I found a stable recipe that works...xs

To generate firmware.bin: esptool.py --chip esp32 elf2image --dont-append-digest firmware.elf

To flash it:

esptool.py -p /dev/ttyUSB0 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x1000 firmware.bin

That was truly painful.

stacksmith commented 1 year ago

esptool can also check a bin file for corruption or incorrectness. That is how I found out that esputil was screwing me for hours - it generated an invalid checksum or something.

I added a few useful targets in the mdk/esp32/build.mk file:

$(PROG).bin: $(PROG).elf $(ESPTOOL)
        $(ESPTOOL) --chip $(ARCH) elf2image --dont-append-digest $(PROG).elf

check: $(PROG).bin
        $(ESPTOOL) --chip $(ARCH) image_info --version 2 $(PROG).bin

flash: $(PROG).bin
        $(ESPTOOL) --chip $(ARCH) --port $(PORT) --baud 921600 --before default\
_reset --after hard_reset write_flash -fm dio --flash_freq 80m --flash_size 4MB\
  $(FLASH_ADDR) $(PROG).bin

And many thanks for a great project! I was really getting sick of Arduino, and the Espressif environment is an enormous vat of excrement.

mdk is actually cool!

bartgrantham commented 1 year ago

@NilsBe This did turn off the watchdog timer, which prevented it from resetting. But there was still no blinking LED.

Which made me wonder: which LED is supposed to blink here? The ws2812? The little red power LED? @cpq Is this example intended to work on this board?

bartgrantham commented 1 year ago

I just realized that the LED that was supposed to light up was the one I was supposed to connect to pin 2. I wired up a pin and now it works. It does need wdt_disable() though, or the watchdog will keep resetting the board before it can blink.

cpq commented 1 year ago

@bartgrantham I suggest https://github.com/cpq/bare-metal-programming-guide/tree/main/templates/blinky/esp32-c3

It has systick interrupt implemented as well, which gives better timing capability

bartgrantham commented 1 year ago

I'll definitely look into that. Is that repo more up to date than this one? Looks like it. Is this repo deprecated?

cpq commented 1 year ago

This is not deprecated, it is just the other one is more advanced at this point.