-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
79 lines (73 loc) · 2.14 KB
/
Copy pathmain.cpp
File metadata and controls
79 lines (73 loc) · 2.14 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
#include "WinbondW25N.h"
#include <cerrno>
W25N flash = W25N();
uint8_t buf[2048];
#define CS_PIN 10
#define SPEED 32000000
void usage(char *sProg)
{
printf("Usage: %s <r|w|e> <out|in file only r|w>\n", sProg);
}
int main(int iArg, char *asArg[])
{
if (iArg < 2) {
usage(asArg[0]);
return 1;
}
if(flash.begin(CS_PIN, SPEED) == 0){
printf("Register Protection: 0x%.2X\nRegister Config: 0x%.2X\nRegister Status: 0x%.2X\n", flash.getStatusReg(W25N_PROT_REG), flash.getStatusReg(W25N_CONFIG_REG), flash.getStatusReg(W25N_STAT_REG));
printf("Flash init successful\n");
} else {
printf("Flash init Failed\n");
return 1;
}
if (!strcmp(asArg[1], "r")) {
if (iArg != 3) {
usage(asArg[0]);
return 1;
}
FILE *fOut = fopen(asArg[2], "w");
if (fOut == NULL) {
printf("Error file: %s\n", strerror(errno));
return 1;
}
uint32_t iMaxPage = flash.getMaxPage();
printf("Max page %d\n", iMaxPage);
for (uint32_t iNdx = 0; iNdx < iMaxPage; iNdx++) {
flash.pageDataRead(iNdx);
memset(buf, 0, sizeof(buf));
flash.read(0, buf, sizeof(buf));
fwrite(buf, sizeof(buf), 1, fOut);
printf("%d of %d\r", iNdx, iMaxPage);
}
fclose(fOut);
} else if (!strcmp(asArg[1], "w")) {
if (iArg != 3) {
usage(asArg[0]);
return 1;
}
FILE *fIn = fopen(asArg[2], "r");
if (fIn == NULL) {
printf("Error file: %s\n", strerror(errno));
return 1;
}
flash.bulkErase();
uint32_t iMaxPage = flash.getMaxPage();
printf("Max page %d\n", iMaxPage);
for (uint32_t iNdx = 0; iNdx < iMaxPage; iNdx++) {
memset(buf, 0, sizeof(buf));
if (fread(buf, sizeof(buf), 1, fIn) != 1) {
printf("fread failed\n");
break;
}
flash.loadProgData(0, buf, sizeof(buf));
flash.ProgramExecute(iNdx);
printf("%d of %d\r", iNdx, iMaxPage);
}
fclose(fIn);
} else if (!strcmp(asArg[1], "e")) {
flash.bulkErase();
} else {
printf("Operation not supported\n");
}
}