一、ESP-IDF部分的处理:

1. 设置esp-idf 4.4环境

# 设置spressif_esp32_c3
mkdir hardware/chip/espressif_esp32_c3
cp hardware/chip/espressif_esp32/*.* hardware/chip/espressif_esp32_c3/
cp hardware/chip/espressif_esp32/SConscript hardware/chip/espressif_esp32_c3/

# ~/esp/esp-idf已经切换到release/v4.4
rsync -avP ~/esp/esp-idf hardware/chip/espressif_esp32_c3/

2. 修改必要的文件

2.1 diskio.h -> ff.h

修改 hardware/chip/espressif_esp32_c3/esp-idf/components/fatfs/src/diskio.h,添加对AliOS-Things文件接口调用

extern "C" {
 #endif

+ // add for HaaSPython
+ #include "ff.h"

 /* Status of Disk Functions */
 typedef BYTE   DSTATUS;
2.2 esp_vfs.h --> statfs

修改 hardware/chip/espressif_esp32_c3/esp-idf/components/vfs/include/esp_vfs.h,添加statfs_p和statfs

esp_err_t (*end_select)(void *end_select_args);
 #endif // CONFIG_VFS_SUPPORT_SELECT
+    // add for HaaSPython
+    union {
+        int (*statfs_p)(void *ctx, const char *path, void* sb);
+        int (*statfs)(const char *path, void *sb);
+    };
 } esp_vfs_t;

二、AliOS-Things部分的处理:

1. 编译环境匹配

1.1 espressif_esp32_c3/package.yaml

修改hardware/chip/espressif_esp32_c3/package.yaml 设置为c3环境:

## 第一部分: 基础信息
name: espressif_esp32_c3                            # <必选项> 包名称 (符合C语言变量命名规则),长度少于等于64字节
version: dev_aos                                    # <必选项> 组件版本号
description: esp-idf for esp32-c3             # <必选项> 建议至少20字以上
type: chip                                          # <必选项> 组件类型,为:solution, chip, board, common, sdk

tag: 核心模块                                        # <可选项> 组件分类,缺省值: ''
keywords:                                           # <可选项> 标签,会影响到组件被搜索的效果,合理的标签很重要
  - base
license: Apache license v2.0                        # <可选项> 源代码的许可证,要确保所有代码、文件的许可证不冲突。如:MIT,Apache license v2.0,BSD

#           指定该组件依赖的组件及版本,版本支持条件比较,支持:>=v1.0, >v1.0, ==v1.0, <=v1.0, <v1.0, v1.0
#           未指定条件时,默认为 ==,如 v1.0 与 ==v1.0
# depends:                                 # <可选项> 该组件依赖其他的组件,合理的依赖才能保证组件能编译、使用
  # - libc_stub: dev_aos
#   - aos: >=v7.2.0

