// // Created by cfif on 29.05.2024. // #include "LittleFsInterface.h" #include "at32f435_437_flash.h" #include "ext_telematica.h" #include "ld_adr.h" #define FLASH_START_SECTOR ((BOOT_AREA_LENGTH + FIRMWARE_MAIN_AREA_LENGTH + FIRMWARE_MAIN_UPDATE_AREA_LENGTH + FIRMWARE_TELE_AREA_LENGTH + FIRMWARE_SETTINGS_AREA_LENGTH + FIRMWARE_SETTINGS_RECOVERY_AREA_LENGTH) / FLASH_PAGE_SIZE) // 496 #define FLASH_STOP_SECTOR ((BOOT_AREA_LENGTH + FIRMWARE_MAIN_AREA_LENGTH + FIRMWARE_MAIN_UPDATE_AREA_LENGTH + FIRMWARE_TELE_AREA_LENGTH + FIRMWARE_SETTINGS_AREA_LENGTH + FIRMWARE_SETTINGS_RECOVERY_AREA_LENGTH + FIRMWARE_FILESYSTEM_LENGTH) / FLASH_PAGE_SIZE) // 511 // Read a region in a block. Negative error codes are propagated // to the user. int readFlashArtery(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) { // uint32_t sectorAdr = (FLASH_START_BLOCK + block) * 0x10000 + off + FLASH_BASE; uint32_t sectorAdr = (FLASH_START_SECTOR + block) * FLASH_PAGE_SIZE + off + FLASH_BASE; for (uint32_t i = 0; i < size; ++i) { ((uint8_t *) buffer)[i] = *(uint8_t *) sectorAdr; ++sectorAdr; } return LFS_ERR_OK; } // Program a region in a block. The block must have previously // been erased. Negative error codes are propagated to the user. // May return LFS_ERR_CORRUPT if the block should be considered bad. int progFlashArtery(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) { // uint32_t sectorAdr = (FLASH_START_BLOCK + block) * 0x10000 + off + FLASH_BASE; uint32_t sectorAdr = (FLASH_START_SECTOR + block) * FLASH_PAGE_SIZE + off + FLASH_BASE; flash_unlock(); for (uint32_t i = 0; i < size; ++i) { if (FLASH_OPERATE_DONE != flash_byte_program(sectorAdr + i, ((uint8_t *) buffer)[i])) { flash_lock(); return LFS_ERR_IO; } } flash_lock(); return LFS_ERR_OK; } // Erase a block. A block must be erased before being programmed. // The state of an erased block is undefined. Negative error codes // are propagated to the user. // May return LFS_ERR_CORRUPT if the block should be considered bad. int eraseFlashArtery(const struct lfs_config *c, lfs_block_t block) { // uint32_t sectorAdr = (FLASH_START_BLOCK + block) * 0x10000 + FLASH_BASE; uint32_t sectorAdr = (FLASH_START_SECTOR + block) * FLASH_PAGE_SIZE + FLASH_BASE; flash_unlock(); if (FLASH_OPERATE_DONE != flash_sector_erase(sectorAdr)) { flash_lock(); return LFS_ERR_IO; } flash_lock(); // W25qxx_EraseSector(block * c->block_size); return LFS_ERR_OK; } // Sync the state of the underlying block device. Negative error codes // are propagated to the user. int syncFlashArtery(const struct lfs_config *c) { return LFS_ERR_OK; } // Lock the underlying block device. Negative error codes // are propagated to the user. int lockFlashArtery(const struct lfs_config *c) { if (osMutexAcquire(EXT_ENV_TELE.store.accessDumper, 5000) == osOK) return LFS_ERR_OK; return LFS_ERR_IO; } // Unlock the underlying block device. Negative error codes // are propagated to the user. int unlockFlashArtery(const struct lfs_config *c) { if (osMutexRelease(EXT_ENV_TELE.store.accessDumper) == osOK) return LFS_ERR_OK; return LFS_ERR_IO; } #define LFS_CACHE_SIZE (2048) #define LFS_LOOK_HEAD_SIZE (256) #ifdef LFS_NO_MALLOC uint8_t readBuffer[LFS_CACHE_SIZE]; uint8_t writeBuffer[LFS_CACHE_SIZE]; uint8_t lookBuffer[LFS_LOOK_HEAD_SIZE]; uint8_t g_file_buffer[LFS_CACHE_SIZE]; #endif int lfs_init_Over(lfs_t *lfs, const struct lfs_config *cfg); int LittleFileFsInit(tLittleFileFs *env) { env->cfg.read = readFlashArtery; env->cfg.prog = progFlashArtery; env->cfg.erase = eraseFlashArtery; env->cfg.sync = syncFlashArtery; env->cfg.lock = lockFlashArtery; env->cfg.unlock = unlockFlashArtery; // block device configuration env->cfg.read_size = LFS_CACHE_SIZE; env->cfg.prog_size = LFS_CACHE_SIZE; env->cfg.block_size = FLASH_PAGE_SIZE; env->cfg.block_count = FLASH_STOP_SECTOR - FLASH_START_SECTOR; env->cfg.cache_size = LFS_CACHE_SIZE; env->cfg.lookahead_size = LFS_LOOK_HEAD_SIZE; env->cfg.block_cycles = 100; #ifdef LFS_NO_MALLOC env->cfg.read_buffer = readBuffer; env->cfg.prog_buffer = writeBuffer; env->cfg.lookahead_buffer = lookBuffer; env->g_file_cfg.buffer = g_file_buffer; #endif int err; err = lfs_init_Over(&env->lfs, &env->cfg); // err = lfs_format(&env->lfs, &env->cfg); // mount the filesystem err = lfs_mount(&env->lfs, &env->cfg); // reformat if we can't mount the filesystem // this should only happen on the first boot if (err) { err = lfs_format(&env->lfs, &env->cfg); err = lfs_mount(&env->lfs, &env->cfg); } return err; }