1 /* 2 * Copyright (c) 2023 HPMicro 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 8 #ifndef _EEPROM_EMULATION_H 9 #define _EEPROM_EMULATION_H 10 11 #include <stdint.h> 12 #include "user_config.h" 13 #include "hpm_nor_flash.h" 14 #include "hpm_common.h" 15 16 #ifdef __cplusplus 17 extern "C" { 18 #endif 19 20 #define E2P_DEBUG_LEVEL_TRACE (0) 21 #define E2P_DEBUG_LEVEL_INFO (1) 22 #define E2P_DEBUG_LEVEL_WARN (2) 23 #define E2P_DEBUG_LEVEL_ERROR (3) 24 25 #ifndef E2P_DEBUG_TRACE 26 # if E2P_DEBUG_LEVEL <= E2P_DEBUG_LEVEL_TRACE 27 # define E2P_TRACE(fmt, ...) printf("%s: %d: debug: "fmt"%s\n", __func__, __LINE__, __VA_ARGS__) 28 # define e2p_trace(...) E2P_TRACE(__VA_ARGS__, "") 29 # else 30 # define e2p_trace(...) 31 # endif 32 #endif 33 34 #ifndef E2P_DEBUG_INFO 35 # if E2P_DEBUG_LEVEL <= E2P_DEBUG_LEVEL_INFO 36 # define E2P_INFO(fmt, ...) printf(""fmt"%s\n", __VA_ARGS__) 37 # define e2p_info(...) E2P_INFO(__VA_ARGS__, "") 38 # else 39 # define e2p_info(...) 40 # endif 41 #endif 42 43 #ifndef E2P_DEBUG_WARN 44 # if E2P_DEBUG_LEVEL <= E2P_DEBUG_LEVEL_WARN 45 # define E2P_WARN(fmt, ...) printf(""fmt"%s\n", __VA_ARGS__) 46 # define e2p_warn(...) E2P_WARN(__VA_ARGS__, "") 47 # else 48 # define e2p_warn(...) 49 # endif 50 #endif 51 52 #ifndef E2P_DEBUG_ERROR 53 # if E2P_DEBUG_LEVEL <= E2P_DEBUG_LEVEL_ERROR 54 # define E2P_ERR(fmt, ...) printf(""fmt"%s\n", __VA_ARGS__) 55 # define e2p_err(...) E2P_ERR(__VA_ARGS__, "") 56 # else 57 # define e2p_err(...) 58 # endif 59 #endif 60 61 #define E2P_MAX_VAR_CNT (100) 62 #ifdef EEPROM_MAX_VAR_CNT 63 #undef E2P_MAX_VAR_CNT 64 #define E2P_MAX_VAR_CNT EEPROM_MAX_VAR_CNT 65 #endif 66 67 typedef enum { 68 e2p_invalid = 0xCCCC, 69 e2p_valid = 0xEEEE, 70 e2p_earsed = 0xFFFF, 71 } e2p_valid_state; 72 73 enum { 74 E2P_STATUS_OK = 0, 75 E2P_ERROR, 76 E2P_ERROR_NO_MEM, 77 E2P_ERROR_INIT_ERR, 78 E2P_ERROR_BAD_ID, 79 E2P_ERROR_BAD_ADDR, 80 E2P_ERROR_MUL_VAR, 81 }; 82 83 typedef struct { 84 uint32_t block_id; 85 uint32_t data_addr; 86 uint16_t length; 87 e2p_valid_state valid_state; 88 uint32_t crc; 89 } e2p_block; 90 91 typedef struct { 92 uint32_t start_addr; 93 uint32_t sector_cnt; 94 uint16_t erase_size; 95 uint32_t version; 96 97 uint32_t (*flash_read)(uint8_t *buf, uint32_t addr, uint32_t size); 98 uint32_t (*flash_write)(uint8_t *buf, uint32_t addr, uint32_t size); 99 void (*flash_erase)(uint32_t start_addr, uint32_t size); 100 } e2p_config_t; 101 102 typedef struct { 103 e2p_config_t config; 104 nor_flash_config_t nor_config; 105 106 uint32_t p_data; 107 uint32_t p_info; 108 uint32_t remain_size; 109 } e2p_t; 110 111 #define E2P_MAGIC_ID (0x48504D43) /*'H' 'P' 'M' 'C'*/ 112 113 #define E2P_EARSED_ID (0xFFFFFFFF) 114 #define E2P_EARSED_VAR (0xFF) 115 116 #define E2P_FLUSH_TRY (0) 117 #define E2P_FLUSH_BEGIN (1) 118 119 /** 120 * @brief eeprom emulation config 121 * 122 * @param e2p instance context 123 * @return hpm_stat_t 124 */ 125 hpm_stat_t e2p_config(e2p_t *e2p); 126 127 /** 128 * @brief eeprom emulation flush whole area, remove redundancy 129 * 130 * @param e2p instance context 131 * @param flag E2P_FLUSH_TRY - conditional flush, E2P_FLUSH_BEGIN - force flush 132 * @return hpm_stat_t 133 */ 134 hpm_stat_t e2p_flush(e2p_t *e2p, uint8_t flag); 135 136 /** 137 * @brief eeprom emulation write 138 * 139 * @param e2p instance context 140 * @param block_id custom id 141 * @param length data length 142 * @param data 143 * @return hpm_stat_t 144 */ 145 hpm_stat_t e2p_write(e2p_t *e2p, uint32_t block_id, uint16_t length, uint8_t *data); 146 147 /** 148 * @brief eeprom emulation read 149 * 150 * @param e2p instance context 151 * @param block_id custom id 152 * @param length data length 153 * @param data 154 * @return hpm_stat_t 155 */ 156 hpm_stat_t e2p_read(e2p_t *e2p, uint32_t block_id, uint16_t length, uint8_t *data); 157 158 /** 159 * @brief generate custom id 160 * 161 * @param name 162 * @return uint32_t 163 */ 164 uint32_t e2p_generate_id(const char *name); 165 166 /** 167 * @brief format whole area, 0xFF 168 * 169 * @param e2p 170 */ 171 void e2p_format(e2p_t *e2p); 172 173 /** 174 * @brief show e2p instance info include config info and store info 175 * 176 * @param e2p 177 */ 178 void e2p_show_info(e2p_t *e2p); 179 180 /* 181 e2p_t demo = { 182 .config.start_addr = 0x80080000, 183 .config.erase_size = 4096, 184 .config.sector_cnt = 128, 185 .config.version = 0x4553, 186 .config.flash_read = flash_read, 187 .config.flash_write = flash_write, 188 .config.flash_erase = flash_erase, 189 }; 190 191 192 int main(void) 193 { 194 e2p_config(&demo); 195 196 197 ... 198 ... 199 } 200 201 */ 202 203 #ifdef __cplusplus 204 } 205 #endif 206 207 #endif