五层抽象把 EEPROM 与 Flash 藏进同一个块号

【免责声明】本文仅限学习使用,涉及的接口行为以 AUTOSAR Classic Platform 4.3 规范原文为准,读者在自研基础软件中复现时自行承担结果。

术语 含义
NvM 非易失存储管理器,向上提供块级读写服务,位于服务层
MemIf 存储器抽象接口,把读写请求按设备索引转发给下层抽象模块
Ea EEPROM 抽象,把逻辑块号换算成 EEPROM 器件地址
Fee Flash EEPROM 仿真,在 Flash 上造出 EEPROM 那样的块语义
Eep EEPROM 驱动,直接操作片上或外部 EEPROM 器件
Fls Flash 驱动,按扇区擦除、按页编程
SPI Handler Driver 串行外设接口驱动,外部存储器通过它接入
逻辑块 上层看到的存储单元,以块号寻址,不关心物理位置
擦除扇区 Flash 可擦除的最小单位,写入前必须整扇区擦净
job 驱动层的一次异步任务,提交后轮询结果
垃圾回收 把仍有效的块搬进新扇区、腾出可擦扇区的过程

一块车身控制器上同时挂着两片存储器:MCU 内部那 32 KB 的 EEPROM,和板子上经 SPI 连出去的一片外部 EEPROM。应用层要保存一份标定参数,代码里只有一句 NvM_WriteBlock,没人关心这份数据最终落在硅片里还是板子上。这份从容是分层换来的,代价则摊在下面五层里。
"Flash 单元" "Fls" "Fee" "MemIf" "NvM" "应用" "Flash 单元" "Fls" "Fee" "MemIf" "NvM" "应用" #mermaid-svg-UgCIKarglH7gdQbL{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-UgCIKarglH7gdQbL .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-UgCIKarglH7gdQbL .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-UgCIKarglH7gdQbL .error-icon{fill:#552222;}#mermaid-svg-UgCIKarglH7gdQbL .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-UgCIKarglH7gdQbL .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-UgCIKarglH7gdQbL .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-UgCIKarglH7gdQbL .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-UgCIKarglH7gdQbL .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-UgCIKarglH7gdQbL .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-UgCIKarglH7gdQbL .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-UgCIKarglH7gdQbL .marker{fill:#333333;stroke:#333333;}#mermaid-svg-UgCIKarglH7gdQbL .marker.cross{stroke:#333333;}#mermaid-svg-UgCIKarglH7gdQbL svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-UgCIKarglH7gdQbL p{margin:0;}#mermaid-svg-UgCIKarglH7gdQbL .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-UgCIKarglH7gdQbL text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-UgCIKarglH7gdQbL .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-UgCIKarglH7gdQbL .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-UgCIKarglH7gdQbL .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-UgCIKarglH7gdQbL .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-UgCIKarglH7gdQbL #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-UgCIKarglH7gdQbL .sequenceNumber{fill:white;}#mermaid-svg-UgCIKarglH7gdQbL #sequencenumber{fill:#333;}#mermaid-svg-UgCIKarglH7gdQbL #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-UgCIKarglH7gdQbL .messageText{fill:#333;stroke:none;}#mermaid-svg-UgCIKarglH7gdQbL .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-UgCIKarglH7gdQbL .labelText,#mermaid-svg-UgCIKarglH7gdQbL .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-UgCIKarglH7gdQbL .loopText,#mermaid-svg-UgCIKarglH7gdQbL .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-UgCIKarglH7gdQbL .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-UgCIKarglH7gdQbL .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-UgCIKarglH7gdQbL .noteText,#mermaid-svg-UgCIKarglH7gdQbL .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-UgCIKarglH7gdQbL .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-UgCIKarglH7gdQbL .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-UgCIKarglH7gdQbL .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-UgCIKarglH7gdQbL .actorPopupMenu{position:absolute;}#mermaid-svg-UgCIKarglH7gdQbL .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-UgCIKarglH7gdQbL .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-UgCIKarglH7gdQbL .actor-man circle,#mermaid-svg-UgCIKarglH7gdQbL line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-UgCIKarglH7gdQbL :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} "写入块 0x0010""按设备索引提交写请求""转发到设备 0""选页、拼数据、判断是否需回收""提交编程 job""整页编程""job 完成""结果回传""块写结果""请求结束""返回成功"

一次写请求的全貌就摊在这张图上:应用没有接触介质,介质也没有感知应用,全部动作夹在中间五层里完成。读图只需盯两个问题:请求在哪一层被翻译,等待发生在哪一层。

1. 同一个块号,两种介质

存储器硬件抽象要回答的第一个问题是:为什么片上 EEPROM 与板外 EEPROM 能被同一套机制访问。

1.1 差异到底有多少条

把两片器件摆在一起看,差异远不止"一个在片内、一个在板外"。擦除单位、写粒度、访问通路、寿命都不同,任何一条漏掉,上层的统一调用就会在某块板子上失灵。

维度 片上 EEPROM 外部 EEPROM 片上 Flash
访问通路 MCU 内部总线 SPI 串行总线 Flash 控制器
写粒度 字节或字 字节或页 页,且擦除后整页编程
擦除单位 无独立擦除 无独立擦除 扇区,通常 2 KB 以上
单次写时延 毫秒级,期间不响应 毫秒级,受总线速率牵制 编程毫秒级,擦除数十毫秒
擦写寿命 数十万到百万次 百万次量级 一万到十万次
掉电风险 写周期内掉电丢数据 同左 擦除中掉电可波及整扇区
是否需要 RAM 缓冲 不需要 不需要 需要,读改写要整页暂存

表格最后三行是最要命的三条:寿命差一个数量级,擦除粒度差三个数量级,掉电损失的边界也不一样。上层若按 EEPROM 的直觉去写 Flash,先坏的一定是寿命。

寿命差一个数量级,落到业务上就是两套更新策略。片上 EEPROM 允许对同一块按事件持续改写,Flash 则要求把改写频率压到扇区寿命能承受的量级。这条差别在需求阶段就该落进数据字典,为每个块标注更新频率与容量,否则抽象层做得再完整,也只能被动承受上层给出的写入节奏。容量与成本之间同样是交易:EEPROM 每字节单价高,Flash 单价低,代价转成了 RAM 占用与实现复杂度。

1.2 差异被收在哪一层

抽象的落点很明确:把"介质是什么"这件事压到抽象层以下,抽象层以上只留"块"这一个概念。
#mermaid-svg-8nRMpgobGok6s68o{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-8nRMpgobGok6s68o .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-8nRMpgobGok6s68o .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-8nRMpgobGok6s68o .error-icon{fill:#552222;}#mermaid-svg-8nRMpgobGok6s68o .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-8nRMpgobGok6s68o .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-8nRMpgobGok6s68o .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-8nRMpgobGok6s68o .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-8nRMpgobGok6s68o .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-8nRMpgobGok6s68o .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-8nRMpgobGok6s68o .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-8nRMpgobGok6s68o .marker{fill:#333333;stroke:#333333;}#mermaid-svg-8nRMpgobGok6s68o .marker.cross{stroke:#333333;}#mermaid-svg-8nRMpgobGok6s68o svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-8nRMpgobGok6s68o p{margin:0;}#mermaid-svg-8nRMpgobGok6s68o .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-8nRMpgobGok6s68o .cluster-label text{fill:#333;}#mermaid-svg-8nRMpgobGok6s68o .cluster-label span{color:#333;}#mermaid-svg-8nRMpgobGok6s68o .cluster-label span p{background-color:transparent;}#mermaid-svg-8nRMpgobGok6s68o .label text,#mermaid-svg-8nRMpgobGok6s68o span{fill:#333;color:#333;}#mermaid-svg-8nRMpgobGok6s68o .node rect,#mermaid-svg-8nRMpgobGok6s68o .node circle,#mermaid-svg-8nRMpgobGok6s68o .node ellipse,#mermaid-svg-8nRMpgobGok6s68o .node polygon,#mermaid-svg-8nRMpgobGok6s68o .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-8nRMpgobGok6s68o .rough-node .label text,#mermaid-svg-8nRMpgobGok6s68o .node .label text,#mermaid-svg-8nRMpgobGok6s68o .image-shape .label,#mermaid-svg-8nRMpgobGok6s68o .icon-shape .label{text-anchor:middle;}#mermaid-svg-8nRMpgobGok6s68o .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-8nRMpgobGok6s68o .rough-node .label,#mermaid-svg-8nRMpgobGok6s68o .node .label,#mermaid-svg-8nRMpgobGok6s68o .image-shape .label,#mermaid-svg-8nRMpgobGok6s68o .icon-shape .label{text-align:center;}#mermaid-svg-8nRMpgobGok6s68o .node.clickable{cursor:pointer;}#mermaid-svg-8nRMpgobGok6s68o .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-8nRMpgobGok6s68o .arrowheadPath{fill:#333333;}#mermaid-svg-8nRMpgobGok6s68o .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-8nRMpgobGok6s68o .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-8nRMpgobGok6s68o .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8nRMpgobGok6s68o .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-8nRMpgobGok6s68o .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8nRMpgobGok6s68o .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-8nRMpgobGok6s68o .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-8nRMpgobGok6s68o .cluster text{fill:#333;}#mermaid-svg-8nRMpgobGok6s68o .cluster span{color:#333;}#mermaid-svg-8nRMpgobGok6s68o div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-8nRMpgobGok6s68o .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-8nRMpgobGok6s68o rect.text{fill:none;stroke-width:0;}#mermaid-svg-8nRMpgobGok6s68o .icon-shape,#mermaid-svg-8nRMpgobGok6s68o .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8nRMpgobGok6s68o .icon-shape p,#mermaid-svg-8nRMpgobGok6s68o .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-8nRMpgobGok6s68o .icon-shape .label rect,#mermaid-svg-8nRMpgobGok6s68o .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8nRMpgobGok6s68o .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-8nRMpgobGok6s68o .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-8nRMpgobGok6s68o :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 应用层:业务数据
RTE 与 NvM:块级读写
MemIf:设备索引转发
Ea:EEPROM 抽象
Fee:Flash 仿真 EEPROM
Eep:EEPROM 驱动
Fls:Flash 驱动
片上 EEPROM
SPI Handler:外部器件
Flash 扇区

读这张图的方法是从下往上看:最底下一排是三种物理事实,往上每一层都在收掉一种差异。Eep 与 Fls 收掉总线差异,Ea 与 Fee 收掉寻址与擦除差异,MemIf 收掉"用哪个设备"的差异。

这种收法在需求里写得相当直接:抽象模块要给上层一个虚拟的 32 位地址空间,块号与物理地址之间的换算由它自己负责,出处见《存储器硬件抽象层需求》6.1.1.3.1 与 6.1.1.3.4。

1.3 两条实现边界

规范给这一层划了两条边界,一条约束实现,一条约束接口,两者方向相反,不能互相顶替。

边界 内容 违反后的后果
实现与 μC 无关 抽象层的代码不得内联任何 MCU 寄存器操作,硬件动作一律下沉到驱动 换芯片要重写抽象层,分层失去意义
实现与外部设备相关 块的容量、器件布局、冗余策略允许随项目配置不同 强行做成通用件,配置项会膨胀到无法维护
上层接口与 μC、ECU 硬件无关 读写接口只出现块号、数据指针、长度、设备索引 接口带上硬件信息,上层业务代码失去可移植性

这三条合起来解释了一个常见误判:抽象层不是"硬件无关层",它是"硬件差异的集散地"。差异没有消失,只是被集中到一处管理。

2. MemIf:上层只认一个句柄

第二层要回答的是:既然下层有 Ea 与 Fee 两个模块,上层凭什么只写一套调用。

2.1 一个入口的必然性

