eCos flash模拟EEPROM实现NV系统

2024-02-03 19:20

本文主要是介绍eCos flash模拟EEPROM实现NV系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Flash需要擦除的原因:先擦除后写入的原因是为了工业上制作方便,即物理实现方便。

#include <cyg/infra/diag.h>

#include <cyg/io/flash.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

// SPI flash size = 4 MB
static bool init = false;
static cyg_mutex_t nv_mutex;
static unsigned char *e2prom_buf = NULL;
static unsigned long e2prom_sz = SZ_2K;
static unsigned long logical_e2prom_cur_idx = 0;
static unsigned long nr_logical_e2prom = 1;
static unsigned long blk_sz = SZ_64K;

#include "oem-nv-lib.c"

static int program_data(void)
{
    cyg_flashaddr_t err_addr;
    cyg_flashaddr_t flash_base = NV_FLASH_BYTES_ADDR;
    int status;
    unsigned long flash_offset;

    flash_offset = logical_e2prom_cur_idx * e2prom_sz;
    oem_printf("[OEM][%s] logical_e2prom_cur_idx: %d, flash_offset: 0x%x(%dK)\n",
            __func__, logical_e2prom_cur_idx, flash_offset, (flash_offset/SZ_1K));

    // 1) Mark we will program data
    status = cyg_flash_program(flash_base + flash_offset,
            e2prom_buf, 2, &err_addr);
    if (status != CYG_FLASH_ERR_OK) {
        oem_printf("[OEM][%s] 1) flash program err!!\n", __func__);
        goto err;
    }
    // 2) Programming data
    status = cyg_flash_program(flash_base + flash_offset + SZ_E2PROM_HDR,
            e2prom_buf + SZ_E2PROM_HDR, e2prom_sz - SZ_E2PROM_HDR, &err_addr);
    if (status != CYG_FLASH_ERR_OK) {
        oem_printf("[OEM][%s] 2) flash program err!!\n", __func__);
        goto err;
    }
    // 3) Mark we have completed programming data
    status = cyg_flash_program(flash_base + flash_offset + 2,
            e2prom_buf + 2, 2, &err_addr);
    if (status != CYG_FLASH_ERR_OK) {
        oem_printf("[OEM][%s] 3) flash program err!!\n", __func__);
        goto err;
    }
    return 0;
err:
    // TODO:
    return -1;
}

static int recovery_of_sudden_power_cut(void)
{
    cyg_flashaddr_t err_addr;
    cyg_flashaddr_t flash_base = NV_FLASH_BYTES_ADDR;
    int i;
    int status;
    unsigned long flash_offset;

    for (i = logical_e2prom_cur_idx; i > 0; i--) {
        flash_offset = i * e2prom_sz;
        status = cyg_flash_read(flash_base + flash_offset, e2prom_buf, e2prom_sz, &err_addr);
        if (status != CYG_FLASH_ERR_OK) {
            oem_printf("[OEM][%s] flash read err!!\n", __func__);
            goto err;
        }

        // little endian
        //oem_printf("magic: 0x%x\n", ((unsigned int*)e2prom_buf)[0]);
        if (((unsigned int *)e2prom_buf)[0] == 0xaaaa5555) {
            oem_printf("[OEM] i: %d, logical_e2prom_cur_idx: %d\n", i, logical_e2prom_cur_idx);
            break;
        }
    }
    if (i != logical_e2prom_cur_idx) {
        oem_printf("[OEM][%s] call cyg_flash_erase()\n", __func__);
        cyg_flash_erase(flash_base, blk_sz, &err_addr);
        logical_e2prom_cur_idx = 0;
        if (program_data() < 0) {
            goto err;
        }
    }

    return 0;
err:
    return -1;
}

