-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheeprom_24LC16B.c
143 lines (109 loc) · 3.79 KB
/
eeprom_24LC16B.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
137
138
139
140
141
142
143
/*
eeprom_24LC16B.c - plugin for for I2C EEPROM (Microchip 24LC16B)
Part of grblHAL
Copyright (c) 2017-2025 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 == 1 || EEPROM_ENABLE == 16
#include "eeprom.h"
#include "grbl/hal.h"
#include "grbl/crc.h"
#include "grbl/nuts_bolts.h"
#define EEPROM_I2C_ADDRESS (0xA0 >> 1)
#define EEPROM_ADDR_BITS_LO 8
#define EEPROM_BLOCK_SIZE (2 ^ EEPROM_LO_ADDR_BITS)
#define EEPROM_PAGE_SIZE 16
static nvs_transfer_t i2c = { .word_addr_bytes = 1 };
static uint8_t getByte (uint32_t addr)
{
uint8_t value = 0;
i2c.address = EEPROM_I2C_ADDRESS | (addr >> 8);
i2c.word_addr = addr & 0xFF;
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 | (addr >> 8);
i2c.word_addr = addr & 0xFF;
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;
nvs_transfer_result_t res = NVS_TransferResult_OK;
while(remaining > 0) {
i2c.address = EEPROM_I2C_ADDRESS | (destination >> EEPROM_ADDR_BITS_LO);
i2c.word_addr = destination & 0xFF;
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;
if((res = i2c_nvs_transfer(&i2c, false)) != NVS_TransferResult_OK)
break;
eeprom_write_delay();
}
if(res == NVS_TransferResult_OK && size > 0 && with_checksum) {
uint16_t checksum = calc_checksum(source, size);
putByte(destination, checksum & 0xFF);
#if NVS_CRC_BYTES > 1
eeprom_write_delay();
putByte(++destination, checksum >> 8);
#endif
eeprom_write_delay();
}
return res;
}
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 | (source >> 8);
i2c.word_addr = source & 0xFF;
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);
#else
return !with_checksum || calc_checksum(destination, size) == (getByte(source) | (getByte(source + 1) << 8));
#endif
}
void i2c_eeprom_init (void)
{
if(i2c_start().ok && i2c_probe(EEPROM_I2C_ADDRESS)) {
#if EEPROM_IS_FRAM
hal.nvs.type = NVS_FRAM;
#else
hal.nvs.type = NVS_EEPROM;
#endif
hal.nvs.size_max = 2048;
hal.nvs.get_byte = getByte;
hal.nvs.put_byte = putByte;
hal.nvs.memcpy_to_nvs = writeBlock;
hal.nvs.memcpy_from_nvs = readBlock;
}
}
#endif