如果让 NvM 自己判断"这次该找 Ea 还是 Fee",判断逻辑就会散进每个调用点,且随硬件配置变化而漂移。MemIf 存在的意义就是把这份判断收成一张配置表。
#mermaid-svg-Rxij60zldiJAUUds{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Rxij60zldiJAUUds .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Rxij60zldiJAUUds .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Rxij60zldiJAUUds .error-icon{fill:#552222;}#mermaid-svg-Rxij60zldiJAUUds .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Rxij60zldiJAUUds .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Rxij60zldiJAUUds .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Rxij60zldiJAUUds .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Rxij60zldiJAUUds .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Rxij60zldiJAUUds .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Rxij60zldiJAUUds .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Rxij60zldiJAUUds .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Rxij60zldiJAUUds .marker.cross{stroke:#333333;}#mermaid-svg-Rxij60zldiJAUUds svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Rxij60zldiJAUUds p{margin:0;}#mermaid-svg-Rxij60zldiJAUUds .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Rxij60zldiJAUUds .cluster-label text{fill:#333;}#mermaid-svg-Rxij60zldiJAUUds .cluster-label span{color:#333;}#mermaid-svg-Rxij60zldiJAUUds .cluster-label span p{background-color:transparent;}#mermaid-svg-Rxij60zldiJAUUds .label text,#mermaid-svg-Rxij60zldiJAUUds span{fill:#333;color:#333;}#mermaid-svg-Rxij60zldiJAUUds .node rect,#mermaid-svg-Rxij60zldiJAUUds .node circle,#mermaid-svg-Rxij60zldiJAUUds .node ellipse,#mermaid-svg-Rxij60zldiJAUUds .node polygon,#mermaid-svg-Rxij60zldiJAUUds .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Rxij60zldiJAUUds .rough-node .label text,#mermaid-svg-Rxij60zldiJAUUds .node .label text,#mermaid-svg-Rxij60zldiJAUUds .image-shape .label,#mermaid-svg-Rxij60zldiJAUUds .icon-shape .label{text-anchor:middle;}#mermaid-svg-Rxij60zldiJAUUds .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Rxij60zldiJAUUds .rough-node .label,#mermaid-svg-Rxij60zldiJAUUds .node .label,#mermaid-svg-Rxij60zldiJAUUds .image-shape .label,#mermaid-svg-Rxij60zldiJAUUds .icon-shape .label{text-align:center;}#mermaid-svg-Rxij60zldiJAUUds .node.clickable{cursor:pointer;}#mermaid-svg-Rxij60zldiJAUUds .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Rxij60zldiJAUUds .arrowheadPath{fill:#333333;}#mermaid-svg-Rxij60zldiJAUUds .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Rxij60zldiJAUUds .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Rxij60zldiJAUUds .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Rxij60zldiJAUUds .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Rxij60zldiJAUUds .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Rxij60zldiJAUUds .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Rxij60zldiJAUUds .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Rxij60zldiJAUUds .cluster text{fill:#333;}#mermaid-svg-Rxij60zldiJAUUds .cluster span{color:#333;}#mermaid-svg-Rxij60zldiJAUUds div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Rxij60zldiJAUUds .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Rxij60zldiJAUUds rect.text{fill:none;stroke-width:0;}#mermaid-svg-Rxij60zldiJAUUds .icon-shape,#mermaid-svg-Rxij60zldiJAUUds .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Rxij60zldiJAUUds .icon-shape p,#mermaid-svg-Rxij60zldiJAUUds .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Rxij60zldiJAUUds .icon-shape .label rect,#mermaid-svg-Rxij60zldiJAUUds .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Rxij60zldiJAUUds .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Rxij60zldiJAUUds .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Rxij60zldiJAUUds :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} NvM:块级请求
MemIf:统一接口
设备 0 至 Ea
设备 1 至 Fee
Eep
Fls

图上 MemIf 只有转发与状态汇总两个动作,它自己不缓存、不校验、不改写数据。这一点常被误解成"MemIf 太薄所以可有可无",恰恰相反:正因为它薄,替换下层设备时它才不需要改。

2.2 设备索引怎么落到模块

分流靠配置,不靠代码里的条件判断。下表是一份典型的映射表,索引值与模块归属由集成阶段写死。

设备索引 下层模块 物理载体 块的典型用途
0 Fee 片上 Flash 扇区 标定参数、故障快照
1 Ea 片上 EEPROM 小容量计数、里程
2 Ea SPI 外部 EEPROM 可拆换模块的配置

同一个 Ea 模块可以同时服务两个设备,区别只在配置里给的驱动句柄与地址基址不同。索引一旦定下就不能在运行期改,否则已保存的块会落到别的介质上。

设备索引本身就是一条需求:《存储器硬件抽象层需求》6.1.2.1.2 要求抽象接口借它选择下层模块,同节 6.1.2.4.1 又限定接口只校验自己会用到的那几个参数,其余原样透传。这条限定是 MemIf 保持轻薄的依据。

2.3 同步调用是错觉

接口签名看起来像同步函数,实际语义是提交任务加轮询结果。三态与四态的组合决定了调用方必须会等。
#mermaid-svg-nYih5e1hMuT6SlS2{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-nYih5e1hMuT6SlS2 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-nYih5e1hMuT6SlS2 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-nYih5e1hMuT6SlS2 .error-icon{fill:#552222;}#mermaid-svg-nYih5e1hMuT6SlS2 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-nYih5e1hMuT6SlS2 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-nYih5e1hMuT6SlS2 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-nYih5e1hMuT6SlS2 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-nYih5e1hMuT6SlS2 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-nYih5e1hMuT6SlS2 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-nYih5e1hMuT6SlS2 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-nYih5e1hMuT6SlS2 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-nYih5e1hMuT6SlS2 .marker.cross{stroke:#333333;}#mermaid-svg-nYih5e1hMuT6SlS2 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-nYih5e1hMuT6SlS2 p{margin:0;}#mermaid-svg-nYih5e1hMuT6SlS2 defs #statediagram-barbEnd{fill:#333333;stroke:#333333;}#mermaid-svg-nYih5e1hMuT6SlS2 g.stateGroup text{fill:#9370DB;stroke:none;font-size:10px;}#mermaid-svg-nYih5e1hMuT6SlS2 g.stateGroup text{fill:#333;stroke:none;font-size:10px;}#mermaid-svg-nYih5e1hMuT6SlS2 g.stateGroup .state-title{font-weight:bolder;fill:#131300;}#mermaid-svg-nYih5e1hMuT6SlS2 g.stateGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-nYih5e1hMuT6SlS2 g.stateGroup line{stroke:#333333;stroke-width:1;}#mermaid-svg-nYih5e1hMuT6SlS2 .transition{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-nYih5e1hMuT6SlS2 .stateGroup .composit{fill:white;border-bottom:1px;}#mermaid-svg-nYih5e1hMuT6SlS2 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px;}#mermaid-svg-nYih5e1hMuT6SlS2 .state-note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-nYih5e1hMuT6SlS2 .state-note text{fill:black;stroke:none;font-size:10px;}#mermaid-svg-nYih5e1hMuT6SlS2 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-nYih5e1hMuT6SlS2 .edgeLabel .label rect{fill:#ECECFF;opacity:0.5;}#mermaid-svg-nYih5e1hMuT6SlS2 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-nYih5e1hMuT6SlS2 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-nYih5e1hMuT6SlS2 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-nYih5e1hMuT6SlS2 .edgeLabel .label text{fill:#333;}#mermaid-svg-nYih5e1hMuT6SlS2 .label div .edgeLabel{color:#333;}#mermaid-svg-nYih5e1hMuT6SlS2 .stateLabel text{fill:#131300;font-size:10px;font-weight:bold;}#mermaid-svg-nYih5e1hMuT6SlS2 .node circle.state-start{fill:#333333;stroke:#333333;}#mermaid-svg-nYih5e1hMuT6SlS2 .node .fork-join{fill:#333333;stroke:#333333;}#mermaid-svg-nYih5e1hMuT6SlS2 .node circle.state-end{fill:#9370DB;stroke:white;stroke-width:1.5;}#mermaid-svg-nYih5e1hMuT6SlS2 .end-state-inner{fill:white;stroke-width:1.5;}#mermaid-svg-nYih5e1hMuT6SlS2 .node rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-nYih5e1hMuT6SlS2 .node polygon{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-nYih5e1hMuT6SlS2 #statediagram-barbEnd{fill:#333333;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-nYih5e1hMuT6SlS2 .cluster-label,#mermaid-svg-nYih5e1hMuT6SlS2 .nodeLabel{color:#131300;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-cluster rect.outer{rx:5px;ry:5px;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-state .divider{stroke:#9370DB;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-state .title-state{rx:5px;ry:5px;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-cluster.statediagram-cluster .inner{fill:white;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#f0f0f0;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-cluster .inner{rx:0;ry:0;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-state rect.basic{rx:5px;ry:5px;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#f0f0f0;}#mermaid-svg-nYih5e1hMuT6SlS2 .note-edge{stroke-dasharray:5;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-note text{fill:black;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram-note .nodeLabel{color:black;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagram .edgeLabel{color:red;}#mermaid-svg-nYih5e1hMuT6SlS2 #dependencyStart,#mermaid-svg-nYih5e1hMuT6SlS2 #dependencyEnd{fill:#333333;stroke:#333333;stroke-width:1;}#mermaid-svg-nYih5e1hMuT6SlS2 .statediagramTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-nYih5e1hMuT6SlS2 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} "初始化完成"
"提交读或写"
"job 完成"
"结果仍为挂起"
"反初始化"
未初始化
空闲
忙

状态机里没有"成功"与"失败"这两个状态,因为那属于 job 结果的取值范围,与模块状态是两个维度。把两者混为一谈的代码,通常在超时分支上出错:状态回到空闲不代表这次写成功,必须再读一次结果。

这种设计与上层的时间预算直接相关。调用方必须给轮询留出周期,周期要短于一次写的最长可能耗时,否则第一次轮询必然拿到挂起;周期也不能短到空转烧 CPU。定时器节拍、业务周期与存储器写入时间三者要对齐,这一步常被集成阶段跳过,后果是某个偶发超时在量产车上才冒出来。

3. Ea:块号到地址的换算

第三层要回答的是:上层给的块号,怎么变成一片硅片上的具体偏移。

3.1 块是逻辑单位,地址是物理单位

块号只在配置表里有意义,它背后挂着一组参数:块大小、器件内起始地址、是否立即写入。换算就是查表加加法,没有别的机关。

概念 由谁定义 取值范围 作用
逻辑块号 配置表,全项目唯一 0 到块总数减一 上层唯一可见的标识
块大小 配置表,按块固定 字节数,不得跨器件边界 决定地址推进量
器件起始地址 硬件布局与原理图 器件地址空间内 把块映射到物理位置
页内偏移 驱动按写入长度维护 页大小内 外部器件按页写入时用到

这张表也说明了 Ea 的一个限制:块大小在配置期固定,运行期不能变。要写一段变长数据,只能预留足够大的块,或由上层拆成多个固定块。

换算规则写在《EEPROM 抽象层规范》7.1.2,那里把寻址方案与地址计算分成两节:寻址方案定块边界与对齐,地址计算只负责在块内加偏移。分成两节的好处是块布局可以随项目重排,而加法部分不用重写。

3.2 一次 Ea 写的调用链

Ea 本身几乎不做计算,它的工作是把块号翻译成地址,然后把 job 交给 Eep,再等结果。
"EEPROM 器件" "Eep" "Ea" "MemIf" "EEPROM 器件" "Eep" "Ea" "MemIf" #mermaid-svg-NvvugIBfx9WwZO2V{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-NvvugIBfx9WwZO2V .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-NvvugIBfx9WwZO2V .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-NvvugIBfx9WwZO2V .error-icon{fill:#552222;}#mermaid-svg-NvvugIBfx9WwZO2V .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-NvvugIBfx9WwZO2V .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-NvvugIBfx9WwZO2V .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-NvvugIBfx9WwZO2V .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-NvvugIBfx9WwZO2V .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-NvvugIBfx9WwZO2V .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-NvvugIBfx9WwZO2V .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-NvvugIBfx9WwZO2V .marker{fill:#333333;stroke:#333333;}#mermaid-svg-NvvugIBfx9WwZO2V .marker.cross{stroke:#333333;}#mermaid-svg-NvvugIBfx9WwZO2V svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-NvvugIBfx9WwZO2V p{margin:0;}#mermaid-svg-NvvugIBfx9WwZO2V .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-NvvugIBfx9WwZO2V text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-NvvugIBfx9WwZO2V .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-NvvugIBfx9WwZO2V .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-NvvugIBfx9WwZO2V .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-NvvugIBfx9WwZO2V .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-NvvugIBfx9WwZO2V #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-NvvugIBfx9WwZO2V .sequenceNumber{fill:white;}#mermaid-svg-NvvugIBfx9WwZO2V #sequencenumber{fill:#333;}#mermaid-svg-NvvugIBfx9WwZO2V #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-NvvugIBfx9WwZO2V .messageText{fill:#333;stroke:none;}#mermaid-svg-NvvugIBfx9WwZO2V .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-NvvugIBfx9WwZO2V .labelText,#mermaid-svg-NvvugIBfx9WwZO2V .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-NvvugIBfx9WwZO2V .loopText,#mermaid-svg-NvvugIBfx9WwZO2V .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-NvvugIBfx9WwZO2V .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-NvvugIBfx9WwZO2V .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-NvvugIBfx9WwZO2V .noteText,#mermaid-svg-NvvugIBfx9WwZO2V .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-NvvugIBfx9WwZO2V .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-NvvugIBfx9WwZO2V .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-NvvugIBfx9WwZO2V .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-NvvugIBfx9WwZO2V .actorPopupMenu{position:absolute;}#mermaid-svg-NvvugIBfx9WwZO2V .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-NvvugIBfx9WwZO2V .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-NvvugIBfx9WwZO2V .actor-man circle,#mermaid-svg-NvvugIBfx9WwZO2V line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-NvvugIBfx9WwZO2V :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} "写块号 N,长度 L""查表得器件地址与块大小""提交写 job,基址加偏移""按总线时序写入""器件应答""job 结果""块写结果"

链路上没有缓存环节,这意味着写请求发出后数据立刻离开 RAM 进入总线。好处是 RAM 占用为零,坏处是调用方必须在数据交出后保持缓冲区不变,直到结果返回。