static void show_flash_ptn(void)
{
    // uboot
    // offset: 0, size: 192K

    // for CFG_set & CFG_get(User config, Switch parameter)
    // Bottom-Boot flsh_cfg_off: 16K, flsh_cfg_sz: 20K
    // Top-Boot flsh_cfg_off: 4M - 20K, flsh_cfg_sz: 20K
    // !!No-Boot flsh_cfg_off: 196K(0x31000), flsh_cfg_sz: 20K
    //oem_printf("[OEM] flsh_cfg_off: 0x%x, flsh_cfg_sz: 0x%x\n", flsh_cfg_off, flsh_cfg_sz);

    // for emulating eeprom to save MAC ADDR(RF parameter)
    // Bottom-Boot flsh_cfg_boot_off: 12K
    // Top-Boot flsh_cfg_boot_off: 60K
    // !!No-Boot flsh_cfg_boot_off: 256K(0x40000), size: 512B
    oem_printf("[OEM] flsh_cfg_boot_off: 0x%x(%dK)\n", flsh_cfg_boot_off,
        (flsh_cfg_boot_off/SZ_1K));

    // for eCos firmware and size
    // Bottom-Boot flsh_cfg_fwm_off: 64K, flsh_cfg_fwm_sz: 4M - 64K
    // Top-Boot flsh_cfg_fwm_off: 64K, flsh_cfg_sz: 4M - 64K - 20K
    // !!No-Boot flsh_cfg_fwm_off: 320K(0x50000), flsh_cfg_sz: 4M - 320K
    oem_printf("[OEM] flsh_cfg_fwm_off: 0x%x(%dK), flsh_cfg_fwm_sz: 0x%x(%dK)\n",
            flsh_cfg_fwm_off, (flsh_cfg_fwm_off/SZ_1K),
            flsh_cfg_fwm_sz, (flsh_cfg_fwm_sz/SZ_1K));

    // for OEM NV read & write
    oem_printf("[OEM] flsh_nv_off: 0x%x(%dK)\n", NV_FLASH_BYTES_ADDR,
            NV_FLASH_BYTES_ADDR/SZ_1K);
}

API int nv_init(void)
{
    cyg_flash_info_t cfi;
    cyg_flashaddr_t err_addr;
    cyg_flashaddr_t flash_base = NV_FLASH_BYTES_ADDR;
    int status;
    unsigned long flash_offset;

    ///
    show_flash_ptn();
    oem_printf("[OEM] nv memory used size: %d Bytes\n", get_nvm_size());
    ///

    // Initializing the FLASH library
    cyg_flash_set_global_printf((cyg_flash_printf *)&diag_printf);
    cyg_flash_init(NULL);
    if (cyg_flash_get_info(0, &cfi) == CYG_FLASH_ERR_OK) {
        if (cfi.block_info) {
            blk_sz = cfi.block_info->block_size;
            // nr_logical_e2prom should be >= 1
            nr_logical_e2prom = blk_sz / e2prom_sz;
            oem_printf("[OEM] nr_logical_e2prom: %d\n", nr_logical_e2prom);

            oem_printf("[OEM] start_addr: 0x%x, end_addr: 0x%x, num_block_infos: %d, "
                    "block_size: %d, blocks: %d\n",
                    cfi.start, cfi.end, cfi.num_block_infos,
                    cfi.block_info->block_size, cfi.block_info->blocks);

            if (!e2prom_buf) {
                e2prom_buf = (unsigned char *)malloc(e2prom_sz);
                if (!e2prom_buf) {
                    oem_printf("[OEM][%s] Can not allocate memory for e2prom_buf!!\n", __func__);
                    goto err;
                }
            }

            for (logical_e2prom_cur_idx = 0; logical_e2prom_cur_idx < nr_logical_e2prom;
                    logical_e2prom_cur_idx++) {
                flash_offset = logical_e2prom_cur_idx * e2prom_sz;
                status = cyg_flash_read(flash_base + flash_offset, e2prom_buf, e2prom_sz, &err_addr);
                if (status != CYG_FLASH_ERR_OK) {
                    logical_e2prom_cur_idx = 0;
                    oem_printf("[OEM][%s] flash read err!!\n", __func__);
                    goto err;
                }
                if (e2prom_buf[0] == 0xff &&
                    e2prom_buf[1] == 0xff &&
                    e2prom_buf[2] == 0xff &&
                    e2prom_buf[3] == 0xff) {
                    oem_printf("[OEM][%s] Got a free logical e2prom idx: %d\n",
                        __func__, logical_e2prom_cur_idx);
                    break;
                }
            }

            oem_printf("[OEM][%s] before chng, logical e2prom idx: %d\n",
                    __func__, logical_e2prom_cur_idx);
            if (logical_e2prom_cur_idx == nr_logical_e2prom) {
                cyg_flash_erase(flash_base, blk_sz, &err_addr);
                logical_e2prom_cur_idx = 0;
                if (program_data() < 0) {
                    goto err;
                }
            } else if (logical_e2prom_cur_idx > 0 && logical_e2prom_cur_idx < nr_logical_e2prom) {
                logical_e2prom_cur_idx--;
            }

            if (recovery_of_sudden_power_cut() < 0) {
                goto err;
            }

            init = true;
        }
    }

    cyg_mutex_init(&nv_mutex);
    return 0;
err:
    return -1;
}

