Add initial NVS Arduino implementation

This commit is contained in:
me-no-dev 2017-02-08 23:09:42 +02:00
parent 7a7b697ff9
commit bfa979a900
5 changed files with 508 additions and 2 deletions

View File

@ -1,4 +1,6 @@
COMPONENT_ADD_INCLUDEDIRS := cores/esp32 variants/esp32 libraries/WiFi/src libraries/SPI/src libraries/Wire/src
ARDUINO_LIB_DIRS := libraries/WiFi/src libraries/SPI/src libraries/Wire/src libraries/ESP32NVS/src
COMPONENT_ADD_INCLUDEDIRS := cores/esp32 variants/esp32 $(ARDUINO_LIB_DIRS)
COMPONENT_PRIV_INCLUDEDIRS := cores/esp32/libb64
COMPONENT_SRCDIRS := cores/esp32/libb64 cores/esp32 variants/esp32 libraries/WiFi/src libraries/SPI/src libraries/Wire/src
COMPONENT_SRCDIRS := cores/esp32/libb64 cores/esp32 variants/esp32 $(ARDUINO_LIB_DIRS)
CXXFLAGS += -fno-rtti

View File

@ -0,0 +1,41 @@
#######################################
# Syntax Coloring Map NVS
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
NVSClass KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
end KEYWORD2
writeChar KEYWORD2
writeUChar KEYWORD2
writeShort KEYWORD2
writeUShort KEYWORD2
writeInt KEYWORD2
writeUInt KEYWORD2
writeLong KEYWORD2
writeULong KEYWORD2
writeString KEYWORD2
writeBytes KEYWORD2
readChar KEYWORD2
readUChar KEYWORD2
readShort KEYWORD2
readUShort KEYWORD2
readInt KEYWORD2
readUInt KEYWORD2
readLong KEYWORD2
readULong KEYWORD2
readString KEYWORD2
readBytes KEYWORD2
erase KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################

View File

@ -0,0 +1,9 @@
name=ESP32NVS
version=1.0
author=Hristo Gochkov
maintainer=Hristo Gochkov <hristo@espressif.com>
sentence=Provides frendly access to ESP32's Non-Volatile Storage
paragraph=
category=Storage
url=
architectures=esp32

View File

