EEPROM
Introduction
EEPROM (Electrically Erasable Programmable Read-Only Memory) refers to memory that can be electrically erased and programmed. It is a type of memory chip that retains data even when power is turned off.
Function List
Initialization — Initialize the driver.
Get Size — Get the size of the EEPROM.
Read Data — Read data from the EEPROM device.
Write Data — Write data to the EEPROM.
Set Write Protection — Set write protection.
Function Overview
EEPROM Device Configuration Configure the EEPROM model, total size, page size, write, write protection control pin, timing, and other parameters in the device table.
Random Address Read/Write Supports reading and writing of any length at any offset within the storage area.
Write Protection Supports write protection functionality.
Configuration
Before using the EEPROM driver, you need to configure the specific EEPROM model, address, size, page size, write page timing, and write protection functionality in the device table. Write protection is not mandatory.
Note
The pre-implemented EEPROM drivers in the SDK have been tested with the following models:
NV24C02
NV24C04
NV24C16
ST24C02WP
AT24C16
AT24C32
AT24C256
Other EEPROM models may not be supported. If the existing driver does not support a particular EEPROM model, you may need to develop the EEPROM driver yourself based on the model specifications or use the I2C driver interface for read/write operations directly.
EEPROM Address
The EEPROM address is determined by the specific EEPROM model and the hardware board wiring method. For details, refer to the EEPROM documentation and the board circuit schematic.
Typically, in a 7-bit address, the high 4 bits are
1 0 1 0
, and the next 3 bits are usually referred to asA2, A1, A0
. These 3 bits can be connected to the board’s GND or VDD high level, usually left floating. Floating and grounded both represent 0, while connecting to VCC represents 1. If all are left floating, the EEPROM address is 0b1010000, and the I2C address is 0x50. If A1 is connected to VDD and A2, A0 are left floating, the address is 0b1010010, and the I2C address is 0x52.
Main Functions
Initialize EEPROM
Before using the EEPROM, you need to call the
wm_drv_eeprom_init()
function to allocate resources for the EEPROM usingwm_device_t
. Example:wm_device_t *eeprom_dev = wm_dt_get_device_by_name("eeprom0"); wm_drv_eeprom_init("eeprom0");The first parameter specifies the device name, which is defined in the device table and must match the name in the device table.
Warning
After initializing the EEPROM, if
wm_drv_eeprom_deinit
is not called, callingwm_drv_eeprom_init
again will returnNULL
.
Get Size
To get the size of the EEPROM, call the
wm_drv_eeprom_get_size()
function, passing in the EEPROM’s device handle.Here is an example of getting the size:
wm_device_t *eeprom_dev = wm_dt_get_device_by_name("eeprom0"); wm_drv_eeprom_init("eeprom0"); /* Get the size of eeprom0 */ wm_drv_eeprom_get_size(eeprom_dev);
Write
Once the data to be sent is ready, call the function
wm_drv_eeprom_write
to send the data to the EEPROM device.Here is an example of writing to the EEPROM:
wm_device_t *eeprom_dev = wm_dt_get_device_by_name("eeprom0"); wm_drv_eeprom_init("eeprom0"); /* Write 5 bytes to the EEPROM at offset 2 */ wm_drv_eeprom_write(eeprom_dev, 2, "hello", 5);
Read
To read data, use the interface function
wm_drv_eeprom_read
, which reads data from the EEPROM device.Here is an example of reading from the EEPROM:
wm_device_t *eeprom_dev = wm_dt_get_device_by_name("eeprom0"); uint8_t buf[5]; wm_drv_eeprom_init("eeprom0"); /* Read 5 bytes from the EEPROM device at offset 2 */ wm_drv_eeprom_read(eeprom_dev, 2, buf, 5);
Set Write Protection
To set write protection, you need to configure a GPIO in the device table to support it, and the EEPROM must also support it. Typically, pulling the EEPROM’s WP pin high puts it into write-protection mode, preventing data from being written to the EEPROM via I2C. If the WP pin is pulled low, it allows writing.
Notes
Note
When configuring the EEPROM, the total size, page size, and write time may vary by model. You need to fill in these parameters according to the exact manual; otherwise, read/write operations may not function correctly.
Warning
Even if write protection is set, the wm_drv_eeprom_write
interface will automatically disable write protection before writing, but direct I2C writes will not be possible.
Application Example
For a basic example of using the EEPROM, please refer to examples/peripheral/eeprom
API Reference
To find EEPROM-related APIs, please refer to: EEPROM API Reference