From 7ab65b94d5326d0b37ab9efa3cd75cff4bd8f578 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 13 Nov 2019 18:38:54 +0100 Subject: [PATCH] driver::storage::Flash: FlashAsStorage-based storage component This is a primitive implementation of a `FlashAsStorage` (or rather, `FlashAsEEPROM`)-based storage component. It's based on `FlashAsEEPROM`, because I couldn't find a sane way to push the storage data variable within our template class. At some point, this needs to be reworked, to pull the size from Props, and not use the EEPROM API wrappers. Signed-off-by: Gergely Nagy --- src/kaleidoscope/driver/storage/Flash.h | 80 +++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/kaleidoscope/driver/storage/Flash.h diff --git a/src/kaleidoscope/driver/storage/Flash.h b/src/kaleidoscope/driver/storage/Flash.h new file mode 100644 index 00000000..a8dc0617 --- /dev/null +++ b/src/kaleidoscope/driver/storage/Flash.h @@ -0,0 +1,80 @@ +/* -*- mode: c++ -*- + * kaleidoscope::driver::storage::Flash -- Storage driver with Flash backend + * Copyright (C) 2019 Keyboard.io, Inc + * Copyright (C) 2019 Dygma Lab S.L. + * + * 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, version 3. + * + * 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 . + */ + +/* + * TODO(algernon): This currently uses , making the Props + * struct fairly useless. At some point, we need to figure out a way to do this + * without EEPROM API emulation, and by having the flash data variable somewhere + * within the `storage::Flash` class. + */ + +#pragma once + +#ifdef __SAMD21G18A__ + +#include "kaleidoscope/driver/storage/Base.h" +#include +#include + +// We need to undefine Flash, because `FlashStorage` defines it as a macro, yet, +// we want to use it as a class name. +#undef Flash + +namespace kaleidoscope { +namespace driver { +namespace storage { + +struct FlashProps : kaleidoscope::driver::storage::BaseProps { + static constexpr uint16_t length = EEPROM_EMULATION_SIZE; +}; + +template +class Flash: public kaleidoscope::driver::storage::Base<_StorageProps> { + public: + template + T& get(uint16_t offset, T& t) { + return EEPROM.get(offset, t); + } + + template + const T& put(uint16_t offset, T& t) { + EEPROM.put(offset, t); + } + + uint8_t read(int idx) { + return EEPROM.read(idx); + } + + void write(int idx, uint8_t val) { + EEPROM.write(idx, val); + } + + void update(int idx, uint8_t val) { + EEPROM.update(idx, val); + } + + void commit() { + EEPROM.commit(); + } +}; + +} +} +} + +#endif