📄 nf.lst
字号:
246 5 }
247 4 else /* else decrease block number */
248 4 {
249 5 if (block == start)
250 5 {
251 6 block = start + 1023;
252 6 }
253 5 else
254 5 {
255 6 block--;
256 6 }
257 5 }
258 4 j++;
259 4 }
260 3 while ((!block_valid ) && (j < 1024));
261 3 if (j == 1024)
262 3 return KO;
263 3
264 3 nf_lut_block[i] = block; /* Update look up table address */
265 3 }
266 2 }
267 1
268 1 /**********************************************************/
269 1 /* Create the look-up table */
270 1 /* Process is done zone per zone and by step of 256 blocks*/
271 1 /* ********************************************************/
272 1 for (nf_zone = 0; nf_zone < NF_ZONE_MAX_CPT; nf_zone++) /* for each zone */
273 1 {
274 2 half_page = 0;
275 2 if (!(lut_is_present & (0x01 << nf_zone))) /* If not LUT */
276 2 {
277 3 start = 0x00;
278 3 end = 0x80; /* 256 bytes for gl_buffer <-> 128 blocks */
279 3 free_bloc_pos = (Uint16)(last_physical_used_block[nf_zone]);
280 3 gl_address = (Uint32)(nf_lut_block[nf_zone]) << 5;
281 3 do
282 3 {
283 4 nf_init_buffer(); /* Reinitialize the buffer */
284 4 temp_address = (Uint32)(nf_zone) << 15; /* We start at the beginning */
285 4 block = (Uint16)(nf_zone) << 10;
286 4 Nf_wait_busy();
287 4 for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--) /* for each block */
288 4 {
289 5 Nf_read_open_C_area(temp_address, 0x05);
290 5 byte_5 = Nf_rd_byte(); /* Block status byte */
291 5 if (byte_5 == 0xFF) /* If not a bad block */
292 5 {
293 6 block2.b[0] = Nf_rd_byte(); /* Read logical block address */
294 6 block2.b[1] = Nf_rd_byte();
295 6 if ((block2.b[0] & 0xF8) == 0x10)
296 6 {
297 7
298 7 block2.w = (block2.w & 0x0FFF) >> 1;
299 7 if ((block2.w < end) && (block2.w >= start))
300 7 { /* Save logical block value in the buffer */
301 8 gl_buffer[2 * block2.b[1]] = block >> 8;
C51 COMPILER V7.50 NF 09/20/2005 21:47:45 PAGE 6
302 8 gl_buffer[2 * block2.b[1] + 1] = block;
303 8 }
304 7 }
305 6 }
306 5 temp_address += 32;
307 5 block++;
308 5 }
309 4
310 4 /* affect to the free physical block a fictive logical block */
311 4 /* free physical block => gl_buffer[x] = 0xFF */
312 4 temp_address = (Uint32)(free_bloc_pos) << 5;
313 4 for (j = 0; j <= 0xFE; j += 2)
314 4 {
315 5 if (gl_buffer[j] == 0xFF)
316 5 {
317 6 do /* Search free physical block */
318 6 {
319 7 temp_address += 32;
320 7 if (temp_address >= ((Uint32)(nf_zone + 1) << 15))
321 7 temp_address = (Uint32)(nf_zone) << 15;
322 7
323 7 Nf_read_open_C_area(temp_address, 0x05);
324 7
325 7 byte_5 = Nf_rd_byte(); /* Invalid/Valid block */
326 7 byte_6 = Nf_rd_byte(); /* Used/Unused block */
327 7 }
328 6 while (((byte_6 != 0xFF) && (byte_6 != 0xE8)) || (byte_5 != 0xFF));
329 6
330 6 free_bloc_pos = temp_address >> 5;
331 6 gl_buffer[j] = (free_bloc_pos >> 8) | 0x80;
332 6 gl_buffer[j + 1] = free_bloc_pos;
333 6 }
334 5 }
335 4
336 4 if (half_page == 0)
337 4 {
338 5 Nf_write_open_A_area(gl_address, 0x00); /* Write first part of LUT */
339 5 nf_download_buffer(); /* Write buffer */
340 5 Nf_send_command(NF_PAGE_PROGRAM_CMD); /* Launch programmation */
341 5 }
342 4 else
343 4 {
344 5 Nf_write_open_B_area(gl_address, 0x00); /* Write second part of LUT */
345 5 nf_download_buffer(); /* Write buffer */
346 5 Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF);/* Write redundant data */
347 5 Nf_wr_byte(0xFF); Nf_wr_byte(0xFF);
348 5 Nf_wr_byte(0xFF);
349 5 Nf_wr_byte(0xE8); Nf_wr_byte(0xFF); /* Logical block value */
350 5 Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); /* ECC area 2 */
351 5 Nf_wr_byte(0xE8); Nf_wr_byte(0xFF); /* Logical block value */
352 5 Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); Nf_wr_byte(0xFF); /* ECC area 1 */
353 5 Nf_send_command(NF_PAGE_PROGRAM_CMD);
354 5 gl_address++;
355 5 }
356 4 half_page = ~half_page;
357 4 start += 0x80; /* process next 128 logical block */
358 4 end += 0x80;
359 4 }
360 3 while (start < NF_BLOCK_PER_ZONE);
361 3 }
362 2 }
363 1 #undef half_page
C51 COMPILER V7.50 NF 09/20/2005 21:47:45 PAGE 7
364 1 #undef temp_address
365 1 #undef i
366 1 #undef start
367 1 #undef end
368 1 #undef free_bloc_pos
369 1
370 1 return OK;
371 1 }
372
373
374 /*F**************************************************************************
375 * NAME: nf_init
376 *----------------------------------------------------------------------------
377 * PARAMS:
378 *
379 * return:
380 * OK : init complete
381 * KO : - NF not supported or not recognise
382 *----------------------------------------------------------------------------
383 * PURPOSE:
384 * NF initialisation
385 *----------------------------------------------------------------------------
386 * EXAMPLE:
387 *----------------------------------------------------------------------------
388 * NOTE:
389 *----------------------------------------------------------------------------
390 * REQUIREMENTS:
391 *****************************************************************************/
392 bit nf_init (void)
393 {
394 1
395 1 P2 = NF_IDLE_STATE_ADD;
396 1 Nf_CS_ON();
397 1
398 1 Nf_send_command(NF_RESET_CMD);
399 1 Nf_wait_busy();
400 1 #if (NF_CAPACITY_AUTO_DETECT == TRUE) /* Auto Detect the type of nand-flash */
401 1 Nf_send_command(NF_READ_ID_CMD);
402 1 Nf_send_address(0x00);
403 1 Nf_rd_byte(); /* Maker code */
404 1 switch (Nf_rd_byte()) /* Device code */
405 1 {
406 2 /***************************************************************************/
407 2 case 0x73 : /*------- 16 Mbyte --------*/
408 2 nf_device_type = NF_SIZE_16MB;
409 2 nf_zone_max = 1; /* 1 zone : 1024 blocks */
410 2 nf_mem_size = NF_SECTOR_SIZE_16MB - MEM_RESERVED_SIZE;
411 2 nf_4_cycle_address = 0; /* 3 address cycles */
412 2 break;
413 2 /***************************************************************************/
414 2 case 0x75 : /*------- 32 Mbyte --------*/
415 2 nf_device_type = NF_SIZE_32MB;
416 2 nf_zone_max = 2; /* 2 zones : 2048 blocks */
417 2 nf_mem_size = NF_SECTOR_SIZE_32MB - MEM_RESERVED_SIZE;
418 2 nf_4_cycle_address = 0; /* 3 address cycles */
419 2 break;
420 2 /***************************************************************************/
421 2 case 0x76 : /*------- 64 Mbyte --------*/
422 2 nf_device_type = NF_SIZE_64MB;
423 2 nf_zone_max = 4; /* 4 zones : 4096 blocks */
424 2 nf_mem_size = NF_SECTOR_SIZE_64MB - MEM_RESERVED_SIZE;
425 2 nf_4_cycle_address = 1; /* 4 address cycles */
C51 COMPILER V7.50 NF 09/20/2005 21:47:45 PAGE 8
426 2 break;
427 2 /***************************************************************************/
428 2 case 0x79 : /*------ 128 Mbyte --------*/
429 2 nf_device_type = NF_SIZE_128MB;
430 2 nf_zone_max = 8; /* 8 zones : 8192 blocks */
431 2 nf_mem_size = NF_SECTOR_SIZE_128MB - MEM_RESERVED_SIZE;
432 2 nf_4_cycle_address = 1; /* 4 address cycles */
433 2 break;
434 2 default:
435 2 return KO;
436 2 }
437 1 #else
nf_mem_size = NF_DISK_SIZE - MEM_RESERVED_SIZE;
#endif
440 1
441 1 nf_reserved_space_start = nf_mem_size + 1;
442 1 return OK;
443 1
444 1 }
445
446
447 /*F**************************************************************************
448 * NAME: nf_read_open
449 *----------------------------------------------------------------------------
450 * PARAMS:
451 * pos: address of the logic sector to read (size 512 bytes)
452 *
453 * return:
454 * Update memory for reading
455 *----------------------------------------------------------------------------
456 * PURPOSE:
457 * Low level memory read update
458 *----------------------------------------------------------------------------
459 * EXAMPLE:
460 *----------------------------------------------------------------------------
461 * NOTE:
462 *----------------------------------------------------------------------------
463 * REQUIREMENTS:
464 *****************************************************************************/
465 bit nf_read_open (Uint32 pos)
466 {
467 1 Uint16 physical_block;
468 1
469 1 Nf_CS_ON();
470 1
471 1 if (nf_lut_modified)
472 1 {
473 2 nf_reassign_block();
474 2 nf_lut_modified = FALSE;
475 2 }
476 1
477 1 gl_ptr_mem = pos;
478 1 gl_cpt_page = 0;
479 1
480 1 /* Determine the logical block value */
481 1 nf_logical_block = (gl_ptr_mem >> 5);
482 1
483 1 /* Determinate zone */
484 1 nf_zone = nf_logical_block / 1000;
485 1
486 1 /* Each zone have 1000 data blocks */
487 1 nf_logical_block = nf_logical_block - (1000 * (Uint16)(nf_zone));
C51 COMPILER V7.50 NF 09/20/2005 21:47:45 PAGE 9
488 1
489 1 /* Calculate the address where are the physical block value */
490 1 gl_address = ((Uint32)(nf_lut_block[nf_zone])<<5) + ((Uint32)(nf_logical_block) >> 8);
491 1 gl_address += ((Uint32)(nf_lut_index[nf_zone])<<2);
492 1 /* Open the look-up table */
493 1 Nf_wait_busy();
494 1 if (nf_logical_block & 0x80)
495 1 {
496 2 Nf_send_command(NF_READ_B_AREA_CMD); /* 2nd half page */
497 2 Nf_send_address((nf_logical_block << 1) - 256);
498 2 }
499 1 else
500 1 {
501 2 Nf_send_command(NF_READ_A_AREA_CMD); /* first half page */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -