-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheeprom_24AAxxx.c
136 lines (106 loc) · 3.64 KB
/
eeprom_24AAxxx.c
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
eeprom_24AAxxx.c - plugin for for I2C EEPROM (Microchip 24AAxxx > 16kbit, 2 byte address)
NOTE: only tested with 24AA256
Part of grblHAL
Copyright (c) 2020-2024 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#if EEPROM_ENABLE >= 32 || EEPROM_ENABLE == 2 || EEPROM_ENABLE == 3
#include "grbl/hal.h"
#include "grbl/crc.h"
#include "grbl/nuts_bolts.h"
#define EEPROM_I2C_ADDRESS (0xA0 >> 1)
#if EEPROM_ENABLE == 2 || EEPROM_ENABLE >= 128
#define EEPROM_PAGE_SIZE 64
#else
#define EEPROM_PAGE_SIZE 32
#endif
static nvs_transfer_t i2c = { .word_addr_bytes = 2 };
static uint8_t getByte (uint32_t addr)
{
uint8_t value = 0;
i2c.address = EEPROM_I2C_ADDRESS;
i2c.word_addr = addr;
i2c.data = &value;
i2c.count = 1;
i2c_nvs_transfer(&i2c, true);
return value;
}
static void putByte (uint32_t addr, uint8_t new_value)
{
i2c.address = EEPROM_I2C_ADDRESS;
i2c.word_addr = addr;
i2c.data = &new_value;
i2c.count = 1;
i2c_nvs_transfer(&i2c, false);
}
static nvs_transfer_result_t writeBlock (uint32_t destination, uint8_t *source, uint32_t size, bool with_checksum)
{
uint32_t remaining = size;
uint8_t *target = source;
while(remaining > 0) {
i2c.address = EEPROM_I2C_ADDRESS;
i2c.word_addr = destination;
i2c.count = EEPROM_PAGE_SIZE - (destination & (EEPROM_PAGE_SIZE - 1));
i2c.count = remaining < i2c.count ? remaining : i2c.count;
i2c.data = target;
remaining -= i2c.count;
target += i2c.count;
destination += i2c.count;
i2c_nvs_transfer(&i2c, false);
}
if(size > 0 && with_checksum) {
uint16_t checksum = calc_checksum(source, size);
putByte(destination, checksum & 0xFF);
#if NVS_CRC_BYTES > 1
putByte(++destination, checksum >> 1);
#endif
}
return NVS_TransferResult_OK;
}
static nvs_transfer_result_t readBlock (uint8_t *destination, uint32_t source, uint32_t size, bool with_checksum)
{
uint32_t remaining = size;
uint8_t *target = destination;
while(remaining) {
i2c.address = EEPROM_I2C_ADDRESS;
i2c.word_addr = source;
i2c.count = remaining > 255 ? 255 : (uint8_t)remaining;
i2c.data = target;
remaining -= i2c.count;
target += i2c.count;
source += i2c.count;
i2c_nvs_transfer(&i2c, true);
}
#if NVS_CRC_BYTES == 1
return with_checksum ? (calc_checksum(destination, size) == getByte(source) ? NVS_TransferResult_OK : NVS_TransferResult_Failed) : NVS_TransferResult_OK;
#else
return with_checksum ? (calc_checksum(destination, size) == (getByte(source) | (getByte(source + 1) << 8)) ? NVS_TransferResult_OK : NVS_TransferResult_Failed) : NVS_TransferResult_OK;
#endif
}
void i2c_eeprom_init (void)
{
#if EEPROM_IS_FRAM
hal.nvs.type = NVS_FRAM;
#else
hal.nvs.type = NVS_EEPROM;
#endif
#if EEPROM_ENABLE >= 32
hal.nvs.size_max = (EEPROM_ENABLE / 8) * 1024;
#endif
hal.nvs.get_byte = getByte;
hal.nvs.put_byte = putByte;
hal.nvs.memcpy_to_nvs = writeBlock;
hal.nvs.memcpy_from_nvs = readBlock;
}
#endif