1 | #include "AT45DB.h"
|
---|
2 |
|
---|
3 | #define StatusReg 0xD7
|
---|
4 | #define WAIT(x) for(uint8_t i = 0; i < x; i++){}
|
---|
5 |
|
---|
6 | extern SPI_HandleTypeDef hspi3;
|
---|
7 | uint8_t FlashCommand[8];
|
---|
8 |
|
---|
9 | void spiTransmit(uint8_t *data, uint16_t len)
|
---|
10 | {
|
---|
11 | HAL_SPI_Transmit(&hspi3, data, len, 5000);
|
---|
12 | // uint8_t f;
|
---|
13 | // while(SPI3->SR & SPI_SR_BSY);
|
---|
14 | // for(uint16_t i = 0; i < len; i++){
|
---|
15 | // SPI3->DR = *data++;
|
---|
16 | // while(!(SPI3->SR & SPI_SR_TXE));
|
---|
17 | // while(!(SPI3->SR & SPI_SR_RXNE));
|
---|
18 | // f = SPI3->DR;
|
---|
19 | // }
|
---|
20 | }
|
---|
21 |
|
---|
22 | void spiReceive(uint8_t *data, uint16_t len)
|
---|
23 | {
|
---|
24 | HAL_SPI_Receive(&hspi3, data, len, 5000);
|
---|
25 | // while(SPI3->SR & SPI_SR_BSY);
|
---|
26 | // for(uint16_t i = 0; i < len; i++){
|
---|
27 | // SPI3->DR = 0x00;
|
---|
28 | // while(!(SPI3->SR & SPI_SR_TXE));
|
---|
29 | // while(!(SPI3->SR & SPI_SR_RXNE));
|
---|
30 | // *data++ = SPI3->DR;
|
---|
31 | // }
|
---|
32 | }
|
---|
33 |
|
---|
34 | uint8_t ReadStatusFlash (void)
|
---|
35 | {
|
---|
36 | uint8_t FlashStatus;
|
---|
37 | FlashCommand[0] = StatusReg;
|
---|
38 | FLASH_CS_SET;
|
---|
39 | // __ASM("nop");
|
---|
40 | spiTransmit(FlashCommand, 1);
|
---|
41 | spiReceive(&FlashStatus, 1);
|
---|
42 | //HAL_SPI_Transmit(&hspi3, FlashCommand, 1, 500);
|
---|
43 | // HAL_SPI_Receive(&hspi3, &FlashStatus, 1, 500);
|
---|
44 | FLASH_CS_RESET;
|
---|
45 | WAIT(5);
|
---|
46 | return FlashStatus;
|
---|
47 | }
|
---|
48 |
|
---|
49 | void Flash_StatusRDY (void)
|
---|
50 | {
|
---|
51 | uint8_t FlashStatus;
|
---|
52 | FlashCommand[0] = StatusReg;
|
---|
53 | FLASH_CS_SET;
|
---|
54 | // __ASM("nop");
|
---|
55 | spiTransmit( FlashCommand, 1);
|
---|
56 | //HAL_SPI_Transmit(&hspi3, FlashCommand, 1, 500);
|
---|
57 | do{
|
---|
58 | spiReceive(&FlashStatus, 1);
|
---|
59 | //HAL_SPI_Receive(&hspi3, &FlashStatus, 1, 500);
|
---|
60 | }while(!(FlashStatus & 0x80));
|
---|
61 | FLASH_CS_RESET;
|
---|
62 | WAIT(5);
|
---|
63 | }
|
---|
64 |
|
---|
65 | void Flash_SetPage512(void)
|
---|
66 | {
|
---|
67 | FlashCommand[0] = 0x3D;
|
---|
68 | FlashCommand[1] = 0x2A;
|
---|
69 | FlashCommand[2] = 0x80;
|
---|
70 | FlashCommand[3] = 0xA6;
|
---|
71 | FLASH_CS_SET;
|
---|
72 | //__ASM("nop");
|
---|
73 | spiTransmit(FlashCommand, 4);
|
---|
74 | // HAL_SPI_Transmit(&hspi3, FlashCommand, 4, 500);
|
---|
75 | FLASH_CS_RESET;
|
---|
76 | WAIT(5);
|
---|
77 | Flash_StatusRDY();
|
---|
78 | }
|
---|
79 |
|
---|
80 | void Flash_FullErase(void)
|
---|
81 | {
|
---|
82 | FlashCommand[0] = 0xC7;
|
---|
83 | FlashCommand[1] = 0x94;
|
---|
84 | FlashCommand[2] = 0x80;
|
---|
85 | FlashCommand[3] = 0x9A;
|
---|
86 | FLASH_CS_SET;
|
---|
87 | //__ASM("nop");
|
---|
88 | spiTransmit(FlashCommand, 4);
|
---|
89 | //HAL_SPI_Transmit(&hspi3, FlashCommand, 4, 500);
|
---|
90 | FLASH_CS_RESET;
|
---|
91 | WAIT(5);
|
---|
92 | Flash_StatusRDY();
|
---|
93 | }
|
---|
94 |
|
---|
95 | void Flash_ByteWrite(uint32_t bADDR, uint16_t num, uint8_t *TXdata)
|
---|
96 | {
|
---|
97 | uint32_t Pg_ADDR,Byte_ADDR ;
|
---|
98 | Pg_ADDR = bADDR & 0x3FFE00 ;
|
---|
99 | Byte_ADDR = bADDR & 0x0001FF ;
|
---|
100 | Flash_StatusRDY();
|
---|
101 | FlashCommand[0] = 0x53;
|
---|
102 | FlashCommand[1] = (uint8_t)(Pg_ADDR>>16);
|
---|
103 | FlashCommand[2] = (uint8_t)(Pg_ADDR>>8);
|
---|
104 | FlashCommand[3] = (uint8_t)(Pg_ADDR);
|
---|
105 | FLASH_CS_SET;
|
---|
106 | //__ASM("nop");
|
---|
107 | spiTransmit(FlashCommand, 4);
|
---|
108 | //HAL_SPI_Transmit(&hspi3, FlashCommand, 4, 500);
|
---|
109 | FLASH_CS_RESET;
|
---|
110 | WAIT(5);
|
---|
111 | Flash_StatusRDY();
|
---|
112 | FlashCommand[0] = 0x84;
|
---|
113 | FlashCommand[1] = (uint8_t)(Byte_ADDR>>16);
|
---|
114 | FlashCommand[2] = (uint8_t)(Byte_ADDR>>8);
|
---|
115 | FlashCommand[3] = (uint8_t)(Byte_ADDR);
|
---|
116 | FLASH_CS_SET;
|
---|
117 | // __ASM("nop");
|
---|
118 | spiTransmit(FlashCommand, 4);
|
---|
119 | spiTransmit(TXdata, num);
|
---|
120 | //HAL_SPI_Transmit(&hspi3, FlashCommand, 4, 500);
|
---|
121 | //HAL_SPI_Transmit(&hspi3, TXdata, num, 5000);
|
---|
122 | FLASH_CS_RESET;
|
---|
123 | WAIT(5);
|
---|
124 | Flash_StatusRDY();
|
---|
125 | FlashCommand[0] = 0x83;
|
---|
126 | FlashCommand[1] = (uint8_t)(Pg_ADDR>>16);
|
---|
127 | FlashCommand[2] = (uint8_t)(Pg_ADDR>>8);
|
---|
128 | FlashCommand[3] = (uint8_t)(Pg_ADDR);
|
---|
129 | FLASH_CS_SET;
|
---|
130 | //__ASM("nop");
|
---|
131 | spiTransmit(FlashCommand, 4);
|
---|
132 | //HAL_SPI_Transmit(&hspi3, FlashCommand, 4, 500);
|
---|
133 | FLASH_CS_RESET;
|
---|
134 | WAIT(5);
|
---|
135 | }
|
---|
136 |
|
---|
137 | void Flash_Read(uint32_t bADDR, uint16_t num, uint8_t *RXdata)
|
---|
138 | {
|
---|
139 | Flash_StatusRDY();
|
---|
140 | FlashCommand[0] = 0xE8;
|
---|
141 | FlashCommand[1] = (uint8_t)(bADDR>>16);
|
---|
142 | FlashCommand[2] = (uint8_t)(bADDR>>8);
|
---|
143 | FlashCommand[3] = (uint8_t)(bADDR);
|
---|
144 | FlashCommand[4] = 0x00;
|
---|
145 | FlashCommand[5] = 0x00;
|
---|
146 | FlashCommand[6] = 0x00;
|
---|
147 | FlashCommand[7] = 0x00;
|
---|
148 | FLASH_CS_SET;
|
---|
149 | //__ASM("nop");
|
---|
150 | spiTransmit(FlashCommand, 8);
|
---|
151 | spiReceive(RXdata, num);
|
---|
152 | //HAL_SPI_Transmit(&hspi3, FlashCommand, 8, 500);
|
---|
153 | // HAL_SPI_Receive(&hspi3, RXdata, num, 5000);
|
---|
154 | FLASH_CS_RESET;
|
---|
155 | WAIT(5);
|
---|
156 | }
|
---|
157 |
|
---|
158 | void Flash_PageErase(uint16_t page) // page 0 - 8191
|
---|
159 | {
|
---|
160 | uint32_t Pg_ADDR = 0x00000000;
|
---|
161 | Pg_ADDR = (page << 9);
|
---|
162 | FlashCommand[0] = 0x81;
|
---|
163 | FlashCommand[1] = (uint8_t)(Pg_ADDR>>16);
|
---|
164 | FlashCommand[2] = (uint8_t)(Pg_ADDR>>8);
|
---|
165 | FlashCommand[3] = (uint8_t)(Pg_ADDR);
|
---|
166 | FLASH_CS_SET;
|
---|
167 | //__ASM("nop");
|
---|
168 | spiTransmit(FlashCommand, 4);
|
---|
169 | //HAL_SPI_Transmit(&hspi3, FlashCommand, 4, 500);
|
---|
170 | FLASH_CS_RESET;
|
---|
171 | WAIT(5);
|
---|
172 | Flash_StatusRDY();
|
---|
173 | }
|
---|
174 |
|
---|
175 | void Flash_Read_ID(uint8_t *ID)
|
---|
176 | {
|
---|
177 | Flash_StatusRDY();
|
---|
178 | FlashCommand[0] = 0x9F;
|
---|
179 | FLASH_CS_SET;
|
---|
180 | // __ASM("nop");
|
---|
181 | spiTransmit(FlashCommand, 1);
|
---|
182 | spiReceive(ID, 4);
|
---|
183 | //HAL_SPI_Transmit(&hspi3, FlashCommand, 1, 500);
|
---|
184 | //HAL_SPI_Receive(&hspi3, ID, 4, 500);
|
---|
185 | FLASH_CS_RESET;
|
---|
186 | WAIT(5);
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|