* Add NVRAM support This is an EEPROM replacement based on MCU's internal Flash memory. The NVRAM class is designed to be stateless. Each operation is written to flash immediately. The last 16k(nRF51)/32k(nRF52) of Flash memory are managed by the VirtualPage class. This class allows writing a single emulated 4k page up 40,000 times. The NVRAM class is a log based byte-wise storage allowing up to 40,000,000 writes depending on the number of used addresses. Each four allocated addresses from the beginning of the NVRAM reduce the write log by one. When first 256 bytes of the emulated storage are used, there is an average writing capacity of 145,000 cycles per byte. When the write log is full, the NVRAM is compressed into a new flash page. After a new page is ready, the old page is released. This process can take up to 4500 ms and cannot be interrupted. The NVRAM functionality can easily be ported to ESP8266 or SAMD by providing a Flash interface. * Add NRF5 platform support This is the support of the Nordic Semiconductor NRF51 (nrf51822, nrf51422) and NRF52 (nrf52832) platform. Based on arduino-nRF5 (https://github.com/sandeepmistry/arduino-nRF5). The included radio driver is compatible with the NRF24 devices. Using a SoftDevice (BLE)is not supported and must be removed before flashing MySensors with a "mass_erase" operation.
114 lines
3.8 KiB
C++
114 lines
3.8 KiB
C++
/*
|
|
* NVRAM.h - Byte-wise storage for Virtual Pages.
|
|
* Original Copyright (c) 2017 Frank Holtz. All right reserved.
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
/**
|
|
* @file NVRAM.h
|
|
* @brief Byte-wise storage for Virtual Pages.
|
|
*
|
|
* @defgroup NVM Nonvolatile Memory
|
|
* @ingroup internals
|
|
* @details Nonvolatile Memory Class
|
|
* @{
|
|
*/
|
|
#pragma once
|
|
|
|
#include "Flash.h"
|
|
#include "VirtualPage.h"
|
|
#include <Arduino.h>
|
|
|
|
/**
|
|
* @class NVRAMClass
|
|
* @brief Nonvolatile Memory
|
|
*/
|
|
class NVRAMClass
|
|
{
|
|
public:
|
|
//----------------------------------------------------------------------------
|
|
/** Constructor. */
|
|
NVRAMClass() {};
|
|
//----------------------------------------------------------------------------
|
|
/** Initialize Class */
|
|
void begin() {};
|
|
//----------------------------------------------------------------------------
|
|
/** Deinitialize Class */
|
|
void end() {};
|
|
//----------------------------------------------------------------------------
|
|
/** NVM available space in bytes
|
|
* @return Number of bytes
|
|
*/
|
|
uint16_t length() const;
|
|
//----------------------------------------------------------------------------
|
|
/** Read a block of bytes
|
|
* @param[in] *dst Write bytes to given pointer
|
|
* @param[in] idx First NVM address to read
|
|
* @param[in] n Number of bytes to read
|
|
*/
|
|
void read_block(uint8_t *dst, uint16_t idx, uint16_t n);
|
|
//----------------------------------------------------------------------------
|
|
/** Read a byte
|
|
* @param[in] idx NVM Address to read
|
|
* @return Byte from given address
|
|
*/
|
|
uint8_t read(const uint16_t idx);
|
|
//----------------------------------------------------------------------------
|
|
/** Write a block
|
|
* @param[in] *src Read bytes from given pointer
|
|
* @param[in] idx First NVM address to write
|
|
* @param[in] n Number of bytes to write
|
|
* @return success
|
|
*/
|
|
bool write_block(uint8_t *src, uint16_t idx, uint16_t n);
|
|
//----------------------------------------------------------------------------
|
|
/** Write a byte
|
|
* @param[in] idx NVM Address to write
|
|
* @param[in] value Byte to write
|
|
* @return success
|
|
*/
|
|
bool write(const uint16_t idx, uint8_t value);
|
|
//----------------------------------------------------------------------------
|
|
/** Preserve the number of bytes in NVM log for time critical writes. Runtime
|
|
* up to 5000ms!
|
|
* @param[in] number Bytes to preserve, can 0 to find out free log space
|
|
* @return Bytes available for writing
|
|
*/
|
|
int write_prepare(uint16_t number);
|
|
//----------------------------------------------------------------------------
|
|
/** lear log if full and prepare released pages for faster reallocation.
|
|
* Runtime up to 5000ms!
|
|
* @param[in] write_preserve Byte to preserve
|
|
*/
|
|
void clean_up(uint16_t write_preserve);
|
|
|
|
private:
|
|
// Return a virtual page
|
|
uint32_t *get_page();
|
|
// Get actual log position
|
|
uint16_t get_log_position(uint32_t *vpage);
|
|
// Read a byte from page
|
|
uint8_t get_byte_from_page(uint32_t *vpage, uint16_t log_start,
|
|
uint16_t log_end, uint16_t idx);
|
|
// switch a page
|
|
uint32_t *switch_page(uint32_t *old_vpage, uint16_t *log_start,
|
|
uint16_t *log_end);
|
|
};
|
|
|
|
/** Variable to access the NVRAMClass */
|
|
extern NVRAMClass NVRAM;
|
|
|
|
/** @} */
|