Code: Select all
void fatfs_test_2(int disk_no) {
fatfs_clearing_partition_table(disk_no);
int logical_drive_1 = 0; // First logical drive
int partition_1 = 1; // First partition
int logical_drive_2 = 1; // Second logical drive
int partition_2 = 2; // Second partition
VolToPart[logical_drive_1].pd = disk_no; // Physical drive number
VolToPart[logical_drive_1].pt = partition_1; // Partition 1
VolToPart[logical_drive_2].pd = disk_no; // Physical drive number
VolToPart[logical_drive_2].pt = partition_2; // Partition 2
size_t buffer_size = 65536; // 64KB
BYTE* work = (BYTE*)kheap_alloc(buffer_size, ALLOCATE_DATA); // 64K buffer
if(!work) {
printf("Failed to allocate %zu bytes for formatting buffer\n", buffer_size);
return;
}
memset(work, 0, buffer_size);
// 256MB sectors for the 1st partition and the remaining for the 2nd partition
uint64_t part_1_sectors = (100 * 1024 * 1024) / 512;
LBA_t plist[] = {part_1_sectors, 100}; // 256MB sectors for the 1st partition and the remaining 60% for the 2nd partition and rest are unallocated
int res = f_fdisk(disk_no, plist, work); // Divide the physical drive 1
if(res != FR_OK){
printf("FATFS: FDISK failed on physical drive %d with error %s\n", disk_no, fatfs_error_string(res));
free(work);
return;
}
printf("FATFS: FDISK succeeded on physical drive %d\n", disk_no);
kheap_free(work, buffer_size);
work = NULL;
res = fatfs_mkfs(logical_drive_1, FM_FAT32); // Create FAT volume on the logical drive 0
if(res != FR_OK){
printf("FATFS: MKFS failed on logical drive 0 with error %s\n", fatfs_error_string(res));
return;
}
res = fatfs_mkfs(logical_drive_2, FM_FAT32); // Create FAT volume on the logical drive 1
if(res != FR_OK){
printf("FATFS: MKFS failed on logical drive 1 with error %s\n", fatfs_error_string(res));
return;
}
res = fatfs_mount(logical_drive_1); // mount logical drive 0
if(res != FR_OK){
printf("FATFS: mounting failed on logical drive 0 with error %s\n", fatfs_error_string(res));
return;
}
res = fatfs_mount(logical_drive_2); // mount logical drive 1
if(res != FR_OK){
printf("FATFS: mounting failed on logical drive 1 with error %s\n", fatfs_error_string(res));
return;
}
//-----------------------------------------------------------------------------------------------
const char *msg0 = "This is FAT32 Partition 0";
const char *msg1 = "This is FAT32 Partition 1";
void *file0 = fatfs_open("0:/part0.txt", FA_CREATE_ALWAYS | FA_WRITE);
void *file1 = fatfs_open("1:/part1.txt", FA_CREATE_ALWAYS | FA_WRITE);
if (file0 && file1) {
fatfs_write(file0, (char*)msg0, strlen(msg0));
fatfs_write(file1, (char*)msg1, strlen(msg1));
printf("Successfully wrote unique files to both partitions.\n");
}
fatfs_close(file0);
fatfs_close(file1);
printf("\n--- Listing 0: ---\n");
fatfs_listdir("0:/");
printf("\n--- Listing 1: ---\n");
fatfs_listdir("1:/");
fatfs_unmount(0);
fatfs_unmount(1);
}
Code: Select all
FATFS: FDISK succeeded on physical drive 1
FATFS: MKFS succeeded on logical drive 0:
FATFS: MKFS succeeded on logical drive 1:
FATFS: Successfully mounted on drive 0
FATFS: Successfully mounted on drive 1
Successfully wrote unique files to both partitions.
--- Listing 0: ---
Listing directory: 0:/
part0.txt 25 bytes
part1.txt 25 bytes
--- Listing 1: ---
Listing directory: 1:/
part0.txt 25 bytes
part1.txt 25 bytes
