-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2promvars.h
54 lines (37 loc) · 1.77 KB
/
e2promvars.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef _E2PROMVARS_
#define _E2PROMVARS_
#include <avr/eeprom.h>
// helper macro
#define EEPROM_NAME(x) eeprom_ ## x
// helpe macro
#define EEPROM_ADDRESS(x) ((&EEPROM_NAME(x)))
// Call to read variable from EEPROM
#define EEPROM_READ(x) eeprom_read_block(&x, EEPROM_ADDRESS(x), sizeof(x))
// Call to store variable to EEPROM
#define EEPROM_WRITE(x) eeprom_write_block(&x, EEPROM_ADDRESS(x), sizeof(x))
// can define var/eeprom var pair, defining an initial value: EEPROM_VAR (int a) = 0;
#define EEPROM_VAR(var_type, var_name) \
var_type var_name; \
var_type EEPROM_NAME(var_name) EEMEM
#define VAR_FN(var_name) var_name ## Init
// defines SRAM/EEPROM var pair with loading EEPROM into SRAM code just before main() call
#define EEPROM_AUTO_VAR(var_type, var_name, var_value) \
EEPROM_VAR(var_type, var_name) = var_value; \
void VAR_FN(var_name) (void) __attribute__ ((naked)) \
__attribute__ ((section (".init8"))); \
void VAR_FN(var_name) (void){EEPROM_READ(var_name);} \
#define EEPROM_SETUP(a,b) {addr:(uint8_t*)&(a), value:(b)}
typedef struct {
uint8_t *addr;
uint8_t value;
} _setup_rec;
typedef char* _pointer;
// define device configuration: EEPROM_CONFIG(name)={EEPROM_SETUP(port, value), ...};
#define EEPROM_CONFIG(name) _setup_rec name[] EEMEM
#define EEPROM_CONFIG_ADR(name) &name[0]
#define EEPROM_CONFIG_END(name) (EEPROM_CONFIG_ADR(name) + sizeof(name))
// Uses EEPROM_CONFIG defined data to setup device
#define EEPROM_CONFIGURE(name) eeprom_setup(&name[0], sizeof(name))
// Rewrites EEPROM config to registers. Don't call directly, use EEPROM_CONFIG instead
void eeprom_setup(const _setup_rec * data, uint16_t size);
#endif