STC15W408AS library 0.18.0
Loading...
Searching...
No Matches
eeprom.h
Go to the documentation of this file.
1#ifndef STC15_EEPROMH
2#define STC15_EEPROMH
3
12#include <sys.h>
13#include <bits.h>
14#include <power_management.h>
15
16
23{
25 READ_OP = 0x01,
27 WRITE_OP = 0x02,
29 ERASE_OP = 0x03,
30};
31
46
47
53#define OP_TRIGGER_SEQ_FIRST_BYTE 0x5A
59#define OP_TRIGGER_SEQ_SECOND_BYTE 0xA5
60
66#define CMD_FAIL_BIT 4
67
73#define ERROR_VALUE 0xFF
74
83#define eeprom_disable_iap() \
84do { \
85 /* Disable IAP */ \
86 bit_clr(IAP_CONTR, SBIT7); \
87 IAP_CMD = 0x00; \
88 IAP_TRIG = 0x00; \
89 IAP_ADDRH = 0xFF; \
90 IAP_ADDRL = 0xFF; \
91} while(0)
92
101#define get_eeprom_last_operation_result() (test_if_bit_set(IAP_CONTR, CMD_FAIL_BIT) ? CMD_FAIL_ERROR : CMD_SUCCESS)
102
126#define eeprom_read_byte(addr_high, addr_low, value_ptr, error_ptr) \
127do { \
128 if (get_power_low_voltage_flag()) \
129 { \
130 *error_ptr = LOW_VOLTAGE_ERROR; \
131 *value_ptr = ERROR_VALUE; \
132 } \
133 else \
134 { \
135 /* Enable IAP */ \
136 bit_set(IAP_CONTR, SBIT7); \
137 /* Set IAP WT2 WT1 WT0 to 011 for eeprom read waiting */ \
138 IAP_CONTR &= ~0x07; \
139 IAP_CONTR |= 0x03; \
140 \
141 /* Set address */ \
142 IAP_ADDRH = (addr_high); \
143 IAP_ADDRL = (addr_low); \
144 \
145 /* Set read operation */ \
146 IAP_CMD = READ_OP; \
147 \
148 /* Set start operation sequence */ \
149 IAP_TRIG = OP_TRIGGER_SEQ_FIRST_BYTE; \
150 IAP_TRIG = OP_TRIGGER_SEQ_SECOND_BYTE; \
151 \
152 /* Wait for operation to complete */ \
153 NOP(); \
154 \
155 /* Read error status from IAP */ \
156 *error_ptr = get_eeprom_last_operation_result(); \
157 /* if no operation error read data from IAP otherwise value is ERROR_VALUE */ \
158 *value_ptr = (*error_ptr) ? ERROR_VALUE : IAP_DATA; \
159 \
160 eeprom_disable_iap(); \
161 } \
162} while (0)
163
187#define eeprom_erase_page(sector_start_addr, error_ptr) \
188do { \
189 if (get_power_low_voltage_flag()) \
190 { \
191 *error_ptr = LOW_VOLTAGE_ERROR; \
192 } \
193 else \
194 { \
195 /* Set address */ \
196 IAP_ADDRH = (sector_start_addr); \
197 IAP_ADDRL = 0x00; \
198 \
199 /* Set erase operation waiting */ \
200 IAP_CONTR &= ~0x07; \
201 IAP_CONTR |= 0x03; \
202 \
203 /* Enable IAP */ \
204 bit_set(IAP_CONTR, SBIT7); \
205 \
206 /* Set erase operation */ \
207 IAP_CMD = ERASE_OP; \
208 \
209 /* Set start operation sequence */ \
210 IAP_TRIG = OP_TRIGGER_SEQ_FIRST_BYTE; \
211 IAP_TRIG = OP_TRIGGER_SEQ_SECOND_BYTE; \
212 \
213 /* Wait for operation to complete */ \
214 NOP(); \
215 \
216 /* Read error status from IAP */ \
217 *error_ptr = get_eeprom_last_operation_result(); \
218 \
219 eeprom_disable_iap(); \
220 } \
221} while(0)
222
250#define eeprom_write_byte(addr_high, addr_low, value, error_ptr) \
251do { \
252 if (get_power_low_voltage_flag()) \
253 { \
254 *error_ptr = LOW_VOLTAGE_ERROR; \
255 } \
256 else \
257 { \
258 /* Set address */ \
259 IAP_ADDRH = addr_high; \
260 IAP_ADDRL = addr_low; \
261 \
262 /* Set write operation waiting */ \
263 IAP_CONTR &= ~0x07; \
264 IAP_CONTR |= 0x03; \
265 \
266 /* Enable IAP */ \
267 bit_set(IAP_CONTR, SBIT7); \
268 \
269 /* Set write operation */ \
270 IAP_CMD = WRITE_OP; \
271 \
272 /* Set data */ \
273 IAP_DATA = value; \
274 \
275 /* Set start operation sequence */ \
276 IAP_TRIG = OP_TRIGGER_SEQ_FIRST_BYTE; \
277 IAP_TRIG = OP_TRIGGER_SEQ_SECOND_BYTE; \
278 \
279 /* Wait for operation to complete */ \
280 NOP(); \
281 \
282 /* Read error status from IAP */ \
283 *error_ptr = get_eeprom_last_operation_result(); \
284 \
285 eeprom_disable_iap(); \
286 } \
287} while (0)
288
304#define eeprom_write_page(addr_high, arr, arr_size, error_ptr) \
305do { \
306 eeprom_erase_page(addr_high, error_ptr); \
307 if (*error_ptr) \
308 { \
309 break; \
310 } \
311 \
312 for (uint8_t i = 0; i < arr_size; i++) \
313 { \
314 eeprom_write_byte(addr_high, i, arr[i], error_ptr); \
315 if (*error_ptr) \
316 { \
317 break; \
318 } \
319 } \
320} while(0)
321
322#endif
eeprom_operation_t
EEPROM operation types.
Definition eeprom.h:23
eeprom_operation_status_t
EEPROM operation status.
Definition eeprom.h:38
@ ERASE_OP
Erase operation.
Definition eeprom.h:29
@ WRITE_OP
Write operation.
Definition eeprom.h:27
@ READ_OP
Read operation.
Definition eeprom.h:25
@ CMD_SUCCESS
Operation success.
Definition eeprom.h:40
@ CMD_FAIL_ERROR
Operation fail.
Definition eeprom.h:42
@ LOW_VOLTAGE_ERROR
Low voltage error.
Definition eeprom.h:44