Skip to content

Commit

Permalink
support saving into SmartEEPROM
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre d Alton <alex@alexdalton.org>
  • Loading branch information
daltona committed Apr 27, 2020
1 parent 82ae6ee commit 6548381
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions tmk_core/common/arm_atsam/eeprom.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,44 @@
* 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 "samd51j18a.h"
#include "eeprom.h"
#include "core_cm4.h"
#include "component/nvmctrl.h"

#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];
volatile uint8_t *SmartEEPROM8 = (uint8_t *) 0x44000000;

uint8_t eeprom_read_byte(const uint8_t *addr) {
uintptr_t offset = (uintptr_t)addr;
return buffer[offset];

if (NVMCTRL->SEESTAT.bit.PSZ == 0 || NVMCTRL->SEESTAT.bit.SBLK == 0)
return buffer[offset];

int timeout = 10000;
while (NVMCTRL->SEESTAT.bit.BUSY && timeout-- > 0)
;
return SmartEEPROM8[offset];
}

void eeprom_write_byte(uint8_t *addr, uint8_t value) {
uintptr_t offset = (uintptr_t)addr;
buffer[offset] = value;

if (NVMCTRL->SEESTAT.bit.PSZ == 0 || NVMCTRL->SEESTAT.bit.SBLK == 0) {
buffer[offset] = value;
return;
}

int timeout = 10000;
while (NVMCTRL->SEESTAT.bit.BUSY && timeout-- > 0)
;

SmartEEPROM8[offset] = value;
}

uint16_t eeprom_read_word(const uint16_t *addr) {
Expand Down

0 comments on commit 6548381

Please sign in to comment.