drivers/periph: add API for internal MCU EEPROM
This commit is contained in:
parent
01934de2ff
commit
9fa58af73b
82
drivers/include/periph/eeprom.h
Normal file
82
drivers/include/periph/eeprom.h
Normal file
@ -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 */
|
||||
/** @} */
|
||||
52
drivers/periph_common/eeprom.c
Normal file
52
drivers/periph_common/eeprom.c
Normal file
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user