📄 dataflash.lst
字号:
175 3 }
176 2
177 2 if(len != 0)
178 2 {
C51 COMPILER V8.02 DATAFLASH 05/08/2008 13:16:17 PAGE 4
179 3 wr_buf1(0,buf,len);
180 3 prog_main_pg_buf1_erase(pg);
181 3 }
182 2 }
183 1 }
184
185 /*
186 ********************************************************************************
187 * Function Name : DF_get_stat
188 * Description : Get dataflash current status.
189 * Parameter : none.
190 * Return : unsigned char type, status register
191 * bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
192 * RDY/BUSY COMP 1 0 1 1 PROTECT PG SIZE
193 ********************************************************************************
194 */
195 unsigned char DF_get_stat(void)
196 {
197 1 unsigned char stat;
198 1 start_SPI();
199 1 wr_byte(OP_CODE_RD_STAT_REG);
200 1 stat = rd_byte();
201 1 stop_SPI();
202 1
203 1 return stat;
204 1 }
205
206 /*
207 ********************************************************************************
208 * Function Name : DF_get_id
209 * Description : Get manufacturer and device ID.
210 * Parameter : none.
211 * Return : unsigned long type, for AT45DB161D it will be 0x1F2600
212 ********************************************************************************
213 */
214 unsigned long DF_get_id(void)
215 {
216 1 unsigned char i;
217 1 unsigned long id = 0;
218 1
219 1 start_SPI();
220 1 wr_byte(OP_CODE_RD_MANU_DEV_ID);
221 1 for(i = 0;i < 4;i ++)
222 1 {
223 2 id <<= 8;
224 2 id |= rd_byte();
225 2 }
226 1 stop_SPI();
227 1
228 1 return id;
229 1 }
230
231 /*
232 ********************************************************************************
233 * INTERNAL FUNCTIONS
234 ********************************************************************************
235 */
236 /* read buffer1 */
237 static void rd_buf1(unsigned int addr,unsigned char * buf,unsigned int len)
238 {
239 1 start_SPI();
240 1 wr_byte(OP_CODE_RD_BUF1);
C51 COMPILER V8.02 DATAFLASH 05/08/2008 13:16:17 PAGE 5
241 1 wr_byte(0x00);
242 1 wr_byte((unsigned char)(addr >> 8));
243 1 wr_byte((unsigned char)addr);
244 1 wr_byte(0x00); /* 1 dummy bytes is need */
245 1
246 1 while(len--) *buf++ = rd_byte();
247 1
248 1 stop_SPI();
249 1 }
250
251 /* write buffer1 */
252 static void wr_buf1(unsigned int addr,unsigned char * buf,unsigned int len)
253 {
254 1 start_SPI();
255 1 wr_byte(OP_CODE_WR_BUF1);
256 1 wr_byte(0x00);
257 1 wr_byte((unsigned char)(addr >> 8));
258 1 wr_byte((unsigned char)addr);
259 1
260 1 while(len--) wr_byte(*buf++);
261 1
262 1 stop_SPI();
263 1 }
264
265 /* buffer1 to main memory page program with built-in erase */
266 static void prog_main_pg_buf1_erase(unsigned int pg_addr)
267 {
268 1
269 1 start_SPI();
270 1 wr_byte(OP_CODE_WR_BUF1_MAIN_ERASE);
271 1 wr_byte((unsigned char)(pg_addr >> 7));
272 1 wr_byte((unsigned char)(pg_addr << 1));
273 1 wr_byte(0x00);
274 1 stop_SPI();
275 1
276 1 wait_rdy(); /* page erase and programming time 40ms(max) */
277 1 }
278
279 /* page erase */
280 static void erase_pg(unsigned int pg_addr)
281 {
282 1 start_SPI();
283 1 wr_byte(OP_CODE_ERASE_PAGE);
284 1 wr_byte((unsigned char)(pg_addr >> 7));
285 1 wr_byte((unsigned char)(pg_addr << 7));
286 1 wr_byte(0x00);
287 1 stop_SPI();
288 1
289 1 wait_rdy(); /* page erase time 35ms(max) */
290 1 }
291
292 /* main memory page to buffer 1 transfer */
293 static void get_main_pg_buf1(unsigned int pg_addr)
294 {
295 1 start_SPI();
296 1 wr_byte(OP_CODE_RD_MAIN_BUF1);
297 1 wr_byte((unsigned char)(pg_addr >> 7));
298 1 wr_byte((unsigned char)(pg_addr << 1));
299 1 wr_byte(0x00);
300 1 stop_SPI();
301 1
302 1 wait_rdy(); /* transfer a page shall take place 200us(max) */
C51 COMPILER V8.02 DATAFLASH 05/08/2008 13:16:17 PAGE 6
303 1 }
304
305 /* read a byte from SPI use SPI mode0 */
306 static unsigned char rd_byte(void)
307 {
308 1 unsigned char i,dat;
309 1 for(i = 0;i < 8;i++)
310 1 {
311 2 AT45_SCK = 1; /* SCK high time 6.8ns */
312 2 dat <<= 1;
313 2 dat |= AT45_SO;
314 2 AT45_SCK = 0; /* SCK low time 6.8ns */
315 2 /* output data valid 6ns */
316 2 }
317 1 return dat;
318 1 }
319
320 /* write a byte from SPI use SPI mode0 */
321 static void wr_byte(unsigned char dat)
322 {
323 1 unsigned char i;
324 1 for(i = 0;i < 8;i++)
325 1 {
326 2 AT45_SI = dat & 0x80; /* input data setup time 2ns */
327 2 dat <<= 1;
328 2 AT45_SCK = 1; /* SCK high time 6.8ns */
329 2 /* input data hold time 3ns */
330 2 AT45_SCK = 0; /* SCK low time 6.8ns */
331 2 }
332 1 }
333
334 /* wait until device is ready */
335 static void wait_rdy(void)
336 {
337 1 start_SPI();
338 1 wr_byte(OP_CODE_RD_STAT_REG);
339 1 while(!(rd_byte() & 0x80)); /* the sequence will repeat itself */
340 1 stop_SPI();
341 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1075 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 5 47
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -