forked from nuket/ArduinoShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEepromShellModule.cpp
More file actions
214 lines (173 loc) · 6.48 KB
/
EepromShellModule.cpp
File metadata and controls
214 lines (173 loc) · 6.48 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
Arduino Shell
Copyright (c) 2015 Max Vilimpoc
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "CommandAndParams.h"
#include "EepromShellModule.h"
#include <EEPROM.h>
#if ENABLE_UNIT_TESTS
#include <assert.h>
#endif
#define EEPROM_ROW_LENGTH 16
#define OUTPUT_ROW_LENGTH 80
namespace ArduinoShell
{
EepromShellModule::EepromShellModule(Stream& serialOut) :
serialOut(serialOut)
{
}
void EepromShellModule::setup()
{
serialOut.print(F("Size of EEPROM: "));
serialOut.print(EEPROM.length());
serialOut.println(F(" bytes."));
}
const String& EepromShellModule::help()
{
serialOut.println(F("EEPROM Shell commands:\r\n"
" p\r\n"
" p <address>\r\n"
" p <address> <rows>\r\n"
" wb <address> <byte-value> -- writes the byte-value to the address\r\n"));
}
struct EepromRow
{
uint8_t data[EEPROM_ROW_LENGTH];
};
static bool is_ascii(char c)
{
// if ('a' <= c && c <= 'z') return true;
// if ('A' <= c && c <= 'Z') return true;
if (0x20 <= c && c <= 0x7e) return true;
return false;
}
/**
* Prints the contents of EEPROM from startAddress to startAddress + length.
*
* Works best with multiples of EEPROM_ROW_LENGTH (16) byte-sized blocks.
*
* Will not print less than EEPROM_ROW_LENGTH bytes, and will round down
* to the next-lower block size.
*/
void EepromShellModule::printContents(uint32_t startAddress, uint32_t length)
{
// Quick check that range is valid.
if (startAddress >= EEPROM.length())
{
char output[OUTPUT_ROW_LENGTH] = {'\0'};
snprintf(output, OUTPUT_ROW_LENGTH, "Address (0x%04x) is out of EEPROM bounds [0, %04x].", startAddress, EEPROM.length());
serialOut.println(output);
return;
}
// Round to nearest row.
startAddress &= 0xFFF0;
uint16_t rows = length / EEPROM_ROW_LENGTH;
uint8_t mod = length % EEPROM_ROW_LENGTH; // How many bytes in the last row?
static const char HEXVAL[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
EepromRow eepromRow;
// Generate a hexdump string for the row.
char outputRow[OUTPUT_ROW_LENGTH] = {0};
for (int row = 0; row < rows; ++row)
{
int address = startAddress + row * EEPROM_ROW_LENGTH;
EEPROM.get(address, eepromRow);
memset(outputRow, 0x20, (OUTPUT_ROW_LENGTH - 1));
outputRow[0] = HEXVAL[(address & 0xF000) >> 12];
outputRow[1] = HEXVAL[(address & 0x0F00) >> 8];
outputRow[2] = HEXVAL[(address & 0x00F0) >> 4];
outputRow[3] = HEXVAL[(address & 0x000F)];
outputRow[5] = '|';
const uint8_t HEX_START = 7;
for (int column = 0; column < EEPROM_ROW_LENGTH; ++column)
{
uint8_t msb = (eepromRow.data[column] & 0xF0) >> 4;
uint8_t lsb = eepromRow.data[column] & 0x0F;
uint8_t linePos = HEX_START + (column * 3);
outputRow[linePos] = HEXVAL[msb];
outputRow[linePos + 1] = HEXVAL[lsb];
}
outputRow[HEX_START + (EEPROM_ROW_LENGTH * 3)] = '|';
const uint8_t ASCII_START = HEX_START + (EEPROM_ROW_LENGTH * 3) + 2;
for (int column = 0; column < EEPROM_ROW_LENGTH; ++column)
{
outputRow[ASCII_START + column] = is_ascii(eepromRow.data[column]) ? eepromRow.data[column] : '.';
}
serialOut.println(outputRow);
}
}
void EepromShellModule::run(String rawCommand)
{
// Parse it.
CommandAndParams cp(rawCommand, serialOut);
// cp.print();
if (cp.command.equals("p"))
{
if (cp.paramCount == 0)
{
// Example: p
//
// Print entire EEPROM.
printContents(0, EEPROM.length());
}
else
if (cp.paramCount == 1)
{
// Example: p 0x10
//
// Print row containing address.
long int address = strtol(cp.params[0].c_str(), NULL, 0);
printContents(address & 0xFFF0, EEPROM_ROW_LENGTH);
}
else
if (cp.paramCount == 2)
{
// Example: p 0x00 10
//
// Print the specified number of rows, starting from the row containing address.
long int address = strtol(cp.params[0].c_str(), NULL, 0);
long int rows = strtol(cp.params[1].c_str(), NULL, 0);
// Find number of rows between address and EEPROM.length();
if (0 <= address && address < EEPROM.length())
{
int rowsLeft = (EEPROM.length() - address) >> 4;
rows = ((rows < rowsLeft) ? rows : rowsLeft);
printContents(address, rows * EEPROM_ROW_LENGTH);
}
}
}
else
if (cp.command.equals("wb") && cp.paramCount == 2)
{
// Example: wb 0x0010 0x41
// Writes the specified byte to the specified location.
// Convert to long.
long int address = strtol(cp.params[0].c_str(), NULL, 0);
// Serial.println(address);
if (!(0 <= address < EEPROM.length())) return;
// Write the byte to the address.
long int data = strtol(cp.params[1].c_str(), NULL, 0) & 0xFF;
// Serial.println(data);
EEPROM.put(address, data);
printContents(address & 0xFFF0, EEPROM_ROW_LENGTH);
}
else
if (cp.command.equals(F("clear-eeprom")) == 0)
{
}
}
} // namespace ArduinoShell