## 第四部分:编译连接信息
# build_config:                            # <可选项> 编译配置项
#   include:                               # <可选项> 编译时,影响编译器的-I 参数 ,全局有效
#     - src                                #        include 只能是该软件包下的目录,不能使用外部目录
#   internal_include:                      # <可选项> 编译时,影响编译器的-I 参数 ,组件内有效
#     - include
#   cflag: ''                              # <可选项> C 编译器所需要要的编译参数
#   cxxflag: ''                            # <可选项> CXX 编译器所需要要的编译参数
#   asmflag: ''                            # <可选项> 汇编器所需要要参数
#   define:                                # <可选项> 宏定义, 增加编译器的-D 选项,如:
#     XXX: 1                               #   -DXXX=1
#     AAA: 1                               #   -DAAA
#     STR: "abc"                           #   -DSTR="abc"
#   libs:                                  # 该组件中支持的二进制静态库,如:libxxx.a, libyyy.a
#     - xxx                                #   -lxxx
#     - yyy                                #   -lyyy
#   libpath:                               # 指定静态库所在的路径(相对于该组件路径)
#     - libs                               #   -Llibs
build_config:
  cflag: >
        -march=rv32imc -ffunction-sections -fdata-sections -Wall -Wextra -Wno-unused-parameter
        -Wno-sign-compare -ggdb -nostartfiles -Wno-format -O2 -fstrict-volatile-bitfields
        -fno-jump-tables -fno-tree-switch-conversion -std=gnu99 -Wno-old-style-declaration
        -Wno-clobbered -Wno-deprecated-declarations -Wno-missing-field-initializers -DKERNEL=FREERTOS

  cxxflag: >
        -march=rv32imc -ffunction-sections -fdata-sections -Wall -Wextra -Wno-unused-parameter
        -Wno-sign-compare -ggdb -nostartfiles -Wno-format -O2 -fstrict-volatile-bitfields
        -fno-jump-tables -fno-tree-switch-conversion -std=gnu99 -Wno-old-style-declaration
        -Wno-clobbered -Wno-deprecated-declarations -Wno-missing-field-initializers
        -std=gnu++11 -fno-exceptions -fno-rtti -fpermissive -DKERNEL=FREERTOS

  ldflag: >
        -march=rv32imc -nostartfiles --specs=nosys.specs -Wl,--cref -Wl,--Map="aos.map" -Wl,--gc-sections
        -fno-rtti -fno-lto -Wl,--wrap=vPortCleanUpTCB
        -uesp_app_desc -upthread_include_pthread_impl -upthread_include_pthread_cond_impl -u__ubsan_include
        -upthread_include_pthread_local_storage_impl -upthread_include_pthread_rwlock_impl
        -u__cxx_fatal_exception -uvfs_include_syscalls_impl -uapp_main -unewlib_include_heap_impl
        -unewlib_include_syscalls_impl -unewlib_include_pthread_impl  -unewlib_include_assert_impl
        -u__cxa_guard_dummy -Tmemory.ld -Tsections.ld -Tesp32c3.rom.ld -Tesp32c3.rom.api.ld
        -Tesp32c3.rom.libgcc.ld -Tesp32c3.rom.newlib.ld -Tesp32c3.rom.version.ld -Tesp32c3.rom.eco3.ld
        -Tesp32c3.peripherals.ld  -Wl,--undefined=uxTopUsedPriority
        -Wl,--wrap=_Unwind_SetEnableExceptionFdeSorting  -Wl,--wrap=__register_frame_info_bases
        -Wl,--wrap=__register_frame_info  -Wl,--wrap=__register_frame
        -Wl,--wrap=__register_frame_info_table_bases -Wl,--wrap=__register_frame_info_table
        -Wl,--wrap=__register_frame_table -Wl,--wrap=__deregister_frame_info_bases
        -Wl,--wrap=__deregister_frame_info  -Wl,--wrap=_Unwind_Find_FDE  -Wl,--wrap=_Unwind_GetGR
        -Wl,--wrap=_Unwind_GetCFA -Wl,--wrap=_Unwind_GetIP  -Wl,--wrap=_Unwind_GetIPInfo
        -Wl,--wrap=_Unwind_GetRegionStart -Wl,--wrap=_Unwind_GetDataRelBase
        -Wl,--wrap=_Unwind_GetTextRelBase -Wl,--wrap=_Unwind_SetIP  -Wl,--wrap=_Unwind_SetGR
        -Wl,--wrap=_Unwind_GetLanguageSpecificData -Wl,--wrap=_Unwind_FindEnclosingFunction
        -Wl,--wrap=_Unwind_Resume -Wl,--wrap=_Unwind_RaiseException
        -Wl,--wrap=_Unwind_DeleteException -Wl,--wrap=_Unwind_ForcedUnwind
        -Wl,--wrap=_Unwind_Resume_or_Rethrow -Wl,--wrap=_Unwind_Backtrace
        -Wl,--wrap=__cxa_call_unexpected -Wl,--wrap=__gxx_personality_v0
        -lstdc++ -lgcc -lgcov -lc -lm -z muldefs

  include:
    - esp-idf/components/newlib/platform_include
    - esp-idf/components/freertos/include
    - esp-idf/components/freertos/include/esp_additions/freertos
    - esp-idf/components/freertos/port/riscv/include
    - esp-idf/components/freertos/include/esp_additions
    - esp-idf/components/esp_hw_support/include
    - esp-idf/components/esp_hw_support/include/soc
    - esp-idf/components/esp_hw_support/include/soc/esp32c3
    - esp-idf/components/esp_hw_support/port/esp32c3
    - esp-idf/components/esp_hw_support/port/esp32c3/private_include
    - esp-idf/components/heap/include
    - esp-idf/components/log/include
    - esp-idf/components/lwip/include/apps
    - esp-idf/components/lwip/include/apps/sntp
    - esp-idf/components/lwip/lwip/src/include
    - esp-idf/components/lwip/port/esp32/include
    - esp-idf/components/lwip/port/esp32/include/arch
    - esp-idf/components/soc/include
    - esp-idf/components/soc/esp32c3
    - esp-idf/components/soc/esp32c3/include
    - esp-idf/components/hal/esp32c3/include
    - esp-idf/components/hal/include
    - esp-idf/components/hal/platform_port/include
    - esp-idf/components/esp_rom/include
    - esp-idf/components/esp_rom/include/esp32c3
    - esp-idf/components/esp_rom/esp32c3
    - esp-idf/components/esp_common/include
    - esp-idf/components/esp_system/include
    - esp-idf/components/esp_system/port/soc
    - esp-idf/components/esp_system/port/include/riscv
    - esp-idf/components/esp_system/port/public_compat
    - esp-idf/components/riscv/include
    - esp-idf/components/driver/include
    - esp-idf/components/driver/esp32c3/include
    - esp-idf/components/esp_pm/include
    - esp-idf/components/esp_ringbuf/include
    - esp-idf/components/efuse/include
    - esp-idf/components/efuse/esp32c3/include
    - esp-idf/components/vfs/include
    - esp-idf/components/esp_wifi/include
    - esp-idf/components/esp_event/include
    - esp-idf/components/esp_netif/include
    - esp-idf/components/esp_eth/include
    - esp-idf/components/tcpip_adapter/include
    - esp-idf/components/esp_phy/include
    - esp-idf/components/esp_phy/esp32c3/include
    - esp-idf/components/esp_ipc/include
    - esp-idf/components/app_trace/include
    - esp-idf/components/esp_timer/include
    - esp-idf/components/app_update/include
    - esp-idf/components/spi_flash/include
    - esp-idf/components/bootloader_support/include
    - esp-idf/components/bt/common/osi/include
    - esp-idf/components/bt/include/esp32c3/include
    - esp-idf/components/bt/common/api/include/api
    - esp-idf/components/bt/common/btc/profile/esp/blufi/include
    - esp-idf/components/bt/common/btc/profile/esp/include
    - esp-idf/components/bt/host/nimble/nimble/porting/nimble/include
    - esp-idf/components/bt/host/nimble/port/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/services/ans/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/services/bas/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/services/dis/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/services/gap/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/services/gatt/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/services/ias/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/services/ipss/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/services/lls/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/services/tps/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/util/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/store/ram/include
    - esp-idf/components/bt/host/nimble/nimble/nimble/host/store/config/include
    - esp-idf/components/bt/host/nimble/nimble/porting/npl/freertos/include
    - esp-idf/components/bt/host/nimble/esp-hci/include
    - esp-idf/components/nvs_flash/include
    - esp-idf/components/mbedtls/port/include
    - esp-idf/components/mbedtls/mbedtls/include
    - esp-idf/components/mbedtls/esp_crt_bundle/include
    - esp-idf/components/mdns/include
    - esp-idf/components/console
    - esp-idf/components/sdmmc/include
    - esp-idf/components/fatfs/vfs
    - esp-idf/components/fatfs/src
    - esp-idf/components/wear_levelling/include
    - esp-idf/components/json/cJSON
    - esp-idf/components/soc/esp32s3/include

  libs:
    - esp_ringbuf
    - efuse
    - esp_ipc
    - driver
    - esp_pm
    - mbedtls
    - mbedcrypto
    - mbedx509
    - coexist
    - core
    - espnow
    - mesh
    - net80211
    - pp
    - smartconfig
    - wapi
    - app_update
    - bootloader_support
    - spi_flash
    - nvs_flash
    - pthread
    - esp_gdbstub
    - espcoredump
    - esp_phy
    - esp_system
    - esp_rom
    - hal
    - vfs
    - esp_eth
    - tcpip_adapter
    - esp_netif
    - esp_event
    - wpa_supplicant
    - esp_wifi
    - console
    - lwip
    - log
    - heap
    - soc
    - esp_hw_support
    - riscv
    - esp_common
    - esp_timer
    - freertos
    - newlib
    - cxx
    - app_trace
    - asio
    - bt
    - cbor
    - unity
    - cmock
    - coap
    - nghttp
    - esp-tls
    - esp_adc_cal
    - esp_hid
    - tcp_transport
    - esp_http_client
    - esp_http_server
    - esp_https_ota
    - esp_lcd
    - protobuf-c
    - protocomm
    - mdns
    - esp_local_ctrl
    - sdmmc
    - esp_serial_slave_link
    - esp_websocket_client
    - expat
    - wear_levelling
    - fatfs
    - freemodbus
    - jsmn
    - json
    - liblibsodium
    - mqtt
    - openssl
    - spiffs
    - wifi_provisioning
    - main
    - btdm_app
    - btbb
    - phy

  libpath:
    - esp-idf/components/bt/controller/lib_esp32c3_family/esp32c3
    - esp-idf/components/esp_phy/lib/esp32c3
    - esp-idf/components/esp_wifi/lib/esp32c3
    - esp-idf/components/soc/esp32c3/ld
    - esp-idf/components/esp_rom/esp32c3/ld

