Mercurial > ~darius > hgwebdir.cgi > stm32temp
annotate spiflash.c @ 87:e30fe4bb8011
Add missing prototype
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Fri, 13 Mar 2015 11:36:26 +1030 |
parents | 05ba84c7da97 |
children |
rev | line source |
---|---|
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
1 #include <stdio.h> |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
2 #include <stdint.h> |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
3 #include <string.h> |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
4 #include <stdlib.h> |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
5 #include <assert.h> |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
6 |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
7 #include "stm32f10x.h" |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
8 #include "spi.h" |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
9 #include "spiflash.h" |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
10 |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
11 #define FL_SELECT() GPIO_ResetBits(GPIOA, GPIO_Pin_4) |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
12 #define FL_DESELECT() GPIO_SetBits(GPIOA, GPIO_Pin_4) |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
13 |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
14 const char *flstattbl[] = { |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
15 "BUSY", |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
16 "WEL", |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
17 "BP0", |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
18 "BP1", |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
19 "BP2", |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
20 "BP3", |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
21 "AAI", |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
22 "BPL" |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
23 }; |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
24 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
25 #define RW_IDLE 0 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
26 #define RW_RUNNING 1 |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
27 |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
28 static int writestate = RW_IDLE; |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
29 static int readstate = RW_IDLE; |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
30 |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
31 void |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
32 spiflash4kerase(uint32_t addr) { |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
33 spiflashenablewrite(); /* Enable writing */ |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
34 |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
35 FL_SELECT(); /* Select device */ |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
36 |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
37 SPI_WriteByte(FL_4KERASE); /* Send command */ |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
38 SPI_WriteByte(addr >> 16); /* Send address */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
39 SPI_WriteByte(addr >> 8); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
40 SPI_WriteByte(addr); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
41 |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
42 FL_DESELECT(); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
43 |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
44 //fputs("4k erase ", stdout); |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
45 spiflashwait(); |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
46 } |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
47 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
48 void |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
49 spiflashwait(void) { |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
50 uint8_t cnt; |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
51 |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
52 /* Wait for not BUSY */ |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
53 for (cnt = 0; (spiflashreadstatus() & FL_BUSY) != 0; cnt++) |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
54 ; |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
55 |
75 | 56 //printf("cnt = %d\n", cnt); |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
57 } |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
58 |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
59 uint16_t |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
60 spiflashreadid(void) { |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
61 uint8_t fac, dev; |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
62 |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
63 FL_SELECT(); /* Select device */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
64 |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
65 SPI_WriteByte(FL_RDID); /* Send command */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
66 SPI_WriteByte(0x00); /* Send address cycles (ID data starts at 0) */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
67 SPI_WriteByte(0x00); |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
68 SPI_WriteByte(0x00); |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
69 fac = SPI_WriteByte(0x00); /* Read ID */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
70 dev = SPI_WriteByte(0x00); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
71 |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
72 FL_DESELECT(); /* De-select device */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
73 |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
74 return fac << 8 | dev; |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
75 } |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
76 |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
77 void |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
78 spiflashenablewrite(void) { |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
79 FL_SELECT(); /* Select device */ |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
80 |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
81 SPI_WriteByte(FL_WREN); /* Send command */ |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
82 |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
83 FL_DESELECT(); /* De-select device */ |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
84 } |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
85 |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
86 uint8_t |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
87 spiflashreadstatus(void) { |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
88 uint8_t status; |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
89 |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
90 FL_SELECT(); /* Select device */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
91 |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
92 SPI_WriteByte(FL_RDSR); /* Send command */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
93 SPI_WriteByte(0x00); /* Send dummy byte for address cycle */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
94 status = SPI_WriteByte(0x00); /* Read status */ |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
95 |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
96 FL_DESELECT(); /* De-select device */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
97 |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
98 return status; |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
99 } |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
100 |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
101 void |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
102 spiflashwritestatus(uint8_t status) { |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
103 /* Enable status write */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
104 FL_SELECT(); /* Select device */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
105 SPI_WriteByte(FL_EWSR); /* Send command */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
106 SPI_WriteByte(0x00); /* Send data byte */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
107 FL_DESELECT(); |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
108 |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
109 /* Actually write status */ |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
110 FL_SELECT(); /* Re-select device for new command */ |
8
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
111 SPI_WriteByte(FL_WRSR); /* Send command */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
112 SPI_WriteByte(status); /* Send data byte */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
113 FL_DESELECT(); /* De-select device */ |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
114 } |
58d76cf522ff
Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
115 |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
116 uint8_t |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
117 spiflashread(uint32_t addr) { |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
118 uint8_t data; |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
119 |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
120 FL_SELECT(); /* Select device */ |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
121 |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
122 SPI_WriteByte(FL_READ); /* Send command */ |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
123 SPI_WriteByte(addr >> 16); /* Send address */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
124 SPI_WriteByte(addr >> 8); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
125 SPI_WriteByte(addr); |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
126 data = SPI_WriteByte(0x00); /* Read data */ |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
127 |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
128 FL_DESELECT(); /* De-select device */ |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
129 |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
130 return data; |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
131 } |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
132 |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
133 void |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
134 spiflashwrite(uint32_t addr, uint8_t data) { |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
135 spiflashwait(); |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
136 spiflashenablewrite(); /* Enable writes */ |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
137 |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
138 FL_SELECT(); /* Select device */ |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
139 |
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
140 SPI_WriteByte(FL_BYTEPROG); /* Send command */ |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
141 SPI_WriteByte(addr >> 16); /* Send address */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
142 SPI_WriteByte(addr >> 8); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
143 SPI_WriteByte(addr); |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
144 SPI_WriteByte(data); /* Write data */ |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
145 |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
146 FL_DESELECT(); /* De-select device */ |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
147 |
21
bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
148 } |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
149 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
150 /* |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
151 * fStream reading looks like so |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
152 * |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
153 */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
154 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
155 void |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
156 spiflashstartread(uint32_t addr) { |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
157 assert(readstate == RW_IDLE); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
158 |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
159 FL_SELECT(); /* Select device */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
160 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
161 SPI_WriteByte(FL_READ); /* Send command */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
162 SPI_WriteByte(addr >> 16); /* Send address */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
163 SPI_WriteByte(addr >> 8); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
164 SPI_WriteByte(addr); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
165 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
166 readstate = RW_RUNNING; |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
167 } |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
168 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
169 uint8_t |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
170 spiflashreadbyte(void) { |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
171 assert(readstate == RW_RUNNING); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
172 return SPI_WriteByte(0x00); /* Read data */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
173 } |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
174 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
175 void |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
176 spiflashstopread(void) { |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
177 assert(readstate == RW_RUNNING); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
178 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
179 FL_DESELECT(); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
180 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
181 readstate = RW_IDLE; |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
182 } |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
183 |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
184 /* |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
185 * Auto increment writing looks like so |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
186 * |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
187 * Enable writing CS, WREN, nCS |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
188 * Send start address & first data word CS, AAI + addr + data, nCS |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
189 * Send subsequent words wait for nBUSY, CS, AAI + data, nCS |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
190 * ... |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
191 * Disable writing CS, WRDI, nCS |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
192 * |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
193 * XXX: EBSY command links SO to flash busy state, I don't think the |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
194 * STM32 could sample it without switching out of SPI mode. |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
195 */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
196 void |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
197 spiflashstartwrite(uint32_t addr, uint16_t data) { |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
198 assert(writestate == RW_IDLE); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
199 |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
200 spiflashenablewrite(); /* Enable writes */ |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
201 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
202 FL_SELECT(); /* Select device */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
203 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
204 SPI_WriteByte(FL_AAIWP); /* Send command */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
205 SPI_WriteByte(addr >> 16); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
206 SPI_WriteByte(addr >> 8); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
207 SPI_WriteByte(addr & 0xff); /* Send address */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
208 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
209 SPI_WriteByte(data & 0xff); /* Write LSB */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
210 SPI_WriteByte(data >> 8); /* Write MSB */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
211 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
212 FL_DESELECT(); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
213 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
214 writestate = RW_RUNNING; |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
215 } |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
216 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
217 void |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
218 spiflashwriteword(uint16_t data) { |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
219 assert(writestate == RW_RUNNING); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
220 |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
221 //fputs("write word ", stdout); |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
222 spiflashwait(); /* Wait until not busy */ |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
223 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
224 FL_SELECT(); /* Select device */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
225 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
226 SPI_WriteByte(FL_AAIWP); /* Send command */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
227 SPI_WriteByte(data & 0xff); /* Write LSB */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
228 SPI_WriteByte(data >> 8); /* Write MSB */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
229 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
230 FL_DESELECT(); /* De-select device */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
231 } |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
232 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
233 void |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
234 spiflashstopwrite(void) { |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
235 assert(writestate == RW_RUNNING); |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
236 |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
237 //fputs("flash stop write start ", stdout); |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
238 spiflashwait(); /* Wait until not busy */ |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
239 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
240 FL_SELECT(); /* Select device */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
241 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
242 SPI_WriteByte(FL_WRDI); /* Send command */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
243 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
244 FL_DESELECT(); /* Deselect device */ |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
245 |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
246 //fputs("flash stop write end ", stdout); |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
247 spiflashwait(); /* Wait until not busy */ |
25
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
248 |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
249 writestate = RW_IDLE; |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
250 } |
a9cc07caa801
Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
251 |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
252 int |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
253 spiflashreadblock(uint32_t addr, uint32_t len, void *_data) { |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
254 uint32_t *data = _data; |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
255 uint32_t flashcrc, ramcrc, tmp; |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
256 |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
257 /* Must be a multiple of 4 due to CRC check */ |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
258 assert(len % 4 == 0); |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
259 |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
260 spiflashstartread(addr); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
261 CRC_ResetDR(); |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
262 for (int i = len; i > 0; i -= 4) { |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
263 tmp = spiflashreadbyte() | |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
264 spiflashreadbyte() << 8 | |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
265 spiflashreadbyte() << 16 | |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
266 spiflashreadbyte() << 24; |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
267 |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
268 *data = tmp; |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
269 CRC_CalcCRC(tmp); |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
270 data++; |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
271 } |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
272 |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
273 flashcrc = spiflashreadbyte(); |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
274 flashcrc |= spiflashreadbyte() << 8; |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
275 flashcrc |= spiflashreadbyte() << 16; |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
276 flashcrc |= spiflashreadbyte() << 24; |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
277 |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
278 spiflashstopread(); |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
279 |
69
cf9eb08b8b23
CRC a word at a time rather than the whole block (since we are reading/writing a word at a time).
Daniel O'Connor <darius@dons.net.au>
parents:
67
diff
changeset
|
280 ramcrc = CRC_GetCRC(); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
281 |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
282 //printf("RAM CRC 0x%08x Flash CRC 0x%08x\n", (uint)ramcrc, (uint)flashcrc); |
69
cf9eb08b8b23
CRC a word at a time rather than the whole block (since we are reading/writing a word at a time).
Daniel O'Connor <darius@dons.net.au>
parents:
67
diff
changeset
|
283 if (ramcrc == flashcrc) |
cf9eb08b8b23
CRC a word at a time rather than the whole block (since we are reading/writing a word at a time).
Daniel O'Connor <darius@dons.net.au>
parents:
67
diff
changeset
|
284 return 1; |
cf9eb08b8b23
CRC a word at a time rather than the whole block (since we are reading/writing a word at a time).
Daniel O'Connor <darius@dons.net.au>
parents:
67
diff
changeset
|
285 else |
cf9eb08b8b23
CRC a word at a time rather than the whole block (since we are reading/writing a word at a time).
Daniel O'Connor <darius@dons.net.au>
parents:
67
diff
changeset
|
286 return 0; |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
287 } |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
288 |
70
aaf0603d7f88
Add routine to CRC a block of flash. Use it to verify a flash block write.
Daniel O'Connor <darius@dons.net.au>
parents:
69
diff
changeset
|
289 uint32_t |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
290 spiflashcrcblock(uint32_t addr, uint32_t len) { |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
291 uint32_t tmp; |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
292 |
70
aaf0603d7f88
Add routine to CRC a block of flash. Use it to verify a flash block write.
Daniel O'Connor <darius@dons.net.au>
parents:
69
diff
changeset
|
293 assert(len % 4 == 0); |
aaf0603d7f88
Add routine to CRC a block of flash. Use it to verify a flash block write.
Daniel O'Connor <darius@dons.net.au>
parents:
69
diff
changeset
|
294 |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
295 CRC_ResetDR(); |
70
aaf0603d7f88
Add routine to CRC a block of flash. Use it to verify a flash block write.
Daniel O'Connor <darius@dons.net.au>
parents:
69
diff
changeset
|
296 |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
297 spiflashstartread(addr); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
298 for (; len > 0; len -= 4) { |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
299 tmp = spiflashreadbyte() | |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
300 spiflashreadbyte() << 8 | |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
301 spiflashreadbyte() << 16 | |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
302 spiflashreadbyte() << 24; |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
303 CRC_CalcCRC(tmp); |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
304 } |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
305 spiflashstopread(); |
70
aaf0603d7f88
Add routine to CRC a block of flash. Use it to verify a flash block write.
Daniel O'Connor <darius@dons.net.au>
parents:
69
diff
changeset
|
306 |
aaf0603d7f88
Add routine to CRC a block of flash. Use it to verify a flash block write.
Daniel O'Connor <darius@dons.net.au>
parents:
69
diff
changeset
|
307 return CRC_GetCRC(); |
aaf0603d7f88
Add routine to CRC a block of flash. Use it to verify a flash block write.
Daniel O'Connor <darius@dons.net.au>
parents:
69
diff
changeset
|
308 } |
aaf0603d7f88
Add routine to CRC a block of flash. Use it to verify a flash block write.
Daniel O'Connor <darius@dons.net.au>
parents:
69
diff
changeset
|
309 |
aaf0603d7f88
Add routine to CRC a block of flash. Use it to verify a flash block write.
Daniel O'Connor <darius@dons.net.au>
parents:
69
diff
changeset
|
310 int |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
311 spiflashwriteblock(uint32_t addr, uint32_t len, void *_data) { |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
312 uint16_t *data = _data; |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
313 uint32_t crc, vcrc, tmp; |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
314 |
75 | 315 //printf("Writing %u bytes to 0x%06x\n", (uint)len, (uint)addr); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
316 |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
317 /* Ensure data is |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
318 * - 16 bit aligned |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
319 * - a multiple of 32 bits in length (for CRCs, the flash only need 16 bits) |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
320 * - not longer than a sector |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
321 */ |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
322 assert(addr % 2 == 0); |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
323 assert(len % 4 == 0); |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
324 assert(len <= 4096); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
325 |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
326 /* Disable write protect */ |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
327 spiflashwritestatus(0x00); |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
328 |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
329 /* Erase sector */ |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
330 spiflash4kerase(addr); |
69
cf9eb08b8b23
CRC a word at a time rather than the whole block (since we are reading/writing a word at a time).
Daniel O'Connor <darius@dons.net.au>
parents:
67
diff
changeset
|
331 |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
332 /* Write data */ |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
333 CRC_ResetDR(); |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
334 for (uint i = 0; i < len / 2; i++) { |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
335 //printf("0x%04x: %04x\n", i, *data); |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
336 if (i == 0) |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
337 spiflashstartwrite(addr, *data); |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
338 else |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
339 spiflashwriteword(*data); |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
340 |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
341 if (i % 2 == 0) |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
342 tmp = *data; |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
343 else { |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
344 tmp |= *data << 16; |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
345 CRC_CalcCRC(tmp); |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
346 } |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
347 data++; |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
348 } |
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
349 |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
350 /* Calculate CRC */ |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
351 crc = CRC_GetCRC(); |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
352 //printf("CRC is 0x%08x\n", (uint)crc); |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
353 |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
354 /* Write CRC */ |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
355 spiflashwriteword(crc); |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
356 spiflashwriteword(crc >> 16); |
70
aaf0603d7f88
Add routine to CRC a block of flash. Use it to verify a flash block write.
Daniel O'Connor <darius@dons.net.au>
parents:
69
diff
changeset
|
357 |
83
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
358 spiflashstopwrite(); |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
359 |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
360 /* Read back and check CRC */ |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
361 vcrc = spiflashcrcblock(addr, len); |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
362 //printf("CRC read back as 0x%08x\n", (uint)vcrc); |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
363 if (vcrc != crc) |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
364 return 1; |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
365 else |
05ba84c7da97
Add a flash layer for compatibility (in future).
Daniel O'Connor <darius@dons.net.au>
parents:
80
diff
changeset
|
366 return 0; |
27
5c9d2e3d6591
Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
367 } |
80
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
368 |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
369 void |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
370 spiflashprintstatus(uint8_t status, FILE *out) { |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
371 for (unsigned int i = 0; i < sizeof(flstattbl) / sizeof(flstattbl[0]); i++) |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
372 if (status & 1 << i) { |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
373 fputs(flstattbl[i], out); |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
374 fputs(" ", out); |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
375 } |
1a4573062b37
Reshuffle in preparation for being able to have a common API for SPI flash and (emulated) EEPROM.
Daniel O'Connor <darius@dons.net.au>
parents:
75
diff
changeset
|
376 } |