diff options
-rw-r--r-- | common_features.mk | 51 | ||||
-rw-r--r-- | docs/_summary.md | 1 | ||||
-rw-r--r-- | docs/eeprom_driver.md | 50 | ||||
-rw-r--r-- | docs/hardware_drivers.md | 4 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_custom.c-template | 46 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_driver.c | 73 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_driver.h | 22 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_i2c.c | 120 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_i2c.h | 115 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_transient.c | 52 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_transient.h | 25 | ||||
-rw-r--r-- | quantum/audio/audio_arm.c | 2 | ||||
-rw-r--r-- | quantum/rgblight.c | 8 | ||||
-rw-r--r-- | tmk_core/common.mk | 30 | ||||
-rw-r--r-- | tmk_core/common/arm_atsam/eeprom.c | 13 | ||||
-rw-r--r-- | tmk_core/common/chibios/eeprom_stm32.c | 6 | ||||
-rw-r--r-- | tmk_core/common/chibios/eeprom_teensy.c | 25 | ||||
-rw-r--r-- | tmk_core/common/eeconfig.c | 10 | ||||
-rw-r--r-- | tmk_core/common/eeprom.h | 9 | ||||
-rw-r--r-- | tmk_core/common/test/eeprom.c | 6 |
20 files changed, 616 insertions, 52 deletions
diff --git a/common_features.mk b/common_features.mk index 6c3646db94..900b27c46c 100644 --- a/common_features.mk +++ b/common_features.mk @@ -102,6 +102,57 @@ ifeq ($(strip $(UNICODE_COMMON)), yes) SRC += $(QUANTUM_DIR)/process_keycode/process_unicode_common.c endif +VALID_EEPROM_DRIVER_TYPES := vendor custom transient i2c +EEPROM_DRIVER ?= vendor +ifeq ($(filter $(EEPROM_DRIVER),$(VALID_EEPROM_DRIVER_TYPES)),) + $(error EEPROM_DRIVER="$(EEPROM_DRIVER)" is not a valid EEPROM driver) +else + OPT_DEFS += -DEEPROM_ENABLE + ifeq ($(strip $(EEPROM_DRIVER)), custom) + OPT_DEFS += -DEEPROM_DRIVER -DEEPROM_CUSTOM + COMMON_VPATH += $(DRIVER_PATH)/eeprom + SRC += eeprom_driver.c + else ifeq ($(strip $(EEPROM_DRIVER)), i2c) + OPT_DEFS += -DEEPROM_DRIVER -DEEPROM_I2C + COMMON_VPATH += $(DRIVER_PATH)/eeprom + QUANTUM_LIB_SRC += i2c_master.c + SRC += eeprom_driver.c eeprom_i2c.c + else ifeq ($(strip $(EEPROM_DRIVER)), transient) + OPT_DEFS += -DEEPROM_DRIVER -DEEPROM_TRANSIENT + COMMON_VPATH += $(DRIVER_PATH)/eeprom + SRC += eeprom_driver.c eeprom_transient.c + else ifeq ($(strip $(EEPROM_DRIVER)), vendor) + OPT_DEFS += -DEEPROM_VENDOR + ifeq ($(PLATFORM),AVR) + # Automatically provided by avr-libc, nothing required + else ifeq ($(PLATFORM),CHIBIOS) + ifeq ($(MCU_SERIES), STM32F3xx) + SRC += $(PLATFORM_COMMON_DIR)/eeprom_stm32.c + SRC += $(PLATFORM_COMMON_DIR)/flash_stm32.c + OPT_DEFS += -DEEPROM_EMU_STM32F303xC + OPT_DEFS += -DSTM32_EEPROM_ENABLE + else ifeq ($(MCU_SERIES), STM32F1xx) + SRC += $(PLATFORM_COMMON_DIR)/eeprom_stm32.c + SRC += $(PLATFORM_COMMON_DIR)/flash_stm32.c + OPT_DEFS += -DEEPROM_EMU_STM32F103xB + OPT_DEFS += -DSTM32_EEPROM_ENABLE + else ifeq ($(MCU_SERIES)_$(MCU_LDSCRIPT), STM32F0xx_STM32F072xB) + SRC += $(PLATFORM_COMMON_DIR)/eeprom_stm32.c + SRC += $(PLATFORM_COMMON_DIR)/flash_stm32.c + OPT_DEFS += -DEEPROM_EMU_STM32F072xB + OPT_DEFS += -DSTM32_EEPROM_ENABLE + else + # This will effectively work the same as "transient" if not supported by the chip + SRC += $(PLATFORM_COMMON_DIR)/eeprom_teensy.c + endif + else ifeq ($(PLATFORM),ARM_ATSAM) + SRC += $(PLATFORM_COMMON_DIR)/eeprom.c + else ifeq ($(PLATFORM),TEST) + SRC += $(PLATFORM_COMMON_DIR)/eeprom.c + endif + endif +endif + ifeq ($(strip $(RGBLIGHT_ENABLE)), yes) POST_CONFIG_H += $(QUANTUM_DIR)/rgblight_post_config.h OPT_DEFS += -DRGBLIGHT_ENABLE diff --git a/docs/_summary.md b/docs/_summary.md index de81481382..08ccdca220 100644 --- a/docs/_summary.md +++ b/docs/_summary.md @@ -104,6 +104,7 @@ * [ADC Driver](adc_driver.md) * [I2C Driver](i2c_driver.md) * [WS2812 Driver](ws2812_driver.md) + * [EEPROM Driver](eeprom_driver.md) * [GPIO Controls](internals_gpio_control.md) * [Custom Matrix](custom_matrix.md) * [Proton C Conversion](proton_c_conversion.md) diff --git a/docs/eeprom_driver.md b/docs/eeprom_driver.md new file mode 100644 index 0000000000..dd12d8ec7b --- /dev/null +++ b/docs/eeprom_driver.md @@ -0,0 +1,50 @@ +# EEPROM Driver Configuration + +The EEPROM driver can be swapped out depending on the needs of the keyboard, or whether extra hardware is present. + +Driver | Description +--------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +`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. +`EEPROM_DRIVER = i2c` | Supports writing to I2C-based 24xx EEPROM chips. See the driver section below. +`EEPROM_DRIVER = transient` | Fake EEPROM driver -- supports reading/writing to RAM, and will be discarded when power is lost. + +## Vendor Driver Configuration + +No configurable options are available. + +## I2C Driver Configuration + +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: + +`config.h` override | Description | Default Value +------------------------------------------- | ----------------------------------------------------------------------------------- | ------------------------------------ +`#define EXTERNAL_EEPROM_I2C_BASE_ADDRESS` | Base I2C address for the EEPROM -- shifted left by 1 as per i2c_master requirements | 0b10100000 +`#define EXTERNAL_EEPROM_I2C_ADDRESS(addr)` | Calculated I2C address for the EEPROM | `(EXTERNAL_EEPROM_I2C_BASE_ADDRESS)` +`#define EXTERNAL_EEPROM_BYTE_COUNT` | Total size of the EEPROM in bytes | 8192 +`#define EXTERNAL_EEPROM_PAGE_SIZE` | Page size of the EEPROM in bytes, as specified in the datasheet | 32 +`#define EXTERNAL_EEPROM_ADDRESS_SIZE` | The number of bytes to transmit for the memory location within the EEPROM | 2 +`#define EXTERNAL_EEPROM_WRITE_TIME` | Write cycle time of the EEPROM, as specified in the datasheet | 5 + +Default values and extended descriptions can be found in `drivers/eeprom/eeprom_i2c.h`. + +Alternatively, there are pre-defined hardware configurations for available chips/modules: + +Module | Equivalent `#define` | Source +-----------------|---------------------------------|------------------------------------------ +CAT24C512 EEPROM | `#define EEPROM_I2C_CAT24C512` | <https://www.sparkfun.com/products/14764> +RM24C512C EEPROM | `#define EEPROM_I2C_RM24C512C` | <https://www.sparkfun.com/products/14764> +24LC128 EEPROM | `#define EEPROM_I2C_24LC128` | <https://www.microchip.com/wwwproducts/en/24LC128> +24LC256 EEPROM | `#define EEPROM_I2C_24LC256` | <https://www.sparkfun.com/products/525> +MB85RC256V FRAM | `#define EEPROM_I2C_MB85RC256V` | <https://www.adafruit.com/product/1895> + +?> 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`. + +## Transient Driver configuration + +The only configurable item for the transient EEPROM driver is its size: + +`config.h` override | Description | Default Value +------------------------------- | ----------------------------------------- | ------------- +`#define TRANSIENT_EEPROM_SIZE` | Total size of the EEPROM storage in bytes | 64 + +Default values and extended descriptions can be found in `drivers/eeprom/eeprom_transient.h`. diff --git a/docs/hardware_drivers.md b/docs/hardware_drivers.md index 1651877957..7e89c0d2b9 100644 --- a/docs/hardware_drivers.md +++ b/docs/hardware_drivers.md @@ -33,3 +33,7 @@ Support for up to 2 drivers. Each driver impliments 2 charlieplex matrices to in ## IS31FL3733 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. + +## 24xx series external I2C EEPROM + +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 --git a/drivers/eeprom/eeprom_custom.c-template b/drivers/eeprom/eeprom_custom.c-template new file mode 100644 index 0000000000..5f915f7fab --- /dev/null +++ b/drivers/eeprom/eeprom_custom.c-template @@ -0,0 +1,46 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdint.h> +#include <string.h> + +#include "eeprom_driver.h" + +void eeprom_driver_init(void) { + /* Any initialisation code */ + } + +void eeprom_driver_erase(void) { + /* Wipe out the EEPROM, setting values to zero */ +} + +void eeprom_read_block(void *buf, const void *addr, size_t len) { + /* + Read a block of data: + buf: target buffer + addr: 0-based offset within the EEPROM + len: length to read + */ +} + +void eeprom_write_block(const void *buf, void *addr, size_t len) { + /* + Write a block of data: + buf: target buffer + addr: 0-based offset within the EEPROM + len: length to write + */ +} diff --git a/drivers/eeprom/eeprom_driver.c b/drivers/eeprom/eeprom_driver.c new file mode 100644 index 0000000000..3835e5e9d0 --- /dev/null +++ b/drivers/eeprom/eeprom_driver.c @@ -0,0 +1,73 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdint.h> +#include <string.h> + +#include "eeprom_driver.h" + +uint8_t eeprom_read_byte(const uint8_t *addr) { + uint8_t ret; + eeprom_read_block(&ret, addr, 1); + return ret; +} + +uint16_t eeprom_read_word(const uint16_t *addr) { + uint16_t ret; + eeprom_read_block(&ret, addr, 2); + return ret; +} + +uint32_t eeprom_read_dword(const uint32_t *addr) { + uint32_t ret; + eeprom_read_block(&ret, addr, 4); + return ret; +} + +void eeprom_write_byte(uint8_t *addr, uint8_t value) { eeprom_write_block(&value, addr, 1); } + +void eeprom_write_word(uint16_t *addr, uint16_t value) { eeprom_write_block(&value, addr, 2); } + +void eeprom_write_dword(uint32_t *addr, uint32_t value) { eeprom_write_block(&value, addr, 4); } + +void eeprom_update_block(const void *buf, void *addr, size_t len) { + uint8_t read_buf[len]; + eeprom_read_block(read_buf, addr, len); + if (memcmp(buf, read_buf, len) != 0) { + eeprom_write_block(buf, addr, len); + } +} + +void eeprom_update_byte(uint8_t *addr, uint8_t value) { + uint8_t orig = eeprom_read_byte(addr); + if (orig != value) { + eeprom_write_byte(addr, value); + } +} + +void eeprom_update_word(uint16_t *addr, uint16_t value) { + uint16_t orig = eeprom_read_word(addr); + if (orig != value) { + eeprom_write_word(addr, value); + } +} + +void eeprom_update_dword(uint32_t *addr, uint32_t value) { + uint32_t orig = eeprom_read_dword(addr); + if (orig != value) { + eeprom_write_dword(addr, value); + } +} diff --git a/drivers/eeprom/eeprom_driver.h b/drivers/eeprom/eeprom_driver.h new file mode 100644 index 0000000000..74592bc8f0 --- /dev/null +++ b/drivers/eeprom/eeprom_driver.h @@ -0,0 +1,22 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "eeprom.h" + +void eeprom_driver_init(void); +void eeprom_driver_erase(void); diff --git a/drivers/eeprom/eeprom_i2c.c b/drivers/eeprom/eeprom_i2c.c new file mode 100644 index 0000000000..03dbc5e516 --- /dev/null +++ b/drivers/eeprom/eeprom_i2c.c @@ -0,0 +1,120 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdint.h> +#include <string.h> + +/* + Note that the implementations of eeprom_XXXX_YYYY on AVR are normally + provided by avr-libc. The same functions are reimplemented below and are + rerouted to the external i2c equivalent. + + Seemingly, as this is compiled from within QMK, the object file generated + during the build overrides the avr-libc implementation during the linking + stage. + + On other platforms such as ARM, there are no provided implementations, so + there is nothing to override during linkage. +*/ + +#include "wait.h" +#include "i2c_master.h" +#include "eeprom.h" +#include "eeprom_i2c.h" + +// #define DEBUG_EEPROM_OUTPUT + +#ifdef DEBUG_EEPROM_OUTPUT +# include "print.h" +#endif // DEBUG_EEPROM_OUTPUT + +static inline void init_i2c_if_required(void) { + static int done = 0; + if (!done) { + i2c_init(); + done = 1; + } +} + +static inline void fill_target_address(uint8_t *buffer, const void *addr) { + intptr_t p = (intptr_t)addr; + for (int i = 0; i < EXTERNAL_EEPROM_ADDRESS_SIZE; ++i) { + buffer[EXTERNAL_EEPROM_ADDRESS_SIZE - 1 - i] = p & 0xFF; + p >>= 8; + } +} + +void eeprom_driver_init(void) {} + +void eeprom_driver_erase(void) { + uint8_t buf[EXTERNAL_EEPROM_PAGE_SIZE]; + memset(buf, 0x00, EXTERNAL_EEPROM_PAGE_SIZE); + for (intptr_t addr = 0; addr < EXTERNAL_EEPROM_BYTE_COUNT; addr += EXTERNAL_EEPROM_PAGE_SIZE) { + eeprom_write_block(buf, (void *)addr, EXTERNAL_EEPROM_PAGE_SIZE); + } +} + +void eeprom_read_block(void *buf, const void *addr, size_t len) { + uint8_t complete_packet[EXTERNAL_EEPROM_ADDRESS_SIZE]; + fill_target_address(complete_packet, addr); + + init_i2c_if_required(); + i2c_transmit(EXTERNAL_EEPROM_I2C_ADDRESS((intptr_t)addr), complete_packet, EXTERNAL_EEPROM_ADDRESS_SIZE, 100); + i2c_receive(EXTERNAL_EEPROM_I2C_ADDRESS((intptr_t)addr), buf, len, 100); + +#ifdef DEBUG_EEPROM_OUTPUT + dprintf("[EEPROM R] 0x%04X: ", ((int)addr)); + for (size_t i = 0; i < len; ++i) { + dprintf(" %02X", (int)(((uint8_t *)buf)[i])); + } + dprintf("\n"); +#endif // DEBUG_EEPROM_OUTPUT +} + +void eeprom_write_block(const void *buf, void *addr, size_t len) { + uint8_t complete_packet[EXTERNAL_EEPROM_ADDRESS_SIZE + EXTERNAL_EEPROM_PAGE_SIZE]; + uint8_t *read_buf = (uint8_t *)buf; + intptr_t target_addr = (intptr_t)addr; + + init_i2c_if_required(); + while (len > 0) { + intptr_t page_offset = target_addr % EXTERNAL_EEPROM_PAGE_SIZE; + int write_length = EXTERNAL_EEPROM_PAGE_SIZE - page_offset; + if (write_length > len) { + write_length = len; + } + + fill_target_address(complete_packet, (const void *)target_addr); + for (uint8_t i = 0; i < write_length; i++) { + complete_packet[EXTERNAL_EEPROM_ADDRESS_SIZE + i] = read_buf[i]; + } + +#ifdef DEBUG_EEPROM_OUTPUT + dprintf("[EEPROM W] 0x%04X: ", ((int)target_addr)); + for (uint8_t i = 0; i < write_length; i++) { + dprintf(" %02X", (int)(read_buf[i])); + } + dprintf("\n"); +#endif // DEBUG_EEPROM_OUTPUT + + i2c_transmit(EXTERNAL_EEPROM_I2C_ADDRESS((intptr_t)addr), complete_packet, EXTERNAL_EEPROM_ADDRESS_SIZE + write_length, 100); + wait_ms(EXTERNAL_EEPROM_WRITE_TIME); + + read_buf += write_length; + target_addr += write_length; + len -= write_length; + } +} diff --git a/drivers/eeprom/eeprom_i2c.h b/drivers/eeprom/eeprom_i2c.h new file mode 100644 index 0000000000..51bce825b0 --- /dev/null +++ b/drivers/eeprom/eeprom_i2c.h @@ -0,0 +1,115 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +/* + Default device configurations: + + For the Sparkfun Qwiic I2C EEPROM module: https://www.sparkfun.com/products/14764 + #define EEPROM_I2C_CAT24C512 // (part number 24512A) + #define EEPROM_I2C_RM24C512C // (part number 24512C) + + For the Sparkfun I2C EEPROM chip: https://www.sparkfun.com/products/525 + #define EEPROM_I2C_24LC256 + + For the Adafruit I2C FRAM chip: https://www.adafruit.com/product/1895 + #define EEPROM_I2C_MB85RC256V +*/ +#if defined(EEPROM_I2C_CAT24C512) +# define EXTERNAL_EEPROM_BYTE_COUNT 65536 +# define EXTERNAL_EEPROM_PAGE_SIZE 128 +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +# define EXTERNAL_EEPROM_WRITE_TIME 5 +#elif defined(EEPROM_I2C_RM24C512C) +# define EXTERNAL_EEPROM_BYTE_COUNT 65536 +# define EXTERNAL_EEPROM_PAGE_SIZE 128 +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +# define EXTERNAL_EEPROM_WRITE_TIME 3 +#elif defined(EEPROM_I2C_24LC256) +# define EXTERNAL_EEPROM_BYTE_COUNT 32768 +# define EXTERNAL_EEPROM_PAGE_SIZE 64 +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +# define EXTERNAL_EEPROM_WRITE_TIME 5 +#elif defined(EEPROM_I2C_24LC128) +# define EXTERNAL_EEPROM_BYTE_COUNT 16384 +# define EXTERNAL_EEPROM_PAGE_SIZE 64 +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +# define EXTERNAL_EEPROM_WRITE_TIME 5 +#elif defined(EEPROM_I2C_MB85RC256V) +# define EXTERNAL_EEPROM_BYTE_COUNT 32768 +# define EXTERNAL_EEPROM_PAGE_SIZE 128 +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +# define EXTERNAL_EEPROM_WRITE_TIME 0 +#endif + +/* + The base I2C address of the EEPROM. + This needs to be shifted up by 1, to match i2c_master requirements. +*/ +#ifndef EXTERNAL_EEPROM_I2C_BASE_ADDRESS +# define EXTERNAL_EEPROM_I2C_BASE_ADDRESS 0b10100000 +#endif + +/* + The calculated I2C address based on the input memory location. + + For EEPROM chips that embed part of the memory location in the I2C address + such as AT24M02 you can use something similar to the following (ensuring the + result is shifted by left by 1): + + #define EXTERNAL_EEPROM_I2C_ADDRESS(loc) \ + (EXTERNAL_EEPROM_I2C_BASE_ADDRESS | ((((loc) >> 16) & 0x07) << 1)) + +*/ +#ifndef EXTERNAL_EEPROM_I2C_ADDRESS +# define EXTERNAL_EEPROM_I2C_ADDRESS(loc) (EXTERNAL_EEPROM_I2C_BASE_ADDRESS) +#endif + +/* + The total size of the EEPROM, in bytes. The EEPROM datasheet will usually + specify this value in kbits, and will require conversion to bytes. +*/ +#ifndef EXTERNAL_EEPROM_BYTE_COUNT +# define EXTERNAL_EEPROM_BYTE_COUNT 8192 +#endif + +/* + The page size in bytes of the EEPROM, as specified in the datasheet. +*/ +#ifndef EXTERNAL_EEPROM_PAGE_SIZE +# define EXTERNAL_EEPROM_PAGE_SIZE 32 +#endif + +/* + The address size in bytes of the EEPROM. For EEPROMs with <=256 bytes, this + will likely be 1. For EEPROMs >256 and <=65536, this will be 2. For EEPROMs + >65536, this will likely need to be 2 with the modified variant of + EXTERNAL_EEPROM_I2C_ADDRESS above. + + As expected, consult the datasheet for specifics of your EEPROM. +*/ +#ifndef EXTERNAL_EEPROM_ADDRESS_SIZE +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +#endif + +/* + The write cycle time of the EEPROM in milliseconds, as specified in the + datasheet. +*/ +#ifndef EXTERNAL_EEPROM_WRITE_TIME +# define EXTERNAL_EEPROM_WRITE_TIME 5 +#endif diff --git a/drivers/eeprom/eeprom_transient.c b/drivers/eeprom/eeprom_transient.c new file mode 100644 index 0000000000..b4c78c6f40 --- /dev/null +++ b/drivers/eeprom/eeprom_transient.c @@ -0,0 +1,52 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdint.h> +#include <string.h> + +#include "eeprom_driver.h" +#include "eeprom_transient.h" + +__attribute__((aligned(4))) static uint8_t transientBuffer[TRANSIENT_EEPROM_SIZE] = {0}; + +size_t clamp_length(intptr_t offset, size_t len) { + if (offset + len > TRANSIENT_EEPROM_SIZE) { + len = TRANSIENT_EEPROM_SIZE - offset; + } + + return len; +} + +void eeprom_driver_init(void) { eeprom_driver_erase(); } + +void eeprom_driver_erase(void) { memset(transientBuffer, 0x00, TRANSIENT_EEPROM_SIZE); } + +void eeprom_read_block(void *buf, const void *addr, size_t len) { + intptr_t offset = (intptr_t)addr; + memset(buf, 0x00, len); + len = clamp_length(offset, len); + if (len > 0) { + memcpy(buf, &transientBuffer[offset], len); + } +} + +void eeprom_write_block(const void *buf, void *addr, size_t len) { + intptr_t offset = (intptr_t)addr; + len = clamp_length(offset, len); + if (len > 0) { + memcpy(&transientBuffer[offset], buf, len); + } +} diff --git a/drivers/eeprom/eeprom_transient.h b/drivers/eeprom/eeprom_transient.h new file mode 100644 index 0000000000..ca9d634f06 --- /dev/null +++ b/drivers/eeprom/eeprom_transient.h @@ -0,0 +1,25 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +/* + The size of the transient EEPROM buffer size. +*/ +#ifndef TRANSIENT_EEPROM_SIZE +# include "eeconfig.h" +# define TRANSIENT_EEPROM_SIZE (((EECONFIG_SIZE+3)/4)*4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO +#endif diff --git a/quantum/audio/audio_arm.c b/quantum/audio/audio_arm.c index 87d625301f..e4483c3028 100644 --- a/quantum/audio/audio_arm.c +++ b/quantum/audio/audio_arm.c @@ -258,7 +258,7 @@ void audio_init() { } // Check EEPROM -#if defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE) +#ifdef EEPROM_ENABLE if (!eeconfig_is_enabled()) { eeconfig_init(); } diff --git a/quantum/rgblight.c b/quantum/rgblight.c index 40de19eac3..67d601af7f 100644 --- a/quantum/rgblight.c +++ b/quantum/rgblight.c @@ -19,9 +19,11 @@ # include <avr/eeprom.h> # include <avr/interrupt.h> #endif +#ifdef EEPROM_ENABLE +# include "eeprom.h" +#endif #ifdef STM32_EEPROM_ENABLE # include "hal.h" -# include "eeprom.h" # include "eeprom_stm32.h" #endif #include "wait.h" @@ -146,7 +148,7 @@ void rgblight_check_config(void) { } uint32_t eeconfig_read_rgblight(void) { -#if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE) +#ifdef EEPROM_ENABLE return eeprom_read_dword(EECONFIG_RGBLIGHT); #else return 0; @@ -154,7 +156,7 @@ uint32_t eeconfig_read_rgblight(void) { } void eeconfig_update_rgblight(uint32_t val) { -#if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE) +#ifdef EEPROM_ENABLE rgblight_check_config(); eeprom_update_dword(EECONFIG_RGBLIGHT, val); #endif diff --git a/tmk_core/common.mk b/tmk_core/common.mk index 8f355da123..5f149d8f4d 100644 --- a/tmk_core/common.mk +++ b/tmk_core/common.mk @@ -26,29 +26,11 @@ TMK_COMMON_SRC += $(COMMON_DIR)/host.c \ $(PLATFORM_COMMON_DIR)/bootloader.c \ ifeq ($(PLATFORM),AVR) - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/xprintf.S + TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/xprintf.S endif ifeq ($(PLATFORM),CHIBIOS) - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/printf.c - ifeq ($(MCU_SERIES), STM32F3xx) - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/eeprom_stm32.c - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/flash_stm32.c - TMK_COMMON_DEFS += -DEEPROM_EMU_STM32F303xC - TMK_COMMON_DEFS += -DSTM32_EEPROM_ENABLE - else ifeq ($(MCU_SERIES), STM32F1xx) - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/eeprom_stm32.c - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/flash_stm32.c - TMK_COMMON_DEFS += -DEEPROM_EMU_STM32F103xB - TMK_COMMON_DEFS += -DSTM32_EEPROM_ENABLE - else ifeq ($(MCU_SERIES)_$(MCU_LDSCRIPT), STM32F0xx_STM32F072xB) - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/eeprom_stm32.c - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/flash_stm32.c - TMK_COMMON_DEFS += -DEEPROM_EMU_STM32F072xB - TMK_COMMON_DEFS += -DSTM32_EEPROM_ENABLE - else - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/eeprom_teensy.c - endif + TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/printf.c ifeq ($(strip $(AUTO_SHIFT_ENABLE)), yes) TMK_COMMON_SRC += $(CHIBIOS)/os/various/syscalls.c else ifeq ($(strip $(TERMINAL_ENABLE)), yes) @@ -57,15 +39,9 @@ ifeq ($(PLATFORM),CHIBIOS) endif ifeq ($(PLATFORM),ARM_ATSAM) - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/printf.c - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/eeprom.c + TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/printf.c endif -ifeq ($(PLATFORM),TEST) - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/eeprom.c -endif - - # Option modules BOOTMAGIC_ENABLE ?= no diff --git a/tmk_core/common/arm_atsam/eeprom.c b/tmk_core/common/arm_atsam/eeprom.c index 44a0bf4d76..a69f382823 100644 --- a/tmk_core/common/arm_atsam/eeprom.c +++ b/tmk_core/common/arm_atsam/eeprom.c @@ -16,9 +16,12 @@ #include "eeprom.h" -#define EEPROM_SIZE 32 +#ifndef EEPROM_SIZE +# include "eeconfig.h" +# define EEPROM_SIZE (((EECONFIG_SIZE+3)/4)*4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO +#endif -static uint8_t buffer[EEPROM_SIZE]; +__attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE]; uint8_t eeprom_read_byte(const uint8_t *addr) { uintptr_t offset = (uintptr_t)addr; @@ -40,7 +43,7 @@ uint32_t eeprom_read_dword(const uint32_t *addr) { return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24); } -void eeprom_read_block(void *buf, const void *addr, uint32_t len) { +void eeprom_read_block(void *buf, const void *addr, size_t len) { const uint8_t *p = (const uint8_t *)addr; uint8_t * dest = (uint8_t *)buf; while (len--) { @@ -62,7 +65,7 @@ void eeprom_write_dword(uint32_t *addr, uint32_t value) { eeprom_write_byte(p, value >> 24); } -void eeprom_write_block(const void *buf, void *addr, uint32_t len) { +void eeprom_write_block(const void *buf, void *addr, size_t len) { uint8_t * p = (uint8_t *)addr; const uint8_t *src = (const uint8_t *)buf; while (len--) { @@ -86,7 +89,7 @@ void eeprom_update_dword(uint32_t *addr, uint32_t value) { eeprom_write_byte(p, value >> 24); } -void eeprom_update_block(const void *buf, void *addr, uint32_t len) { +void eeprom_update_block(const void *buf, void *addr, size_t len) { uint8_t * p = (uint8_t *)addr; const uint8_t *src = (const uint8_t *)buf; while (len--) { diff --git a/tmk_core/common/chibios/eeprom_stm32.c b/tmk_core/common/chibios/eeprom_stm32.c index 926b581c6b..ea51989728 100644 --- a/tmk_core/common/chibios/eeprom_stm32.c +++ b/tmk_core/common/chibios/eeprom_stm32.c @@ -173,7 +173,7 @@ void eeprom_update_dword(uint32_t *Address, uint32_t Value) { } } -void eeprom_read_block(void *buf, const void *addr, uint32_t len) { +void eeprom_read_block(void *buf, const void *addr, size_t len) { const uint8_t *p = (const uint8_t *)addr; uint8_t * dest = (uint8_t *)buf; while (len--) { @@ -181,7 +181,7 @@ void eeprom_read_block(void *buf, const void *addr, uint32_t len) { } } -void eeprom_write_block(const void *buf, void *addr, uint32_t len) { +void eeprom_write_block(const void *buf, void *addr, size_t len) { uint8_t * p = (uint8_t *)addr; const uint8_t *src = (const uint8_t *)buf; while (len--) { @@ -189,7 +189,7 @@ void eeprom_write_block(const void *buf, void *addr, uint32_t len) { } } -void eeprom_update_block(const void *buf, void *addr, uint32_t len) { +void eeprom_update_block(const void *buf, void *addr, size_t len) { uint8_t * p = (uint8_t *)addr; const uint8_t *src = (const uint8_t *)buf; while (len--) { diff --git a/tmk_core/common/chibios/eeprom_teensy.c b/tmk_core/common/chibios/eeprom_teensy.c index a4093fb3ba..0fab9f73d6 100644 --- a/tmk_core/common/chibios/eeprom_teensy.c +++ b/tmk_core/common/chibios/eeprom_teensy.c @@ -50,7 +50,17 @@ // (aligned to 2 or 4 byte boundaries) has twice the endurance // compared to writing 8 bit bytes. // -# define EEPROM_SIZE 32 +# ifndef EEPROM_SIZE +# define EEPROM_SIZE 32 +# endif + +/* + ^^^ Here be dragons: + NXP AppNote AN4282 section 3.1 states that partitioning must only be done once. + Once EEPROM partitioning is done, the size is locked to this initial configuration. + Attempts to modify the EEPROM_SIZE setting may brick your board. +*/ + // Writing unaligned 16 or 32 bit data is handled automatically when // this is defined, but at a cost of extra code size. Without this, @@ -517,8 +527,11 @@ void eeprom_write_block(const void *buf, void *addr, uint32_t len) { #else // No EEPROM supported, so emulate it -# define EEPROM_SIZE 32 -static uint8_t buffer[EEPROM_SIZE]; +# ifndef EEPROM_SIZE +# include "eeconfig.h" +# define EEPROM_SIZE (((EECONFIG_SIZE+3)/4)*4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO +# endif +__attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE]; uint8_t eeprom_read_byte(const uint8_t *addr) { uint32_t offset = (uint32_t)addr; @@ -540,7 +553,7 @@ uint32_t eeprom_read_dword(const uint32_t *addr) { return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24); } -void eeprom_read_block(void *buf, const void *addr, uint32_t len) { +void eeprom_read_block(void *buf, const void *addr, size_t len) { const uint8_t *p = (const uint8_t *)addr; uint8_t * dest = (uint8_t *)buf; while (len--) { @@ -562,7 +575,7 @@ void eeprom_write_dword(uint32_t *addr, uint32_t value) { eeprom_write_byte(p, value >> 24); } -void eeprom_write_block(const void *buf, void *addr, uint32_t len) { +void eeprom_write_block(const void *buf, void *addr, size_t len) { uint8_t * p = (uint8_t *)addr; const uint8_t *src = (const uint8_t *)buf; while (len--) { @@ -589,7 +602,7 @@ void eeprom_update_dword(uint32_t *addr, uint32_t value) { eeprom_write_byte(p, value >> 24); } -void eeprom_update_block(const void *buf, void *addr, uint32_t len) { +void eeprom_update_block(const void *buf, void *addr, size_t len) { uint8_t * p = (uint8_t *)addr; const uint8_t *src = (const uint8_t *)buf; while (len--) { diff --git a/tmk_core/common/eeconfig.c b/tmk_core/common/eeconfig.c index 7cec4bd7df..fe56c57d11 100644 --- a/tmk_core/common/eeconfig.c +++ b/tmk_core/common/eeconfig.c @@ -9,6 +9,10 @@ # include "eeprom_stm32.h" #endif +#if defined(EEPROM_DRIVER) +# include "eeprom_driver.h" +#endif + /** \brief eeconfig enable * * FIXME: needs doc @@ -32,6 +36,9 @@ void eeconfig_init_quantum(void) { #ifdef STM32_EEPROM_ENABLE EEPROM_Erase(); #endif +#if defined(EEPROM_DRIVER) + eeprom_driver_erase(); +#endif eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER); eeprom_update_byte(EECONFIG_DEBUG, 0); eeprom_update_byte(EECONFIG_DEFAULT_LAYER, 0); @@ -81,6 +88,9 @@ void eeconfig_disable(void) { #ifdef STM32_EEPROM_ENABLE EEPROM_Erase(); #endif +#if defined(EEPROM_DRIVER) + eeprom_driver_erase(); +#endif eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER_OFF); } diff --git a/tmk_core/common/eeprom.h b/tmk_core/common/eeprom.h index fcd1667c06..8a81e70668 100644 --- a/tmk_core/common/eeprom.h +++ b/tmk_core/common/eeprom.h @@ -1,23 +1,24 @@ #ifndef TMK_CORE_COMMON_EEPROM_H_ #define TMK_CORE_COMMON_EEPROM_H_ -#if defined(__AVR__) +#if defined(__AVR__) && !defined(EEPROM_DRIVER) # include <avr/eeprom.h> #else # include <stdint.h> +# include <stdlib.h> uint8_t eeprom_read_byte(const uint8_t *__p); uint16_t eeprom_read_word(const uint16_t *__p); uint32_t eeprom_read_dword(const uint32_t *__p); -void eeprom_read_block(void *__dst, const void *__src, uint32_t __n); +void eeprom_read_block(void *__dst, const void *__src, size_t __n); void eeprom_write_byte(uint8_t *__p, uint8_t __value); void eeprom_write_word(uint16_t *__p, uint16_t __value); void eeprom_write_dword(uint32_t *__p, uint32_t __value); -void eeprom_write_block(const void *__src, void *__dst, uint32_t __n); +void eeprom_write_block(const void *__src, void *__dst, size_t __n); void eeprom_update_byte(uint8_t *__p, uint8_t __value); void eeprom_update_word(uint16_t *__p, uint16_t __value); void eeprom_update_dword(uint32_t *__p, uint32_t __value); -void eeprom_update_block(const void *__src, void *__dst, uint32_t __n); +void eeprom_update_block(const void *__src, void *__dst, size_t __n); #endif #endif /* TMK_CORE_COMMON_EEPROM_H_ */ diff --git a/tmk_core/common/test/eeprom.c b/tmk_core/common/test/eeprom.c index 44a0bf4d76..5c8e69dae3 100644 --- a/tmk_core/common/test/eeprom.c +++ b/tmk_core/common/test/eeprom.c @@ -40,7 +40,7 @@ uint32_t eeprom_read_dword(const uint32_t *addr) { return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24); } -void eeprom_read_block(void *buf, const void *addr, uint32_t len) { +void eeprom_read_block(void *buf, const void *addr, size_t len) { const uint8_t *p = (const uint8_t *)addr; uint8_t * dest = (uint8_t *)buf; while (len--) { @@ -62,7 +62,7 @@ void eeprom_write_dword(uint32_t *addr, uint32_t value) { eeprom_write_byte(p, value >> 24); } -void eeprom_write_block(const void *buf, void *addr, uint32_t len) { +void eeprom_write_block(const void *buf, void *addr, size_t len) { uint8_t * p = (uint8_t *)addr; const uint8_t *src = (const uint8_t *)buf; while (len--) { @@ -86,7 +86,7 @@ void eeprom_update_dword(uint32_t *addr, uint32_t value) { eeprom_write_byte(p, value >> 24); } -void eeprom_update_block(const void *buf, void *addr, uint32_t len) { +void eeprom_update_block(const void *buf, void *addr, size_t len) { uint8_t * p = (uint8_t *)addr; const uint8_t *src = (const uint8_t *)buf; while (len--) { |