## 第五部分:配置信息
def_config:                              # 组件的可配置项
  IDF_VER: "8153bfe4"
  ESP_PLATFORM: 1
  OS_FREERTOS: 1
  MBEDTLS_CONFIG_FILE: "mbedtls/esp_config.h"
  CONFIG_ESP_DISPATCHER_DELEGATE_TASK_CORE: 0
  CONFIG_ESP_DISPATCHER_DELEGATE_TASK_PRIO: 10
  CONFIG_ESP_DISPATCHER_DELEGATE_STACK_SIZE: 4096

hw_info:
  arch_name: riscv
  cpu_name: ESP32-C3
  toolchain_prefix: riscv32-esp-elf
  vendor_name: espressif
  ld_script: espressif_idf.lds

## 第六部分:安装信息
# install:
#   - dest: include/                       # 安装的目的路径 dest是相当路径,通常是相对于YoC SDK 安装目录
#     source:                              # 安装源列表
#      - src/*.h                           # 支持通配符,相对路径
# install:
#   - dest: "include"
#     source:
#       - hal/2ndboot/*.h
#       - aos/*.h
#       - aos/arch/*.h
#       - drivers/platform/hal/*.h
#       - drivers/platform/cmsis/inc/*.h
#       - drivers/rtos/rhino/cmsis/*.h
#       - drivers/utils/hwtimer_list/*.h
#       - drivers/platform/hal/haas1000/*.h
#       - drivers/services/wifi_app/alsa_adapter/*.h
#   - dest: "lib"
#     source:
#       - drivers/libmcuhaas1000.a ? <!CONFIG_A7_DSP_ENABLE>
#       - drivers/libmcuhaas1000_a7.a ? <CONFIG_A7_DSP_ENABLE>


