Skip to content

Commit d13ada1

Browse files
committed
Add customisable EEPROM driver selection (qmk#7274)
- uprintf -> dprintf - Fix atsam "vendor" eeprom. - Bump Kinetis K20x to 64 bytes, too. - Rollback Kinetis to 32 bytes as partitioning can only be done once. Add warning about changing the value. - Change RAM-backed "fake" EEPROM implementations to match eeconfig's current usage. - Add 24LC128 by request.
1 parent 6ff093e commit d13ada1

20 files changed

+616
-52
lines changed

Diff for: common_features.mk

+51
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,57 @@ ifeq ($(strip $(UNICODE_COMMON)), yes)
102102
SRC += $(QUANTUM_DIR)/process_keycode/process_unicode_common.c
103103
endif
104104

105+
VALID_EEPROM_DRIVER_TYPES := vendor custom transient i2c
106+
EEPROM_DRIVER ?= vendor
107+
ifeq ($(filter $(EEPROM_DRIVER),$(VALID_EEPROM_DRIVER_TYPES)),)
108+
$(error EEPROM_DRIVER="$(EEPROM_DRIVER)" is not a valid EEPROM driver)
109+
else
110+
OPT_DEFS += -DEEPROM_ENABLE
111+
ifeq ($(strip $(EEPROM_DRIVER)), custom)
112+
OPT_DEFS += -DEEPROM_DRIVER -DEEPROM_CUSTOM
113+
COMMON_VPATH += $(DRIVER_PATH)/eeprom
114+
SRC += eeprom_driver.c
115+
else ifeq ($(strip $(EEPROM_DRIVER)), i2c)
116+
OPT_DEFS += -DEEPROM_DRIVER -DEEPROM_I2C
117+
COMMON_VPATH += $(DRIVER_PATH)/eeprom
118+
QUANTUM_LIB_SRC += i2c_master.c
119+
SRC += eeprom_driver.c eeprom_i2c.c
120+
else ifeq ($(strip $(EEPROM_DRIVER)), transient)
121+
OPT_DEFS += -DEEPROM_DRIVER -DEEPROM_TRANSIENT
122+
COMMON_VPATH += $(DRIVER_PATH)/eeprom
123+
SRC += eeprom_driver.c eeprom_transient.c
124+
else ifeq ($(strip $(EEPROM_DRIVER)), vendor)
125+
OPT_DEFS += -DEEPROM_VENDOR
126+
ifeq ($(PLATFORM),AVR)
127+
# Automatically provided by avr-libc, nothing required
128+
else ifeq ($(PLATFORM),CHIBIOS)
129+
ifeq ($(MCU_SERIES), STM32F3xx)
130+
SRC += $(PLATFORM_COMMON_DIR)/eeprom_stm32.c
131+
SRC += $(PLATFORM_COMMON_DIR)/flash_stm32.c
132+
OPT_DEFS += -DEEPROM_EMU_STM32F303xC
133+
OPT_DEFS += -DSTM32_EEPROM_ENABLE
134+
else ifeq ($(MCU_SERIES), STM32F1xx)
135+
SRC += $(PLATFORM_COMMON_DIR)/eeprom_stm32.c
136+
SRC += $(PLATFORM_COMMON_DIR)/flash_stm32.c
137+
OPT_DEFS += -DEEPROM_EMU_STM32F103xB
138+
OPT_DEFS += -DSTM32_EEPROM_ENABLE
139+
else ifeq ($(MCU_SERIES)_$(MCU_LDSCRIPT), STM32F0xx_STM32F072xB)
140+
SRC += $(PLATFORM_COMMON_DIR)/eeprom_stm32.c
141+
SRC += $(PLATFORM_COMMON_DIR)/flash_stm32.c
142+
OPT_DEFS += -DEEPROM_EMU_STM32F072xB
143+
OPT_DEFS += -DSTM32_EEPROM_ENABLE
144+
else
145+
# This will effectively work the same as "transient" if not supported by the chip
146+
SRC += $(PLATFORM_COMMON_DIR)/eeprom_teensy.c
147+
endif
148+
else ifeq ($(PLATFORM),ARM_ATSAM)
149+
SRC += $(PLATFORM_COMMON_DIR)/eeprom.c
150+
else ifeq ($(PLATFORM),TEST)
151+
SRC += $(PLATFORM_COMMON_DIR)/eeprom.c
152+
endif
153+
endif
154+
endif
155+
105156
ifeq ($(strip $(RGBLIGHT_ENABLE)), yes)
106157
POST_CONFIG_H += $(QUANTUM_DIR)/rgblight_post_config.h
107158
OPT_DEFS += -DRGBLIGHT_ENABLE

Diff for: docs/_summary.md

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
* [ADC Driver](adc_driver.md)
105105
* [I2C Driver](i2c_driver.md)
106106
* [WS2812 Driver](ws2812_driver.md)
107+
* [EEPROM Driver](eeprom_driver.md)
107108
* [GPIO Controls](internals_gpio_control.md)
108109
* [Custom Matrix](custom_matrix.md)
109110
* [Proton C Conversion](proton_c_conversion.md)

Diff for: docs/eeprom_driver.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# EEPROM Driver Configuration
2+
3+
The EEPROM driver can be swapped out depending on the needs of the keyboard, or whether extra hardware is present.
4+
5+
Driver | Description
6+
--------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7+
`EEPROM_DRIVER = vendor` | Uses the on-chip driver provided by the chip manufacturer. For AVR, this is provided by avr-libc. This is supported on ARM for a subset of chips -- STM32F3xx, STM32F1xx, and STM32F072xB will be emulated by writing to flash. Other chips will generally act as "transient" below.
8+
`EEPROM_DRIVER = i2c` | Supports writing to I2C-based 24xx EEPROM chips. See the driver section below.
9+
`EEPROM_DRIVER = transient` | Fake EEPROM driver -- supports reading/writing to RAM, and will be discarded when power is lost.
10+
11+
## Vendor Driver Configuration
12+
13+
No configurable options are available.
14+
15+
## I2C Driver Configuration
16+
17+
Currently QMK supports 24xx-series chips over I2C. As such, requires a working i2c_master driver configuration. You can override the driver configuration via your config.h:
18+
19+
`config.h` override | Description | Default Value
20+
------------------------------------------- | ----------------------------------------------------------------------------------- | ------------------------------------
21+
`#define EXTERNAL_EEPROM_I2C_BASE_ADDRESS` | Base I2C address for the EEPROM -- shifted left by 1 as per i2c_master requirements | 0b10100000
22+
`#define EXTERNAL_EEPROM_I2C_ADDRESS(addr)` | Calculated I2C address for the EEPROM | `(EXTERNAL_EEPROM_I2C_BASE_ADDRESS)`
23+
`#define EXTERNAL_EEPROM_BYTE_COUNT` | Total size of the EEPROM in bytes | 8192
24+
`#define EXTERNAL_EEPROM_PAGE_SIZE` | Page size of the EEPROM in bytes, as specified in the datasheet | 32
25+
`#define EXTERNAL_EEPROM_ADDRESS_SIZE` | The number of bytes to transmit for the memory location within the EEPROM | 2
26+
`#define EXTERNAL_EEPROM_WRITE_TIME` | Write cycle time of the EEPROM, as specified in the datasheet | 5
27+
28+
Default values and extended descriptions can be found in `drivers/eeprom/eeprom_i2c.h`.
29+
30+
Alternatively, there are pre-defined hardware configurations for available chips/modules:
31+
32+
Module | Equivalent `#define` | Source
33+
-----------------|---------------------------------|------------------------------------------
34+
CAT24C512 EEPROM | `#define EEPROM_I2C_CAT24C512` | <https://www.sparkfun.com/products/14764>
35+
RM24C512C EEPROM | `#define EEPROM_I2C_RM24C512C` | <https://www.sparkfun.com/products/14764>
36+
24LC128 EEPROM | `#define EEPROM_I2C_24LC128` | <https://www.microchip.com/wwwproducts/en/24LC128>
37+
24LC256 EEPROM | `#define EEPROM_I2C_24LC256` | <https://www.sparkfun.com/products/525>
38+
MB85RC256V FRAM | `#define EEPROM_I2C_MB85RC256V` | <https://www.adafruit.com/product/1895>
39+
40+
?> If you find that the EEPROM is not cooperating, ensure you've correctly shifted up your EEPROM address by 1. For example, the datasheet might state the address as `0b01010000` -- the correct value of `EXTERNAL_EEPROM_I2C_BASE_ADDRESS` needs to be `0b10100000`.
41+
42+
## Transient Driver configuration
43+
44+
The only configurable item for the transient EEPROM driver is its size:
45+
46+
`config.h` override | Description | Default Value
47+
------------------------------- | ----------------------------------------- | -------------
48+
`#define TRANSIENT_EEPROM_SIZE` | Total size of the EEPROM storage in bytes | 64
49+
50+
Default values and extended descriptions can be found in `drivers/eeprom/eeprom_transient.h`.

Diff for: docs/hardware_drivers.md

+4
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ Support for up to 2 drivers. Each driver impliments 2 charlieplex matrices to in
3333
## IS31FL3733
3434

3535
Support for up to a single driver with room for expansion. Each driver can control 192 individual LEDs or 64 RGB LEDs. For more information on how to setup the driver see the [RGB Matrix](feature_rgb_matrix.md) page.
36+
37+
## 24xx series external I2C EEPROM
38+
39+
Support for an external I2C-based EEPROM instead of using the on-chip EEPROM. For more information on how to setup the driver see the [EEPROM Driver](eeprom_driver.md) page.

Diff for: drivers/eeprom/eeprom_custom.c-template

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright 2019 Nick Brassel (tzarc)
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include <stdint.h>
18+
#include <string.h>
19+
20+
#include "eeprom_driver.h"
21+
22+
void eeprom_driver_init(void) {
23+
/* Any initialisation code */
24+
}
25+
26+
void eeprom_driver_erase(void) {
27+
/* Wipe out the EEPROM, setting values to zero */
28+
}
29+
30+
void eeprom_read_block(void *buf, const void *addr, size_t len) {
31+
/*
32+
Read a block of data:
33+
buf: target buffer
34+
addr: 0-based offset within the EEPROM
35+
len: length to read
36+
*/
37+
}
38+
39+
void eeprom_write_block(const void *buf, void *addr, size_t len) {
40+
/*
41+
Write a block of data:
42+
buf: target buffer
43+
addr: 0-based offset within the EEPROM
44+
len: length to write
45+
*/
46+
}

Diff for: drivers/eeprom/eeprom_driver.c

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* Copyright 2019 Nick Brassel (tzarc)
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include <stdint.h>
18+
#include <string.h>
19+
20+
#include "eeprom_driver.h"
21+
22+
uint8_t eeprom_read_byte(const uint8_t *addr) {
23+
uint8_t ret;
24+
eeprom_read_block(&ret, addr, 1);
25+
return ret;
26+
}
27+
28+
uint16_t eeprom_read_word(const uint16_t *addr) {
29+
uint16_t ret;
30+
eeprom_read_block(&ret, addr, 2);
31+
return ret;
32+
}
33+
34+
uint32_t eeprom_read_dword(const uint32_t *addr) {
35+
uint32_t ret;
36+
eeprom_read_block(&ret, addr, 4);
37+
return ret;
38+
}
39+
40+
void eeprom_write_byte(uint8_t *addr, uint8_t value) { eeprom_write_block(&value, addr, 1); }
41+
42+
void eeprom_write_word(uint16_t *addr, uint16_t value) { eeprom_write_block(&value, addr, 2); }
43+
44+
void eeprom_write_dword(uint32_t *addr, uint32_t value) { eeprom_write_block(&value, addr, 4); }
45+
46+
void eeprom_update_block(const void *buf, void *addr, size_t len) {
47+
uint8_t read_buf[len];
48+
eeprom_read_block(read_buf, addr, len);
49+
if (memcmp(buf, read_buf, len) != 0) {
50+
eeprom_write_block(buf, addr, len);
51+
}
52+
}
53+
54+
void eeprom_update_byte(uint8_t *addr, uint8_t value) {
55+
uint8_t orig = eeprom_read_byte(addr);
56+
if (orig != value) {
57+
eeprom_write_byte(addr, value);
58+
}
59+
}
60+
61+
void eeprom_update_word(uint16_t *addr, uint16_t value) {
62+
uint16_t orig = eeprom_read_word(addr);
63+
if (orig != value) {
64+
eeprom_write_word(addr, value);
65+
}
66+
}
67+
68+
void eeprom_update_dword(uint32_t *addr, uint32_t value) {
69+
uint32_t orig = eeprom_read_dword(addr);
70+
if (orig != value) {
71+
eeprom_write_dword(addr, value);
72+
}
73+
}

Diff for: drivers/eeprom/eeprom_driver.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Copyright 2019 Nick Brassel (tzarc)
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#pragma once
18+
19+
#include "eeprom.h"
20+
21+
void eeprom_driver_init(void);
22+
void eeprom_driver_erase(void);

Diff for: drivers/eeprom/eeprom_i2c.c

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* Copyright 2019 Nick Brassel (tzarc)
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include <stdint.h>
18+
#include <string.h>
19+
20+
/*
21+
Note that the implementations of eeprom_XXXX_YYYY on AVR are normally
22+
provided by avr-libc. The same functions are reimplemented below and are
23+
rerouted to the external i2c equivalent.
24+
25+
Seemingly, as this is compiled from within QMK, the object file generated
26+
during the build overrides the avr-libc implementation during the linking
27+
stage.
28+
29+
On other platforms such as ARM, there are no provided implementations, so
30+
there is nothing to override during linkage.
31+
*/
32+
33+
#include "wait.h"
34+
#include "i2c_master.h"
35+
#include "eeprom.h"
36+
#include "eeprom_i2c.h"
37+
38+
// #define DEBUG_EEPROM_OUTPUT
39+
40+
#ifdef DEBUG_EEPROM_OUTPUT
41+
# include "print.h"
42+
#endif // DEBUG_EEPROM_OUTPUT
43+
44+
static inline void init_i2c_if_required(void) {
45+
static int done = 0;
46+
if (!done) {
47+
i2c_init();
48+
done = 1;
49+
}
50+
}
51+
52+
static inline void fill_target_address(uint8_t *buffer, const void *addr) {
53+
intptr_t p = (intptr_t)addr;
54+
for (int i = 0; i < EXTERNAL_EEPROM_ADDRESS_SIZE; ++i) {
55+
buffer[EXTERNAL_EEPROM_ADDRESS_SIZE - 1 - i] = p & 0xFF;
56+
p >>= 8;
57+
}
58+
}
59+
60+
void eeprom_driver_init(void) {}
61+
62+
void eeprom_driver_erase(void) {
63+
uint8_t buf[EXTERNAL_EEPROM_PAGE_SIZE];
64+
memset(buf, 0x00, EXTERNAL_EEPROM_PAGE_SIZE);
65+
for (intptr_t addr = 0; addr < EXTERNAL_EEPROM_BYTE_COUNT; addr += EXTERNAL_EEPROM_PAGE_SIZE) {
66+
eeprom_write_block(buf, (void *)addr, EXTERNAL_EEPROM_PAGE_SIZE);
67+
}
68+
}
69+
70+
void eeprom_read_block(void *buf, const void *addr, size_t len) {
71+
uint8_t complete_packet[EXTERNAL_EEPROM_ADDRESS_SIZE];
72+
fill_target_address(complete_packet, addr);
73+
74+
init_i2c_if_required();
75+
i2c_transmit(EXTERNAL_EEPROM_I2C_ADDRESS((intptr_t)addr), complete_packet, EXTERNAL_EEPROM_ADDRESS_SIZE, 100);
76+
i2c_receive(EXTERNAL_EEPROM_I2C_ADDRESS((intptr_t)addr), buf, len, 100);
77+
78+
#ifdef DEBUG_EEPROM_OUTPUT
79+
dprintf("[EEPROM R] 0x%04X: ", ((int)addr));
80+
for (size_t i = 0; i < len; ++i) {
81+
dprintf(" %02X", (int)(((uint8_t *)buf)[i]));
82+
}
83+
dprintf("\n");
84+
#endif // DEBUG_EEPROM_OUTPUT
85+
}
86+
87+
void eeprom_write_block(const void *buf, void *addr, size_t len) {
88+
uint8_t complete_packet[EXTERNAL_EEPROM_ADDRESS_SIZE + EXTERNAL_EEPROM_PAGE_SIZE];
89+
uint8_t *read_buf = (uint8_t *)buf;
90+
intptr_t target_addr = (intptr_t)addr;
91+
92+
init_i2c_if_required();
93+
while (len > 0) {
94+
intptr_t page_offset = target_addr % EXTERNAL_EEPROM_PAGE_SIZE;
95+
int write_length = EXTERNAL_EEPROM_PAGE_SIZE - page_offset;
96+
if (write_length > len) {
97+
write_length = len;
98+
}
99+
100+
fill_target_address(complete_packet, (const void *)target_addr);
101+
for (uint8_t i = 0; i < write_length; i++) {
102+
complete_packet[EXTERNAL_EEPROM_ADDRESS_SIZE + i] = read_buf[i];
103+
}
104+
105+
#ifdef DEBUG_EEPROM_OUTPUT
106+
dprintf("[EEPROM W] 0x%04X: ", ((int)target_addr));
107+
for (uint8_t i = 0; i < write_length; i++) {
108+
dprintf(" %02X", (int)(read_buf[i]));
109+
}
110+
dprintf("\n");
111+
#endif // DEBUG_EEPROM_OUTPUT
112+
113+
i2c_transmit(EXTERNAL_EEPROM_I2C_ADDRESS((intptr_t)addr), complete_packet, EXTERNAL_EEPROM_ADDRESS_SIZE + write_length, 100);
114+
wait_ms(EXTERNAL_EEPROM_WRITE_TIME);
115+
116+
read_buf += write_length;
117+
target_addr += write_length;
118+
len -= write_length;
119+
}
120+
}

0 commit comments

Comments
 (0)