qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

nvm_via.c (2616B)


      1 // Copyright 2024 Nick Brassel (@tzarc)
      2 // SPDX-License-Identifier: GPL-2.0-or-later
      3 
      4 #include "eeprom.h"
      5 #include "util.h"
      6 #include "via.h"
      7 #include "nvm_via.h"
      8 #include "nvm_eeprom_eeconfig_internal.h"
      9 #include "nvm_eeprom_via_internal.h"
     10 
     11 void nvm_via_erase(void) {
     12     // No-op, nvm_eeconfig_erase() will have already erased EEPROM if necessary.
     13 }
     14 
     15 void nvm_via_read_magic(uint8_t *magic0, uint8_t *magic1, uint8_t *magic2) {
     16     if (magic0) {
     17         *magic0 = eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0);
     18     }
     19 
     20     if (magic1) {
     21         *magic1 = eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1);
     22     }
     23 
     24     if (magic2) {
     25         *magic2 = eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2);
     26     }
     27 }
     28 
     29 void nvm_via_update_magic(uint8_t magic0, uint8_t magic1, uint8_t magic2) {
     30     eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0, magic0);
     31     eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1, magic1);
     32     eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2, magic2);
     33 }
     34 
     35 uint32_t nvm_via_read_layout_options(void) {
     36     uint32_t value = 0;
     37     // Start at the most significant byte
     38     void *source = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR);
     39     for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
     40         value = value << 8;
     41         value |= eeprom_read_byte(source);
     42         source++;
     43     }
     44     return value;
     45 }
     46 
     47 void nvm_via_update_layout_options(uint32_t val) {
     48     // Start at the least significant byte
     49     void *target = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR + VIA_EEPROM_LAYOUT_OPTIONS_SIZE - 1);
     50     for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
     51         eeprom_update_byte(target, val & 0xFF);
     52         val = val >> 8;
     53         target--;
     54     }
     55 }
     56 
     57 uint32_t nvm_via_read_custom_config(void *buf, uint32_t offset, uint32_t length) {
     58 #if VIA_EEPROM_CUSTOM_CONFIG_SIZE > 0
     59     void *ee_start = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + offset);
     60     void *ee_end   = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + MIN(VIA_EEPROM_CUSTOM_CONFIG_SIZE, offset + length));
     61     eeprom_read_block(buf, ee_start, ee_end - ee_start);
     62     return ee_end - ee_start;
     63 #else
     64     return 0;
     65 #endif
     66 }
     67 
     68 uint32_t nvm_via_update_custom_config(const void *buf, uint32_t offset, uint32_t length) {
     69 #if VIA_EEPROM_CUSTOM_CONFIG_SIZE > 0
     70     void *ee_start = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + offset);
     71     void *ee_end   = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + MIN(VIA_EEPROM_CUSTOM_CONFIG_SIZE, offset + length));
     72     eeprom_update_block(buf, ee_start, ee_end - ee_start);
     73     return ee_end - ee_start;
     74 #else
     75     return 0;
     76 #endif
     77 }