
Starting embedded firmware week and I have to admit something: until today I was quietly treating "the chip" like a tiny version of my laptop. CPU does math, storage holds files, RAM is just... more storage that's faster. Wei watched me say something like that out loud and gently corrected me, and now I get why he winced.
A microcontroller (MCU) is a whole computer squeezed onto one chip — CPU core, flash memory, RAM, and a bunch of peripherals (timers, ADC, UART, SPI, GPIO, radio interfaces on some parts) all fabricated together. On a PC, storage and RAM are physically separate components you can swap independently, sitting behind a whole OS that abstracts memory management away from you. On an MCU running firmware, there's usually no OS in the PC sense, and flash and RAM are fixed, tiny, and you as the firmware author are directly responsible for how both get used.
Flash is non-volatile — it holds its contents with no power applied, which is exactly why it's where your compiled firmware code lives. Pull the battery, plug it back in weeks later, and the firmware is still there, ready to boot. That part isn't surprising. What did surprise me: flash has a limited write endurance — each cell can only be erased and reprogrammed so many times (I've seen numbers like tens of thousands of cycles thrown around for common embedded flash, though I haven't verified what applies to our exact parts) before it starts becoming unreliable. And it's a fixed, finite size — however many kilobytes the chip gives you, that's it. Every function you add, every library you pull in, competes for that same finite space. That's the "oh, THAT'S why we care so much about flash size" moment for me today — it's not an abstract efficiency concern, it's a hard ceiling.
RAM is volatile — it holds the program's live, running state: variables, the stack, buffers, whatever the firmware is actively working with right now. The moment power drops or the chip resets, RAM's contents are gone. That's normal and expected — RAM was never meant to persist anything. But it means anything you actually need to survive a reset or a power loss (calibration values, a device ID, a saved configuration) has to be deliberately written to flash, not just left sitting in a variable.
Putting it together: flash is "what the device knows how to do," RAM is "what the device is doing right now," and neither one is elastic the way a PC's disk-plus-swap setup feels. For something like the SQC485Iv2 node, where the firmware has to fit a mesh/radio stack plus the actual sensor logic into a fixed flash budget, I can already tell this distinction is going to come up constantly for the rest of the month. Tomorrow: OTA updates, and why you apparently need two copies of firmware instead of one.

進入韌體週的第一天,先自首一件事:在今天之前,我心裡一直偷偷把「這顆晶片」當成一台迷你版的筆電——CPU 做運算、storage 放檔案、RAM 就只是「比較快的 storage」而已。我把這句話講出來的時候 Wei 就在旁邊,他很委婉地糾正了我,現在我懂他為什麼皺眉了。
MCU(微控制器)是把整台電腦濃縮進一顆晶片。 CPU core、flash memory、RAM,加上一堆 peripherals(timer、ADC、UART、SPI、GPIO,某些型號還有 radio 介面)全部一起做在同一顆晶片上。在 PC 上,storage 跟 RAM 是可以各自獨立更換的實體元件,而且背後有一整套作業系統幫你把記憶體管理抽象掉。但在跑韌體的 MCU 上,通常沒有 PC 意義下的作業系統,flash 跟 RAM 的容量都是固定、而且很小的,怎麼用這兩塊資源是韌體工程師直接要負責的事。
Flash 是 non-volatile(非揮發性)——沒有供電時內容也不會消失,這就是為什麼編譯好的韌體 code 會放在這裡。把電池拔掉,幾週後再插回去,韌體還在,還是可以正常開機。這部分不算意外。真正讓我意外的是:flash 有寫入次數上限(write endurance)——每個 cell 能被抹除、重寫的次數是有限的(我看到一般 embedded flash 常被引用的數字是幾萬次這個等級,但我還沒查證這對我們實際用的晶片是不是準確),超過之後就會開始不穩定。而且它的容量是固定、有限的——晶片給你多少 KB 就是多少 KB。每加一個功能、每 include 一個 library,都在搶同一塊有限空間。這就是我今天「喔,原來這就是為什麼大家這麼在意 flash 大小」的頓悟時刻——這不是抽象的效率問題,是一個實打實的天花板。
RAM 是 volatile(揮發性)——存的是程式當下正在跑的即時狀態:變數、stack、buffer,任何韌體現在正在處理的東西。一旦斷電或重置,RAM 裡的內容就全沒了。這是正常、也是預期中的行為——RAM 從來就不打算讓資料留下來。但這也代表任何你真的需要在重置或斷電後還能保留的東西(校正值、裝置 ID、儲存的設定),都必須刻意寫進 flash,不能只是放在變數裡就以為它會留著。
整理一下:flash 是「這台裝置知道怎麼做的事」,RAM 是「這台裝置現在正在做的事」,兩者都不像 PC 的硬碟加 swap 那樣有彈性。以 SQC485Iv2 這類節點來說,韌體要在固定的 flash 預算裡塞進 mesh/radio stack 加上實際的感測邏輯,我已經可以預見接下來這個月這個區別會一直冒出來。明天要看 OTA 更新——為什麼似乎需要兩份韌體副本,而不是一份。