From 9fa58af73b45fdcb9b32c1a099f7c3972ce06bf8 Mon Sep 17 00:00:00 2001
From: Alexandre Abadie <alexandre.abadie@inria.fr>
Date: Sun, 1 Apr 2018 17:38:37 +0200
Subject: [PATCH] drivers/periph: add API for internal MCU EEPROM

---
 drivers/include/periph/eeprom.h | 82 +++++++++++++++++++++++++++++++++
 drivers/periph_common/eeprom.c  | 52 +++++++++++++++++++++
 2 files changed, 134 insertions(+)
 create mode 100644 drivers/include/periph/eeprom.h
 create mode 100644 drivers/periph_common/eeprom.c

diff --git a/drivers/include/periph/eeprom.h b/drivers/include/periph/eeprom.h
new file mode 100644
index 0000000000..14750c594e
--- /dev/null
+++ b/drivers/include/periph/eeprom.h
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2018 Inria
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License v2.1. See the file LICENSE in the top level
+ * directory for more details.
+ */
+
+/**
+ * @defgroup    drivers_periph_eeprom EEPROM driver
+ * @ingroup     drivers_periph
+ * @brief       Low-level EEPROM interface
+ *
+ * @{
+ * @file
+ * @brief       Low-level eeprom driver interface
+ *
+ * @author      Alexandre Abadie <alexandre.abadie@inria.fr>
+ *
+ */
+
+#ifndef PERIPH_EEPROM_H
+#define PERIPH_EEPROM_H
+
+#include <stdint.h>
+
+#include "cpu.h"
+#include "periph_cpu.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef EEPROM_SIZE
+#error "periph/eeprom: EEPROM_SIZE is not defined"
+#endif
+
+/**
+ * @brief   Read a byte at the given position in eeprom
+ *
+ * @param[in]  pos      position to read
+ *
+ * @return the byte read
+ */
+uint8_t eeprom_read_byte(uint32_t pos);
+
+/**
+ * @brief   Read @p len bytes from the given position
+ *
+ * @param[in]  pos      start position in eeprom
+ * @param[out] data     output byte array to write to
+ * @param[in]  len      the number of bytes to read
+ *
+ * @return  the number of bytes read
+ */
+size_t eeprom_read(uint32_t pos, uint8_t *data, size_t len);
+
+/**
+ * @brief   Write a byte at the given position
+ *
+ * @param[in] pos       position to write
+ * @param[in] data      byte address to write to
+ */
+void eeprom_write_byte(uint32_t pos, uint8_t data);
+
+/**
+ * @brief   Write @p len bytes at the given position
+ *
+ * @param[in] pos       start position in eeprom
+ * @param[in] data      input byte array to read into
+ * @param[in] len       the number of bytes to read
+ *
+ * @return the number of bytes written
+ */
+size_t eeprom_write(uint32_t pos, const uint8_t *data, size_t len);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* PERIPH_EEPROM_H */
+/** @} */
diff --git a/drivers/periph_common/eeprom.c b/drivers/periph_common/eeprom.c
new file mode 100644
index 0000000000..f04c7af866
--- /dev/null
+++ b/drivers/periph_common/eeprom.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2018 Inria
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License v2.1. See the file LICENSE in the top level
+ * directory for more details.
+ */
+
+/**
+ * @ingroup     drivers
+ * @{
+ *
+ * @file
+ * @brief       Common eeprom functions implementation
+ *
+ * @author      Alexandre Abadie <alexandre.abadie@inria.fr>
+ *
+ * @}
+ */
+
+#include <string.h>
+#include "cpu.h"
+#include "assert.h"
+
+/* guard this file, must be done before including periph/eeprom.h */
+#if defined(EEPROM_SIZE)
+
+#include "periph/eeprom.h"
+
+size_t eeprom_read(uint32_t pos, uint8_t *data, size_t len)
+{
+    assert(pos + len < EEPROM_SIZE);
+
+    for (size_t i = 0; i < len; i++) {
+        data[i] = eeprom_read_byte(pos++);
+    }
+
+    return len;
+}
+
+size_t eeprom_write(uint32_t pos, const uint8_t *data, size_t len)
+{
+    assert(pos + len < EEPROM_SIZE);
+
+    for (size_t i = 0; i < len; i++) {
+        eeprom_write_byte(pos++, data[i]);
+    }
+
+    return len;
+}
+
+#endif
-- 
GitLab