forked from bristlemouth/bm_protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_w25.cpp
More file actions
268 lines (259 loc) · 9.93 KB
/
debug_w25.cpp
File metadata and controls
268 lines (259 loc) · 9.93 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "debug_w25.h"
#include "FreeRTOS.h"
#include "FreeRTOS_CLI.h"
#include <string.h>
#include <stdlib.h>
#include "debug.h"
#include "cli.h"
#include <stdlib.h>
#include "crc.h"
#define BITMASK24BIT (0x00ffffff)
#define MAX_READ_LEN_BYTES (2048)
#define MAX_WRITE_LEN_BYTES (MAX_READ_LEN_BYTES)
#define BIG_WRITE_BLOCK_LEN (MAX_WRITE_LEN_BYTES)
static W25* _w25_instance = NULL;
static BaseType_t w25Command( char *writeBuffer,
size_t writeBufferLen,
const char *commandString);
static const CLI_Command_Definition_t cmdW25 = {
// Command string
"w25",
// Help string
"w25:\n"
" * w25 r <addr> <len>\n"
" * w25 w <addr> <str>\n"
" * w25 es <addr>\n"
" * w25 ec\n"
" * w25 bw <addr> <len>\n"
" * w25 test <addr> <len>\n",
// Command function
w25Command,
// Number of parameters (variable)
-1
};
void debugW25Init(W25* w25) {
_w25_instance = w25;
FreeRTOS_CLIRegisterCommand( &cmdW25 );
}
static BaseType_t w25Command( char *writeBuffer,
size_t writeBufferLen,
const char *commandString) {
BaseType_t parameterStringLength;
( void ) writeBuffer;
( void ) writeBufferLen;
do {
if(!_w25_instance) {
printf("w25 not initialized.\n");
break;
}
const char *parameter = FreeRTOS_CLIGetParameter(
commandString,
1, // Get the first parameter (command)
¶meterStringLength);
if(parameter == NULL) {
printf("ERR Invalid paramters\n");
break;
}
if (strncmp("r", parameter, parameterStringLength) == 0) {
const char *addr = FreeRTOS_CLIGetParameter(
commandString,
2,
¶meterStringLength);
if(parameterStringLength == 0) {
printf("ERR Invalid read paramters\n");
break;
}
const char *len = FreeRTOS_CLIGetParameter(
commandString,
3,
¶meterStringLength);
if(parameterStringLength == 0) {
printf("ERR Invalid read paramters\n");
break;
}
uint32_t address = atoi(addr) & BITMASK24BIT;
uint32_t read_len = atoi(len);
if(read_len > MAX_READ_LEN_BYTES) {
printf("Read size too large\n");
break;
}
uint8_t* read_buf = (uint8_t *)pvPortMalloc(read_len);
configASSERT(read_buf);
if(!_w25_instance->read(address, read_buf, read_len)){
printf("Read failed\n");
}
printf("Byte string:\n");
for(uint32_t i = 0; i < read_len; i++){
if(i>0){
printf(":");
}
if(i%8==0){
printf("\n");
}
printf("%02X", read_buf[i]);
}
printf("\n");
vPortFree(read_buf);
} else if (strncmp("w", parameter, parameterStringLength) == 0) {
const char *addr = FreeRTOS_CLIGetParameter(
commandString,
2,
¶meterStringLength);
if(parameterStringLength == 0) {
printf("ERR Invalid write paramters\n");
break;
}
const char *str = FreeRTOS_CLIGetParameter(
commandString,
3,
¶meterStringLength);
if(parameterStringLength == 0) {
printf("ERR Invalid write paramters\n");
break;
}
uint32_t address = atoi(addr) & BITMASK24BIT;
size_t write_len = parameterStringLength;
if(write_len > MAX_WRITE_LEN_BYTES) {
printf("Write size too large\n");
break;
}
uint8_t* wr_buf = (uint8_t *)pvPortMalloc(write_len);
configASSERT(wr_buf);
memcpy(wr_buf, str, write_len);
if(!_w25_instance->write(address, wr_buf, write_len)){
printf("Write failed\n");
} else {
printf("Write succeded!\n");
}
vPortFree(wr_buf);
} else if (strncmp("es", parameter, parameterStringLength) == 0) {
const char *addr = FreeRTOS_CLIGetParameter(
commandString,
2,
¶meterStringLength);
if(parameterStringLength == 0) {
printf("ERR Invalid erase sector paramters\n");
break;
}
uint32_t address = atoi(addr) & BITMASK24BIT;
if(!_w25_instance->eraseSector(address)){
printf("Erase sector failed\n");
} else {
printf("Erase sector succeded!\n");
}
} else if (strncmp("ec", parameter, parameterStringLength) == 0) {
if(!_w25_instance->eraseChip()){
printf("Erase chip failed\n");
} else {
printf("Erase chip succeded!\n");
}
} else if (strncmp("bw", parameter, parameterStringLength) == 0) {
const char *addr = FreeRTOS_CLIGetParameter(
commandString,
2,
¶meterStringLength);
if(parameterStringLength == 0) {
printf("ERR Invalid write paramters\n");
break;
}
const char *len = FreeRTOS_CLIGetParameter(
commandString,
3,
¶meterStringLength);
if(parameterStringLength == 0) {
printf("ERR Invalid write paramters\n");
break;
}
uint32_t address = atoi(addr) & BITMASK24BIT;
uint32_t write_len = atoi(len);
uint32_t blocklen = (write_len < BIG_WRITE_BLOCK_LEN) ? write_len : BIG_WRITE_BLOCK_LEN;
uint8_t* wr_buf = (uint8_t *)pvPortMalloc(blocklen);
configASSERT(wr_buf);
for(uint32_t i = 0; i < blocklen; i++) {
wr_buf[i] = 0xAA + ((i % 5) * 0x11);
}
uint32_t offset = 0;
while(write_len){
printf("Writing addr %lu, size %lu \n", (address + offset), blocklen);
if(!_w25_instance->write(address + offset, wr_buf, blocklen)){
printf("Write failed\n");
break;
}
offset += blocklen;
write_len -= blocklen;
blocklen = (write_len < BIG_WRITE_BLOCK_LEN) ? write_len : BIG_WRITE_BLOCK_LEN;
}
if(!write_len){
printf("Write succeded!\n");
}
vPortFree(wr_buf);
} else if (strncmp("test", parameter, parameterStringLength) == 0) {
const char *addr = FreeRTOS_CLIGetParameter(
commandString,
2,
¶meterStringLength);
if(parameterStringLength == 0) {
printf("ERR Invalid write paramters\n");
break;
}
const char *len = FreeRTOS_CLIGetParameter(
commandString,
3,
¶meterStringLength);
if(parameterStringLength == 0) {
printf("ERR Invalid write paramters\n");
break;
}
uint32_t address = atoi(addr) & BITMASK24BIT;
uint32_t write_len = atoi(len);
uint32_t blocklen = (write_len < BIG_WRITE_BLOCK_LEN) ? write_len : BIG_WRITE_BLOCK_LEN;
uint8_t* wr_buf = (uint8_t *)pvPortMalloc(blocklen);
uint32_t crc32_write = 0;
uint32_t crc32_read = 0;
bool first_loop = true;
configASSERT(wr_buf);
for(uint32_t i = 0; i < blocklen; i++) {
wr_buf[i] = (uint8_t) i;
}
uint32_t offset = 0;
do {
while(write_len){
printf("Writing addr %lu, size %lu \n", (address + offset), blocklen);
if(!_w25_instance->write(address + offset, wr_buf, blocklen)){
printf("Write failed\n");
break;
}
if(first_loop) {
crc32_write = crc32_ieee(wr_buf, blocklen);
first_loop = false;
} else {
crc32_write = crc32_ieee_update(crc32_write, wr_buf, blocklen);
}
offset += blocklen;
write_len -= blocklen;
blocklen = (write_len < BIG_WRITE_BLOCK_LEN) ? write_len : BIG_WRITE_BLOCK_LEN;
}
if(!write_len){
printf("Write succeded!\n");
} else {
printf("Write failed!\n");
break;
}
if(!_w25_instance->crc32Checksum(address,atoi(len),crc32_read)){
printf("Read CRC32 failed!\n");
break;
}
} while(0);
if(crc32_write == crc32_read && crc32_write != 0){
printf("flash test success, crc_w:%lu, crc_r:%lu\n", crc32_write, crc32_read);
} else {
printf("flash test failed, crc_w:%lu, crc_r:%lu\n", crc32_write, crc32_read);
}
vPortFree(wr_buf);
} else {
printf("ERR Invalid paramters\n");
break;
}
} while(0);
return pdFALSE;
}