API int nv_read(nv_items_enum_t id, u8 *buf, int len)
{
    cyg_flashaddr_t err_addr;
    // flash_base is where in the flash to read from, it is a byte address,
    // not sector address.
    cyg_flashaddr_t flash_base = NV_FLASH_BYTES_ADDR;
    int status;
    unsigned long flash_offset = 0;
    long nv_offset = NV_OFFSET(id);
    unsigned long nv_sz = NV_SZ(id);

    cyg_mutex_lock(&nv_mutex);
    if (!init) {
        if (false == nv_init()) {
            goto err;
        }
    }

    if (nv_offset < 0) {
        goto err;
    }
    if (nv_sz > len) {
        nv_sz = len;
    }

    flash_offset = logical_e2prom_cur_idx * e2prom_sz;
    status = cyg_flash_read(flash_base + flash_offset + nv_offset, (void *)buf, nv_sz, &err_addr);
    if (status != CYG_FLASH_ERR_OK) {
        oem_printf("[OEM][%s] flash read err!!\n", __func__);
        goto err;
    }
    cyg_mutex_unlock(&nv_mutex);
    oem_printf("[OEM][%s] succeeded in reading nv_%d, nv_sz: %d Bytes "
            "@logical_e2prom_cur_idx: %d\n", __func__, id, nv_sz,
            logical_e2prom_cur_idx);
    return nv_sz;
err:
    cyg_mutex_unlock(&nv_mutex);
    return -1;
}