## 第七部分:导出部分
# export:
#   - dest: "<SOLUTION_PATH>/generated/data" # 安装的目的路径 dest是相当路径
#     source:                                # 安装源列表
#       - "bootimgs/boot"
#       - "bootimgs/tee"
#       - "bootimgs/mtb"
#       - "configs/config.yaml"
1.2 espressif_idf.lds设置
# 重命名.lds文件,该文件内容为空
mv hardware/chip/espressif_esp32_c3/_esp32_alios.lds hardware/chip/espressif_esp32_c3/espressif_idf.lds

2. board配置:

2.1 生成nodemcu32_c3/package.yaml
cp -a hardware/board/nodemcu32s hardware/board/nodemcu32s_c3

修改hardware/board/nodemcu32_c3/package.yaml

## 第一部分: 基础信息
name: nodemcu32s_c3                                      # <必选项> 包名称 (符合C语言变量命名规则),长度少于等于64字节
version: dev_aos                                   # <必选项> 组件版本号
description: NodeMCU-32SC3 board configure.             # <必选项> 建议至少20字以上
type: board                                        # <必选项> 组件类型,为:solution, chip, board, common

tag: 核心模块                                      # <可选项> 组件分类,缺省值: ''

## 第二部分:依赖信息
#           指定该组件依赖的组件及版本,版本支持条件比较,支持:>=v1.0, >v1.0, ==v1.0, <=v1.0, <v1.0, v1.0
#           未指定条件时,默认为 ==,如 v1.0 与 ==v1.0
# depends:                                 # <可选项> 该组件依赖其他的组件,合理的依赖才能保证组件能编译、使用
#   - minilibc: v7.2.0
#   - aos: >=v7.2.0
depends:
  - espressif_esp32_c3: master

