Esprit.cpp
1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/****************************************************************************
*
* Arduino Helper Library for Freematics Esprit Development Board
* Distributed under BSD license
*
* This library currently provides access to BLE (GATT server), NVS, Flash I/O
* and additional hardware serial UARTs, within the scope of Arduino.
*
* Developed by Stanley Huang <stanley@freematics.com.au>
* Visit http://freematics.com/products/freematics-esprit for more information
*
****************************************************************************/
#include <Arduino.h>
#ifndef _SIM
#include "esp_system.h"
#include "esp_log.h"
#include "nvs_flash.h"
#endif
#include "Esprit.h"
#ifndef _SIM
bool CNVS::flashRead(void* buffer, size_t bufsize, size_t offset)
{
if (offset == -1) {
offset = m_offset;
}
else {
m_offset = offset;
}
bool success = spi_flash_read(m_offset, buffer, bufsize) == ESP_OK;
if (success) {
m_offset += bufsize;
}
return success;
}
bool CNVS::flashWrite(void* buffer, size_t bufsize, size_t offset)
{
if (offset == -1) {
offset = m_offset;
}
else {
m_offset = offset;
}
bool success = spi_flash_write(FLASH_STORAGE_BASE_ADDR, buffer, bufsize) == ESP_OK;
if (success) {
m_offset += bufsize;
}
return success;
}
#else
bool CNVS::flashRead(void* buffer, size_t bufsize, size_t offset)
{
if (m_fpFlash) {
if (offset != -1) {
fseek(m_fpFlash, offset, SEEK_SET);
}
return fread(buffer, 1, bufsize, m_fpFlash) == bufsize;
}
return false;
}
bool CNVS::flashWrite(void* buffer, size_t bufsize, size_t offset)
{
if (m_fpFlash) {
if (offset != -1) {
fseek(m_fpFlash, offset, SEEK_SET);
}
return fwrite(buffer, 1, bufsize, m_fpFlash) == bufsize;
}
return false;
}
#endif