API int nv_write(nv_items_enum_t id, u8 *buf, int len)
{
    unsigned char magic[] = {0x55, 0x55, 0xaa, 0xaa};
    cyg_flashaddr_t err_addr;
    // flash_base is where in the flash to write from, it is a byte address,
    // not sector address.
    cyg_flashaddr_t flash_base = NV_FLASH_BYTES_ADDR;
    int status;
    unsigned long flash_offset = 0;
    long nv_offset = NV_OFFSET(id);
    unsigned long nv_sz = NV_SZ(id);

    cyg_mutex_lock(&nv_mutex);
    if (!init) {
        if (false == nv_init()) {
            goto err;
        }
    }

    if (nv_offset < 0) {
        goto err;
    }
    if (nv_sz > len) {
        nv_sz = len;
    }

    flash_offset = logical_e2prom_cur_idx * e2prom_sz;
    status = cyg_flash_read(flash_base + flash_offset, e2prom_buf, e2prom_sz, &err_addr);
    if (status != CYG_FLASH_ERR_OK) {
        oem_printf("[OEM][%s] flash read err!!\n", __func__);
        goto err;
    }
    memcpy(e2prom_buf, magic, sizeof(magic));
    memcpy(e2prom_buf + nv_offset, buf, nv_sz);
#if defined(BATCH_COMMIT)
    cyg_mutex_unlock(&nv_mutex);
    return nv_sz;
#else
    // No any data in e2prom, so check here
    if (0 == logical_e2prom_cur_idx) {
        status = cyg_flash_read(flash_base, buf, 4, &err_addr);
        if (status != CYG_FLASH_ERR_OK) {
            oem_printf("[OEM][%s] Oops here, check it manually\n", __func__);
        } else if (CYG_FLASH_ERR_OK == status && buf[0] == 0xff &&
                buf[1] == 0xff &&
                buf[2] == 0xff &&
                buf[3] == 0xff) {
            oem_printf("[OEM][%s] do not add e2prom cur index\n", __func__);
        } else {
            logical_e2prom_cur_idx++;
        }
    } else {
        logical_e2prom_cur_idx++;
    }

    if (logical_e2prom_cur_idx >= nr_logical_e2prom) {
        logical_e2prom_cur_idx = 0;
        cyg_flash_erase(flash_base, blk_sz, &err_addr);
    }

    if (program_data() < 0) {
        goto err;
    }
    cyg_mutex_unlock(&nv_mutex);
    return nv_sz;
#endif
err:
    cyg_mutex_unlock(&nv_mutex);
    return -1;
}

#if defined(BATCH_COMMIT)
API int nv_commit(void)
{
    cyg_flashaddr_t err_addr;
    cyg_flashaddr_t flash_base = NV_FLASH_BYTES_ADDR;
    int status;
    u8 buf[4];

    cyg_mutex_lock(&nv_mutex);

    // No any data in e2prom, so check here
    if (0 == logical_e2prom_cur_idx) {
        status = cyg_flash_read(flash_base, buf, 4, &err_addr);
        if (status != CYG_FLASH_ERR_OK) {
            oem_printf("[OEM][%s] Oops here, check it manually\n", __func__);
        } else if (CYG_FLASH_ERR_OK == status && buf[0] == 0xff &&
                buf[1] == 0xff &&
                buf[2] == 0xff &&
                buf[3] == 0xff) {
            oem_printf("[OEM][%s] do not add e2prom cur index\n", __func__);
        } else {
            logical_e2prom_cur_idx++;
        }
    } else {
        logical_e2prom_cur_idx++;
    }

    if (logical_e2prom_cur_idx >= nr_logical_e2prom) {
        oem_printf("[OEM][%s] need erase block, logical_e2prom_cur_idx: %d\n",
                __func__, logical_e2prom_cur_idx);
        logical_e2prom_cur_idx = 0;
        cyg_flash_erase(flash_base, blk_sz, &err_addr);
    }

    // 3M = 0x300000
    // spi rd 300000 64
    // spi wr 300000 55 55 aa aa
    // spi er 300000 65536
    if (program_data() < 0) {
        goto err;
    }

    cyg_mutex_unlock(&nv_mutex);
    oem_printf("[OEM][%s] succeeded in updating logical_e2prom_cur_idx: %d\n",
            __func__, logical_e2prom_cur_idx);
    return 0;
err:
    cyg_mutex_unlock(&nv_mutex);
    return -1;
}
#else
API int nv_commit(void)
{
    return 0;
}
#endif

这篇关于eCos flash模拟EEPROM实现NV系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/675135

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Linux系统中卸载与安装JDK的详细教程

《Linux系统中卸载与安装JDK的详细教程》本文详细介绍了如何在Linux系统中通过Xshell和Xftp工具连接与传输文件,然后进行JDK的安装与卸载,安装步骤包括连接Linux、传输JDK安装包... 目录1、卸载1.1 linux删除自带的JDK1.2 Linux上卸载自己安装的JDK2、安装2.1

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义