board:
  chip_name: espressif_esp32_c3

def_config:
  BOARD_NODEMCU32C3: 1
  SYSINFO_MCU: "ESP32"
  SYSINFO_BOARD: "NODEMCU32C3"
  # littlefs config
  CONFIG_LITTLEFS_CNT: 1 # lfs supports up to 2 partitions, please choose 1 or 2 here
  CONFIG_LFS_MOUNTPOINT: "" # the mount path of the lfs partition
  CONFIG_MAX_FILES: 10 # concurrently opened files num
  CONFIG_LFS_PROG_SIZE: 256
  CONFIG_LFS_PAGE_NUM_PER_BLOCK: 16
  LFS_CONFIG_DEBUG: 0
  LFS_CONFIG_TRACE: 0
# CONFIG_LFS_PROG_SIZE: 1024 # the mininal programable size, usually page size for nand and any size for nor
#  CONFIG_LFS_PAGE_NUM_PER_BLOCK: 4 # choose the right value for nand, and usually any value for nor
#  CONFIG_LFS_BLOCK_NUMS: 1198 # the total block count for lfs parition

3. 工程配置:

3.1 package.yaml(无需修改)

文件:solutions/py_engine_esp32_demo/package.yaml 文件根据工程选择,自动修改

3.2 makeimg.sh

文件:solutions/py_engine_esp32_demo/makeimg.sh

修改 espressif_esp32 为 espressif_esp32_c3
修改 --chip esp32 为 --chip esp32c3
3.3 makeimg.py

文件:olutions/py_engine_esp32_demo/makeimg.py

可根据需要,注释如下data部分:

files_in = [
    ("bootloader", offset_bootloader, max_size_bootloader, arg_bootloader_bin),
    ("partitions", offset_partitions, max_size_partitions, arg_partitions_bin),
    ("application", offset_application, max_size_application, arg_application_bin),
    //("data", offset_data, max_size_data, arg_data_bin),
]

4. 组件配置

4.1 设置编译配置参数:GENERIC_C3/sdkconfig.board

修改文件文件components/py_engine/adapter/esp32/boards/GENERIC_C3/sdkconfig.board,添加如下信息:

