Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Wear levelling #126

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions esp32/BUILD.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# ###############################################
# This is the clone of the MicroPython repository
# (https://github.com/micropython/micropython)
# with added ESP32 build
# ###############################################

# Usage:
# ./BUILD - run the build, create MicroPython firmware
# ./BUILD clean - clean the build
# ./BUILD deploy - flash MicroPython firmware to ESP32
# ./BUILD erase - erase the whole ESP32 Flash
# ./BUILD monitor - run esp-idf terminal program

# ###########################################################################
# build MicroPython cross compiler which compiles .py scripts into .mpy files
# ###########################################################################
make -C ../mpy-cross

export HOST_PLATFORM=linux

# ########################################
# Add Xtensa toolchain path to system path
# ########################################
export PATH=<path_to>/xtensa-esp32-elf/bin:$PATH

# ###################
# Export esp-idf path
# ###################
export IDF_PATH=<path_to>/esp-idf

# ##################################
# Export Micropython build variables
# ##################################
export CROSS_COMPILE=xtensa-esp32-elf-
export ESPIDF=$IDF_PATH
export PORT=/dev/ttyUSB1
export FLASH_MODE=dio
export FLASH_SIZE=4MB
export BAUD=921600
export DEPLOY_ERASE_RESET=no_reset

# ##########################
# build MicroPython firmware
# ##########################
cd esp32

make "$@"
36 changes: 33 additions & 3 deletions esp32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ include ../py/py.mk

PORT ?= /dev/ttyUSB0
BAUD ?= 460800
MONITOR_BAUD ?= 115200
FLASH_MODE ?= dio
FLASH_FREQ ?= 40m
FLASH_SIZE ?= 4MB
CROSS_COMPILE ?= xtensa-esp32-elf-
DEPLOY_ERASE_RESET ?= reset

# paths to ESP IDF and its components
ifeq ($(ESPIDF),)
$(error Please configure the ESPIDF variable)
endif
ESPCOMP = $(ESPIDF)/components
ESPTOOL ?= $(ESPCOMP)/esptool_py/esptool/esptool.py
ESPMONITOR ?= $(ESPIDF)/tools/idf_monitor.py
MONITOR_OPTS := --baud $(MONITOR_BAUD) --port $(PORT) --toolchain-prefix $(CROSS_COMPILE)


# verify the ESP IDF version
ESPIDF_SUPHASH := 9b955f4c9f1b32652ea165d3e4cdaad01bba170e
Expand Down Expand Up @@ -72,13 +77,16 @@ INC += -I$(ESPCOMP)/lwip/include/lwip/posix
INC += -I$(ESPCOMP)/mbedtls/include
INC += -I$(ESPCOMP)/mbedtls/port/include
INC += -I$(ESPCOMP)/spi_flash/include
INC += -I$(ESPCOMP)/wear_levelling/include
INC += -I$(ESPCOMP)/wear_levelling/private_include
INC += -I$(ESPCOMP)/vfs/include
INC += -I$(ESPCOMP)/newlib/platform_include
INC += -I$(ESPCOMP)/xtensa-debug-module/include
INC += -I$(ESPCOMP)/wpa_supplicant/include
INC += -I$(ESPCOMP)/wpa_supplicant/port/include
INC += -I$(ESPCOMP)/ethernet/include
INC += -I$(ESPCOMP)/app_trace/include
INC += -I$(ESPCOMP)/sdmmc/include

CFLAGS = -std=gnu99 -Os -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib -Wall -Werror -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -DMBEDTLS_CONFIG_FILE='"mbedtls/esp_config.h"' -DHAVE_CONFIG_H -DESP_PLATFORM $(INC)
CFLAGS += -DIDF_VER=\"$(IDF_VER)\"
Expand Down Expand Up @@ -134,6 +142,9 @@ SRC_C = \
espneopixel.c \
machine_hw_spi.c \
mpthreadport.c \
mpsleep.c \
machrtc.c \
timeutils_epoch.c \
$(SRC_MOD)