3.3 校验与重试放在哪一侧

EEPROM 器件通常自身不带校验,读回来的数据可能是写坏的结果。校验可以放在 Ea 之上,也可以放在器件布局里,两条路的成本不同。
#mermaid-svg-Jv0Y0OC6PZLsDFNK{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .error-icon{fill:#552222;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .marker.cross{stroke:#333333;}#mermaid-svg-Jv0Y0OC6PZLsDFNK svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Jv0Y0OC6PZLsDFNK p{margin:0;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .cluster-label text{fill:#333;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .cluster-label span{color:#333;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .cluster-label span p{background-color:transparent;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .label text,#mermaid-svg-Jv0Y0OC6PZLsDFNK span{fill:#333;color:#333;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .node rect,#mermaid-svg-Jv0Y0OC6PZLsDFNK .node circle,#mermaid-svg-Jv0Y0OC6PZLsDFNK .node ellipse,#mermaid-svg-Jv0Y0OC6PZLsDFNK .node polygon,#mermaid-svg-Jv0Y0OC6PZLsDFNK .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .rough-node .label text,#mermaid-svg-Jv0Y0OC6PZLsDFNK .node .label text,#mermaid-svg-Jv0Y0OC6PZLsDFNK .image-shape .label,#mermaid-svg-Jv0Y0OC6PZLsDFNK .icon-shape .label{text-anchor:middle;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .rough-node .label,#mermaid-svg-Jv0Y0OC6PZLsDFNK .node .label,#mermaid-svg-Jv0Y0OC6PZLsDFNK .image-shape .label,#mermaid-svg-Jv0Y0OC6PZLsDFNK .icon-shape .label{text-align:center;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .node.clickable{cursor:pointer;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .arrowheadPath{fill:#333333;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Jv0Y0OC6PZLsDFNK .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Jv0Y0OC6PZLsDFNK .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Jv0Y0OC6PZLsDFNK .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .cluster text{fill:#333;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .cluster span{color:#333;}#mermaid-svg-Jv0Y0OC6PZLsDFNK div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Jv0Y0OC6PZLsDFNK rect.text{fill:none;stroke-width:0;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .icon-shape,#mermaid-svg-Jv0Y0OC6PZLsDFNK .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .icon-shape p,#mermaid-svg-Jv0Y0OC6PZLsDFNK .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .icon-shape .label rect,#mermaid-svg-Jv0Y0OC6PZLsDFNK .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Jv0Y0OC6PZLsDFNK .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Jv0Y0OC6PZLsDFNK .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Jv0Y0OC6PZLsDFNK :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
写入请求
附加校验字段
写器件
读回比对
一致
返回成功
重写一次

读回比对要多花一次总线往返,重写会额外消耗寿命。小容量、低频写的场景值得这么做;高频写的数据块更适合交给 NvM 的冗余拷贝机制,用两份块互为备份来换取读时的可判断性。

这笔开销可以量化:一次 256 字节块的往返在低速总线上要数毫秒,按每秒十次的写入频率,累计到一年就是上百小时的总线占用。值不值取决于该块损坏后的后果,而非取决于开销本身------存不回来的数据值得为它多付一次往返,能重算的数据连校验都可省。

4. Fee:在 Flash 上造一个 EEPROM

第四层是整条链路里最难的一层:Flash 不能按字节改写,却要装出按块改写的样子。

4.1 擦除单位决定仿真方式

Flash 的基本矛盾只有一句话:擦除以一整块为单位,写入只能把 1 变 0。要在这样的介质上做"改一个字节",唯一出路是不在原地改,而是另找空地写新版本。
#mermaid-svg-6Nda6Vw2kS2EJSJO{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-6Nda6Vw2kS2EJSJO .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-6Nda6Vw2kS2EJSJO .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-6Nda6Vw2kS2EJSJO .error-icon{fill:#552222;}#mermaid-svg-6Nda6Vw2kS2EJSJO .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-6Nda6Vw2kS2EJSJO .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-6Nda6Vw2kS2EJSJO .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-6Nda6Vw2kS2EJSJO .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-6Nda6Vw2kS2EJSJO .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-6Nda6Vw2kS2EJSJO .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-6Nda6Vw2kS2EJSJO .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-6Nda6Vw2kS2EJSJO .marker{fill:#333333;stroke:#333333;}#mermaid-svg-6Nda6Vw2kS2EJSJO .marker.cross{stroke:#333333;}#mermaid-svg-6Nda6Vw2kS2EJSJO svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-6Nda6Vw2kS2EJSJO p{margin:0;}#mermaid-svg-6Nda6Vw2kS2EJSJO .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-6Nda6Vw2kS2EJSJO .cluster-label text{fill:#333;}#mermaid-svg-6Nda6Vw2kS2EJSJO .cluster-label span{color:#333;}#mermaid-svg-6Nda6Vw2kS2EJSJO .cluster-label span p{background-color:transparent;}#mermaid-svg-6Nda6Vw2kS2EJSJO .label text,#mermaid-svg-6Nda6Vw2kS2EJSJO span{fill:#333;color:#333;}#mermaid-svg-6Nda6Vw2kS2EJSJO .node rect,#mermaid-svg-6Nda6Vw2kS2EJSJO .node circle,#mermaid-svg-6Nda6Vw2kS2EJSJO .node ellipse,#mermaid-svg-6Nda6Vw2kS2EJSJO .node polygon,#mermaid-svg-6Nda6Vw2kS2EJSJO .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-6Nda6Vw2kS2EJSJO .rough-node .label text,#mermaid-svg-6Nda6Vw2kS2EJSJO .node .label text,#mermaid-svg-6Nda6Vw2kS2EJSJO .image-shape .label,#mermaid-svg-6Nda6Vw2kS2EJSJO .icon-shape .label{text-anchor:middle;}#mermaid-svg-6Nda6Vw2kS2EJSJO .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-6Nda6Vw2kS2EJSJO .rough-node .label,#mermaid-svg-6Nda6Vw2kS2EJSJO .node .label,#mermaid-svg-6Nda6Vw2kS2EJSJO .image-shape .label,#mermaid-svg-6Nda6Vw2kS2EJSJO .icon-shape .label{text-align:center;}#mermaid-svg-6Nda6Vw2kS2EJSJO .node.clickable{cursor:pointer;}#mermaid-svg-6Nda6Vw2kS2EJSJO .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-6Nda6Vw2kS2EJSJO .arrowheadPath{fill:#333333;}#mermaid-svg-6Nda6Vw2kS2EJSJO .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-6Nda6Vw2kS2EJSJO .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-6Nda6Vw2kS2EJSJO .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-6Nda6Vw2kS2EJSJO .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-6Nda6Vw2kS2EJSJO .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-6Nda6Vw2kS2EJSJO .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-6Nda6Vw2kS2EJSJO .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-6Nda6Vw2kS2EJSJO .cluster text{fill:#333;}#mermaid-svg-6Nda6Vw2kS2EJSJO .cluster span{color:#333;}#mermaid-svg-6Nda6Vw2kS2EJSJO div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-6Nda6Vw2kS2EJSJO .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-6Nda6Vw2kS2EJSJO rect.text{fill:none;stroke-width:0;}#mermaid-svg-6Nda6Vw2kS2EJSJO .icon-shape,#mermaid-svg-6Nda6Vw2kS2EJSJO .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-6Nda6Vw2kS2EJSJO .icon-shape p,#mermaid-svg-6Nda6Vw2kS2EJSJO .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-6Nda6Vw2kS2EJSJO .icon-shape .label rect,#mermaid-svg-6Nda6Vw2kS2EJSJO .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-6Nda6Vw2kS2EJSJO .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-6Nda6Vw2kS2EJSJO .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-6Nda6Vw2kS2EJSJO :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 逻辑块 0x0010
数据块实例:第 1 版
数据块实例:第 2 版
数据块实例:第 3 版
扇区 0:已满
扇区 1:使用中
扇区 2:已擦净待用

图上的关键点是"一个逻辑块对应多个物理实例"。写一次不是覆盖,而是在扇区里追加一个新实例,最新版本由写入顺序与块头信息判定。读的时候要扫到最新那一份,这份扫描成本就是 Fee 的固有开销。

4.2 垃圾回收什么时候启动

追加写总有写满的一天。扇区写满后,仍然有效的块要被搬到别处,整扇区才能擦净复用。
#mermaid-svg-ejsHrFhWWkBYJeOq{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ejsHrFhWWkBYJeOq .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ejsHrFhWWkBYJeOq .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ejsHrFhWWkBYJeOq .error-icon{fill:#552222;}#mermaid-svg-ejsHrFhWWkBYJeOq .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ejsHrFhWWkBYJeOq .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ejsHrFhWWkBYJeOq .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ejsHrFhWWkBYJeOq .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ejsHrFhWWkBYJeOq .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ejsHrFhWWkBYJeOq .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ejsHrFhWWkBYJeOq .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ejsHrFhWWkBYJeOq .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ejsHrFhWWkBYJeOq .marker.cross{stroke:#333333;}#mermaid-svg-ejsHrFhWWkBYJeOq svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ejsHrFhWWkBYJeOq p{margin:0;}#mermaid-svg-ejsHrFhWWkBYJeOq defs #statediagram-barbEnd{fill:#333333;stroke:#333333;}#mermaid-svg-ejsHrFhWWkBYJeOq g.stateGroup text{fill:#9370DB;stroke:none;font-size:10px;}#mermaid-svg-ejsHrFhWWkBYJeOq g.stateGroup text{fill:#333;stroke:none;font-size:10px;}#mermaid-svg-ejsHrFhWWkBYJeOq g.stateGroup .state-title{font-weight:bolder;fill:#131300;}#mermaid-svg-ejsHrFhWWkBYJeOq g.stateGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-ejsHrFhWWkBYJeOq g.stateGroup line{stroke:#333333;stroke-width:1;}#mermaid-svg-ejsHrFhWWkBYJeOq .transition{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-ejsHrFhWWkBYJeOq .stateGroup .composit{fill:white;border-bottom:1px;}#mermaid-svg-ejsHrFhWWkBYJeOq .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px;}#mermaid-svg-ejsHrFhWWkBYJeOq .state-note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-ejsHrFhWWkBYJeOq .state-note text{fill:black;stroke:none;font-size:10px;}#mermaid-svg-ejsHrFhWWkBYJeOq .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-ejsHrFhWWkBYJeOq .edgeLabel .label rect{fill:#ECECFF;opacity:0.5;}#mermaid-svg-ejsHrFhWWkBYJeOq .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ejsHrFhWWkBYJeOq .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ejsHrFhWWkBYJeOq .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ejsHrFhWWkBYJeOq .edgeLabel .label text{fill:#333;}#mermaid-svg-ejsHrFhWWkBYJeOq .label div .edgeLabel{color:#333;}#mermaid-svg-ejsHrFhWWkBYJeOq .stateLabel text{fill:#131300;font-size:10px;font-weight:bold;}#mermaid-svg-ejsHrFhWWkBYJeOq .node circle.state-start{fill:#333333;stroke:#333333;}#mermaid-svg-ejsHrFhWWkBYJeOq .node .fork-join{fill:#333333;stroke:#333333;}#mermaid-svg-ejsHrFhWWkBYJeOq .node circle.state-end{fill:#9370DB;stroke:white;stroke-width:1.5;}#mermaid-svg-ejsHrFhWWkBYJeOq .end-state-inner{fill:white;stroke-width:1.5;}#mermaid-svg-ejsHrFhWWkBYJeOq .node rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ejsHrFhWWkBYJeOq .node polygon{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ejsHrFhWWkBYJeOq #statediagram-barbEnd{fill:#333333;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ejsHrFhWWkBYJeOq .cluster-label,#mermaid-svg-ejsHrFhWWkBYJeOq .nodeLabel{color:#131300;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-cluster rect.outer{rx:5px;ry:5px;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-state .divider{stroke:#9370DB;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-state .title-state{rx:5px;ry:5px;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-cluster.statediagram-cluster .inner{fill:white;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-cluster.statediagram-cluster-alt .inner{fill:#f0f0f0;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-cluster .inner{rx:0;ry:0;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-state rect.basic{rx:5px;ry:5px;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#f0f0f0;}#mermaid-svg-ejsHrFhWWkBYJeOq .note-edge{stroke-dasharray:5;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-note text{fill:black;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram-note .nodeLabel{color:black;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagram .edgeLabel{color:red;}#mermaid-svg-ejsHrFhWWkBYJeOq #dependencyStart,#mermaid-svg-ejsHrFhWWkBYJeOq #dependencyEnd{fill:#333333;stroke:#333333;stroke-width:1;}#mermaid-svg-ejsHrFhWWkBYJeOq .statediagramTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ejsHrFhWWkBYJeOq :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} "空闲空间低于阈值"
"选源扇区与目标扇区"
"有效块复制完毕"
"源扇区无有效块"
"扇区擦净"
正常写入
扇区将满
搬迁中
擦除中