CONFIG_ESP32C3_REV_MIN_3=y
CONFIG_ESP32C3_REV_MIN=3
CONFIG_ESP32C3_BROWNOUT_DET=y
CONFIG_ESP32C3_BROWNOUT_DET_LVL_SEL_7=
CONFIG_ESP32C3_BROWNOUT_DET_LVL_SEL_4=y
CONFIG_ESP32C3_BROWNOUT_DET_LVL=4
CONFIG_ESP_CONSOLE_UART_DEFAULT=y
CONFIG_ESP_CONSOLE_USB_CDC=
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=

CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096
4.1.1 2MB规格开发板特殊处理
4.1.1.1 启用2MB配置

修改文件文件components/py_engine/adapter/esp32/boards/GENERIC_C3/sdkconfig.board,继续添加如下信息:

# 此处应修改该文件中已有的配置
CONFIG_PARTITION_TABLE_OFFSET=0x9000

# 2M规格
CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-2MiB.csv"
4.1.1.2 配置对应分区表

修改文件文件components/py_engine/adapter/esp32/partitions-2MiB.csv:

# 注释原有的,添加下面的:
factory,    app,    factory,0x10000,    1900k,
nvs,        data,   nvs,    0x1eb000,    4K,
phy_init,   data,   phy,    0x1ec000,    4K,
vfs,        data,   fat,    0x1ed000,   72K,
kv,         data,   131,    0x1ff000,   4K,

说明:因为固件本身接近1.9M,故取消了OTA分区,其他分区也相应减小

4.2 修改源码:components/py_engine/
4.2.1 adapter/esp32/main.c

文件:components/py_engine/adapter/esp32/main.c,注释machine_i2s_init0调用;

或者修改adapter/esp32/mpconfigport.h设置MICROPY_PY_MACHINE_I2S定义为0

#if MICROPY_PY_MACHINE_I2S
-    machine_i2s_init0();
+    // machine_i2s_init0();
 #endif
#ifndef MICROPY_PY_MACHINE_I2S
- #define MICROPY_PY_MACHINE_I2S              (1)
+ #define MICROPY_PY_MACHINE_I2S              (0)
#endif
4.2.2 adapter/esp32/uart.c

文件:components/py_engine/adapter/esp32/uart.c 大量修改

#include "uart.h"

STATIC void uart_irq_handler(void *arg);

void uart_init(void) {
    uart_config_t uartcfg = {
        .baud_rate = MICROPY_HW_UART_REPL_BAUD,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .rx_flow_ctrl_thresh = 0
    };
    uart_param_config(MICROPY_HW_UART_REPL, &uartcfg);

    const uint32_t rxbuf = 129; // IDF requires > 128 min
    const uint32_t txbuf = 0;

    uart_driver_install(MICROPY_HW_UART_REPL, rxbuf, txbuf, 0, NULL, 0);

    uart_isr_handle_t handle;
    uart_isr_free(MICROPY_HW_UART_REPL);
    uart_isr_register(MICROPY_HW_UART_REPL, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle);
    uart_enable_rx_intr(MICROPY_HW_UART_REPL);
}

int uart_stdout_tx_strn(const char *str, size_t len) {
    size_t remaining = len;
    // TODO add a timeout
    for (;;) {
        int ret = uart_tx_chars(MICROPY_HW_UART_REPL, str, remaining);
        if (ret == -1) {
            return -1;
        }
        remaining -= ret;
        if (remaining <= 0) {
            break;
        }
        str += ret;
        ulTaskNotifyTake(pdFALSE, 1);
    }
    return len - remaining;
}

