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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/666404.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gerrit 安装插件

1.插件下载 gerrit 3.9 插件&#xff0c;打开链接去右上角搜索插件名称&#xff0c;找到合适的版本&#xff0c;由于我这儿需要安装gerrit 3.9.1 的 autosubmitter 插件&#xff0c;但是好像没有 3.9 的&#xff0c;有下面这俩 上面那个可以理解为基于插件的主分支代码进行构…

java.lang.NoClassDefFoundError: org/springframework/aot/AotDetector 240204

springboot3.2.2改为2.7.18后 控制台异常显示: java.lang.NoClassDefFoundError: org/springframework/aot/AotDetector at org.mybatis.spring.mapper.ClassPathMapperScanner.(ClassPathMapperScanner.java:91) ~[mybatis-spring-3.0.3.jar:3.0.3] at org.mybatis.spring.m…

鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之MenuItemGroup组件

鸿蒙&#xff08;HarmonyOS&#xff09;项目方舟框架&#xff08;ArkUI&#xff09;之MenuItemGroup组件 一、操作环境 操作系统: Windows 10 专业版、IDE:DevEco Studio 3.1、SDK:HarmonyOS 3.1 二、MenuItemGroup组件 该组件用来展示菜单MenuItem的分组。 子组件 无 接…

高通android设备themal读取cpu温度

以msm8953的themal分布信息&#xff0c;主要是下图的位置&#xff1a; 这其中 cpu相关的themal的位置有&#xff1a; 读取thermal 温度数据可以通过以下几个步骤&#xff1a; 获取sensor_info rootmsm8953_64:/ # cat /sys/module/msm_thermal/sensor_info tsens:tsens_tz_se…

2024.1.30 Spark SQL的高级用法

目录 1、如何快速生成多行的序列 2、如何快速生成表数据 3.开窗函数 排序函数 平分函数 聚合函数 向上向下窗口函数 1、如何快速生成多行的序列 -- 需求: 请生成一列数据, 内容为 1 , 2 , 3 , 4 ,5 仅使用select语句 select explode(split(1,2,3,4,5,,)) as num;-- 需…

我用全志V851s做了一个魔法棒,使用Keras训练手势识别模型控制一切电子设备

这是一个可以直接启动原神的魔法棒~ 原神&#xff0c;启动&#xff01; 这是一个万全的解决方案&#xff01;只需要花80元再动动手&#xff0c;就可以将哈利波特的魔杖与人工智能结合到一起&#xff01;它就是用全志V851s做的赛博魔杖&#xff01; 这个魔法手杖有啥亮点 手势…

Linux mount命令教程:如何挂载文件系统(附案例详解和注意事项)

Linux mount命令介绍 mount命令在Linux中用于挂载Linux系统外的其它文件系统&#xff0c;每个设备在使用前都必须先挂载。此命令通常用于挂载文件系统。 Linux mount命令适用的Linux版本 mount命令在所有的Linux发行版中都是可用的&#xff0c;包括Debian、Ubuntu、Alpine、…

oracle 热备份和冷备份的优缺点

Oracle的热备份和冷备份是两种不同的备份策略&#xff0c;各有其优缺点。 热备份的优点包括&#xff1a; 可快速备份数据&#xff0c;备份时间短。备份时数据库仍可使用。可达到秒级恢复&#xff0c;即恢复到某一时间点上。可对几乎所有数据库实体进行恢复。恢复快速&#xf…

Go协程揭秘:轻量、并发与性能的完美结合

目录 1. Go协程简介什么是Go协程&#xff1f;Go协程与线程的比较Go协程的核心优势 2. Go协程的基本使用创建并启动Go协程使用匿名函数创建Go协程Go协程与主函数 3. Go协程的同步机制1. 通道 (Channels)2. sync.WaitGroup3. 互斥锁 (sync.Mutex) 4. Go协程的高级用法1. 选择器 (…

每日一题——LeetCode1394.找出数组中的幸运数

方法一 桶数组计数法 又要保存整数的数值和他出现的频次&#xff0c;那么碰到一个整数num就让res[num]&#xff0c;那么循环res数组&#xff0c;如果res[i]0则代表i没有在arr中出现过&#xff0c;res[i]n则代表i在arr中出现n次 因为题目要求只返回最大的幸运数&#xff0c;所…

