I have an assignment that do FAT12 system. Follow the requirement, I need 2 functions to read sector and multi sector. Those functions in HAL layer. I just tested in app (main) layer below: In HAL.c:
#define SIZE_OF_SECTOR 512
static FILE* s_pimage = NULL;
void kmc_open_img_file()
{
s_pimage = fopen("floppy.img", "rb");
if(NULL == s_pimage)
{
printf("Can not open file!");
}
}
int32_t kmc_read_sector(uint32_t index, uint8_t *buff)
{
uint32_t offset = 0;
int32_t num_of_bytes = 0;
offset = SIZE_OF_SECTOR * index;
fseek(s_pimage, SIZE_OF_SECTOR * index, SEEK_SET);
num_of_bytes = (int32_t)fread(buff, 1, SIZE_OF_SECTOR, s_pimage);
if (num_of_bytes == 0) printf("Fail");
return num_of_bytes;
}
and in main: (added header file)
int main()
{
kmc_open_img_file();
uint8_t *buff = NULL ;
int32_t a = kmc_read_sector(0, buff);
printf("%d", a);
kmc_close_img_file();
return 0;
}
Result :Fail and 0 byte.
Can anyone help me solve it without extern variable? (this is requirement too)