not setting cluster 128

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys, have a wee look at the code below and see if you can help me to discover why when the cluster number is 0x80/128 that it does not set the field properly.

After I allocate cluster number 128, when I go back for a new cluster it give me 128 back again!!!

 

unsigned int allocate_fat_entry(struct ff_file * stream){

	unsigned int free_fat = 0, * fat32;


	// allocate temporary storage to wipe clean the allocated fat entry
	char * cluster_data = safe_malloc(total_addr_space(stream)); memset(cluster_data, 0, total_addr_space(stream));



	// allocate a DMA sector to process
	char * fat = stream->cache_base;

	// FAT Allocation Table Address
	unsigned int fat_table_addr =  endian16(stream->Master_Parameter_Block->BPB_ResvdSecCnt) + stream->partition_index;//

	int fat_cluster = 0;

	// Scan FAT Allocation Table
	for(fat_cluster = 0;; fat_cluster++){



		// read the first entries in the FAT table
		dma_hardware_read(1, fat_table_addr + fat_cluster, 1, fat, 0);

		// system is using FAT32, 32 bits per FAT location
		fat32 = (unsigned int *)fat;


		// search for free fat entry
		for(free_fat = 0; ((* fat32 != 0x00000000) && (free_fat < 128)); free_fat++, fat32++);



		// Free Cluster found!!!
		if(* fat32 == 0x00000000){

			// End of Cluster
			* fat32 = 0xffffffff;

			// update FAT Allocation Table
			dma_hardware_write(1, fat_table_addr + fat_cluster, 1, fat, 0);
			break;
		}
	}

	// Cluster Address
	free_fat += (fat_cluster * 128);


	// clear located new fat entry
	unsigned int addr = cluster_address(stream, free_fat);

	// Clear Cluster data
	dma_hardware_write(1, addr, stream->Master_Parameter_Block->BPB_SecPerClus, cluster_data, 0);
	safe_free(cluster_data);

	return free_fat;
}

 

This topic has a solution.
Last Edited: Wed. Sep 18, 2019 - 04:34 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Solution was found, by setting the two values of "128" to 127 fixed it.  Not sure if I know why but it works.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:
  Not sure if I know why 

A problem with signed variables ... ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...