diff --git a/.gitmodules b/.gitmodules index 7c382da67..e67a5d595 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,9 @@ [submodule "lib/btstack"] path = lib/btstack url = https://github.com/bluekitchen/btstack.git +[submodule "lib/pico-vfs"] + path = lib/pico-vfs + url = https://github.com/oyama/pico-vfs.git +[submodule "lib/littlefs"] + path = lib/littlefs + url = https://github.com/littlefs-project/littlefs.git diff --git a/lib/littlefs b/lib/littlefs new file mode 160000 index 000000000..d01280e64 --- /dev/null +++ b/lib/littlefs @@ -0,0 +1 @@ +Subproject commit d01280e64934a09ba16cac60cf9d3a37e228bb66 diff --git a/lib/pico-vfs b/lib/pico-vfs new file mode 160000 index 000000000..30362eb00 --- /dev/null +++ b/lib/pico-vfs @@ -0,0 +1 @@ +Subproject commit 30362eb00ac60bdd786e9b623a1dcfc91e808b69 diff --git a/src/cmake/rp2_common.cmake b/src/cmake/rp2_common.cmake index 494b1202b..6ee773346 100644 --- a/src/cmake/rp2_common.cmake +++ b/src/cmake/rp2_common.cmake @@ -120,6 +120,8 @@ if (NOT PICO_BARE_METAL) pico_add_subdirectory(rp2_common/pico_stdio_usb) pico_add_subdirectory(rp2_common/pico_i2c_slave) + pico_add_subdirectory(rp2_common/pico_filesystem) + # networking libraries - note dependency order is important pico_add_subdirectory(rp2_common/pico_async_context) pico_add_subdirectory(rp2_common/pico_btstack) diff --git a/src/rp2_common/pico_filesystem/CMakeLists.txt b/src/rp2_common/pico_filesystem/CMakeLists.txt new file mode 100644 index 000000000..bea05f78e --- /dev/null +++ b/src/rp2_common/pico_filesystem/CMakeLists.txt @@ -0,0 +1,138 @@ +if (NOT PICO_VFS_PATH) + set(PICO_VFS_PATH ${PROJECT_SOURCE_DIR}/lib/pico-vfs) + if (NOT EXISTS ${PICO_VFS_PATH}/tests) + message(WARNING "pico-vfs submodule has not been initialized: File system support will be unavailable +hint: try 'git submodule update --init' from your SDK directory (${PICO_SDK_PATH}).") + endif() + if (NOT EXISTS ${PROJECT_SOURCE_DIR}/lib/littlefs/tests) + message(WARNING "littlefs submodule has not been initialized: File system support will be unavailabel +hint: try 'git submodule update --init' from your SDK directory (${PICO_SDK_PATH}).") + endif() +elseif (NOT EXISTS ${PICO_VFS_PATH}/tests) + message(WARNING "PICO_VFS_PATH specified but content not present.") +endif() + +if (EXISTS ${PICO_VFS_PATH}/tests) + message("pico-vfs available at ${PICO_VFS_PATH}/tests; enabling build support for file system.") + + pico_register_common_scope_var(PICO_VFS_PATH) + + pico_add_library(pico_filesystem_blockdevice_sd) + target_sources(pico_filesystem_blockdevice_sd INTERFACE + ${PICO_VFS_PATH}/src/blockdevice/sd.c) + target_include_directories(pico_filesystem_blockdevice_sd INTERFACE + ${PICO_VFS_PATH}/include) + target_link_libraries(pico_filesystem_blockdevice_sd INTERFACE + hardware_spi + pico_sync + ) + + pico_add_library(pico_filesystem_blockdevice_flash) + target_sources(pico_filesystem_blockdevice_flash INTERFACE + ${PICO_VFS_PATH}/src/blockdevice/flash.c) + target_include_directories(pico_filesystem_blockdevice_flash INTERFACE + ${PICO_VFS_PATH}/include) + target_link_libraries(pico_filesystem_blockdevice_flash INTERFACE + hardware_exception + hardware_flash + pico_sync + pico_flash + ) + + pico_add_library(pico_filesystem_blockdevice_heap) + target_sources(pico_filesystem_blockdevice_heap INTERFACE + ${PICO_VFS_PATH}/src/blockdevice/heap.c) + target_include_directories(pico_filesystem_blockdevice_heap INTERFACE + ${PICO_VFS_PATH}/include) + target_link_libraries(pico_filesystem_blockdevice_heap INTERFACE pico_sync) + + pico_add_library(pico_filesystem_filesystem_littlefs) + target_sources(pico_filesystem_filesystem_littlefs INTERFACE + ${PICO_VFS_PATH}/src/filesystem/littlefs.c + ${PROJECT_SOURCE_DIR}/lib/littlefs/lfs.c + ${PROJECT_SOURCE_DIR}/lib/littlefs/lfs_util.c + ) + target_include_directories(pico_filesystem_filesystem_littlefs INTERFACE + ${PROJECT_SOURCE_DIR}/lib/littlefs) + target_compile_options(pico_filesystem_filesystem_littlefs INTERFACE -Wno-unused-function -Wno-null-dereference) + target_link_libraries(pico_filesystem_filesystem_littlefs INTERFACE pico_sync) + + pico_add_library(pico_filesystem_filesystem_fat) + target_sources(pico_filesystem_filesystem_fat INTERFACE + ${PICO_VFS_PATH}/src/filesystem/fat.c + ${PICO_VFS_PATH}/vendor/ff15/source/ff.c + ${PICO_VFS_PATH}/vendor/ff15/source/ffsystem.c + ${PICO_VFS_PATH}/vendor/ff15/source/ffunicode.c + ) + target_include_directories(pico_filesystem_filesystem_fat INTERFACE + ${PICO_VFS_PATH} + ${PICO_VFS_PATH}/include/filesystem/ChaN + ${PICO_VFS_PATH}/vendor/ff15/source + ) + target_link_libraries(pico_filesystem_filesystem_fat INTERFACE pico_sync) + + pico_add_library(pico_filesystem) + target_sources(pico_filesystem INTERFACE + ${PICO_VFS_PATH}/src/filesystem/vfs.c) + target_include_directories(pico_filesystem INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/include + ${PICO_VFS_PATH}/include) + target_link_libraries(pico_filesystem INTERFACE + pico_clib_interface + pico_sync + ) + + pico_add_library(pico_filesystem_default) + target_sources(pico_filesystem_default INTERFACE + ${PICO_VFS_PATH}/src/filesystem/fs_init.c) + target_include_directories(pico_filesystem_default INTERFACE + ${PICO_VFS_PATH}/include) + target_link_libraries(pico_filesystem_default INTERFACE + pico_filesystem + pico_filesystem_blockdevice_flash + pico_filesystem_filesystem_littlefs + ) + + pico_promote_common_scope_vars() + + # + # File system enable and customise function + # + function(pico_enable_filesystem TARGET) + set(options "") + set(oneValueArgs SIZE AUTO_INIT MAX_FAT_VOLUME MAX_MOUNTPOINT) + set(multiValueArgs FS_INIT) + cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + # Default file system size in bytes. Must be a multiple of 4096 bytes + if(ARG_SIZE) + target_compile_definitions(${TARGET} PRIVATE PICO_FS_DEFAULT_SIZE=${ARG_SIZE}) + endif() + + # Add custom fs_init.c source files + if(ARG_FS_INIT) + target_sources(${TARGET} PRIVATE ${ARG_FS_INIT}) + else() + target_link_libraries(${TARGET} PRIVATE pico_filesystem_default) + endif() + + # Enable automatic execution of fs_init() + if(ARG_AUTO_INIT) + target_compile_definitions(${TARGET} PRIVATE PICO_FS_AUTO_INIT=1) + endif() + + # Maximum number of file system mount points + if(ARG_MAX_MOUNTPOINT) + target_compile_definitions(${TARGET} PRIVATE PICO_VFS_MAX_MOUNTPOINT=${ARG_MAX_MOUNTPOINT}) + else() + target_compile_definitions(${TARGET} PRIVATE PICO_VFS_MAX_MOUNTPOINT=8) + endif() + + # Maximum number of volumes in a FAT file system + if(ARG_MAX_FAT_VOLUME) + target_compile_definitions(${TARGET} PRIVATE PICO_VFS_MAX_FAT_VOLUME=${ARG_MAX_FAT_VOLUME}) + else() + target_compile_definitions(${TARGET} PRIVATE PICO_VFS_MAX_FAT_VOLUME=4) + endif() + endfunction() +endif() diff --git a/src/rp2_common/pico_filesystem/include/pico/filesystem.h b/src/rp2_common/pico_filesystem/include/pico/filesystem.h new file mode 100644 index 000000000..806b1c135 --- /dev/null +++ b/src/rp2_common/pico_filesystem/include/pico/filesystem.h @@ -0,0 +1,10 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + */ +#pragma once + +#include "filesystem/vfs.h" + +#if !defined(PICO_FS_DEFAULT_SIZE) +#define PICO_FS_DEFAULT_SIZE 1441792 +#endif diff --git a/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice.h b/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice.h new file mode 100644 index 000000000..49542822e --- /dev/null +++ b/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice.h @@ -0,0 +1,7 @@ +/* + * Copyright 2024, Hiroyuki OYAMA. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + */ +#pragma once + +#include "blockdevice/blockdevice.h" diff --git a/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice/flash.h b/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice/flash.h new file mode 100644 index 000000000..47e7436d3 --- /dev/null +++ b/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice/flash.h @@ -0,0 +1,6 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + */ +#pragma once + +#include "blockdevice/flash.h" diff --git a/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice/heap.h b/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice/heap.h new file mode 100644 index 000000000..b75836db6 --- /dev/null +++ b/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice/heap.h @@ -0,0 +1,6 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + */ +#pragma once + +#include "blockdevice/heap.h" diff --git a/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice/sd.h b/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice/sd.h new file mode 100644 index 000000000..13368c5c5 --- /dev/null +++ b/src/rp2_common/pico_filesystem/include/pico/filesystem/blockdevice/sd.h @@ -0,0 +1,6 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + */ +#pragma once + +#include "blockdevice/sd.h" diff --git a/src/rp2_common/pico_filesystem/include/pico/filesystem/filesystem.h b/src/rp2_common/pico_filesystem/include/pico/filesystem/filesystem.h new file mode 100644 index 000000000..ea9779ef8 --- /dev/null +++ b/src/rp2_common/pico_filesystem/include/pico/filesystem/filesystem.h @@ -0,0 +1,7 @@ +/* + * Copyright 2024, Hiroyuki OYAMA. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + */ +#pragma once + +#include "filesystem/filesystem.h" diff --git a/src/rp2_common/pico_filesystem/include/pico/filesystem/filesystem/fat.h b/src/rp2_common/pico_filesystem/include/pico/filesystem/filesystem/fat.h new file mode 100644 index 000000000..1c90f6895 --- /dev/null +++ b/src/rp2_common/pico_filesystem/include/pico/filesystem/filesystem/fat.h @@ -0,0 +1,6 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + */ +#pragma once + +#include "filesystem/fat.h" diff --git a/src/rp2_common/pico_filesystem/include/pico/filesystem/filesystem/littlefs.h b/src/rp2_common/pico_filesystem/include/pico/filesystem/filesystem/littlefs.h new file mode 100644 index 000000000..ceae143a7 --- /dev/null +++ b/src/rp2_common/pico_filesystem/include/pico/filesystem/filesystem/littlefs.h @@ -0,0 +1,6 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + */ +#pragma once + +#include "filesystem/littlefs.h"