diff --git a/.vscode/settings.json b/.vscode/settings.json index 256c5994b..1c8378888 100755 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -174,7 +174,19 @@ "u_env.h": "c", "u_app_loader.h": "c", "crt_arch_init.h": "c", - "kstat.h": "c" + "kstat.h": "c", + "mk_dev.h": "c", + "appfs.h": "c", + "appfs_tiny.h": "c", + "parse_cfg.h": "c", + "system_stm32f2xx.h": "c", + "u_rpc_svr.h": "c", + "blk_drv_svr.h": "c", + "rpc_prot.h": "c", + "flash.h": "c", + "blk_drv_cli.h": "c", + "u_slist.h": "c", + "u_share_mem.h": "c" }, "cortex-debug.showRTOS": false, "cortex-debug.variableUseNaturalFormat": true, diff --git a/Kconfig b/Kconfig index ea6329ed5..a8d9b3199 100644 --- a/Kconfig +++ b/Kconfig @@ -30,9 +30,15 @@ config DTBO_OFFSET hex "dtbo img offset." default 0x1000 -config RTT_DIR - string "" - default "./" +config CPIO_SUPPORT + bool "support cpio img." + default y +config APP_PARAMS_NR + int "app params nr" + default 8 +config APP_ENV_NR + int "app env nr" + default 8 diff --git a/armv7_8.cmake b/armv7_8.cmake index e84d07a87..deeb7de2e 100644 --- a/armv7_8.cmake +++ b/armv7_8.cmake @@ -1,6 +1,6 @@ message("========use armv7_8.cmake") -set(CMAKE_C_FLAGS "-mcpu=${CONFIG_ARCH} -O2 -g3 -mfloat-abi=${CONFIG_FLOAT_TYPE} -mthumb -DMKRTOS \ +set(CMAKE_C_FLAGS "-mcpu=${CONFIG_ARCH} -O0 -g3 -mfloat-abi=${CONFIG_FLOAT_TYPE} -mthumb -DMKRTOS \ -std=gnu11 -ffunction-sections -fdata-sections -fno-builtin -u=_printf_float \ -nostartfiles -nodefaultlibs -nostdlib -nostdinc \ -fno-stack-protector -Wl,--gc-sections -D__ARM_ARCH_7M__ \ diff --git a/mkrtos_bootstrap/bsp/AT32F437/link.lds b/mkrtos_bootstrap/bsp/AT32F437/link.lds index 39abf5f09..e7cb9fe21 100755 --- a/mkrtos_bootstrap/bsp/AT32F437/link.lds +++ b/mkrtos_bootstrap/bsp/AT32F437/link.lds @@ -5,7 +5,7 @@ _Min_Stack_Size = 0x300; MEMORY { RAM (xrw) : ORIGIN = 0x20000000 + 0x60000 - 0x4000, LENGTH = 0x4000 - 0x300 -FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 0x2000 +FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 0x3000 } SECTIONS { diff --git a/mkrtos_configs/ATSURFF437_defconfig b/mkrtos_configs/ATSURFF437_defconfig index ae8db8a19..c21004084 100644 --- a/mkrtos_configs/ATSURFF437_defconfig +++ b/mkrtos_configs/ATSURFF437_defconfig @@ -7,7 +7,7 @@ CONFIG_SYS_TEXT_ADDR=0x8000000 CONFIG_SYS_TEXT_SIZE=0x3F0000 CONFIG_SYS_DATA_ADDR=0x20000000 CONFIG_SYS_DATA_SIZE=0x60000 -CONFIG_BOOTSTRAP_TEXT_SIZE=0x2000 +CONFIG_BOOTSTRAP_TEXT_SIZE=0x3000 CONFIG_DTBO_TEXT_SIZE=0x1000 CONFIG_KNL_TEXT_SIZE=0x30000 @@ -37,79 +37,8 @@ CONFIG_SIG_THREAD_STACK_SIZE=512 CONFIG_SIG_THREAD_PRIO=3 # end of Sys util config -# -# DFS: device virtual file system -# -CONFIG_RT_USING_DFS=y -CONFIG_DFS_USING_POSIX=y -CONFIG_DFS_USING_WORKDIR=y -# CONFIG_RT_USING_DFS_MNTTABLE is not set -CONFIG_DFS_FD_MAX=16 -CONFIG_RT_USING_DFS_V1=y -# CONFIG_RT_USING_DFS_V2 is not set -CONFIG_DFS_FILESYSTEMS_MAX=4 -CONFIG_DFS_FILESYSTEM_TYPES_MAX=4 -# CONFIG_RT_USING_DFS_ELMFAT is not set -CONFIG_RT_USING_DFS_DEVFS=y -# CONFIG_RT_USING_DFS_ROMFS is not set -# CONFIG_RT_USING_DFS_CROMFS is not set -# CONFIG_RT_USING_DFS_RAMFS is not set -# CONFIG_RT_USING_DFS_TMPFS is not set -# CONFIG_RT_USING_DFS_MQUEUE is not set -# end of DFS: device virtual file system - -# -# Device Drivers -# -# CONFIG_RT_USING_DM is not set -CONFIG_RT_USING_DEVICE_IPC=y -CONFIG_RT_UNAMED_PIPE_NUMBER=64 -# CONFIG_RT_USING_SYSTEM_WORKQUEUE is not set -CONFIG_RT_USING_SERIAL=y -CONFIG_RT_USING_SERIAL_V1=y -# CONFIG_RT_USING_SERIAL_V2 is not set -CONFIG_RT_SERIAL_USING_DMA=y -CONFIG_RT_SERIAL_RB_BUFSZ=64 -# CONFIG_RT_USING_CAN is not set -# CONFIG_RT_USING_HWTIMER is not set -# CONFIG_RT_USING_CPUTIME is not set -# CONFIG_RT_USING_I2C is not set -# CONFIG_RT_USING_PHY is not set -CONFIG_RT_USING_PIN=y -# CONFIG_RT_USING_ADC is not set -# CONFIG_RT_USING_DAC is not set -# CONFIG_RT_USING_NULL is not set -# CONFIG_RT_USING_ZERO is not set -# CONFIG_RT_USING_RANDOM is not set -# CONFIG_RT_USING_PWM is not set -# CONFIG_RT_USING_MTD_NOR is not set -# CONFIG_RT_USING_MTD_NAND is not set -# CONFIG_RT_USING_PM is not set -# CONFIG_RT_USING_RTC is not set -# CONFIG_RT_USING_SDIO is not set -# CONFIG_RT_USING_SPI is not set -# CONFIG_RT_USING_WDT is not set -# CONFIG_RT_USING_AUDIO is not set -# CONFIG_RT_USING_SENSOR is not set -# CONFIG_RT_USING_TOUCH is not set -# CONFIG_RT_USING_LCD is not set -# CONFIG_RT_USING_HWCRYPTO is not set -# CONFIG_RT_USING_PULSE_ENCODER is not set -# CONFIG_RT_USING_INPUT_CAPTURE is not set -# CONFIG_RT_USING_DEV_BUS is not set -# CONFIG_RT_USING_WIFI is not set -# CONFIG_RT_USING_VIRTIO is not set - -# -# Using USB -# -# CONFIG_RT_USING_USB_HOST is not set -# CONFIG_RT_USING_USB_DEVICE is not set -# end of Using USB -# end of Device Drivers CONFIG_CPU_TYPE="at32f437" -CONFIG_RTT_DIR="./" CONFIG_ARCH="cortex-m4" CONFIG_FLOAT_TYPE="soft" CONFIG_BOARD_NAME="ATSURFF437" diff --git a/mkrtos_configs/STM32F205_defconfig b/mkrtos_configs/STM32F205_defconfig index f3293f7b1..42ddf5f9f 100644 --- a/mkrtos_configs/STM32F205_defconfig +++ b/mkrtos_configs/STM32F205_defconfig @@ -8,8 +8,8 @@ CONFIG_SYS_TEXT_SIZE=0x800000 CONFIG_SYS_DATA_ADDR=0x20000000 CONFIG_SYS_DATA_SIZE=0x2000000 CONFIG_BOOTSTRAP_TEXT_SIZE=0x2000 -CONFIG_DTBO_TEXT_SIZE=0x1000 -CONFIG_KNL_TEXT_SIZE=0x30000 +CONFIG_DTBO_TEXT_SIZE=0x2000 +CONFIG_KNL_TEXT_SIZE=0x1C000 CONFIG_MK_MPU_CFG=y CONFIG_FT_ADDR_NR=16 @@ -38,7 +38,6 @@ CONFIG_SIG_THREAD_PRIO=3 # end of Sys util config CONFIG_CPU_TYPE="stm32f2" -CONFIG_RTT_DIR="./" CONFIG_ARCH="cortex-m3" CONFIG_BOARD_NAME="STM32F205" CONFIG_BUDDY_SLAB=n @@ -55,8 +54,10 @@ CONFIG_THREAD_MAP_BUF_LEN=4 CONFIG_THREAD_USER_BUF_LEN=4 CONFIG_MSG_BUF_VADDR=0xE0000000 CONFIG_BOOT_FS_VADDR=0xE0001000 -CONFIG_MPU_PAGE_FAULT_SUPPORT=y +# CONFIG_MPU_PAGE_FAULT_SUPPORT=y CONFIG_MPU_PAGE_NUM=64 CONFIG_MPU_PAGE_FAULT_REGIONS_NUM=2 CONFIG_BOOT_INFO_SUPPORT=y -CONFIG_DTBO_SUPPORT=y \ No newline at end of file +CONFIG_DTBO_SUPPORT=y +CONFIG_CPIO_SUPPORT=n +CONFIG_APP_PARAMS_NR=16 \ No newline at end of file diff --git a/mkrtos_doc/appfs.md b/mkrtos_doc/appfs.md new file mode 100644 index 000000000..fe4e3feda --- /dev/null +++ b/mkrtos_doc/appfs.md @@ -0,0 +1,20 @@ + + +# 目标 +- 支持app直接再flash上运行,下载,删除等,直接以文件的方式进行操作。 +- app文件的块必须连续存储。 +# 结构 +info nodeinfo dirinfo block...block + +info: +存储磁盘容量 +nodeinfo入口 +dirinfo入口 +block入口 +nodeinfo: +块信息,每一位代表块是否使用 +dirinfo: +存储目录信息(该文件系统暂时只有一层目录) +block: +可以用来存储文件内容的块 + diff --git a/mkrtos_img/CMakeLists.txt b/mkrtos_img/CMakeLists.txt index 831f21d7a..018eb97c2 100755 --- a/mkrtos_img/CMakeLists.txt +++ b/mkrtos_img/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.13) math(EXPR BOOTFS_OFFSET "${CONFIG_BOOTSTRAP_TEXT_SIZE} + ${CONFIG_KNL_TEXT_SIZE} + ${CONFIG_DTBO_TEXT_SIZE}") math(EXPR KNL_OFFSET "${CONFIG_BOOTSTRAP_TEXT_SIZE} + ${CONFIG_DTBO_TEXT_SIZE}") - +math(EXPR APPFS_IMG_SIZE "${CONFIG_SYS_TEXT_SIZE} - ${BOOTFS_OFFSET}") add_custom_target( mkrtos_img_dump ALL COMMAND @@ -12,18 +12,22 @@ add_custom_target( COMMAND ls | cpio -H newc -o > ${CMAKE_SOURCE_DIR}/build/output/rootfs.cpio COMMAND + ${CMAKE_SOURCE_DIR}/mkrtos_user/server/fs/appfs/src/build/appfs -g ${CMAKE_SOURCE_DIR}/build/output/cpio -o ../appfs.img -s ${APPFS_IMG_SIZE} + COMMAND srec_cat -output ${CMAKE_SOURCE_DIR}/build/output/kernel.img -binary ${CMAKE_SOURCE_DIR}/build/output/bootstrap -binary -offset 0x0 ${CMAKE_SOURCE_DIR}/build/output/dtbo -binary -offset ${CONFIG_BOOTSTRAP_TEXT_SIZE} ${CMAKE_SOURCE_DIR}/build/output/mkrtos -binary -offset ${KNL_OFFSET} - ${CMAKE_SOURCE_DIR}/build/output/rootfs.cpio -binary -offset ${BOOTFS_OFFSET} + ${CMAKE_SOURCE_DIR}/build/output/appfs.img -binary -offset ${BOOTFS_OFFSET} COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/build/output/kernel.img ${CMAKE_SOURCE_DIR}/build/output/kernel.bin COMMAND ${CMAKE_OBJCOPY} -I binary -O elf32-littlearm -B arm ${CMAKE_SOURCE_DIR}/build/output/kernel.img ${CMAKE_SOURCE_DIR}/build/output/kernel.img.out --rename-section .data=.text COMMAND - ${CMAKE_OBJCOPY} --change-section-address .text=${CONFIG_SYS_TEXT_ADDR} ${CMAKE_SOURCE_DIR}/build/output/kernel.img.out + ${CMAKE_OBJCOPY} --change-section-address .text=${CONFIG_SYS_TEXT_ADDR} ${CMAKE_SOURCE_DIR}/build/output/kernel.img.out + + #以下命令用于生成binutils链接需要的库 COMMAND cd ../../ COMMAND diff --git a/mkrtos_knl/CMakeLists.txt b/mkrtos_knl/CMakeLists.txt index a125f9bc1..edee41753 100755 --- a/mkrtos_knl/CMakeLists.txt +++ b/mkrtos_knl/CMakeLists.txt @@ -1,7 +1,6 @@ cmake_minimum_required(VERSION 3.13) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} \ --O3 \ -Werror \ -Wno-unused-parameter \ -Wno-unused-function \ diff --git a/mkrtos_knl/arch/cortex-m3/link.lds b/mkrtos_knl/arch/cortex-m3/link.lds index 4a905ad02..b63054b88 100644 --- a/mkrtos_knl/arch/cortex-m3/link.lds +++ b/mkrtos_knl/arch/cortex-m3/link.lds @@ -2,7 +2,7 @@ ENTRY(Reset_Handler) MEMORY { RAM (arw) : ORIGIN = 0x20000000, LENGTH = 0x2000000 - FLASH (arx) : ORIGIN = 0x8000000 + 0x2000 + 0x1000, LENGTH = 0x30000 + FLASH (arx) : ORIGIN = 0x8000000 + 0x2000 + 0x2000, LENGTH = 0x1C000 } SECTIONS { diff --git a/mkrtos_knl/arch/cortex-m4/link.lds b/mkrtos_knl/arch/cortex-m4/link.lds index 97cb2e95a..28861c727 100644 --- a/mkrtos_knl/arch/cortex-m4/link.lds +++ b/mkrtos_knl/arch/cortex-m4/link.lds @@ -2,7 +2,7 @@ ENTRY(Reset_Handler) MEMORY { RAM (arw) : ORIGIN = 0x20000000, LENGTH = 0x60000 - FLASH (arx) : ORIGIN = 0x8000000 + 0x2000 + 0x1000, LENGTH = 0x30000 + FLASH (arx) : ORIGIN = 0x8000000 + 0x3000 + 0x1000, LENGTH = 0x30000 } SECTIONS { diff --git a/mkrtos_knl/inc/lib/appfs_tiny.h b/mkrtos_knl/inc/lib/appfs_tiny.h new file mode 100644 index 000000000..3999f0cd2 --- /dev/null +++ b/mkrtos_knl/inc/lib/appfs_tiny.h @@ -0,0 +1,49 @@ +#pragma once + +#include "types.h" +struct hw_callback; +typedef struct hw_callback hw_callback_t; +struct fs_info; +typedef struct fs_info fs_info_t; +#define APPFS_MAGIC "APPFS" +#define APPFS_VERSION 1 + +typedef struct fs_info_save +{ + char magic[8]; //!< APPFS + int version; //!< 版本号 + int blockinfo_inx; //!< 块信息起始块号 + int blockinfo_nr; //!< 块信息块数量 + + int dirinfo_inx; //!< 目录信息起始块号 + int dirinfo_nr; //!< 目录信息块数量 + + int block_inx; //!< 块起始块号 + int block_nr; //!< 块数量 + + int block_size; //!< 块大小 +} fs_info_save_t; + +typedef struct fs_info +{ + fs_info_save_t save; //!< 保存的文件系统信息 +} fs_info_t; + +#define APPFS_FILE_NAME_MAX 52 //!< 文件名最大长度 + +typedef struct dir_info +{ + char name[APPFS_FILE_NAME_MAX]; //!< 文件名 + int block_inx; //!< 文件起始块号 + int size; //!< 文件大小 + int ref; //!< 引用计数 +} dir_info_t; +static inline unsigned long appfs_get_file_addr(fs_info_t *fs, const dir_info_t *info) +{ + assert(fs); + return info->block_inx * fs->save.block_size + (unsigned long)fs; //!< 计算文件地址 +} + +fs_info_t *appfs_get_form_addr(void *addr); +const dir_info_t *appfs_find_file_by_name(fs_info_t *info, const char *name); +umword_t appfs_find_file_addr_by_name(fs_info_t *info, const char *name, size_t *size); diff --git a/mkrtos_knl/inc/lib/cpio.h b/mkrtos_knl/inc/lib/cpio.h index 6e603b4ad..fc991c5fa 100644 --- a/mkrtos_knl/inc/lib/cpio.h +++ b/mkrtos_knl/inc/lib/cpio.h @@ -1,5 +1,8 @@ #pragma once #include +#include +#if IS_ENABLED(CONFIG_CPIO_SUPPORT) + //! 26bytes #pragma pack(1) typedef struct cpio_fs @@ -32,3 +35,4 @@ static inline int64_t check_magic(char *magic) umword_t cpio_get_size(umword_t st); umword_t cpio_find_file(umword_t st, umword_t en, const char *name, size_t *size); +#endif diff --git a/mkrtos_knl/knl/thread_knl.c b/mkrtos_knl/knl/thread_knl.c index a67795689..bebbad873 100755 --- a/mkrtos_knl/knl/thread_knl.c +++ b/mkrtos_knl/knl/thread_knl.c @@ -32,7 +32,11 @@ #if IS_ENABLED(CONFIG_MMU) #include #endif +#if IS_ENABLED(CONFIG_CPIO_SUPPORT) #include +#else +#include +#endif #include static uint8_t knl_msg_buf[CONFIG_CPU][THREAD_MSG_BUG_LEN]; @@ -145,8 +149,11 @@ static void knl_init_2(void) #if IS_ENABLED(CONFIG_ELF_LAUNCH) addr_t entry; - +#if IS_ENABLED(CONFIG_CPIO_SUPPORT) ret_addr = cpio_find_file(cpio_images, (umword_t)(-1), "init", &size); +#else + ret_addr = appfs_find_file_addr_by_name(appfs_get_form_addr((void *)(cpio_images)), "init", &size); +#endif assert(ret_addr); elf_load(init_task, ret_addr, size, &entry); void *init_msg_buf = mm_buddy_alloc_one_page(); @@ -163,7 +170,11 @@ static void knl_init_2(void) #else app_info_t *app; +#if IS_ENABLED(CONFIG_CPIO_SUPPORT) ret_addr = cpio_find_file(arch_get_boot_info()->flash_layer.flash_layer_list[BOOTFS_LAYER_3].st_addr, (umword_t)(-1), "init", &size); +#else + ret_addr = appfs_find_file_addr_by_name(appfs_get_form_addr((void *)(arch_get_boot_info()->flash_layer.flash_layer_list[BOOTFS_LAYER_3].st_addr)), "init", &size); +#endif assert(ret_addr); app = app_info_get((void *)(ret_addr)); assert(app); diff --git a/mkrtos_knl/lib/appfs_tiny.c b/mkrtos_knl/lib/appfs_tiny.c new file mode 100644 index 000000000..059f96845 --- /dev/null +++ b/mkrtos_knl/lib/appfs_tiny.c @@ -0,0 +1,71 @@ + +#include +#include +#include "appfs_tiny.h" +/** + * 通过文件名查找文件 + */ +const dir_info_t *appfs_find_file_by_name(fs_info_t *info, const char *name) +{ + assert(info); + assert(name); + + int ret = 0; + + for (int i = 0; i < info->save.dirinfo_nr; i++) + { + // 读取目录信息 + if (info->save.dirinfo_inx + i >= info->save.block_nr) + { + return NULL; + } + const void *buf = (void *)((info->save.dirinfo_inx + i) * info->save.block_size + (unsigned long)info); + + if (ret < 0) + { + return NULL; + } + // 遍历目录信息 + for (int j = 0; j < info->save.block_size / sizeof(dir_info_t); j++) + { + const dir_info_t *dir_info = &((dir_info_t *)buf)[j]; + if (strcmp(dir_info->name, name) == 0) + { + return (const dir_info_t *)((char *)info + (info->save.dirinfo_inx + i) * info->save.block_size + j * sizeof(dir_info_t)); + } + } + } + + return NULL; +} +fs_info_t *appfs_get_form_addr(void *addr) +{ + assert(addr); + fs_info_t *fs = addr; + const char *magic_str = APPFS_MAGIC; + + for (int i = 0; magic_str[i]; i++) + { + if (fs->save.magic[0] != magic_str[0]) + { + return NULL; + } + } + return fs; +} +umword_t appfs_find_file_addr_by_name(fs_info_t *info, const char *name, size_t *size) +{ + const dir_info_t *dir_info; + + dir_info = appfs_find_file_by_name(info, name); + + if (dir_info == NULL) + { + return 0; + } + if (size) + { + *size = dir_info->size; + } + return (umword_t)appfs_get_file_addr(info, dir_info); +} \ No newline at end of file diff --git a/mkrtos_knl/lib/cpio.c b/mkrtos_knl/lib/cpio.c index 31c7482f9..9a680ab19 100644 --- a/mkrtos_knl/lib/cpio.c +++ b/mkrtos_knl/lib/cpio.c @@ -1,10 +1,9 @@ -// #include "mk_sys.h" -#include "cpio.h" #include +#if IS_ENABLED(CONFIG_CPIO_SUPPORT) +#include "cpio.h" #include #include -// #define MIN(a, b) ((a) < (b) ? (a) : (b)) //!< 最小值 -// #define ALIGN(mem, align) (((mem) + ((align) - 1)) & (~((align) - 1))) //!< 向上对齐 + static int htoi(char *str, int len); umword_t cpio_get_size(umword_t st) @@ -117,3 +116,4 @@ static int htoi(char *str, int len) } return n; } +#endif \ No newline at end of file diff --git a/mkrtos_script/build_at32f437.sh b/mkrtos_script/build_at32f437.sh index 0ea5276f4..8a987bb58 100755 --- a/mkrtos_script/build_at32f437.sh +++ b/mkrtos_script/build_at32f437.sh @@ -1,12 +1,15 @@ #!/bin/bash +export TOOLCHAIN=/home/zhangzheng/toolchain/gcc-arm-10.3-2021.07-x86_64-arm-none-eabi/bin/ +export TOOLCHAIN_LIB=/home/zhangzheng/toolchain/gcc-arm-10.3-2021.07-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/10.3.1/thumb/v7-m/nofp + # linux build # export TOOLCHAIN=/home/zhangzheng/gcc-arm-none-eabi-5_4-2016q3/bin/ # export TOOLCHAIN_LIB=/home/zhangzheng/gcc-arm-none-eabi-5_4-2016q3/lib/gcc/arm-none-eabi/5.4.1/armv7e-m/fpu # mac compile -export TOOLCHAIN=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/bin/ -export TOOLCHAIN_LIB=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/lib/gcc/arm-none-eabi/10.3.1/thumb/v7e-m+fp/softfp +# export TOOLCHAIN=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/bin/ +# export TOOLCHAIN_LIB=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/lib/gcc/arm-none-eabi/10.3.1/thumb/v7e-m+fp/softfp # windows compile # export TOOLCHAIN=/d/GNUArmEmbeddedToolchain/102021.10/bin/ diff --git a/mkrtos_user/lib/sys_svr/inc/blk_drv_cli.h b/mkrtos_user/lib/sys_svr/inc/blk_drv_cli.h new file mode 100644 index 000000000..14dcf1749 --- /dev/null +++ b/mkrtos_user/lib/sys_svr/inc/blk_drv_cli.h @@ -0,0 +1,8 @@ +#pragma once +#include "u_types.h" +#include "u_rpc_svr.h" +#include "u_slist.h" +#include "blk_drv_types.h" +int blk_drv_cli_write(obj_handler_t dm9000_obj, obj_handler_t shm_obj, int len, int inx); +int blk_drv_cli_read(obj_handler_t dm9000_obj, obj_handler_t shm_obj, int len, int inx); +int blk_drv_cli_map(obj_handler_t svr_boj, obj_handler_t *sem_obj); diff --git a/mkrtos_user/lib/sys_svr/inc/blk_drv_svr.h b/mkrtos_user/lib/sys_svr/inc/blk_drv_svr.h new file mode 100644 index 000000000..335446635 --- /dev/null +++ b/mkrtos_user/lib/sys_svr/inc/blk_drv_svr.h @@ -0,0 +1,9 @@ +#pragma once +#include "u_types.h" +#include "u_rpc_svr.h" +#include "u_slist.h" +#include "blk_drv_types.h" +int blk_drv_write(obj_handler_t obj, int len, int inx); +int blk_drv_read(obj_handler_t obj, int len, int inx); +int blk_drv_map(obj_handler_t *hd); +void blk_drv_init(blk_drv_t *ns); diff --git a/mkrtos_user/lib/sys_svr/inc/net_drv_types.h b/mkrtos_user/lib/sys_svr/inc/blk_drv_types.h similarity index 74% rename from mkrtos_user/lib/sys_svr/inc/net_drv_types.h rename to mkrtos_user/lib/sys_svr/inc/blk_drv_types.h index 5da189278..9a4090ae8 100644 --- a/mkrtos_user/lib/sys_svr/inc/net_drv_types.h +++ b/mkrtos_user/lib/sys_svr/inc/blk_drv_types.h @@ -3,7 +3,7 @@ #include "u_rpc_svr.h" #include "u_slist.h" -typedef struct net_drv +typedef struct blk_drv { rpc_svr_obj_t svr; -} net_drv_t; +} blk_drv_t; diff --git a/mkrtos_user/lib/sys_svr/inc/net_drv_cli.h b/mkrtos_user/lib/sys_svr/inc/net_drv_cli.h deleted file mode 100644 index c071f0974..000000000 --- a/mkrtos_user/lib/sys_svr/inc/net_drv_cli.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include "u_types.h" -#include "u_rpc_svr.h" -#include "u_slist.h" - -int net_drv_cli_write(obj_handler_t dm9000_obj, obj_handler_t shm_obj, int len); -int net_drv_cli_read(obj_handler_t dm9000_obj, obj_handler_t shm_obj); -int net_drv_cli_map(obj_handler_t dm9000_obj, obj_handler_t *sem_obj); diff --git a/mkrtos_user/lib/sys_svr/inc/net_drv_svr.h b/mkrtos_user/lib/sys_svr/inc/net_drv_svr.h deleted file mode 100644 index 32559e9a3..000000000 --- a/mkrtos_user/lib/sys_svr/inc/net_drv_svr.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "u_types.h" -#include "u_rpc_svr.h" -#include "u_slist.h" -#include "net_drv_types.h" -int net_drv_write(obj_handler_t obj, int len); -int net_drv_read(obj_handler_t obj, int len); -int net_drv_map(obj_handler_t *hd); -void net_drv_init(net_drv_t *ns); diff --git a/mkrtos_user/lib/sys_svr/inc/rpc_prot.h b/mkrtos_user/lib/sys_svr/inc/rpc_prot.h index 9be0b5a57..a68aa1300 100644 --- a/mkrtos_user/lib/sys_svr/inc/rpc_prot.h +++ b/mkrtos_user/lib/sys_svr/inc/rpc_prot.h @@ -49,12 +49,7 @@ #define PM_SIG_PROT 0x0007 //!< pm信号协议 #define PM_SIG_NOTIFY ((umword_t)0) //!< 通知消息 -#define NET_DRV_PROT 0x0008 -#define NET_DRV_WRITE ((umword_t)0) //!< 网络驱动写 -#define NET_DRV_READ ((umword_t)1) //!< 网络驱动读 -#define NET_DRV_MAP ((umword_t)2) //!< net map - -#define SND_DRV_PROT 0x0009 -#define SND_DRV_WRITE ((umword_t)0) //!< snd驱动写 -#define SND_DRV_READ ((umword_t)1) //!< snd驱动读 -#define SND_DRV_MAP ((umword_t)2) //!< snd map +#define BLK_DRV_PROT 0x0008 +#define BLK_DRV_WRITE ((umword_t)0) //!< 块驱动写 +#define BLK_DRV_READ ((umword_t)1) //!< 块驱动读 +#define BLK_DRV_MAP ((umword_t)2) //!< 块驱动对象映射 diff --git a/mkrtos_user/lib/sys_svr/inc/snd_drv_cli.h b/mkrtos_user/lib/sys_svr/inc/snd_drv_cli.h deleted file mode 100644 index 90186bbbb..000000000 --- a/mkrtos_user/lib/sys_svr/inc/snd_drv_cli.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include "u_types.h" -#include "u_rpc_svr.h" -#include "u_slist.h" - -int snd_drv_cli_write(obj_handler_t dm9000_obj, obj_handler_t shm_obj, int len); -int snd_drv_cli_read(obj_handler_t dm9000_obj, obj_handler_t shm_obj); -int snd_drv_cli_map(obj_handler_t dm9000_obj, obj_handler_t *sem_obj); diff --git a/mkrtos_user/lib/sys_svr/inc/snd_drv_svr.h b/mkrtos_user/lib/sys_svr/inc/snd_drv_svr.h deleted file mode 100644 index 655226b3d..000000000 --- a/mkrtos_user/lib/sys_svr/inc/snd_drv_svr.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "u_types.h" -#include "u_rpc_svr.h" -#include "u_slist.h" -#include "snd_drv_types.h" -int snd_drv_write(obj_handler_t obj, int len); -int snd_drv_read(obj_handler_t obj, int len); -int snd_drv_map(obj_handler_t *hd); -void snd_drv_init(snd_drv_t *ns); diff --git a/mkrtos_user/lib/sys_svr/inc/snd_drv_types.h b/mkrtos_user/lib/sys_svr/inc/snd_drv_types.h deleted file mode 100644 index 50cda719c..000000000 --- a/mkrtos_user/lib/sys_svr/inc/snd_drv_types.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "u_types.h" -#include "u_rpc_svr.h" -#include "u_slist.h" - -typedef struct snd_drv -{ - rpc_svr_obj_t svr; -} snd_drv_t; diff --git a/mkrtos_user/lib/sys_svr/src/net_drv_cli.c b/mkrtos_user/lib/sys_svr/src/blk_drv_cli.c similarity index 54% rename from mkrtos_user/lib/sys_svr/src/net_drv_cli.c rename to mkrtos_user/lib/sys_svr/src/blk_drv_cli.c index ea9945e87..16b4f10d1 100644 --- a/mkrtos_user/lib/sys_svr/src/net_drv_cli.c +++ b/mkrtos_user/lib/sys_svr/src/blk_drv_cli.c @@ -1,20 +1,20 @@ +#include "blk_drv_cli.h" +#include "blk_drv_types.h" #include "rpc_prot.h" -#include "net_drv_cli.h" -#include "net_drv_types.h" -#include "u_rpc.h" -#include "u_rpc_svr.h" #include "u_arch.h" -#include "u_rpc_buf.h" #include "u_hd_man.h" +#include "u_rpc.h" #include "u_rpc_buf.h" -#include -#include +#include "u_rpc_svr.h" #include +#include +#include -RPC_GENERATION_CALL2(TRUE, net_drv_t, NET_DRV_PROT, NET_DRV_WRITE, write, +RPC_GENERATION_CALL3(TRUE, blk_drv_t, BLK_DRV_PROT, BLK_DRV_WRITE, write, rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) -int net_drv_cli_write(obj_handler_t dm9000_obj, obj_handler_t shm_obj, int len) + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, inx) +int blk_drv_cli_write(obj_handler_t dm9000_obj, obj_handler_t shm_obj, int len, int inx) { rpc_obj_handler_t_t rpc_hd = { .data = shm_obj, @@ -22,49 +22,53 @@ int net_drv_cli_write(obj_handler_t dm9000_obj, obj_handler_t shm_obj, int len) rpc_int_t rpc_len = { .data = len, }; - - msg_tag_t tag = net_drv_t_write_call(dm9000_obj, &rpc_hd, &rpc_len); + rpc_int_t rpc_inx = { + .data = inx, + }; + msg_tag_t tag = blk_drv_t_write_call(dm9000_obj, &rpc_hd, &rpc_len, &rpc_inx); return msg_tag_get_val(tag); } -RPC_GENERATION_CALL2(TRUE, net_drv_t, NET_DRV_PROT, NET_DRV_READ, read, +RPC_GENERATION_CALL3(TRUE, blk_drv_t, BLK_DRV_PROT, BLK_DRV_READ, read, rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, inx) -int net_drv_cli_read(obj_handler_t dm9000_obj, obj_handler_t shm_obj) +int blk_drv_cli_read(obj_handler_t dm9000_obj, obj_handler_t shm_obj, int len, int inx) { rpc_obj_handler_t_t rpc_hd = { .data = shm_obj, }; rpc_int_t rpc_len = { - .data = 0, + .data = len, }; - msg_tag_t tag = net_drv_t_read_call(dm9000_obj, &rpc_hd, &rpc_len); + rpc_int_t rpc_inx = { + .data = inx, + }; + msg_tag_t tag = blk_drv_t_read_call(dm9000_obj, &rpc_hd, &rpc_len, &rpc_inx); return msg_tag_get_val(tag); } -RPC_GENERATION_CALL1(TRUE, net_drv_t, NET_DRV_PROT, NET_DRV_MAP, map, +RPC_GENERATION_CALL1(TRUE, blk_drv_t, BLK_DRV_PROT, BLK_DRV_MAP, map, rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_INOUT, RPC_TYPE_BUF, cli_hd) -int net_drv_cli_map(obj_handler_t dm9000_obj, obj_handler_t *sem_obj) +int blk_drv_cli_map(obj_handler_t dm9000_obj, obj_handler_t *sem_obj) { assert(sem_obj); obj_handler_t newfd; newfd = handler_alloc(); - if (newfd == HANDLER_INVALID) - { + if (newfd == HANDLER_INVALID) { return -ENOENT; } rpc_obj_handler_t_t rpc_sem_obj = { .data = newfd, .del_map_flags = VPAGE_FLAGS_MAP, }; - msg_tag_t tag = net_drv_t_map_call(dm9000_obj, &rpc_sem_obj); + msg_tag_t tag = blk_drv_t_map_call(dm9000_obj, &rpc_sem_obj); - if (msg_tag_get_val(tag) < 0) - { + if (msg_tag_get_val(tag) < 0) { handler_free(newfd); return msg_tag_get_val(tag); } diff --git a/mkrtos_user/lib/sys_svr/src/blk_drv_svr.c b/mkrtos_user/lib/sys_svr/src/blk_drv_svr.c new file mode 100644 index 000000000..e4934c11e --- /dev/null +++ b/mkrtos_user/lib/sys_svr/src/blk_drv_svr.c @@ -0,0 +1,57 @@ +#include "rpc_prot.h" +#include "blk_drv_svr.h" +#include "blk_drv_types.h" +#include "u_rpc.h" +#include "u_rpc_svr.h" +#include "u_arch.h" +#include "u_rpc_buf.h" +#include "u_hd_man.h" +#include "u_rpc_buf.h" +#include + +RPC_GENERATION_OP3(blk_drv_t, BLK_DRV_PROT, BLK_DRV_WRITE, write, + rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, inx + ) +{ + return blk_drv_write(rpc_hd_get(0), len->data, inx->data); +} +RPC_GENERATION_DISPATCH3(blk_drv_t, BLK_DRV_PROT, BLK_DRV_WRITE, write, + rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, svr_hd, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, type, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, inx) + +RPC_GENERATION_OP3(blk_drv_t, BLK_DRV_PROT, BLK_DRV_READ, read, + rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, inx + ) +{ + return blk_drv_read(rpc_hd_get(0), len->data, inx->data); +} + +RPC_GENERATION_DISPATCH3(blk_drv_t, BLK_DRV_PROT, BLK_DRV_READ, read, + rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, inx) + +RPC_GENERATION_OP1(blk_drv_t, BLK_DRV_PROT, BLK_DRV_MAP, map, + rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_INOUT, RPC_TYPE_BUF, cli_hd) +{ + int ret = blk_drv_map(&cli_hd->data); + if (ret < 0) + { + printf("blk drv aailed to request map"); + } + return ret; +} +RPC_GENERATION_DISPATCH1(blk_drv_t, BLK_DRV_PROT, BLK_DRV_MAP, map, + rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_INOUT, RPC_TYPE_BUF, cli_hd) + +RPC_DISPATCH3(blk_drv_t, BLK_DRV_PROT, typeof(BLK_DRV_READ), BLK_DRV_WRITE, write, BLK_DRV_READ, read, BLK_DRV_MAP, map) + +void blk_drv_init(blk_drv_t *ns) +{ + rpc_svr_obj_init(&ns->svr, rpc_blk_drv_t_dispatch, BLK_DRV_PROT); +} diff --git a/mkrtos_user/lib/sys_svr/src/net_drv_svr.c b/mkrtos_user/lib/sys_svr/src/net_drv_svr.c deleted file mode 100644 index 3a5bba8e3..000000000 --- a/mkrtos_user/lib/sys_svr/src/net_drv_svr.c +++ /dev/null @@ -1,51 +0,0 @@ -#include "rpc_prot.h" -#include "net_drv_svr.h" -#include "net_drv_types.h" -#include "u_rpc.h" -#include "u_rpc_svr.h" -#include "u_arch.h" -#include "u_rpc_buf.h" -#include "u_hd_man.h" -#include "u_rpc_buf.h" -#include - -RPC_GENERATION_OP2(net_drv_t, NET_DRV_PROT, NET_DRV_WRITE, write, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) -{ - return net_drv_write(rpc_hd_get(0), len->data); -} -RPC_GENERATION_DISPATCH2(net_drv_t, NET_DRV_PROT, NET_DRV_WRITE, write, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, svr_hd, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, type) - -RPC_GENERATION_OP2(net_drv_t, NET_DRV_PROT, NET_DRV_READ, read, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) -{ - return net_drv_read(rpc_hd_get(0), len->data); -} - -RPC_GENERATION_DISPATCH2(net_drv_t, NET_DRV_PROT, NET_DRV_READ, read, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) - -RPC_GENERATION_OP1(net_drv_t, NET_DRV_PROT, NET_DRV_MAP, map, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_INOUT, RPC_TYPE_BUF, cli_hd) -{ - int ret = net_drv_map(&cli_hd->data); - if (ret < 0) - { - printf("net drv aailed to request map"); - } - return ret; -} -RPC_GENERATION_DISPATCH1(net_drv_t, NET_DRV_PROT, NET_DRV_MAP, map, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_INOUT, RPC_TYPE_BUF, cli_hd) - -RPC_DISPATCH3(net_drv_t, NET_DRV_PROT, typeof(NET_DRV_READ), NET_DRV_WRITE, write, NET_DRV_READ, read, NET_DRV_MAP, map) - -void net_drv_init(net_drv_t *ns) -{ - rpc_svr_obj_init(&ns->svr, rpc_net_drv_t_dispatch, NET_DRV_PROT); -} diff --git a/mkrtos_user/lib/sys_svr/src/snd_drv_cli.c b/mkrtos_user/lib/sys_svr/src/snd_drv_cli.c deleted file mode 100644 index 4b31745f2..000000000 --- a/mkrtos_user/lib/sys_svr/src/snd_drv_cli.c +++ /dev/null @@ -1,73 +0,0 @@ -#include "rpc_prot.h" -#include "snd_drv_cli.h" -#include "snd_drv_types.h" -#include "u_rpc.h" -#include "u_rpc_svr.h" -#include "u_arch.h" -#include "u_rpc_buf.h" -#include "u_hd_man.h" -#include "u_rpc_buf.h" -#include -#include -#include - -RPC_GENERATION_CALL2(TRUE, snd_drv_t, SND_DRV_PROT, SND_DRV_WRITE, write, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) -int snd_drv_cli_write(obj_handler_t dm9000_obj, obj_handler_t shm_obj, int len) -{ - rpc_obj_handler_t_t rpc_hd = { - .data = shm_obj, - }; - rpc_int_t rpc_len = { - .data = len, - }; - - msg_tag_t tag = snd_drv_t_write_call(dm9000_obj, &rpc_hd, &rpc_len); - - return msg_tag_get_val(tag); -} -RPC_GENERATION_CALL2(TRUE, snd_drv_t, SND_DRV_PROT, SND_DRV_READ, read, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) - -int snd_drv_cli_read(obj_handler_t dm9000_obj, obj_handler_t shm_obj) -{ - rpc_obj_handler_t_t rpc_hd = { - .data = shm_obj, - }; - rpc_int_t rpc_len = { - .data = 0, - }; - msg_tag_t tag = snd_drv_t_read_call(dm9000_obj, &rpc_hd, &rpc_len); - - return msg_tag_get_val(tag); -} -RPC_GENERATION_CALL1(TRUE, snd_drv_t, SND_DRV_PROT, SND_DRV_MAP, map, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_INOUT, RPC_TYPE_BUF, cli_hd) - -int snd_drv_cli_map(obj_handler_t dm9000_obj, obj_handler_t *sem_obj) -{ - assert(sem_obj); - - obj_handler_t newfd; - - newfd = handler_alloc(); - if (newfd == HANDLER_INVALID) - { - return -ENOENT; - } - rpc_obj_handler_t_t rpc_sem_obj = { - .data = newfd, - .del_map_flags = VPAGE_FLAGS_MAP, - }; - msg_tag_t tag = snd_drv_t_map_call(dm9000_obj, &rpc_sem_obj); - - if (msg_tag_get_val(tag) < 0) - { - handler_free(newfd); - return msg_tag_get_val(tag); - } - *sem_obj = newfd; - return msg_tag_get_val(tag); -} diff --git a/mkrtos_user/lib/sys_svr/src/snd_drv_svr.c b/mkrtos_user/lib/sys_svr/src/snd_drv_svr.c deleted file mode 100644 index 0cc523676..000000000 --- a/mkrtos_user/lib/sys_svr/src/snd_drv_svr.c +++ /dev/null @@ -1,51 +0,0 @@ -#include "rpc_prot.h" -#include "snd_drv_svr.h" -#include "snd_drv_types.h" -#include "u_rpc.h" -#include "u_rpc_svr.h" -#include "u_arch.h" -#include "u_rpc_buf.h" -#include "u_hd_man.h" -#include "u_rpc_buf.h" -#include - -RPC_GENERATION_OP2(snd_drv_t, SND_DRV_PROT, SND_DRV_WRITE, write, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) -{ - return snd_drv_write(rpc_hd_get(0), len->data); -} -RPC_GENERATION_DISPATCH2(snd_drv_t, SND_DRV_PROT, SND_DRV_WRITE, write, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, svr_hd, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, type) - -RPC_GENERATION_OP2(snd_drv_t, SND_DRV_PROT, SND_DRV_READ, read, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) -{ - return snd_drv_read(rpc_hd_get(0), len->data); -} - -RPC_GENERATION_DISPATCH2(snd_drv_t, SND_DRV_PROT, SND_DRV_READ, read, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, shm_obj, - rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) - -RPC_GENERATION_OP1(snd_drv_t, SND_DRV_PROT, SND_DRV_MAP, map, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_INOUT, RPC_TYPE_BUF, cli_hd) -{ - int ret = snd_drv_map(&cli_hd->data); - if (ret < 0) - { - printf("snd drv aailed to request map"); - } - return ret; -} -RPC_GENERATION_DISPATCH1(snd_drv_t, SND_DRV_PROT, SND_DRV_MAP, map, - rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_INOUT, RPC_TYPE_BUF, cli_hd) - -RPC_DISPATCH3(snd_drv_t, SND_DRV_PROT, typeof(SND_DRV_READ), SND_DRV_WRITE, write, SND_DRV_READ, read, SND_DRV_MAP, map) - -void snd_drv_init(snd_drv_t *ns) -{ - rpc_svr_obj_init(&ns->svr, rpc_snd_drv_t_dispatch, SND_DRV_PROT); -} diff --git a/mkrtos_user/lib/sys_util/CMakeLists.txt b/mkrtos_user/lib/sys_util/CMakeLists.txt index 125f2947e..59f7e67e4 100644 --- a/mkrtos_user/lib/sys_util/CMakeLists.txt +++ b/mkrtos_user/lib/sys_util/CMakeLists.txt @@ -30,10 +30,11 @@ target_link_libraries( ${LIBC_NAME} cpio util + appfs_lib ) set_target_properties(sys_util PROPERTIES LINK_FLAGS ${CORTEX_M_LINK_FLAGS}) add_dependencies(sys_util sys) # add_dependencies(sys_util ${LIBC_NAME}) -add_dependencies(sys_util util) +add_dependencies(sys_util util appfs_lib) diff --git a/mkrtos_user/lib/sys_util/src/fast_ipc_setsp.S b/mkrtos_user/lib/sys_util/src/fast_ipc_setsp.S index 74ac13983..a6817f56b 100644 --- a/mkrtos_user/lib/sys_util/src/fast_ipc_setsp.S +++ b/mkrtos_user/lib/sys_util/src/fast_ipc_setsp.S @@ -2,7 +2,7 @@ .thumb .global fast_ipc_setsp /** -* @brief syscall结束时到这儿 +* @brief fast_ip_setsp */ .type fast_ipc_setsp, %function fast_ipc_setsp: diff --git a/mkrtos_user/lib/sys_util/src/u_app_loader.c b/mkrtos_user/lib/sys_util/src/u_app_loader.c index b5520b42a..4105c8777 100644 --- a/mkrtos_user/lib/sys_util/src/u_app_loader.c +++ b/mkrtos_user/lib/sys_util/src/u_app_loader.c @@ -26,6 +26,10 @@ #include #include #include + +#if !IS_ENABLED(CONFIG_CPIO_SUPPORT) +#include +#endif /** * @brief 向栈中存放数据 * @@ -132,8 +136,18 @@ int app_load(const char *name, uenv_t *cur_env, pid_t *pid, // } int type; umword_t addr; - int ret = cpio_find_file((umword_t)sys_info.bootfs_start_addr, (umword_t)(-1), name, NULL, &type, &addr); + int ret; +#if IS_ENABLED(CONFIG_CPIO_SUPPORT) + ret = cpio_find_file((umword_t)sys_info.bootfs_start_addr, (umword_t)(-1), name, NULL, &type, &addr); +#else + type = 0; + addr = (umword_t)appfs_tiny_find_file_addr_by_name(appfs_tiny_get_form_addr((void *)sys_info.bootfs_start_addr), name, NULL); + if (addr == 0) + { + ret = -ENOENT; + } +#endif if (ret < 0 || (ret >= 0 && type == 1)) { return -ENOENT; @@ -259,8 +273,8 @@ int app_load(const char *name, uenv_t *cur_env, pid_t *pid, .rev2 = HANDLER_INVALID, }; umword_t *app_env; - char *cp_args[8/*FIXME:*/]; - char *cp_envp[8]; + char *cp_args[CONFIG_APP_PARAMS_NR]; + char *cp_envp[CONFIG_APP_ENV_NR]; size_t params_envp_len = 0; app_env = app_stack_push_array(hd_task, &usp_top, (uint8_t *)(&uenv), sizeof(uenv)); diff --git a/mkrtos_user/server/fs/CMakeLists.txt b/mkrtos_user/server/fs/CMakeLists.txt index 577266385..eb3540516 100644 --- a/mkrtos_user/server/fs/CMakeLists.txt +++ b/mkrtos_user/server/fs/CMakeLists.txt @@ -3,4 +3,5 @@ cmake_minimum_required(VERSION 3.13) add_subdirectory(cpiofs) add_subdirectory(fatfs) +add_subdirectory(appfs) # add_subdirectory(lxext4) diff --git a/mkrtos_user/server/fs/appfs/CMakeLists.txt b/mkrtos_user/server/fs/appfs/CMakeLists.txt new file mode 100644 index 000000000..693ba2ed1 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/CMakeLists.txt @@ -0,0 +1,95 @@ +cmake_minimum_required(VERSION 3.13) + +# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ") + +#appfs lib +file(GLOB appfs_src src/appfs.c src/appfs.h) +add_library( + appfs_lib + STATIC + ${appfs_src} +) +target_include_directories( + appfs_lib + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src +) + +# appfstiny lib +file(GLOB appfs_tiny_src src/appfs_tiny.c src/appfs_tiny.h) +add_library( + appfs_tiny + STATIC + ${appfs_tiny_src} +) +target_include_directories( + appfs_tiny + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src +) + +file( + GLOB deps + heap_stack.c + main.c +) + +add_executable( + appfs.elf + ${deps} + ${START_SRC} +) +target_link_libraries( + appfs.elf + PUBLIC + -Bstatic + ${LIBC_NAME} + ${START_LIB} + sys + sys_util + sys_svr + appfs_lib + ${GCC_LIB_PATH}/libgcc.a +) +target_include_directories( + appfs.elf + PUBLIC + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys/inc + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys_svr/inc + + ${CMAKE_CURRENT_SOURCE_DIR}/src +) +add_dependencies( + appfs.elf + ${START_LIB} + sys + sys_util + appfs_lib +) +set_target_properties( + appfs.elf PROPERTIES LINK_FLAGS + "-T ${CMAKE_CURRENT_LIST_DIR}/${ARCH_NAME}/link.lds ${CORTEX_M_LINK_FLAGS} --gc-section -no-dynamic-linker " + #--no-warn-rwx-segments +) +add_custom_target( + appfs_dump ALL + COMMAND + ${CMAKE_OBJDUMP} -s -S appfs.elf > ${CMAKE_SOURCE_DIR}/build/output/appfs.S + COMMAND + ${CMAKE_READELF} -a appfs.elf > ${CMAKE_SOURCE_DIR}/build/output/appfs.txt + COMMAND + ${CMAKE_OBJCOPY} -O binary -S appfs.elf appfs.bin + COMMAND + ${CMAKE_SIZE} appfs.elf + COMMAND + mkdir -p ${CMAKE_SOURCE_DIR}/build/output/cpio + COMMAND + cp appfs.bin ${CMAKE_SOURCE_DIR}/build/output/cpio/appfs +) + +add_dependencies(appfs_dump appfs.elf) + +execute_process( + COMMAND cd ${CMAKE_CURRENT_SOURCE_DIR}/src/ && sh build.sh + COMMAND_ECHO STDOUT +) diff --git a/mkrtos_user/server/fs/appfs/armv7_8m/link.lds b/mkrtos_user/server/fs/appfs/armv7_8m/link.lds new file mode 100644 index 000000000..f297e7ddb --- /dev/null +++ b/mkrtos_user/server/fs/appfs/armv7_8m/link.lds @@ -0,0 +1,124 @@ +ENTRY(_start_) + +SECTIONS +{ + .text : { + . = ALIGN(4); + __text_start__ = .; + KEEP(*(.first)) + *(.text) + *(.text.*) + + KEEP(*(.init)) + KEEP(*(.fini)) + + /* .ctors */ + *crtbegin.o(.ctors) + *crtbegin?.o(.ctors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors) + *(SORT(.ctors.*)) + *(.ctors) + + /* .dtors */ + *crtbegin.o(.dtors) + *crtbegin?.o(.dtors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors) + *(SORT(.dtors.*)) + *(.dtors) + + *(SORT(.rodata.*)) + *(.rodata) + + KEEP(*(.eh_frame*)) + + . = ALIGN(4); + __rel_start__ = .; + *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*) + __rel_end__ = .; + } + .ARM.exidx : { + . = ALIGN(4); + __exdix_start = .; + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + /* This is used by the startup in order to initialize the .data secion */ + __exdix_end = .; + } + + .permissions_table : { + . = ALIGN(4); + __permissions_table_start__ = .; + KEEP(*(.permissions_table)) + __permissions_table_end__ = .; + } + + + PROVIDE(__ram_size__ = __bss_end__ - __data_start__); + .data : { + . = ALIGN(4); + __data_start__ = .; + __got_start__ = .; + *(.got) + __got_end__ = .; + . = ALIGN(4); + *(.data) + *(.data.*) + + *(vtable) + *(.data*) + + . = ALIGN(4); + /* preinit data */ + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP(*(.preinit_array)) + PROVIDE_HIDDEN (__preinit_array_end = .); + + . = ALIGN(4); + /* init data */ + PROVIDE_HIDDEN (__init_array_start = .); + KEEP(*(SORT(.init_array.*))) + KEEP(*(.init_array)) + PROVIDE_HIDDEN (__init_array_end = .); + + . = ALIGN(4); + /* finit data */ + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP(*(SORT(.fini_array.*))) + KEEP(*(.fini_array)) + PROVIDE_HIDDEN (__fini_array_end = .); + + . = ALIGN(4); + _shell_command_start = .; + KEEP(*(shellCommand)) + _shell_command_end = .; + + . = ALIGN(4); + /* All data end */ + __data_end__ = .; + } + + PROVIDE(__heap_size__ = __heap_end__ - __heap_start__); + PROVIDE(__stack_size__ = __stack_end__ - __stack_start__); + .bss : { + . = ALIGN(4); + /* This is used by the startup in order to initialize the .bss secion */ + __bss_start__ = .; + *(.bss) + *(COMMON) + + . = ALIGN(4); + __heap_start__ = .; + KEEP(*(.bss.heap)) + __heap_end__ = .; + + . = ALIGN(4); + __stack_start__ = .; + KEEP(*(.bss.stack)) + __stack_end__ = .; + + *(.bss.*) + /* This is used by the startup in order to initialize the .bss secion */ + . = ALIGN(4); + __bss_end__ = .; + } + _end = .; +} diff --git a/mkrtos_user/server/fs/appfs/fs_rpc.c b/mkrtos_user/server/fs/appfs/fs_rpc.c new file mode 100644 index 000000000..e9e10b24d --- /dev/null +++ b/mkrtos_user/server/fs/appfs/fs_rpc.c @@ -0,0 +1,437 @@ +#include "cons_cli.h" +#include "ff.h" +#include "fs_svr.h" +#include "rpc_prot.h" +#include "u_env.h" +#include "u_log.h" +#include "u_rpc.h" +#include "u_rpc_svr.h" +#include "u_sig.h" +#include +#include +#include +#include +#include +#include "kstat.h" +static fs_t fs; +static int fs_sig_call_back(pid_t pid, umword_t sig_val); +void fs_svr_init(void) +{ + fs_init(&fs); + meta_reg_svr_obj(&fs.svr, FS_PROT); +#ifdef CONFIG_USING_SIG + pm_sig_func_set(fs_sig_call_back); +#endif +} +typedef struct file_desc +{ + union + { + FIL fp; + FATFS_DIR dir; + }; + pid_t pid; + uint8_t type; //!< 0:file 1:dir +} file_desc_t; + +#define FILE_DESC_NR 8 //!< 最多同时可以打开多少个文件 +static file_desc_t files[FILE_DESC_NR]; //!< 预先设置的文件描述符 + +static void free_fd(pid_t pid) +{ + for (int i = 0; i < FILE_DESC_NR; i++) + { + if (files[i].fp.obj.fs) + { + fs_svr_close(i); + files[i].fp.obj.fs = NULL; + files[i].pid = 0; + } + } +} + +static int fs_sig_call_back(pid_t pid, umword_t sig_val) +{ + switch (sig_val) + { + case KILL_SIG: + free_fd(pid); + break; + } + return 0; +} + +static file_desc_t *alloc_file(int *fd) +{ + for (int i = 0; i < FILE_DESC_NR; i++) + { + if (files[i].fp.obj.fs == NULL) + { + *fd = i; + files[i].pid = thread_get_src_pid(); + return &files[i]; + } + } + return NULL; +} +static void free_file(int fd) +{ + files[fd].fp.obj.fs = NULL; +} +static file_desc_t *file_get(int fd) +{ + if (fd < 0 || fd >= FILE_DESC_NR) + { + return NULL; + } + if (files[fd].fp.obj.fs == NULL) + { + return NULL; + } + return files + fd; +} +static int fatfs_err_conv(FRESULT res) +{ + switch (res) + { + case FR_OK: + return 0; + case FR_DISK_ERR: + return -EIO; + case FR_NO_FILE: + return -ENOENT; + case FR_NO_PATH: + return -ENOENT; + case FR_INVALID_NAME: + return -EINVAL; + case FR_DENIED: + return -EPERM; + case FR_EXIST: + return -EEXIST; + case FR_WRITE_PROTECTED: + return -EIO; + case FR_TIMEOUT: + return -ETIMEDOUT; + case FR_NOT_ENOUGH_CORE: + return -ENOMEM; + case FR_INVALID_PARAMETER: + return -EINVAL; + default: + return -1; + } +} +int fs_svr_open(const char *path, int flags, int mode) +{ + // printf("open %s.\n", path); + int fd; + pid_t pid = thread_get_src_pid(); + file_desc_t *file = alloc_file(&fd); + + if (!file) + { + return -ENOMEM; + } + int new_mode = 0; + + switch (flags & O_ACCMODE) + { + case O_RDWR: + new_mode |= FA_READ; + new_mode |= FA_WRITE; + break; + case O_RDONLY: + new_mode |= FA_READ; + break; + case O_WRONLY: + new_mode |= FA_WRITE; + break; + } + if ((flags & O_CREAT) && (flags & O_EXCL)) + { + new_mode |= FA_CREATE_NEW; + } + else if ((flags & O_CREAT)) + { + new_mode |= FA_OPEN_ALWAYS; + } + if (flags & O_APPEND) + { + new_mode |= FA_OPEN_APPEND; + } + + FRESULT ret = f_open(&file->fp, path, new_mode); + + if (ret != FR_OK) + { + if (ret == FR_NO_FILE || ret == FR_INVALID_NAME) + { + // 打开的是一个目录,则作为一个目录打开 + ret = f_opendir(&file->dir, path); + if (ret != FR_OK) + { + cons_write_str("open fail..\n"); + free_file(fd); + return fatfs_err_conv(ret); + } + file->type = 1; + // cons_write_str("open dir..\n"); + } + } + else + { + file->type = 0; + // cons_write_str("open file..\n"); + } + + if (ret != FR_OK) + { + return fatfs_err_conv(ret); + } +#ifdef CONFIG_USING_SIG + int w_ret = pm_sig_watch(pid, 0 /*TODO:现在只有kill */); + if (w_ret < 0) + { + printf("pm wath pid %d err.\n", w_ret); + } +#endif + return fd; +} + +int fs_svr_read(int fd, void *buf, size_t len) +{ + UINT br; + file_desc_t *file = file_get(fd); + + if (!file) + { + return -ENOENT; + } + if (file->type != 0) + { + return -EACCES; + } + FRESULT ret = f_read(&file->fp, buf, len, &br); + + if (ret != FR_OK) + { + return fatfs_err_conv(ret); + } + return br; +} +int fs_svr_write(int fd, void *buf, size_t len) +{ + UINT bw; + file_desc_t *file = file_get(fd); + + if (!file) + { + return -ENOENT; + } + if (file->type != 0) + { + return -EACCES; + } + FRESULT ret = f_write(&file->fp, buf, len, &bw); + + if (ret != FR_OK) + { + return fatfs_err_conv(ret); + } + return bw; +} +void fs_svr_close(int fd) +{ + file_desc_t *file = file_get(fd); + + if (!file) + { + return; + } + switch (file->type) + { + case 0: + f_close(&file->fp); + break; + case 1: + f_closedir(&file->dir); + break; + } + file->fp.obj.fs = NULL; +} +int fs_svr_readdir(int fd, dirent_t *dir) +{ + file_desc_t *file = file_get(fd); + + if (!file) + { + return -ENOENT; + } + FILINFO info; + FRESULT ret = f_readdir(&file->dir, &info); + + if (ret != FR_OK || info.fname[0] == 0) + { + return -ENOENT; + } + strncpy(dir->d_name, info.fname, sizeof(dir->d_name)); + dir->d_name[sizeof(dir->d_name) - 1] = 0; + dir->d_reclen = sizeof(*dir); + dir->d_off = 0; + if (info.fattrib & AM_DIR) + { /* Directory */ + dir->d_type = DT_DIR; + } + else + { /* File */ + dir->d_type = DT_CHR; + } + return sizeof(*dir); +} +int fs_svr_lseek(int fd, int offs, int whence) +{ + UINT bw; + file_desc_t *file = file_get(fd); + int new_offs = 0; + + if (!file) + { + return -ENOENT; + } + if (file->type != 0) + { + return -EACCES; + } + switch (whence) + { + case SEEK_SET: + new_offs = offs; + break; + case SEEK_END: + { + new_offs = f_size(&file->fp) + offs; + } + break; + case SEEK_CUR: + { + new_offs = offs + f_tell(&file->fp); + } + break; + default: + return -EINVAL; + } +#if 0 + if (new_offs > f_size(&file->fp)) { + new_offs = f_size(&file->fp); + } +#endif + if (new_offs < 0) + { + new_offs = 0; + } + FRESULT ret = f_lseek(&file->fp, new_offs); + + return fatfs_err_conv(ret); +} +int fs_svr_ftruncate(int fd, off_t off) +{ + file_desc_t *file = file_get(fd); + + if (!file) + { + return -ENOENT; + } + if (file->type != 0) + { + return -EACCES; + } + FRESULT ret = f_truncate(&file->fp); + + return fatfs_err_conv(ret); +} +int fs_svr_fstat(int fd, void *_stat) +{ + struct kstat *stat = _stat; + file_desc_t *file = file_get(fd); + + if (!file) + { + return -ENOENT; + } + memset(stat, 0, sizeof(*stat)); + stat->st_size = file->type == 1 ? 0 : f_size(&file->fp); + stat->st_mode = file->type == 1 ? S_IFDIR : S_IFREG; + stat->st_blksize = 0; + return 0; +} +int fs_svr_ioctl(int fd, int req, void *arg) +{ + return 0; /*TODO:*/ +} +int fs_svr_fsync(int fd) +{ + file_desc_t *file = file_get(fd); + + if (!file) + { + return -EBADFD; + } + if (file->type != 0) + { + return -EBADFD; + } + f_sync(&file->fp); + return 0; +} +int fs_svr_unlink(const char *path) +{ + FRESULT ret = f_unlink(path); + + return fatfs_err_conv(ret); +} +int fs_svr_symlink(const char *existing, const char *new) +{ + return -ENOSYS; +} +int fs_svr_mkdir(char *path) +{ + FRESULT ret = f_mkdir(path); + + return fatfs_err_conv(ret); +} +int fs_svr_rmdir(char *path) +{ + return -ENOSYS; +} +int fs_svr_rename(char *oldname, char *newname) +{ + return fatfs_err_conv(f_rename(oldname, newname)); +} +int fs_svr_stat(const char *path, void *_buf) +{ + FILINFO INFO; + FRESULT ret; + struct kstat *buf = (struct kstat *)_buf; + + ret = f_stat(path, &INFO); + if (ret != FR_OK) + { + return fatfs_err_conv(ret); + } + memset(buf, 0, sizeof(*buf)); + buf->st_size = INFO.fsize; + buf->st_mode = (INFO.fattrib & AM_DIR) ? S_IFDIR : S_IFREG; + return 0; +} +ssize_t fs_svr_readlink(const char *path, char *buf, size_t bufsize) +{ + return -ENOSYS; +} + +int fs_svr_statfs(const char *path, struct statfs *buf) +{ + return -ENOSYS; +} +void fs_svr_loop(void) +{ + rpc_loop(); +} \ No newline at end of file diff --git a/mkrtos_user/server/fs/appfs/fs_rpc.h b/mkrtos_user/server/fs/appfs/fs_rpc.h new file mode 100644 index 000000000..ae79b7a30 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/fs_rpc.h @@ -0,0 +1,5 @@ +#pragma once + +void fs_svr_init(void); +void fs_svr_loop(void); +void *file_temp_buf_get(void); diff --git a/mkrtos_user/server/fs/appfs/heap_stack.c b/mkrtos_user/server/fs/appfs/heap_stack.c new file mode 100644 index 000000000..a88cb819e --- /dev/null +++ b/mkrtos_user/server/fs/appfs/heap_stack.c @@ -0,0 +1,17 @@ + +#define HEAP_SIZE 512 +#define STACK_SIZE 1024 * 2 + +#if defined(__CC_ARM) +#define HEAP_ATTR SECTION("HEAP") __attribute__((zero_init)) +#define STACK_ATTR SECTION("STACK") __attribute__((zero_init)) +#elif defined(__GNUC__) +#define HEAP_ATTR __attribute__((__section__(".bss.heap"))) +#define STACK_ATTR __attribute__((__section__(".bss.stack"))) +#elif defined(__IAR_SYSTEMS_ICC__) +#define HEAP_ATTR +#define STACK_ATTR +#endif + +__attribute__((used)) HEAP_ATTR static char _____heap_____[HEAP_SIZE]; +__attribute__((used)) STACK_ATTR static char _____stack_____[STACK_SIZE]; diff --git a/mkrtos_user/server/fs/appfs/main.c b/mkrtos_user/server/fs/appfs/main.c new file mode 100644 index 000000000..f7dd4859c --- /dev/null +++ b/mkrtos_user/server/fs/appfs/main.c @@ -0,0 +1,8 @@ + +#include + +int main(int argv, char *argc[]) +{ + printf("%s run.\n", argc[0]); + return 0; +} \ No newline at end of file diff --git a/mkrtos_user/server/fs/appfs/src/.gitignore b/mkrtos_user/server/fs/appfs/src/.gitignore new file mode 100644 index 000000000..5eff4f873 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/.gitignore @@ -0,0 +1,2 @@ +build +out.img \ No newline at end of file diff --git a/mkrtos_user/server/fs/appfs/src/CMakeLists.txt b/mkrtos_user/server/fs/appfs/src/CMakeLists.txt new file mode 100644 index 000000000..8760506c7 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.5.0) +project(appfs VERSION 0.1.0 LANGUAGES C) +set(CMAKE_BUILD_TYPE Debug) +add_executable(appfs appfs.c hw_block_test.c hw_block_sim.c main.c test.c open.c) + +include(CTest) +enable_testing() + diff --git a/mkrtos_user/server/fs/appfs/src/appfs.c b/mkrtos_user/server/fs/appfs/src/appfs.c new file mode 100644 index 000000000..7d3332136 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/appfs.c @@ -0,0 +1,978 @@ +#include +#include +#include +#include +#include +#include +#include "appfs.h" + +#define BYPTE_PER_PTR (sizeof(uint32_t)) //!< 先统一4字节处理方式 +#define BITS_PER_BYTE (8) +#define BITS_PER_PTR (sizeof(uint32_t) * BITS_PER_BYTE) + +int appfs_init(fs_info_t *fs) +{ + int ret; + assert(fs); + fs->cb.hw_init_fs_for_block(fs); + fs_info_t *fs_info = (fs_info_t *)fs->mem_addr; + + ret = fs->cb.hw_read_block(fs, 0, fs_info, MIN(sizeof(fs_info_t), fs->save.block_size)); + + *fs = *fs_info; + return ret; +} +static int mem_set_bits(uint32_t *buf, int start, int end) +{ + assert(buf); + assert(start >= 0); + assert(end >= 0); + assert(start <= end); + + for (int i = start; i <= end; i++) + { + int block_inx = i / (BITS_PER_PTR); + int block_bit = i % (BITS_PER_PTR); + buf[block_inx] |= (1UL << block_bit); + } + + return 0; +} +static int mem_clear_bits(uint32_t *buf, int start, int end) +{ + assert(buf); + assert(start >= 0); + assert(end >= 0); + assert(start <= end); + + for (int i = start; i <= end; i++) + { + int block_inx = i / (BITS_PER_PTR); + int block_bit = i % (BITS_PER_PTR); + buf[block_inx] &= ~(1UL << block_bit); + } + + return 0; +} +/** + * 设置块中的某些位为1 + */ +static int appfs_blockinfo_bits_set(fs_info_t *info, int st, int end, int is_set) +{ + assert(info); + int block_inx = -1; + int block_last_inx = -1; + int ret = 0; + for (int i = st; i <= end; i++) + { + block_inx = i / (info->save.block_size / BYPTE_PER_PTR) / BITS_PER_PTR; + + if (block_last_inx != -1 && block_last_inx != block_inx) + { + ret = info->cb.hw_write_and_erase_block(info, info->save.blockinfo_inx + block_last_inx, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + } + + int block_bit = i % (info->save.block_size * BITS_PER_BYTE); + + if (block_last_inx == -1 || block_last_inx != block_inx) + { + ret = info->cb.hw_read_block(info, info->save.blockinfo_inx + block_inx, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + } + if (is_set) + { + info->buf[block_bit / BITS_PER_PTR] |= (1UL << (block_bit % BITS_PER_PTR)); + } + else + { + info->buf[block_bit / BITS_PER_PTR] &= ~(1UL << (block_bit % BITS_PER_PTR)); + } + block_last_inx = block_inx; + } + if (end - st >= 0) + { + // 更新最后一个块 + ret = info->cb.hw_write_and_erase_block(info, info->save.blockinfo_inx + block_last_inx, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + } + return ret; +} + +/** + * @brief 格式化文件系统 + * + */ +int appfs_format(fs_info_t *fs, int file_nr) +{ + assert(fs); + assert(file_nr > 0); + + int ret = 0; + fs->cb.hw_init_fs_for_block(fs); + + // 擦除所有块 + for (int i = 0; i < fs->save.block_nr; i++) + { + ret = fs->cb.hw_erase_block(fs, i); + if (ret < 0) + { + return ret; + } + } + strcpy(fs->save.magic, APPFS_MAGIC); + fs->save.version = APPFS_VERSION; + // 初始化文件系统信息 + fs->save.blockinfo_inx = 1; + fs->save.blockinfo_nr = ROUND_UP(fs->save.block_nr, fs->save.block_size * 8); + + fs->save.dirinfo_inx = fs->save.blockinfo_inx + fs->save.blockinfo_nr; + fs->save.dirinfo_nr = ROUND_UP(file_nr * sizeof(dir_info_t), fs->save.block_size); + + memset((fs_info_t *)fs->mem_addr, 0, fs->save.block_size); + for (int i = fs->save.dirinfo_inx; i < fs->save.dirinfo_inx + fs->save.dirinfo_nr; i++) + { + ret = fs->cb.hw_write_and_erase_block(fs, i, fs->buf, fs->save.block_size); + if (ret < 0) + { + return ret; + } + } + ret = appfs_blockinfo_bits_set(fs, 0, fs->save.blockinfo_nr + fs->save.dirinfo_nr + 1, 0); + if (ret < 0) + { + return ret; + } + + ret = fs->cb.hw_write_and_erase_block(fs, 0, fs, MIN(sizeof(fs_info_t), fs->save.block_size)); + return ret; +} +/** + * @brief 查找连续的count个块 + * + * @param info + * @param st + * @param end + * @param count + * @return int + */ +static inline int calculate_block_index(int i, int j, int m, fs_info_t *info) +{ + return i * (info->save.block_size * BITS_PER_BYTE) + j * BITS_PER_PTR + m; +} + +/** + * @brief 查找连续的count个块并设置为已使用 + * 1. 从blockinfo_inx开始查找 + * 2. 查找到count个连续的块 + * 3. 设置这些块为已使用 + * 4. 返回起始块号和结束块号 + * @param info + * @param st + * @param end + * @param count + * + */ +int appfs_find_set_blocks(fs_info_t *info, int *st, int *end, int count) +{ + assert(st); + assert(end); + assert(info); + int ret = 0; + int state_flag = 0; //!< 0:未找到 1:找到起始块 2:找到结束块 + int count_cur = 0; //!< 当前计数 + + for (int i = 0; i < info->save.blockinfo_nr; i++) + { + ret = info->cb.hw_read_block(info, info->save.blockinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + for (int j = 0; j < info->save.block_size / BYPTE_PER_PTR; j++) + { + for (int m = 0; m < BITS_PER_PTR; m++) + { + if ((info->buf[j] & (1UL << m))) + { + switch (state_flag) + { + case 0: + *st = calculate_block_index(i, j, m, info); //!< 计算起始块号 + state_flag = 1; + count_cur++; + break; + case 1: + count_cur++; + if (count_cur >= count) + { + *end = calculate_block_index(i, j, m, info); //!< 计算结束块号 + state_flag = 2; + goto __next; + } + break; + } + } + else + { + *st = 0; + *end = 0; + count_cur = 0; + state_flag = 0; + } + } + } + } + if (count_cur < count) + { + printf("appfs mem is low.\n"); + return -1; + } +__next: + ret = appfs_blockinfo_bits_set(info, *st, *end, 0); +#if 0 + if (ret >= 0) + { + printf("st:%d end %d.\n", *st, *end); + } +#endif + return ret; +} +static int appfs_check_file_exist(fs_info_t *info, const char *name) +{ + assert(info); + assert(name); + + int ret = 0; + + for (int i = 0; i < info->save.dirinfo_nr; i++) + { + ret = info->cb.hw_read_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + for (int j = 0; j < info->save.block_size / sizeof(dir_info_t); j++) + { + dir_info_t *dir_info = &((dir_info_t *)info->buf)[j]; + if (strcmp(dir_info->name, name) == 0) + { + return -1; + } + } + } + + return 0; +} +/** + * @brief 创建文件 + * 1. 检查文件是否存在 + * 2. 查找连续的块并设置为已使用 + * 3. 设置文件信息 + * 4. 更新目录信息 + * @param info + * @param path + * @param size + */ +int appfs_create_file(fs_info_t *info, const char *path, int size) +{ + assert(info); + assert(path); + + int ret = -1; + + ret = appfs_check_file_exist(info, path); + if (ret < 0) + { + return ret; + } + + for (int i = 0; i < info->save.dirinfo_nr; i++) + { + assert(info->cb.hw_read_block); + ret = info->cb.hw_read_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + for (int j = 0; j < info->save.block_size / sizeof(dir_info_t); j++) + { + dir_info_t *dir_info = &((dir_info_t *)info->buf)[j]; + if (strcmp(dir_info->name, path) == 0) + { + return -1; + } + if (dir_info->block_inx == 0) + { + int st = 0; + int end = 0; + + if (size != 0) + { + // 查找连续的块并设置为已使用 + ret = appfs_find_set_blocks(info, &st, &end, ROUND_UP(size, info->save.block_size)); + if (ret < 0) + { + return ret; + } + } + // 更新目录信息 + ret = info->cb.hw_read_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + // 设置文件信息 + dir_info->block_inx = st == 0 ? -1 : st; + dir_info->size = size; + strncpy(dir_info->name, path, APPFS_FILE_NAME_MAX); + dir_info->name[APPFS_FILE_NAME_MAX - 1] = '\0'; + + ret = info->cb.hw_write_and_erase_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + return 0; + } + } + } + + return ret; +} +/** + * @brief 写文件 + * 1. 查找文件 + * 2. 设置文件块为未使用 + * 3. 更新目录信息 + * + * @param info + * @param path + * @param offset + * @return int 0:成功 -1:失败 + */ +int appfs_write_file(fs_info_t *info, const char *name, void *data, int size, int offset) +{ + assert(info); + assert(data); + + int ret = 0; + int write_len = 0; + + const dir_info_t *dir_info = appfs_find_file_by_name(info, name); + + if (!dir_info) + { + return -ENOENT; + } + // 计算块号 + int block_inx = dir_info->block_inx; + int block_offset_inx = ROUND_DOWN(offset, info->save.block_size); + int block_nr = ROUND_UP(offset + size, info->save.block_size); + + block_nr = MIN(block_nr, ROUND_UP(dir_info->size - offset, info->save.block_size)); + + for (int k = 0; k < block_nr && write_len < dir_info->size; k++) + { + // 写入数据 + int write_size = MIN(size, info->save.block_size - (offset % info->save.block_size)); + + if (write_size != info->save.block_size) + { + ret = info->cb.hw_read_block(info, block_inx + k + block_offset_inx, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + } + + // 从data + k * info->save.block_size拷贝write_size大小的数据到buf + memcpy((uint8_t *)(info->buf) + (offset % info->save.block_size), data + write_len, write_size); + // 写入块 + ret = info->cb.hw_write_and_erase_block(info, block_inx + k + block_offset_inx, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + offset = 0; + write_len += write_size; + size -= write_size; + } + return write_len; +} +int appfs_read_file(fs_info_t *info, const char *name, void *data, int size, int offset) +{ + assert(info); + assert(data); + + int ret = 0; + int read_len = 0; + + const dir_info_t *dir_info = appfs_find_file_by_name(info, name); + + if (!dir_info) + { + return -ENOENT; + } + // 计算块号 + int block_inx = dir_info->block_inx; + int block_offset_inx = ROUND_DOWN(offset, info->save.block_size); + int block_nr = ROUND_UP(offset + size, info->save.block_size); + + block_nr = MIN(block_nr, ROUND_UP(dir_info->size - offset, info->save.block_size)); + + for (int k = 0; k < block_nr && read_len < dir_info->size; k++) + { + // 写入数据 + int read_size = MIN(size, info->save.block_size - (offset % info->save.block_size)); + + ret = info->cb.hw_read_block(info, block_inx + k + block_offset_inx, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + + memcpy(data + read_len, (uint8_t *)(info->buf) + (offset % info->save.block_size), read_size); + offset = 0; + read_len += read_size; + size -= read_size; + } + return read_len; +} +static int appfs_write_dirinfo(fs_info_t *info, dir_info_t *in_dir_info) +{ + assert(info); + assert(in_dir_info); + int ret = -1; + + for (int i = 0; i < info->save.dirinfo_nr; i++) + { + // 读取目录信息 + ret = info->cb.hw_read_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + // 遍历目录信息 + for (int j = 0; j < info->save.block_size / sizeof(dir_info_t); j++) + { + dir_info_t *dir_info = &((dir_info_t *)info->buf)[j]; + if (strcmp(dir_info->name, in_dir_info->name) == 0) + { + + *dir_info = *in_dir_info; + ret = info->cb.hw_write_and_erase_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + ret = 0; + goto end; + } + } + } +end: + return ret; +} +/** + * @brief 删除目录信息 + * 1. 通过文件名查找文件 + * 2. 删除目录信息 + * @param info + * @param name + * @return int 0:成功 -1:失败 + */ +static int appfs_delete_dirinfo_by_name(fs_info_t *info, const char *name) +{ + assert(info); + assert(name); + + int ret = 0; + + for (int i = 0; i < info->save.dirinfo_nr; i++) + { + // 读取目录信息 + ret = info->cb.hw_read_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + // 遍历目录信息 + for (int j = 0; j < info->save.block_size / sizeof(dir_info_t); j++) + { + dir_info_t *dir_info = &((dir_info_t *)info->buf)[j]; + if (strcmp(dir_info->name, name) == 0) + { + memset(dir_info, 0, sizeof(dir_info_t)); + ret = info->cb.hw_write_and_erase_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + return 0; + } + } + } + + return 0; +} +const dir_info_t *appfs_dir_info_get_next(fs_info_t *info, const dir_info_t *cur) +{ + assert(info); + assert(cur); + int ret = 0; + + for (const dir_info_t *cur_dir = (const dir_info_t *)cur + 1; + (unsigned long)cur_dir < info->mem_addr + (info->save.dirinfo_inx + info->save.dirinfo_nr) * info->save.block_size; + cur_dir++) + { + if (cur_dir->block_inx == 0) + { + continue; + } + return cur_dir; + } + return NULL; +} +const dir_info_t *appfs_dir_info_get_first(fs_info_t *info) +{ + assert(info); + for (const dir_info_t *cur_dir = (const dir_info_t *)(info->mem_addr + (info->save.dirinfo_inx * info->save.block_size)); + (unsigned long)cur_dir < info->mem_addr + (info->save.dirinfo_inx + info->save.dirinfo_nr) * info->save.block_size; + cur_dir++) + { + if (cur_dir->block_inx == 0) + { + continue; + } + return cur_dir; + } + return NULL; +} +const dir_info_t *appfs_dir_info_get_index(fs_info_t *info, int index) +{ + assert(info); + int i = 0; + for (const dir_info_t *cur_dir = (const dir_info_t *)(info->mem_addr + (info->save.dirinfo_inx * info->save.block_size)); + (unsigned long)cur_dir < info->mem_addr + (info->save.dirinfo_inx + info->save.dirinfo_nr) * info->save.block_size; + cur_dir++) + { + if (cur_dir->block_inx == 0) + { + continue; + } + if (i == index) + { + return cur_dir; + } + i++; + } + return NULL; +} +const int appfs_get_dir_info_size(fs_info_t *info) +{ + assert(info); + int i = 0; + for (const dir_info_t *cur_dir = (const dir_info_t *)(info->mem_addr + (info->save.dirinfo_inx * info->save.block_size)); + (unsigned long)cur_dir < info->mem_addr + (info->save.dirinfo_inx + info->save.dirinfo_nr) * info->save.block_size; + cur_dir++) + { + if (cur_dir->block_inx == 0) + { + continue; + } + i++; + } + return i; +} +/** + * 通过文件名查找文件 + */ +const dir_info_t *appfs_find_file_by_name(fs_info_t *info, const char *name) +{ + assert(info); + assert(name); + + int ret = 0; + + for (int i = 0; i < info->save.dirinfo_nr; i++) + { + // 读取目录信息 + ret = info->cb.hw_read_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return NULL; + } + // 遍历目录信息 + for (int j = 0; j < info->save.block_size / sizeof(dir_info_t); j++) + { + const dir_info_t *dir_info = &((dir_info_t *)info->buf)[j]; + if (strcmp(dir_info->name, name) == 0) + { + return (const dir_info_t *)(info->mem_addr + (info->save.dirinfo_inx + i) * info->save.block_size + j * sizeof(dir_info_t)); + } + } + } + + return NULL; +} +/** + * 通过尾块查找文件 + * 1. 遍历目录信息 + * 2. 通过尾块查找文件 + * 3. 返回文件信息 + * @param info + * @param tail_block_inx + */ +static const dir_info_t *appfs_find_file_by_tail_block(fs_info_t *info, int tail_block_inx) +{ + assert(info); + + int ret = 0; + + for (int i = 0; i < info->save.dirinfo_nr; i++) + { + // 读取目录信息 + ret = info->cb.hw_read_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return NULL; + } + // 遍历目录信息 + for (int j = 0; j < info->save.block_size / sizeof(dir_info_t); j++) + { + dir_info_t *dir_info = &((dir_info_t *)info->buf)[j]; + int file_tail_block_inx = dir_info->block_inx + ROUND_UP(dir_info->size, info->save.block_size); + if (file_tail_block_inx == tail_block_inx) + { + return (const dir_info_t *)(info->mem_addr + (info->save.dirinfo_inx + i) * info->save.block_size + j * sizeof(dir_info_t)); + } + } + } + + return NULL; +} +/** + * 通过头块查找文件 + */ +static const dir_info_t *appfs_find_file_by_head_block(fs_info_t *info, int head_block_inx) +{ + assert(info); + + int ret = 0; + + for (int i = 0; i < info->save.dirinfo_nr; i++) + { + // 读取目录信息 + ret = info->cb.hw_read_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return NULL; + } + // 遍历目录信息 + for (int j = 0; j < info->save.block_size / sizeof(dir_info_t); j++) + { + const dir_info_t *dir_info = &((dir_info_t *)info->buf)[j]; + if (dir_info->block_inx == head_block_inx) + { + return (const dir_info_t *)(info->mem_addr + (info->save.dirinfo_inx + i) * info->save.block_size + j * sizeof(dir_info_t)); + } + } + } + + return NULL; +} +/** + * 将文件移动到指定的块上 + */ +static const dir_info_t *appfs_move_file_to_block(fs_info_t *info, int file_block_inx, int move_to_block_inx) +{ + assert(info); + + const dir_info_t *dir_info = NULL; + int ret; + + // printf("move file %d to %d.\n", file_block_inx, move_to_block_inx); + + // 通过尾块查找文件 + dir_info = appfs_find_file_by_head_block(info, file_block_inx); + if (dir_info == NULL) + { + printf("file is not found!\n"); + return NULL; + } + for (int i = move_to_block_inx; + i < move_to_block_inx + ROUND_UP(dir_info->size, info->save.block_size); i++) + { + // 读取块 + ret = info->cb.hw_read_block(info, dir_info->block_inx + i - move_to_block_inx, + info->buf, info->save.block_size); + if (ret < 0) + { + /*TODO:错误处理*/ + return NULL; + } + // 写入块 + ret = info->cb.hw_write_and_erase_block(info, i, info->buf, info->save.block_size); + if (ret < 0) + { + /*TODO:错误处理*/ + return NULL; + } + } + ret = appfs_blockinfo_bits_set(info, dir_info->block_inx, dir_info->block_inx + ROUND_UP(dir_info->size, info->save.block_size) - 1, 1); + if (ret < 0) + { + return NULL; + } + ret = appfs_blockinfo_bits_set(info, move_to_block_inx, move_to_block_inx + ROUND_UP(dir_info->size, info->save.block_size) - 1, 0); + if (ret < 0) + { + return NULL; + } + // 更新dirinfo + + for (int i = 0; i < info->save.dirinfo_nr; i++) + { + // 读取目录信息 + ret = info->cb.hw_read_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return NULL; + } + // 遍历目录信息 + for (int j = 0; j < info->save.block_size / sizeof(dir_info_t); j++) + { + dir_info_t *dir_info_tmp = &((dir_info_t *)info->buf)[j]; + if (dir_info_tmp->block_inx == file_block_inx) + { + dir_info_tmp->block_inx = move_to_block_inx; + ret = info->cb.hw_write_and_erase_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return NULL; + } + goto end; + } + } + } +end: + return dir_info; +} + +/** + * 删除文件 + */ +int appfs_delete_file(fs_info_t *info, const char *name) +{ + assert(info); + assert(name); + int ret = 0; + const dir_info_t *dir_info = NULL; + + // 通过文件名查找文件 + dir_info = appfs_find_file_by_name(info, name); + if (dir_info == NULL) + { + printf("file is not found!\n"); + return -1; + } + if (dir_info->ref != 0) + { + printf("delete filed, ref not is 0.\n"); + return -1; + } + + // 删除已有的文件 + ret = appfs_blockinfo_bits_set(info, dir_info->block_inx, dir_info->block_inx + ROUND_UP(dir_info->size, info->save.block_size) - 1, 1); + if (ret < 0) + { + return ret; + } + + ret = appfs_delete_dirinfo_by_name(info, name); + if (ret < 0) + { + printf("delete dirinfo failed!\n"); + return ret; + } + return 0; +} +/** + * 重设大小,会导致所有的数据消失 + * @param info + * @param dir_info + * @param size + * @return <0 fail, >=0 旧大小 + */ +int appfs_file_resize_raw(fs_info_t *info, const dir_info_t *dir_info, int size) +{ + int ret; + int old_size = dir_info->size; + int new_size = size; + + if (new_size == old_size) + { + return old_size; + } + + ret = appfs_blockinfo_bits_set(info, dir_info->block_inx, dir_info->block_inx + ROUND_UP(dir_info->size, info->save.block_size) - 1, 1); + if (ret < 0) + { + return ret; + } + + dir_info_t new_info = *dir_info; + + if (dir_info->size != 0) + { + new_info.size = 0; // 大小设置为0 + new_info.block_inx = 0; + ret = appfs_write_dirinfo(info, &new_info); + if (ret < 0) + { + return ret; + } + } + + ret = appfs_arrange_files(info); + if (ret < 0) + { + return ret; + } + int st; + int end; + + // 查找连续的块并设置为已使用 + ret = appfs_find_set_blocks(info, &st, &end, ROUND_UP(new_size, info->save.block_size)); + if (ret < 0) + { + return ret; + } + new_info.block_inx = st; + new_info.size = new_size; + ret = appfs_write_dirinfo(info, &new_info); + if (ret < 0) + { + return ret; + } + return ret; +} +/** + * 重新设置文件大小 + * 如果文件大小发生变化,则会清空所有的文件的所有数据 + */ +int appfs_file_resize(fs_info_t *info, const char *name, int size) +{ + assert(info); + + int ret = 0; + const dir_info_t *dir_info = NULL; + + // 通过文件名查找文件 + dir_info = appfs_find_file_by_name(info, name); + if (dir_info == NULL) + { + printf("file is not found!\n"); + return -1; + } + + return appfs_file_resize_raw(info, dir_info, size); +} +/** + * 整理文件,将所有文件整齐排列 + * 1. 遍历所有块 + * 2. 查找空闲块 + * 3. 将文件移动到空闲块 + * @param info 文件系统信息 + * @return int 0:成功 -1:失败 + */ +int appfs_arrange_files(fs_info_t *info) +{ + int last_idle_block_inx = -1; + int status = 0; + assert(info); + for (int i = 0; i < info->save.blockinfo_nr; i++) + { + int ret; + again: + ret = info->cb.hw_read_block(info, info->save.blockinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + // 遍历每个块 + for (int j = 0; j < info->save.block_size / BYPTE_PER_PTR; j++) + { + // 遍历每个bit + for (int m = 0; m < BITS_PER_PTR; m++) + { + // 找到使用的块 + if ((info->buf[j] & (1UL << m)) == 0) + { + int block_inx = i * (info->save.block_size * BITS_PER_BYTE) + j * BITS_PER_PTR + m; + + if (last_idle_block_inx != -1) + { + // 遇到中间有空闲块,需要调整使得中间没有空闲块 + const dir_info_t *dir_info = appfs_move_file_to_block(info, block_inx, last_idle_block_inx); + + if (dir_info == NULL) + { + printf("move file to block failed!\n"); + return -1; + } + last_idle_block_inx = -1; + goto again; + } + } + else + { + if (last_idle_block_inx == -1) + { + last_idle_block_inx = i * (info->save.block_size / BYPTE_PER_PTR) * BITS_PER_PTR + j * BITS_PER_PTR + m; + } + } + } + } + } + + return 0; +} +/** + * @brief 打印文件信息 + */ +int appfs_print_files(fs_info_t *info) +{ + assert(info); + int ret = 0; + for (int i = 0; i < info->save.dirinfo_nr; i++) + { + // 读取目录信息 + ret = info->cb.hw_read_block(info, info->save.dirinfo_inx + i, info->buf, info->save.block_size); + if (ret < 0) + { + return ret; + } + // 遍历目录信息 + for (int j = 0; j < info->save.block_size / sizeof(dir_info_t); j++) + { + dir_info_t *dir_info = &((dir_info_t *)info->buf)[j]; + if (dir_info->block_inx == 0) + { + continue; + } + printf("name:%s, block_inx:%d, size:%d\n", dir_info->name, dir_info->block_inx, dir_info->size); + } + } + return 0; +} diff --git a/mkrtos_user/server/fs/appfs/src/appfs.h b/mkrtos_user/server/fs/appfs/src/appfs.h new file mode 100644 index 000000000..973a4a28d --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/appfs.h @@ -0,0 +1,125 @@ +#pragma once +#include +#include +#include +struct hw_callback; +typedef struct hw_callback hw_callback_t; +struct fs_info; +typedef struct fs_info fs_info_t; + +#ifndef ROUND_UP +#define ROUND(a, b) (((a) / (b)) + (((a) % (b)) ? 1 : 0)) //!< a/b后的值向上取整 +#define ROUND_UP(a, b) ROUND(a, b) //!< a除b向上取整数 +#endif + +#ifndef MIN +#define MIN(a, b) ((a) < (b) ? (a) : (b)) //!< 最小值 +#endif + +#ifndef ROUND_DOWN +#define ROUND_DOWN(a, b) ((a) / (b)) //!< a/b向下取整 +#endif + +#define APPFS_MAGIC "APPFS" +#define APPFS_VERSION 1 + +typedef struct fs_info_save +{ + char magic[8]; //!< APPFS + int version; //!< 版本号 + int blockinfo_inx; //!< 块信息起始块号 + int blockinfo_nr; //!< 块信息块数量 + + int dirinfo_inx; //!< 目录信息起始块号 + int dirinfo_nr; //!< 目录信息块数量 + + int block_inx; //!< 块起始块号 + int block_nr; //!< 块数量 + + int block_size; //!< 块大小 +} fs_info_save_t; + +typedef struct hw_callback +{ + int (*hw_erase_block)(fs_info_t *fs, int block_inx); //!< 擦除块 + int (*hw_write_and_erase_block)(fs_info_t *fs, int block_inx, void *buf, int size); //!< 擦除并写入 + int (*hw_read_block)(fs_info_t *fs, int block_inx, void *buf, int size); //!< 读块 + int (*hw_init_fs_for_block)(fs_info_t *fs); //!< 初始化 +} hw_callback_t; + +typedef struct fs_info +{ + fs_info_save_t save; //!< 保存的文件系统信息 + uint32_t *buf; //!< 缓冲区 + unsigned long mem_addr; //!< 内存地址 + int dev_fd; //!< device file descriptor + hw_callback_t cb; +} fs_info_t; + +#define APPFS_FILE_NAME_MAX 52 //!< 文件名最大长度 + +typedef struct dir_info +{ + char name[APPFS_FILE_NAME_MAX]; //!< 文件名 + int block_inx; //!< 文件起始块号 + int size; //!< 文件大小 + int ref; //!< 引用计数 +} dir_info_t; + +int appfs_init(fs_info_t *fs); +int appfs_format(fs_info_t *fs, int file_nr); +int appfs_create_file(fs_info_t *info, const char *path, int size); +int appfs_write_file(fs_info_t *info, const char *name, void *data, int size, int offset); +int appfs_read_file(fs_info_t *info, const char *name, void *data, int size, int offset); +int appfs_delete_file(fs_info_t *info, const char *name); +int appfs_file_resize_raw(fs_info_t *info, const dir_info_t *dir_info, int size); +int appfs_file_resize(fs_info_t *info, const char *name, int size); +int appfs_arrange_files(fs_info_t *info); +int appfs_print_files(fs_info_t *info); + +static inline int appfs_get_file_size(const dir_info_t *info) +{ + return info->size; +} +static inline unsigned long appfs_get_file_addr(fs_info_t *fs, const dir_info_t *info) +{ + return info->block_inx * fs->save.block_size + fs->mem_addr; //!< 计算文件地址 +} + +const dir_info_t *appfs_find_file_by_name(fs_info_t *info, const char *name); +const dir_info_t *appfs_dir_info_get_next(fs_info_t *info, const dir_info_t *cur); +const dir_info_t *appfs_dir_info_get_first(fs_info_t *info); +const dir_info_t *appfs_dir_info_get_index(fs_info_t *info, int index); +const int appfs_get_dir_info_size(fs_info_t *info); + +static inline fs_info_t *appfs_get_form_addr(void *addr) +{ + assert(addr); + fs_info_t *fs = addr; + const char *magic_str = APPFS_MAGIC; + + for (int i = 0; magic_str[i]; i++) + { + if (fs->save.magic[0] != magic_str[0]) + { + return NULL; + } + } + return fs; +} +static inline void *appfs_find_file_addr_by_name(fs_info_t *info, const char *name, size_t *size) +{ + const dir_info_t *dir_info; + + dir_info = appfs_find_file_by_name(info, name); + + if (dir_info == NULL) + { + return NULL; + } + if (size) + { + *size = dir_info->size; + } + return (void *)appfs_get_file_addr(info, dir_info); +} diff --git a/mkrtos_user/server/fs/appfs/src/appfs_tiny.c b/mkrtos_user/server/fs/appfs/src/appfs_tiny.c new file mode 100644 index 000000000..68a5b9848 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/appfs_tiny.c @@ -0,0 +1,71 @@ + +#include +#include +#include "appfs_tiny.h" +/** + * 通过文件名查找文件 + */ +const dir_info_t *appfs_tiny_find_file_by_name(fs_info_t *info, const char *name) +{ + assert(info); + assert(name); + + int ret = 0; + + for (int i = 0; i < info->save.dirinfo_nr; i++) + { + // 读取目录信息 + if (info->save.dirinfo_inx + i >= info->save.block_nr) + { + return NULL; + } + const void *buf = (void *)((info->save.dirinfo_inx + i) * info->save.block_size + (unsigned long)info); + + if (ret < 0) + { + return NULL; + } + // 遍历目录信息 + for (int j = 0; j < info->save.block_size / sizeof(dir_info_t); j++) + { + const dir_info_t *dir_info = &((dir_info_t *)buf)[j]; + if (strcmp(dir_info->name, name) == 0) + { + return dir_info; + } + } + } + + return NULL; +} +fs_info_t *appfs_tiny_get_form_addr(void *addr) +{ + assert(addr); + fs_info_t *fs = addr; + const char *magic_str = APPFS_MAGIC; + + for (int i = 0; magic_str[i]; i++) + { + if (fs->save.magic[0] != magic_str[0]) + { + return NULL; + } + } + return fs; +} +unsigned long appfs_tiny_find_file_addr_by_name(fs_info_t *info, const char *name, unsigned long *size) +{ + const dir_info_t *dir_info; + + dir_info = appfs_tiny_find_file_by_name(info, name); + + if (dir_info == NULL) + { + return 0; + } + if (size) + { + *size = dir_info->size; + } + return (unsigned long)appfs_tiny_get_file_addr(info, dir_info); +} \ No newline at end of file diff --git a/mkrtos_user/server/fs/appfs/src/appfs_tiny.h b/mkrtos_user/server/fs/appfs/src/appfs_tiny.h new file mode 100644 index 000000000..7a7bc6858 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/appfs_tiny.h @@ -0,0 +1,51 @@ +#pragma once + +struct hw_callback; +typedef struct hw_callback hw_callback_t; +struct fs_info; +typedef struct fs_info fs_info_t; +#define APPFS_MAGIC "APPFS" +#define APPFS_VERSION 1 + +typedef struct fs_info_save +{ + char magic[8]; //!< APPFS + int version; //!< 版本号 + int blockinfo_inx; //!< 块信息起始块号 + int blockinfo_nr; //!< 块信息块数量 + + int dirinfo_inx; //!< 目录信息起始块号 + int dirinfo_nr; //!< 目录信息块数量 + + int block_inx; //!< 块起始块号 + int block_nr; //!< 块数量 + + int block_size; //!< 块大小 +} fs_info_save_t; + +typedef struct fs_info +{ + fs_info_save_t save; //!< 保存的文件系统信息 +} fs_info_t; + +#define APPFS_FILE_NAME_MAX 52 //!< 文件名最大长度 + +typedef struct dir_info +{ + char name[APPFS_FILE_NAME_MAX]; //!< 文件名 + int block_inx; //!< 文件起始块号 + int size; //!< 文件大小 + int ref; //!< 引用计数 +} dir_info_t; +static inline unsigned long appfs_tiny_get_file_addr(fs_info_t *fs, const dir_info_t *info) +{ + if (fs == NULL) + { + return 0; + } + return info->block_inx * fs->save.block_size + (unsigned long)fs; //!< 计算文件地址 +} + +fs_info_t *appfs_tiny_get_form_addr(void *addr); +const dir_info_t *appfs_tiny_find_file_by_name(fs_info_t *info, const char *name); +unsigned long appfs_tiny_find_file_addr_by_name(fs_info_t *info, const char *name, unsigned long *size); diff --git a/mkrtos_user/server/fs/appfs/src/build.sh b/mkrtos_user/server/fs/appfs/src/build.sh new file mode 100755 index 000000000..47b371b2e --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/build.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +set -e -x +cmake -G Ninja -B build . +cd build && ninja diff --git a/mkrtos_user/server/fs/appfs/src/hw_block_sim.c b/mkrtos_user/server/fs/appfs/src/hw_block_sim.c new file mode 100644 index 000000000..e8e1bcc15 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/hw_block_sim.c @@ -0,0 +1,154 @@ +#include "appfs.h" +#include "hw_block_test.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BLOCK_SIZE 512 //!< 块大小 + +static int block_mem_size = -1; +static int block_nr = -1; +static uint8_t *block; // 32MB +static uint8_t block_buf[BLOCK_SIZE]; //!< 块缓冲区 + +static int hw_erase_block(fs_info_t *fs, int block_inx) +{ + assert(fs); + assert(block_inx < fs->save.block_nr); + memset(block + block_inx * fs->save.block_size, 0xff, fs->save.block_size); + return 0; +} +static int hw_write_and_erase_block(fs_info_t *fs, int block_inx, void *buf, int size) +{ + assert(fs); + assert(buf); + assert(block_inx < fs->save.block_nr); + memset(block + block_inx * fs->save.block_size, 0xff, fs->save.block_size); + memcpy(block + block_inx * fs->save.block_size, buf, size); + return 0; +} +static int hw_read_block(fs_info_t *fs, int block_inx, void *buf, int size) +{ + assert(fs); + assert(buf); + assert(block_inx < fs->save.block_nr); + memcpy(buf, block + block_inx * fs->save.block_size, size); + return 0; +} +static int hw_init_fs_for_block(fs_info_t *fs) +{ + fs->save.block_size = BLOCK_SIZE; + fs->save.block_nr = block_nr; + fs->dev_fd = -1; // 模拟设备没有文件描述符 + fs->mem_addr = (unsigned long)block; + fs->buf = (uint32_t *)block_buf; + + return 0; +} +int hw_read_file_all(const char *file_path, void **ret_ptr) +{ + FILE *file = fopen(file_path, "rbw+"); // 以二进制模式打开文件 + if (!file) + { + perror("fopen"); + return EXIT_FAILURE; + } + + // 寻求文件末尾以确定文件大小 + fseek(file, 0, SEEK_END); + long file_size = ftell(file); + fseek(file, 0, SEEK_SET); + + if (file_size < 0) + { + perror("ftell"); + fclose(file); + return EXIT_FAILURE; + } + + // 分配内存来存储文件内容 + char *content = (char *)malloc((size_t)file_size); + if (!content) + { + perror("malloc"); + fclose(file); + return EXIT_FAILURE; + } + + // 读取文件内容到内存中 + size_t bytes_read = fread(content, 1, (size_t)file_size, file); + if (bytes_read != (size_t)file_size) + { + perror("fread"); + free(content); + fclose(file); + return EXIT_FAILURE; + } + *ret_ptr = content; + fclose(file); + return file_size; +} + +int hw_offload_img_from_file(const char *name) +{ + int ret; + void *ptr_file_content; + + ret = hw_read_file_all(name, &ptr_file_content); + if (ret < 0) + { + return ret; + } + if (ret != block_mem_size) + { + free(ptr_file_content); + printf("input img size is error.\n"); + return -1; + } + memcpy(block, ptr_file_content, block_mem_size); + free(ptr_file_content); + return 0; +} +int hw_dump_to_file(const char *name) +{ + int ret; + int fd = open(name, O_RDWR | O_CREAT, 0777); + if (fd < 0) + { + return fd; + } + ret = write(fd, block, block_mem_size); + if (ret < 0) + { + return ret; + } + close(fd); + return 0; +} +int hw_init_block_sim_test(fs_info_t *fs, int img_size) +{ + fs->cb.hw_erase_block = hw_erase_block; + fs->cb.hw_init_fs_for_block = hw_init_fs_for_block; + fs->cb.hw_read_block = hw_read_block; + fs->cb.hw_write_and_erase_block = hw_write_and_erase_block; + + block = (void *)malloc(img_size); + if (block == NULL) + { + return -ENOMEM; + } + block_mem_size = (img_size / BLOCK_SIZE) * BLOCK_SIZE; + block_nr = img_size / BLOCK_SIZE; + printf("img size is %d, block nr is %d.\n", block_mem_size, block_nr); + // 使用内存模拟一个块设备 + memset(block, 0, block_mem_size); + return 0; +} diff --git a/mkrtos_user/server/fs/appfs/src/hw_block_sim.h b/mkrtos_user/server/fs/appfs/src/hw_block_sim.h new file mode 100644 index 000000000..f9d109dc7 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/hw_block_sim.h @@ -0,0 +1,6 @@ +#pragma once +#include "appfs.h" +int hw_dump_to_file(const char *name); +int hw_init_block_sim_test(fs_info_t *fs, int img_size); +int hw_read_file_all(const char *file_path, void **ret_ptr); +int hw_offload_img_from_file(const char *name); diff --git a/mkrtos_user/server/fs/appfs/src/hw_block_test.c b/mkrtos_user/server/fs/appfs/src/hw_block_test.c new file mode 100644 index 000000000..edf2aadc1 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/hw_block_test.c @@ -0,0 +1,58 @@ +#include "hw_block_test.h" +#include "appfs.h" +#include +#include +#include + +#define BLOCK_SIZE 512 //!< 块大小 +#define BLOCK_NR 1792 //!< 块数量 + +#define BOCK_MEM_SIZE (BLOCK_NR * BLOCK_SIZE) //!< 块设备内存大小 +static uint8_t block[BOCK_MEM_SIZE]; // 32MB +static uint8_t block_buf[BLOCK_SIZE]; //!< 块缓冲区 + +static int hw_erase_block(fs_info_t *fs, int block_inx) +{ + assert(fs); + assert(block_inx < fs->save.block_nr); + memset(block + block_inx * fs->save.block_size, 0xff, fs->save.block_size); + return 0; +} +static int hw_write_and_erase_block(fs_info_t *fs, int block_inx, void *buf, int size) +{ + assert(fs); + assert(buf); + assert(block_inx < fs->save.block_nr); + memset(block + block_inx * fs->save.block_size, 0xff, fs->save.block_size); + memcpy(block + block_inx * fs->save.block_size, buf, size); + return 0; +} +static int hw_read_block(fs_info_t *fs, int block_inx, void *buf, int size) +{ + assert(fs); + assert(buf); + assert(block_inx < fs->save.block_nr); + memcpy(buf, block + block_inx * fs->save.block_size, size); + return 0; +} +static int hw_init_fs_for_block(fs_info_t *fs) +{ + fs->save.block_size = BLOCK_SIZE; + fs->save.block_nr = BLOCK_NR; + fs->dev_fd = -1; // 模拟设备没有文件描述符 + fs->mem_addr = (unsigned long)block; + fs->buf = (uint32_t *)block_buf; + + return 0; +} +int hw_init_block_test(fs_info_t *fs) +{ + fs->cb.hw_erase_block = hw_erase_block; + fs->cb.hw_init_fs_for_block = hw_init_fs_for_block; + fs->cb.hw_read_block = hw_read_block; + fs->cb.hw_write_and_erase_block = hw_write_and_erase_block; + + // 使用内存模拟一个块设备 + memset(block, 0, BOCK_MEM_SIZE); + return 0; +} diff --git a/mkrtos_user/server/fs/appfs/src/hw_block_test.h b/mkrtos_user/server/fs/appfs/src/hw_block_test.h new file mode 100644 index 000000000..029837fa3 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/hw_block_test.h @@ -0,0 +1,5 @@ +#pragma once + +#include "appfs.h" + +int hw_init_block_test(fs_info_t *fs); diff --git a/mkrtos_user/server/fs/appfs/src/main.c b/mkrtos_user/server/fs/appfs/src/main.c new file mode 100644 index 000000000..806679d0d --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/main.c @@ -0,0 +1,217 @@ +#include "appfs.h" +#include "hw_block_sim.h" +#include "test.h" +#include +#include +#include +#include +#include +#include +#include +#include +#define APPFS_FILE_NR 128 + +static fs_info_t fs; +static char *pack_path; +static char *output_path; + +static int read_file_all(const char *file_path, void **ret_ptr) +{ + FILE *file = fopen(file_path, "rb"); // 以二进制模式打开文件 + if (!file) + { + perror("fopen"); + return EXIT_FAILURE; + } + + // 寻求文件末尾以确定文件大小 + fseek(file, 0, SEEK_END); + long file_size = ftell(file); + fseek(file, 0, SEEK_SET); + + if (file_size < 0) + { + perror("ftell"); + fclose(file); + return EXIT_FAILURE; + } + + // 分配内存来存储文件内容 + char *content = (char *)malloc((size_t)file_size); + if (!content) + { + perror("malloc"); + fclose(file); + return EXIT_FAILURE; + } + + // 读取文件内容到内存中 + size_t bytes_read = fread(content, 1, (size_t)file_size, file); + if (bytes_read != (size_t)file_size) + { + perror("fread"); + free(content); + fclose(file); + return EXIT_FAILURE; + } + *ret_ptr = content; + fclose(file); + return file_size; +} + +int read_files_and_write_appfs(const char *path) +{ + DIR *dir; + struct dirent *entry; + struct stat info; + int ret; + long total_size = 0; + + // 打开目录 + dir = opendir(path); + if (dir == NULL) + { + perror("opendir"); + return errno; + } + + // 读取目录中的每一个条目 + while ((entry = readdir(dir)) != NULL) + { + // 忽略"."和".."目录 + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + { + continue; + } + + // 构建完整的文件路径 + char full_path[1024]; + snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name); + + // 获取文件信息 + if (stat(full_path, &info) != 0) + { + printf("pack fail:%s\n", full_path); + continue; + } + + if (S_ISREG(info.st_mode)) + { + printf("%s %fMB\n", full_path, (float)info.st_size / 1024.0f / 1024.0f); + ret = appfs_create_file(&fs, entry->d_name, info.st_size); + if (ret < 0) + { + printf("create file error.\n"); + return -1; + } + void *file_ptr; + ret = read_file_all(full_path, &file_ptr); + if (ret < 0) + { + printf("read file error.\n"); + return ret; + } + ret = appfs_write_file(&fs, entry->d_name, file_ptr, info.st_size, 0); + if (ret < 0) + { + free(file_ptr); + printf("write file error.\n"); + return ret; + } + free(file_ptr); + total_size += info.st_size; + } + } + + // 关闭目录 + closedir(dir); + printf("file total size:%fMB\n", ((float)total_size / 1024.0f / 1024.0f)); + return 0; +} +static char *debug_img_path; +static int gen_img_size = -1; +static int appfs_gen_init(void) +{ + int ret; + + ret = hw_init_block_sim_test(&fs, gen_img_size); + if (ret < 0) + { + return ret; + } + if (debug_img_path == NULL) + { + appfs_format(&fs, APPFS_FILE_NR); + } + ret = appfs_init(&fs); + return ret; +} +int main(int argc, char *argv[]) +{ + int opt; + int ret = 0; + // appfs_test_func(); + + while ((opt = getopt(argc, argv, "ti:g:o:s:")) != -1) + { + switch (opt) + { + case 't': + printf("test mode.\n"); + appfs_test_func(); + return 0; + case 'i': + printf("input img:%s\n", optarg); + debug_img_path = optarg; + break; + case 'g': + printf("package path is %s\n", optarg); + pack_path = optarg; + break; + case 'o': + printf("output file is %s\n", optarg); + output_path = optarg; + break; + case 's': + gen_img_size = atol(optarg); + printf("img size is %d.\n", gen_img_size); + break; + default: + printf("Unknown option\n"); + break; + } + } + if (!pack_path) + { + printf("pack patch is not set.\n"); + return -1; + } + if (gen_img_size < 0) + { + printf("output img size don't set."); + return -1; + } + if (output_path == NULL) + { + output_path = "a.img"; + } + if (appfs_gen_init() < 0) + { + printf("init fail.\n"); + return -1; + } + + ret = read_files_and_write_appfs(pack_path); + if (ret < 0) + { + printf("appfs create failed\n"); + return ret; + } + ret = hw_dump_to_file(output_path); + if (ret < 0) + { + printf("appfs dump failed\n"); + return ret; + } + return 0; +} diff --git a/mkrtos_user/server/fs/appfs/src/open.c b/mkrtos_user/server/fs/appfs/src/open.c new file mode 100644 index 000000000..0f093b4cf --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/open.c @@ -0,0 +1,476 @@ +#include "open.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "appfs.h" + +static fs_info_t *fs; + +#define DIR_INFO_CACHE_NR 32 +typedef struct dir_info_cache +{ + const dir_info_t *info; + int ref; +} dir_info_cache_t; + +static dir_info_cache_t dir_info_cache_list[DIR_INFO_CACHE_NR]; +static int dir_info_cache_get(const dir_info_t *info) +{ + for (int i = 0; i < DIR_INFO_CACHE_NR; i++) + { + if (dir_info_cache_list[i].info == NULL) + { + continue; + } + if (strcmp(dir_info_cache_list[i].info->name, info->name) == 0) + { + dir_info_cache_list[i].ref++; + return i; + } + } + for (int i = 0; i < DIR_INFO_CACHE_NR; i++) + { + if (dir_info_cache_list[i].info == NULL) + { + dir_info_cache_list[i].info = info; + dir_info_cache_list[i].ref++; + return i; + } + } + return -1; +} +static void dir_info_cache_put(const dir_info_t *info) +{ + assert(info); + for (int i = 0; i < DIR_INFO_CACHE_NR; i++) + { + if (dir_info_cache_list[i].info == NULL) + { + continue; + } + if (strcmp(dir_info_cache_list[i].info->name, info->name) == 0) + { + dir_info_cache_list[i].ref--; + if (dir_info_cache_list[i].ref == 0) + { + dir_info_cache_list[i].info = NULL; + return; + } + } + } +} +enum appfs_type +{ + APPFS_FILE_TYPE, + APPFS_DIR_TYPE, +}; +#define DIR_INFO_NR 32 +typedef struct appfs_file +{ + int dir_info_fd; //!< dirinfo cache fd. + uint8_t flags; //!< 文件操作的flags + int offset; //!< 文件操作的偏移 + enum appfs_type type; //!< 类型 + uint8_t used; + // int pid; //!< 属于哪个进程 + // pthread_mutex_t mutex; //!< 锁 +} appfs_file_t; + +static appfs_file_t appfs_files[DIR_INFO_NR]; + +static appfs_file_t *appfs_get_file(int fd); + +static int appfs_file_alloc(const dir_info_t *file, int flags, enum appfs_type type) +{ + int dir_info_fd = -1; + + if (file != NULL) + { + dir_info_fd = dir_info_cache_get(file); + if (dir_info_fd < 0) + { + printf("dir_info cache is full.\n"); + return dir_info_fd; + } + } + + for (int i = 0; i < DIR_INFO_NR; i++) + { + if (appfs_files[i].used == 0) + { + // 不存在则增加一个新的 + appfs_files[i].dir_info_fd = dir_info_fd; + appfs_files[i].flags = flags; + // appfs_files[i].pid = pid; + appfs_files[i].offset = 0; + appfs_files[i].type = type; + appfs_files[i].used = 1; + // pthread_mutex_init(&appfs_files[i].mutex, NULL); + return i; + } + } + return -1; +} + +static appfs_file_t *appfs_get_file(int fd) +{ + if (fd < 0 || fd >= DIR_INFO_NR) + { + return NULL; + } + if (appfs_files[fd].used == 0) + { + return NULL; + } + return appfs_files + fd; +} + +int appfs_open(const char *name, int flags, int mode) +{ + int ret; + const dir_info_t *file; + int fd; + enum appfs_type type; + + // appfs只有根目录一级,所以打开目录只会有根目录 + if (name[0] == '\0' || (name[0] == '/' && name[1] == '\0')) + { + type = APPFS_DIR_TYPE; + } + else + { + type = APPFS_FILE_TYPE; + } + if (type == APPFS_FILE_TYPE) + { + if (flags & O_CREAT) + { + ret = appfs_create_file(fs, name, 0); + } + if (ret < 0) + { + return ret; + } + file = appfs_find_file_by_name(fs, name); + if (file == NULL) + { + return -ENOENT; + } + fd = appfs_file_alloc(file, flags, type); + if (fd < 0) + { + return fd; + } + } + else if (type == APPFS_DIR_TYPE) + { + fd = appfs_file_alloc(NULL, flags, type); + if (fd < 0) + { + return fd; + } + } + return fd; +} +int appfs_write(int fd, void *data, int len) +{ + appfs_file_t *file = appfs_get_file(fd); + + if (!file || file->used == 0) + { + return -ENOENT; + } + if (file->type != APPFS_FILE_TYPE) + { + return -EACCES; + } + int ret; + + ret = appfs_write_file(fs, dir_info_cache_list[file->dir_info_fd].info->name, data, len, file->offset); + if (ret < 0) + { + return ret; + } + file->offset += ret; + return ret; +} +int appfs_read(int fd, void *data, int len) +{ + appfs_file_t *file = appfs_get_file(fd); + + if (!file || file->used == 0) + { + return -ENOENT; + } + if (file->type != APPFS_FILE_TYPE) + { + return -EACCES; + } + int ret; + + ret = appfs_read_file(fs, dir_info_cache_list[file->dir_info_fd].info->name, data, len, file->offset); + if (ret < 0) + { + return ret; + } + file->offset += ret; + return ret; +} + +int appfs_ioctl(int fd, unsigned long cmd, unsigned long arg) +{ + appfs_file_t *file = appfs_get_file(fd); + int ret = 0; + + if (!file || file->used == 0) + { + return -ENOENT; + } + if (file->type != APPFS_FILE_TYPE) + { + return -EACCES; + } + switch (cmd) + { + case APPFS_IOCTOL_GET_ACCESS_ADDR: + { + appfs_ioctl_arg_t *fs_arg = (void *)arg; + + fs_arg->addr = appfs_get_file_addr(fs, dir_info_cache_list[file->dir_info_fd].info); + } + break; + default: + ret = -ENOSYS; + break; + } + return ret; +} +static int appfs_get_file_dir_size(appfs_file_t *file) +{ + switch (file->type) + { + case APPFS_DIR_TYPE: + return appfs_get_file_size(dir_info_cache_list[file->dir_info_fd].info); + case APPFS_FILE_TYPE: + return appfs_get_dir_info_size(fs); + default: + assert(0); + } + return -1; +} +int appfs_lseek(int fd, int offset, unsigned long whence) +{ + appfs_file_t *file = appfs_get_file(fd); + int new_offs = 0; + int old_offs = 0; + + if (!file || file->used == 0) + { + return -ENOENT; + } + old_offs = file->offset; + switch (whence) + { + case SEEK_SET: + new_offs = offset; + break; + case SEEK_END: + { + new_offs = appfs_get_file_dir_size(file) + offset; + } + break; + case SEEK_CUR: + { + new_offs = offset + file->offset; + } + break; + default: + return -EINVAL; + } + if (new_offs > appfs_get_file_dir_size(file)) + { + if (file->type == APPFS_FILE_TYPE) + { + int ret; + + ret = appfs_file_resize_raw(fs, dir_info_cache_list[file->dir_info_fd].info, new_offs); + if (ret < 0) + { + return ret; + } + } + else + { + new_offs = appfs_get_file_dir_size(file); + } + } + if (new_offs < 0) + { + new_offs = 0; + } + if (file->type == APPFS_DIR_TYPE) + { + const dir_info_t *new_dir_info; + + new_dir_info = appfs_dir_info_get_index(fs, new_offs); + if (new_dir_info == NULL) + { + return -ENOENT; + } + if (file->dir_info_fd != -1) + { + dir_info_cache_put(dir_info_cache_list[file->dir_info_fd].info); + } + dir_info_cache_get(new_dir_info); + } + file->offset = new_offs; + + return old_offs; +} +int appfs_truncate(int fd, off_t length) +{ + appfs_file_t *file = appfs_get_file(fd); + int ret; + + if (!file || file->used == 0) + { + return -ENOENT; + } + if (file->type != APPFS_FILE_TYPE) + { + return -EACCES; + } + ret = appfs_file_resize_raw(fs, dir_info_cache_list[file->dir_info_fd].info, length); + return ret; +} +int appfs_stat(int fd, struct stat *st) +{ + appfs_file_t *file = appfs_get_file(fd); + + if (!file || file->used == 0) + { + return -ENOENT; + } + if (file->type != APPFS_FILE_TYPE) + { + return -EACCES; + } + st->st_size = dir_info_cache_list[file->dir_info_fd].info->size; + st->st_mode = __S_IFREG; + st->st_nlink = dir_info_cache_list[file->dir_info_fd].info->ref; + return 0; +} +int appfs_close(int fd) +{ + appfs_file_t *file = appfs_get_file(fd); + if (!file || file->used == 0) + { + return -ENOENT; + } + if (file->type == APPFS_FILE_TYPE) + { + dir_info_cache_put(dir_info_cache_list[file->dir_info_fd].info); + } + else + { + if (file->dir_info_fd != -1) + { + dir_info_cache_put(dir_info_cache_list[file->dir_info_fd].info); + } + } + file->dir_info_fd = -1; + // file->pid = -1; + file->flags = 0; + file->offset = 0; + file->used = 0; + file->type = APPFS_FILE_TYPE; + return 0; +} +int appfs_remove(const char *name) +{ + const dir_info_t *file; + int ret; + int dir_info_fd; + + file = appfs_find_file_by_name(fs, name); + if (file == NULL) + { + return -ENOENT; + } + + dir_info_fd = dir_info_cache_get(file); + if (dir_info_cache_list[dir_info_fd].ref == 1) + { + ret = appfs_delete_file(fs, name); + if (ret < 0) + { + return ret; + } + } + dir_info_cache_put(file); + return 0; +} +int appfs_readdir(int fd, struct dirent *_dir) +{ + int ret = -1; + appfs_file_t *file = appfs_get_file(fd); + + if (!file || file->used == 0) + { + return -ENOENT; + } + if (file->type != APPFS_DIR_TYPE) + { + return -EACCES; + } + if (file->dir_info_fd == -1) + { + const dir_info_t *dir_info = appfs_dir_info_get_first(fs); + + ret = dir_info_cache_get(dir_info); + if (ret < 0) + { + return ret; + } + file->dir_info_fd = ret; + } + _dir->d_reclen = sizeof(*_dir); + _dir->d_type = 4; // DT_REG‌; + strncpy(_dir->d_name, dir_info_cache_list[file->dir_info_fd].info->name, sizeof(_dir->d_name)); + _dir->d_name[sizeof(_dir->d_name) - 1] = 0; + + const dir_info_t *next_dir_info = + appfs_dir_info_get_next(fs, dir_info_cache_list[file->dir_info_fd].info); + if (next_dir_info == NULL) + { + return -ENOENT; + } + dir_info_cache_put(dir_info_cache_list[file->dir_info_fd].info); + ret = dir_info_cache_get(next_dir_info); + if (ret < 0) + { + return ret; + } + + file->dir_info_fd = ret; + file->offset++; + return sizeof(*_dir); +} + +int appfs_open_init(fs_info_t *fs_info) +{ + for (int i = 0; i < DIR_INFO_NR; i++) + { + appfs_files[i].dir_info_fd = -1; + } + fs = fs_info; + return 0; +} diff --git a/mkrtos_user/server/fs/appfs/src/open.h b/mkrtos_user/server/fs/appfs/src/open.h new file mode 100644 index 000000000..3b09c137e --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/open.h @@ -0,0 +1,26 @@ +#pragma once +#include +#include +#include "appfs.h" +enum appfs_ioctl_cmd_op +{ + APPFS_IOCTOL_GET_ACCESS_ADDR, +}; +typedef struct appfs_ioctl_arg +{ + unsigned long addr; +} appfs_ioctl_arg_t; + +int appfs_open(const char *name, int flags, int mode); +int appfs_write(int fd, void *data, int len); +int appfs_read(int fd, void *data, int len); +int appfs_ioctl(int fd, unsigned long cmd, unsigned long arg); +int appfs_lseek(int fd, int offset, unsigned long whence); +int appfs_stat(int fd, struct stat *st); +int appfs_close(int fd); +int appfs_remove(const char *name); +int appfs_truncate(int fd, off_t length); + +int appfs_readdir(int fd, struct dirent *_dir); + +int appfs_open_init(fs_info_t *fs_info); diff --git a/mkrtos_user/server/fs/appfs/src/test.c b/mkrtos_user/server/fs/appfs/src/test.c new file mode 100644 index 000000000..86ba1240b --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/test.c @@ -0,0 +1,372 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "appfs.h" +#include "hw_block_test.h" +#include "open.h" +static fs_info_t fs; +#define APPFS_FILE_NR 128 + +uint8_t test_data[1234]; +uint8_t test_data2[1234]; + +int appfs_test(void) +{ + int ret; + ret = appfs_create_file(&fs, "test.txt", 1234); + if (ret < 0) + { + printf("create file failed!\n"); + return ret; + } + for (int i = 0; i < 1234; i++) + { + test_data[i] = i; + } + ret = appfs_write_file(&fs, "test.txt", test_data, sizeof(test_data), 0); + if (ret < 0) + { + printf("write file failed!\n"); + return ret; + } + memset(test_data2, 0, sizeof(test_data2)); + ret = appfs_read_file(&fs, "test.txt", test_data2, sizeof(test_data2), 0); + if (memcmp(test_data, test_data2, sizeof(test_data)) != 0) + { + printf("read file failed!\n"); + return ret; + } + ret = appfs_delete_file(&fs, "test.txt"); + assert(ret == 0); + return ret; +} +/** + * @brief 测试多文件 + */ +int appfs_test2(void) +{ + int ret; + char name[APPFS_FILE_NR][APPFS_FILE_NAME_MAX]; + for (int i = 0; i < 1234; i++) + { + test_data[i] = i; + } + for (int i = 0; i < APPFS_FILE_NR; i++) + { + snprintf(name[i], APPFS_FILE_NAME_MAX, "test%d.txt", i); + ret = appfs_create_file(&fs, name[i], 1234); + assert(ret == 0); + appfs_write_file(&fs, name[i], test_data, sizeof(test_data), 0); + } + for (int i = 0; i < APPFS_FILE_NR; i++) + { + ret = appfs_read_file(&fs, name[rand() % APPFS_FILE_NR], test_data2, sizeof(test_data2), 0); + assert(ret == sizeof(test_data2)); + if (memcmp(test_data, test_data2, sizeof(test_data)) != 0) + { + printf("read file failed!\n"); + return ret; + } + } + + // 删除文件 + for (int i = 0; i < APPFS_FILE_NR; i++) + { + ret = appfs_delete_file(&fs, name[i]); + assert(ret == 0); + } + + return ret; +} +int appfs_test_create_mult_file_and_delete_mid_file(void) +{ + int ret; + char name[APPFS_FILE_NR][APPFS_FILE_NAME_MAX]; + for (int i = 0; i < 1234; i++) + { + test_data[i] = i; + } + for (int i = 0; i < APPFS_FILE_NR; i++) + { + snprintf(name[i], APPFS_FILE_NAME_MAX, "test%d.txt", i); + ret = appfs_create_file(&fs, name[i], 1234); + assert(ret == 0); + appfs_write_file(&fs, name[i], test_data, sizeof(test_data), 0); + } + for (int i = 0; i < APPFS_FILE_NR; i++) + { + ret = appfs_read_file(&fs, name[rand() % APPFS_FILE_NR], test_data2, sizeof(test_data2), 0); + assert(ret == sizeof(test_data2)); + if (memcmp(test_data, test_data2, sizeof(test_data)) != 0) + { + printf("read file failed!\n"); + return ret; + } + } + // 删除部分文件 + for (int i = 0; i < APPFS_FILE_NR; i++) + { + if (i % 2 == 0) + { + ret = appfs_delete_file(&fs, name[i]); + assert(ret == 0); + } + } + // 再次读取文件 + for (int i = 0; i < APPFS_FILE_NR; i++) + { + if (i % 2 == 1) + { + ret = appfs_read_file(&fs, name[i], test_data2, sizeof(test_data2), 0); + assert(ret == sizeof(test_data2)); + if (memcmp(test_data, test_data2, sizeof(test_data)) != 0) + { + printf("read file failed!\n"); + return ret; + } + } + } + // 删除剩余文件 + for (int i = 0; i < APPFS_FILE_NR; i++) + { + if (i % 2 == 1) + { + ret = appfs_delete_file(&fs, name[i]); + assert(ret == 0); + } + } + return 0; +} +/** + * @brief 创建多个文件并排列文件 + * + */ +int appfs_create_mult_file_and_arrange_file(void) +{ + int ret; + char name[APPFS_FILE_NR][APPFS_FILE_NAME_MAX]; + for (int i = 0; i < 1234; i++) + { + test_data[i] = i; + } + for (int i = 0; i < APPFS_FILE_NR; i++) + { + snprintf(name[i], APPFS_FILE_NAME_MAX, "test%d.txt", i); + ret = appfs_create_file(&fs, name[i], 1234); + assert(ret == 0); + appfs_write_file(&fs, name[i], test_data, sizeof(test_data), 0); + } + // 删除部分文件 + for (int i = 0; i < APPFS_FILE_NR; i++) + { + if (i % 2 == 0) + { + ret = appfs_delete_file(&fs, name[i]); + assert(ret == 0); + } + } + // 整理文件 + ret = appfs_arrange_files(&fs); + assert(ret == 0); + // 读取文件并测试 + for (int i = 0; i < APPFS_FILE_NR; i++) + { + if (i % 2 == 1) + { + ret = appfs_read_file(&fs, name[i], test_data2, sizeof(test_data2), 0); + assert(ret == sizeof(test_data2)); + if (memcmp(test_data, test_data2, sizeof(test_data)) != 0) + { + printf("read file failed!\n"); + return ret; + } + } + } + // 删除剩余文件 + for (int i = 0; i < APPFS_FILE_NR; i++) + { + if (i % 2 == 1) + { + ret = appfs_delete_file(&fs, name[i]); + assert(ret == 0); + } + } + return 0; +} +void appfs_test_offset_write(void) +{ + int ret; + char name[APPFS_FILE_NAME_MAX]; + + memset(test_data, 0xff, sizeof(test_data)); + strcpy(name, "test0.txt"); + ret = appfs_create_file(&fs, name, 1234); + assert(ret == 0); + ret = appfs_write_file(&fs, name, test_data, sizeof(test_data), 0); + assert(ret == sizeof(test_data)); + memset(test_data, 0x00, 512 + 1); + ret = appfs_write_file(&fs, name, test_data, sizeof(test_data), 513); + assert(ret == (ROUND_UP(sizeof(test_data), fs.save.block_size) * fs.save.block_size - (512 + 1))); + + memset(test_data, 0xff, sizeof(test_data)); + ret = appfs_write_file(&fs, name, test_data, sizeof(test_data), 0); + assert(ret == sizeof(test_data)); + + memset(test_data + 513, 0x00, 512); + ret = appfs_write_file(&fs, name, test_data + 513, 512, 513); + assert(ret == (512)); + + ret = appfs_read_file(&fs, name, test_data2, sizeof(test_data), 0); + assert(ret >= 0); + if (memcmp(test_data, test_data2, sizeof(test_data)) != 0) + { + assert(0); + } + ret = appfs_read_file(&fs, name, test_data2, 512, 513); + assert(ret >= 0); + for (int i = 0; i < 512; i++) + { + if (test_data2[i] != 0x00) + { + assert(0); + } + } + + for (int i = 0; i < 32; i++) + { + int write_inx; + + write_inx = rand() % sizeof(test_data); + ret = appfs_write_file(&fs, name, "1", 1, write_inx); + assert(ret == 1); + char read_char; + ret = appfs_read_file(&fs, name, &read_char, 1, write_inx); + assert(ret == 1); + assert(read_char == '1'); + } + + appfs_delete_file(&fs, name); +} + +void appfs_posix_test(void) +{ + int fd; + int ret; + char file_name[32]; + + ret = appfs_open_init(&fs); + assert(ret >= 0); + + for (int i = 0; i < APPFS_FILE_NR; i++) + { + snprintf(file_name, sizeof(file_name), "%d.bin", i); + + // printf("%d\n", i); + fd = appfs_open(file_name, O_CREAT, 0777); + assert(fd >= 0); + ret = appfs_truncate(fd, sizeof(test_data)); + assert(ret >= 0); + memset(test_data, 0x11, sizeof(test_data)); + ret = appfs_write(fd, test_data, sizeof(test_data)); + assert(ret == sizeof(test_data)); + ret = appfs_lseek(fd, 0, SEEK_SET); + assert(ret >= 0); + ret = appfs_read(fd, test_data2, sizeof(test_data2)); + assert(ret == sizeof(test_data2)); + assert(memcmp(test_data2, test_data, sizeof(test_data)) == 0); + appfs_close(fd); + } + + fd = appfs_open("32.bin", O_CREAT, 0777); + assert(fd <= 0); + + for (int i = 0; i < APPFS_FILE_NR; i++) + { + snprintf(file_name, sizeof(file_name), "%d.bin", i); + + appfs_remove(file_name); + } +} +void appfs_posix_readdir_test(void) +{ + int fd; + int ret; + char file_name[32]; + + for (int i = 0; i < APPFS_FILE_NR; i++) + { + snprintf(file_name, sizeof(file_name), "%d.bin", i); + + // printf("%d\n", i); + fd = appfs_open(file_name, O_CREAT, 0777); + assert(fd >= 0); + ret = appfs_truncate(fd, sizeof(test_data)); + assert(ret >= 0); + memset(test_data, 0x11, sizeof(test_data)); + ret = appfs_write(fd, test_data, sizeof(test_data)); + assert(ret == sizeof(test_data)); + ret = appfs_lseek(fd, 0, SEEK_SET); + assert(ret >= 0); + ret = appfs_read(fd, test_data2, sizeof(test_data2)); + assert(ret == sizeof(test_data2)); + assert(memcmp(test_data2, test_data, sizeof(test_data)) == 0); + appfs_close(fd); + } + + int dir_fd = appfs_open("", O_RDWR, 0777); + + assert(dir_fd >= 0); + struct dirent _dirent; + while ((ret = appfs_readdir(dir_fd, &_dirent)) >= 0) + { + printf("%s\n", _dirent.d_name); + } + printf("\n"); + assert(appfs_lseek(dir_fd, 0, SEEK_SET) >= 0); + while ((ret = appfs_readdir(dir_fd, &_dirent)) >= 0) + { + printf("%s\n", _dirent.d_name); + } + printf("\n"); + assert(appfs_lseek(dir_fd, 120, SEEK_SET) >= 0); + while ((ret = appfs_readdir(dir_fd, &_dirent)) >= 0) + { + printf("%s\n", _dirent.d_name); + } + appfs_close(dir_fd); + + for (int i = 0; i < APPFS_FILE_NR; i++) + { + snprintf(file_name, sizeof(file_name), "%d.bin", i); + + appfs_remove(file_name); + } +} +void appfs_test_func(void) +{ + int ret; + printf("appfs testing.\n\n"); + hw_init_block_test(&fs); + appfs_format(&fs, APPFS_FILE_NR); + appfs_init(&fs); + appfs_test_offset_write(); + + appfs_print_files(&fs); + appfs_test(); + appfs_print_files(&fs); + appfs_test2(); + appfs_print_files(&fs); + appfs_test_create_mult_file_and_delete_mid_file(); + appfs_print_files(&fs); + appfs_create_mult_file_and_arrange_file(); + appfs_print_files(&fs); + appfs_posix_test(); + appfs_print_files(&fs); + appfs_posix_readdir_test(); + appfs_print_files(&fs); +} \ No newline at end of file diff --git a/mkrtos_user/server/fs/appfs/src/test.h b/mkrtos_user/server/fs/appfs/src/test.h new file mode 100644 index 000000000..ad20784df --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/test.h @@ -0,0 +1,4 @@ +#pragma once + +void appfs_test_func(void); + diff --git a/mkrtos_user/server/fs/appfs/src/test.sh b/mkrtos_user/server/fs/appfs/src/test.sh new file mode 100755 index 000000000..fa50fae88 --- /dev/null +++ b/mkrtos_user/server/fs/appfs/src/test.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +./build/appfs -g test -o out.img +./build/appfs -t + diff --git a/mkrtos_user/server/fs/appfs/src/test/1.bin b/mkrtos_user/server/fs/appfs/src/test/1.bin new file mode 100644 index 000000000..b4fdf7944 Binary files /dev/null and b/mkrtos_user/server/fs/appfs/src/test/1.bin differ diff --git a/mkrtos_user/server/fs/appfs/src/test/2.bin b/mkrtos_user/server/fs/appfs/src/test/2.bin new file mode 100644 index 000000000..2d48733ca Binary files /dev/null and b/mkrtos_user/server/fs/appfs/src/test/2.bin differ diff --git a/mkrtos_user/server/fs/appfs/src/test/3.bin b/mkrtos_user/server/fs/appfs/src/test/3.bin new file mode 100644 index 000000000..ddc10ddcf Binary files /dev/null and b/mkrtos_user/server/fs/appfs/src/test/3.bin differ diff --git a/mkrtos_user/server/fs/appfs/src/test/4.bin b/mkrtos_user/server/fs/appfs/src/test/4.bin new file mode 100644 index 000000000..46777e496 Binary files /dev/null and b/mkrtos_user/server/fs/appfs/src/test/4.bin differ diff --git a/mkrtos_user/server/fs/appfs/src/test/5.bin b/mkrtos_user/server/fs/appfs/src/test/5.bin new file mode 100644 index 000000000..bf6860ccc Binary files /dev/null and b/mkrtos_user/server/fs/appfs/src/test/5.bin differ diff --git a/mkrtos_user/server/fs/appfs/src/test/6.bin b/mkrtos_user/server/fs/appfs/src/test/6.bin new file mode 100644 index 000000000..353e7cb46 Binary files /dev/null and b/mkrtos_user/server/fs/appfs/src/test/6.bin differ diff --git a/mkrtos_user/server/fs/fatfs/CMakeLists.txt b/mkrtos_user/server/fs/fatfs/CMakeLists.txt index 79cfc3a6e..382835937 100644 --- a/mkrtos_user/server/fs/fatfs/CMakeLists.txt +++ b/mkrtos_user/server/fs/fatfs/CMakeLists.txt @@ -68,8 +68,8 @@ add_custom_target( mkdir -p ${CMAKE_SOURCE_DIR}/build/output/cpio COMMAND cp fatfs.bin ${CMAKE_SOURCE_DIR}/build/output/cpio/fatfs - COMMAND - cp fatfs.elf ${CMAKE_SOURCE_DIR}/build/output/cpio/fatfs.elf + # COMMAND + # cp fatfs.elf ${CMAKE_SOURCE_DIR}/build/output/cpio/fatfs.elf ) add_dependencies(fatfs_dump fatfs.elf) diff --git a/mkrtos_user/server/init/CMakeLists.txt b/mkrtos_user/server/init/CMakeLists.txt index 07c26e8ff..c6eb5b3f0 100644 --- a/mkrtos_user/server/init/CMakeLists.txt +++ b/mkrtos_user/server/init/CMakeLists.txt @@ -27,6 +27,7 @@ target_link_libraries(init.elf util cutest --no-whole-archive + appfs_tiny ${GCC_LIB_PATH}/libgcc.a ) target_include_directories( @@ -71,4 +72,4 @@ if ((DEFINED CONFIG_ELF_LAUNCH) AND (CONFIG_ELF_LAUNCH STREQUAL "y")) add_dependencies(init_dump_elf init_dump) endif() add_dependencies(init_dump init.elf) -add_dependencies(init.elf libc_be util) +add_dependencies(init.elf libc_be util appfs_tiny) diff --git a/mkrtos_user/server/init/src/parse_cfg.c b/mkrtos_user/server/init/src/parse_cfg.c index 7f7c5776c..12b4ca5fe 100644 --- a/mkrtos_user/server/init/src/parse_cfg.c +++ b/mkrtos_user/server/init/src/parse_cfg.c @@ -24,6 +24,7 @@ #include #include +#include static char cmd_line[CMD_LEN]; //!< 命令行 static char cmd_params[CMD_PARAMS_CN][CMD_PARAMS_ITEM_LEN]; //!< 参数数组 static int cmd_params_off[CMD_PARAMS_CN]; //!< 参数偏移量 @@ -128,9 +129,17 @@ int parse_cfg(const char *parse_cfg_file_name, uenv_t *env) umword_t size; int type; const char *str; +#if IS_ENABLED(CONFIG_CPIO_SUPPORT) ret = cpio_find_file((umword_t)sys_info.bootfs_start_addr, (umword_t)(-1), parse_cfg_file_name, &size, &type, (umword_t *)&str); - +#else + str = (const char *)appfs_tiny_find_file_addr_by_name(appfs_tiny_get_form_addr((void *)sys_info.bootfs_start_addr), parse_cfg_file_name, &size); + if (str == NULL) + { + ret = -ENOENT; + } + type = 0; +#endif if (ret < 0 || (ret >= 0 && type == 1)) { ret = -ENOENT; diff --git a/mkrtos_user/server/init/src/parse_cfg.h b/mkrtos_user/server/init/src/parse_cfg.h index 946759b83..b546205c6 100644 --- a/mkrtos_user/server/init/src/parse_cfg.h +++ b/mkrtos_user/server/init/src/parse_cfg.h @@ -2,17 +2,17 @@ /** * @file parse_cfg.h * @author ATShining (1358745329@qq.com) - * @brief + * @brief * @version 0.1 * @date 2023-11-28 - * + * * @copyright Copyright (c) 2023 - * + * */ #include -#define CMD_LEN 64 //!< 命令行最大长度 -#define CMD_PARAMS_CN 8 //!< 参数个数 -#define CMD_PARAMS_ITEM_LEN 16 //!< 每个参数的最大长度 +#define CMD_LEN 64 //!< 命令行最大长度 +#define CMD_PARAMS_CN CONFIG_APP_PARAMS_NR //!< 参数个数 +#define CMD_PARAMS_ITEM_LEN 16 //!< 每个参数的最大长度 void parse_cfg_init(void); int parse_cfg(const char *parse_cfg_file_name, uenv_t *env); diff --git a/mkrtos_user/server/net/src/ethernetif.c b/mkrtos_user/server/net/src/ethernetif.c index 102ca72ee..e5608e696 100644 --- a/mkrtos_user/server/net/src/ethernetif.c +++ b/mkrtos_user/server/net/src/ethernetif.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include @@ -64,7 +64,7 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p) int ret; memcpy((void *)send_shm_addr, p->payload, p->len); - ret = net_drv_cli_write(net_drv_hd, send_shm_hd, p->len); + ret = blk_drv_cli_write(net_drv_hd, send_shm_hd, p->len, 0); return ret >= 0 ? ERR_OK : ERR_IF; } diff --git a/mkrtos_user/server/net/src/main.c b/mkrtos_user/server/net/src/main.c index bb621790c..db59274a9 100644 --- a/mkrtos_user/server/net/src/main.c +++ b/mkrtos_user/server/net/src/main.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include umword_t addr; umword_t size; @@ -42,7 +42,7 @@ again: obj_handler_t sem_hd; - if (net_drv_cli_map(net_drv_hd, &sem_hd) < 0) + if (blk_drv_cli_map(net_drv_hd, &sem_hd) < 0) { printf("net drv sem map error.\n"); return -1; @@ -62,7 +62,7 @@ again: { printf("error.\n"); } - int ret = net_drv_cli_read(net_drv_hd, shm_hd); + int ret = blk_drv_cli_read(net_drv_hd, shm_hd, 0, 0); if (ret > 0) { diff --git a/mkrtos_user/user/app/ATSURFF437/nes_simulator/nes.S b/mkrtos_user/user/app/ATSURFF437/nes_simulator/nes.S index f4776cdc2..810b93904 100644 --- a/mkrtos_user/user/app/ATSURFF437/nes_simulator/nes.S +++ b/mkrtos_user/user/app/ATSURFF437/nes_simulator/nes.S @@ -2,7 +2,7 @@ .balign 4 .global nes_rom nes_rom: -.incbin "/Users/zhangzheng/mkrtos-real/mkrtos_user/user/app/ATSURFF437/nes_simulator/nes_rom/超级马里奥兄弟3[MM之神汉化](JP)[ACT](3Mb).nes" +.incbin "/home/zhangzheng/projects/mkrtos-real/mkrtos_user/user/app/ATSURFF437/nes_simulator/nes_rom/超级马里奥兄弟3[MM之神汉化](JP)[ACT](3Mb).nes" .global nes_rom_end nes_rom_end: .long 0 diff --git a/mkrtos_user/user/app/ATSURFF437/nes_simulator/vnes/nes_main.c b/mkrtos_user/user/app/ATSURFF437/nes_simulator/vnes/nes_main.c index 7bf346cf6..c46360856 100644 --- a/mkrtos_user/user/app/ATSURFF437/nes_simulator/vnes/nes_main.c +++ b/mkrtos_user/user/app/ATSURFF437/nes_simulator/vnes/nes_main.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include ////////////////////////////////////////////////////////////////////////////////// @@ -571,7 +571,7 @@ void nes_sound_close(void) void nes_apu_fill_buffer(int samples, u16 *wavebuf) { memcpy((void *)addr, wavebuf, APU_PCMBUF_SIZE * 2); - int ret = snd_drv_cli_write(snd_drv_hd, shm_hd, APU_PCMBUF_SIZE * 2); + int ret = blk_drv_cli_write(snd_drv_hd, shm_hd, APU_PCMBUF_SIZE * 2, 0); if (ret < 0) { printf("snd write error.\n"); diff --git a/mkrtos_user/user/app/binutils-2.33.1/binutils/nm-new.bin b/mkrtos_user/user/app/binutils-2.33.1/binutils/nm-new.bin index 86455b5c5..ea31f2af9 100644 Binary files a/mkrtos_user/user/app/binutils-2.33.1/binutils/nm-new.bin and b/mkrtos_user/user/app/binutils-2.33.1/binutils/nm-new.bin differ diff --git a/mkrtos_user/user/app/binutils-2.33.1/binutils/objcopy.bin b/mkrtos_user/user/app/binutils-2.33.1/binutils/objcopy.bin index afc45b6f7..7b092b31d 100644 Binary files a/mkrtos_user/user/app/binutils-2.33.1/binutils/objcopy.bin and b/mkrtos_user/user/app/binutils-2.33.1/binutils/objcopy.bin differ diff --git a/mkrtos_user/user/app/binutils-2.33.1/binutils/objdump.bin b/mkrtos_user/user/app/binutils-2.33.1/binutils/objdump.bin index abde2e6e9..6c07bf83f 100644 Binary files a/mkrtos_user/user/app/binutils-2.33.1/binutils/objdump.bin and b/mkrtos_user/user/app/binutils-2.33.1/binutils/objdump.bin differ diff --git a/mkrtos_user/user/app/binutils-2.33.1/binutils/readelf.bin b/mkrtos_user/user/app/binutils-2.33.1/binutils/readelf.bin index e8232a9d1..6916617a8 100644 Binary files a/mkrtos_user/user/app/binutils-2.33.1/binutils/readelf.bin and b/mkrtos_user/user/app/binutils-2.33.1/binutils/readelf.bin differ diff --git a/mkrtos_user/user/app/binutils-2.33.1/binutils/size.bin b/mkrtos_user/user/app/binutils-2.33.1/binutils/size.bin index 480d64389..32941423e 100644 Binary files a/mkrtos_user/user/app/binutils-2.33.1/binutils/size.bin and b/mkrtos_user/user/app/binutils-2.33.1/binutils/size.bin differ diff --git a/mkrtos_user/user/app/binutils-2.33.1/build.sh b/mkrtos_user/user/app/binutils-2.33.1/build.sh index df56e8680..b93d728b5 100755 --- a/mkrtos_user/user/app/binutils-2.33.1/build.sh +++ b/mkrtos_user/user/app/binutils-2.33.1/build.sh @@ -1,64 +1,64 @@ -#!/bin/bash -set -x -export PATH=$PATH:$TOOLCHAIN -SCRIPT_DIR=$(dirname "$0") -cd $SCRIPT_DIR +# #!/bin/bash +# set -x +# export PATH=$PATH:$TOOLCHAIN +# SCRIPT_DIR=$(dirname "$0") +# cd $SCRIPT_DIR -make distclean -./configure --target=arm-none-eabi --host=arm-none-eabi -cd bfd -./configure --target=arm-none-eabi --host=arm-none-eabi -make -j72 -cd .. -cd libiberty -./configure --target=arm-none-eabi --host=arm-none-eabi -make -j72 -cd .. -cd libctf -./configure --target=arm-none-eabi --host=arm-none-eabi -make -j72 -cd .. -cd zlib -./configure --target=arm-none-eabi --host=arm-none-eabi -make -j72 -cd intf -./configure --target=arm-none-eabi --host=arm-none-eabi -make -j72 +# make distclean +# ./configure --target=arm-none-eabi --host=arm-none-eabi +# cd bfd +# ./configure --target=arm-none-eabi --host=arm-none-eabi +# make -j72 +# cd .. +# cd libiberty +# ./configure --target=arm-none-eabi --host=arm-none-eabi +# make -j72 +# cd .. +# cd libctf +# ./configure --target=arm-none-eabi --host=arm-none-eabi +# make -j72 +# cd .. +# cd zlib +# ./configure --target=arm-none-eabi --host=arm-none-eabi +# make -j72 +# cd intf +# ./configure --target=arm-none-eabi --host=arm-none-eabi +# make -j72 -cd .. -cd ld -./configure --target=arm-none-eabi --host=arm-none-eabi -make -j72 -arm-none-eabi-objcopy -O binary -S ld-new ld-new.bin -cp ld-new.bin $PROJECT_PATH/build/output/cpio/ld +# cd .. +# cd ld +# ./configure --target=arm-none-eabi --host=arm-none-eabi +# make -j72 +# arm-none-eabi-objcopy -O binary -S ld-new ld-new.bin +# cp ld-new.bin $PROJECT_PATH/build/output/cpio/ld -cd .. -cd opcodes -./configure --target=arm-none-eabi --host=arm-none-eabi -make -j72 +# cd .. +# cd opcodes +# ./configure --target=arm-none-eabi --host=arm-none-eabi +# make -j72 -cd .. -cd binutils -./configure --target=arm-none-eabi --host=arm-none-eabi -make -j72 -arm-none-eabi-objcopy -O binary -S objdump objdump.bin -cp objdump.bin $PROJECT_PATH/build/output/cpio/objdump -arm-none-eabi-objcopy -O binary -S objcopy objcopy.bin -cp objcopy.bin $PROJECT_PATH/build/output/cpio/objcopy -arm-none-eabi-objcopy -O binary -S readelf readelf.bin -cp readelf.bin $PROJECT_PATH/build/output/cpio/readelf -arm-none-eabi-objcopy -O binary -S size size.bin -cp size.bin $PROJECT_PATH/build/output/cpio/size -arm-none-eabi-objcopy -O binary -S nm-new nm-new.bin -cp nm-new.bin $PROJECT_PATH/build/output/cpio/nm +# cd .. +# cd binutils +# ./configure --target=arm-none-eabi --host=arm-none-eabi +# make -j72 +# arm-none-eabi-objcopy -O binary -S objdump objdump.bin +# cp objdump.bin $PROJECT_PATH/build/output/cpio/objdump +# arm-none-eabi-objcopy -O binary -S objcopy objcopy.bin +# cp objcopy.bin $PROJECT_PATH/build/output/cpio/objcopy +# arm-none-eabi-objcopy -O binary -S readelf readelf.bin +# cp readelf.bin $PROJECT_PATH/build/output/cpio/readelf +# arm-none-eabi-objcopy -O binary -S size size.bin +# cp size.bin $PROJECT_PATH/build/output/cpio/size +# arm-none-eabi-objcopy -O binary -S nm-new nm-new.bin +# cp nm-new.bin $PROJECT_PATH/build/output/cpio/nm -cd .. -cd gas -./configure --target=arm-none-eabi --host=arm-none-eabi -make -j72 -arm-none-eabi-objcopy -O binary -S as-new as-new.bin -cp as-new.bin $PROJECT_PATH/build/output/cpio/as +# cd .. +# cd gas +# ./configure --target=arm-none-eabi --host=arm-none-eabi +# make -j72 +# arm-none-eabi-objcopy -O binary -S as-new as-new.bin +# cp as-new.bin $PROJECT_PATH/build/output/cpio/as -# cat config.log \ No newline at end of file +# # cat config.log \ No newline at end of file diff --git a/mkrtos_user/user/app/binutils-2.33.1/gas/as-new.bin b/mkrtos_user/user/app/binutils-2.33.1/gas/as-new.bin index bdb8bfe46..1575c4a39 100644 Binary files a/mkrtos_user/user/app/binutils-2.33.1/gas/as-new.bin and b/mkrtos_user/user/app/binutils-2.33.1/gas/as-new.bin differ diff --git a/mkrtos_user/user/app/binutils-2.33.1/ld/ld-new.bin b/mkrtos_user/user/app/binutils-2.33.1/ld/ld-new.bin index c0f1bc169..0dd36905b 100644 Binary files a/mkrtos_user/user/app/binutils-2.33.1/ld/ld-new.bin and b/mkrtos_user/user/app/binutils-2.33.1/ld/ld-new.bin differ diff --git a/mkrtos_user/user/app/misc/dm9000_drv/src/main.c b/mkrtos_user/user/app/misc/dm9000_drv/src/main.c index 58efa1910..e0aedebb8 100644 --- a/mkrtos_user/user/app/misc/dm9000_drv/src/main.c +++ b/mkrtos_user/user/app/misc/dm9000_drv/src/main.c @@ -68,7 +68,7 @@ int main(int argc, char *args[]) ret = rpc_meta_init(THREAD_MAIN, &hd); assert(ret >= 0); - meta_reg_svr_obj(&net_drv.svr, NET_DRV_PROT); + meta_reg_svr_obj(&net_drv.svr, BLK_DRV_PROT); assert(ns_register("/dm9000", hd, MOUNT_NODE) >= 0); mk_printf("dm9000 reg success\n"); diff --git a/mkrtos_user/user/drv/ATSURFF437/CMakeLists.txt b/mkrtos_user/user/drv/ATSURFF437/CMakeLists.txt index 9fce41ef5..e9b90ee4d 100644 --- a/mkrtos_user/user/drv/ATSURFF437/CMakeLists.txt +++ b/mkrtos_user/user/drv/ATSURFF437/CMakeLists.txt @@ -6,3 +6,4 @@ add_subdirectory(i2c) add_subdirectory(pca9555) add_subdirectory(eth) add_subdirectory(snd) +add_subdirectory(block) diff --git a/mkrtos_user/user/drv/ATSURFF437/block/CMakeLists.txt b/mkrtos_user/user/drv/ATSURFF437/block/CMakeLists.txt new file mode 100644 index 000000000..d024e936b --- /dev/null +++ b/mkrtos_user/user/drv/ATSURFF437/block/CMakeLists.txt @@ -0,0 +1,88 @@ +cmake_minimum_required(VERSION 3.13) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DAT32F437ZMT7 -DUSE_STDPERIPH_DRIVER -DAT_SURF_F437_V1 ") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DAT32F437ZMT7 -DUSE_STDPERIPH_DRIVER -DAT_SURF_F437_V1 ") +set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -DAT32F437ZMT7 -DUSE_STDPERIPH_DRIVER -DAT_SURF_F437_V1 ") + +file( + GLOB deps + ${CMAKE_SOURCE_DIR}/mkrtos_bsp/AT32/AT32F435_437_Firmware_Library_V2.2.0/libraries/drivers/src/*.c + *.c +) + + +add_executable( + block.elf + ${deps} + ${START_SRC} +) +target_link_libraries( + block.elf + PUBLIC + -Bstatic + ${LIBC_NAME} + mk_drv + mk_char + --whole-archive + ${START_LIB} + libc_be + sys + sys_util + sys_svr + printf + --no-whole-archive + ${GCC_LIB_PATH}/libgcc.a +) +target_include_directories( + block.elf + PUBLIC + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys/inc + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys_svr/inc + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/util/inc + + ${CMAKE_SOURCE_DIR}/mkrtos_bsp/AT32/AT32F435_437_Firmware_Library_V2.2.0/libraries/drivers/inc + ${CMAKE_SOURCE_DIR}/mkrtos_bsp/AT32/AT32F435_437_Firmware_Library_V2.2.0/libraries/cmsis/cm4/device_support + ${CMAKE_SOURCE_DIR}/mkrtos_bsp/AT32/AT32F435_437_Firmware_Library_V2.2.0/libraries/cmsis/cm4/core_support + ${CMAKE_SOURCE_DIR}/mkrtos_bsp/AT32/AT32F435_437_Firmware_Library_V2.2.0/project/at_surf_f437_board/ + ${CMAKE_SOURCE_DIR}/mkrtos_bsp/AT32/AT32F435_437_Firmware_Library_V2.2.0/middlewares/i2c_application_library + ${CMAKE_CURRENT_SOURCE_DIR}/ + + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys/inc + + ${CMAKE_SOURCE_DIR}/mkrtos_user/user/drv/lib/mk_block + ${CMAKE_SOURCE_DIR}/mkrtos_user/user/drv/lib/mk_pin + ${CMAKE_SOURCE_DIR}/mkrtos_user/user/drv/lib/mk_pca9555 +) +add_dependencies( + block.elf + ${START_LIB} + sys + sys_util + mk_char + mk_drv + printf + # mk_block +) +set_target_properties( + block.elf PROPERTIES LINK_FLAGS + "-T ${CMAKE_CURRENT_LIST_DIR}/${ARCH_NAME}/link.lds ${CORTEX_M_LINK_FLAGS} --gc-section -no-dynamic-linker " + #--no-warn-rwx-segments +) +add_custom_target( + block_dump ALL + COMMAND + ${CMAKE_OBJDUMP} -s -S block.elf > ${CMAKE_SOURCE_DIR}/build/output/block.S + COMMAND + ${CMAKE_READELF} -a block.elf > ${CMAKE_SOURCE_DIR}/build/output/block.txt + COMMAND + ${CMAKE_OBJCOPY} -O binary -S block.elf block.bin + COMMAND + ${CMAKE_SIZE} block.elf + COMMAND + ${CMAKE_COMMAND} -E copy block.bin ${CMAKE_SOURCE_DIR}/build/output/cpio/block + COMMAND + cp block.elf ${CMAKE_SOURCE_DIR}/build/output/block.elf +) + +add_dependencies(block_dump block.elf) + \ No newline at end of file diff --git a/mkrtos_user/user/drv/ATSURFF437/block/armv7_8m/link.lds b/mkrtos_user/user/drv/ATSURFF437/block/armv7_8m/link.lds new file mode 100644 index 000000000..34fdd0c60 --- /dev/null +++ b/mkrtos_user/user/drv/ATSURFF437/block/armv7_8m/link.lds @@ -0,0 +1,124 @@ +ENTRY(_start_) + +SECTIONS +{ + .text : { + . = ALIGN(4); + __text_start__ = .; + KEEP(*(.first)) + *(.text) + *(.text.*) + + KEEP(*(.init)) + KEEP(*(.fini)) + + /* .ctors */ + *crtbegin.o(.ctors) + *crtbegin?.o(.ctors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors) + *(SORT(.ctors.*)) + *(.ctors) + + /* .dtors */ + *crtbegin.o(.dtors) + *crtbegin?.o(.dtors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors) + *(SORT(.dtors.*)) + *(.dtors) + + *(SORT(.rodata.*)) + *(.rodata) + + KEEP(*(.eh_frame*)) + + . = ALIGN(4); + __rel_start__ = .; + *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*) + __rel_end__ = .; + } + .ARM.exidx : { + . = ALIGN(4); + __exdix_start = .; + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + /* This is used by the startup in order to initialize the .data secion */ + __exdix_end = .; + } + + .permissions_table : { + . = ALIGN(4); + __permissions_table_start__ = .; + KEEP(*(.permissions_table)) + __permissions_table_end__ = .; + } + + + PROVIDE(__ram_size__ = __bss_end__ - __data_start__); + .data : { + . = ALIGN(4); + __data_start__ = .; + __got_start__ = .; + *(.got) + __got_end__ = .; + . = ALIGN(4); + *(.data) + *(.data.*) + + *(vtable) + *(.data*) + + . = ALIGN(4); + /* preinit data */ + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP(*(.preinit_array)) + PROVIDE_HIDDEN (__preinit_array_end = .); + + . = ALIGN(4); + /* init data */ + PROVIDE_HIDDEN (__init_array_start = .); + KEEP(*(SORT(.init_array.*))) + KEEP(*(.init_array)) + PROVIDE_HIDDEN (__init_array_end = .); + + . = ALIGN(4); + /* finit data */ + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP(*(SORT(.fini_array.*))) + KEEP(*(.fini_array)) + PROVIDE_HIDDEN (__fini_array_end = .); + + . = ALIGN(4); + _shell_command_start = .; + KEEP(*(shellCommand)) + _shell_command_end = .; + + . = ALIGN(4); + /* All data end */ + __data_end__ = .; + } + + PROVIDE(__heap_size__ = __heap_end__ - __heap_start__); + PROVIDE(__stack_size__ = __stack_end__ - __stack_start__); + .bss : { + . = ALIGN(4); + /* This is used by the startup in order to initialize the .bss secion */ + __bss_start__ = .; + *(.bss) + *(COMMON) + + . = ALIGN(4); + __heap_start__ = .; + KEEP(*(.bss.heap)) + __heap_end__ = .; + + . = ALIGN(4); + __stack_start__ = .; + KEEP(*(.bss.stack)) + __stack_end__ = .; + + *(.bss.*) + /* This is used by the startup in order to initialize the .bss secion */ + . = ALIGN(4); + __bss_end__ = .; + } + _end = .; +} diff --git a/mkrtos_user/user/drv/ATSURFF437/block/at32f435_437_conf.h b/mkrtos_user/user/drv/ATSURFF437/block/at32f435_437_conf.h new file mode 100644 index 000000000..4d0bb6570 --- /dev/null +++ b/mkrtos_user/user/drv/ATSURFF437/block/at32f435_437_conf.h @@ -0,0 +1,176 @@ +/** + ************************************************************************** + * @file at32f435_437_conf.h + * @brief at32f435_437 config header file + ************************************************************************** + * Copyright notice & Disclaimer + * + * The software Board Support Package (BSP) that is made available to + * download from Artery official website is the copyrighted work of Artery. + * Artery authorizes customers to use, copy, and distribute the BSP + * software and its related documentation for the purpose of design and + * development in conjunction with Artery microcontrollers. Use of the + * software is governed by this copyright notice and the following disclaimer. + * + * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES, + * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS, + * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR + * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS, + * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * + ************************************************************************** + */ + +/* define to prevent recursive inclusion -------------------------------------*/ +#ifndef __AT32F435_437_CONF_H +#define __AT32F435_437_CONF_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** + * @brief in the following line adjust the value of high speed external crystal (hext) + * used in your application + * + * tip: to avoid modifying this file each time you need to use different hext, you + * can define the hext value in your toolchain compiler preprocessor. + * + */ +#if !defined HEXT_VALUE +#define HEXT_VALUE ((uint32_t)8000000) /*!< value of the high speed external crystal in hz */ +#endif + +/** + * @brief in the following line adjust the high speed external crystal (hext) startup + * timeout value + */ +#define HEXT_STARTUP_TIMEOUT ((uint16_t)0x3000) /*!< time out for hext start up */ +#define HICK_VALUE ((uint32_t)8000000) /*!< value of the high speed internal clock in hz */ +#define LEXT_VALUE ((uint32_t)32768) /*!< value of the low speed external clock in hz */ + +/* module define -------------------------------------------------------------*/ +#define CRM_MODULE_ENABLED +#define TMR_MODULE_ENABLED +#define ERTC_MODULE_ENABLED +#define GPIO_MODULE_ENABLED +#define I2C_MODULE_ENABLED +#define USART_MODULE_ENABLED +#define PWC_MODULE_ENABLED +#define CAN_MODULE_ENABLED +#define ADC_MODULE_ENABLED +#define DAC_MODULE_ENABLED +#define SPI_MODULE_ENABLED +#define EDMA_MODULE_ENABLED +#define DMA_MODULE_ENABLED +#define DEBUG_MODULE_ENABLED +#define FLASH_MODULE_ENABLED +#define CRC_MODULE_ENABLED +#define WWDT_MODULE_ENABLED +#define WDT_MODULE_ENABLED +#define EXINT_MODULE_ENABLED +#define SDIO_MODULE_ENABLED +#define XMC_MODULE_ENABLED +#define USB_MODULE_ENABLED +#define ACC_MODULE_ENABLED +#define MISC_MODULE_ENABLED +#define QSPI_MODULE_ENABLED +#define DVP_MODULE_ENABLED +#define SCFG_MODULE_ENABLED +#define EMAC_MODULE_ENABLED + +/* includes ------------------------------------------------------------------*/ +#ifdef CRM_MODULE_ENABLED +#include "at32f435_437_crm.h" +#endif +#ifdef TMR_MODULE_ENABLED +#include "at32f435_437_tmr.h" +#endif +#ifdef ERTC_MODULE_ENABLED +#include "at32f435_437_ertc.h" +#endif +#ifdef GPIO_MODULE_ENABLED +#include "at32f435_437_gpio.h" +#endif +#ifdef I2C_MODULE_ENABLED +#include "at32f435_437_i2c.h" +#endif +#ifdef USART_MODULE_ENABLED +#include "at32f435_437_usart.h" +#endif +#ifdef PWC_MODULE_ENABLED +#include "at32f435_437_pwc.h" +#endif +#ifdef CAN_MODULE_ENABLED +#include "at32f435_437_can.h" +#endif +#ifdef ADC_MODULE_ENABLED +#include "at32f435_437_adc.h" +#endif +#ifdef DAC_MODULE_ENABLED +#include "at32f435_437_dac.h" +#endif +#ifdef SPI_MODULE_ENABLED +#include "at32f435_437_spi.h" +#endif +#ifdef DMA_MODULE_ENABLED +#include "at32f435_437_dma.h" +#endif +#ifdef DEBUG_MODULE_ENABLED +#include "at32f435_437_debug.h" +#endif +#ifdef FLASH_MODULE_ENABLED +#include "at32f435_437_flash.h" +#endif +#ifdef CRC_MODULE_ENABLED +#include "at32f435_437_crc.h" +#endif +#ifdef WWDT_MODULE_ENABLED +#include "at32f435_437_wwdt.h" +#endif +#ifdef WDT_MODULE_ENABLED +#include "at32f435_437_wdt.h" +#endif +#ifdef EXINT_MODULE_ENABLED +#include "at32f435_437_exint.h" +#endif +#ifdef SDIO_MODULE_ENABLED +#include "at32f435_437_sdio.h" +#endif +#ifdef XMC_MODULE_ENABLED +#include "at32f435_437_xmc.h" +#endif +#ifdef ACC_MODULE_ENABLED +#include "at32f435_437_acc.h" +#endif +#ifdef MISC_MODULE_ENABLED +#include "at32f435_437_misc.h" +#endif +#ifdef EDMA_MODULE_ENABLED +#include "at32f435_437_edma.h" +#endif +#ifdef QSPI_MODULE_ENABLED +#include "at32f435_437_qspi.h" +#endif +#ifdef SCFG_MODULE_ENABLED +#include "at32f435_437_scfg.h" +#endif +#ifdef EMAC_MODULE_ENABLED +#include "at32f435_437_emac.h" +#endif +#ifdef DVP_MODULE_ENABLED +#include "at32f435_437_dvp.h" +#endif +#ifdef USB_MODULE_ENABLED +#include "at32f435_437_usb.h" +#endif + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/mkrtos_user/user/drv/ATSURFF437/block/flash.c b/mkrtos_user/user/drv/ATSURFF437/block/flash.c new file mode 100644 index 000000000..8b41162bf --- /dev/null +++ b/mkrtos_user/user/drv/ATSURFF437/block/flash.c @@ -0,0 +1,55 @@ + +#include "flash.h" +#include "at32f435_437_conf.h" +#if defined(AT32F437xM) || defined(AT32F435xM) || defined(AT32F437xD) || defined(AT32F435xD) +#define SECTOR_SIZE 4096 /* this parameter depends on the specific model of the chip */ +#else +#define SECTOR_SIZE 2048 /* this parameter depends on the specific model of the chip */ +#endif + +int flash_get_sector_size(void) +{ + return SECTOR_SIZE; +} +int flash_read_sector(uint32_t sector_inx, uint32_t *p_buffer, uint16_t num_read) +{ + uint16_t i; + uint32_t read_addr; + + if (num_read < SECTOR_SIZE / 4) { + return -1; + } + read_addr = sector_inx * SECTOR_SIZE + FLASH_BASE; + + for (i = 0; i < num_read; i++) { + p_buffer[i] = *(uint32_t *)(read_addr); + read_addr += 4; + } + return num_read; +} +int flash_write_sector(uint32_t sector_inx, uint32_t *p_buffer, uint16_t num_write) +{ + flash_status_type status = FLASH_OPERATE_DONE; + uint32_t write_addr = sector_inx * SECTOR_SIZE + FLASH_BASE; + + if (num_write < SECTOR_SIZE / 4) { + return -1; + } + + flash_unlock(); + status = flash_sector_erase(sector_inx * SECTOR_SIZE + FLASH_BASE); + if (status != FLASH_OPERATE_DONE) { + flash_lock(); + return -1; + } + for (int i = 0; i < num_write; i++) { + status = flash_word_program(write_addr, p_buffer[i]); + if (status != FLASH_OPERATE_DONE) { + flash_lock(); + return -1; + } + write_addr += 4; + } + flash_lock(); + return num_write; +} diff --git a/mkrtos_user/user/drv/ATSURFF437/block/flash.h b/mkrtos_user/user/drv/ATSURFF437/block/flash.h new file mode 100644 index 000000000..fb33bf68b --- /dev/null +++ b/mkrtos_user/user/drv/ATSURFF437/block/flash.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +int flash_get_sector_size(void); +int flash_read_sector(uint32_t sector_inx, uint32_t *p_buffer, uint16_t num_read); +int flash_write_sector(uint32_t sector_inx, uint32_t *p_buffer, uint16_t num_write); diff --git a/mkrtos_user/user/drv/ATSURFF437/block/heap_stack.c b/mkrtos_user/user/drv/ATSURFF437/block/heap_stack.c new file mode 100644 index 000000000..9cc3c8c15 --- /dev/null +++ b/mkrtos_user/user/drv/ATSURFF437/block/heap_stack.c @@ -0,0 +1,17 @@ + +#define HEAP_SIZE (1024 + 512) +#define STACK_SIZE (1024 + 512) + +#if defined(__CC_ARM) +#define HEAP_ATTR SECTION("HEAP") __attribute__((zero_init)) +#define STACK_ATTR SECTION("STACK") __attribute__((zero_init)) +#elif defined(__GNUC__) +#define HEAP_ATTR __attribute__((__section__(".bss.heap"))) +#define STACK_ATTR __attribute__((__section__(".bss.stack"))) +#elif defined(__IAR_SYSTEMS_ICC__) +#define HEAP_ATTR +#define STACK_ATTR +#endif + +__attribute__((used)) HEAP_ATTR static char _____heap_____[HEAP_SIZE]; +__attribute__((used)) STACK_ATTR static char _____stack_____[STACK_SIZE]; diff --git a/mkrtos_user/user/drv/ATSURFF437/block/main.c b/mkrtos_user/user/drv/ATSURFF437/block/main.c new file mode 100644 index 000000000..e43cc240c --- /dev/null +++ b/mkrtos_user/user/drv/ATSURFF437/block/main.c @@ -0,0 +1,85 @@ + +#include "flash.h" +#include "u_share_mem.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define STACK_COM_ITME_SIZE (1024 + 512) +ATTR_ALIGN(8) +uint8_t stack_coms[STACK_COM_ITME_SIZE]; +uint8_t msg_buf_coms[MSG_BUG_LEN]; +void fast_ipc_init(void) +{ + u_fast_ipc_init(stack_coms, msg_buf_coms, 1, STACK_COM_ITME_SIZE); +} +static blk_drv_t blk_drv; +int blk_drv_write(obj_handler_t obj, int len, int inx) +{ + int ret = -1; + addr_t addr = 0; + umword_t size = 0; + msg_tag_t tag = share_mem_map(obj, vma_addr_create(VPAGE_PROT_RWX, 0, 0), &addr, &size); + + if (msg_tag_get_val(tag) < 0) { + handler_free_umap(obj); + printf("net write error.\n"); + return msg_tag_get_val(tag); + } + + ret = flash_write_sector(inx, (void *)addr, len / 4); + handler_free_umap(obj); + if (ret < 0) { + return ret; + } + return ret * 4; +} +int blk_drv_read(obj_handler_t obj, int len, int inx) +{ + int ret = -1; + addr_t addr = 0; + umword_t size = 0; + msg_tag_t tag = share_mem_map(obj, vma_addr_create(VPAGE_PROT_RWX, 0, 0), &addr, &size); + uint32_t _err; + + ret = flash_read_sector(inx, (void *)addr, len / 4); + handler_free_umap(obj); + if (ret < 0) { + return ret; + } + return ret * 4; +} +int blk_drv_map(obj_handler_t *hd) +{ + return -ENOSYS; +} +int main(int argc, char *argv[]) +{ + obj_handler_t hd; + int ret; + task_set_obj_name(TASK_THIS, TASK_THIS, "tk_blk"); + task_set_obj_name(TASK_THIS, THREAD_MAIN, "th_blk"); + printf("%s init..\n", argv[0]); + fast_ipc_init(); + + blk_drv_init(&blk_drv); + ret = rpc_meta_init(THREAD_MAIN, &hd); + assert(ret >= 0); + ns_register("/block", hd, FILE_NODE); + meta_reg_svr_obj(&blk_drv.svr, BLK_DRV_PROT); + + while (1) { + rpc_loop(); + } +} diff --git a/mkrtos_user/user/drv/ATSURFF437/display/fs_rpc.c b/mkrtos_user/user/drv/ATSURFF437/display/fs_rpc.c index 203bb366d..499eb94f1 100644 --- a/mkrtos_user/user/drv/ATSURFF437/display/fs_rpc.c +++ b/mkrtos_user/user/drv/ATSURFF437/display/fs_rpc.c @@ -323,7 +323,7 @@ int fs_svr_rename(char *old, char *new) } int fs_svr_stat(const char *path, void *_buf) { - if (path == NULL || buf == NULL) + if (path == NULL || _buf == NULL) { return -EINVAL; } diff --git a/mkrtos_user/user/drv/ATSURFF437/eth/main.c b/mkrtos_user/user/drv/ATSURFF437/eth/main.c index 424010b7d..50ec70a62 100644 --- a/mkrtos_user/user/drv/ATSURFF437/eth/main.c +++ b/mkrtos_user/user/drv/ATSURFF437/eth/main.c @@ -13,7 +13,7 @@ #include "rpc_prot.h" #include "u_hd_man.h" #include "u_share_mem.h" -#include +#include #include #include #include "mk_eth_drv_impl.h" @@ -21,7 +21,7 @@ #include #include "at_surf_f437_board_emac.h" -static net_drv_t net_drv; //!< 网络驱动的协议 +static blk_drv_t net_drv; //!< 网络驱动的协议 #define STACK_COM_ITME_SIZE (1024+512) ATTR_ALIGN(8) uint8_t stack_coms[STACK_COM_ITME_SIZE]; @@ -30,7 +30,7 @@ void fast_ipc_init(void) { u_fast_ipc_init(stack_coms, msg_buf_coms, 1, STACK_COM_ITME_SIZE); } -int net_drv_write(obj_handler_t obj, int len) +int blk_drv_write(obj_handler_t obj, int len, int inx) { int ret = -1; addr_t addr = 0; @@ -49,7 +49,7 @@ int net_drv_write(obj_handler_t obj, int len) handler_free_umap(obj); return len; } -int net_drv_read(obj_handler_t obj, int len) +int blk_drv_read(obj_handler_t obj, int len, int inx) { int ret = -1; addr_t addr = 0; @@ -61,7 +61,7 @@ int net_drv_read(obj_handler_t obj, int len) handler_free_umap(obj); return ret; } -int net_drv_map(obj_handler_t *hd) +int blk_drv_map(obj_handler_t *hd) { *hd = emac_get_sema(); return 0; @@ -79,13 +79,13 @@ int main(int argc, char *argv[]) drv_eth_init(); dtb_parse_init(); - net_drv_init(&net_drv); + blk_drv_init(&net_drv); ret = rpc_meta_init(THREAD_MAIN, &hd); assert(ret >= 0); // fs_svr_init(); // mkdir("/dev", 0777); ns_register("/eth", hd, FILE_NODE); - meta_reg_svr_obj(&net_drv.svr, NET_DRV_PROT); + meta_reg_svr_obj(&net_drv.svr, BLK_DRV_PROT); while (1) { rpc_loop(); diff --git a/mkrtos_user/user/drv/ATSURFF437/i2c/fs_rpc.c b/mkrtos_user/user/drv/ATSURFF437/i2c/fs_rpc.c index 2b7cba3f8..7d79e1a8c 100644 --- a/mkrtos_user/user/drv/ATSURFF437/i2c/fs_rpc.c +++ b/mkrtos_user/user/drv/ATSURFF437/i2c/fs_rpc.c @@ -357,7 +357,7 @@ int fs_svr_rename(char *old, char *new) } int fs_svr_stat(const char *path, void *_buf) { - if (path == NULL || buf == NULL) + if (path == NULL || _buf == NULL) { return -EINVAL; } diff --git a/mkrtos_user/user/drv/ATSURFF437/pca9555/fs_rpc.c b/mkrtos_user/user/drv/ATSURFF437/pca9555/fs_rpc.c index f015bfdf3..0b2aea407 100644 --- a/mkrtos_user/user/drv/ATSURFF437/pca9555/fs_rpc.c +++ b/mkrtos_user/user/drv/ATSURFF437/pca9555/fs_rpc.c @@ -363,7 +363,7 @@ int fs_svr_rename(char *old, char *new) } int fs_svr_stat(const char *path, void *_buf) { - if (path == NULL || buf == NULL) + if (path == NULL || _buf == NULL) { return -EINVAL; } diff --git a/mkrtos_user/user/drv/ATSURFF437/pin/fs_rpc.c b/mkrtos_user/user/drv/ATSURFF437/pin/fs_rpc.c index dd7e7a638..94ea98036 100644 --- a/mkrtos_user/user/drv/ATSURFF437/pin/fs_rpc.c +++ b/mkrtos_user/user/drv/ATSURFF437/pin/fs_rpc.c @@ -18,6 +18,8 @@ #include #include #include +#include "kstat.h" + static fs_t fs; typedef struct file_desc @@ -398,7 +400,7 @@ int fs_svr_rename(char *old, char *new) } int fs_svr_stat(const char *path, void *_buf) { - if (path == NULL || buf == NULL) + if (path == NULL || _buf == NULL) { return -EINVAL; } diff --git a/mkrtos_user/user/drv/ATSURFF437/snd/main.c b/mkrtos_user/user/drv/ATSURFF437/snd/main.c index a1ea29dc2..eb405da2f 100644 --- a/mkrtos_user/user/drv/ATSURFF437/snd/main.c +++ b/mkrtos_user/user/drv/ATSURFF437/snd/main.c @@ -16,11 +16,11 @@ #include #include #include -#include +#include #include #include "mk_snd_drv_impl.h" -static snd_drv_t snd_drv; //!< 网络驱动的协议 +static blk_drv_t snd_drv; //!< 网络驱动的协议 #define STACK_COM_ITME_SIZE (1024+512) ATTR_ALIGN(8) uint8_t stack_coms[STACK_COM_ITME_SIZE]; @@ -29,7 +29,7 @@ void fast_ipc_init(void) { u_fast_ipc_init(stack_coms, msg_buf_coms, 1, STACK_COM_ITME_SIZE); } -int snd_drv_write(obj_handler_t obj, int len) +int blk_drv_write(obj_handler_t obj, int len, int inx) { int ret = -1; addr_t addr = 0; @@ -48,7 +48,7 @@ int snd_drv_write(obj_handler_t obj, int len) handler_free_umap(obj); return len; } -int snd_drv_read(obj_handler_t obj, int len) +int blk_drv_read(obj_handler_t obj, int len, int inx) { int ret = -1; addr_t addr = 0; @@ -60,9 +60,8 @@ int snd_drv_read(obj_handler_t obj, int len) handler_free_umap(obj); return ret; } -int snd_drv_map(obj_handler_t *hd) +int blk_drv_map(obj_handler_t *hd) { - // *hd = emac_get_sema(); return -1; } int main(int argc, char *argv[]) @@ -78,11 +77,11 @@ int main(int argc, char *argv[]) drv_snd_init(); dtb_parse_init(); - snd_drv_init(&snd_drv); + blk_drv_init(&snd_drv); ret = rpc_meta_init(THREAD_MAIN, &hd); assert(ret >= 0); ns_register("/snd", hd, FILE_NODE); - meta_reg_svr_obj(&snd_drv.svr, SND_DRV_PROT); + meta_reg_svr_obj(&snd_drv.svr, BLK_DRV_PROT); while (1) { rpc_loop(); diff --git a/mkrtos_user/user/drv/CMakeLists.txt b/mkrtos_user/user/drv/CMakeLists.txt index e7d484496..5d461cf45 100644 --- a/mkrtos_user/user/drv/CMakeLists.txt +++ b/mkrtos_user/user/drv/CMakeLists.txt @@ -5,5 +5,4 @@ set(CMAKE_CXX_FLAGS "${CMAKE_ASM_FLAGS} -DMKRTOS_DRV ") if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${CONFIG_BOARD_NAME}) add_subdirectory(${CONFIG_BOARD_NAME}) endif() -# add_subdirectory(ATSURFF437) add_subdirectory(lib) diff --git a/mkrtos_user/user/drv/STM32F205/CMakeLists.txt b/mkrtos_user/user/drv/STM32F205/CMakeLists.txt new file mode 100644 index 000000000..e3117d0e7 --- /dev/null +++ b/mkrtos_user/user/drv/STM32F205/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.13) + +add_subdirectory(block) diff --git a/mkrtos_user/user/drv/STM32F205/block/CMakeLists.txt b/mkrtos_user/user/drv/STM32F205/block/CMakeLists.txt new file mode 100644 index 000000000..d33790d96 --- /dev/null +++ b/mkrtos_user/user/drv/STM32F205/block/CMakeLists.txt @@ -0,0 +1,75 @@ +cmake_minimum_required(VERSION 3.13) + +file(GLOB_RECURSE deps + *.c + *.S + ${CMAKE_SOURCE_DIR}/mkrtos_bsp/STM32/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/src/*.c +) +add_executable( + block + ${deps} + ${START_SRC} +) +target_link_libraries( + block + PUBLIC + -Bstatic + ${LIBC_NAME} + mk_drv + mk_char + --whole-archive + ${START_LIB} + libc_be + sys + sys_util + sys_svr + --no-whole-archive + ${GCC_LIB_PATH}/libgcc.a +) + +target_include_directories( + block + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/ + ${CMAKE_CURRENT_SOURCE_DIR}/../mk_drv + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys/inc + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys/inc/${ARCH_NAME} + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys_svr/inc + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/util/inc + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/libfdt/lib/contrib + + ${CMAKE_SOURCE_DIR}/mkrtos_bsp/STM32/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/CMSIS/Include + ${CMAKE_SOURCE_DIR}/mkrtos_bsp/STM32/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/CMSIS/Device/ST/STM32F2xx + ${CMAKE_SOURCE_DIR}/mkrtos_bsp/STM32/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc +) + +set_target_properties( + block PROPERTIES LINK_FLAGS + "-T ${CMAKE_CURRENT_LIST_DIR}/${ARCH_NAME}/link.lds ${CORTEX_M_LINK_FLAGS} --gc-section -no-dynamic-linker " + #--no-warn-rwx-segments +) +add_custom_target( + block_dump ALL + COMMAND + ${CMAKE_OBJDUMP} -s -S block > ${CMAKE_SOURCE_DIR}/build/output/block.S + COMMAND + ${CMAKE_READELF} -a block > ${CMAKE_SOURCE_DIR}/build/output/block.txt + COMMAND + ${CMAKE_OBJCOPY} -O binary -S block block.bin + COMMAND + ${CMAKE_SIZE} block + COMMAND + ${CMAKE_COMMAND} -E copy block.bin ${CMAKE_SOURCE_DIR}/build/output/cpio/block + COMMAND + cp block ${CMAKE_SOURCE_DIR}/build/output/block +) +add_dependencies(block_dump block) + +add_dependencies( + block + fdt + sys + sys_util + sys_svr + mk_drv +) diff --git a/mkrtos_user/user/drv/STM32F205/block/armv7_8m/link.lds b/mkrtos_user/user/drv/STM32F205/block/armv7_8m/link.lds new file mode 100644 index 000000000..34fdd0c60 --- /dev/null +++ b/mkrtos_user/user/drv/STM32F205/block/armv7_8m/link.lds @@ -0,0 +1,124 @@ +ENTRY(_start_) + +SECTIONS +{ + .text : { + . = ALIGN(4); + __text_start__ = .; + KEEP(*(.first)) + *(.text) + *(.text.*) + + KEEP(*(.init)) + KEEP(*(.fini)) + + /* .ctors */ + *crtbegin.o(.ctors) + *crtbegin?.o(.ctors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors) + *(SORT(.ctors.*)) + *(.ctors) + + /* .dtors */ + *crtbegin.o(.dtors) + *crtbegin?.o(.dtors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors) + *(SORT(.dtors.*)) + *(.dtors) + + *(SORT(.rodata.*)) + *(.rodata) + + KEEP(*(.eh_frame*)) + + . = ALIGN(4); + __rel_start__ = .; + *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*) + __rel_end__ = .; + } + .ARM.exidx : { + . = ALIGN(4); + __exdix_start = .; + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + /* This is used by the startup in order to initialize the .data secion */ + __exdix_end = .; + } + + .permissions_table : { + . = ALIGN(4); + __permissions_table_start__ = .; + KEEP(*(.permissions_table)) + __permissions_table_end__ = .; + } + + + PROVIDE(__ram_size__ = __bss_end__ - __data_start__); + .data : { + . = ALIGN(4); + __data_start__ = .; + __got_start__ = .; + *(.got) + __got_end__ = .; + . = ALIGN(4); + *(.data) + *(.data.*) + + *(vtable) + *(.data*) + + . = ALIGN(4); + /* preinit data */ + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP(*(.preinit_array)) + PROVIDE_HIDDEN (__preinit_array_end = .); + + . = ALIGN(4); + /* init data */ + PROVIDE_HIDDEN (__init_array_start = .); + KEEP(*(SORT(.init_array.*))) + KEEP(*(.init_array)) + PROVIDE_HIDDEN (__init_array_end = .); + + . = ALIGN(4); + /* finit data */ + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP(*(SORT(.fini_array.*))) + KEEP(*(.fini_array)) + PROVIDE_HIDDEN (__fini_array_end = .); + + . = ALIGN(4); + _shell_command_start = .; + KEEP(*(shellCommand)) + _shell_command_end = .; + + . = ALIGN(4); + /* All data end */ + __data_end__ = .; + } + + PROVIDE(__heap_size__ = __heap_end__ - __heap_start__); + PROVIDE(__stack_size__ = __stack_end__ - __stack_start__); + .bss : { + . = ALIGN(4); + /* This is used by the startup in order to initialize the .bss secion */ + __bss_start__ = .; + *(.bss) + *(COMMON) + + . = ALIGN(4); + __heap_start__ = .; + KEEP(*(.bss.heap)) + __heap_end__ = .; + + . = ALIGN(4); + __stack_start__ = .; + KEEP(*(.bss.stack)) + __stack_end__ = .; + + *(.bss.*) + /* This is used by the startup in order to initialize the .bss secion */ + . = ALIGN(4); + __bss_end__ = .; + } + _end = .; +} diff --git a/mkrtos_user/user/drv/STM32F205/block/flash.c b/mkrtos_user/user/drv/STM32F205/block/flash.c new file mode 100644 index 000000000..52e075e56 --- /dev/null +++ b/mkrtos_user/user/drv/STM32F205/block/flash.c @@ -0,0 +1,115 @@ + +#include "stm32f2xx_conf.h" +#include + +/* Base address of the Flash sectors */ +#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */ +#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */ +#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */ +#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */ +#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */ +#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */ +#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */ +#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */ +#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */ +#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */ +#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */ +#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */ + +uint32_t GetSector(uint32_t Address) +{ + uint32_t sector = 0; + + if ((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0)) { + sector = FLASH_Sector_0; + } else if ((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1)) { + sector = FLASH_Sector_1; + } else if ((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2)) { + sector = FLASH_Sector_2; + } else if ((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3)) { + sector = FLASH_Sector_3; + } else if ((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4)) { + sector = FLASH_Sector_4; + } else if ((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5)) { + sector = FLASH_Sector_5; + } else if ((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6)) { + sector = FLASH_Sector_6; + } else if ((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7)) { + sector = FLASH_Sector_7; + } else if ((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8)) { + sector = FLASH_Sector_8; + } else if ((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9)) { + sector = FLASH_Sector_9; + } else if ((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10)) { + sector = FLASH_Sector_10; + } else /*(Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_11))*/ + { + sector = FLASH_Sector_11; + } + + return sector; +} +int flash_write(addr_t st_addr, addr_t end_addr, umword_t *data) +{ + uint32_t StartSector = 0, EndSector = 0, Address = 0, SectorCounter = 0; + /* Unlock the Flash to enable the flash control register access *************/ + FLASH_Unlock(); + + /* Erase the user Flash area + (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/ + + /* Clear pending flags (if any) */ + FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | + FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); + + /* Get the number of the start and end sectors */ + StartSector = GetSector(st_addr); + EndSector = GetSector(end_addr); + + for (SectorCounter = StartSector; SectorCounter < EndSector; SectorCounter += 8) { + /* Device voltage range supposed to be [2.7V to 3.6V], the operation will + be done by word */ + if (FLASH_EraseSector(SectorCounter, VoltageRange_3) != FLASH_COMPLETE) { + /* Error occurred while sector erase. + User can add here some code to deal with this error */ + FLASH_Lock(); + return -1; + } + } + Address = st_addr; + + while (Address < end_addr) { + if (FLASH_ProgramWord(Address, *data++) == FLASH_COMPLETE) { + Address = Address + 4; + } else { + /* Error occurred while writing data in Flash memory. + User can add here some code to deal with this error */ + FLASH_Lock(); + return -1; + } + } + FLASH_Lock(); + return 0; +} + +#include +#include + +int flash_init(void) +{ + msg_tag_t tag; + addr_t vaddr; + + tag = u_vmam_alloc(VMA_PROT, vma_addr_create(VPAGE_PROT_RWX, 0, 0), + 0x400 /*TODO:*/, 0x40023C00, &vaddr); + if (msg_tag_get_val(tag) < 0) { + return msg_tag_get_val(tag); + } + + tag = u_vmam_alloc(VMA_PROT, vma_addr_create(VPAGE_PROT_RWX, 0, 0), + 32 * 1024 * 1024 /*TODO:*/, 0x8000000, &vaddr); + if (msg_tag_get_val(tag) < 0) { + return msg_tag_get_val(tag); + } + return 0; +} \ No newline at end of file diff --git a/mkrtos_user/user/drv/STM32F205/block/flash.h b/mkrtos_user/user/drv/STM32F205/block/flash.h new file mode 100644 index 000000000..6f9d93538 --- /dev/null +++ b/mkrtos_user/user/drv/STM32F205/block/flash.h @@ -0,0 +1,5 @@ +#pragma once + +#include +int flash_init(void); +int flash_write(addr_t st_addr, addr_t end_addr, umword_t *data); diff --git a/mkrtos_user/user/drv/STM32F205/block/heap_stack.c b/mkrtos_user/user/drv/STM32F205/block/heap_stack.c new file mode 100644 index 000000000..9cc3c8c15 --- /dev/null +++ b/mkrtos_user/user/drv/STM32F205/block/heap_stack.c @@ -0,0 +1,17 @@ + +#define HEAP_SIZE (1024 + 512) +#define STACK_SIZE (1024 + 512) + +#if defined(__CC_ARM) +#define HEAP_ATTR SECTION("HEAP") __attribute__((zero_init)) +#define STACK_ATTR SECTION("STACK") __attribute__((zero_init)) +#elif defined(__GNUC__) +#define HEAP_ATTR __attribute__((__section__(".bss.heap"))) +#define STACK_ATTR __attribute__((__section__(".bss.stack"))) +#elif defined(__IAR_SYSTEMS_ICC__) +#define HEAP_ATTR +#define STACK_ATTR +#endif + +__attribute__((used)) HEAP_ATTR static char _____heap_____[HEAP_SIZE]; +__attribute__((used)) STACK_ATTR static char _____stack_____[STACK_SIZE]; diff --git a/mkrtos_user/user/drv/STM32F205/block/main.c b/mkrtos_user/user/drv/STM32F205/block/main.c new file mode 100644 index 000000000..da51a842c --- /dev/null +++ b/mkrtos_user/user/drv/STM32F205/block/main.c @@ -0,0 +1,68 @@ + +#include +// #include +#include "flash.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define STACK_COM_ITME_SIZE (1024 + 512) +ATTR_ALIGN(8) +uint8_t stack_coms[STACK_COM_ITME_SIZE]; +uint8_t msg_buf_coms[MSG_BUG_LEN]; +void fast_ipc_init(void) +{ + u_fast_ipc_init(stack_coms, msg_buf_coms, 1, STACK_COM_ITME_SIZE); +} +void assert_param(int val) +{ + if (!val) { + while (1) + ; + } +} +int main(int argc, char *argv[]) +{ + obj_handler_t hd; + int ret; + task_set_obj_name(TASK_THIS, TASK_THIS, "tk_blk"); + task_set_obj_name(TASK_THIS, THREAD_MAIN, "th_blk"); + printf("%s init..\n", argv[0]); + fast_ipc_init(); + printf("%s:%d\n", __func__, __LINE__); + ret = flash_init(); + if (ret < 0) { + printf("flash init error.\n"); + return -1; + } + printf("%s:%d\n", __func__, __LINE__); + ret = flash_write(0x8000000 + 512 * 1024, 0x8000000 + 512 * 1024 + 4, "123"); + if (ret < 0) { + printf("flash init error.\n"); + return -1; + } + printf("%s:%d\n", __func__, __LINE__); + char *read_str = (char *)(0x8000000 + 512 * 1024); + printf("read str is %s\n", read_str); + while (1) { + u_sleep_ms(1000); + } + // mk_drv_init(); + // mk_dev_init(); + // // drv_i2c_init(); + // dtb_parse_init(); + + // ret = rpc_meta_init(THREAD_MAIN, &hd); + // assert(ret >= 0); + // fs_svr_init(); + // ns_register("/block", hd, FILE_NODE); + // while (1) + // { + // fs_svr_loop(); + // } +} diff --git a/mkrtos_user/user/drv/STM32F205/block/stm32f2xx.h b/mkrtos_user/user/drv/STM32F205/block/stm32f2xx.h new file mode 100644 index 000000000..3bcdd1b80 --- /dev/null +++ b/mkrtos_user/user/drv/STM32F205/block/stm32f2xx.h @@ -0,0 +1,6963 @@ +/** + ****************************************************************************** + * @file stm32f2xx.h + * @author MCD Application Team + * @version V1.1.3 + * @date 05-March-2012 + * @brief CMSIS Cortex-M3 Device Peripheral Access Layer Header File. + * This file contains all the peripheral register's definitions, bits + * definitions and memory mapping for STM32F2xx devices. + * + * The file is the unique include file that the application programmer + * is using in the C source code, usually in main.c. This file contains: + * - Configuration section that allows to select: + * - The device used in the target application + * - To use or not the peripherals drivers in application code(i.e. + * code will be based on direct access to peripherals registers + * rather than drivers API), this option is controlled by + * "#define USE_STDPERIPH_DRIVER" + * - To change few application-specific parameters such as the HSE + * crystal frequency + * - Data structures and the address mapping for all peripherals + * - Peripheral's registers declarations and bits definition + * - Macros to access peripherals registers hardware + * + ****************************************************************************** + * @attention + * + *

© COPYRIGHT 2012 STMicroelectronics

+ * + * Licensed under MCD-ST Liberty SW License Agreement V2, (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.st.com/software_license_agreement_liberty_v2 + * + * 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. + * + ****************************************************************************** + */ + +/** @addtogroup CMSIS + * @{ + */ + +/** @addtogroup stm32f2xx + * @{ + */ + +#ifndef __STM32F2xx_H +#define __STM32F2xx_H + +#ifdef __cplusplus + extern "C" { +#endif /* __cplusplus */ + +/** @addtogroup Library_configuration_section + * @{ + */ + +/* Uncomment the line below according to the target STM32 device used in your + application + */ + +#if !defined (STM32F2XX) + #define STM32F2XX +#endif + +/* Tip: To avoid modifying this file each time you need to switch between these + devices, you can define the device in your toolchain compiler preprocessor. + */ + +#if !defined (STM32F2XX) + #error "Please select first the target STM32F2XX device used in your application (in stm32f2xx.h file)" +#endif + +#if !defined (USE_STDPERIPH_DRIVER) +/** + * @brief Comment the line below if you will not use the peripherals drivers. + In this case, these drivers will not be included and the application code will + be based on direct access to peripherals registers + */ + /*#define USE_STDPERIPH_DRIVER*/ +#endif /* USE_STDPERIPH_DRIVER */ + +/** + * @brief In the following line adjust the value of External High Speed oscillator (HSE) + used in your application + + Tip: To avoid modifying this file each time you need to use different HSE, you + can define the HSE value in your toolchain compiler preprocessor. + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +/** + * @brief In the following line adjust the External High Speed oscillator (HSE) Startup + Timeout value + */ +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT ((uint16_t)0x0500) /*!< Time out for HSE start up */ +#endif /* HSE_STARTUP_TIMEOUT */ + +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief STM32F2XX Standard Peripherals Library version number V1.1.3 + */ +#define __STM32F2XX_STDPERIPH_VERSION_MAIN (0x01) /*!< [31:24] main version */ +#define __STM32F2XX_STDPERIPH_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */ +#define __STM32F2XX_STDPERIPH_VERSION_SUB2 (0x03) /*!< [15:8] sub2 version */ +#define __STM32F2XX_STDPERIPH_VERSION_RC (0x00) /*!< [7:0] release candidate */ +#define __STM32F2XX_STDPERIPH_VERSION ((__STM32F2XX_STDPERIPH_VERSION_MAIN << 24)\ + |(__STM32F2XX_STDPERIPH_VERSION_SUB1 << 16)\ + |(__STM32F2XX_STDPERIPH_VERSION_SUB2 << 8)\ + |(__STM32F2XX_STDPERIPH_VERSION_RC)) + +/** + * @} + */ + +/** @addtogroup Configuration_section_for_CMSIS + * @{ + */ + +/** + * @brief Configuration of the Cortex-M3 Processor and Core Peripherals + */ +#define __CM3_REV 0x0200 /*!< Core Revision r2p0 */ +#define __MPU_PRESENT 1 /*!< STM32F2XX provides an MPU */ +#define __NVIC_PRIO_BITS 4 /*!< STM32F2XX uses 4 Bits for the Priority Levels */ +#define __Vendor_SysTickConfig 0 /*!< Set to 1 if different SysTick Config is used */ + +/** + * @brief STM32F2XX Interrupt Number Definition, according to the selected device + * in @ref Library_configuration_section + */ +typedef enum IRQn +{ +/****** Cortex-M3 Processor Exceptions Numbers ****************************************************************/ + NonMaskableInt_IRQn = -14, /*!< 2 Non Maskable Interrupt */ + MemoryManagement_IRQn = -12, /*!< 4 Cortex-M3 Memory Management Interrupt */ + BusFault_IRQn = -11, /*!< 5 Cortex-M3 Bus Fault Interrupt */ + UsageFault_IRQn = -10, /*!< 6 Cortex-M3 Usage Fault Interrupt */ + SVCall_IRQn = -5, /*!< 11 Cortex-M3 SV Call Interrupt */ + DebugMonitor_IRQn = -4, /*!< 12 Cortex-M3 Debug Monitor Interrupt */ + PendSV_IRQn = -2, /*!< 14 Cortex-M3 Pend SV Interrupt */ + SysTick_IRQn = -1, /*!< 15 Cortex-M3 System Tick Interrupt */ +/****** STM32 specific Interrupt Numbers **********************************************************************/ + WWDG_IRQn = 0, /*!< Window WatchDog Interrupt */ + PVD_IRQn = 1, /*!< PVD through EXTI Line detection Interrupt */ + TAMP_STAMP_IRQn = 2, /*!< Tamper and TimeStamp interrupts through the EXTI line */ + RTC_WKUP_IRQn = 3, /*!< RTC Wakeup interrupt through the EXTI line */ + FLASH_IRQn = 4, /*!< FLASH global Interrupt */ + RCC_IRQn = 5, /*!< RCC global Interrupt */ + EXTI0_IRQn = 6, /*!< EXTI Line0 Interrupt */ + EXTI1_IRQn = 7, /*!< EXTI Line1 Interrupt */ + EXTI2_IRQn = 8, /*!< EXTI Line2 Interrupt */ + EXTI3_IRQn = 9, /*!< EXTI Line3 Interrupt */ + EXTI4_IRQn = 10, /*!< EXTI Line4 Interrupt */ + DMA1_Stream0_IRQn = 11, /*!< DMA1 Stream 0 global Interrupt */ + DMA1_Stream1_IRQn = 12, /*!< DMA1 Stream 1 global Interrupt */ + DMA1_Stream2_IRQn = 13, /*!< DMA1 Stream 2 global Interrupt */ + DMA1_Stream3_IRQn = 14, /*!< DMA1 Stream 3 global Interrupt */ + DMA1_Stream4_IRQn = 15, /*!< DMA1 Stream 4 global Interrupt */ + DMA1_Stream5_IRQn = 16, /*!< DMA1 Stream 5 global Interrupt */ + DMA1_Stream6_IRQn = 17, /*!< DMA1 Stream 6 global Interrupt */ + ADC_IRQn = 18, /*!< ADC1, ADC2 and ADC3 global Interrupts */ + CAN1_TX_IRQn = 19, /*!< CAN1 TX Interrupt */ + CAN1_RX0_IRQn = 20, /*!< CAN1 RX0 Interrupt */ + CAN1_RX1_IRQn = 21, /*!< CAN1 RX1 Interrupt */ + CAN1_SCE_IRQn = 22, /*!< CAN1 SCE Interrupt */ + EXTI9_5_IRQn = 23, /*!< External Line[9:5] Interrupts */ + TIM1_BRK_TIM9_IRQn = 24, /*!< TIM1 Break interrupt and TIM9 global interrupt */ + TIM1_UP_TIM10_IRQn = 25, /*!< TIM1 Update Interrupt and TIM10 global interrupt */ + TIM1_TRG_COM_TIM11_IRQn = 26, /*!< TIM1 Trigger and Commutation Interrupt and TIM11 global interrupt */ + TIM1_CC_IRQn = 27, /*!< TIM1 Capture Compare Interrupt */ + TIM2_IRQn = 28, /*!< TIM2 global Interrupt */ + TIM3_IRQn = 29, /*!< TIM3 global Interrupt */ + TIM4_IRQn = 30, /*!< TIM4 global Interrupt */ + I2C1_EV_IRQn = 31, /*!< I2C1 Event Interrupt */ + I2C1_ER_IRQn = 32, /*!< I2C1 Error Interrupt */ + I2C2_EV_IRQn = 33, /*!< I2C2 Event Interrupt */ + I2C2_ER_IRQn = 34, /*!< I2C2 Error Interrupt */ + SPI1_IRQn = 35, /*!< SPI1 global Interrupt */ + SPI2_IRQn = 36, /*!< SPI2 global Interrupt */ + USART1_IRQn = 37, /*!< USART1 global Interrupt */ + USART2_IRQn = 38, /*!< USART2 global Interrupt */ + USART3_IRQn = 39, /*!< USART3 global Interrupt */ + EXTI15_10_IRQn = 40, /*!< External Line[15:10] Interrupts */ + RTC_Alarm_IRQn = 41, /*!< RTC Alarm (A and B) through EXTI Line Interrupt */ + OTG_FS_WKUP_IRQn = 42, /*!< USB OTG FS Wakeup through EXTI line interrupt */ + TIM8_BRK_TIM12_IRQn = 43, /*!< TIM8 Break Interrupt and TIM12 global interrupt */ + TIM8_UP_TIM13_IRQn = 44, /*!< TIM8 Update Interrupt and TIM13 global interrupt */ + TIM8_TRG_COM_TIM14_IRQn = 45, /*!< TIM8 Trigger and Commutation Interrupt and TIM14 global interrupt */ + TIM8_CC_IRQn = 46, /*!< TIM8 Capture Compare Interrupt */ + DMA1_Stream7_IRQn = 47, /*!< DMA1 Stream7 Interrupt */ + FSMC_IRQn = 48, /*!< FSMC global Interrupt */ + SDIO_IRQn = 49, /*!< SDIO global Interrupt */ + TIM5_IRQn = 50, /*!< TIM5 global Interrupt */ + SPI3_IRQn = 51, /*!< SPI3 global Interrupt */ + UART4_IRQn = 52, /*!< UART4 global Interrupt */ + UART5_IRQn = 53, /*!< UART5 global Interrupt */ + TIM6_DAC_IRQn = 54, /*!< TIM6 global and DAC1&2 underrun error interrupts */ + TIM7_IRQn = 55, /*!< TIM7 global interrupt */ + DMA2_Stream0_IRQn = 56, /*!< DMA2 Stream 0 global Interrupt */ + DMA2_Stream1_IRQn = 57, /*!< DMA2 Stream 1 global Interrupt */ + DMA2_Stream2_IRQn = 58, /*!< DMA2 Stream 2 global Interrupt */ + DMA2_Stream3_IRQn = 59, /*!< DMA2 Stream 3 global Interrupt */ + DMA2_Stream4_IRQn = 60, /*!< DMA2 Stream 4 global Interrupt */ + ETH_IRQn = 61, /*!< Ethernet global Interrupt */ + ETH_WKUP_IRQn = 62, /*!< Ethernet Wakeup through EXTI line Interrupt */ + CAN2_TX_IRQn = 63, /*!< CAN2 TX Interrupt */ + CAN2_RX0_IRQn = 64, /*!< CAN2 RX0 Interrupt */ + CAN2_RX1_IRQn = 65, /*!< CAN2 RX1 Interrupt */ + CAN2_SCE_IRQn = 66, /*!< CAN2 SCE Interrupt */ + OTG_FS_IRQn = 67, /*!< USB OTG FS global Interrupt */ + DMA2_Stream5_IRQn = 68, /*!< DMA2 Stream 5 global interrupt */ + DMA2_Stream6_IRQn = 69, /*!< DMA2 Stream 6 global interrupt */ + DMA2_Stream7_IRQn = 70, /*!< DMA2 Stream 7 global interrupt */ + USART6_IRQn = 71, /*!< USART6 global interrupt */ + I2C3_EV_IRQn = 72, /*!< I2C3 event interrupt */ + I2C3_ER_IRQn = 73, /*!< I2C3 error interrupt */ + OTG_HS_EP1_OUT_IRQn = 74, /*!< USB OTG HS End Point 1 Out global interrupt */ + OTG_HS_EP1_IN_IRQn = 75, /*!< USB OTG HS End Point 1 In global interrupt */ + OTG_HS_WKUP_IRQn = 76, /*!< USB OTG HS Wakeup through EXTI interrupt */ + OTG_HS_IRQn = 77, /*!< USB OTG HS global interrupt */ + DCMI_IRQn = 78, /*!< DCMI global interrupt */ + CRYP_IRQn = 79, /*!< CRYP crypto global interrupt */ + HASH_RNG_IRQn = 80 /*!< Hash and Rng global interrupt */ +} IRQn_Type; + +/** + * @} + */ + +#include "core_cm3.h" +#include "system_stm32f2xx.h" +#include + +/** @addtogroup Exported_types + * @{ + */ +/*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */ +typedef int32_t s32; +typedef int16_t s16; +typedef int8_t s8; + +typedef const int32_t sc32; /*!< Read Only */ +typedef const int16_t sc16; /*!< Read Only */ +typedef const int8_t sc8; /*!< Read Only */ + +typedef __IO int32_t vs32; +typedef __IO int16_t vs16; +typedef __IO int8_t vs8; + +typedef __I int32_t vsc32; /*!< Read Only */ +typedef __I int16_t vsc16; /*!< Read Only */ +typedef __I int8_t vsc8; /*!< Read Only */ + +typedef uint32_t u32; +typedef uint16_t u16; +typedef uint8_t u8; + +typedef const uint32_t uc32; /*!< Read Only */ +typedef const uint16_t uc16; /*!< Read Only */ +typedef const uint8_t uc8; /*!< Read Only */ + +typedef __IO uint32_t vu32; +typedef __IO uint16_t vu16; +typedef __IO uint8_t vu8; + +typedef __I uint32_t vuc32; /*!< Read Only */ +typedef __I uint16_t vuc16; /*!< Read Only */ +typedef __I uint8_t vuc8; /*!< Read Only */ + +typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus; + +typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; +#define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE)) + +typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus; + +/** + * @} + */ + +/** @addtogroup Peripheral_registers_structures + * @{ + */ + +/** + * @brief Analog to Digital Converter + */ + +typedef struct +{ + __IO uint32_t SR; /*!< ADC status register, Address offset: 0x00 */ + __IO uint32_t CR1; /*!< ADC control register 1, Address offset: 0x04 */ + __IO uint32_t CR2; /*!< ADC control register 2, Address offset: 0x08 */ + __IO uint32_t SMPR1; /*!< ADC sample time register 1, Address offset: 0x0C */ + __IO uint32_t SMPR2; /*!< ADC sample time register 2, Address offset: 0x10 */ + __IO uint32_t JOFR1; /*!< ADC injected channel data offset register 1, Address offset: 0x14 */ + __IO uint32_t JOFR2; /*!< ADC injected channel data offset register 2, Address offset: 0x18 */ + __IO uint32_t JOFR3; /*!< ADC injected channel data offset register 3, Address offset: 0x1C */ + __IO uint32_t JOFR4; /*!< ADC injected channel data offset register 4, Address offset: 0x20 */ + __IO uint32_t HTR; /*!< ADC watchdog higher threshold register, Address offset: 0x24 */ + __IO uint32_t LTR; /*!< ADC watchdog lower threshold register, Address offset: 0x28 */ + __IO uint32_t SQR1; /*!< ADC regular sequence register 1, Address offset: 0x2C */ + __IO uint32_t SQR2; /*!< ADC regular sequence register 2, Address offset: 0x30 */ + __IO uint32_t SQR3; /*!< ADC regular sequence register 3, Address offset: 0x34 */ + __IO uint32_t JSQR; /*!< ADC injected sequence register, Address offset: 0x38*/ + __IO uint32_t JDR1; /*!< ADC injected data register 1, Address offset: 0x3C */ + __IO uint32_t JDR2; /*!< ADC injected data register 2, Address offset: 0x40 */ + __IO uint32_t JDR3; /*!< ADC injected data register 3, Address offset: 0x44 */ + __IO uint32_t JDR4; /*!< ADC injected data register 4, Address offset: 0x48 */ + __IO uint32_t DR; /*!< ADC regular data register, Address offset: 0x4C */ +} ADC_TypeDef; + +typedef struct +{ + __IO uint32_t CSR; /*!< ADC Common status register, Address offset: ADC1 base address + 0x300 */ + __IO uint32_t CCR; /*!< ADC common control register, Address offset: ADC1 base address + 0x304 */ + __IO uint32_t CDR; /*!< ADC common regular data register for dual + AND triple modes, Address offset: ADC1 base address + 0x308 */ +} ADC_Common_TypeDef; + + +/** + * @brief Controller Area Network TxMailBox + */ + +typedef struct +{ + __IO uint32_t TIR; /*!< CAN TX mailbox identifier register */ + __IO uint32_t TDTR; /*!< CAN mailbox data length control and time stamp register */ + __IO uint32_t TDLR; /*!< CAN mailbox data low register */ + __IO uint32_t TDHR; /*!< CAN mailbox data high register */ +} CAN_TxMailBox_TypeDef; + +/** + * @brief Controller Area Network FIFOMailBox + */ + +typedef struct +{ + __IO uint32_t RIR; /*!< CAN receive FIFO mailbox identifier register */ + __IO uint32_t RDTR; /*!< CAN receive FIFO mailbox data length control and time stamp register */ + __IO uint32_t RDLR; /*!< CAN receive FIFO mailbox data low register */ + __IO uint32_t RDHR; /*!< CAN receive FIFO mailbox data high register */ +} CAN_FIFOMailBox_TypeDef; + +/** + * @brief Controller Area Network FilterRegister + */ + +typedef struct +{ + __IO uint32_t FR1; /*!< CAN Filter bank register 1 */ + __IO uint32_t FR2; /*!< CAN Filter bank register 1 */ +} CAN_FilterRegister_TypeDef; + +/** + * @brief Controller Area Network + */ + +typedef struct +{ + __IO uint32_t MCR; /*!< CAN master control register, Address offset: 0x00 */ + __IO uint32_t MSR; /*!< CAN master status register, Address offset: 0x04 */ + __IO uint32_t TSR; /*!< CAN transmit status register, Address offset: 0x08 */ + __IO uint32_t RF0R; /*!< CAN receive FIFO 0 register, Address offset: 0x0C */ + __IO uint32_t RF1R; /*!< CAN receive FIFO 1 register, Address offset: 0x10 */ + __IO uint32_t IER; /*!< CAN interrupt enable register, Address offset: 0x14 */ + __IO uint32_t ESR; /*!< CAN error status register, Address offset: 0x18 */ + __IO uint32_t BTR; /*!< CAN bit timing register, Address offset: 0x1C */ + uint32_t RESERVED0[88]; /*!< Reserved, 0x020 - 0x17F */ + CAN_TxMailBox_TypeDef sTxMailBox[3]; /*!< CAN Tx MailBox, Address offset: 0x180 - 0x1AC */ + CAN_FIFOMailBox_TypeDef sFIFOMailBox[2]; /*!< CAN FIFO MailBox, Address offset: 0x1B0 - 0x1CC */ + uint32_t RESERVED1[12]; /*!< Reserved, 0x1D0 - 0x1FF */ + __IO uint32_t FMR; /*!< CAN filter master register, Address offset: 0x200 */ + __IO uint32_t FM1R; /*!< CAN filter mode register, Address offset: 0x204 */ + uint32_t RESERVED2; /*!< Reserved, 0x208 */ + __IO uint32_t FS1R; /*!< CAN filter scale register, Address offset: 0x20C */ + uint32_t RESERVED3; /*!< Reserved, 0x210 */ + __IO uint32_t FFA1R; /*!< CAN filter FIFO assignment register, Address offset: 0x214 */ + uint32_t RESERVED4; /*!< Reserved, 0x218 */ + __IO uint32_t FA1R; /*!< CAN filter activation register, Address offset: 0x21C */ + uint32_t RESERVED5[8]; /*!< Reserved, 0x220-0x23F */ + CAN_FilterRegister_TypeDef sFilterRegister[28]; /*!< CAN Filter Register, Address offset: 0x240-0x31C */ +} CAN_TypeDef; + +/** + * @brief CRC calculation unit + */ + +typedef struct +{ + __IO uint32_t DR; /*!< CRC Data register, Address offset: 0x00 */ + __IO uint8_t IDR; /*!< CRC Independent data register, Address offset: 0x04 */ + uint8_t RESERVED0; /*!< Reserved, 0x05 */ + uint16_t RESERVED1; /*!< Reserved, 0x06 */ + __IO uint32_t CR; /*!< CRC Control register, Address offset: 0x08 */ +} CRC_TypeDef; + +/** + * @brief Digital to Analog Converter + */ + +typedef struct +{ + __IO uint32_t CR; /*!< DAC control register, Address offset: 0x00 */ + __IO uint32_t SWTRIGR; /*!< DAC software trigger register, Address offset: 0x04 */ + __IO uint32_t DHR12R1; /*!< DAC channel1 12-bit right-aligned data holding register, Address offset: 0x08 */ + __IO uint32_t DHR12L1; /*!< DAC channel1 12-bit left aligned data holding register, Address offset: 0x0C */ + __IO uint32_t DHR8R1; /*!< DAC channel1 8-bit right aligned data holding register, Address offset: 0x10 */ + __IO uint32_t DHR12R2; /*!< DAC channel2 12-bit right aligned data holding register, Address offset: 0x14 */ + __IO uint32_t DHR12L2; /*!< DAC channel2 12-bit left aligned data holding register, Address offset: 0x18 */ + __IO uint32_t DHR8R2; /*!< DAC channel2 8-bit right-aligned data holding register, Address offset: 0x1C */ + __IO uint32_t DHR12RD; /*!< Dual DAC 12-bit right-aligned data holding register, Address offset: 0x20 */ + __IO uint32_t DHR12LD; /*!< DUAL DAC 12-bit left aligned data holding register, Address offset: 0x24 */ + __IO uint32_t DHR8RD; /*!< DUAL DAC 8-bit right aligned data holding register, Address offset: 0x28 */ + __IO uint32_t DOR1; /*!< DAC channel1 data output register, Address offset: 0x2C */ + __IO uint32_t DOR2; /*!< DAC channel2 data output register, Address offset: 0x30 */ + __IO uint32_t SR; /*!< DAC status register, Address offset: 0x34 */ +} DAC_TypeDef; + +/** + * @brief Debug MCU + */ + +typedef struct +{ + __IO uint32_t IDCODE; /*!< MCU device ID code, Address offset: 0x00 */ + __IO uint32_t CR; /*!< Debug MCU configuration register, Address offset: 0x04 */ + __IO uint32_t APB1FZ; /*!< Debug MCU APB1 freeze register, Address offset: 0x08 */ + __IO uint32_t APB2FZ; /*!< Debug MCU APB2 freeze register, Address offset: 0x0C */ +}DBGMCU_TypeDef; + +/** + * @brief DCMI + */ + +typedef struct +{ + __IO uint32_t CR; /*!< DCMI control register 1, Address offset: 0x00 */ + __IO uint32_t SR; /*!< DCMI status register, Address offset: 0x04 */ + __IO uint32_t RISR; /*!< DCMI raw interrupt status register, Address offset: 0x08 */ + __IO uint32_t IER; /*!< DCMI interrupt enable register, Address offset: 0x0C */ + __IO uint32_t MISR; /*!< DCMI masked interrupt status register, Address offset: 0x10 */ + __IO uint32_t ICR; /*!< DCMI interrupt clear register, Address offset: 0x14 */ + __IO uint32_t ESCR; /*!< DCMI embedded synchronization code register, Address offset: 0x18 */ + __IO uint32_t ESUR; /*!< DCMI embedded synchronization unmask register, Address offset: 0x1C */ + __IO uint32_t CWSTRTR; /*!< DCMI crop window start, Address offset: 0x20 */ + __IO uint32_t CWSIZER; /*!< DCMI crop window size, Address offset: 0x24 */ + __IO uint32_t DR; /*!< DCMI data register, Address offset: 0x28 */ +} DCMI_TypeDef; + +/** + * @brief DMA Controller + */ + +typedef struct +{ + __IO uint32_t CR; /*!< DMA stream x configuration register */ + __IO uint32_t NDTR; /*!< DMA stream x number of data register */ + __IO uint32_t PAR; /*!< DMA stream x peripheral address register */ + __IO uint32_t M0AR; /*!< DMA stream x memory 0 address register */ + __IO uint32_t M1AR; /*!< DMA stream x memory 1 address register */ + __IO uint32_t FCR; /*!< DMA stream x FIFO control register */ +} DMA_Stream_TypeDef; + +typedef struct +{ + __IO uint32_t LISR; /*!< DMA low interrupt status register, Address offset: 0x00 */ + __IO uint32_t HISR; /*!< DMA high interrupt status register, Address offset: 0x04 */ + __IO uint32_t LIFCR; /*!< DMA low interrupt flag clear register, Address offset: 0x08 */ + __IO uint32_t HIFCR; /*!< DMA high interrupt flag clear register, Address offset: 0x0C */ +} DMA_TypeDef; + +/** + * @brief Ethernet MAC + */ + +typedef struct +{ + __IO uint32_t MACCR; + __IO uint32_t MACFFR; + __IO uint32_t MACHTHR; + __IO uint32_t MACHTLR; + __IO uint32_t MACMIIAR; + __IO uint32_t MACMIIDR; + __IO uint32_t MACFCR; + __IO uint32_t MACVLANTR; /* 8 */ + uint32_t RESERVED0[2]; + __IO uint32_t MACRWUFFR; /* 11 */ + __IO uint32_t MACPMTCSR; + uint32_t RESERVED1[2]; + __IO uint32_t MACSR; /* 15 */ + __IO uint32_t MACIMR; + __IO uint32_t MACA0HR; + __IO uint32_t MACA0LR; + __IO uint32_t MACA1HR; + __IO uint32_t MACA1LR; + __IO uint32_t MACA2HR; + __IO uint32_t MACA2LR; + __IO uint32_t MACA3HR; + __IO uint32_t MACA3LR; /* 24 */ + uint32_t RESERVED2[40]; + __IO uint32_t MMCCR; /* 65 */ + __IO uint32_t MMCRIR; + __IO uint32_t MMCTIR; + __IO uint32_t MMCRIMR; + __IO uint32_t MMCTIMR; /* 69 */ + uint32_t RESERVED3[14]; + __IO uint32_t MMCTGFSCCR; /* 84 */ + __IO uint32_t MMCTGFMSCCR; + uint32_t RESERVED4[5]; + __IO uint32_t MMCTGFCR; + uint32_t RESERVED5[10]; + __IO uint32_t MMCRFCECR; + __IO uint32_t MMCRFAECR; + uint32_t RESERVED6[10]; + __IO uint32_t MMCRGUFCR; + uint32_t RESERVED7[334]; + __IO uint32_t PTPTSCR; + __IO uint32_t PTPSSIR; + __IO uint32_t PTPTSHR; + __IO uint32_t PTPTSLR; + __IO uint32_t PTPTSHUR; + __IO uint32_t PTPTSLUR; + __IO uint32_t PTPTSAR; + __IO uint32_t PTPTTHR; + __IO uint32_t PTPTTLR; + __IO uint32_t RESERVED8; + __IO uint32_t PTPTSSR; /* added for STM32F2xx */ + uint32_t RESERVED9[565]; + __IO uint32_t DMABMR; + __IO uint32_t DMATPDR; + __IO uint32_t DMARPDR; + __IO uint32_t DMARDLAR; + __IO uint32_t DMATDLAR; + __IO uint32_t DMASR; + __IO uint32_t DMAOMR; + __IO uint32_t DMAIER; + __IO uint32_t DMAMFBOCR; + __IO uint32_t DMARSWTR; /* added for STM32F2xx */ + uint32_t RESERVED10[8]; + __IO uint32_t DMACHTDR; + __IO uint32_t DMACHRDR; + __IO uint32_t DMACHTBAR; + __IO uint32_t DMACHRBAR; +} ETH_TypeDef; + +/** + * @brief External Interrupt/Event Controller + */ + +typedef struct +{ + __IO uint32_t IMR; /*!< EXTI Interrupt mask register, Address offset: 0x00 */ + __IO uint32_t EMR; /*!< EXTI Event mask register, Address offset: 0x04 */ + __IO uint32_t RTSR; /*!< EXTI Rising trigger selection register, Address offset: 0x08 */ + __IO uint32_t FTSR; /*!< EXTI Falling trigger selection register, Address offset: 0x0C */ + __IO uint32_t SWIER; /*!< EXTI Software interrupt event register, Address offset: 0x10 */ + __IO uint32_t PR; /*!< EXTI Pending register, Address offset: 0x14 */ +} EXTI_TypeDef; + +/** + * @brief FLASH Registers + */ + +typedef struct +{ + __IO uint32_t ACR; /*!< FLASH access control register, Address offset: 0x00 */ + __IO uint32_t KEYR; /*!< FLASH key register, Address offset: 0x04 */ + __IO uint32_t OPTKEYR; /*!< FLASH option key register, Address offset: 0x08 */ + __IO uint32_t SR; /*!< FLASH status register, Address offset: 0x0C */ + __IO uint32_t CR; /*!< FLASH control register, Address offset: 0x10 */ + __IO uint32_t OPTCR; /*!< FLASH option control register, Address offset: 0x14 */ +} FLASH_TypeDef; + +/** + * @brief Flexible Static Memory Controller + */ + +typedef struct +{ + __IO uint32_t BTCR[8]; /*!< NOR/PSRAM chip-select control register(BCR) and chip-select timing register(BTR), Address offset: 0x00-1C */ +} FSMC_Bank1_TypeDef; + +/** + * @brief Flexible Static Memory Controller Bank1E + */ + +typedef struct +{ + __IO uint32_t BWTR[7]; /*!< NOR/PSRAM write timing registers, Address offset: 0x104-0x11C */ +} FSMC_Bank1E_TypeDef; + +/** + * @brief Flexible Static Memory Controller Bank2 + */ + +typedef struct +{ + __IO uint32_t PCR2; /*!< NAND Flash control register 2, Address offset: 0x60 */ + __IO uint32_t SR2; /*!< NAND Flash FIFO status and interrupt register 2, Address offset: 0x64 */ + __IO uint32_t PMEM2; /*!< NAND Flash Common memory space timing register 2, Address offset: 0x68 */ + __IO uint32_t PATT2; /*!< NAND Flash Attribute memory space timing register 2, Address offset: 0x6C */ + uint32_t RESERVED0; /*!< Reserved, 0x70 */ + __IO uint32_t ECCR2; /*!< NAND Flash ECC result registers 2, Address offset: 0x74 */ +} FSMC_Bank2_TypeDef; + +/** + * @brief Flexible Static Memory Controller Bank3 + */ + +typedef struct +{ + __IO uint32_t PCR3; /*!< NAND Flash control register 3, Address offset: 0x80 */ + __IO uint32_t SR3; /*!< NAND Flash FIFO status and interrupt register 3, Address offset: 0x84 */ + __IO uint32_t PMEM3; /*!< NAND Flash Common memory space timing register 3, Address offset: 0x88 */ + __IO uint32_t PATT3; /*!< NAND Flash Attribute memory space timing register 3, Address offset: 0x8C */ + uint32_t RESERVED0; /*!< Reserved, 0x90 */ + __IO uint32_t ECCR3; /*!< NAND Flash ECC result registers 3, Address offset: 0x94 */ +} FSMC_Bank3_TypeDef; + +/** + * @brief Flexible Static Memory Controller Bank4 + */ + +typedef struct +{ + __IO uint32_t PCR4; /*!< PC Card control register 4, Address offset: 0xA0 */ + __IO uint32_t SR4; /*!< PC Card FIFO status and interrupt register 4, Address offset: 0xA4 */ + __IO uint32_t PMEM4; /*!< PC Card Common memory space timing register 4, Address offset: 0xA8 */ + __IO uint32_t PATT4; /*!< PC Card Attribute memory space timing register 4, Address offset: 0xAC */ + __IO uint32_t PIO4; /*!< PC Card I/O space timing register 4, Address offset: 0xB0 */ +} FSMC_Bank4_TypeDef; + +/** + * @brief General Purpose I/O + */ + +typedef struct +{ + __IO uint32_t MODER; /*!< GPIO port mode register, Address offset: 0x00 */ + __IO uint32_t OTYPER; /*!< GPIO port output type register, Address offset: 0x04 */ + __IO uint32_t OSPEEDR; /*!< GPIO port output speed register, Address offset: 0x08 */ + __IO uint32_t PUPDR; /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ + __IO uint32_t IDR; /*!< GPIO port input data register, Address offset: 0x10 */ + __IO uint32_t ODR; /*!< GPIO port output data register, Address offset: 0x14 */ + __IO uint16_t BSRRL; /*!< GPIO port bit set/reset low register, Address offset: 0x18 */ + __IO uint16_t BSRRH; /*!< GPIO port bit set/reset high register, Address offset: 0x1A */ + __IO uint32_t LCKR; /*!< GPIO port configuration lock register, Address offset: 0x1C */ + __IO uint32_t AFR[2]; /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ +} GPIO_TypeDef; + +/** + * @brief System configuration controller + */ + +typedef struct +{ + __IO uint32_t MEMRMP; /*!< SYSCFG memory remap register, Address offset: 0x00 */ + __IO uint32_t PMC; /*!< SYSCFG peripheral mode configuration register, Address offset: 0x04 */ + __IO uint32_t EXTICR[4]; /*!< SYSCFG external interrupt configuration registers, Address offset: 0x08-0x14 */ + uint32_t RESERVED[2]; /*!< Reserved, 0x18-0x1C */ + __IO uint32_t CMPCR; /*!< SYSCFG Compensation cell control register, Address offset: 0x20 */ +} SYSCFG_TypeDef; + +/** + * @brief Inter-integrated Circuit Interface + */ + +typedef struct +{ + __IO uint16_t CR1; /*!< I2C Control register 1, Address offset: 0x00 */ + uint16_t RESERVED0; /*!< Reserved, 0x02 */ + __IO uint16_t CR2; /*!< I2C Control register 2, Address offset: 0x04 */ + uint16_t RESERVED1; /*!< Reserved, 0x06 */ + __IO uint16_t OAR1; /*!< I2C Own address register 1, Address offset: 0x08 */ + uint16_t RESERVED2; /*!< Reserved, 0x0A */ + __IO uint16_t OAR2; /*!< I2C Own address register 2, Address offset: 0x0C */ + uint16_t RESERVED3; /*!< Reserved, 0x0E */ + __IO uint16_t DR; /*!< I2C Data register, Address offset: 0x10 */ + uint16_t RESERVED4; /*!< Reserved, 0x12 */ + __IO uint16_t SR1; /*!< I2C Status register 1, Address offset: 0x14 */ + uint16_t RESERVED5; /*!< Reserved, 0x16 */ + __IO uint16_t SR2; /*!< I2C Status register 2, Address offset: 0x18 */ + uint16_t RESERVED6; /*!< Reserved, 0x1A */ + __IO uint16_t CCR; /*!< I2C Clock control register, Address offset: 0x1C */ + uint16_t RESERVED7; /*!< Reserved, 0x1E */ + __IO uint16_t TRISE; /*!< I2C TRISE register, Address offset: 0x20 */ + uint16_t RESERVED8; /*!< Reserved, 0x22 */ +} I2C_TypeDef; + +/** + * @brief Independent WATCHDOG + */ + +typedef struct +{ + __IO uint32_t KR; /*!< IWDG Key register, Address offset: 0x00 */ + __IO uint32_t PR; /*!< IWDG Prescaler register, Address offset: 0x04 */ + __IO uint32_t RLR; /*!< IWDG Reload register, Address offset: 0x08 */ + __IO uint32_t SR; /*!< IWDG Status register, Address offset: 0x0C */ +} IWDG_TypeDef; + +/** + * @brief Power Control + */ + +typedef struct +{ + __IO uint32_t CR; /*!< PWR power control register, Address offset: 0x00 */ + __IO uint32_t CSR; /*!< PWR power control/status register, Address offset: 0x04 */ +} PWR_TypeDef; + +/** + * @brief Reset and Clock Control + */ + +typedef struct +{ + __IO uint32_t CR; /*!< RCC clock control register, Address offset: 0x00 */ + __IO uint32_t PLLCFGR; /*!< RCC PLL configuration register, Address offset: 0x04 */ + __IO uint32_t CFGR; /*!< RCC clock configuration register, Address offset: 0x08 */ + __IO uint32_t CIR; /*!< RCC clock interrupt register, Address offset: 0x0C */ + __IO uint32_t AHB1RSTR; /*!< RCC AHB1 peripheral reset register, Address offset: 0x10 */ + __IO uint32_t AHB2RSTR; /*!< RCC AHB2 peripheral reset register, Address offset: 0x14 */ + __IO uint32_t AHB3RSTR; /*!< RCC AHB3 peripheral reset register, Address offset: 0x18 */ + uint32_t RESERVED0; /*!< Reserved, 0x1C */ + __IO uint32_t APB1RSTR; /*!< RCC APB1 peripheral reset register, Address offset: 0x20 */ + __IO uint32_t APB2RSTR; /*!< RCC APB2 peripheral reset register, Address offset: 0x24 */ + uint32_t RESERVED1[2]; /*!< Reserved, 0x28-0x2C */ + __IO uint32_t AHB1ENR; /*!< RCC AHB1 peripheral clock register, Address offset: 0x30 */ + __IO uint32_t AHB2ENR; /*!< RCC AHB2 peripheral clock register, Address offset: 0x34 */ + __IO uint32_t AHB3ENR; /*!< RCC AHB3 peripheral clock register, Address offset: 0x38 */ + uint32_t RESERVED2; /*!< Reserved, 0x3C */ + __IO uint32_t APB1ENR; /*!< RCC APB1 peripheral clock enable register, Address offset: 0x40 */ + __IO uint32_t APB2ENR; /*!< RCC APB2 peripheral clock enable register, Address offset: 0x44 */ + uint32_t RESERVED3[2]; /*!< Reserved, 0x48-0x4C */ + __IO uint32_t AHB1LPENR; /*!< RCC AHB1 peripheral clock enable in low power mode register, Address offset: 0x50 */ + __IO uint32_t AHB2LPENR; /*!< RCC AHB2 peripheral clock enable in low power mode register, Address offset: 0x54 */ + __IO uint32_t AHB3LPENR; /*!< RCC AHB3 peripheral clock enable in low power mode register, Address offset: 0x58 */ + uint32_t RESERVED4; /*!< Reserved, 0x5C */ + __IO uint32_t APB1LPENR; /*!< RCC APB1 peripheral clock enable in low power mode register, Address offset: 0x60 */ + __IO uint32_t APB2LPENR; /*!< RCC APB2 peripheral clock enable in low power mode register, Address offset: 0x64 */ + uint32_t RESERVED5[2]; /*!< Reserved, 0x68-0x6C */ + __IO uint32_t BDCR; /*!< RCC Backup domain control register, Address offset: 0x70 */ + __IO uint32_t CSR; /*!< RCC clock control & status register, Address offset: 0x74 */ + uint32_t RESERVED6[2]; /*!< Reserved, 0x78-0x7C */ + __IO uint32_t SSCGR; /*!< RCC spread spectrum clock generation register, Address offset: 0x80 */ + __IO uint32_t PLLI2SCFGR; /*!< RCC PLLI2S configuration register, Address offset: 0x84 */ +} RCC_TypeDef; + +/** + * @brief Real-Time Clock + */ + +typedef struct +{ + __IO uint32_t TR; /*!< RTC time register, Address offset: 0x00 */ + __IO uint32_t DR; /*!< RTC date register, Address offset: 0x04 */ + __IO uint32_t CR; /*!< RTC control register, Address offset: 0x08 */ + __IO uint32_t ISR; /*!< RTC initialization and status register, Address offset: 0x0C */ + __IO uint32_t PRER; /*!< RTC prescaler register, Address offset: 0x10 */ + __IO uint32_t WUTR; /*!< RTC wakeup timer register, Address offset: 0x14 */ + __IO uint32_t CALIBR; /*!< RTC calibration register, Address offset: 0x18 */ + __IO uint32_t ALRMAR; /*!< RTC alarm A register, Address offset: 0x1C */ + __IO uint32_t ALRMBR; /*!< RTC alarm B register, Address offset: 0x20 */ + __IO uint32_t WPR; /*!< RTC write protection register, Address offset: 0x24 */ + uint32_t RESERVED1; /*!< Reserved, 0x28 */ + uint32_t RESERVED2; /*!< Reserved, 0x2C */ + __IO uint32_t TSTR; /*!< RTC time stamp time register, Address offset: 0x30 */ + __IO uint32_t TSDR; /*!< RTC time stamp date register, Address offset: 0x34 */ + uint32_t RESERVED3; /*!< Reserved, 0x38 */ + uint32_t RESERVED4; /*!< Reserved, 0x3C */ + __IO uint32_t TAFCR; /*!< RTC tamper and alternate function configuration register, Address offset: 0x40 */ + uint32_t RESERVED5; /*!< Reserved, 0x44 */ + uint32_t RESERVED6; /*!< Reserved, 0x48 */ + uint32_t RESERVED7; /*!< Reserved, 0x4C */ + __IO uint32_t BKP0R; /*!< RTC backup register 1, Address offset: 0x50 */ + __IO uint32_t BKP1R; /*!< RTC backup register 1, Address offset: 0x54 */ + __IO uint32_t BKP2R; /*!< RTC backup register 2, Address offset: 0x58 */ + __IO uint32_t BKP3R; /*!< RTC backup register 3, Address offset: 0x5C */ + __IO uint32_t BKP4R; /*!< RTC backup register 4, Address offset: 0x60 */ + __IO uint32_t BKP5R; /*!< RTC backup register 5, Address offset: 0x64 */ + __IO uint32_t BKP6R; /*!< RTC backup register 6, Address offset: 0x68 */ + __IO uint32_t BKP7R; /*!< RTC backup register 7, Address offset: 0x6C */ + __IO uint32_t BKP8R; /*!< RTC backup register 8, Address offset: 0x70 */ + __IO uint32_t BKP9R; /*!< RTC backup register 9, Address offset: 0x74 */ + __IO uint32_t BKP10R; /*!< RTC backup register 10, Address offset: 0x78 */ + __IO uint32_t BKP11R; /*!< RTC backup register 11, Address offset: 0x7C */ + __IO uint32_t BKP12R; /*!< RTC backup register 12, Address offset: 0x80 */ + __IO uint32_t BKP13R; /*!< RTC backup register 13, Address offset: 0x84 */ + __IO uint32_t BKP14R; /*!< RTC backup register 14, Address offset: 0x88 */ + __IO uint32_t BKP15R; /*!< RTC backup register 15, Address offset: 0x8C */ + __IO uint32_t BKP16R; /*!< RTC backup register 16, Address offset: 0x90 */ + __IO uint32_t BKP17R; /*!< RTC backup register 17, Address offset: 0x94 */ + __IO uint32_t BKP18R; /*!< RTC backup register 18, Address offset: 0x98 */ + __IO uint32_t BKP19R; /*!< RTC backup register 19, Address offset: 0x9C */ +} RTC_TypeDef; + +/** + * @brief SD host Interface + */ + +typedef struct +{ + __IO uint32_t POWER; /*!< SDIO power control register, Address offset: 0x00 */ + __IO uint32_t CLKCR; /*!< SDI clock control register, Address offset: 0x04 */ + __IO uint32_t ARG; /*!< SDIO argument register, Address offset: 0x08 */ + __IO uint32_t CMD; /*!< SDIO command register, Address offset: 0x0C */ + __I uint32_t RESPCMD; /*!< SDIO command response register, Address offset: 0x10 */ + __I uint32_t RESP1; /*!< SDIO response 1 register, Address offset: 0x14 */ + __I uint32_t RESP2; /*!< SDIO response 2 register, Address offset: 0x18 */ + __I uint32_t RESP3; /*!< SDIO response 3 register, Address offset: 0x1C */ + __I uint32_t RESP4; /*!< SDIO response 4 register, Address offset: 0x20 */ + __IO uint32_t DTIMER; /*!< SDIO data timer register, Address offset: 0x24 */ + __IO uint32_t DLEN; /*!< SDIO data length register, Address offset: 0x28 */ + __IO uint32_t DCTRL; /*!< SDIO data control register, Address offset: 0x2C */ + __I uint32_t DCOUNT; /*!< SDIO data counter register, Address offset: 0x30 */ + __I uint32_t STA; /*!< SDIO status register, Address offset: 0x34 */ + __IO uint32_t ICR; /*!< SDIO interrupt clear register, Address offset: 0x38 */ + __IO uint32_t MASK; /*!< SDIO mask register, Address offset: 0x3C */ + uint32_t RESERVED0[2]; /*!< Reserved, 0x40-0x44 */ + __I uint32_t FIFOCNT; /*!< SDIO FIFO counter register, Address offset: 0x48 */ + uint32_t RESERVED1[13]; /*!< Reserved, 0x4C-0x7C */ + __IO uint32_t FIFO; /*!< SDIO data FIFO register, Address offset: 0x80 */ +} SDIO_TypeDef; + +/** + * @brief Serial Peripheral Interface + */ + +typedef struct +{ + __IO uint16_t CR1; /*!< SPI control register 1 (not used in I2S mode), Address offset: 0x00 */ + uint16_t RESERVED0; /*!< Reserved, 0x02 */ + __IO uint16_t CR2; /*!< SPI control register 2, Address offset: 0x04 */ + uint16_t RESERVED1; /*!< Reserved, 0x06 */ + __IO uint16_t SR; /*!< SPI status register, Address offset: 0x08 */ + uint16_t RESERVED2; /*!< Reserved, 0x0A */ + __IO uint16_t DR; /*!< SPI data register, Address offset: 0x0C */ + uint16_t RESERVED3; /*!< Reserved, 0x0E */ + __IO uint16_t CRCPR; /*!< SPI CRC polynomial register (not used in I2S mode), Address offset: 0x10 */ + uint16_t RESERVED4; /*!< Reserved, 0x12 */ + __IO uint16_t RXCRCR; /*!< SPI RX CRC register (not used in I2S mode), Address offset: 0x14 */ + uint16_t RESERVED5; /*!< Reserved, 0x16 */ + __IO uint16_t TXCRCR; /*!< SPI TX CRC register (not used in I2S mode), Address offset: 0x18 */ + uint16_t RESERVED6; /*!< Reserved, 0x1A */ + __IO uint16_t I2SCFGR; /*!< SPI_I2S configuration register, Address offset: 0x1C */ + uint16_t RESERVED7; /*!< Reserved, 0x1E */ + __IO uint16_t I2SPR; /*!< SPI_I2S prescaler register, Address offset: 0x20 */ + uint16_t RESERVED8; /*!< Reserved, 0x22 */ +} SPI_TypeDef; + +/** + * @brief TIM + */ + +typedef struct +{ + __IO uint16_t CR1; /*!< TIM control register 1, Address offset: 0x00 */ + uint16_t RESERVED0; /*!< Reserved, 0x02 */ + __IO uint16_t CR2; /*!< TIM control register 2, Address offset: 0x04 */ + uint16_t RESERVED1; /*!< Reserved, 0x06 */ + __IO uint16_t SMCR; /*!< TIM slave mode control register, Address offset: 0x08 */ + uint16_t RESERVED2; /*!< Reserved, 0x0A */ + __IO uint16_t DIER; /*!< TIM DMA/interrupt enable register, Address offset: 0x0C */ + uint16_t RESERVED3; /*!< Reserved, 0x0E */ + __IO uint16_t SR; /*!< TIM status register, Address offset: 0x10 */ + uint16_t RESERVED4; /*!< Reserved, 0x12 */ + __IO uint16_t EGR; /*!< TIM event generation register, Address offset: 0x14 */ + uint16_t RESERVED5; /*!< Reserved, 0x16 */ + __IO uint16_t CCMR1; /*!< TIM capture/compare mode register 1, Address offset: 0x18 */ + uint16_t RESERVED6; /*!< Reserved, 0x1A */ + __IO uint16_t CCMR2; /*!< TIM capture/compare mode register 2, Address offset: 0x1C */ + uint16_t RESERVED7; /*!< Reserved, 0x1E */ + __IO uint16_t CCER; /*!< TIM capture/compare enable register, Address offset: 0x20 */ + uint16_t RESERVED8; /*!< Reserved, 0x22 */ + __IO uint32_t CNT; /*!< TIM counter register, Address offset: 0x24 */ + __IO uint16_t PSC; /*!< TIM prescaler, Address offset: 0x28 */ + uint16_t RESERVED9; /*!< Reserved, 0x2A */ + __IO uint32_t ARR; /*!< TIM auto-reload register, Address offset: 0x2C */ + __IO uint16_t RCR; /*!< TIM repetition counter register, Address offset: 0x30 */ + uint16_t RESERVED10; /*!< Reserved, 0x32 */ + __IO uint32_t CCR1; /*!< TIM capture/compare register 1, Address offset: 0x34 */ + __IO uint32_t CCR2; /*!< TIM capture/compare register 2, Address offset: 0x38 */ + __IO uint32_t CCR3; /*!< TIM capture/compare register 3, Address offset: 0x3C */ + __IO uint32_t CCR4; /*!< TIM capture/compare register 4, Address offset: 0x40 */ + __IO uint16_t BDTR; /*!< TIM break and dead-time register, Address offset: 0x44 */ + uint16_t RESERVED11; /*!< Reserved, 0x46 */ + __IO uint16_t DCR; /*!< TIM DMA control register, Address offset: 0x48 */ + uint16_t RESERVED12; /*!< Reserved, 0x4A */ + __IO uint16_t DMAR; /*!< TIM DMA address for full transfer, Address offset: 0x4C */ + uint16_t RESERVED13; /*!< Reserved, 0x4E */ + __IO uint16_t OR; /*!< TIM option register, Address offset: 0x50 */ + uint16_t RESERVED14; /*!< Reserved, 0x52 */ +} TIM_TypeDef; + +/** + * @brief Universal Synchronous Asynchronous Receiver Transmitter + */ + +typedef struct +{ + __IO uint16_t SR; /*!< USART Status register, Address offset: 0x00 */ + uint16_t RESERVED0; /*!< Reserved, 0x02 */ + __IO uint16_t DR; /*!< USART Data register, Address offset: 0x04 */ + uint16_t RESERVED1; /*!< Reserved, 0x06 */ + __IO uint16_t BRR; /*!< USART Baud rate register, Address offset: 0x08 */ + uint16_t RESERVED2; /*!< Reserved, 0x0A */ + __IO uint16_t CR1; /*!< USART Control register 1, Address offset: 0x0C */ + uint16_t RESERVED3; /*!< Reserved, 0x0E */ + __IO uint16_t CR2; /*!< USART Control register 2, Address offset: 0x10 */ + uint16_t RESERVED4; /*!< Reserved, 0x12 */ + __IO uint16_t CR3; /*!< USART Control register 3, Address offset: 0x14 */ + uint16_t RESERVED5; /*!< Reserved, 0x16 */ + __IO uint16_t GTPR; /*!< USART Guard time and prescaler register, Address offset: 0x18 */ + uint16_t RESERVED6; /*!< Reserved, 0x1A */ +} USART_TypeDef; + +/** + * @brief Window WATCHDOG + */ + +typedef struct +{ + __IO uint32_t CR; /*!< WWDG Control register, Address offset: 0x00 */ + __IO uint32_t CFR; /*!< WWDG Configuration register, Address offset: 0x04 */ + __IO uint32_t SR; /*!< WWDG Status register, Address offset: 0x08 */ +} WWDG_TypeDef; + +/** + * @brief Crypto Processor + */ + +typedef struct +{ + __IO uint32_t CR; /*!< CRYP control register, Address offset: 0x00 */ + __IO uint32_t SR; /*!< CRYP status register, Address offset: 0x04 */ + __IO uint32_t DR; /*!< CRYP data input register, Address offset: 0x08 */ + __IO uint32_t DOUT; /*!< CRYP data output register, Address offset: 0x0C */ + __IO uint32_t DMACR; /*!< CRYP DMA control register, Address offset: 0x10 */ + __IO uint32_t IMSCR; /*!< CRYP interrupt mask set/clear register, Address offset: 0x14 */ + __IO uint32_t RISR; /*!< CRYP raw interrupt status register, Address offset: 0x18 */ + __IO uint32_t MISR; /*!< CRYP masked interrupt status register, Address offset: 0x1C */ + __IO uint32_t K0LR; /*!< CRYP key left register 0, Address offset: 0x20 */ + __IO uint32_t K0RR; /*!< CRYP key right register 0, Address offset: 0x24 */ + __IO uint32_t K1LR; /*!< CRYP key left register 1, Address offset: 0x28 */ + __IO uint32_t K1RR; /*!< CRYP key right register 1, Address offset: 0x2C */ + __IO uint32_t K2LR; /*!< CRYP key left register 2, Address offset: 0x30 */ + __IO uint32_t K2RR; /*!< CRYP key right register 2, Address offset: 0x34 */ + __IO uint32_t K3LR; /*!< CRYP key left register 3, Address offset: 0x38 */ + __IO uint32_t K3RR; /*!< CRYP key right register 3, Address offset: 0x3C */ + __IO uint32_t IV0LR; /*!< CRYP initialization vector left-word register 0, Address offset: 0x40 */ + __IO uint32_t IV0RR; /*!< CRYP initialization vector right-word register 0, Address offset: 0x44 */ + __IO uint32_t IV1LR; /*!< CRYP initialization vector left-word register 1, Address offset: 0x48 */ + __IO uint32_t IV1RR; /*!< CRYP initialization vector right-word register 1, Address offset: 0x4C */ +} CRYP_TypeDef; + +/** + * @brief HASH + */ + +typedef struct +{ + __IO uint32_t CR; /*!< HASH control register, Address offset: 0x00 */ + __IO uint32_t DIN; /*!< HASH data input register, Address offset: 0x04 */ + __IO uint32_t STR; /*!< HASH start register, Address offset: 0x08 */ + __IO uint32_t HR[5]; /*!< HASH digest registers, Address offset: 0x0C-0x1C */ + __IO uint32_t IMR; /*!< HASH interrupt enable register, Address offset: 0x20 */ + __IO uint32_t SR; /*!< HASH status register, Address offset: 0x24 */ + uint32_t RESERVED[52]; /*!< Reserved, 0x28-0xF4 */ + __IO uint32_t CSR[51]; /*!< HASH context swap registers, Address offset: 0x0F8-0x1C0 */ +} HASH_TypeDef; + +/** + * @brief HASH + */ + +typedef struct +{ + __IO uint32_t CR; /*!< RNG control register, Address offset: 0x00 */ + __IO uint32_t SR; /*!< RNG status register, Address offset: 0x04 */ + __IO uint32_t DR; /*!< RNG data register, Address offset: 0x08 */ +} RNG_TypeDef; + +/** + * @} + */ + +/** @addtogroup Peripheral_memory_map + * @{ + */ + +#define FLASH_BASE ((uint32_t)0x08000000) /*!< FLASH base address in the alias region */ +#define SRAM_BASE ((uint32_t)0x20000000) /*!< SRAM base address in the alias region */ +#define PERIPH_BASE ((uint32_t)0x40000000) /*!< Peripheral base address in the alias region */ + +#define SRAM_BB_BASE ((uint32_t)0x22000000) /*!< SRAM base address in the bit-band region */ +#define PERIPH_BB_BASE ((uint32_t)0x42000000) /*!< Peripheral base address in the bit-band region */ + +#define FSMC_R_BASE ((uint32_t)0xA0000000) /*!< FSMC registers base address */ + +/*!< Peripheral memory map */ +#define APB1PERIPH_BASE PERIPH_BASE +#define APB2PERIPH_BASE (PERIPH_BASE + 0x00010000) +#define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000) +#define AHB2PERIPH_BASE (PERIPH_BASE + 0x10000000) + +/*!< APB1 peripherals */ +#define TIM2_BASE (APB1PERIPH_BASE + 0x0000) +#define TIM3_BASE (APB1PERIPH_BASE + 0x0400) +#define TIM4_BASE (APB1PERIPH_BASE + 0x0800) +#define TIM5_BASE (APB1PERIPH_BASE + 0x0C00) +#define TIM6_BASE (APB1PERIPH_BASE + 0x1000) +#define TIM7_BASE (APB1PERIPH_BASE + 0x1400) +#define TIM12_BASE (APB1PERIPH_BASE + 0x1800) +#define TIM13_BASE (APB1PERIPH_BASE + 0x1C00) +#define TIM14_BASE (APB1PERIPH_BASE + 0x2000) +#define RTC_BASE (APB1PERIPH_BASE + 0x2800) +#define WWDG_BASE (APB1PERIPH_BASE + 0x2C00) +#define IWDG_BASE (APB1PERIPH_BASE + 0x3000) +#define SPI2_BASE (APB1PERIPH_BASE + 0x3800) +#define SPI3_BASE (APB1PERIPH_BASE + 0x3C00) +#define USART2_BASE (APB1PERIPH_BASE + 0x4400) +#define USART3_BASE (APB1PERIPH_BASE + 0x4800) +#define UART4_BASE (APB1PERIPH_BASE + 0x4C00) +#define UART5_BASE (APB1PERIPH_BASE + 0x5000) +#define I2C1_BASE (APB1PERIPH_BASE + 0x5400) +#define I2C2_BASE (APB1PERIPH_BASE + 0x5800) +#define I2C3_BASE (APB1PERIPH_BASE + 0x5C00) +#define CAN1_BASE (APB1PERIPH_BASE + 0x6400) +#define CAN2_BASE (APB1PERIPH_BASE + 0x6800) +#define PWR_BASE (APB1PERIPH_BASE + 0x7000) +#define DAC_BASE (APB1PERIPH_BASE + 0x7400) + +/*!< APB2 peripherals */ +#define TIM1_BASE (APB2PERIPH_BASE + 0x0000) +#define TIM8_BASE (APB2PERIPH_BASE + 0x0400) +#define USART1_BASE (APB2PERIPH_BASE + 0x1000) +#define USART6_BASE (APB2PERIPH_BASE + 0x1400) +#define ADC1_BASE (APB2PERIPH_BASE + 0x2000) +#define ADC2_BASE (APB2PERIPH_BASE + 0x2100) +#define ADC3_BASE (APB2PERIPH_BASE + 0x2200) +#define ADC_BASE (APB2PERIPH_BASE + 0x2300) +#define SDIO_BASE (APB2PERIPH_BASE + 0x2C00) +#define SPI1_BASE (APB2PERIPH_BASE + 0x3000) +#define SYSCFG_BASE (APB2PERIPH_BASE + 0x3800) +#define EXTI_BASE (APB2PERIPH_BASE + 0x3C00) +#define TIM9_BASE (APB2PERIPH_BASE + 0x4000) +#define TIM10_BASE (APB2PERIPH_BASE + 0x4400) +#define TIM11_BASE (APB2PERIPH_BASE + 0x4800) + +/*!< AHB1 peripherals */ +#define GPIOA_BASE (AHB1PERIPH_BASE + 0x0000) +#define GPIOB_BASE (AHB1PERIPH_BASE + 0x0400) +#define GPIOC_BASE (AHB1PERIPH_BASE + 0x0800) +#define GPIOD_BASE (AHB1PERIPH_BASE + 0x0C00) +#define GPIOE_BASE (AHB1PERIPH_BASE + 0x1000) +#define GPIOF_BASE (AHB1PERIPH_BASE + 0x1400) +#define GPIOG_BASE (AHB1PERIPH_BASE + 0x1800) +#define GPIOH_BASE (AHB1PERIPH_BASE + 0x1C00) +#define GPIOI_BASE (AHB1PERIPH_BASE + 0x2000) +#define CRC_BASE (AHB1PERIPH_BASE + 0x3000) +#define RCC_BASE (AHB1PERIPH_BASE + 0x3800) +#define FLASH_R_BASE (AHB1PERIPH_BASE + 0x3C00) +#define BKPSRAM_BASE (AHB1PERIPH_BASE + 0x4000) +#define DMA1_BASE (AHB1PERIPH_BASE + 0x6000) +#define DMA1_Stream0_BASE (DMA1_BASE + 0x010) +#define DMA1_Stream1_BASE (DMA1_BASE + 0x028) +#define DMA1_Stream2_BASE (DMA1_BASE + 0x040) +#define DMA1_Stream3_BASE (DMA1_BASE + 0x058) +#define DMA1_Stream4_BASE (DMA1_BASE + 0x070) +#define DMA1_Stream5_BASE (DMA1_BASE + 0x088) +#define DMA1_Stream6_BASE (DMA1_BASE + 0x0A0) +#define DMA1_Stream7_BASE (DMA1_BASE + 0x0B8) +#define DMA2_BASE (AHB1PERIPH_BASE + 0x6400) +#define DMA2_Stream0_BASE (DMA2_BASE + 0x010) +#define DMA2_Stream1_BASE (DMA2_BASE + 0x028) +#define DMA2_Stream2_BASE (DMA2_BASE + 0x040) +#define DMA2_Stream3_BASE (DMA2_BASE + 0x058) +#define DMA2_Stream4_BASE (DMA2_BASE + 0x070) +#define DMA2_Stream5_BASE (DMA2_BASE + 0x088) +#define DMA2_Stream6_BASE (DMA2_BASE + 0x0A0) +#define DMA2_Stream7_BASE (DMA2_BASE + 0x0B8) +#define ETH_BASE (AHB1PERIPH_BASE + 0x8000) +#define ETH_MAC_BASE (ETH_BASE) +#define ETH_MMC_BASE (ETH_BASE + 0x0100) +#define ETH_PTP_BASE (ETH_BASE + 0x0700) +#define ETH_DMA_BASE (ETH_BASE + 0x1000) + +/*!< AHB2 peripherals */ +#define DCMI_BASE (AHB2PERIPH_BASE + 0x50000) +#define CRYP_BASE (AHB2PERIPH_BASE + 0x60000) +#define HASH_BASE (AHB2PERIPH_BASE + 0x60400) +#define RNG_BASE (AHB2PERIPH_BASE + 0x60800) + +/*!< FSMC Bankx registers base address */ +#define FSMC_Bank1_R_BASE (FSMC_R_BASE + 0x0000) +#define FSMC_Bank1E_R_BASE (FSMC_R_BASE + 0x0104) +#define FSMC_Bank2_R_BASE (FSMC_R_BASE + 0x0060) +#define FSMC_Bank3_R_BASE (FSMC_R_BASE + 0x0080) +#define FSMC_Bank4_R_BASE (FSMC_R_BASE + 0x00A0) + +/* Debug MCU registers base address */ +#define DBGMCU_BASE ((uint32_t )0xE0042000) + +/** + * @} + */ + +/** @addtogroup Peripheral_declaration + * @{ + */ +#define TIM2 ((TIM_TypeDef *) TIM2_BASE) +#define TIM3 ((TIM_TypeDef *) TIM3_BASE) +#define TIM4 ((TIM_TypeDef *) TIM4_BASE) +#define TIM5 ((TIM_TypeDef *) TIM5_BASE) +#define TIM6 ((TIM_TypeDef *) TIM6_BASE) +#define TIM7 ((TIM_TypeDef *) TIM7_BASE) +#define TIM12 ((TIM_TypeDef *) TIM12_BASE) +#define TIM13 ((TIM_TypeDef *) TIM13_BASE) +#define TIM14 ((TIM_TypeDef *) TIM14_BASE) +#define RTC ((RTC_TypeDef *) RTC_BASE) +#define WWDG ((WWDG_TypeDef *) WWDG_BASE) +#define IWDG ((IWDG_TypeDef *) IWDG_BASE) +#define SPI2 ((SPI_TypeDef *) SPI2_BASE) +#define SPI3 ((SPI_TypeDef *) SPI3_BASE) +#define USART2 ((USART_TypeDef *) USART2_BASE) +#define USART3 ((USART_TypeDef *) USART3_BASE) +#define UART4 ((USART_TypeDef *) UART4_BASE) +#define UART5 ((USART_TypeDef *) UART5_BASE) +#define I2C1 ((I2C_TypeDef *) I2C1_BASE) +#define I2C2 ((I2C_TypeDef *) I2C2_BASE) +#define I2C3 ((I2C_TypeDef *) I2C3_BASE) +#define CAN1 ((CAN_TypeDef *) CAN1_BASE) +#define CAN2 ((CAN_TypeDef *) CAN2_BASE) +#define PWR ((PWR_TypeDef *) PWR_BASE) +#define DAC ((DAC_TypeDef *) DAC_BASE) +#define TIM1 ((TIM_TypeDef *) TIM1_BASE) +#define TIM8 ((TIM_TypeDef *) TIM8_BASE) +#define USART1 ((USART_TypeDef *) USART1_BASE) +#define USART6 ((USART_TypeDef *) USART6_BASE) +#define ADC ((ADC_Common_TypeDef *) ADC_BASE) +#define ADC1 ((ADC_TypeDef *) ADC1_BASE) +#define ADC2 ((ADC_TypeDef *) ADC2_BASE) +#define ADC3 ((ADC_TypeDef *) ADC3_BASE) +#define SDIO ((SDIO_TypeDef *) SDIO_BASE) +#define SPI1 ((SPI_TypeDef *) SPI1_BASE) +#define SYSCFG ((SYSCFG_TypeDef *) SYSCFG_BASE) +#define EXTI ((EXTI_TypeDef *) EXTI_BASE) +#define TIM9 ((TIM_TypeDef *) TIM9_BASE) +#define TIM10 ((TIM_TypeDef *) TIM10_BASE) +#define TIM11 ((TIM_TypeDef *) TIM11_BASE) +#define GPIOA ((GPIO_TypeDef *) GPIOA_BASE) +#define GPIOB ((GPIO_TypeDef *) GPIOB_BASE) +#define GPIOC ((GPIO_TypeDef *) GPIOC_BASE) +#define GPIOD ((GPIO_TypeDef *) GPIOD_BASE) +#define GPIOE ((GPIO_TypeDef *) GPIOE_BASE) +#define GPIOF ((GPIO_TypeDef *) GPIOF_BASE) +#define GPIOG ((GPIO_TypeDef *) GPIOG_BASE) +#define GPIOH ((GPIO_TypeDef *) GPIOH_BASE) +#define GPIOI ((GPIO_TypeDef *) GPIOI_BASE) +#define CRC ((CRC_TypeDef *) CRC_BASE) +#define RCC ((RCC_TypeDef *) RCC_BASE) +#define FLASH ((FLASH_TypeDef *) FLASH_R_BASE) +#define DMA1 ((DMA_TypeDef *) DMA1_BASE) +#define DMA1_Stream0 ((DMA_Stream_TypeDef *) DMA1_Stream0_BASE) +#define DMA1_Stream1 ((DMA_Stream_TypeDef *) DMA1_Stream1_BASE) +#define DMA1_Stream2 ((DMA_Stream_TypeDef *) DMA1_Stream2_BASE) +#define DMA1_Stream3 ((DMA_Stream_TypeDef *) DMA1_Stream3_BASE) +#define DMA1_Stream4 ((DMA_Stream_TypeDef *) DMA1_Stream4_BASE) +#define DMA1_Stream5 ((DMA_Stream_TypeDef *) DMA1_Stream5_BASE) +#define DMA1_Stream6 ((DMA_Stream_TypeDef *) DMA1_Stream6_BASE) +#define DMA1_Stream7 ((DMA_Stream_TypeDef *) DMA1_Stream7_BASE) +#define DMA2 ((DMA_TypeDef *) DMA2_BASE) +#define DMA2_Stream0 ((DMA_Stream_TypeDef *) DMA2_Stream0_BASE) +#define DMA2_Stream1 ((DMA_Stream_TypeDef *) DMA2_Stream1_BASE) +#define DMA2_Stream2 ((DMA_Stream_TypeDef *) DMA2_Stream2_BASE) +#define DMA2_Stream3 ((DMA_Stream_TypeDef *) DMA2_Stream3_BASE) +#define DMA2_Stream4 ((DMA_Stream_TypeDef *) DMA2_Stream4_BASE) +#define DMA2_Stream5 ((DMA_Stream_TypeDef *) DMA2_Stream5_BASE) +#define DMA2_Stream6 ((DMA_Stream_TypeDef *) DMA2_Stream6_BASE) +#define DMA2_Stream7 ((DMA_Stream_TypeDef *) DMA2_Stream7_BASE) +#define ETH ((ETH_TypeDef *) ETH_BASE) +#define DCMI ((DCMI_TypeDef *) DCMI_BASE) +#define CRYP ((CRYP_TypeDef *) CRYP_BASE) +#define HASH ((HASH_TypeDef *) HASH_BASE) +#define RNG ((RNG_TypeDef *) RNG_BASE) +#define FSMC_Bank1 ((FSMC_Bank1_TypeDef *) FSMC_Bank1_R_BASE) +#define FSMC_Bank1E ((FSMC_Bank1E_TypeDef *) FSMC_Bank1E_R_BASE) +#define FSMC_Bank2 ((FSMC_Bank2_TypeDef *) FSMC_Bank2_R_BASE) +#define FSMC_Bank3 ((FSMC_Bank3_TypeDef *) FSMC_Bank3_R_BASE) +#define FSMC_Bank4 ((FSMC_Bank4_TypeDef *) FSMC_Bank4_R_BASE) +#define DBGMCU ((DBGMCU_TypeDef *) DBGMCU_BASE) + +/** + * @} + */ + +/** @addtogroup Exported_constants + * @{ + */ + + /** @addtogroup Peripheral_Registers_Bits_Definition + * @{ + */ + +/******************************************************************************/ +/* Peripheral Registers_Bits_Definition */ +/******************************************************************************/ + +/******************************************************************************/ +/* */ +/* Analog to Digital Converter */ +/* */ +/******************************************************************************/ +/******************** Bit definition for ADC_SR register ********************/ +#define ADC_SR_AWD ((uint8_t)0x01) /*!*/ + +#define DAC_CR_EN2 ((uint32_t)0x00010000) /*!*/ + +/***************** Bit definition for DAC_SWTRIGR register ******************/ +#define DAC_SWTRIGR_SWTRIG1 ((uint8_t)0x01) /*!
© COPYRIGHT 2012 STMicroelectronics
+ * + * Licensed under MCD-ST Liberty SW License Agreement V2, (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.st.com/software_license_agreement_liberty_v2 + * + * 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. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F2xx_CONF_H +#define __STM32F2xx_CONF_H + +/* Includes ------------------------------------------------------------------*/ +/* Uncomment the line below to enable peripheral header file inclusion */ +#include "stm32f2xx_adc.h" +#include "stm32f2xx_can.h" +#include "stm32f2xx_crc.h" +#include "stm32f2xx_cryp.h" +#include "stm32f2xx_dac.h" +#include "stm32f2xx_dbgmcu.h" +#include "stm32f2xx_dcmi.h" +#include "stm32f2xx_dma.h" +#include "stm32f2xx_exti.h" +#include "stm32f2xx_flash.h" +#include "stm32f2xx_fsmc.h" +#include "stm32f2xx_hash.h" +#include "stm32f2xx_gpio.h" +#include "stm32f2xx_i2c.h" +#include "stm32f2xx_iwdg.h" +#include "stm32f2xx_pwr.h" +#include "stm32f2xx_rcc.h" +#include "stm32f2xx_rng.h" +#include "stm32f2xx_rtc.h" +#include "stm32f2xx_sdio.h" +#include "stm32f2xx_spi.h" +#include "stm32f2xx_syscfg.h" +#include "stm32f2xx_tim.h" +#include "stm32f2xx_usart.h" +#include "stm32f2xx_wwdg.h" +#include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */ + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +/* If an external clock source is used, then the value of the following define + should be set to the value of the external clock source, else, if no external + clock is used, keep this define commented */ +/*#define I2S_EXTERNAL_CLOCK_VAL 12288000 */ /* Value of the external clock in Hz */ + + +/* Uncomment the line below to expanse the "assert_param" macro in the + Standard Peripheral Library drivers code */ +/* #define USE_FULL_ASSERT 1 */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT + +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ + #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ + void assert_failed(uint8_t* file, uint32_t line); +#else + #define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + +#endif /* __STM32F2xx_CONF_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/mkrtos_user/user/drv/STM32F205/block/system_stm32f2xx.h b/mkrtos_user/user/drv/STM32F205/block/system_stm32f2xx.h new file mode 100644 index 000000000..427b22255 --- /dev/null +++ b/mkrtos_user/user/drv/STM32F205/block/system_stm32f2xx.h @@ -0,0 +1,105 @@ +/** + ****************************************************************************** + * @file system_stm32f2xx.h + * @author MCD Application Team + * @version V1.1.3 + * @date 05-March-2012 + * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Header File. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT 2012 STMicroelectronics

+ * + * Licensed under MCD-ST Liberty SW License Agreement V2, (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.st.com/software_license_agreement_liberty_v2 + * + * 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. + * + ****************************************************************************** + */ + +/** @addtogroup CMSIS + * @{ + */ + +/** @addtogroup stm32f2xx_system + * @{ + */ + +/** + * @brief Define to prevent recursive inclusion + */ +#ifndef __SYSTEM_STM32F2XX_H +#define __SYSTEM_STM32F2XX_H + +#ifdef __cplusplus + extern "C" { +#endif + +/** @addtogroup STM32F2xx_System_Includes + * @{ + */ + +/** + * @} + */ + + +/** @addtogroup STM32F2xx_System_Exported_types + * @{ + */ + +extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ + + +/** + * @} + */ + +/** @addtogroup STM32F2xx_System_Exported_Constants + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32F2xx_System_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32F2xx_System_Exported_Functions + * @{ + */ + +extern void SystemInit(void); +extern void SystemCoreClockUpdate(void); +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /*__SYSTEM_STM32F2XX_H */ + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/