垃圾回收的代价集中在两点:搬迁期间 RAM 里要同时放得下一页甚至一扇区的数据,写请求的响应时间也会被拉长到擦除量级。阈值定得太晚,会在最需要写入的时刻触发回收;定得太早,又浪费可用空间。

4.3 掉电一致性靠写入顺序

扇区擦到一半掉电,会毁掉整扇区。Fee 用块头与写入顺序把损失限制在单块内。
"Fls" "页缓冲" "Fee" "Fls" "页缓冲" "Fee" #mermaid-svg-7TNRYLidvrXoa6aY{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-7TNRYLidvrXoa6aY .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-7TNRYLidvrXoa6aY .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-7TNRYLidvrXoa6aY .error-icon{fill:#552222;}#mermaid-svg-7TNRYLidvrXoa6aY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-7TNRYLidvrXoa6aY .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-7TNRYLidvrXoa6aY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-7TNRYLidvrXoa6aY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-7TNRYLidvrXoa6aY .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-7TNRYLidvrXoa6aY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-7TNRYLidvrXoa6aY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-7TNRYLidvrXoa6aY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-7TNRYLidvrXoa6aY .marker.cross{stroke:#333333;}#mermaid-svg-7TNRYLidvrXoa6aY svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-7TNRYLidvrXoa6aY p{margin:0;}#mermaid-svg-7TNRYLidvrXoa6aY .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-7TNRYLidvrXoa6aY text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-7TNRYLidvrXoa6aY .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-7TNRYLidvrXoa6aY .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-7TNRYLidvrXoa6aY .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-7TNRYLidvrXoa6aY .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-7TNRYLidvrXoa6aY #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-7TNRYLidvrXoa6aY .sequenceNumber{fill:white;}#mermaid-svg-7TNRYLidvrXoa6aY #sequencenumber{fill:#333;}#mermaid-svg-7TNRYLidvrXoa6aY #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-7TNRYLidvrXoa6aY .messageText{fill:#333;stroke:none;}#mermaid-svg-7TNRYLidvrXoa6aY .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-7TNRYLidvrXoa6aY .labelText,#mermaid-svg-7TNRYLidvrXoa6aY .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-7TNRYLidvrXoa6aY .loopText,#mermaid-svg-7TNRYLidvrXoa6aY .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-7TNRYLidvrXoa6aY .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-7TNRYLidvrXoa6aY .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-7TNRYLidvrXoa6aY .noteText,#mermaid-svg-7TNRYLidvrXoa6aY .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-7TNRYLidvrXoa6aY .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-7TNRYLidvrXoa6aY .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-7TNRYLidvrXoa6aY .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-7TNRYLidvrXoa6aY .actorPopupMenu{position:absolute;}#mermaid-svg-7TNRYLidvrXoa6aY .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-7TNRYLidvrXoa6aY .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-7TNRYLidvrXoa6aY .actor-man circle,#mermaid-svg-7TNRYLidvrXoa6aY line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-7TNRYLidvrXoa6aY :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} "掉电发生时,新实例块头不完整,读时被判为无效" "组数据加块头""编程到扇区空闲位置""编程完成""更新最新实例索引"

顺序是先保证数据与块头的完整落盘,再更新内存里的索引。掉电后重新扫描扇区,块头不完整的实例直接跳过,旧版本仍然有效,读到的就是上一个成功写入的值。

块一致性信息的维护在《Flash EEPROM 仿真规范》7.1.5 有专门一节,三条要求连成一条线:能识别写入中断造成的不一致、如实上报、不把不一致的数据交回调用方。第三条最关键,它把"数据可能坏了"这件事挡在抽象层内部,上层永远只看到成功或失败两种结果。

4.4 磨损的账要提前算

Flash 扇区寿命有限,垃圾回收会把擦除次数摊到各扇区,摊不平就会出现短板。

因素 对寿命的影响 可调手段
单块写入频率 写入越频繁,所在扇区擦得越快 热块分散到不同扇区
扇区大小 扇区越大,单次擦除搬迁的数据越多 按总容量与块数选扇区数
回收策略 总选同一扇区回收,寿命快速耗尽 轮转选择,记录擦除计数
块利用率 利用率低时无效实例多,回收更频繁 提高块大小或减少逻辑块数量

按每天写一万次、扇区寿命十万次估算,同一个扇区只撑十天。这就是为什么 Fee 的回收策略不能图省事,它直接决定整车的存储器寿命预算。

回收阈值与写入放大是同一枚硬币的两面。阈值宽松,可用空间充裕,但一次搬迁要处理的有效块更多;阈值收紧,搬迁轻量,却频繁触发。工程上的做法是估一个平均搬迁量,乘上单位时间内的触发次数,与控制器的写入带宽对照,选一个两边都留余量的点。这个点不是常数,随块的有效比例漂移,定期用诊断数据回看一次比拍脑袋定死更可靠。

5. 驱动只认 job

第五层往下的驱动不再回答"数据是什么",只回答"这项操作做完了没有"。这个转变是整条链路异步化的起点。

5.1 为什么接口不能是一个阻塞函数

EEPROM 单次写需要几毫秒,Flash 单次擦除需要几十毫秒。在这段时间里,若调用方原地忙等,整车的任务调度会被一起拖住,看门狗可能先动手复位。
#mermaid-svg-QX2XH77Xn0ZY55Uj{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-QX2XH77Xn0ZY55Uj .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-QX2XH77Xn0ZY55Uj .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-QX2XH77Xn0ZY55Uj .error-icon{fill:#552222;}#mermaid-svg-QX2XH77Xn0ZY55Uj .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-QX2XH77Xn0ZY55Uj .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-QX2XH77Xn0ZY55Uj .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-QX2XH77Xn0ZY55Uj .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-QX2XH77Xn0ZY55Uj .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-QX2XH77Xn0ZY55Uj .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-QX2XH77Xn0ZY55Uj .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-QX2XH77Xn0ZY55Uj .marker{fill:#333333;stroke:#333333;}#mermaid-svg-QX2XH77Xn0ZY55Uj .marker.cross{stroke:#333333;}#mermaid-svg-QX2XH77Xn0ZY55Uj svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-QX2XH77Xn0ZY55Uj p{margin:0;}#mermaid-svg-QX2XH77Xn0ZY55Uj .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-QX2XH77Xn0ZY55Uj .cluster-label text{fill:#333;}#mermaid-svg-QX2XH77Xn0ZY55Uj .cluster-label span{color:#333;}#mermaid-svg-QX2XH77Xn0ZY55Uj .cluster-label span p{background-color:transparent;}#mermaid-svg-QX2XH77Xn0ZY55Uj .label text,#mermaid-svg-QX2XH77Xn0ZY55Uj span{fill:#333;color:#333;}#mermaid-svg-QX2XH77Xn0ZY55Uj .node rect,#mermaid-svg-QX2XH77Xn0ZY55Uj .node circle,#mermaid-svg-QX2XH77Xn0ZY55Uj .node ellipse,#mermaid-svg-QX2XH77Xn0ZY55Uj .node polygon,#mermaid-svg-QX2XH77Xn0ZY55Uj .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-QX2XH77Xn0ZY55Uj .rough-node .label text,#mermaid-svg-QX2XH77Xn0ZY55Uj .node .label text,#mermaid-svg-QX2XH77Xn0ZY55Uj .image-shape .label,#mermaid-svg-QX2XH77Xn0ZY55Uj .icon-shape .label{text-anchor:middle;}#mermaid-svg-QX2XH77Xn0ZY55Uj .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-QX2XH77Xn0ZY55Uj .rough-node .label,#mermaid-svg-QX2XH77Xn0ZY55Uj .node .label,#mermaid-svg-QX2XH77Xn0ZY55Uj .image-shape .label,#mermaid-svg-QX2XH77Xn0ZY55Uj .icon-shape .label{text-align:center;}#mermaid-svg-QX2XH77Xn0ZY55Uj .node.clickable{cursor:pointer;}#mermaid-svg-QX2XH77Xn0ZY55Uj .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-QX2XH77Xn0ZY55Uj .arrowheadPath{fill:#333333;}#mermaid-svg-QX2XH77Xn0ZY55Uj .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-QX2XH77Xn0ZY55Uj .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-QX2XH77Xn0ZY55Uj .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QX2XH77Xn0ZY55Uj .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-QX2XH77Xn0ZY55Uj .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QX2XH77Xn0ZY55Uj .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-QX2XH77Xn0ZY55Uj .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-QX2XH77Xn0ZY55Uj .cluster text{fill:#333;}#mermaid-svg-QX2XH77Xn0ZY55Uj .cluster span{color:#333;}#mermaid-svg-QX2XH77Xn0ZY55Uj div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-QX2XH77Xn0ZY55Uj .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-QX2XH77Xn0ZY55Uj rect.text{fill:none;stroke-width:0;}#mermaid-svg-QX2XH77Xn0ZY55Uj .icon-shape,#mermaid-svg-QX2XH77Xn0ZY55Uj .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QX2XH77Xn0ZY55Uj .icon-shape p,#mermaid-svg-QX2XH77Xn0ZY55Uj .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-QX2XH77Xn0ZY55Uj .icon-shape .label rect,#mermaid-svg-QX2XH77Xn0ZY55Uj .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QX2XH77Xn0ZY55Uj .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-QX2XH77Xn0ZY55Uj .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-QX2XH77Xn0ZY55Uj :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 未就绪
就绪
上层写请求
驱动接受 job
配置总线或控制器
启动硬件动作
立即返回挂起
主循环或周期任务轮询
结果就绪
读取结果并回调上层

图右侧那个轮询环是这套设计的核心:等待发生在调用方的周期任务里,不发生在驱动内部。驱动只负责启动与登记,不负责占用 CPU 去等硬件。

同一时刻只处理一个 job 是硬性约束,《EEPROM 驱动需求》5.1.1.1.2.10 单列一条把它定死。这意味着排队与互斥不能指望驱动代劳,要么上层串行化请求,要么由集成代码在调用点加锁。

5.2 job 的结果怎么表达

一次 job 从提交到结束要经过几个稳定状态,结果码另有一套取值,两者不能混用。
#mermaid-svg-x4bG62nmcTRHVFXN{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-x4bG62nmcTRHVFXN .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-x4bG62nmcTRHVFXN .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-x4bG62nmcTRHVFXN .error-icon{fill:#552222;}#mermaid-svg-x4bG62nmcTRHVFXN .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-x4bG62nmcTRHVFXN .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-x4bG62nmcTRHVFXN .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-x4bG62nmcTRHVFXN .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-x4bG62nmcTRHVFXN .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-x4bG62nmcTRHVFXN .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-x4bG62nmcTRHVFXN .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-x4bG62nmcTRHVFXN .marker{fill:#333333;stroke:#333333;}#mermaid-svg-x4bG62nmcTRHVFXN .marker.cross{stroke:#333333;}#mermaid-svg-x4bG62nmcTRHVFXN svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-x4bG62nmcTRHVFXN p{margin:0;}#mermaid-svg-x4bG62nmcTRHVFXN defs #statediagram-barbEnd{fill:#333333;stroke:#333333;}#mermaid-svg-x4bG62nmcTRHVFXN g.stateGroup text{fill:#9370DB;stroke:none;font-size:10px;}#mermaid-svg-x4bG62nmcTRHVFXN g.stateGroup text{fill:#333;stroke:none;font-size:10px;}#mermaid-svg-x4bG62nmcTRHVFXN g.stateGroup .state-title{font-weight:bolder;fill:#131300;}#mermaid-svg-x4bG62nmcTRHVFXN g.stateGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-x4bG62nmcTRHVFXN g.stateGroup line{stroke:#333333;stroke-width:1;}#mermaid-svg-x4bG62nmcTRHVFXN .transition{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-x4bG62nmcTRHVFXN .stateGroup .composit{fill:white;border-bottom:1px;}#mermaid-svg-x4bG62nmcTRHVFXN .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px;}#mermaid-svg-x4bG62nmcTRHVFXN .state-note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-x4bG62nmcTRHVFXN .state-note text{fill:black;stroke:none;font-size:10px;}#mermaid-svg-x4bG62nmcTRHVFXN .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-x4bG62nmcTRHVFXN .edgeLabel .label rect{fill:#ECECFF;opacity:0.5;}#mermaid-svg-x4bG62nmcTRHVFXN .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-x4bG62nmcTRHVFXN .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-x4bG62nmcTRHVFXN .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-x4bG62nmcTRHVFXN .edgeLabel .label text{fill:#333;}#mermaid-svg-x4bG62nmcTRHVFXN .label div .edgeLabel{color:#333;}#mermaid-svg-x4bG62nmcTRHVFXN .stateLabel text{fill:#131300;font-size:10px;font-weight:bold;}#mermaid-svg-x4bG62nmcTRHVFXN .node circle.state-start{fill:#333333;stroke:#333333;}#mermaid-svg-x4bG62nmcTRHVFXN .node .fork-join{fill:#333333;stroke:#333333;}#mermaid-svg-x4bG62nmcTRHVFXN .node circle.state-end{fill:#9370DB;stroke:white;stroke-width:1.5;}#mermaid-svg-x4bG62nmcTRHVFXN .end-state-inner{fill:white;stroke-width:1.5;}#mermaid-svg-x4bG62nmcTRHVFXN .node rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-x4bG62nmcTRHVFXN .node polygon{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-x4bG62nmcTRHVFXN #statediagram-barbEnd{fill:#333333;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-x4bG62nmcTRHVFXN .cluster-label,#mermaid-svg-x4bG62nmcTRHVFXN .nodeLabel{color:#131300;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-cluster rect.outer{rx:5px;ry:5px;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-state .divider{stroke:#9370DB;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-state .title-state{rx:5px;ry:5px;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-cluster.statediagram-cluster .inner{fill:white;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-cluster.statediagram-cluster-alt .inner{fill:#f0f0f0;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-cluster .inner{rx:0;ry:0;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-state rect.basic{rx:5px;ry:5px;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#f0f0f0;}#mermaid-svg-x4bG62nmcTRHVFXN .note-edge{stroke-dasharray:5;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-note text{fill:black;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram-note .nodeLabel{color:black;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagram .edgeLabel{color:red;}#mermaid-svg-x4bG62nmcTRHVFXN #dependencyStart,#mermaid-svg-x4bG62nmcTRHVFXN #dependencyEnd{fill:#333333;stroke:#333333;stroke-width:1;}#mermaid-svg-x4bG62nmcTRHVFXN .statediagramTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-x4bG62nmcTRHVFXN :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} "提交 job"
"硬件动作完成"
"上层取消"
"结果被读取"
空闲
处理中
已取消

