Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f981320

Browse files
committedNov 11, 2020
Remove hard-coded __AVR__ in favor of platform configuration
1 parent cb67e90 commit f981320

File tree

7 files changed

+207
-2
lines changed

7 files changed

+207
-2
lines changed
 

‎CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99
### Added
10-
- Add `__AVR__` to defines when compiling
1110
- `arduino_ci_remote.rb` CLI switch `--skip-examples-compilation`
1211
- Add support for `diditalPinToPort()`, `digitalPinToBitMask()`, and `portOutputRegister()`
1312
- `CppLibrary.header_files` to find header files
@@ -16,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1615
- `CppLibrary.arduino_library_dependencies` to list the dependent libraries specified by the library.properties file
1716
- `CppLibrary.print_stack_dump` prints stack trace dumps (on Windows specifically) to the console if encountered
1817
- Definitions for Arduino zero
18+
- Support for mock EEPROM (but only if board supports it)
1919

2020
### Changed
2121
- Move repository from https://github.com/ianfixes/arduino_ci to https://github.com/Arduino-CI/arduino_ci

‎REFERENCE.md

+37
Original file line numberDiff line numberDiff line change
@@ -586,3 +586,40 @@ unittest(spi) {
586586
assertEqual("LMNOe", String(inBuf));
587587
}
588588
```
589+
590+
### EEPROM
591+
592+
`EEPROM` is a global with a simple API to read and write bytes to persistent memory (like a tiny hard disk) given an `int` location. Since the Arduino core already provides this as a global, and the core API is sufficient for basic testing (read/write), there is no direct tie to the `GODMODE` API. (If you need more, such as a log of intermediate values, enter a feature request.)
593+
594+
```C++
595+
unittest(eeprom)
596+
{
597+
uint8_t a;
598+
// size
599+
assertEqual(EEPROM_SIZE, EEPROM.length());
600+
// initial values
601+
a = EEPROM.read(0);
602+
assertEqual(255, a);
603+
// write and read
604+
EEPROM.write(0, 24);
605+
a = EEPROM.read(0);
606+
assertEqual(24, a);
607+
// update
608+
EEPROM.write(1, 14);
609+
EEPROM.update(1, 22);
610+
a = EEPROM.read(1);
611+
assertEqual(22, a);
612+
// put and get
613+
const float f1 = 0.025f;
614+
float f2 = 0.0f;
615+
EEPROM.put(5, f1);
616+
assertEqual(0.0f, f2);
617+
EEPROM.get(5, f2);
618+
assertEqual(0.025f, f2);
619+
// array access
620+
int val = 10;
621+
EEPROM[2] = val;
622+
a = EEPROM[2];
623+
assertEqual(10, a);
624+
}
625+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <ArduinoUnitTests.h>
2+
#include <Arduino.h>
3+
#include <Godmode.h>
4+
5+
// Only run EEPROM tests if there is hardware support!
6+
#if defined(EEPROM_SIZE)
7+
#include <EEPROM.h>
8+
9+
GodmodeState* state = GODMODE();
10+
11+
unittest_setup()
12+
{
13+
state->reset();
14+
}
15+
16+
unittest(length)
17+
{
18+
assertEqual(EEPROM_SIZE, EEPROM.length());
19+
}
20+
21+
unittest(firstRead)
22+
{
23+
uint8_t a = EEPROM.read(0);
24+
assertEqual(255, a);
25+
}
26+
27+
unittest(writeRead)
28+
{
29+
EEPROM.write(0, 24);
30+
uint8_t a = EEPROM.read(0);
31+
assertEqual(24, a);
32+
33+
EEPROM.write(0, 128);
34+
a = EEPROM.read(0);
35+
assertEqual(128, a);
36+
37+
EEPROM.write(0, 255);
38+
a = EEPROM.read(0);
39+
assertEqual(255, a);
40+
41+
int addr = EEPROM_SIZE / 2;
42+
EEPROM.write(addr, 63);
43+
a = EEPROM.read(addr);
44+
assertEqual(63, a);
45+
46+
addr = EEPROM_SIZE - 1;
47+
EEPROM.write(addr, 188);
48+
a = EEPROM.read(addr);
49+
assertEqual(188, a);
50+
}
51+
52+
unittest(updateWrite)
53+
{
54+
EEPROM.write(1, 14);
55+
EEPROM.update(1, 22);
56+
uint8_t a = EEPROM.read(1);
57+
assertEqual(22, a);
58+
}
59+
60+
unittest(putGet)
61+
{
62+
const float f1 = 0.025f;
63+
float f2 = 0.0f;
64+
EEPROM.put(5, f1);
65+
assertEqual(0.0f, f2);
66+
EEPROM.get(5, f2);
67+
assertEqual(0.025f, f2);
68+
}
69+
70+
unittest(array)
71+
{
72+
int val = 10;
73+
EEPROM[2] = val;
74+
uint8_t a = EEPROM[2];
75+
assertEqual(10, a);
76+
}
77+
78+
#endif
79+
80+
unittest_main()

