10. EEPROM HAL

10.1. Overview

  • The EEPROM HAL (Hardware Abstraction Layer) component provides a unified interface for accessing various types of EEPROM storage devices within the HPMicro SDK. It abstracts away underlying hardware differences, offering a consistent API for applications to read and write EEPROM data.

    • EEPROM HAL Features

      • Support for Three Driver Types:
        • Flash-based EEPROM emulation

        • I2C EEPROM

        • SPI EEPROM

      • Multi-Instance Support:
        • The HAL supports multiple EEPROM device instances through a registration mechanism, managed by unique names.

        • Each instance can be independently configured with different hardware interfaces and parameters.

      • Unified API:
        • Provides a consistent set of read/write APIs, shielding applications from the complexities of the underlying implementation.

          Users can call the HAL-level APIs or, alternatively, invoke the low-level driver APIs directly (requires manual parameter configuration).

      • Page-Aware Writes:
        • The low-level drivers provide anti-wraparound functionality, automatically handling writes that cross page boundaries.

      • Status Polling:
        • Includes a polling interface to check for write completion, ensuring data is successfully written before proceeding.

10.2. Architecture

  • The architecture of the EEPROM HAL is illustrated below:

    ┌──────────────────────────────────────────────────────────┐
    │                   Application Layer                      │
    │         (User application using EEPROM storage)          │
    └──────────────────────────────────────────────────────────┘
                                │
                                │ Unified API
                                ▼
    ┌──────────────────────────────────────────────────────────┐
    │                   HAL Layer (eeprom_hal)                 │
    │  - Device Registration & Management                      │
    │  - Device Discovery (find by name)                       │
    │  - Unified Interface (read/write/init/deinit)            │
    └──────────────────────────────────────────────────────────┘
                                │
            ┌─────────────────┼─────────────────┐
            │                 │                 │
            ▼                 ▼                 ▼
    ┌──────────────────┐ ┌──────────────┐ ┌──────────────────┐
    │  I2C Device      │ │ SPI Device   │ │ Flash Emulation  │
    │  Driver Layer    │ │ Driver Layer │ │ Driver Layer     │
    │                  │ │              │ │                  │
    │ - I2C Protocol   │ │ - SPI Proto  │ │ - XPI Flash API  │
    │ - AT24Cxx Series │ │ - AT25xxx    │ │ - E2P Emulation  │
    │ - Page Write Mgmt│ │ - Status Reg │ │ - Wear Leveling  │
    └──────────────────┘ └──────────────┘ └──────────────────┘
            │                 │                 │
            ▼                 ▼                 ▼
    ┌──────────────────────────────────────────────────────────┐
    │              Hardware Abstraction Layer                  │
    │         (I2C Driver, SPI Driver, XPI Driver)             │
    └──────────────────────────────────────────────────────────┘
            │                 │                 │
            ▼                 ▼                 ▼
    ┌──────────────────────────────────────────────────────────┐
    │                Hardware Layer                            │
    │     (I2C EEPROM, SPI EEPROM, NOR Flash)                  │
    └──────────────────────────────────────────────────────────┘
    

