-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw25.c
260 lines (184 loc) · 5.15 KB
/
w25.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
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
#include "stm32f4xx_hal.h"
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "w25.h"
static struct w25_device devs[W25_MAXDEVS];
size_t devcount = 0;
#define min(a, b) ((a) < (b) ? (a) : (b))
static int w25_getid(struct w25_device *dev)
{
uint8_t sbuf[4], rbuf[4];
sbuf[0] = 0x9f;
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(dev->hspi, sbuf, 1, 5000);
HAL_SPI_Receive(dev->hspi, rbuf, 3, 5000);
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_SET);
return ((rbuf[0] << 16) | (rbuf[1] << 8) | rbuf[2]);
}
static int w25_init(struct w25_device *dev)
{
uint8_t sbuf[4];
HAL_Delay(100);
sbuf[0] = 0x66;
sbuf[1] = 0x99;
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(dev->hspi, sbuf, 2, 5000);
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_SET);
HAL_Delay(100);
return w25_getid(dev);
}
static int w25_writeenable(struct w25_device *dev)
{
uint8_t sbuf[4];
sbuf[0] = 0x06;
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(dev->hspi, sbuf, 1, 5000);
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_SET);
return 0;
}
static int w25_writedisable(struct w25_device *dev)
{
uint8_t sbuf[4];
sbuf[0] = 0x04;
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(dev->hspi, sbuf, 1, 5000);
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_SET);
return 0;
}
static int w25_waitwrite(struct w25_device *dev)
{
uint8_t sbuf[4], rbuf[4];
sbuf[0] = 0x05;
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(dev->hspi, sbuf, 1, 5000);
do {
HAL_SPI_Receive(dev->hspi, rbuf, 1, 5000);
} while ((rbuf[0] & 0x01) == 0x01);
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_SET);
return 0;
}
static int w25_blockprotect(struct w25_device *dev, uint8_t flags)
{
uint8_t sbuf[4];
sbuf[0] = 0x50;
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(dev->hspi, sbuf, 1, 5000);
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_SET);
sbuf[0] = 0x01;
sbuf[1] = (flags & 0x0f) << 2;
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(dev->hspi, sbuf, 2, 5000);
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_SET);
return 0;
}
int w25_read(void *d, size_t addr, void *data, size_t sz)
{
struct w25_device *dev;
uint8_t sbuf[4];
dev = (struct w25_device *) d;
sbuf[0] = 0x03;
sbuf[1] = (addr >> 16) & 0xff;
sbuf[2] = (addr >> 8) & 0xff;
sbuf[3] = addr & 0xff;
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(dev->hspi, sbuf, 4, 5000);
HAL_SPI_Receive(dev->hspi, data, sz, 5000);
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_SET);
return 0;
}
int w25_write(void *d, size_t addr, const void *data, size_t sz)
{
struct w25_device *dev;
uint8_t sbuf[4];
dev = (struct w25_device *) d;
w25_waitwrite(dev);
w25_blockprotect(dev, 0x00);
w25_writeenable(dev);
sbuf[0] = 0x02;
sbuf[1] = (addr >> 16) & 0xff;
sbuf[2] = (addr >> 8) & 0xff;
sbuf[3] = addr & 0xff;
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(dev->hspi, sbuf, 4, 5000);
HAL_SPI_Transmit(dev->hspi, (uint8_t *) data, sz, 5000);
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_SET);
w25_waitwrite(dev);
w25_writedisable(dev);
w25_blockprotect(dev, 0x0f);
return 0;
}
int w25_eraseall(void *d)
{
struct w25_device *dev;
uint8_t sbuf[4];
dev = (struct w25_device *) d;
w25_waitwrite(dev);
w25_blockprotect(dev, 0x00);
w25_writeenable(dev);
sbuf[0] = 0xc7;
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(dev->hspi, sbuf, 1, 5000);
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_SET);
w25_waitwrite(dev);
w25_writedisable(dev);
w25_blockprotect(dev, 0x0f);
return 0;
}
int w25_erasesector(void *d, size_t addr)
{
struct w25_device *dev;
uint8_t sbuf[4];
dev = (struct w25_device *) d;
w25_waitwrite(dev);
w25_blockprotect(dev, 0x00);
w25_writeenable(dev);
sbuf[0] = 0x20;
sbuf[1] = (addr >> 16) & 0xff;
sbuf[2] = (addr >> 8) & 0xff;
sbuf[3] = addr & 0xff;
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(dev->hspi, sbuf, 4, 5000);
HAL_GPIO_WritePin(dev->gpio, dev->pin, GPIO_PIN_SET);
w25_waitwrite(dev);
w25_writedisable(dev);
w25_blockprotect(dev, 0x0f);
return 0;
}
int w25_writesector(void *d, size_t addr, const void *data,
size_t sz)
{
struct w25_device *dev;
int i;
dev = (struct w25_device *) d;
for (i = 0; i < sz; i += min(W25_PAGESIZE, sz - i))
w25_write(dev, addr + i, data + i, W25_PAGESIZE);
return 0;
}
int w25_ioctl(void *d, int req, ...)
{
return 0;
}
int initdevice(void *is, struct bdevice *dev)
{
memmove(devs + devcount, is, sizeof(struct w25_device));
sprintf(dev->name, "%s%d", "flash", devcount);
dev->priv = devs + devcount;
dev->read = w25_read;
dev->write = w25_write;
dev->ioctl = w25_ioctl;
dev->eraseall = w25_eraseall;
dev->erasesector = w25_erasesector;
dev->writesector = w25_writesector;
dev->writesize = W25_PAGESIZE;
dev->sectorsize = W25_SECTORSIZE;
dev->totalsize = W25_TOTALSIZE;
w25_init(devs + devcount);
devcount++;
return 0;
}
int w25_getdriver(struct driver *driver)
{
driver->initdevice = initdevice;
return 0;
}