结果码的语义划分决定了调用方要怎么处理,下表是同一份语义在四个环节上的对应关系。

结果码 含义 上层该做什么 典型误用
进行中 硬件尚未结束 下个周期继续轮询 当成失败重发,导致双重写
成功 本次 job 正常完成 推进到下一步 不看结果直接当作成功
失败 硬件报错或校验不过 记录故障,按策略重试 无限重试拖死总线
已取消 被上层主动中止 回滚业务状态 当成成功,数据其实没写

实践里最容易被忽略的是"已取消"。取消只保证硬件不再继续,不保证介质没有被部分改写,回滚责任在上层。

5.3 总线与供电这些共享资源

存储器驱动不是孤立运行的,它要和别的驱动抢总线、抢供电模式。谁先谁后属于集成阶段的调度设计。

共享资源 冲突表现 常见处理
SPI 总线 两个从设备同时被选中,读到错数据 由 SPI 驱动串行化访问,同一时刻只选一个
Flash 控制器 代码正在同一块 Flash 上执行,取指被阻塞 把擦写代码放到 RAM 执行,或避开同扇区
供电模式 写操作未完成就断电,数据半写 写完再允许休眠,或接受丢失并依赖旧版本
中断上下文 在中断里提交 job 并轮询,导致长时间占用 只在任务上下文提交,中断里只置标志

共享资源的管理方式决定了一条隐性约束:存储器操作的响应时间不只由存储器决定,还由总线上其它使用者决定。集成阶段若把存储器写在通信任务之后,任何一轮总线拥塞都会抬高写请求的完成时间,这个时间必须计入前一层配的超时值。

6. 外部器件:SPI 后面的存储器

外部 EEPROM 与外部 Flash 不会直接连到 MCU 的存储控制器上,它们挂在串行总线上,走的是通信驱动这条路。

6.1 外部器件为什么必须过 SPI

同一个 EEPROM 驱动可以同时服务片内与片外器件,差别只在下面挂的是内部总线访问还是 SPI 传输。
#mermaid-svg-rPuWlhzIIxYEF1Wm{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-rPuWlhzIIxYEF1Wm .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-rPuWlhzIIxYEF1Wm .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-rPuWlhzIIxYEF1Wm .error-icon{fill:#552222;}#mermaid-svg-rPuWlhzIIxYEF1Wm .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-rPuWlhzIIxYEF1Wm .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-rPuWlhzIIxYEF1Wm .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-rPuWlhzIIxYEF1Wm .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-rPuWlhzIIxYEF1Wm .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-rPuWlhzIIxYEF1Wm .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-rPuWlhzIIxYEF1Wm .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-rPuWlhzIIxYEF1Wm .marker{fill:#333333;stroke:#333333;}#mermaid-svg-rPuWlhzIIxYEF1Wm .marker.cross{stroke:#333333;}#mermaid-svg-rPuWlhzIIxYEF1Wm svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-rPuWlhzIIxYEF1Wm p{margin:0;}#mermaid-svg-rPuWlhzIIxYEF1Wm .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-rPuWlhzIIxYEF1Wm .cluster-label text{fill:#333;}#mermaid-svg-rPuWlhzIIxYEF1Wm .cluster-label span{color:#333;}#mermaid-svg-rPuWlhzIIxYEF1Wm .cluster-label span p{background-color:transparent;}#mermaid-svg-rPuWlhzIIxYEF1Wm .label text,#mermaid-svg-rPuWlhzIIxYEF1Wm span{fill:#333;color:#333;}#mermaid-svg-rPuWlhzIIxYEF1Wm .node rect,#mermaid-svg-rPuWlhzIIxYEF1Wm .node circle,#mermaid-svg-rPuWlhzIIxYEF1Wm .node ellipse,#mermaid-svg-rPuWlhzIIxYEF1Wm .node polygon,#mermaid-svg-rPuWlhzIIxYEF1Wm .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-rPuWlhzIIxYEF1Wm .rough-node .label text,#mermaid-svg-rPuWlhzIIxYEF1Wm .node .label text,#mermaid-svg-rPuWlhzIIxYEF1Wm .image-shape .label,#mermaid-svg-rPuWlhzIIxYEF1Wm .icon-shape .label{text-anchor:middle;}#mermaid-svg-rPuWlhzIIxYEF1Wm .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-rPuWlhzIIxYEF1Wm .rough-node .label,#mermaid-svg-rPuWlhzIIxYEF1Wm .node .label,#mermaid-svg-rPuWlhzIIxYEF1Wm .image-shape .label,#mermaid-svg-rPuWlhzIIxYEF1Wm .icon-shape .label{text-align:center;}#mermaid-svg-rPuWlhzIIxYEF1Wm .node.clickable{cursor:pointer;}#mermaid-svg-rPuWlhzIIxYEF1Wm .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-rPuWlhzIIxYEF1Wm .arrowheadPath{fill:#333333;}#mermaid-svg-rPuWlhzIIxYEF1Wm .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-rPuWlhzIIxYEF1Wm .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-rPuWlhzIIxYEF1Wm .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-rPuWlhzIIxYEF1Wm .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-rPuWlhzIIxYEF1Wm .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-rPuWlhzIIxYEF1Wm .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-rPuWlhzIIxYEF1Wm .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-rPuWlhzIIxYEF1Wm .cluster text{fill:#333;}#mermaid-svg-rPuWlhzIIxYEF1Wm .cluster span{color:#333;}#mermaid-svg-rPuWlhzIIxYEF1Wm div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-rPuWlhzIIxYEF1Wm .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-rPuWlhzIIxYEF1Wm rect.text{fill:none;stroke-width:0;}#mermaid-svg-rPuWlhzIIxYEF1Wm .icon-shape,#mermaid-svg-rPuWlhzIIxYEF1Wm .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-rPuWlhzIIxYEF1Wm .icon-shape p,#mermaid-svg-rPuWlhzIIxYEF1Wm .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-rPuWlhzIIxYEF1Wm .icon-shape .label rect,#mermaid-svg-rPuWlhzIIxYEF1Wm .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-rPuWlhzIIxYEF1Wm .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-rPuWlhzIIxYEF1Wm .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-rPuWlhzIIxYEF1Wm :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 片内
板外
Eep:EEPROM 驱动
器件位置
内部总线直接访问
SPI Handler:选从设备、发命令、收数据
外部 EEPROM
外部 Flash
片内 EEPROM

图里 SPI Handler 承担的是通道角色:它不知道传的是 EEPROM 命令还是别的协议,只负责在片选有效期间把字节搬过去。器件语义留在 Eep 里,通道语义留在 SPI 里。

6.2 一次外部读的时序

外部读的比例通常远高于写,它的响应时间直接决定上层读块的时延。
"外部 EEPROM" "SPI Handler" "Eep" "外部 EEPROM" "SPI Handler" "Eep" #mermaid-svg-e63OoImGcUno2QeB{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-e63OoImGcUno2QeB .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-e63OoImGcUno2QeB .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-e63OoImGcUno2QeB .error-icon{fill:#552222;}#mermaid-svg-e63OoImGcUno2QeB .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-e63OoImGcUno2QeB .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-e63OoImGcUno2QeB .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-e63OoImGcUno2QeB .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-e63OoImGcUno2QeB .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-e63OoImGcUno2QeB .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-e63OoImGcUno2QeB .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-e63OoImGcUno2QeB .marker{fill:#333333;stroke:#333333;}#mermaid-svg-e63OoImGcUno2QeB .marker.cross{stroke:#333333;}#mermaid-svg-e63OoImGcUno2QeB svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-e63OoImGcUno2QeB p{margin:0;}#mermaid-svg-e63OoImGcUno2QeB .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-e63OoImGcUno2QeB text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-e63OoImGcUno2QeB .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-e63OoImGcUno2QeB .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-e63OoImGcUno2QeB .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-e63OoImGcUno2QeB .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-e63OoImGcUno2QeB #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-e63OoImGcUno2QeB .sequenceNumber{fill:white;}#mermaid-svg-e63OoImGcUno2QeB #sequencenumber{fill:#333;}#mermaid-svg-e63OoImGcUno2QeB #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-e63OoImGcUno2QeB .messageText{fill:#333;stroke:none;}#mermaid-svg-e63OoImGcUno2QeB .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-e63OoImGcUno2QeB .labelText,#mermaid-svg-e63OoImGcUno2QeB .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-e63OoImGcUno2QeB .loopText,#mermaid-svg-e63OoImGcUno2QeB .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-e63OoImGcUno2QeB .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-e63OoImGcUno2QeB .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-e63OoImGcUno2QeB .noteText,#mermaid-svg-e63OoImGcUno2QeB .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-e63OoImGcUno2QeB .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-e63OoImGcUno2QeB .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-e63OoImGcUno2QeB .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-e63OoImGcUno2QeB .actorPopupMenu{position:absolute;}#mermaid-svg-e63OoImGcUno2QeB .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-e63OoImGcUno2QeB .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-e63OoImGcUno2QeB .actor-man circle,#mermaid-svg-e63OoImGcUno2QeB line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-e63OoImGcUno2QeB :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} "请求传输:片选从设备 2""发送读命令与地址""返回数据字节""传输完成""校验并按块返回"

注意链路里有两次语义切换:Eep 把块读写翻译成器件命令,Spi 把器件命令翻译成总线时序。每一次切换都要额外几个字节的开销,读小块的效率因此明显低于读大块。

6.3 串行总线的带宽账

把速率代进公式,才能知道一次整页读到底要多久。下表按每字节 8 位、忽略命令开销估算。

SPI 时钟 理论吞吐 读 256 字节 读 4 KB
1 MHz 约 125 KB/s 约 2.0 ms 约 33 ms
5 MHz 约 625 KB/s 约 0.4 ms 约 6.6 ms
10 MHz 约 1.25 MB/s 约 0.2 ms 约 3.3 ms

对照片上 EEPROM 的毫秒级写时间可以看出,低速 SPI 下搬 4 KB 的耗时已经和一次写相当。这就是为什么大块数据不适合放在外部器件上,也是为什么外部器件通常只存小配置。

6.4 与其他从设备的争用

SPI 总线上往往还挂着传感器、收发器,存储器只能排队。排队带来的抖动比平均时延更难处理。

场景 对存储器的影响 缓解方式
同一总线上有周期性传感器 写请求被推迟到传感器空闲 给存储器分配独立通道,或提高总线速率
从设备时钟极性配置不一致 通信失败,读到固定错误值 按器件手册配置模式与位序
长走线与干扰 偶发位错,表现成校验失败 降低速率、加校验、缩短走线

命令开销在短传输里的占比高得刺眼。读 4 字节,命令与地址就要四五个字节,有效载荷不到一半;读 256 字节时这点开销可以忽略。这条差异直接约束外部器件的块大小------块设计得太小,等于每次访问都先交一笔固定税,带宽预算会成倍恶化。

7. 合成:一次写请求走完全程

把五层接起来看,一次写入的真实路径比接口签名长得多,等待点也比想象中多。

7.1 从 NvM 到 Flash 的完整调用栈

