Skip to content

Commit c8a4cbc

Browse files
committed
Added EEPROM emulation and some cleanup
1 parent 2a7b044 commit c8a4cbc

14 files changed

+270
-379
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
/.vscode/c_cpp_properties.json
44
/.vscode/launch.json
55
/.vscode/ipch
6+
7+
/tmp

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ Arduino Style Firmware/Library based on joan2937/lg written for PlatformIO Nativ
66

77
* Arduino style digital Functions (pinMode, digitalWrite, digitalRead)
88
* Arduino TwoWire (100% conform to meta standard)
9+
- Enable I2C before use
910
* Arduino SPI (99% conform to meta standart, Hardware and Software Chip Sleect)
11+
- Enable I2C before use
1012
- when Hardware CS Pins are used, SPI.transfer controlls CS. This is different from the Arduino behaviour. To get 100% the expected behaviour, use Software Chip Selects
13+
* EEPROM Emulation
1114

1215

1316

lib/jNative/src/Arduino.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ bool pinMode(uint8_t pin, uint8_t mode);
6363
void digitalWrite(uint8_t pin, uint8_t val);
6464
int digitalRead(uint8_t pin);
6565

66+
void terminate();
6667

6768
//float random();
6869

6970
#ifndef __attribute__
7071
#define __attribute__(...)
7172
#endif
7273

73-
/*
74+
7475
#ifndef F
7576
//Define macro for strings stored in flash.
7677
#define F(str) (str)
7778
#endif // F
78-
*/

lib/jNative/src/EEPROM.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#include <Arduino.h>
2+
#include "EEPROM.h"
3+
4+
5+
#ifdef ENABLE_ARDUINO_DEBUG
6+
#define LogX printf
7+
#else
8+
#define LogX(...)
9+
#endif
10+
11+
12+
#include <filesystem>
13+
namespace fs = std::filesystem;
14+
15+
#include <fstream> // std::fstream
16+
static std::fstream file;
17+
18+
19+
#define path "mem.bin"
20+
21+
EEPROMClass::EEPROMClass(void)
22+
{
23+
24+
}
25+
26+
bool EEPROMClass::begin() {
27+
28+
for(int i = 0; i < length(); i++)
29+
_data[i] = 0xFF;
30+
31+
if(file.is_open())
32+
file.close();
33+
34+
// create mem file if not exists
35+
if(!fs::exists(path))
36+
{
37+
LogX("eeprom.state: creating a new memory file\n");
38+
39+
//file.open(path, (std::ios::in | std::ios::out | std::ios::binary));
40+
//file.open(path, (std::ios::in | std::ios::out | std::ios::binary | std::fstream::app));
41+
file.open(path, (std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc));
42+
43+
//printf(">>> IS_OPEN: %d\n", file.is_open());
44+
45+
//char c = 0xFF;
46+
char c = 'X';
47+
for(int i = 0; i < _size; i++)
48+
file.write(&c, 1);
49+
50+
file.flush();
51+
file.close();
52+
53+
54+
//printf(">>> EXISTS: %d\n", fs::exists(path));
55+
}
56+
57+
file.open(path, (std::ios::out | std::ios::in | std::ios::binary));
58+
59+
if(!file.is_open())
60+
{
61+
LogX("eeprom.err: open of memory file failed\n");
62+
return false;
63+
}
64+
65+
// go to start of file
66+
file.seekg(0);
67+
file.seekp(0);
68+
69+
// load from file
70+
file.read((char*) _data, _size);
71+
72+
return true;
73+
}
74+
75+
bool EEPROMClass::end() {
76+
77+
if(file.is_open())
78+
file.close();
79+
80+
return true;
81+
}
82+
83+
uint8_t EEPROMClass::read(int const address) {
84+
if (address < 0 || (size_t)address >= _size) {
85+
return 0;
86+
}
87+
88+
return _data[address];
89+
}
90+
91+
void EEPROMClass::write(int const address, uint8_t const value) {
92+
if (address < 0 || (size_t)address >= _size) {
93+
return;
94+
}
95+
if (!_data) {
96+
return;
97+
}
98+
99+
_data[address] = value;
100+
}
101+
102+
bool EEPROMClass::commit() {
103+
if (!_size) {
104+
return false;
105+
}
106+
107+
if(file.is_open()) {
108+
109+
// go to start of file
110+
file.seekg(0);
111+
file.seekp(0);
112+
113+
// override file content with memory in ram
114+
file.write((char*) _data, _size);
115+
116+
return true;
117+
}
118+
119+
return false;
120+
}
121+
122+
123+
EEPROMClass EEPROM;