@ -0,0 +1,397 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ESP32NVS.h"
#include "nvs.h"
const char * nvs_errors[] = { "OTHER", "NOT_INITIALIZED", "NOT_FOUND", "TYPE_MISMATCH", "READ_ONLY", "NOT_ENOUGH_SPACE", "INVALID_NAME", "INVALID_HANDLE", "REMOVE_FAILED", "KEY_TOO_LONG", "PAGE_FULL", "INVALID_STATE", "INVALID_LENGHT"};
#define nvs_error(e) (((e)>ESP_ERR_NVS_BASE)?nvs_errors[(e)&~(ESP_ERR_NVS_BASE)]:nvs_errors[0])
NVSClass::NVSClass()
:_handle(0)
,_started(false)
,_readOnly(false)
{}
NVSClass::~NVSClass(){
end();
}
bool NVSClass::begin(const char * name, bool readOnly){
if(_started){
return false;
}
_readOnly = readOnly;
esp_err_t err = nvs_open(name, readOnly?NVS_READONLY:NVS_READWRITE, &_handle);
if(err){
log_e("nvs_open failed: %s", nvs_error(err));
return false;
}
_started = true;
return true;
}
void NVSClass::end(){
if(!_started){
return;
}
nvs_close(_handle);
_started = false;
}
/*
* Erase
* */
bool NVSClass::erase(){
if(!_started || _readOnly){
return false;
}
esp_err_t err = nvs_erase_all(_handle);
if(err){
log_e("nvs_erase_all fail: %s", nvs_error(err));
return false;
}
return true;
}
bool NVSClass::erase(const char * key){
if(!_started || !key || _readOnly){
return false;
}
esp_err_t err = nvs_erase_key(_handle, key);
if(err){
log_e("nvs_erase_key fail: %s %s", key, nvs_error(err));
return false;
}
return true;
}
/*
* Write
* */
size_t NVSClass::writeChar(const char* key, int8_t value){
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_i8(_handle, key, value);
if(err){
log_e("nvs_set_i8 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 1;
}
size_t NVSClass::writeUChar(const char* key, uint8_t value){
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_u8(_handle, key, value);
if(err){
log_e("nvs_set_u8 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 1;
}
size_t NVSClass::writeShort(const char* key, int16_t value){
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_i16(_handle, key, value);
if(err){
log_e("nvs_set_i16 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 2;
}
size_t NVSClass::writeUShort(const char* key, uint16_t value){
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_u16(_handle, key, value);
if(err){
log_e("nvs_set_u16 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 2;
}
size_t NVSClass::writeInt(const char* key, int32_t value){
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_i32(_handle, key, value);
if(err){
log_e("nvs_set_i32 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 4;
}
size_t NVSClass::writeUInt(const char* key, uint32_t value){
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_u32(_handle, key, value);
if(err){
log_e("nvs_set_u32 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 4;
}
size_t NVSClass::writeLong(const char* key, int64_t value){
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_i64(_handle, key, value);
if(err){
log_e("nvs_set_i64 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 8;
}
size_t NVSClass::writeULong(const char* key, uint64_t value){
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_u64(_handle, key, value);
if(err){
log_e("nvs_set_u64 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 8;
}
size_t NVSClass::writeString(const char* key, const char* value){
if(!_started || !key || !value || _readOnly){
return 0;
}
esp_err_t err = nvs_set_str(_handle, key, value);
if(err){
log_e("nvs_set_str fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return strlen(value);
}
size_t NVSClass::writeBytes(const char* key, const void* value, size_t len){
if(!_started || !key || !value || !len || _readOnly){
return 0;
}
esp_err_t err = nvs_set_blob(_handle, key, value, len);
if(err){
log_e("nvs_set_blob fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return len;
}
/*
* Read
* */
int8_t NVSClass::readChar(const char* key){
int8_t value = 0;
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_i8(_handle, key, &value);
if(err){
log_e("nvs_get_i8 fail: %s %s", key, nvs_error(err));
}
return value;
}
uint8_t NVSClass::readUChar(const char* key){
uint8_t value = 0;
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_u8(_handle, key, &value);
if(err){
log_e("nvs_get_u8 fail: %s %s", key, nvs_error(err));
}
return value;
}
int16_t NVSClass::readShort(const char* key){
int16_t value = 0;
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_i16(_handle, key, &value);
if(err){
log_e("nvs_get_i16 fail: %s %s", key, nvs_error(err));
}
return value;
}
uint16_t NVSClass::readUShort(const char* key){
uint16_t value = 0;
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_u16(_handle, key, &value);
if(err){
log_e("nvs_get_u16 fail: %s %s", key, nvs_error(err));
}
return value;
}
int32_t NVSClass::readInt(const char* key){
int32_t value = 0;
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_i32(_handle, key, &value);
if(err){
log_e("nvs_get_i32 fail: %s %s", key, nvs_error(err));
}
return value;
}
uint32_t NVSClass::readUInt(const char* key){
uint32_t value = 0;
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_u32(_handle, key, &value);
if(err){
log_e("nvs_get_u32 fail: %s %s", key, nvs_error(err));
}
return value;
}
int64_t NVSClass::readLong(const char* key){
int64_t value = 0;
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_i64(_handle, key, &value);
if(err){
log_e("nvs_get_i64 fail: %s %s", key, nvs_error(err));
}
return value;
}
uint64_t NVSClass::readULong(const char* key){
uint64_t value = 0;
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_u64(_handle, key, &value);
if(err){
log_e("nvs_get_u64 fail: %s %s", key, nvs_error(err));
}
return value;
}
String NVSClass::readString(const char* key){
char * value = NULL;
size_t len = 0;
if(!_started || !key){
return String();
}
esp_err_t err = nvs_get_str(_handle, key, value, &len);
if(err){
log_e("nvs_get_str len fail: %s %s", key, nvs_error(err));
return String();
}
char buf[len];
value = buf;
err = nvs_get_str(_handle, key, value, &len);
if(err){
log_e("nvs_get_str fail: %s %s", key, nvs_error(err));
return String();
}
return String(buf);
}
size_t NVSClass::readBytes(const char* key, void * buf, size_t maxLen){
size_t len = 0;
if(!_started || !key || !buf || !maxLen){
return 0;
}
esp_err_t err = nvs_get_blob(_handle, key, NULL, &len);
if(err){
log_e("nvs_get_blob len fail: %s %s", key, nvs_error(err));
return 0;
}
if(len > maxLen){
log_e("not enough space in buffer: %u < %u", maxLen, len);
return 0;
}
err = nvs_get_blob(_handle, key, buf, &len);
if(err){
log_e("nvs_get_blob fail: %s %s", key, nvs_error(err));
return 0;
}
return len;
}

View File

@ -0,0 +1,57 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ESP32NVS_H_
#define _ESP32NVS_H_
#include "Arduino.h"
class NVSClass {
protected:
uint32_t _handle;
bool _started;
bool _readOnly;
public:
NVSClass();
~NVSClass();
bool begin(const char * name, bool readOnly=false);
void end();
bool erase();
bool erase(const char * key);
size_t writeChar(const char* key, int8_t value);
size_t writeUChar(const char* key, uint8_t value);
size_t writeShort(const char* key, int16_t value);
size_t writeUShort(const char* key, uint16_t value);
size_t writeInt(const char* key, int32_t value);
size_t writeUInt(const char* key, uint32_t value);
size_t writeLong(const char* key, int64_t value);
size_t writeULong(const char* key, uint64_t value);
size_t writeString(const char* key, const char* value);
size_t writeBytes(const char* key, const void* value, size_t len);
int8_t readChar(const char* key);
uint8_t readUChar(const char* key);
int16_t readShort(const char* key);
uint16_t readUShort(const char* key);
int32_t readInt(const char* key);
uint32_t readUInt(const char* key);
int64_t readLong(const char* key);
uint64_t readULong(const char* key);
String readString(const char* key);
size_t readBytes(const char* key, void * buf, size_t maxLen);
};
#endif