下面这张图把前面各层的动作串成一次连续过程,包含冗余块与垃圾回收两条支路。
"Flash" "Fls" "Fee" "MemIf" "NvM" "应用" "Flash" "Fls" "Fee" "MemIf" "NvM" "应用" #mermaid-svg-3ITOoWt4vzUpD74Y{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-3ITOoWt4vzUpD74Y .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-3ITOoWt4vzUpD74Y .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-3ITOoWt4vzUpD74Y .error-icon{fill:#552222;}#mermaid-svg-3ITOoWt4vzUpD74Y .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-3ITOoWt4vzUpD74Y .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-3ITOoWt4vzUpD74Y .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-3ITOoWt4vzUpD74Y .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-3ITOoWt4vzUpD74Y .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-3ITOoWt4vzUpD74Y .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-3ITOoWt4vzUpD74Y .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-3ITOoWt4vzUpD74Y .marker{fill:#333333;stroke:#333333;}#mermaid-svg-3ITOoWt4vzUpD74Y .marker.cross{stroke:#333333;}#mermaid-svg-3ITOoWt4vzUpD74Y svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-3ITOoWt4vzUpD74Y p{margin:0;}#mermaid-svg-3ITOoWt4vzUpD74Y .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-3ITOoWt4vzUpD74Y text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-3ITOoWt4vzUpD74Y .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-3ITOoWt4vzUpD74Y .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-3ITOoWt4vzUpD74Y .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-3ITOoWt4vzUpD74Y .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-3ITOoWt4vzUpD74Y #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-3ITOoWt4vzUpD74Y .sequenceNumber{fill:white;}#mermaid-svg-3ITOoWt4vzUpD74Y #sequencenumber{fill:#333;}#mermaid-svg-3ITOoWt4vzUpD74Y #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-3ITOoWt4vzUpD74Y .messageText{fill:#333;stroke:none;}#mermaid-svg-3ITOoWt4vzUpD74Y .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-3ITOoWt4vzUpD74Y .labelText,#mermaid-svg-3ITOoWt4vzUpD74Y .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-3ITOoWt4vzUpD74Y .loopText,#mermaid-svg-3ITOoWt4vzUpD74Y .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-3ITOoWt4vzUpD74Y .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-3ITOoWt4vzUpD74Y .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-3ITOoWt4vzUpD74Y .noteText,#mermaid-svg-3ITOoWt4vzUpD74Y .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-3ITOoWt4vzUpD74Y .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-3ITOoWt4vzUpD74Y .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-3ITOoWt4vzUpD74Y .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-3ITOoWt4vzUpD74Y .actorPopupMenu{position:absolute;}#mermaid-svg-3ITOoWt4vzUpD74Y .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-3ITOoWt4vzUpD74Y .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-3ITOoWt4vzUpD74Y .actor-man circle,#mermaid-svg-3ITOoWt4vzUpD74Y line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-3ITOoWt4vzUpD74Y :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} "写块请求""取数据、判断是否需冗余""提交块写""转发到设备 0""检查扇区空闲空间""提交编程 job""整页编程""完成""结果""空间不足则启动回收""块写结果""结果""冗余块再写一次""返回"

图里最后一步的冗余写常被忽视:块级保护通常是两份副本,实际介质写入次数是调用次数的两倍,寿命预算要按两倍算。

块级保护在上层是个配置项,块管理类型分原生、冗余、数据集三类,出处见《NVRAM 管理器规范》7.1.4.5。对存储器这一层来说,它并不知道自己被写了两次,只看到两次独立的块写请求。

7.2 各层的时间贡献

把典型量级摊在表上,能看出时延究竟花在哪一层。

环节 典型耗时 是否可优化 优化手段
NvM 取数据与判冗余 微秒级 可 减少块数、避免大块
MemIf 转发 微秒级以下 不可 无,本身是查表
Fee 选页与组包 十微秒级 部分可 减少块数量、提高缓存命中
Flash 页编程 毫秒级 不可 无,属于器件物理时间
扇区擦除 数十毫秒 可规避 提前回收,避开关键写入窗口
外部器件 SPI 传输 亚毫秒到数十毫秒 可 提高总线速率、加大传输长度

表里只有擦除与编程两项不可压缩,其余都属于软件开销。这也解释了为什么调优时先减块数、再提高总线速率,最后才考虑换器件。

还有一笔量常被漏算:调用次数与冗余份数的乘积。一次业务保存触发两份块写,若其中一份还撞上回收,实际耗时可能是一次正常写的十几倍。给写操作设超时值时按最坏路径算,而不是按典型路径算,可以避免大量偶发超时;按典型值算的做法在实验室永远跑不出问题,到了整车上却天天报故障。

7.3 阻塞点:谁在等谁

链路是一条串行的接力,任何一环等待都会向后传导。
#mermaid-svg-2xmI0MicuswPZuGT{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-2xmI0MicuswPZuGT .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-2xmI0MicuswPZuGT .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-2xmI0MicuswPZuGT .error-icon{fill:#552222;}#mermaid-svg-2xmI0MicuswPZuGT .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2xmI0MicuswPZuGT .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-2xmI0MicuswPZuGT .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2xmI0MicuswPZuGT .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2xmI0MicuswPZuGT .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-2xmI0MicuswPZuGT .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2xmI0MicuswPZuGT .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2xmI0MicuswPZuGT .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2xmI0MicuswPZuGT .marker.cross{stroke:#333333;}#mermaid-svg-2xmI0MicuswPZuGT svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2xmI0MicuswPZuGT p{margin:0;}#mermaid-svg-2xmI0MicuswPZuGT .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-2xmI0MicuswPZuGT .cluster-label text{fill:#333;}#mermaid-svg-2xmI0MicuswPZuGT .cluster-label span{color:#333;}#mermaid-svg-2xmI0MicuswPZuGT .cluster-label span p{background-color:transparent;}#mermaid-svg-2xmI0MicuswPZuGT .label text,#mermaid-svg-2xmI0MicuswPZuGT span{fill:#333;color:#333;}#mermaid-svg-2xmI0MicuswPZuGT .node rect,#mermaid-svg-2xmI0MicuswPZuGT .node circle,#mermaid-svg-2xmI0MicuswPZuGT .node ellipse,#mermaid-svg-2xmI0MicuswPZuGT .node polygon,#mermaid-svg-2xmI0MicuswPZuGT .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2xmI0MicuswPZuGT .rough-node .label text,#mermaid-svg-2xmI0MicuswPZuGT .node .label text,#mermaid-svg-2xmI0MicuswPZuGT .image-shape .label,#mermaid-svg-2xmI0MicuswPZuGT .icon-shape .label{text-anchor:middle;}#mermaid-svg-2xmI0MicuswPZuGT .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-2xmI0MicuswPZuGT .rough-node .label,#mermaid-svg-2xmI0MicuswPZuGT .node .label,#mermaid-svg-2xmI0MicuswPZuGT .image-shape .label,#mermaid-svg-2xmI0MicuswPZuGT .icon-shape .label{text-align:center;}#mermaid-svg-2xmI0MicuswPZuGT .node.clickable{cursor:pointer;}#mermaid-svg-2xmI0MicuswPZuGT .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-2xmI0MicuswPZuGT .arrowheadPath{fill:#333333;}#mermaid-svg-2xmI0MicuswPZuGT .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-2xmI0MicuswPZuGT .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-2xmI0MicuswPZuGT .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2xmI0MicuswPZuGT .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-2xmI0MicuswPZuGT .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2xmI0MicuswPZuGT .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-2xmI0MicuswPZuGT .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-2xmI0MicuswPZuGT .cluster text{fill:#333;}#mermaid-svg-2xmI0MicuswPZuGT .cluster span{color:#333;}#mermaid-svg-2xmI0MicuswPZuGT div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-2xmI0MicuswPZuGT .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-2xmI0MicuswPZuGT rect.text{fill:none;stroke-width:0;}#mermaid-svg-2xmI0MicuswPZuGT .icon-shape,#mermaid-svg-2xmI0MicuswPZuGT .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2xmI0MicuswPZuGT .icon-shape p,#mermaid-svg-2xmI0MicuswPZuGT .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-2xmI0MicuswPZuGT .icon-shape .label rect,#mermaid-svg-2xmI0MicuswPZuGT .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2xmI0MicuswPZuGT .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-2xmI0MicuswPZuGT .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-2xmI0MicuswPZuGT :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 应用发起写
NvM 等结果
Fee 等空闲空间
Fls 等硬件完成
Flash 等电压稳定
回收时等擦除

从图上看,等待的源头只有两个:硬件的物理时间,与扇区的可用空间。前者只能通过减少写入量来摊薄,后者取决于回收策略留了多少余量。所谓"写不进去"的现场故障,多数不是驱动坏了,而是回收没来得及腾出位置。

8. 代价与边界

抽象把差异收进一层,同时也把成本集中到这一层。选 Ea 还是选 Fee,本质上是在四种资源之间做交易。

8.1 Ea 与 Fee 的取舍矩阵

两者对上层是同一套块语义,落到资源账上完全不同。

维度 Ea:真实 EEPROM Fee:Flash 仿真
RAM 占用 几乎为零,无缓冲 需要页缓冲,常为扇区或页大小
单次写时延 毫秒级,与数据量成正比 页编程毫秒级,回收时数十毫秒
擦写寿命 百万次量级 万次量级,靠均衡策略摊薄
容量成本 每字节贵,容量通常很小 按扇区分配,容量大且便宜
掉电风险面 单块数据 单块数据,回收时波及扇区
实现复杂度 低,只做地址换算 高,需回收、扫描、一致性设计

这张表解释了一个常见配置:里程、计数器这类小数据放在片上 EEPROM,标定参数、故障快照这类大块数据放在 Flash 仿真区。前者买的是寿命与确定性,后者买的是容量与成本。

8.2 三条硬约束

无论怎么配置,有三条约束无法绕开,只能在设计阶段接受。

约束 表现 设计上的应对
RAM Fee 的缓冲与回收暂存要挤在有限的 RAM 里 控制块数量与扇区大小,避免缓冲面积失控
寿命 同一扇区被反复擦写,最先到达寿命上限 轮转回收,热块分散,必要时把高频数据移到 EEPROM
时延 擦除与编程时间不可压缩,写入窗口要预留 把写操作放在非关键时序,提前触发回收

三条约束互相牵制:为了省 RAM 缩小扇区,回收更频繁,寿命与时延都变差。集成阶段的配置调优,实际是在这三个方向上找平衡点。

8.3 抽象没有解决的事

分层把"怎么访问介质"统一了,但没有也不能统一"数据该不该这么写"。

仍在应用或上层的问题 抽象层为何不管 处理位置
写入频率是否合理 抽象层无法改变业务的更新节奏 应用设计,配合 NvM 的写触发策略
数据完整性等级 块语义不包含校验强度要求 NvM 的冗余与 CRC 配置
掉电后业务如何恢复 抽象层只保证单块可判有效,不保证业务一致 应用上电自检与默认值回填
多块之间的一致性 块是独立单位,跨块事务不在抽象范围内 应用侧事务设计或版本号机制

这四条是现场问题的高发区。数据写坏了往往不是驱动出错,而是业务把不该频繁变的数据每秒写一次,寿命提前耗尽。

反方向也有浪费:把超出抽象层职责的诉求硬塞进去。校验强度、跨块事务、掉电后的业务回填,一旦要求 Fee 或 Ea 感知业务语义,这两个模块就再也无法换介质。抽象层的可移植性来自无知------它不知道数据是什么,所以才能对任何数据一视同仁。

9. 配置期要定的三个数

分层定完,剩下的事全压在配置表里。块怎么分、扇区留几个、缓冲给多大,这三个数决定后续所有账。

9.1 块按写频次划分

