• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2014-2018, Armink, <armink.ztl@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * 'Software'), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Function: It is an head file for this library. You can see all be called functions.
24  * Created on: 2014-09-10
25  */
26 #ifndef _EASYFLASH_H_
27 #define _EASYFLASH_H_
28 
29 #include <ef_cfg.h>
30 #include <stdint.h>
31 #include <stddef.h>
32 #include <stdbool.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #if defined(EF_USING_ENV) && (!defined(ENV_USER_SETTING_SIZE) || !defined(ENV_AREA_SIZE))
39 #error "Please configure user setting ENV size or ENV area size (in ef_cfg.h)"
40 #endif
41 
42 #if defined(EF_USING_LOG) && !defined(LOG_AREA_SIZE)
43 #error "Please configure log area size (in ef_cfg.h)"
44 #endif
45 
46 #if !defined(EF_START_ADDR)
47 #error "Please configure backup area start address (in ef_cfg.h)"
48 #endif
49 
50 #if !defined(EF_ERASE_MIN_SIZE)
51 #error "Please configure minimum size of flash erasure (in ef_cfg.h)"
52 #endif
53 
54 /* EasyFlash debug print function. Must be implement by user. */
55 #define EF_DEBUG(...) ef_log_debug(__FILE__, __LINE__, __VA_ARGS__)
56 /* EasyFlash routine print function. Must be implement by user. */
57 #define EF_INFO(...)  ef_log_info(__VA_ARGS__)
58 /* EasyFlash assert for developer. */
59 #define EF_ASSERT(EXPR)                                                       \
60 	if (!(EXPR))                                                                  \
61 	{                                                                             \
62 		EF_DEBUG("(%s) has assert failed at %s.\n", #EXPR, __FUNCTION__);         \
63 		while (1);                                                                \
64 	}
65 /* EasyFlash software version number */
66 #define EF_SW_VERSION                "3.0.4"
67 
68 typedef struct _ef_env {
69 	char *key;
70 	char *value;
71 } ef_env, *ef_env_t;
72 
73 /* EasyFlash error code */
74 typedef enum {
75 	EF_NO_ERR,
76 	EF_ERASE_ERR,
77 	EF_WRITE_ERR,
78 	EF_ENV_NAME_ERR,
79 	EF_ENV_NAME_EXIST,
80 	EF_ENV_FULL,
81 	EF_ENV_INIT_FAILED,
82 } EfErrCode;
83 
84 /* the flash sector current status */
85 typedef enum {
86 	EF_SECTOR_EMPTY,
87 	EF_SECTOR_USING,
88 	EF_SECTOR_FULL,
89 } EfSecrorStatus;
90 
91 /* easyflash.c */
92 EfErrCode easyflash_init(void);
93 
94 #ifdef EF_USING_ENV
95 /* ef_env.c ef_env_wl.c */
96 EfErrCode ef_load_env(void);
97 void ef_print_env(void);
98 char *ef_get_env(const char *key);
99 EfErrCode ef_set_env(const char *key, const char *value);
100 EfErrCode ef_save_env(void);
101 EfErrCode ef_env_set_default(void);
102 size_t ef_get_env_write_bytes(void);
103 EfErrCode ef_set_and_save_env(const char *key, const char *value);
104 #endif
105 
106 #ifdef EF_USING_IAP
107 /* ef_iap.c */
108 EfErrCode ef_erase_bak_app(size_t app_size);
109 EfErrCode ef_erase_user_app(uint32_t user_app_addr, size_t user_app_size);
110 EfErrCode ef_erase_spec_user_app(uint32_t user_app_addr, size_t app_size,
111 								 EfErrCode(*app_erase)(uint32_t addr, size_t size));
112 EfErrCode ef_erase_bl(uint32_t bl_addr, size_t bl_size);
113 EfErrCode ef_write_data_to_bak(uint8_t *data, size_t size, size_t *cur_size,
114 							   size_t total_size);
115 EfErrCode ef_copy_app_from_bak(uint32_t user_app_addr, size_t app_size);
116 EfErrCode ef_copy_spec_app_from_bak(uint32_t user_app_addr, size_t app_size,
117 									EfErrCode(*app_write)(uint32_t addr, const uint32_t *buf, size_t size));
118 EfErrCode ef_copy_bl_from_bak(uint32_t bl_addr, size_t bl_size);
119 uint32_t ef_get_bak_app_start_addr(void);
120 #endif
121 
122 #ifdef EF_USING_LOG
123 /* ef_log.c */
124 EfErrCode ef_log_read(size_t index, uint32_t *log, size_t size);
125 EfErrCode ef_log_write(const uint32_t *log, size_t size);
126 EfErrCode ef_log_clean(void);
127 size_t ef_log_get_used_size(void);
128 #endif
129 
130 /* ef_utils.c */
131 uint32_t ef_calc_crc32(uint32_t crc, const void *buf, size_t size);
132 
133 /* ef_port.c */
134 EfErrCode ef_port_read(uint32_t addr, uint32_t *buf, size_t size);
135 EfErrCode ef_port_erase(uint32_t addr, size_t size);
136 EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size);
137 void ef_port_env_lock(void);
138 void ef_port_env_unlock(void);
139 void ef_log_debug(const char *file, const long line, const char *format, ...);
140 void ef_log_info(const char *format, ...);
141 void ef_print(const char *format, ...);
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 
147 #endif /* EASYFLASH_H_ */
148