aboutsummaryrefslogtreecommitdiff
path: root/tmk_core
diff options
context:
space:
mode:
authorAlexandre d'Alton <alex@alexdalton.org>2021-09-29 22:56:13 +0200
committerGitHub <noreply@github.com>2021-09-30 06:56:13 +1000
commit90797d903c81944c80b4957e118adfb20145b9ba (patch)
tree66a375a6d9b927183d2efd160690d3f4ee33830b /tmk_core
parentb02a5396251c0fadb92f2632e242b555c238ad8b (diff)
downloadqmk_firmware-90797d903c81944c80b4957e118adfb20145b9ba.tar.gz
qmk_firmware-90797d903c81944c80b4957e118adfb20145b9ba.zip
massdrop alt/ctrl: support saving into nvm (#6068)
* support saving into SmartEEPROM Signed-off-by: Alexandre d Alton <alex@alexdalton.org> * atsam: update smarteeprom implementation - Use define for SmartEEPROM buffer address - Check buffer overflow - Do not perform operation when timeout occurs Signed-off-by: Alexandre d'Alton <alex@alexdalton.org> * return 0 instead of ff for invalid address or timeout Signed-off-by: Alexandre d'Alton <alex@alexdalton.org> * clang-format * Add extra bounds checks Co-authored-by: zvecr <git@zvecr.com>
Diffstat (limited to 'tmk_core')
-rw-r--r--tmk_core/common/arm_atsam/eeprom.c94
1 files changed, 90 insertions, 4 deletions
diff --git a/tmk_core/common/arm_atsam/eeprom.c b/tmk_core/common/arm_atsam/eeprom.c
index ccd5d15a5..ff1a69262 100644
--- a/tmk_core/common/arm_atsam/eeprom.c
+++ b/tmk_core/common/arm_atsam/eeprom.c
@@ -13,24 +13,110 @@
13 * You should have received a copy of the GNU General Public License 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/>. 14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */ 15 */
16
17#include "eeprom.h" 16#include "eeprom.h"
17#include "debug.h"
18#include "samd51j18a.h"
19#include "core_cm4.h"
20#include "component/nvmctrl.h"
18 21
19#ifndef EEPROM_SIZE 22#ifndef EEPROM_SIZE
20# include "eeconfig.h" 23# include "eeconfig.h"
21# define EEPROM_SIZE (((EECONFIG_SIZE + 3) / 4) * 4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO 24# define EEPROM_SIZE (((EECONFIG_SIZE + 3) / 4) * 4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO
22#endif 25#endif
23 26
24__attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE]; 27#ifndef MAX
28# define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
29#endif
30
31#ifndef BUSY_RETRIES
32# define BUSY_RETRIES 10000
33#endif
34
35// #define DEBUG_EEPROM_OUTPUT
36
37/*
38 * Debug print utils
39 */
40#if defined(DEBUG_EEPROM_OUTPUT)
41# define eeprom_printf(fmt, ...) xprintf(fmt, ##__VA_ARGS__);
42#else /* NO_DEBUG */
43# define eeprom_printf(fmt, ...)
44#endif /* NO_DEBUG */
45
46__attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE] = {0};
47volatile uint8_t * SmartEEPROM8 = (uint8_t *)SEEPROM_ADDR;
48
49static inline bool eeprom_is_busy(void) {
50 int timeout = BUSY_RETRIES;
51 while (NVMCTRL->SEESTAT.bit.BUSY && timeout-- > 0)
52 ;
53
54 return NVMCTRL->SEESTAT.bit.BUSY;
55}
56
57static uint32_t get_virtual_eeprom_size(void) {
58 // clang-format off
59 static const uint32_t VIRTUAL_EEPROM_MAP[11][8] = {
60 /* 4 8 16 32 64 128 256 512 */
61 /* 0*/ { 0, 0, 0, 0, 0, 0, 0, 0 },
62 /* 1*/ { 512, 1024, 2048, 4096, 4096, 4096, 4096, 4096 },
63 /* 2*/ { 512, 1024, 2048, 4096, 8192, 8192, 8192, 8192 },
64 /* 3*/ { 512, 1024, 2048, 4096, 8192, 16384, 16384, 16384 },
65 /* 4*/ { 512, 1024, 2048, 4096, 8192, 16384, 16384, 16384 },
66 /* 5*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
67 /* 6*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
68 /* 7*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
69 /* 8*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
70 /* 9*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 },
71 /*10*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 },
72 };
73 // clang-format on
74
75 static uint32_t virtual_eeprom_size = UINT32_MAX;
76 if (virtual_eeprom_size == UINT32_MAX) {
77 virtual_eeprom_size = VIRTUAL_EEPROM_MAP[NVMCTRL->SEESTAT.bit.PSZ][NVMCTRL->SEESTAT.bit.SBLK];
78 }
79 // eeprom_printf("get_virtual_eeprom_size:: %d:%d:%d\n", NVMCTRL->SEESTAT.bit.PSZ, NVMCTRL->SEESTAT.bit.SBLK, virtual_eeprom_size);
80 return virtual_eeprom_size;
81}
25 82
26uint8_t eeprom_read_byte(const uint8_t *addr) { 83uint8_t eeprom_read_byte(const uint8_t *addr) {
27 uintptr_t offset = (uintptr_t)addr; 84 uintptr_t offset = (uintptr_t)addr;
28 return buffer[offset]; 85 if (offset >= MAX(EEPROM_SIZE, get_virtual_eeprom_size())) {
86 eeprom_printf("eeprom_read_byte:: out of bounds\n");
87 return 0x0;
88 }
89
90 if (get_virtual_eeprom_size() == 0) {
91 return buffer[offset];
92 }
93
94 if (eeprom_is_busy()) {
95 eeprom_printf("eeprom_write_byte:: timeout\n");
96 return 0x0;
97 }
98
99 return SmartEEPROM8[offset];
29} 100}
30 101
31void eeprom_write_byte(uint8_t *addr, uint8_t value) { 102void eeprom_write_byte(uint8_t *addr, uint8_t value) {
32 uintptr_t offset = (uintptr_t)addr; 103 uintptr_t offset = (uintptr_t)addr;
33 buffer[offset] = value; 104 if (offset >= MAX(EEPROM_SIZE, get_virtual_eeprom_size())) {
105 eeprom_printf("eeprom_write_byte:: out of bounds\n");
106 return;
107 }
108
109 if (get_virtual_eeprom_size() == 0) {
110 buffer[offset] = value;
111 return;
112 }
113
114 if (eeprom_is_busy()) {
115 eeprom_printf("eeprom_write_byte:: timeout\n");
116 return;
117 }
118
119 SmartEEPROM8[offset] = value;
34} 120}
35 121
36uint16_t eeprom_read_word(const uint16_t *addr) { 122uint16_t eeprom_read_word(const uint16_t *addr) {