按功能分块是最省事的做法,也是最容易把寿命写穿的做法。一个 200 字节的模块配置和每秒刷新的计数器放在同一扇区,前者的寿命被后者拖着走。
#mermaid-svg-h2x3EHk8PwwJMCNn{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-h2x3EHk8PwwJMCNn .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-h2x3EHk8PwwJMCNn .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-h2x3EHk8PwwJMCNn .error-icon{fill:#552222;}#mermaid-svg-h2x3EHk8PwwJMCNn .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-h2x3EHk8PwwJMCNn .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-h2x3EHk8PwwJMCNn .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-h2x3EHk8PwwJMCNn .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-h2x3EHk8PwwJMCNn .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-h2x3EHk8PwwJMCNn .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-h2x3EHk8PwwJMCNn .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-h2x3EHk8PwwJMCNn .marker{fill:#333333;stroke:#333333;}#mermaid-svg-h2x3EHk8PwwJMCNn .marker.cross{stroke:#333333;}#mermaid-svg-h2x3EHk8PwwJMCNn svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-h2x3EHk8PwwJMCNn p{margin:0;}#mermaid-svg-h2x3EHk8PwwJMCNn .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-h2x3EHk8PwwJMCNn .cluster-label text{fill:#333;}#mermaid-svg-h2x3EHk8PwwJMCNn .cluster-label span{color:#333;}#mermaid-svg-h2x3EHk8PwwJMCNn .cluster-label span p{background-color:transparent;}#mermaid-svg-h2x3EHk8PwwJMCNn .label text,#mermaid-svg-h2x3EHk8PwwJMCNn span{fill:#333;color:#333;}#mermaid-svg-h2x3EHk8PwwJMCNn .node rect,#mermaid-svg-h2x3EHk8PwwJMCNn .node circle,#mermaid-svg-h2x3EHk8PwwJMCNn .node ellipse,#mermaid-svg-h2x3EHk8PwwJMCNn .node polygon,#mermaid-svg-h2x3EHk8PwwJMCNn .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-h2x3EHk8PwwJMCNn .rough-node .label text,#mermaid-svg-h2x3EHk8PwwJMCNn .node .label text,#mermaid-svg-h2x3EHk8PwwJMCNn .image-shape .label,#mermaid-svg-h2x3EHk8PwwJMCNn .icon-shape .label{text-anchor:middle;}#mermaid-svg-h2x3EHk8PwwJMCNn .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-h2x3EHk8PwwJMCNn .rough-node .label,#mermaid-svg-h2x3EHk8PwwJMCNn .node .label,#mermaid-svg-h2x3EHk8PwwJMCNn .image-shape .label,#mermaid-svg-h2x3EHk8PwwJMCNn .icon-shape .label{text-align:center;}#mermaid-svg-h2x3EHk8PwwJMCNn .node.clickable{cursor:pointer;}#mermaid-svg-h2x3EHk8PwwJMCNn .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-h2x3EHk8PwwJMCNn .arrowheadPath{fill:#333333;}#mermaid-svg-h2x3EHk8PwwJMCNn .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-h2x3EHk8PwwJMCNn .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-h2x3EHk8PwwJMCNn .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-h2x3EHk8PwwJMCNn .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-h2x3EHk8PwwJMCNn .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-h2x3EHk8PwwJMCNn .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-h2x3EHk8PwwJMCNn .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-h2x3EHk8PwwJMCNn .cluster text{fill:#333;}#mermaid-svg-h2x3EHk8PwwJMCNn .cluster span{color:#333;}#mermaid-svg-h2x3EHk8PwwJMCNn div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-h2x3EHk8PwwJMCNn .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-h2x3EHk8PwwJMCNn rect.text{fill:none;stroke-width:0;}#mermaid-svg-h2x3EHk8PwwJMCNn .icon-shape,#mermaid-svg-h2x3EHk8PwwJMCNn .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-h2x3EHk8PwwJMCNn .icon-shape p,#mermaid-svg-h2x3EHk8PwwJMCNn .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-h2x3EHk8PwwJMCNn .icon-shape .label rect,#mermaid-svg-h2x3EHk8PwwJMCNn .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-h2x3EHk8PwwJMCNn .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-h2x3EHk8PwwJMCNn .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-h2x3EHk8PwwJMCNn :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 全部块
高频写:秒级
中频写:事件触发
低频写:下电或诊断
独立扇区,优先 EEPROM
Flash 仿真区,容量优先
可与中频共扇区

分区的判据只有一个:同一扇区内的块,写入频率应尽量接近。频率差一个数量级,混放就没有意义。

9.2 扇区数量怎么估

容量不是唯一变量,回收需要余量,写入放大还要吃掉一部分。

项 估算方式 说明
有效数据量 块大小乘以块数量 逻辑上必须存下的部分
版本冗余 按每块保留两到三份实例估 追加写模式下同一块会存在多个实例
回收余量 额外留一到两个空扇区 没有空扇区就无法搬迁,回收卡死
块头开销 每实例几十字节 实例越多,头部开销占比越高

四项相加再向上取整到扇区大小的整数倍,就是 Flash 分区的最小需求。只按有效数据量申请分区,运行一段时间后必然撞上"回收失败"。

9.3 块大小与缓冲联动

Fee 的缓冲通常按页或扇区对齐,块大小越过一页时,一次写要拆成多次编程。

块大小 缓冲需求 写放大 适用数据
小于一页 一页 接近一倍 计数器、状态字
等于一页 一页 最小 小配置块
数页 按页循环搬运 需对齐填充,偏高 标定参数
整扇区 与扇区同级 受回收策略影响大 故障快照、大表

这张表给出一个经验:块大小尽量取页的整数倍,别取页加几十字节。多出来的那几十字节会把每次写变成两次编程,寿命与时间双输。

反方向的极端也有代价:块取到整扇区那么大,任何一次改写都要搬走整片扇区的空间,回收频率随之上来。块大小落在页与扇区之间的中档更稳,容量利用率与回收成本都能接受;真要存整扇区级别的数据,就要接受它与同扇区内所有块共享命运。

10. 两条错误路径的推演

配置错了不会立刻报错,会在运行几千小时后以两种面貌出现。

10.1 高频写把寿命提前耗尽

把每秒刷新一次的运行小时数直接写进 Flash 扇区,账很快就能算清。
"同一扇区" "Fee" "应用" "同一扇区" "Fee" "应用" #mermaid-svg-IpRoc3VwpLhhkP9R{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-IpRoc3VwpLhhkP9R .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-IpRoc3VwpLhhkP9R .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-IpRoc3VwpLhhkP9R .error-icon{fill:#552222;}#mermaid-svg-IpRoc3VwpLhhkP9R .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-IpRoc3VwpLhhkP9R .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-IpRoc3VwpLhhkP9R .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-IpRoc3VwpLhhkP9R .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-IpRoc3VwpLhhkP9R .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-IpRoc3VwpLhhkP9R .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-IpRoc3VwpLhhkP9R .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-IpRoc3VwpLhhkP9R .marker{fill:#333333;stroke:#333333;}#mermaid-svg-IpRoc3VwpLhhkP9R .marker.cross{stroke:#333333;}#mermaid-svg-IpRoc3VwpLhhkP9R svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-IpRoc3VwpLhhkP9R p{margin:0;}#mermaid-svg-IpRoc3VwpLhhkP9R .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-IpRoc3VwpLhhkP9R text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-IpRoc3VwpLhhkP9R .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-IpRoc3VwpLhhkP9R .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-IpRoc3VwpLhhkP9R .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-IpRoc3VwpLhhkP9R .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-IpRoc3VwpLhhkP9R #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-IpRoc3VwpLhhkP9R .sequenceNumber{fill:white;}#mermaid-svg-IpRoc3VwpLhhkP9R #sequencenumber{fill:#333;}#mermaid-svg-IpRoc3VwpLhhkP9R #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-IpRoc3VwpLhhkP9R .messageText{fill:#333;stroke:none;}#mermaid-svg-IpRoc3VwpLhhkP9R .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-IpRoc3VwpLhhkP9R .labelText,#mermaid-svg-IpRoc3VwpLhhkP9R .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-IpRoc3VwpLhhkP9R .loopText,#mermaid-svg-IpRoc3VwpLhhkP9R .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-IpRoc3VwpLhhkP9R .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-IpRoc3VwpLhhkP9R .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-IpRoc3VwpLhhkP9R .noteText,#mermaid-svg-IpRoc3VwpLhhkP9R .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-IpRoc3VwpLhhkP9R .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-IpRoc3VwpLhhkP9R .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-IpRoc3VwpLhhkP9R .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-IpRoc3VwpLhhkP9R .actorPopupMenu{position:absolute;}#mermaid-svg-IpRoc3VwpLhhkP9R .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-IpRoc3VwpLhhkP9R .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-IpRoc3VwpLhhkP9R .actor-man circle,#mermaid-svg-IpRoc3VwpLhhkP9R line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-IpRoc3VwpLhhkP9R :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} loop每秒一次 "实例填满扇区后触发回收,整扇区擦除" "十万次寿命按每天八万次消耗,不足两天" "写计数器块""追加一个实例""擦除次数快速累加"

这条路径的隐蔽之处在于前几周一切正常,现象只在某天突然变成"数据写不进去"。此时补救已来不及,只能改分区或把数据迁到 EEPROM。

10.2 回收与写请求撞车

第二个故障更急:写请求恰好落在回收过程中,等不到擦除完成就超时。
"Fls" "Fee" "应用" "Fls" "Fee" "应用" #mermaid-svg-SaT6hyjWv5rabLJD{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-SaT6hyjWv5rabLJD .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-SaT6hyjWv5rabLJD .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-SaT6hyjWv5rabLJD .error-icon{fill:#552222;}#mermaid-svg-SaT6hyjWv5rabLJD .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-SaT6hyjWv5rabLJD .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-SaT6hyjWv5rabLJD .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-SaT6hyjWv5rabLJD .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-SaT6hyjWv5rabLJD .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-SaT6hyjWv5rabLJD .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-SaT6hyjWv5rabLJD .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-SaT6hyjWv5rabLJD .marker{fill:#333333;stroke:#333333;}#mermaid-svg-SaT6hyjWv5rabLJD .marker.cross{stroke:#333333;}#mermaid-svg-SaT6hyjWv5rabLJD svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-SaT6hyjWv5rabLJD p{margin:0;}#mermaid-svg-SaT6hyjWv5rabLJD .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-SaT6hyjWv5rabLJD text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-SaT6hyjWv5rabLJD .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-SaT6hyjWv5rabLJD .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-SaT6hyjWv5rabLJD .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-SaT6hyjWv5rabLJD .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-SaT6hyjWv5rabLJD #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-SaT6hyjWv5rabLJD .sequenceNumber{fill:white;}#mermaid-svg-SaT6hyjWv5rabLJD #sequencenumber{fill:#333;}#mermaid-svg-SaT6hyjWv5rabLJD #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-SaT6hyjWv5rabLJD .messageText{fill:#333;stroke:none;}#mermaid-svg-SaT6hyjWv5rabLJD .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-SaT6hyjWv5rabLJD .labelText,#mermaid-svg-SaT6hyjWv5rabLJD .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-SaT6hyjWv5rabLJD .loopText,#mermaid-svg-SaT6hyjWv5rabLJD .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-SaT6hyjWv5rabLJD .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-SaT6hyjWv5rabLJD .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-SaT6hyjWv5rabLJD .noteText,#mermaid-svg-SaT6hyjWv5rabLJD .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-SaT6hyjWv5rabLJD .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-SaT6hyjWv5rabLJD .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-SaT6hyjWv5rabLJD .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-SaT6hyjWv5rabLJD .actorPopupMenu{position:absolute;}#mermaid-svg-SaT6hyjWv5rabLJD .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-SaT6hyjWv5rabLJD .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-SaT6hyjWv5rabLJD .actor-man circle,#mermaid-svg-SaT6hyjWv5rabLJD line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-SaT6hyjWv5rabLJD :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} "写关键块,限定超时""发现空闲空间不足""擦除源扇区""擦除需要数十毫秒""超时时间到,判定失败""写入实际仍在进行"

调用方已经放弃,硬件还在写,下一次上电读到的就是一段状态不一致的数据。避免的办法只有一条:把回收从关键路径上挪开,在空闲期主动触发。

10.3 两条路径的共同点

对比项 寿命耗尽 回收撞车
触发条件 写入频率超预算 空闲空间不足
暴露时间 数月后突然失效 偶发,难复现
根因位置 块划分与介质选择 回收策略与超时设置
可观测信号 擦除计数接近上限 写请求时延出现尖峰

两条路径都指向同一件事:Fee 的时间与寿命不是无限的,它需要预留余量。余量在配置期留,在诊断期看,在现场期就没有补救余地了。

11. 掉电、坏块与寿命监控

存储器的可靠性问题几乎都发生在写操作的窗口里。把窗口逐个看清,才能判断哪些损失可以接受。

11.1 掉电窗口到底有几个