10.3. Macros, Enums, and Structures

  • Macros

    • Macros are defined for configuring EEPROM types and the maximum number of registrable instances.

    /**
    * @brief Supported EEPROM driver types.
    * @note Used to specify the device type during registration.
    */
    #define EEPROM_FLAG_EMULATION_XPI 0x0
    #define EEPROM_FLAG_DEVICE_I2C    0x4
    #define EEPROM_FLAG_DEVICE_SPI    0x5
    
    /**
    * @brief Maximum number of registrable EEPROM devices.
    * @note Defines the size of the device registration array.
    */
    #define EEPROM_MAX_DEVICE_CNT 32
    
    /**
    * @brief SPI EEPROM instruction set.
    */
    #define E2P_SPI_INSTRUCTION_WREN  0x06  /* Write Enable */
    #define E2P_SPI_INSTRUCTION_RDSR  0x05  /* Read Status Register */
    #define E2P_SPI_INSTRUCTION_WRDI  0x04  /* Write Disable */
    #define E2P_SPI_INSTRUCTION_READ  0x03  /* Read Data */
    #define E2P_SPI_INSTRUCTION_WRITE 0x02  /* Write Data */
    #define E2P_SPI_INSTRUCTION_WRSR  0x01  /* Write Status Register */
    
  • Predefined Device Attributes

    • The HAL provides lists of supported physical I2C and SPI EEPROM devices. These lists contain parameters derived from the device datasheets and can be extended.

    support i2c device list:
    +--------------------+-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    | Company/Brand      | Part Number (Series)  | Density    | Page Size (Bytes)| Macro Definition                         | Notes                                    |
    +====================+=======================+============+================+==========================================+==========================================+
    | Microchip (Atmel)  | AT24C01 / AT24C02     | 1Kbit/2Kbit| 8              | E2P_DEVICE_AT24C01, E2P_DEVICE_AT24C02    | Standard 8-bit address                   |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | 24xx01 / 24xx02       | 1Kbit/2Kbit| 8              | E2P_DEVICE_24xx01, E2P_DEVICE_24xx02      | Standard 8-bit address                   |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | AT24C04 / 24xx04      | 4Kbit      | 16             | E2P_DEVICE_AT24C04, E2P_DEVICE_24xx04     | 9-bit address, A8 in device address      |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | AT24C08 / 24xx08      | 8Kbit      | 16             | E2P_DEVICE_AT24C08, E2P_DEVICE_24xx08     | 10-bit address, A9, A8 in device address |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | AT24C16 / 24xx16      | 16Kbit     | 16             | E2P_DEVICE_AT24C16, E2P_DEVICE_24xx16     | 11-bit address, A10-A8 in device address |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | AT24C32               | 32Kbit     | 32             | E2P_DEVICE_AT24C32                       | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | AT24C64 / 24xxC64     | 64Kbit     | 32             | E2P_DEVICE_AT24C64, E2P_DEVICE_24xxC64    | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | AT24C128 / 24xx128    | 128Kbit    | 64             | E2P_DEVICE_AT24C128, E2P_DEVICE_24xx128   | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | AT24C256 / 24xx256    | 256Kbit    | 64             | E2P_DEVICE_AT24C256, E2P_DEVICE_24xx256   | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | AT24C512 / 24xx512    | 512Kbit    | 128            | E2P_DEVICE_AT24C512, E2P_DEVICE_24xx512   | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | AT24CM01              | 1Mbit      | 256            | E2P_DEVICE_AT24CM01                      | 17-bit address, A16 in device address    |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | AT24CM02              | 2Mbit      | 256            | E2P_DEVICE_AT24CM02                      | 18-bit address, A17, A16 in device addr  |
    +--------------------+-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    | ON Semi            | NV24C02               | 2Kbit      | 16             | E2P_DEVICE_NV24C02                       | Standard 8-bit address                   |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | NV24C04               | 4Kbit      | 16             | E2P_DEVICE_NV24C04                       | 9-bit address, A8 in device address      |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | NV24C08               | 8Kbit      | 16             | E2P_DEVICE_NV24C08                       | 10-bit address, A9, A8 in device address |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | NV24C16               | 16Kbit     | 16             | E2P_DEVICE_NV24C16                       | 11-bit address, A10-A8 in device address |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | NV24C32               | 32Kbit     | 32             | E2P_DEVICE_NV24C32                       | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | NV24C64               | 64Kbit     | 32             | E2P_DEVICE_NV24C64                       | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | NV24C128              | 128Kbit    | 64             | E2P_DEVICE_NV24C128                      | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | NV24C256              | 256Kbit    | 64             | E2P_DEVICE_NV24C256                      | 2-byte address                           |
    +--------------------+-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    | STMicroelectronics | M24C01 / M24C02       | 1Kbit/2Kbit| 16             | E2P_DEVICE_M24C01, E2P_DEVICE_M24C02     | Standard 8-bit address                   |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | M24C04                | 4Kbit      | 16             | E2P_DEVICE_M24C04                        | 9-bit address, A8 in device address      |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | M24C08                | 8Kbit      | 16             | E2P_DEVICE_M24C08                        | 10-bit address, A9, A8 in device address |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | M24C16                | 16Kbit     | 16             | E2P_DEVICE_M24C16                        | 11-bit address, A10-A8 in device address |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | M24C32                | 32Kbit     | 32             | E2P_DEVICE_M24C32                        | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | M24128                | 128Kbit    | 64             | E2P_DEVICE_M24128                        | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | M24256                | 256Kbit    | 64             | E2P_DEVICE_M24256                        | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | M24512                | 512Kbit    | 128            | E2P_DEVICE_M24512                        | 2-byte address                           |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | M24M01                | 1Mbit      | 256            | E2P_DEVICE_M24M01                        | 17-bit address, A16 in device address    |
    |                    +-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    |                    | M24M02                | 2Mbit      | 256            | E2P_DEVICE_M24M02                        | 18-bit address, A17, A16 in device addr  |
    +--------------------+-----------------------+------------+----------------+------------------------------------------+------------------------------------------+
    
    support spi device list:
    +--------------------+------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    | Company/Brand      | Part Number      | Density   | Page Size      | Macro Definition                    | Notes                                                   |
    |                    | (Series)         |           | (Bytes)        |                                     |                                                         |
    +====================+==================+===========+================+=====================================+=========================================================+
    | Microchip (Atmel)  | AT25010B         | 1Kbit     | 8              | E2P_DEVICE_AT25010B                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | AT25020B         | 2Kbit     | 8              | E2P_DEVICE_AT25020B                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | AT25040B         | 4Kbit     | 8              | E2P_DEVICE_AT25040B                 | 9-bit address, MSB sent in instruction sequence       |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | AT25080B         | 8Kbit     | 32             | E2P_DEVICE_AT25080B                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | AT25160B         | 16Kbit    | 32             | E2P_DEVICE_AT25160B                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | AT25320B         | 32Kbit    | 32             | E2P_DEVICE_AT25320B                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | AT25640B         | 64Kbit    | 32             | E2P_DEVICE_AT25640B                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | AT25128B         | 128Kbit   | 64             | E2P_DEVICE_AT25128B                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | AT25256B         | 256Kbit   | 64             | E2P_DEVICE_AT25256B                 |                                                         |
    +--------------------+------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    | STMicroelectronics | M95010           | 1Kbit     | 16             | E2P_DEVICE_M95010                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95020           | 2Kbit     | 16             | E2P_DEVICE_M95020                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95040           | 4Kbit     | 16             | E2P_DEVICE_M95040                   | 9-bit address, MSB sent in instruction sequence       |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95080           | 8Kbit     | 32             | E2P_DEVICE_M95080                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95160           | 16Kbit    | 32             | E2P_DEVICE_M95160                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95320           | 32Kbit    | 32             | E2P_DEVICE_M95320                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95640           | 64Kbit    | 32             | E2P_DEVICE_M95640                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95128           | 128Kbit   | 64             | E2P_DEVICE_M95128                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95256           | 256Kbit   | 64             | E2P_DEVICE_M95256                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95512           | 512Kbit   | 128            | E2P_DEVICE_M95512                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95M01           | 1Mbit     | 256            | E2P_DEVICE_M95M01                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95M02           | 2Mbit     | 256            | E2P_DEVICE_M95M02                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | M95M04           | 4Mbit     | 256            | E2P_DEVICE_M95M04                   |                                                         |
    +--------------------+------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    | ABLIC (Seiko)      | S-25C010A        | 1Kbit     | 16             | E2P_DEVICE_S_25C010A                |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | S-25C020A        | 2Kbit     | 16             | E2P_DEVICE_S_25C020A                |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | S-25C040A        | 4Kbit     | 16             | E2P_DEVICE_S_25C040A                | 9-bit address, MSB sent in instruction sequence       |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | S-25C080A        | 8Kbit     | 32             | E2P_DEVICE_S_25C080A                |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | S-25C160A        | 16Kbit    | 32             | E2P_DEVICE_S_25C160A                |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | S-25C320A        | 32Kbit    | 32             | E2P_DEVICE_S_25C320A                |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | S-25C640A        | 64Kbit    | 32             | E2P_DEVICE_S_25C640A                |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | S-25C128A        | 128Kbit   | 64             | E2P_DEVICE_S_25C128A                |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | S-25C256A        | 256Kbit   | 64             | E2P_DEVICE_S_25C256A                |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | S-25C512A        | 512Kbit   | 128            | E2P_DEVICE_S_25C512A                |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | S-25CM01A        | 1Mbit     | 256            | E2P_DEVICE_S_25CM01A                |                                                         |
    +--------------------+------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    | Generic/Other      | 25xx010A         | 1Kbit     | 16             | E2P_DEVICE_25xx010A                 | (Often compatible with S-25C series)                    |
    | (Microchip, etc.)  +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25xx020A         | 2Kbit     | 16             | E2P_DEVICE_25xx020A                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25xx040A         | 4Kbit     | 16             | E2P_DEVICE_25xx040A                 | 9-bit address, MSB sent in instruction sequence       |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25xx080A         | 8Kbit     | 16             | E2P_DEVICE_25xx080A                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25xx160A         | 16Kbit    | 16             | E2P_DEVICE_25xx160A                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25xx080B         | 8Kbit     | 32             | E2P_DEVICE_25xx080B                 | (Often compatible with AT25 series)                     |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25xx160B         | 16Kbit    | 32             | E2P_DEVICE_25xx160B                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25xx320A         | 32Kbit    | 32             | E2P_DEVICE_25xx320A                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25xx640A         | 64Kbit    | 32             | E2P_DEVICE_25xx640A                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25xx128          | 128Kbit   | 64             | E2P_DEVICE_25xx128                  |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25xx256          | 256Kbit   | 64             | E2P_DEVICE_25xx256                  |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25x512           | 512Kbit   | 128            | E2P_DEVICE_25x512                   |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | 25xx1024         | 1Mbit     | 256            | E2P_DEVICE_25xx1024                 |                                                         |
    +--------------------+------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    | ON Semi            | NV25010A         | 1Kbit     | 16             | E2P_DEVICE_NV25010A                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | NV25020A         | 2Kbit     | 16             | E2P_DEVICE_NV25020A                 |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | NV25040A         | 4Kbit     | 16             | E2P_DEVICE_NV25040A                 | 9-bit address, MSB sent in instruction sequence       |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | NV25080          | 8Kbit     | 32             | E2P_DEVICE_NV25080                  |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | NV25160          | 16Kbit    | 32             | E2P_DEVICE_NV25160                  |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | NV25320          | 32Kbit    | 32             | E2P_DEVICE_NV25320                  |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | NV25640          | 64Kbit    | 32             | E2P_DEVICE_NV25640                  |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | NV25128          | 128Kbit   | 64             | E2P_DEVICE_NV25128                  |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | NV25256          | 256Kbit   | 64             | E2P_DEVICE_NV25256                  |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | NV25512          | 512Kbit   | 128            | E2P_DEVICE_NV25512                  |                                                         |
    |                    +------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    |                    | NV25M01          | 1Mbit     | 256            | E2P_DEVICE_NV25M01                  |                                                         |
    +--------------------+------------------+-----------+----------------+-------------------------------------+---------------------------------------------------------+
    
  • Structures

    • The following structures define the core components of the EEPROM HAL.

      • EEPROM Device Structure

      typedef struct {
          uint32_t flag; /* EEPROM device type flag /
          char device_name[E2P_DEVICE_NAME_MAX_LEN]; / Device instance name */
          void host; / Handle to the hardware interface used by the device */
          void private; / User-defined private data */
          void config; / Pointer to the EEPROM device configuration */
          void ops; / Pointer to the set of device operation functions */
      } hpm_eeprom_device_t;
      
    • Device Operations Structure

      typedef struct {
          hpm_stat_t (*init)(hpm_eeprom_device_t *device);
          hpm_stat_t (*deinit)(hpm_eeprom_device_t *device);
          hpm_stat_t (*write)(hpm_eeprom_device_t *device, uint32_t data_addr, uint32_t len, uint8_t *data);
          hpm_stat_t (*read)(hpm_eeprom_device_t *device, uint32_t data_addr, uint32_t len, uint8_t *data);
      } e2p_device_ops_t;
      
    • EEPROM Context Structure

      typedef struct {
          hpm_eeprom_device_t device[EEPROM_MAX_DEVICE_CNT]; /* Array of EEPROM device instances /
          uint8_t device_count; / Number of registered devices /
          uint32_t device_bit; / Bitmask of registered devices */
      } eeprom_context_t;
      
    • EEPROM Emulation Configuration

      typedef struct {
          uint32_t flash_base_addr; /* Base address of the flash used for emulation /
          uint32_t version; / Emulation version number /
          uint32_t opt_header; / Emulation configuration header /
          uint32_t opt0; / Emulation configuration option 0 /
          uint32_t opt1; / Emulation configuration option 1 /
          uint32_t start_addr; / Start address of the emulation data area /
          uint32_t erase_size; / Erase unit size /
          uint32_t sector_cnt; / Number of erase units (sectors) */
      } e2p_emu_xpi_config_t;
      
    • SPI EEPROM Device Configuration

      typedef struct {
          SPI_Type base; / SPI peripheral base address /
          uint32_t cs_pin; / SPI chip select pin */
          uint32_t (*clock_init)(SPI_Type ptr); / Function pointer for SPI clock initialization */
          void (*pin_init)(void); / Function pointer for SPI pin initialization */
          void (*cs_control)(uint32_t pin, uint8_t state); / Function pointer for SPI CS pin control */
      } e2p_device_spi_host_config_t;
      
    • I2C EEPROM Device Configuration

      typedef struct {
          I2C_Type base; / I2C peripheral base address */
          uint32_t (*clock_init)(I2C_Type ptr); / Function pointer for I2C clock initialization */
          void (*pin_init)(void); / Function pointer for I2C pin initialization */
      } e2p_device_i2c_host_config_t;
      
      typedef struct {
          uint32_t dev_addr; /* EEPROM I2C device address /
          uint32_t addr_bytes; / Number of address bytes /
          uint32_t page_size; / Page size in bytes /
          uint32_t timeout; / I2C operation timeout */
      } e2p_device_i2c_attribute_t;
      

