Exemple #1
0
static int test_read_performance(struct harness_t *harness_p)
{
    int i, block, res;
    struct time_t start, stop, diff;
    float seconds;
    unsigned long bytes_per_seconds;
    int number_of_blocks = 32;

    /* Write reference data to given block and verify that it was
       written. */
    for (i = 0; i < membersof(buf); i++) {
        buf[i] = (i & 0xff);
    }

    time_get(&start);

    /* Write to and read from the first five blocks. */
    for (block = 0; block < number_of_blocks; block++) {
        BTASSERT((res = sd_read_block(&sd, buf, block)) == SD_BLOCK_SIZE,
                 ", res = %d\r\n", res);
    }

    time_get(&stop);
    time_diff(&diff, &stop, &start);
    seconds = (diff.seconds + diff.nanoseconds / 1000000000.0f);
    bytes_per_seconds = ((SD_BLOCK_SIZE * number_of_blocks) / seconds);

    std_printf(FSTR("Read 32 blocks of %d bytes in %f s (%lu bytes/s).\r\n"),
               SD_BLOCK_SIZE,
               seconds,
               bytes_per_seconds);

    return (0);
}
Exemple #2
0
/**
 * def read_into(self, block, buffer)
 */
static mp_obj_t class_sd_read_block_into(mp_obj_t self_in,
                                         mp_obj_t block_in,
                                         mp_obj_t buffer_in)
{
    struct class_sd_t *self_p;
    uint32_t block;
    mp_buffer_info_t buffer_info;

    self_p = MP_OBJ_TO_PTR(self_in);
    block = mp_obj_get_int(block_in);
    mp_get_buffer_raise(MP_OBJ_TO_PTR(buffer_in),
                        &buffer_info,
                        MP_BUFFER_WRITE);

    if (buffer_info.len != SD_BLOCK_SIZE) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
                                           "bad buffer length"));
    }

    if (sd_read_block(&self_p->drv, buffer_info.buf, block) != SD_BLOCK_SIZE) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
                                                "sd_read_block(%d) failed",
                                                block));
    }

    return (mp_const_none);
}
uint8_t read_message(uint32_t file_number,uint8_t message_number)//reads particular message in queue
{
	uint32_t address;
	uint8_t ret_val;// of read_low_level

	if(!CHECK_MSG_DELETE(file_number,message_number))//checking sms to read is not marked as delete
	{
		address=mat[file_number]->address[message_number];//getting address of the sms to read
		add_temp = address;

		ret_val=sd_read_block(&sd,address,temp.data);
		temp.flag=0x11;//message exisits

		if(delete_message(file_number,message_number))//call to delete sms
				;
		else
			return 0xf0; // can not delete sms I dont no what is problem
	
		ptr=&temp;

	return ret_val;// Read sms correctly					
	}
	else
		return 0x0f;//Unable to Read Message
		
}
Exemple #4
0
int fs_close() //{{{
{
	fs_file_startblock* file = NULL; 

	if (file_startblock_idx < 0)
		return FS_ERROR_FILE_NOT_OPEN;

	// finish updating the file
	while(fs_busy())
		fs_update();

	// there is one more partial block that remains to be written
	uint32_t last_block_idx = file_startblock_idx + BYTES_TO_BLOCKS(file_byte_idx);
	if (last_block_idx != block_idx)
	{
		if (last_block_idx >= fs_capacity)
			return FS_ERROR_FILE_TOO_LONG;
		sd_write_block_start(block, last_block_idx);
		while (sd_write_block_update() < 0);
	}

	file = (fs_file_startblock*)block;
	memset(block, 0, sizeof(block));
	file->seq = file_seq;
	file->byte_len = file_byte_idx;
	file->fmt_iter = fs_fmt_iter;

	// write file block
	if (file_startblock_idx >= fs_capacity)
			return FS_ERROR_FILE_TOO_LONG;
	if (!sd_write_block_start(block, file_startblock_idx))
		return FS_ERROR_SD_WRITE;
	while (sd_write_block_update() < 0);

	// this is a skip file, point to it from the previous skip file
	if ((file_seq % FS_FILE_SKIP_LEN) == 0 &&
	    file_prev_skip_startblock_idx > 0)
	{
		// read the prev skip file startblock
		fs_file_startblock* skip_file = (fs_file_startblock*)block;
		memset(block, 0, sizeof(block));
		if (!sd_read_block(block, file_prev_skip_startblock_idx))
			return FS_ERROR_SD_READ;

		// write prev skip file startblock with the new skip pointer
		skip_file->next_skip_file_startblock_idx = file_startblock_idx;
		if (!sd_write_block_start(block, file_prev_skip_startblock_idx))
			return FS_ERROR_SD_WRITE;
		while (sd_write_block_update() < 0);
	}

	// reset filesystem state
	fs_init();

	return FS_ERROR_NONE;
} //}}}
Exemple #5
0
static int test_read_write(struct harness_t *harness_p)
{
    int i, block, res;

    /* Write to and read from the first five blocks. */
    for (block = 0; block < 5; block++) {
        /* Write reference data to given block and verify that it was
           written. */
        for (i = 0; i < membersof(buf); i++) {
            buf[i] = ((block + i) & 0xff);
        }

        BTASSERT((res = sd_write_block(&sd, block, buf)) == SD_BLOCK_SIZE,
                 ", res = %d\r\n", res);
        memset(buf, 0, sizeof(buf));
        BTASSERT((res = sd_read_block(&sd, buf, block)) == SD_BLOCK_SIZE,
                 ", res = %d\r\n", res);

        for (i = 0; i < membersof(buf); i++) {
            BTASSERT(buf[i] == ((block + i) & 0xff));
        }

        /* Write zeros to given block and verify that it was
           written. */
        for (i = 0; i < membersof(buf); i++) {
            buf[i] = 0;
        }

        memset(buf, 0, sizeof(buf));
        BTASSERT((res = sd_write_block(&sd, block, buf)) == SD_BLOCK_SIZE,
                 ", res = %d\r\n", res);
        memset(buf, -1, sizeof(buf));
        BTASSERT((res = sd_read_block(&sd, buf, block)) == SD_BLOCK_SIZE,
                 ", res = %d\r\n", res);

        for (i = 0; i < membersof(buf); i++) {
            BTASSERT(buf[i] == 0);
        }
    }

    return (0);
}
Exemple #6
0
bool disk_init(sd_disk *sd)
{
    uint32_t pstart = 0;
    uint16_t psize = 0;

    //
    // Find MBR and First partition and get fat and root directory from them
    //
    if (sd_read_block(0, sd)
            && BLOCK_EQ(510,0x55) && BLOCK_EQ(511,0xAA) && BLOCK_EQ(P1_START+4,0x04)) {
        pstart = uint32_value(sd->buf+P1_START+8);
        if (pstart != 0 && sd_read_block(pstart, sd)) {
            // fat start = pstart + number reserved sectors
            sd->fat_start = pstart + uint16_value(sd->buf+14);

            sd->p_size = uint16_value(sd->buf+19);

            // root_dir start = <fat start> + <fat size> * <number of fats>;
            sd->root_dir = sd->fat_start + uint8_value(sd->buf+16) * uint16_value(sd->buf+22);

            // data_start = <root_dir start> + <num root entries> * 32 / 512
            sd->data_start = sd->root_dir + (uint16_value(sd->buf+17) >> 4);
            return true;
        }
Exemple #7
0
static int format() //{{{
{
	int i;
	fs_superblock* sb = (fs_superblock*)block;

	// reset filesystem state
	fs_init();

	// read superblock
	block_idx = FS_SUPERBLOCK_IDX;
	if (!sd_read_block(block, block_idx))
		return FS_ERROR_SD_READ;

	// this sd card has been formatted before
	if (memcmp(sb->magic, magic, sizeof(magic)) == 0 &&
		sb->ver == FS_VERSION)
	{
		// format to the next format iteration
		sb->fmt_iter++;
		fs_fmt_iter = sb->fmt_iter;
	}
	// this sd card has not been formatted before, format it
	else
	{
		// create a superblock for the first iteration
		memcpy(sb->magic, magic, sizeof(magic));
		sb->ver = FS_VERSION;
		sb->fmt_iter = 0;
		fs_fmt_iter = sb->fmt_iter;
	}

	// write the new superblock
	block_idx = FS_SUPERBLOCK_IDX;
	if (!sd_write_block_start(block, block_idx))
		return FS_ERROR_SD_WRITE;
	while (sd_write_block_update() < 0);

	// fill the first file block with ones so it is invalid
	for (i = 0; i < SD_BLOCK_LEN; i++)
		block[i] = 0xFF;
	if (!sd_write_block_start(block, block_idx + 1))
		return FS_ERROR_SD_WRITE;
	while (sd_write_block_update() < 0);

	return FS_ERROR_NONE;
} //}}}
Exemple #8
0
DRESULT disk_readp (
	BYTE* buff,		/* Pointer to the destination object */
	DWORD sector,	/* Sector number (LBA) */
	UINT offset,	/* Offset in the sector */
	UINT count		/* Byte count (bit15:destination) */
)
{
	
	DRESULT res;
	
	send_data_uart0_str("Reading sector : ");
	send_data_uart0_str(itoa(sector));
	send_data_uart0_str(" ; offset : ");
	send_data_uart0_str(itoa(offset));
	send_data_uart0_str(" ; count : ");
	send_data_uart0_str(itoa(count));
	send_data_uart0_str("\r\n");
	
	/*if(sd_init == FALSE)
		return (DRESULT) RES_NOTRDY;*/
	int i, cnt = 0;
	
	if(sd_read_block(sector) == SD_NO_ERROR)
	{
	
		for(i = offset; i < offset+count; i++)
		{
			
				buff[i - offset] = sd_block_buffer[i];
				send_data_uart0_str(itoa(sd_block_buffer[i]));
				send_data_uart0_str(";");
				cnt++;
			
		}
		
		return (DRESULT)RES_OK;
	}
	else
	{ 
		return (DRESULT)RES_ERROR;
		
	}


	return res;
}
Exemple #9
0
/**
 * def read_block(self, block)
 */
static mp_obj_t class_sd_read_block(mp_obj_t self_in,
                                    mp_obj_t block_in)
{
    struct class_sd_t *self_p;
    vstr_t vstr;
    uint32_t block;

    self_p = MP_OBJ_TO_PTR(self_in);
    block = mp_obj_get_int(block_in);

    vstr_init_len(&vstr, SD_BLOCK_SIZE);

    if (sd_read_block(&self_p->drv, vstr.buf, block) != SD_BLOCK_SIZE) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
                                                "sd_read_block(%d) failed",
                                                block));
    }

    return (mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr));
}
Exemple #10
0
int test_sd()
{
  int i = sd_init();
  if(i==0) {
    puts("FAILED INIT of Card\n");
    return 0;
  }
  
  unsigned char buf[512];
  i = sd_read_block(2048,buf);
  if(i == 0) {
    puts("READ Command Rejected\n");
    return 0;
  }
  if((buf[0]==0xb8)&&(buf[1]==0x08)&&(buf[2]==0x00)&&(buf[3]==0x50))
    ;
  else {
    puts("Read bad data from SD Card\n");
    return 0;
  }
  return 1;
}
Exemple #11
0
//------使用上注意!! 讀取一次最好不要超過1個buff---- 不然會出現不可預期錯誤
DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address in LBA */
	UINT count		/* Number of sectors to read */
)
{
	DRESULT res;
	//int result;


                    sd_read_block(&sdc, sector,buff);
                    sd_wait_notbusy (&sdc);
                res = RES_OK;
                return res;

        
       /* 
	switch (pdrv) {
	case ATA :
		// translate the arguments here
		result = ATA_disk_read(buff, sector, count);
		// translate the reslut code here
		return res;
	case MMC :
		// translate the arguments here
		result = MMC_disk_read(buff, sector, count);
		// translate the reslut code here
		return res;
	case USB :
		// translate the arguments here
		result = USB_disk_read(buff, sector, count);
		// translate the reslut code here
		return res;
	}
                
	return RES_PARERR;
                */
}
Exemple #12
0
int fs_read(void* buf, uint16_t len) //{{{
{
	uint16_t read = 0;

	if (file_startblock_idx < 0)
		return FS_ERROR_FILE_NOT_OPEN;

	if (file_byte_idx >= file_byte_len)
		return FS_ERROR_EOF;

	// TODO add a check to make sure not trying to read from a new file

	// read until end of requested len or end of file
	while ((read < len) && (file_byte_idx < file_byte_len))
	{
		uint16_t block_byte_idx = BYTES_IN_LAST_BLOCK(file_byte_idx);
		uint16_t read_remaining = min(len - read, file_byte_len - file_byte_idx);

		// can only read at most a block at a time
		uint16_t to_read = min(read_remaining, SD_BLOCK_LEN - block_byte_idx);

		// next block needs to be read from sd
		if (file_startblock_idx +
			BYTES_TO_BLOCKS(file_byte_idx + to_read) != block_idx)
		{
			block_idx = file_startblock_idx + 
				BYTES_TO_BLOCKS(file_byte_idx + to_read);
			sd_read_block(block, block_idx);
		}

		memcpy(buf + read, block + block_byte_idx, to_read);
		read += to_read;
		file_byte_idx += to_read;
	}

	return read;
} //}}}
Exemple #13
0
int fs_self_test() //{{{
{
	uint8_t buf[SD_BLOCK_LEN], buf2[SD_BLOCK_LEN];
	uint32_t fmt_iter;
	int ret, i;

	printf("fs self test\n");

	// start with clean fs and
	// remember fs_fmt_iter so it can be read after fs_close()
	format();
	fmt_iter = fs_fmt_iter;

	printf("fs_open new file with no existing file...");
	if ((ret = fs_open(1)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	if (file_startblock_idx != 1)
	{
		printf("FAILED first new file startblock is not block 1, it's %ld\n", file_startblock_idx);
		return 1;
	}
	printf("PASSED\n");

	printf("fs_open existing file with no existing file...");
	if ((ret = fs_open(0)) >= 0)
	{
		printf("FAILED fs_open did not fail even though it should\n");
		return 1;
	}
	printf("PASSED\n");

	printf("fs_open new file and fs_close...");
	if ((ret = fs_open(1)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	if ((ret = fs_close()) < 0)
	{
		printf("FAILED fs_close failed, returned %d\n", ret);
		return 1;
	}
	// check file block
	fs_file_startblock file1;
	sd_read_block(block, 1);
	file1.seq = 0;
	file1.byte_len = 0;
	file1.fmt_iter = fmt_iter;
	file1.next_skip_file_startblock_idx = 0;
	if (memcmp(&file1, block, sizeof(file1)) != 0)
	{
		printf("FAILED file block is incorrect\n");
		return 1;
	}
	printf("PASSED\n");

	printf("fs_open another new file and fs_close...");
	if ((ret = fs_open(1)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	if ((ret = fs_close()) < 0)
	{
		printf("FAILED fs_close failed, returned %d\n", ret);
		return 1;
	}
	// check file block
	fs_file_startblock file2;
	sd_read_block(block, 2);
	file2.fmt_iter = fmt_iter;
	file2.seq = 1;
	file2.byte_len = 0;
	file2.next_skip_file_startblock_idx = 0;

	if (memcmp(&file2, block, sizeof(file2)) != 0)
	{
		printf("FAILED file block is incorrect\n");
		return 1;
	}
	printf("PASSED\n");

	printf("fs_open a new file, write less than a block, and fs_close...");
	if ((ret = fs_open(1)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	// write 10 bytes
	for (i = 0; i < 10; i++)
		buf[i] = i;
	fs_write(buf, 10);
	if ((ret = fs_close()) < 0)
	{
		printf("FAILED fs_close failed, returned %d\n", ret);
		return 1;
	}
	// check file block
	fs_file_startblock file3;
	sd_read_block(block, 3);
	file3.fmt_iter = fmt_iter;
	file3.seq = 2;
	file3.byte_len = 10;
	file3.next_skip_file_startblock_idx = 0;

	if (memcmp(&file3, block, sizeof(file3)) != 0)
	{
		printf("FAILED file block is incorrect\n");
		return 1;
	}
	// check data
	sd_read_block(block, 4);
	if (memcmp(buf, block, 10) != 0)
	{
		printf("FAILED file data is incorrect\n");
		return 1;
	}
	printf("PASSED\n");

	printf("fs_open a new file, write more than a block, and fs_close...");
	if ((ret = fs_open(1)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	memset(buf, (uint8_t)fmt_iter, sizeof(buf));
	// write 500 bytes
	fs_write(buf, 500);
	while (fs_busy())
		fs_update();
	memset(buf, (uint8_t)fmt_iter + 1, sizeof(buf));
	// write 14 bytes
	fs_write(buf, 14);
	if ((ret = fs_close()) < 0)
	{
		printf("FAILED fs_close failed, returned %d\n", ret);
		return 1;
	}
	// check file block
	fs_file_startblock file4;
	sd_read_block(block, 5);
	file4.fmt_iter = fmt_iter;
	file4.seq = 3;
	file4.byte_len = 514;
	file4.next_skip_file_startblock_idx = 0;

	if (memcmp(&file4, block, sizeof(file3)) != 0)
	{
		printf("FAILED file block is incorrect\n");
		return 1;
	}
	// check first block
	memset(buf, (uint8_t)fmt_iter, sizeof(buf));
	sd_read_block(block, 6);
	if (memcmp(buf, block, 500) != 0)
	{
		printf("FAILED file data is incorrect #1\n");
		return 1;
	}
	memset(buf, (uint8_t)fmt_iter + 1, sizeof(buf));
	if (memcmp(buf, block + 500, 12) != 0)
	{
		printf("FAILED file data is incorrect #2\n");
		return 1;
	}
	// check second block 
	sd_read_block(block, 7);
	if (memcmp(buf, block, 2) != 0)
	{
		printf("FAILED file data is incorrect #3\n");
		return 1;
	}
	printf("PASSED\n");

	printf("fs_open an existing file, fs_read more than a block...");
	if ((ret = fs_open(0)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	fs_read(buf2, 500);
	memset(buf, (uint8_t)fmt_iter, sizeof(buf));
	if (memcmp(buf, buf2, 500) != 0)
	{
		printf("FAILED file data is incorrect #1\n");
		return 1;
	}
	fs_read(buf2, 14);
	memset(buf, (uint8_t)fmt_iter + 1, sizeof(buf));
	if (memcmp(buf, buf2, 14) != 0)
	{
		printf("FAILED file data is incorrect #2\n");
		return 1;
	}
	if (fs_read(buf2, 1) != FS_ERROR_EOF)
	{
		printf("FAILED fs_read should have failed with EOF\n");
		return 1;
	}
	printf("PASSED\n");

	printf("fs_open a new file, big write, big read...");
	if ((ret = fs_open(1)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	// init write/read buffer
	for (i = 0; i < 200; i++)
		buf[i] = i;
	// write many times
	for (i = 0; i < 10; i++)
	{
		fs_write(buf, 200);
		while (fs_busy())
			fs_update();
	}
	if ((ret = fs_close()) < 0)
	{
		printf("FAILED fs_close failed, returned %d\n", ret);
		return 1;
	}
	if ((ret = fs_open(0)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	// read many times
	for (i = 0; i < 10; i++)
	{
		fs_read(buf2, 200);
		if (memcmp(buf, buf2, 200) != 0)
		{
			printf("FAILED file data is incorrect in iteration %d\n", i);
			return 1;
		}
	}
	printf("PASSED\n");

	// start with clean fs to see if reformat happens correctly
	// remember fs_fmt_iter so it can be read after fs_close()
	format();
	fmt_iter = fs_fmt_iter;
	fs_capacity = FS_LAST_FILE_BLOCKS + 2;
	fs_force_capacity = 1;

	printf("fs_open file near the end of the disk...");
	if ((ret = fs_open(1)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	fs_write(buf, 10); // write one more block
	if ((ret = fs_close()) < 0)
	{
		printf("FAILED fs_close failed, returned %d\n", ret);
		return 1;
	}
	// check file block
	fs_file_startblock file5;
	sd_read_block(block, 1);
	file5.fmt_iter = fmt_iter;
	file5.seq = 0;
	file5.byte_len = 10;
	file5.next_skip_file_startblock_idx = 0;
	if (memcmp(&file5, block, sizeof(file5)) != 0)
	{
		printf("FAILED file block is incorrect\n");
		return 1;
	}
	printf("PASSED\n");

	printf("fs_open file past the end of the last blocks on the disk...");
	if ((ret = fs_open(1)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	fs_write(buf, 10); // write one more block
	if ((ret = fs_close()) < 0)
	{
		printf("FAILED fs_close failed, returned %d\n", ret);
		return 1;
	}
	// check file block
	fs_file_startblock file6;
	sd_read_block(block, 1);
	file6.fmt_iter = fmt_iter+1;
	file6.seq = 0;
	file6.byte_len = 10;
	file6.next_skip_file_startblock_idx = 0;
	if (memcmp(&file6, block, sizeof(file6)) != 0)
	{
		printf("FAILED file block is incorrect\n");
		return 1;
	}
	printf("PASSED\n");

	fs_force_capacity = 0;

#ifdef FS_SKIP_TEST
	fs_file_startblock *file = (fs_file_startblock*)block;

	// start with clean fs so counting skip blocks is possible and
	// remember fs_fmt_iter so it can be read after fs_close()
	format();
	fmt_iter = fs_fmt_iter;

	printf("fs_open and fs_close %d files to check skip...", FS_FILE_SKIP_LEN+1);

	for (i = 0; i < 251; i++)
	{
		if ((ret = fs_open(1)) < 0)
		{
			printf("FAILED fs_open failed, returned %d\n", ret);
			return 1;
		}
		if ((ret = fs_close()) < 0)
		{
			printf("FAILED fs_close failed, returned %d\n", ret);
			return 1;
		}
	}
	// check file block
	fs_file_startblock file_skip1;
	file_skip1.fmt_iter = fmt_iter;
	file_skip1.seq = 0;
	file_skip1.byte_len = 0;
	file_skip1.next_skip_file_startblock_idx = 251;
	sd_read_block(block, 1);
	if (memcmp(&file_skip1, block, sizeof(file_skip1)) != 0)
	{
		printf("FAILED file skip block is not idx 251, it's %lu\n",
			file->next_skip_file_startblock_idx);
		return 1;
	}
	printf("PASSED\n");

	printf("check if fs_open new uses skip...");
	if ((ret = fs_open(1)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	if (fs_open_reads != 3)
	{
		printf("FAILED should be 3 iterations to open a file with a skip block, was %ld\n", fs_open_reads);
		return 1;
	}
	if (file_startblock_idx != 252)
	{
		printf("FAILED new file is not one after skip block\n");
		return 1;
	}
	if ((ret = fs_close()) < 0)
	{
		printf("FAILED fs_close failed, returned %d\n", ret);
		return 1;
	}
	printf("PASSED\n");

	printf("check if fs_open existing uses skip...");
	if ((ret = fs_open(0)) < 0)
	{
		printf("FAILED fs_open failed, returned %d\n", ret);
		return 1;
	}
	if (fs_open_reads != 4)
	{
		printf("FAILED should be 4 iterations to open a file with a skip block, was %ld\n", fs_open_reads);
		return 1;
	}
	if (file_startblock_idx != 252)
	{
		printf("FAILED new file is not one after skip block\n");
		return 1;
	}
	if ((ret = fs_close()) < 0)
	{
		printf("FAILED fs_close failed, returned %d\n", ret);
		return 1;
	}
	printf("PASSED\n");

	printf("fs_open, fs_write, fs_close %d files to check 2nd skip...", FS_FILE_SKIP_LEN+1);
	for (i = 0; i < 251; i++)
	{
		if ((ret = fs_open(1)) < 0)
		{
			printf("FAILED fs_open failed, returned %d\n", ret);
			return 1;
		}
		fs_write(buf, 200);
		if ((ret = fs_close()) < 0)
		{
			printf("FAILED fs_close failed, returned %d\n", ret);
			return 1;
		}
	}
	// check file block
	fs_file_startblock file_skip2;
	file_skip2.fmt_iter = fmt_iter;
	file_skip2.seq = 250;
	file_skip2.byte_len = 0;
	file_skip2.next_skip_file_startblock_idx = 750;
	sd_read_block(block, 251);
	if (memcmp(&file_skip2, block, sizeof(file_skip2)) != 0)
	{
		printf("FAILED file skip block is not idx 750, it's %lu\n",
			file->next_skip_file_startblock_idx);
		return 1;
	}
	printf("PASSED\n");
#endif

	return 0;
} //}}}
Exemple #14
0
int fs_open(uint8_t new_file) //{{{
{
	uint32_t file_byte_len_prev = -1;
	int32_t block_idx_prev = -1;

	// reset filesystem state
	fs_init();

	// read superblock
	fs_superblock* sb = (fs_superblock*)block;
	block_idx = FS_SUPERBLOCK_IDX;
	if (!sd_read_block(block, block_idx))
		return FS_ERROR_SD_READ;
	// no superblock or corrupted, format
	if (!(memcmp(sb->magic, magic, sizeof(magic)) == 0 &&
		sb->ver == FS_VERSION))
		format();
	// superblock is good, read iteration
	else
		fs_fmt_iter = sb->fmt_iter;

	// find the first free file
	while (block_idx < (fs_capacity - FS_LAST_FILE_BLOCKS))
	{
		// read in a potential file block
		fs_file_startblock* file = (fs_file_startblock*)block;
		block_idx++;
		if (!sd_read_block(block, block_idx))
			return FS_ERROR_SD_READ;
#ifdef FS_SKIP_TEST
			fs_open_reads++;
#endif

		// file startblock found
		if (file->fmt_iter == fs_fmt_iter && 
			file->seq == file_seq)
		{
			// store previous skip block idx so it can be written with next skip block
			if ((file_seq % FS_FILE_SKIP_LEN) == 0)
				file_prev_skip_startblock_idx = block_idx;

			// at skip file - skip
			if (file->next_skip_file_startblock_idx > 0)
			{
				// no prev file due to skip
				block_idx_prev = -1;
				file_byte_len_prev = -1;

				block_idx = file->next_skip_file_startblock_idx-1;
				file_seq += FS_FILE_SKIP_LEN;
			}
			// at normal file - seek to the end
			else
			{
				block_idx_prev = block_idx;
				file_byte_len_prev = file->byte_len;

				block_idx += BYTES_TO_BLOCKS(file->byte_len);
				file_seq++;
			}
		}
		else
		{
			// opening new file
			if (new_file)
			{
				file_startblock_idx = block_idx;
				file_byte_idx = 0;
			}
			// opening existing file
			else
			{
				if (block_idx_prev >= 0)
				{
					file_startblock_idx = block_idx_prev;
					file_byte_len = file_byte_len_prev;
					file_byte_idx = 0;
				}
				else
					return FS_ERROR_NO_EXISTING_FILE;
			}
			return FS_ERROR_NONE;
		}
	}

	// the only free blocks are past the end of the last file
	if (block_idx >= (fs_capacity - FS_LAST_FILE_BLOCKS))
	{
		// format and retry open
		format();
		return fs_open(new_file);
	}

	return FS_ERROR_FILE_TOO_LONG;
} //}}}
uint8_t load_fat_mat_from_SD()// Loads the Fat and mat from SD in case of reset
{

	uint32_t address,temp;
	uint8_t data[message_size];
	uint16_t i,j,shift=0,x;
	uint16_t next=0;
	uint32_t MAT_START=SP_ADD_MAT_START;

	/* Read FAT From any special address */

	address=(SP_ADD_FAT1 << 9);
	sd_read_block(&sd,address,data); // read FAT from SD
	sd_delay(15); // needs to be withdrawn
	/* Take FAT into <main> memory */
	
	address=0;
	for(i=0;i<files;i++) /* loads Addresses of file into  FAT */
	{
		for(j=0;j<4;j++)
		{
			temp= data[next++] << shift;
			address= address | temp;
			shift += 8;
		}
		root->address[i]= address;
		shift= 0;
	}

	address=0;
	shift=0;
	for(i=0;i<files;i++) /* loads no of files into  FAT */
	{
		for(j=0;j<4;j++)
		{
			temp= data[next++] << shift;
			address= address | temp;
			shift += 8;
		}
		root->file_number[i]= address;
		shift= 0;
	}

	address=0;
	shift=0;
	for(i=0;i<4;i++) /* loads flag_tables of file into  FAT */
	{
		for(j=0;j<4;j++)
		{
			temp= data[next++] << shift;
			address= address | temp;
			shift += 8;
		}
		root->flag_table[i]= address;
		shift= 0;
	}

	/* Fat is loaded into memory but not MAT let us do it */

	
	shift=next=0; // (optimized);

	for(x=0;x<files;x++)
	{
		address=MAT_START << 9;
		sd_read_block(&sd,address,data); // For each file read mat

		address=0;
		shift=0;
		for(i=0;i<4;i++) // loads file number
		{
			temp= data[next++] << shift;
			address= address | temp;
			shift += 8;
		}
		mat[x]->file_number=address; // loads file number

		mat[x]->next= data[next++];	// loads next ptr;

		for(i=0;i<messages_per_file;i++) // loads message number
			mat[x]->message_number[i]= data[next++];
		
		shift=0;
		address=0;
		for(i=0;i<8;i++) /* loads exists flag  */
		{
			for(j=0;j<4;j++)
			{
				temp= data[next++] << shift;
				address= address | temp;
				shift += 8;
			}
			mat[x]->exists_flag_table[i]= address;
			shift= 0;
		}

		shift=0;
		address=0;
		for(i=0;i<8;i++) /* loads delete flag  */
		{
			for(j=0;j<4;j++)
			{
				temp= data[next++] << shift;
				address= address | temp;
				shift += 8;
			}
			mat[x]->delete_flag_table[i]= address;
			shift= 0;
		}

		shift=0;
		address=0;
		for(i=0;i<messages_per_file;i++) /* loads Addresses messages*/
		{
			for(j=0;j<4;j++)
			{
				temp= data[next++] << shift;
				address= address | temp;
				shift += 8;
			}
			mat[x]->address[i]= address;
			shift= 0;
		}
		next=0;
	}


	return 0;
}