计算机软件能力认证考试CCF-202312-1 仓库规划

#自己跑的测试没问题&#xff0c;不知道为啥就是不能满分 原理比较绕&#xff0c;就是让数组中一行不断地与其他行进行比较&#xff0c;最终得到各自的索引 #include <iostream> using namespace std; int main() {int n;int m;cin>>n>>m; int array[n][m];…

【C/C++ 08】简单计算器

一、题目 输入算术表达式&#xff0c;可包含空格&#xff0c;检查算术表达式的合法性&#xff0c;若算术表达式不合法&#xff0c;打印错误类型&#xff0c;若合法&#xff0c;则进行运算&#xff0c;打印计算结果。 二、算法 1. 将输入的算术表达式字符串去除空格。 2. 检查…

vue在main.js中引入三方插件不生效的原因

有的时候需要比较复杂的功能,但是自己实现比较复杂的话,可以引入第三方插件.如果这个第三方插件需要全局都使用的话,可以在main.js中进行引入. 比如router elementplus之类的. import { createApp } from vue import ElementPlus from element-plus import element-plus/dist/…

电源模块欠压保护点测试方法分享 纳米软件

电源欠压保护原理 欠压保护是指当电源电压低于一定值时&#xff0c;电源的保护功能会及时断开电路&#xff0c;避免设备受到损坏。电源欠压保护一般是通过一个或多个传感器来检测电压&#xff0c;当电压低于设定值时就会触发电源的保护功能&#xff0c;断开电路&#xff0c;保护…

猫用空气净化器真的能除菌吗?除毛可以用宠物空气净化器吗?

猫咪给我们带来了无尽的欢乐&#xff0c;但它们换毛时家里到处都是猫毛。我们会在地板、沙发上发现一大堆&#xff0c;甚至衣服也难逃其影响。这些浮毛中可能携带着微生物和尘螨等。对于免疫力较低的老年人、孩子和孕妇来说&#xff0c;他们更容易感染这些微生物。而对于鼻炎患…

[Tomcat问题]--使用Tomcat 10.x部署项目时,出现实例化Servlet类[xxx]异常

[Tomcat问题]–使用Tomcat 10.x部署项目时&#xff0c;出现实例化Servlet类[xxx]异常 本片博文在知乎同步更新 环境 OS: Windows 11 23H2Java Version: java 21.0.1 2023-10-17 LTSIDE: IntelliJ IDEA 2023.3.3Maven: Apache Maven 3.9.6Tomcat: Tomcat 10.1.18 ReleasedSer…

windows下docker的使用

目录 1&#xff1a;docker是什么&#xff0c;能干什么&#xff1f; 2&#xff1a;docker下初始化一个容器 1&#xff1a;工具支持 2&#xff1a;运行装载docker镜像 a&#xff1a;在docker toolbox底下有个start.sh&#xff0c;我们进去里面修改里面路径配置&#xff1a; …

vulhub中Adminer远程文件读取漏洞复现(CVE-2021-43008)

Adminer是一个PHP编写的开源数据库管理工具&#xff0c;支持MySQL、MariaDB、PostgreSQL、SQLite、MS SQL、Oracle、Elasticsearch、MongoDB等数据库。 在其版本1.12.0到4.6.2之间存在一处因为MySQL LOAD DATA LOCAL导致的文件读取漏洞。 参考链接&#xff1a; https://gith…

JAVA Studynote(7-8)

JAVA Studynote(7-8) 1.DOS系统 ​ *接受指令 *解析指令 *执行指令 2.相对路径和绝对路径 *相对路径 ​ *从当前目录开始定位&#xff0c;形成的一个路径 *绝对路径 ​ *从顶级目录d&#xff0c;开始定位&#xff0c;形成的路径 ​ *示例&#xff1a; 3.常用DOS指令 …

arch linux python venv

0x00 前言 直接使用全局pip安装库的&#xff0c;会有如下error error: externally-managed-environment This environment is externally managed ╰─> To install Python packages system-wide, try pacman -Spython-xyz, where xyz is the package you are trying toin…