-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspiflash.c
162 lines (114 loc) · 3.12 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
/* (c) 2016-07-01 Jens Hauke <[email protected]> */
#include "spiflash.h"
#include "spi.h"
void spiflash_end(void) {
spi_cs_clr();
}
uint8_t spiflash_is_busy(void) {
uint8_t res;
spiflash_cmd_start(W25Q80BV_CMD_READ_STAT1);
res = (spiflash_read_uint8() & W25Q80BV_MASK_STAT_BUSY);
spiflash_end();
return res;
}
// Wait until not busy
void spiflash_wait(void) {
spiflash_cmd_start(W25Q80BV_CMD_READ_STAT1);
// ToDo: Timeout?
while (spiflash_read_uint8() & W25Q80BV_MASK_STAT_BUSY);
spiflash_end();
}
void spiflash_end_wait(void) {
spi_cs_clr();
spiflash_wait();
spiflash_end();
}
void spiflash_cmd_start(uint8_t cmd) {
spi_cs_set();
spi_transfer(cmd);
}
void spiflash_cmd_addr_start(uint8_t cmd, uint32_t addr) {
spiflash_cmd_start(cmd);
spi_transfer((uint8_t)(addr >> 16));
spi_transfer((uint8_t)(addr >> 8));
spi_transfer((uint8_t)(addr));
}
static inline
void spiflash_write_enable(void) {
spiflash_cmd_start(W25Q80BV_CMD_WRITE_ENABLE);
spiflash_end();
}
void spiflash_write_start(uint32_t addr) {
spiflash_write_enable();
spiflash_cmd_addr_start(W25Q80BV_CMD_PAGE_PROG, addr);
}
void spiflash_read_start(uint32_t addr) {
spiflash_cmd_addr_start(W25Q80BV_CMD_READ_DATA, addr);
}
uint8_t spiflash_read_uint8(void) {
return spi_transfer(0);
}
uint16_t spiflash_read_uint16(void) {
return (uint16_t)spiflash_read_uint8() << 8 | spiflash_read_uint8();
}
uint32_t spiflash_read_uint32(void) {
return (uint32_t)spiflash_read_uint16() << 16 | spiflash_read_uint16();
}
uint16_t spiflash_read_uint16_le(void) {
return spiflash_read_uint8() | (uint16_t)spiflash_read_uint8() << 8;
}
uint32_t spiflash_read_uint32_le(void) {
return spiflash_read_uint16_le() | (uint32_t)spiflash_read_uint16_le() << 16;
}
void spiflash_write_uint16(uint16_t val) {
spiflash_write_uint8(val >> 8);
spiflash_write_uint8(val);
}
void spiflash_write_uint32(uint32_t val) {
spiflash_write_uint16(val >> 16);
spiflash_write_uint16(val);
}
void spiflash_write_uint16_le(uint16_t val) {
spiflash_write_uint8(val);
spiflash_write_uint8(val >> 8);
}
void spiflash_write_uint32_le(uint32_t val) {
spiflash_write_uint16_le(val);
spiflash_write_uint16_le(val >> 16);
}
void spiflash_erase4k(uint32_t addr) {
spiflash_write_enable();
spiflash_cmd_addr_start(W25Q80BV_CMD_ERASE_4K, addr);
spiflash_end_wait();
}
void spiflash_erase32k(uint32_t addr) {
spiflash_write_enable();
spiflash_cmd_addr_start(W25Q80BV_CMD_ERASE_32K, addr);
spiflash_end_wait();
}
void spiflash_erase64k(uint32_t addr) {
spiflash_write_enable();
spiflash_cmd_addr_start(W25Q80BV_CMD_ERASE_64K, addr);
spiflash_end_wait();
}
void spiflash_chip_erase(void) {
spiflash_write_enable();
spiflash_cmd_start(W25Q80BV_CMD_CHIP_ERASE);
spiflash_end_wait();
}
uint16_t spiflash_device_id(void) {
uint16_t id;
spiflash_cmd_addr_start(W25Q80BV_CMD_READ_MAN_DEV_ID, 0x0000);
id = spiflash_read_uint16();
spiflash_end();
return id;
}
uint64_t spiflash_unique_id(void) {
uint64_t id;
spiflash_cmd_addr_start(W25Q80BV_CMD_READ_UNIQUE_ID, 0x0000);
spi_transfer(0);
id = (uint64_t)spiflash_read_uint32() << 32;
id |= spiflash_read_uint32();
spiflash_end();
return id;
}