ESP8266_SRC_C = $(addprefix esp8266/,\
Expand Down Expand Up @@ -205,6 +216,8 @@ ESPIDF_DRIVER_O = $(addprefix $(ESPCOMP)/driver/,\
spi_master.o \
spi_common.o \
rtc_module.o \
sdmmc_host.o \
sdmmc_transaction.o \
)

$(BUILD)/$(ESPCOMP)/esp32/dport_access.o: CFLAGS += -Wno-array-bounds
Expand Down Expand Up @@ -374,6 +387,18 @@ ESPIDF_SPI_FLASH_O = $(addprefix $(ESPCOMP)/spi_flash/,\
flash_ops.o \
)

ESPIDF_WEAR_LEVELLING_O = $(addprefix $(ESPCOMP)/wear_levelling/,\
wear_levelling.o \
Partition.o \
WL_Flash.o \
crc32.o \
SPI_Flash.o \
)

ESPIDF_SDMMC_O = $(addprefix $(ESPCOMP)/sdmmc/,\
sdmmc_cmd.o \
)

$(BUILD)/$(ESPCOMP)/lwip/%.o: CFLAGS += -Wno-address -Wno-unused-variable -Wno-unused-but-set-variable
ESPIDF_LWIP_O = $(addprefix $(ESPCOMP)/lwip/,\
api/pppapi.o \
Expand Down Expand Up @@ -556,13 +581,15 @@ OBJ_ESPIDF += $(addprefix $(BUILD)/, $(ESPIDF_NGHTTP_O))
OBJ_ESPIDF += $(addprefix $(BUILD)/, $(ESPIDF_NVS_FLASH_O))
OBJ_ESPIDF += $(addprefix $(BUILD)/, $(ESPIDF_OPENSSL_O))
OBJ_ESPIDF += $(addprefix $(BUILD)/, $(ESPIDF_SPI_FLASH_O))
OBJ_ESPIDF += $(addprefix $(BUILD)/, $(ESPIDF_WEAR_LEVELLING_O))
OBJ_ESPIDF += $(addprefix $(BUILD)/, $(ESPIDF_SDMMC_O))
OBJ_ESPIDF += $(addprefix $(BUILD)/, $(ESPIDF_WPA_SUPPLICANT_O))
################################################################################
# Main targets

all: $(BUILD)/firmware.bin

.PHONY: idf-version deploy erase
.PHONY: idf-version deploy erase monitor

idf-version:
$(ECHO) "ESP IDF supported hash: $(ESPIDF_SUPHASH)"
Expand All @@ -573,11 +600,14 @@ $(BUILD)/firmware.bin: $(BUILD)/bootloader.bin $(BUILD)/partitions.bin $(BUILD)/

deploy: $(BUILD)/firmware.bin
$(ECHO) "Writing $^ to the board"
$(Q)$(ESPTOOL) --chip esp32 --port $(PORT) --baud $(BAUD) write_flash -z --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) --flash_size $(FLASH_SIZE) 0 $^
$(Q)$(ESPTOOL) --chip esp32 --port $(PORT) --baud $(BAUD) --after $(DEPLOY_ERASE_RESET) write_flash -z --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) --flash_size $(FLASH_SIZE) 0 $^

erase:
$(ECHO) "Erasing flash"
$(Q)$(ESPTOOL) --chip esp32 --port $(PORT) --baud $(BAUD) erase_flash
$(Q)$(ESPTOOL) --chip esp32 --port $(PORT) --baud $(BAUD) --after $(DEPLOY_ERASE_RESET) erase_flash

monitor:
$(ESPMONITOR) $(MONITOR_OPTS) $(BUILD)/application.elf

################################################################################
# Declarations to build the application
Expand Down
11 changes: 4 additions & 7 deletions esp32/fatfs_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@

#include "py/obj.h"
#include "lib/oofatfs/ff.h"
#include "timeutils.h"
//#include "modmachine.h"
#include "timeutils_epoch.h"
#include "machrtc.h"

DWORD get_fattime(void) {

// TODO: Optimize division (there's no HW division support on ESP8266,
// so it's expensive).
//uint32_t secs = (uint32_t)(pyb_rtc_get_us_since_2000() / 1000000);
uint32_t secs = 0;
uint32_t secs = (uint32_t)(mach_rtc_get_us_since_epoch() / 1000000);

timeutils_struct_time_t tm;
timeutils_seconds_since_2000_to_struct_time(secs, &tm);
timeutils_seconds_since_epoch_to_struct_time(secs, &tm);

return (((DWORD)(tm.tm_year - 1980) << 25) | ((DWORD)tm.tm_mon << 21) | ((DWORD)tm.tm_mday << 16) |
((DWORD)tm.tm_hour << 11) | ((DWORD)tm.tm_min << 5) | ((DWORD)tm.tm_sec >> 1));
Expand Down
Loading