/
XNAND.c
executable file
·115 lines (84 loc) · 1.73 KB
/
XNAND.c
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
#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "XSPI.h"
BOOL XNANDWaitReady(WORD timeout)
{
do {
if (!(XSPIReadByte(0x04) & 0x01))
return TRUE;
} while (timeout--);
return FALSE;
}
WORD XNANDGetStatus()
{
return XSPIReadWord(0x04);
}
WORD XNANDClearStatus()
{
BYTE tmp[4];
XSPIRead(4, tmp);
XSPIWrite(4, tmp);
}
WORD XNANDReadStart(DWORD block)
{
WORD res;
WORD tries = 0x1000;
XNANDClearStatus();
XSPIWriteDWORD(0x0C, block << 9);
XSPIWriteByte(0x08, 0x03);
if (!XNANDWaitReady(0x1000))
return 0x8011;
res = XNANDGetStatus();
XSPIWrite0(0x0C);
return res;
}
void XNANDReadProcess(BYTE *buffer, BYTE words) {
while (words--) {
XSPIWrite0(0x08);
XSPIRead(0x10, buffer);
buffer+=4;
}
}
WORD XNANDErase(DWORD block)
{
WORD res;
BYTE tmp[4];
XNANDClearStatus();
XSPIRead(0, tmp);
tmp[0] |= 0x08;
XSPIWrite(0, tmp);
XSPIWriteDWORD(0x0C, block << 9);
if (!XNANDWaitReady(0x1000))
return 0x8001;
XSPIWriteByte(0x08, 0xAA);
XSPIWriteByte(0x08, 0x55);
if (!XNANDWaitReady(0x1000))
return 0x8002;
XSPIWriteByte(0x08, 0x5);
if (!XNANDWaitReady(0x1000))
return 0x8003;
return XNANDGetStatus();
}
void XNANDWriteStart()
{
XNANDClearStatus();
XSPIWrite0(0x0C);
}
void XNANDWriteProcess(BYTE *buffer, BYTE words) {
while (words--) {
XSPIWrite(0x10, buffer);
XSPIWriteByte(0x08, 0x01);
buffer += 4;
}
}
WORD XNANDWriteExecute(DWORD block) {
WORD res;
WORD tries = 0x1000;
XSPIWriteDWORD(0x0C, block << 9);
XSPIWriteByte(0x08, 0x55);
XSPIWriteByte(0x08, 0xAA);
XSPIWriteByte(0x08, 0x4);
if (!XNANDWaitReady(0x1000))
return 0x8021;
return XNANDGetStatus();
}