‎cpp/arduino/EEPROM.h

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#pragma once
2+
3+
#include <cassert>
4+
#include <inttypes.h>
5+
#include <Godmode.h>
6+
7+
// Does the current board have EEPROM?
8+
#ifndef EEPROM_SIZE
9+
// In lieu of an "EEPROM.h not found" error for unsupported boards
10+
#error "EEPROM library not available for your board"
11+
#endif
12+
13+
class EEPROMClass {
14+
private:
15+
GodmodeState* state;
16+
public:
17+
// constructor
18+
EEPROMClass() {
19+
state = GODMODE();
20+
}
21+
// array subscript operator
22+
uint8_t &operator[](const int index) {
23+
assert(index < EEPROM_SIZE);
24+
return state->eeprom[index];
25+
}
26+
27+
uint8_t read(const int index) {
28+
assert(index < EEPROM_SIZE);
29+
return state->eeprom[index];
30+
}
31+
32+
void write(const int index, const uint8_t value) {
33+
assert(index < EEPROM_SIZE);
34+
state->eeprom[index] = value;
35+
}
36+
37+
void update(const int index, const uint8_t value) {
38+
assert(index < EEPROM_SIZE);
39+
state->eeprom[index] = value;
40+
}
41+
42+
uint16_t length() { return EEPROM_SIZE; }
43+
44+
// read any object
45+
template <typename T> T &get(const int index, T &object) {
46+
uint8_t *ptr = (uint8_t *)&object;
47+
for (int i = 0; i < sizeof(T); ++i) {
48+
*ptr++ = read(index + i);
49+
}
50+
return object;
51+
}
52+
53+
// write any object
54+
template <typename T> const T &put(const int index, T &object) {
55+
const uint8_t *ptr = (const uint8_t *)&object;
56+
for (int i = 0; i < sizeof(T); ++i) {
57+
write(index + i, *ptr++);
58+
}
59+
return object;
60+
}
61+
};
62+
63+
// global available in Godmode.cpp
64+
extern EEPROMClass EEPROM;

‎cpp/arduino/Godmode.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,8 @@ SPIClass SPI = SPIClass(&GODMODE()->spi.dataIn, &GODMODE()->spi.dataOut);
113113

114114
// defined in Wire.h
115115
TwoWire Wire = TwoWire();
116+
117+
#if defined(EEPROM_SIZE)
118+
#include <EEPROM.h>
119+
EEPROMClass EEPROM;
120+
#endif

‎cpp/arduino/Godmode.h

+19
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ unsigned long micros();
3434
#endif
3535
#endif
3636

37+
// different EEPROM implementations have different macros that leak out
38+
#if !defined(EEPROM_SIZE) && defined(E2END) && (E2END)
39+
// public value indicates that feature is available
40+
#define EEPROM_SIZE (E2END + 1)
41+
// local array size
42+
#define _EEPROM_SIZE EEPROM_SIZE
43+
#else
44+
// feature is not available but we want to have the array so other code compiles
45+
#define _EEPROM_SIZE (0)
46+
#endif
47+
3748
class GodmodeState {
3849
private:
3950
struct PortDef {
@@ -60,6 +71,7 @@ class GodmodeState {
6071
struct PortDef serialPort[NUM_SERIAL_PORTS];
6172
struct InterruptDef interrupt[MOCK_PINS_COUNT]; // not sure how to get actual number
6273
struct PortDef spi;
74+
uint8_t eeprom[_EEPROM_SIZE];
6375

6476
void resetPins() {
6577
for (int i = 0; i < MOCK_PINS_COUNT; ++i) {
@@ -99,13 +111,20 @@ class GodmodeState {
99111
}
100112
}
101113

114+
void resetEEPROM() {
115+
for(int i = 0; i < EEPROM_SIZE; ++i) {
116+
eeprom[i] = 255;
117+
}
118+
}
119+
102120
void reset() {
103121
resetClock();
104122
resetPins();
105123
resetInterrupts();
106124
resetPorts();
107125
resetSPI();
108126
resetMmapPorts();
127+
resetEEPROM();
109128
seed = 1;
110129
}
111130

‎lib/arduino_ci/cpp_library.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def build_for_test_with_configuration(test_file, aux_libraries, gcc_binary, ci_g
398398
executable = Pathname.new("unittest_#{base}.bin").expand_path
399399
File.delete(executable) if File.exist?(executable)
400400
arg_sets = []
401-
arg_sets << ["-std=c++0x", "-o", executable.to_s, "-DARDUINO=100", "-D__AVR__"]
401+
arg_sets << ["-std=c++0x", "-o", executable.to_s, "-DARDUINO=100"]
402402
if libasan?(gcc_binary)
403403
arg_sets << [ # Stuff to help with dynamic memory mishandling
404404
"-g", "-O1",

0 commit comments

Comments
 (0)
Please sign in to comment.