From 64147c379423c9da9416eaadfb37e37e0f393f2b Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Mon, 27 Jun 2022 15:16:50 +0800 Subject: [PATCH] CXX: Make spi_flash related part works --- .../test_spi_flash_emulation.cpp | 20 ++----- .../sim/stubs/bsd/include/bsd_strings.h | 8 +++ components/spi_flash/sim/stubs/bsd/strlcpy.c | 60 +++++++++++++++++++ components/wear_levelling/SPI_Flash.cpp | 13 ++-- .../wear_levelling/test_wl_host/test_wl.cpp | 2 +- tools/ci/check_copyright_ignore.txt | 4 +- 6 files changed, 83 insertions(+), 24 deletions(-) create mode 100644 components/spi_flash/sim/stubs/bsd/include/bsd_strings.h create mode 100644 components/spi_flash/sim/stubs/bsd/strlcpy.c diff --git a/components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp b/components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp index 64ea96b174..aa9c05f1c6 100644 --- a/components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp +++ b/components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp @@ -1,18 +1,10 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include "catch.hpp" -#include "esp_spi_flash.h" +#include "spi_flash_mmap.h" #include "esp_partition.h" #include "spi_flash_emulation.h" #include diff --git a/components/spi_flash/sim/stubs/bsd/include/bsd_strings.h b/components/spi_flash/sim/stubs/bsd/include/bsd_strings.h new file mode 100644 index 0000000000..1e7cfac71d --- /dev/null +++ b/components/spi_flash/sim/stubs/bsd/include/bsd_strings.h @@ -0,0 +1,8 @@ +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +size_t strlcpy(char *dst, const char *src, size_t dsize); diff --git a/components/spi_flash/sim/stubs/bsd/strlcpy.c b/components/spi_flash/sim/stubs/bsd/strlcpy.c new file mode 100644 index 0000000000..464a994004 --- /dev/null +++ b/components/spi_flash/sim/stubs/bsd/strlcpy.c @@ -0,0 +1,60 @@ +/* $OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $ */ + +/*- + * Copyright (c) 1998 Todd C. Miller + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +/* + * Copy string src to buffer dst of size dsize. At most dsize-1 + * chars will be copied. Always NUL terminates (unless dsize == 0). + * Returns strlen(src); if retval >= dsize, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t dsize) +{ + const char *osrc = src; + size_t nleft = dsize; + + /* Copy as many bytes as will fit. */ + if (nleft != 0) { + while (--nleft != 0) { + if ((*dst++ = *src++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src. */ + if (nleft == 0) { + if (dsize != 0) + *dst = '\0'; /* NUL-terminate dst */ + while (*src++) + ; + } + + return(src - osrc - 1); /* count does not include NUL */ +} diff --git a/components/wear_levelling/SPI_Flash.cpp b/components/wear_levelling/SPI_Flash.cpp index 664e1947c7..695b1979a3 100644 --- a/components/wear_levelling/SPI_Flash.cpp +++ b/components/wear_levelling/SPI_Flash.cpp @@ -6,7 +6,8 @@ #include "esp_log.h" #include "SPI_Flash.h" -#include "esp_spi_flash.h" +#include "spi_flash_mmap.h" +#include "esp_rom_spiflash.h" static const char *TAG = "spi_flash"; SPI_Flash::SPI_Flash() @@ -15,12 +16,12 @@ SPI_Flash::SPI_Flash() size_t SPI_Flash::chip_size() { - return spi_flash_get_chip_size(); + return g_rom_flashchip.chip_size; } esp_err_t SPI_Flash::erase_sector(size_t sector) { - esp_err_t result = spi_flash_erase_sector(sector); + esp_err_t result = esp_rom_spiflash_erase_sector(sector); if (result == ESP_OK) { ESP_LOGV(TAG, "erase_sector - sector=0x%08x, result=0x%08x", sector, result); } else { @@ -32,7 +33,7 @@ esp_err_t SPI_Flash::erase_range(size_t start_address, size_t size) { size = (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE; size = size * SPI_FLASH_SEC_SIZE; - esp_err_t result = spi_flash_erase_range(start_address, size); + esp_err_t result = esp_rom_spiflash_erase_area(start_address, size); if (result == ESP_OK) { ESP_LOGV(TAG, "erase_range - start_address=0x%08x, size=0x%08x, result=0x%08x", start_address, size, result); } else { @@ -43,7 +44,7 @@ esp_err_t SPI_Flash::erase_range(size_t start_address, size_t size) esp_err_t SPI_Flash::write(size_t dest_addr, const void *src, size_t size) { - esp_err_t result = spi_flash_write(dest_addr, src, size); + esp_err_t result = esp_rom_spiflash_write(dest_addr, (uint32_t*)src, size); if (result == ESP_OK) { ESP_LOGV(TAG, "write - dest_addr=0x%08x, size=0x%08x, result=0x%08x", dest_addr, size, result); } else { @@ -54,7 +55,7 @@ esp_err_t SPI_Flash::write(size_t dest_addr, const void *src, size_t size) esp_err_t SPI_Flash::read(size_t src_addr, void *dest, size_t size) { - esp_err_t result = spi_flash_read(src_addr, dest, size); + esp_err_t result = esp_rom_spiflash_read(src_addr, (uint32_t*)dest, size); if (result == ESP_OK) { ESP_LOGV(TAG, "read - src_addr=0x%08x, size=0x%08x, result=0x%08x", src_addr, size, result); } else { diff --git a/components/wear_levelling/test_wl_host/test_wl.cpp b/components/wear_levelling/test_wl_host/test_wl.cpp index 6a9bf3260d..fcf8ad7c44 100644 --- a/components/wear_levelling/test_wl_host/test_wl.cpp +++ b/components/wear_levelling/test_wl_host/test_wl.cpp @@ -7,7 +7,7 @@ #include #include -#include "esp_spi_flash.h" +#include "spi_flash_mmap.h" #include "esp_partition.h" #include "wear_levelling.h" #include "WL_Flash.h" diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 5dc6c4f080..397ac17feb 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -648,7 +648,6 @@ components/esp_serial_slave_link/include/esp_serial_slave_link/essl_spi.h components/esp_serial_slave_link/include/essl_spi/esp32c3_defs.h components/esp_serial_slave_link/include/essl_spi/esp32s2_defs.h components/esp_serial_slave_link/include/essl_spi/esp32s3_defs.h -components/esp_system/esp_err.c components/esp_system/include/eh_frame_parser.h components/esp_system/include/esp_expression_with_stack.h components/esp_system/include/esp_freertos_hooks.h @@ -1002,7 +1001,6 @@ components/nvs_flash/test_nvs_host/esp_error_check_stub.cpp components/nvs_flash/test_nvs_host/main.cpp components/nvs_flash/test_nvs_host/sdkconfig.h components/nvs_flash/test_nvs_host/spi_flash_emulation.cpp -components/nvs_flash/test_nvs_host/spi_flash_emulation.h components/nvs_flash/test_nvs_host/test_fixtures.hpp components/nvs_flash/test_nvs_host/test_intrusive_list.cpp components/nvs_flash/test_nvs_host/test_nvs_cxx_api.cpp @@ -1010,7 +1008,6 @@ components/nvs_flash/test_nvs_host/test_nvs_handle.cpp components/nvs_flash/test_nvs_host/test_nvs_initialization.cpp components/nvs_flash/test_nvs_host/test_nvs_partition.cpp components/nvs_flash/test_nvs_host/test_nvs_storage.cpp -components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp components/openthread/include/esp_openthread.h components/openthread/include/esp_openthread_lock.h components/openthread/include/esp_openthread_netif_glue.h @@ -1431,6 +1428,7 @@ components/spi_flash/sim/SpiFlash.cpp components/spi_flash/sim/flash_mock.cpp components/spi_flash/sim/flash_mock_util.c components/spi_flash/sim/sdkconfig/sdkconfig.h +components/spi_flash/sim/stubs/bsd/strlcpy.c components/spi_flash/spi_flash_chip_boya.c components/spi_flash/spi_flash_chip_gd.c components/spi_flash/spi_flash_chip_issi.c