10.4. API Usage Flow

  • The typical workflow for using the EEPROM HAL is as follows:
        Application Start
                │
                ▼
    ┌──────────────────────────┐
    │  1. Prepare Configuration│
    │     - Host config        │
    │     - Device attributes  │
    └──────────┬───────────────┘
            │
            ▼
    ┌──────────────────────────┐
    │  2. Register Parameters  │
    │     hpm_eeprom_          │
    │       register_param()   │
    └──────────┬───────────────┘
            │
            ▼
    ┌──────────────────────────┐
    │  3. Register Device      │
    │     hpm_eeprom_          │
    │       register()         │
    └──────────┬───────────────┘
            │
            ▼
    ┌──────────────────────────┐
    │  4. Find Device          │
    │     hpm_eeprom_find()    │
    └──────────┬───────────────┘
            │
            ▼
    ┌──────────────────────────┐
    │  5. Initialize Device    │
    │     hpm_eeprom_init()    │
    └──────────┬───────────────┘
            │
            ▼
    ┌──────────────────────────┐
    │  6. Read/Write Operations│
    │     hpm_eeprom_read_     │
    │       block()            │
    │     hpm_eeprom_write_    │
    │       block()            │
    └──────────┬───────────────┘
            │
            ▼
    ┌──────────────────────────┐
    │  7. Cleanup (Optional)   │
    │     hpm_eeprom_          │
    │       unregister()       │
    └──────────────────────────┘
    
  • Registration Examples

    Registering an Emulated EEPROM Instance (XPI Flash)

    For a detailed example, refer to unit_test/drivers/eeprom/e2p_emu/e2p_emu_test.c.

    • Preparation: You need to know the base address of the flash memory to be used, the simulated EEPROM version number, the configuration header and options, the starting address of the simulated EEPROM data area, the size and number of erase cells, and other parameters.

    e2p_emu_xpi_config_t emu_config = {
        .flash_base_addr = BOARD_FLASH_BASE_ADDRESS,
        .version = 0x4553,
        .opt_header = BOARD_APP_XPI_NOR_CFG_OPT_HDR,
        .opt0 = BOARD_APP_XPI_NOR_CFG_OPT_OPT0,
        .opt1 = BOARD_APP_XPI_NOR_CFG_OPT_OPT1,
        .start_addr = BOARD_APP_EEPROM_EMU_START_ADDR,
        .erase_size = BOARD_APP_EEPROM_EMU_ERASE_SIZE,
        .sector_cnt = BOARD_APP_EEPROM_EMU_SECTOR_COUNT,
    };
    
    hpm_eeprom_device_t e2p_emu_temp = {
        .ptr = BOARD_APP_XPI_NOR_XPI_BASE,
        .flag = EEPROM_FLAG_EMULATION_XPI,
        .config = &emu_config,
    };
    
    hpm_eeprom_register_emulation_xpi_param(&e2p_emu_temp);
    hpm_eeprom_register(&e2p_emu_temp, "e2p_emu");
    
    • Registering an SPI EEPROM Instance (AT25128)

      • The EEPROM HAL provides a global list hpm_support_e2p_spi_device_list[] for supported devices. Select the correct index and specify the SPI interface and CS pin. For details, see unit_test/drivers/eeprom/e2p_spi/e2p_spi_test.c.

        • Preparation: Configure the base address, chip select pin, clock initialization function, pin initialization function, and chip select pin control function for the SPI hardware interface. Also, confirm the EEPROM model being used. If it’s in the list above, assign a value to its ID; if not, manually fill in the dev_attr parameter and assign 0xff to its ID.

        uint32_t dev_type = E2P_DEVICE_AT25128;
        e2p_device_spi_attribute_t dev_attr;
        e2p_device_spi_host_config_t host_info = {
            .base = BOARD_APP_SPI_BASE,
            .cs_pin = BOARD_SPI_CS_PIN,
            .clock_init = board_init_spi_eeprom_clock,
            .pin_init = board_init_spi_eeprom_pin,
            .cs_control = board_write_spi_cs,
        };
        
        hpm_eeprom_device_t e2p_dev_temp = {
            .host = &host_info,
            .flag = EEPROM_FLAG_DEVICE_SPI,
            .config = &dev_attr,
        };
        
        hpm_eeprom_register_spi_param(&e2p_dev_temp, &host_info, dev_id);
        hpm_eeprom_register(&e2p_dev_temp, "e2p_dev_spi");
        
    • Registering an I2C EEPROM Instance (AT24C02)

      • Similar to SPI, select the device index from hpm_support_e2p_i2c_device_list[] and specify the I2C interface. For details, see unit_test/drivers/eeprom/e2p_i2c/e2p_i2c_test.c.

        • Preparation: Configure the base address, clock initialization function, pin initialization function, and other parameters for the I2C hardware interface. Also, confirm the EEPROM model used. If it’s in the list above, assign a value to id; if not, manually fill in the dev_attr parameter and assign id the value 0xff.

        uint8_t custom_id = 0;
        uint32_t e2p_device_index = HPM_E2P_I2C_ADD_CUSTOM_ID_TO_DEV_ID(custom_id, E2P_DEVICE_AT24C02);
        e2p_device_i2c_attribute_t dev_attr;
        e2p_device_i2c_host_config_t host_info = {
            .base = HPM_I2C1,
            .clock_init = board_init_i2c_eeprom_clock,
            .pin_init = board_init_i2c_eeprom_pin,
        };
        
        hpm_eeprom_device_t e2p_dev_temp = {
            .host = &host_info,
            .flag = EEPROM_FLAG_DEVICE_I2C,
            .config = &dev_attr,
        }; /* config known parameters */
        
        hpm_eeprom_register_i2c_param(&e2p_dev_temp, &host_info, e2p_device_id); /* get spi eeprom parameters */
        hpm_eeprom_register(&e2p_dev_temp, "e2p_dev_i2c"); /* register spi eeprom */
        
    • Important Notes:
      • Each registered instance must have a unique name for identification.

      • When using I2C eeprom, users need to pay attention to distinguishing different device IDs and specify the custom_id value according to the actual situation at the board level to avoid conflicts.

      • Registration requires prior registration of parameters before instance registration; without parameter registration, the ops interface will be empty during instance registration, preventing subsequent operations (this may cause exceptions when deleting items from the list).

      • If your specific EEPROM model is not in the predefined lists, you can manually populate the configuration structure and register it. Ensure the pinmux settings are correct.

      • For direct access to the low-level drivers, you can bypass the HAL by calling the driver APIs directly, but you will be responsible for managing all configuration parameters.

    • Finding and Using a Device

      • Find the device handle by name and initialize it.

        hpm_eeprom_device_t *e2p_dev = hpm_eeprom_find("e2p_dev_spi");
        hpm_eeprom_init(e2p_dev);
        
    • Perform read/write operations using the device handle.

      hpm_eeprom_write_block(device, addr, len, (uint8_t *)&write_val);
      hpm_eeprom_read_block(device, addr, len, (uint8_t *)&read_val);
      
      Once a device is registered, the application can perform operations using the unified API without needing to know the underlying hardware details.