从提交 job 到硬件真正落盘,中间有若干个时刻断电会留下不同后果。
#mermaid-svg-eKtvz71ViC7ta6Cj{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-eKtvz71ViC7ta6Cj .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-eKtvz71ViC7ta6Cj .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-eKtvz71ViC7ta6Cj .error-icon{fill:#552222;}#mermaid-svg-eKtvz71ViC7ta6Cj .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-eKtvz71ViC7ta6Cj .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-eKtvz71ViC7ta6Cj .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-eKtvz71ViC7ta6Cj .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-eKtvz71ViC7ta6Cj .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-eKtvz71ViC7ta6Cj .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-eKtvz71ViC7ta6Cj .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-eKtvz71ViC7ta6Cj .marker{fill:#333333;stroke:#333333;}#mermaid-svg-eKtvz71ViC7ta6Cj .marker.cross{stroke:#333333;}#mermaid-svg-eKtvz71ViC7ta6Cj svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-eKtvz71ViC7ta6Cj p{margin:0;}#mermaid-svg-eKtvz71ViC7ta6Cj .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-eKtvz71ViC7ta6Cj .cluster-label text{fill:#333;}#mermaid-svg-eKtvz71ViC7ta6Cj .cluster-label span{color:#333;}#mermaid-svg-eKtvz71ViC7ta6Cj .cluster-label span p{background-color:transparent;}#mermaid-svg-eKtvz71ViC7ta6Cj .label text,#mermaid-svg-eKtvz71ViC7ta6Cj span{fill:#333;color:#333;}#mermaid-svg-eKtvz71ViC7ta6Cj .node rect,#mermaid-svg-eKtvz71ViC7ta6Cj .node circle,#mermaid-svg-eKtvz71ViC7ta6Cj .node ellipse,#mermaid-svg-eKtvz71ViC7ta6Cj .node polygon,#mermaid-svg-eKtvz71ViC7ta6Cj .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-eKtvz71ViC7ta6Cj .rough-node .label text,#mermaid-svg-eKtvz71ViC7ta6Cj .node .label text,#mermaid-svg-eKtvz71ViC7ta6Cj .image-shape .label,#mermaid-svg-eKtvz71ViC7ta6Cj .icon-shape .label{text-anchor:middle;}#mermaid-svg-eKtvz71ViC7ta6Cj .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-eKtvz71ViC7ta6Cj .rough-node .label,#mermaid-svg-eKtvz71ViC7ta6Cj .node .label,#mermaid-svg-eKtvz71ViC7ta6Cj .image-shape .label,#mermaid-svg-eKtvz71ViC7ta6Cj .icon-shape .label{text-align:center;}#mermaid-svg-eKtvz71ViC7ta6Cj .node.clickable{cursor:pointer;}#mermaid-svg-eKtvz71ViC7ta6Cj .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-eKtvz71ViC7ta6Cj .arrowheadPath{fill:#333333;}#mermaid-svg-eKtvz71ViC7ta6Cj .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-eKtvz71ViC7ta6Cj .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-eKtvz71ViC7ta6Cj .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-eKtvz71ViC7ta6Cj .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-eKtvz71ViC7ta6Cj .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-eKtvz71ViC7ta6Cj .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-eKtvz71ViC7ta6Cj .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-eKtvz71ViC7ta6Cj .cluster text{fill:#333;}#mermaid-svg-eKtvz71ViC7ta6Cj .cluster span{color:#333;}#mermaid-svg-eKtvz71ViC7ta6Cj div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-eKtvz71ViC7ta6Cj .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-eKtvz71ViC7ta6Cj rect.text{fill:none;stroke-width:0;}#mermaid-svg-eKtvz71ViC7ta6Cj .icon-shape,#mermaid-svg-eKtvz71ViC7ta6Cj .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-eKtvz71ViC7ta6Cj .icon-shape p,#mermaid-svg-eKtvz71ViC7ta6Cj .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-eKtvz71ViC7ta6Cj .icon-shape .label rect,#mermaid-svg-eKtvz71ViC7ta6Cj .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-eKtvz71ViC7ta6Cj .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-eKtvz71ViC7ta6Cj .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-eKtvz71ViC7ta6Cj :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 掉电:无影响
掉电:新实例不完整
掉电:块头不完整
掉电:索引丢失
提交 job
组包进缓冲
写数据区
写块头
更新索引
返回成功
旧数据仍有效
重启后扫描恢复

图中虚线标出的四个窗口,损失都被限制在"新数据没写进去、旧数据还在"这个范围内。前提是写入顺序不能颠倒:先数据后块头,块头写完才算这次写入成立。

11.2 坏块与数据损坏怎么被识别

介质损坏不会主动上报,只能靠读时的判据发现。

故障形态 表现 识别手段
编程失败 job 返回失败,单元未达目标值 驱动结果码加读回比对
位翻转 数据能读但内容不同 块级校验字段
扇区擦除不净 写入后读出旧内容 擦除后空白检查
块头损坏 实例被判无效,回落到旧版本 块头完整性校验

四条里只有第一条来自驱动,其余三条都要求上层在读路径上做判断。把校验交给别人,等于把发现故障的机会一起交出去了。

11.3 寿命余量怎么监控

擦除次数不是运行期的必需数据,却是项目后期最有价值的诊断量。
#mermaid-svg-RJYYo8EvsHWilFKC{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-RJYYo8EvsHWilFKC .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-RJYYo8EvsHWilFKC .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-RJYYo8EvsHWilFKC .error-icon{fill:#552222;}#mermaid-svg-RJYYo8EvsHWilFKC .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-RJYYo8EvsHWilFKC .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-RJYYo8EvsHWilFKC .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-RJYYo8EvsHWilFKC .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-RJYYo8EvsHWilFKC .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-RJYYo8EvsHWilFKC .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-RJYYo8EvsHWilFKC .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-RJYYo8EvsHWilFKC .marker{fill:#333333;stroke:#333333;}#mermaid-svg-RJYYo8EvsHWilFKC .marker.cross{stroke:#333333;}#mermaid-svg-RJYYo8EvsHWilFKC svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-RJYYo8EvsHWilFKC p{margin:0;}#mermaid-svg-RJYYo8EvsHWilFKC .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-RJYYo8EvsHWilFKC .cluster-label text{fill:#333;}#mermaid-svg-RJYYo8EvsHWilFKC .cluster-label span{color:#333;}#mermaid-svg-RJYYo8EvsHWilFKC .cluster-label span p{background-color:transparent;}#mermaid-svg-RJYYo8EvsHWilFKC .label text,#mermaid-svg-RJYYo8EvsHWilFKC span{fill:#333;color:#333;}#mermaid-svg-RJYYo8EvsHWilFKC .node rect,#mermaid-svg-RJYYo8EvsHWilFKC .node circle,#mermaid-svg-RJYYo8EvsHWilFKC .node ellipse,#mermaid-svg-RJYYo8EvsHWilFKC .node polygon,#mermaid-svg-RJYYo8EvsHWilFKC .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-RJYYo8EvsHWilFKC .rough-node .label text,#mermaid-svg-RJYYo8EvsHWilFKC .node .label text,#mermaid-svg-RJYYo8EvsHWilFKC .image-shape .label,#mermaid-svg-RJYYo8EvsHWilFKC .icon-shape .label{text-anchor:middle;}#mermaid-svg-RJYYo8EvsHWilFKC .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-RJYYo8EvsHWilFKC .rough-node .label,#mermaid-svg-RJYYo8EvsHWilFKC .node .label,#mermaid-svg-RJYYo8EvsHWilFKC .image-shape .label,#mermaid-svg-RJYYo8EvsHWilFKC .icon-shape .label{text-align:center;}#mermaid-svg-RJYYo8EvsHWilFKC .node.clickable{cursor:pointer;}#mermaid-svg-RJYYo8EvsHWilFKC .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-RJYYo8EvsHWilFKC .arrowheadPath{fill:#333333;}#mermaid-svg-RJYYo8EvsHWilFKC .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-RJYYo8EvsHWilFKC .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-RJYYo8EvsHWilFKC .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RJYYo8EvsHWilFKC .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-RJYYo8EvsHWilFKC .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RJYYo8EvsHWilFKC .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-RJYYo8EvsHWilFKC .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-RJYYo8EvsHWilFKC .cluster text{fill:#333;}#mermaid-svg-RJYYo8EvsHWilFKC .cluster span{color:#333;}#mermaid-svg-RJYYo8EvsHWilFKC div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-RJYYo8EvsHWilFKC .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-RJYYo8EvsHWilFKC rect.text{fill:none;stroke-width:0;}#mermaid-svg-RJYYo8EvsHWilFKC .icon-shape,#mermaid-svg-RJYYo8EvsHWilFKC .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RJYYo8EvsHWilFKC .icon-shape p,#mermaid-svg-RJYYo8EvsHWilFKC .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-RJYYo8EvsHWilFKC .icon-shape .label rect,#mermaid-svg-RJYYo8EvsHWilFKC .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RJYYo8EvsHWilFKC .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-RJYYo8EvsHWilFKC .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-RJYYo8EvsHWilFKC :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
每次扇区擦除
擦除计数加一
写入计数器块
诊断服务读取
接近上限
上报故障并降级写入策略
继续运行

这里有个循环依赖要避开:计数器本身也要占一个块,写计数器又是一次擦写。通常的做法是把计数合并进已有的高频块,或降低计数精度,用少量寿命换可观的可观测性。

掉电测试要在实验室主动做,不能等现场暴露。用可控电源在每个写入窗口切断供电,反复上千次,检查上电后读到的值是否为上一个完整版本。受限于窗口覆盖不全,它抓不到所有时序缝隙,但足以暴露写入顺序颠倒、块头早于数据落盘这类结构性错误,这类错误一旦流到量产,代价远超测试工时。

12. 收束:分层交付的责任清单

回到开头那句 NvM_WriteBlock。它之所以能一行写完,是因为下面每一层都认领了自己的那份责任。

12.1 每层各自承诺什么

把责任摊开,比记住模块缩写更有用。

层 承诺 不承诺
NvM 块级读写、冗余、上电恢复 跨块事务
MemIf 按设备索引正确转发 任何数据正确性
Ea 块号到地址的正确换算 写坏后的重试
Fee 块语义、回收、掉电后仍可判有效 块容量与寿命的合理性
Eep 与 Fls 按 job 操作器件、如实返回结果 重试、缓存、校验策略

表里"不承诺"一列才是这套分层的价值所在:每一层明确知道自己不管什么,责任才不会在层间漂移,也不会层层重试把总线压满。

12.2 判断一个项目是否用对了抽象

看现象比看配置快。下面几条现象,出现任意两条就说明分层的账没算清。

现象 指向的问题
高频数据也放在 Flash 仿真区 寿命预算没做,或块划分按功能而非按写频次
写操作集中在点火或下电瞬间 回收没有提前触发,把擦除挤进了关键窗口
读块要等几十毫秒 外部器件与低速总线承担了本该片内承担的数据
掉电后整块失效而非回落到旧值 写入顺序或块头设计有问题
故障都堆在驱动层日志里 上层没有校验手段,只能依赖驱动结果码

存储器硬件抽象能给的,是让同一份业务代码在两种介质上都跑得通的自由;它收走的,是对"数据落在哪里、写了几次、还能写几次"的直觉。这两笔账在第一版配置里就要算,等到量产阶段再回头改,改的就不只是配置了。

13. 参考文献

文献以 AUTOSAR 官方发布文档为准,覆盖从需求、抽象接口到底层驱动的六个环节。

13.1 文献清单

按文中出现顺序排列,出处给到章节号或条款 ID;换版本时以条款 ID 为准,页码会漂移。

序号 文献 版本 出处(章节或条款)
1 《存储器硬件抽象层需求》(Requirements on Memory Hardware Abstraction Layer) R4.3 6.1.1.3.1 虚拟地址空间、6.1.1.3.4 地址换算、6.1.2.1.2 设备索引、6.1.2.4.1 参数校验边界、6.1.1.5.3 不返回不一致数据
2 《存储器抽象接口规范》(Specification of Memory Abstraction Interface) R4.3 8.2.1 MemIf_StatusType、8.2.2 MemIf_JobResultType、8.3.3 MemIf_Write、9 时序图、10 配置规范
3 《EEPROM 抽象层规范》(Specification of EEPROM Abstraction) R4.3 7.1.1 寻址方案与分段、7.1.2 地址计算、7.1.5 块一致性信息、8.3.4 Ea_Write
4 《Flash EEPROM 仿真规范》(Specification of Flash EEPROM Emulation) R4.3 7.1.3 擦除次数限制、7.1.5 块正确性信息、8.3.4 Fee_Write
5 《EEPROM 驱动规范》(Specification of EEPROM Driver) R4.3 7.3.4 超时监控、7.5 job 处理通用要求、7.7 写 job 处理、7.9 比对 job 处理
6 《Flash 驱动规范》(Specification of Flash Driver) R4.3 7.3 外部 Flash 驱动、7.4 访问代码的加载与移除、8.3.2 Fls_Erase、8.3.11 Fls_BlankCheck
7 《EEPROM 驱动需求》(Requirements on EEPROM Driver) R4.3 5.1.1.1.2.10 SRS_Eep_00095 同一时刻一个 job、5.1.1.2.1.1 SRS_Eep_12051 内外器件需求一致
8 《NVRAM 管理器规范》(Specification of NVRAM Manager) R4.3 7.1.1 层结构、7.1.2 存储器硬件抽象的寻址方案、7.1.4.5 冗余 NVRAM 块
相关推荐
政在远航4 天前
RAID≠磁盘阵列!存储方式与视频监控存储方案详解
存储·分布式存储·nas·磁盘阵列·raid·视频监控存储·ip-san
李兆龙的博客1 个月前
从一到无穷大 #106:Nimble 文件格式——超宽表的元数据、编码与索引设计
数据库·存储·parquet
张星河zen1 个月前
下一代 HBM 存储不再只是堆层数:iHBM、vHBM、zHBM 三大厂商三种技术路线之争
存储·先进封装·三星·hbm·三维集成·美光·海力士
黑猫学长呀1 个月前
存储芯片千问千答第11问:对于磨损类用例,waf越大的模型磨损的更快,还是waf更小的模型磨损的更快?
gc·存储·ftl·ufs·nand·存储芯片·nandflash
李兆龙的博客1 个月前
从一到无穷大 #105:OpenAI Habitat II——多租户演进、全球读取优化与 CDC 平台
云计算·存储
李兆龙的博客1 个月前
从一到无穷大 #101:SSD 的工作原理——从 NAND 到数据库读写
数据库·ssd·存储
闻缺陷则喜何志丹1 个月前
vc入门宝典(九)
数据结构·list·mfc·存储·null·class
闻缺陷则喜何志丹2 个月前
详细设计说明书的内容要求
算法·测试·存储
工作10年+,存储芯片行业2 个月前
U.2 企业盘 × PCIe 4.0 / 5.0:主控与验证
ssd·nvme·存储·pcie·主控·nand·u.2