STATIC void IRAM_ATTR uart_irq_handler(void *arg) {
    volatile uart_dev_t *uart = &UART0;
    #if CONFIG_IDF_TARGET_ESP32S3
    uart->int_clr.rxfifo_full_int_clr = 1;
    uart->int_clr.rxfifo_tout_int_clr = 1;
    #else
    uart->int_clr.rxfifo_full = 1;
    uart->int_clr.rxfifo_tout = 1;
    uart->int_clr.frm_err = 1;
    #endif
    while (uart->status.rxfifo_cnt) {
        #if CONFIG_IDF_TARGET_ESP32
        uint8_t c = uart->fifo.rw_byte;
        #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
        uint8_t c = READ_PERI_REG(UART_FIFO_AHB_REG(0)); // UART0
        #endif
        if (c == mp_interrupt_char) {
            mp_sched_keyboard_interrupt();
        } else {
            // this is an inline function so will be in IRAM
            ringbuf_put(&stdin_ringbuf, c);
        }
    }
}
4.2.3 adapter/esp32/uart.h

文件:components/py_engine/adapter/esp32/uart.h

#ifndef MICROPY_INCLUDED_ESP32_UART_H
#define MICROPY_INCLUDED_ESP32_UART_H

+ #ifndef MICROPY_HW_UART_REPL
+ #define MICROPY_HW_UART_REPL (UART_NUM_0)
+ #endif

+ #ifndef MICROPY_HW_UART_REPL_BAUD
+ #define MICROPY_HW_UART_REPL_BAUD (115200)
+ #endif

void uart_init(void);
+ int uart_stdout_tx_strn(const char *str, size_t len);

#endif // MICROPY_INCLUDED_ESP32_UART_H
4.3 脚本:
4.3.1 build_cmake.sh(无需修改)

文件:components/py_engine/build_cmake.sh

无需修改,内置C3,只要前面board配置正确名称为NODEMCU32C3即可

三、编译

使用haas studio的编译按钮编译,或者使用如下指令:

# 在 AliOS-Things 根目录操作
aos make V=1 2>&1 | tee build.log

如编译错误,通过build.log查看错误信息,关键字:【error:】、【failed】、【raise】

四、烧录

# esptool.py 在macOS、Linux命令会自动查找可用串口
esptool.py --chip esp32c3 --baud 460800 write_flash -z 0x0 firmware.bin

# 手动设置串口:  --port 串口名称

# 如遇到问题,可以先清空flash: esptool.py --chip esp32c3 erase_flash

五、测试

# 使用串口工具或者终端连接,波特率115200
>>> help()
>>> import machine
>>> machine.reset()
>>> help('modules')

六、可能问题

1. sdkconfig.h

提示:fatal error: sdkconfig.h: No such file or directory
原因:查找该信息前面的错误信息,找到根源

2. genhdr/qstrdefs.generated.h

# components/py_engine/engine/py/qstr.h
问题:
components/py_engine/engine/py/qstr.h:44:14: fatal error: genhdr/qstrdefs.generated.h: No such file or directory

检查:
vim components/py_engine/engine/py/qstr.h
#define QDEF0(id, str) id,
#define QDEF1(id, str)
    #include "genhdr/qstrdefs.generated.h"
#undef QDEF0
#undef QDEF1

find . -name "qstrdefs.generated.h"
./adapter/esp32/build-GENERIC_C3/genhdr/qstrdefs.generated.h
./engine/mpy-cross/build/genhdr/qstrdefs.generated.h

处理:
正确设置hardware/board/nodemcu32_c3/package.yaml中的BOARD_NODEMCU32C3,就不会出现该错误

3. esp32/machine_pin.c

# components/py_engine/adapter/esp32/machine_pin.c
问题:
components/py_engine/adapter/esp32/machine_pin.c:264:29: error: 'USB_DEVICE_CONF0_REG' undeclared (first use in this function); did you mean 'UART_SWFC_CONF0_REG'?

处理:
考虑先注释掉? 注释后,micropython部分可以编译通过
    #if CONFIG_IDF_TARGET_ESP32C3
    if (self->id == 18 || self->id == 19) {
        // CLEAR_PERI_REG_MASK(USB_DEVICE_CONF0_REG, USB_DEVICE_USB_PAD_ENABLE);
    }