-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiflash.c
189 lines (155 loc) · 4.36 KB
/
spiflash.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
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
/* spiflash.c */
#include "hello.h"
#include "spiflash.h"
#include "spi.h"
#include "write.h"
#include "timer.h"
void spiflash_reset(void)
{
LPC_GPIO->DATA[DATAREG] = LPC_GPIO->DATA[DATAREG] & ~(1<<11);
delay(1);
LPC_GPIO->DATA[DATAREG] = LPC_GPIO->DATA[DATAREG] | (1<<11);
}
void spiflash_wp_off(void)
{
LPC_GPIO->DATA[DATAREG] = LPC_GPIO->DATA[DATAREG] | (1<<3);
}
void spiflash_wp_on(void)
{
LPC_GPIO->DATA[DATAREG] = LPC_GPIO->DATA[DATAREG] & ~(1<<3);
}
void spiflash_read_deviceid(uint8_t *data, int len)
{
uint8_t command = 0x9f;
spi_start();
spi_transfer(&command, NULL, 1);
spi_transfer(NULL, data, len);
spi_stop();
}
void spiflash_short_command(uint8_t command)
{
spi_start();
spi_transfer(&command, NULL, 1);
spi_stop();
}
#define spiflash_address_mode3() spiflash_short_command(0xe9)
#define spiflash_address_mode4() spiflash_short_command(0xb7)
#define spiflash_clsr() spiflash_short_command(0x30)
#define spiflash_write_enable() spiflash_short_command(0x06)
#define spiflash_write_disable() spiflash_short_command(0x04)
uint8_t spiflash_status(uint8_t command, uint8_t mask)
{
uint8_t data;
spi_start();
spi_transfer(&command, NULL, 1);
do {
spi_transfer(NULL, &data, 1);
} while (data & mask);
spi_stop();
return data;
}
#define spiflash_status1() spiflash_status(0x05, 0)
#define spiflash_status2() spiflash_status(0x07, 0)
#define spiflash_status1_poll(mask) spiflash_status(0x05, (mask))
#define spiflash_status2_poll(mask) spiflash_status(0x07, (mask))
void spiflash_read(uint32_t address, uint8_t *data, int len)
{
uint32_t command = __builtin_bswap32(address) | 0x03;
// uart_write_string("R: ");
// uart_write_hex(command);
// uart_write_string("\r\n");
spi_start();
spi_transfer((uint8_t *)&command, NULL, 4);
spi_transfer(NULL, data, len);
spi_stop();
// uart_write_hex(((uint32_t *)data)[0]);
// uart_write_string("\r\n");
}
bool spiflash_write_page(uint32_t address, uint8_t *data, int len)
{
uint32_t command = __builtin_bswap32(address) | 0x02;
spiflash_wp_off();
spiflash_write_enable();
spi_start();
spi_transfer((uint8_t *)&command, NULL, 4);
spi_transfer(data, NULL, len);
spi_stop();
// uart_write_hex(spiflash_status1() | (spiflash_status2() << 16));
while ((spiflash_status1() & 0x01) && !(spiflash_status2() & 0x20))
;
spiflash_wp_on();
if (spiflash_status2() & 0x20) {
spiflash_clsr();
return false;
}
return true;
}
#define PAGE_SIZE 256
#define ERASE_SIZE 4096
#define PAGE(x) ((x) & ~(PAGE_SIZE-1))
bool spiflash_write(uint32_t address, uint8_t *data, int len)
{
while (len) {
uint32_t limit = PAGE(address) + PAGE_SIZE;
int pagelen = limit - address;
if (len < pagelen)
pagelen = len;
// uart_write_hex(address);
// uart_write_string(": ");
// uart_write_hex(pagelen);
// uart_write_string(": ");
// uart_write_hex(((uint32_t *)data)[0]);
// uart_write_string("\r\n");
if (!spiflash_write_page(address, data, pagelen))
return false;
address += pagelen;
data += pagelen;
len -= pagelen;
}
// uart_write_string("Write complete\r\n");
return true;
}
bool spiflash_erase_chip(void)
{
spiflash_wp_off();
spiflash_write_enable();
spiflash_short_command(0x60);
while ((spiflash_status1() & 0x01) && !(spiflash_status2() & 0x40))
;
spiflash_wp_on();
if (spiflash_status2() & 0x40) {
spiflash_clsr();
return false;
}
return true;
}
bool spiflash_erase_sector(uint32_t address)
{
uint32_t command = __builtin_bswap32(address) | 0x20;
// uart_write_string("E: ");
// uart_write_hex(command);
// uart_write_string("\r\n");
spiflash_wp_off();
spiflash_write_enable();
spi_start();
spi_transfer((uint8_t *)&command, NULL, 4);
spi_stop();
while ((spiflash_status1() & 0x01) && !(spiflash_status2() & 0x40))
;
spiflash_wp_on();
if (spiflash_status2() & 0x40) {
spiflash_clsr();
return false;
}
return true;
}
bool spiflash_erase(uint32_t address, int len)
{
while (len > 0) {
if (!spiflash_erase_sector(address))
return false;
address += ERASE_SIZE;
len -= ERASE_SIZE;
}
return true;
}