lib/jNative/src/EEPROM.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#pragma once
2+
3+
#include "Arduino.h"
4+
5+
#ifndef EEPROM_SIZE
6+
#define EEPROM_SIZE 1024
7+
#endif // EEPROM_SIZE
8+
9+
class EEPROMClass {
10+
public:
11+
EEPROMClass(void);
12+
13+
bool begin();
14+
bool end();
15+
16+
uint8_t read(int const address);
17+
void write(int const address, uint8_t const val);
18+
19+
// write eeprom data to file
20+
bool commit();
21+
22+
23+
24+
25+
template<typename T>
26+
T &get(int const address, T &t) {
27+
if (address < 0 || address + sizeof(T) > _size) {
28+
return t;
29+
}
30+
31+
memcpy((uint8_t*) &t, _data + address, sizeof(T));
32+
return t;
33+
}
34+
35+
template<typename T>
36+
const T &put(int const address, const T &t) {
37+
if (address < 0 || address + sizeof(T) > _size) {
38+
return t;
39+
}
40+
if (memcmp(_data + address, (const uint8_t*)&t, sizeof(T)) != 0) {
41+
memcpy(_data + address, (const uint8_t*)&t, sizeof(T));
42+
}
43+
44+
return t;
45+
}
46+
47+
size_t length() {
48+
return _size;
49+
}
50+
51+
uint8_t& operator[](int const address) {
52+
return _data[address];
53+
}
54+
uint8_t const & operator[](int const address) const {
55+
return _data[address];
56+
}
57+
58+
protected:
59+
60+
// inter-buffer that stores data in ram. only commit writes out to file
61+
uint8_t _data[EEPROM_SIZE];
62+
size_t _size = EEPROM_SIZE;
63+
64+
};
65+
66+
extern EEPROMClass EEPROM;
67+

src/main_arduino_base.cpp renamed to lib/jNative/src/main_arduino_base.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
2-
#include "main.h"
3-
4-
#ifdef ENABLE_MAIN_ARDUINO
5-
61
#include "Arduino.h"
72

83
// Arduino GPIO functions
@@ -33,5 +28,3 @@ int main()
3328

3429
GPIO_end();
3530
}
36-
37-
#endif // ENABLE_MAIN_ARDUINO

src/main.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,13 @@
77
*
88
* ************************************************************/
99

10-
// Basic Code Examples to test low level code
11-
//#define ENABLE_MAIN_BASIC_DIGITAL_WRITE
12-
//#define ENABLE_MAIN_BASIC_DIGITAL_READ
13-
//#define ENABLE_MAIN_BASIC_I2C_REG_WRITE_READ
14-
//#define ENABLE_MAIN_BASIC_SPI_REG_WRITE_READ
1510

16-
// Arduino Like Code Examples
17-
#define ENABLE_MAIN_ARDUINO
11+
//#define ENABLE_MAIN_EMPTY
12+
//#define ENABLE_MAIN_BASIC
13+
//#define ENABLE_MAIN_ARDUINO_GPIO
14+
//#define ENABLE_MAIN_ARDUINO_I2C
15+
//#define ENABLE_MAIN_ARDUINO_SPI
16+
//#define ENABLE_MAIN_ARDUINO_SPI_MAX31865
17+
#define ENABLE_MAIN_EEPROM
1818

19-
#ifdef ENABLE_MAIN_ARDUINO
2019

21-
#define ENABLE_MAIN_EMPTY
22-
//#define ENABLE_MAIN_ARDUINO_GPIO
23-
//#define ENABLE_MAIN_ARDUINO_I2C
24-
//#define ENABLE_MAIN_ARDUINO_SPI
25-
//#define ENABLE_MAIN_ARDUINO_SPI_MAX31865
26-
27-
void terminate();
28-
29-
#endif // ENABLE_MAIN_ARDUINO

src/main_basic.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
#include "main.h"
3+
4+
#ifdef ENABLE_MAIN_BASIC
5+
6+
#include "Arduino.h"
7+
8+
void setup() {
9+
10+
}
11+
12+
void loop() {
13+
printf("Hallo World\n");
14+
delay(1000);
15+
16+
if(millis() > 10000)
17+
terminate();
18+
}
19+
20+
#endif // ENABLE_MAIN_BASIC

src/main_basic_digital_read.cpp

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)