STC15W408AS library 0.13.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 <stdint.h>
13
14#include <sys.h>
15#include <bits.h>
16#include <power_management.h>
17
18
25{
27 READ_OP = 0x01,
29 WRITE_OP = 0x02,
31 ERASE_OP = 0x03,
32};
33
48
49
55#define OP_TRIGGER_SEQ_FIRST_BYTE 0x5A
61#define OP_TRIGGER_SEQ_SECOND_BYTE 0xA5
62
68#define CMD_FAIL_BIT 4
69
75#define ERROR_VALUE 0xFF
76
85#define eeprom_disable_iap() \
86do { \
87 /* Disable IAP */ \
88 bit_clr(IAP_CONTR, CBIT7); \
89 IAP_CMD = 0x00; \
90 IAP_TRIG = 0x00; \
91 IAP_ADDRH = 0xFF; \
92 IAP_ADDRL = 0xFF; \
93} while(0)
94
103#define get_eeprom_last_operation_result() (get_bit(IAP_CONTR, CMD_FAIL_BIT) ? CMD_FAIL_ERROR : CMD_SUCCESS)
104
128#define eeprom_read_byte(addr_high, addr_low, value_ptr, error_ptr) \
129do { \
130 if (power_low_voltage_flag_get()) \
131 { \
132 *error_ptr = LOW_VOLTAGE_ERROR; \
133 *value_ptr = ERROR_VALUE; \
134 } \
135 else \
136 { \
137 /* Enable IAP */ \
138 bit_set(IAP_CONTR, SBIT7); \
139 /* Set IAP WT2 WT1 WT0 to 011 for eeprom read waiting */ \
140 IAP_CONTR &= ~0x07; \
141 IAP_CONTR |= 0x03; \
142 \
143 /* Set address */ \
144 IAP_ADDRH = (addr_high); \
145 IAP_ADDRL = (addr_low); \
146 \
147 /* Set read operation */ \
148 IAP_CMD = READ_OP; \
149 \
150 /* Set start operation sequence */ \
151 IAP_TRIG = OP_TRIGGER_SEQ_FIRST_BYTE; \
152 IAP_TRIG = OP_TRIGGER_SEQ_SECOND_BYTE; \
153 \
154 /* Wait for operation to complete */ \
155 NOP(); \
156 \
157 /* Read error status from IAP */ \
158 *error_ptr = get_eeprom_last_operation_result(); \
159 /* if no operation error read data from IAP otherwise value is ERROR_VALUE */ \
160 *value_ptr = (*error_ptr) ? ERROR_VALUE : IAP_DATA; \
161 \
162 eeprom_disable_iap(); \
163 } \
164} while (0)
165
189#define eeprom_erase_page(sector_start_addr, error_ptr) \
190do { \
191 if (power_low_voltage_flag_get()) \
192 { \
193 *error_ptr = LOW_VOLTAGE_ERROR; \
194 } \
195 else \
196 { \
197 /* Set address */ \
198 IAP_ADDRH = (sector_start_addr); \
199 IAP_ADDRL = 0x00; \
200 \
201 /* Set erase operation waiting */ \
202 IAP_CONTR &= ~0x07; \
203 IAP_CONTR |= 0x03; \
204 \
205 /* Enable IAP */ \
206 bit_set(IAP_CONTR, SBIT7); \
207 \
208 /* Set erase operation */ \
209 IAP_CMD = ERASE_OP; \
210 \
211 /* Set start operation sequence */ \
212 IAP_TRIG = OP_TRIGGER_SEQ_FIRST_BYTE; \
213 IAP_TRIG = OP_TRIGGER_SEQ_SECOND_BYTE; \
214 \
215 /* Wait for operation to complete */ \
216 NOP(); \
217 \
218 /* Read error status from IAP */ \
219 *error_ptr = get_eeprom_last_operation_result(); \
220 \
221 eeprom_disable_iap(); \
222 } \
223} while(0)
224
252#define eeprom_write_byte(addr_high, addr_low, value, error_ptr) \
253do { \
254 if (power_low_voltage_flag_get()) \
255 { \
256 *error_ptr = LOW_VOLTAGE_ERROR; \
257 } \
258 else \
259 { \
260 /* Set address */ \
261 IAP_ADDRH = addr_high; \
262 IAP_ADDRL = addr_low; \
263 \
264 /* Set write operation waiting */ \
265 IAP_CONTR &= ~0x07; \
266 IAP_CONTR |= 0x03; \
267 \
268 /* Enable IAP */ \
269 bit_set(IAP_CONTR, SBIT7); \
270 \
271 /* Set write operation */ \
272 IAP_CMD = WRITE_OP; \
273 \
274 /* Set data */ \
275 IAP_DATA = value; \
276 \
277 /* Set start operation sequence */ \
278 IAP_TRIG = OP_TRIGGER_SEQ_FIRST_BYTE; \
279 IAP_TRIG = OP_TRIGGER_SEQ_SECOND_BYTE; \
280 \
281 /* Wait for operation to complete */ \
282 NOP(); \
283 \
284 /* Read error status from IAP */ \
285 *error_ptr = get_eeprom_last_operation_result(); \
286 \
287 eeprom_disable_iap(); \
288 } \
289} while (0)
290
291#endif
eeprom_operation_t
EEPROM operation types.
Definition eeprom.h:25
eeprom_operation_status_t
EEPROM operation status.
Definition eeprom.h:40
@ ERASE_OP
Erase operation.
Definition eeprom.h:31
@ WRITE_OP
Write operation.
Definition eeprom.h:29
@ READ_OP
Read operation.
Definition eeprom.h:27
@ CMD_SUCCESS
Operation success.
Definition eeprom.h:42
@ CMD_FAIL_ERROR
Operation fail.
Definition eeprom.h:44
@ LOW_VOLTAGE_ERROR
Low voltage error.
Definition eeprom.h:46