⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesystem.c

📁 ENC28J60 System HTTP
💻 C
📖 第 1 页 / 共 3 页
字号:
//close file & story lookup infovoid filesystem_close_file(){	unsigned int pos;	unsigned char i;	#if FILESYSTEM_DEBUG	softuart_puts_progmem("FS  : closing file ");	softuart_put_uint16(filesystem_fileid);	softuart_putnewline();	#endif	//check for invalid fileid:	if (filesystem_fileid == 0)		return;	//store active buffer:	dataflash_write_n_to_page_buffer(0,0,filesystem_buffer, filesystem_file_pos);	filesystem_store_current_block(0);	//store file info in lookup table!		//copy lookuptable page to dataflash buffer 0	dataflash_copy_page_to_buffer(((filesystem_fileid-1)/11), 0);		//pos inside page is ?	pos = 24*((filesystem_fileid-1) % 11);	//step1: write file id:	dataflash_write_to_page_buffer(pos + FILESYSTEM_FTABLE_ID_HI, 0, (filesystem_fileid>>8)&0xFF);	dataflash_write_to_page_buffer(pos + FILESYSTEM_FTABLE_ID_LO, 0, (filesystem_fileid   )&0xFF);		//step2: write timestamp	///NOT SUPPORTED RIGHT NOW///		//step3: write filename + ext:	for(i=0; i<8; i++)		dataflash_write_to_page_buffer(pos + FILESYSTEM_FTABLE_FILENAME_START + i, 0, filesystem_filename[i]);	for(i=0; i<3; i++)		dataflash_write_to_page_buffer(pos + FILESYSTEM_FTABLE_FILENAME_START + i + 8, 0, filesystem_filename_ext[i]);	//step4: write start block address:	dataflash_write_to_page_buffer(pos + FILESYSTEM_FTABLE_BLOCK_START_HI, 0, (filesystem_first_block>>8)&0xFF);	dataflash_write_to_page_buffer(pos + FILESYSTEM_FTABLE_BLOCK_START_LO, 0, (filesystem_first_block   )&0xFF);	//step5: write block count:	dataflash_write_to_page_buffer(pos + FILESYSTEM_FTABLE_BLOCK_COUNT_HI, 0, (filesystem_block_count>>8)&0xFF);	dataflash_write_to_page_buffer(pos + FILESYSTEM_FTABLE_BLOCK_COUNT_LO, 0, (filesystem_block_count   )&0xFF);	//store data on flash:	dataflash_copy_buffer_to_page((filesystem_fileid-1)/11,0);	//reset vars:	filesystem_status = FILESYSTEM_STATUS_IDLE;	filesystem_fileid = 0;	filesystem_first_block = 0;	filesystem_current_block = 0;	filesystem_file_pos = 0;}//alocate 264 byte block & store file info header inside:unsigned int filesystem_allocate_block(){	//search block	#if FILESYSTEM_DEBUG	softuart_puts_progmem("FS  : searching free block...");	#endif	for(unsigned int b=FILESYSTEM_TABLE_SIZE; b<FILESYSTEM_BLOCK_COUNT; b++){		//copy selected page to buffer		dataflash_copy_page_to_buffer(b, 1);			//rprintf("FS: checking block: [%d].\r\n",b);		//is it free ? 		//-> ignore b = current block !		if ((dataflash_read_buffer(FILESYSTEM_FILE_FREETAG,1) != 0xE0) && (b != filesystem_current_block)){			//-> free !			#if FILESYSTEM_DEBUG			softuart_puts_progmem(" found block ");			softuart_put_uint16(b);			softuart_putnewline();			#endif			return b;		}	}	//no free block found :(	#if FILESYSTEM_DEBUG	softuart_puts_progmem(" FAILED -> FS FULL ?!");	softuart_putnewline();	#endif	return 0;}//open a new file:unsigned int filesystem_open_file_wr(unsigned char *filename, unsigned char *filename_ext){	//make sure there is no open file !	if (filesystem_status != FILESYSTEM_STATUS_IDLE)		filesystem_close_file();		//make file extension lowercase !!	for(unsigned char i=0; i<3 && filename_ext[i] != 0; i++){		if ((filename_ext[i] >= 'A') && (filename_ext[i] <= 'Z'))			filename_ext[i] = filename_ext[i] - 'A' + 'a';	}	//protect filename, only allow a-zA-Z0-9_-	for(unsigned char i=0; i<8; i++){		if (filename[i] == 0)			break;		if (					!((filename[i] >= 'a') && (filename[i] <= 'z')) &&					!((filename[i] >= 'A') && (filename[i] <= 'Z')) &&					!((filename[i] >= '0') && (filename[i] <= '9')) 				){			filename[i] = '_';		}	}	#if FILESYSTEM_DEBUG	softuart_puts_progmem("FS  : new file [");	for(unsigned char i=0; i<8 && filename[i] != 0; i++)		softuart_putc(filename[i]);	softuart_putc('.');	for(unsigned char i=0; i<3 && filename_ext[i] != 0; i++)		softuart_putc(filename_ext[i]);	softuart_putc(']');	softuart_putnewline();	#endif	//check if file exists, returns 0 if file is unknown!	filesystem_fileid = filesystem_search_file(filename, filename_ext);	if (filesystem_fileid != 0){		//file exists !!!		//->delete file & replace it with the new data:		#if FILESYSTEM_DEBUG		softuart_puts_progmem("FS  : WARNING: overwriting existing file !");		softuart_putnewline();		#endif		filesystem_delete_file(filesystem_fileid);	}else{		//new file -> get id:		filesystem_fileid = filesystem_allocate_fileid();	}	filesystem_block_count = 1;	//check for error:	if (filesystem_fileid  == 0)		return 0;	//now search a free block adddress for the first block:	unsigned int block = filesystem_allocate_block();	//check for error:	if (block == 0)		return 0;	//store info:	filesystem_first_block = block;	filesystem_current_block = block;	for (unsigned char i=0; i<8; i++)		filesystem_filename[i] = filename[i];	for (unsigned char i=0; i<3; i++)		filesystem_filename_ext[i] = filename_ext[i];	//return first block:	return block;}/*unsigned int filesystem_open_file_rd(unsigned char *filename, unsigned char *filename_ext){	unsigned int pos;	//make sure there is no open file !	if (filesystem_status != FILESYSTEM_STATUS_IDLE)		filesystem_close_file();		//check if file exists, returns 0 if file is unknown!	filesystem_fileid = filesystem_search_file(filename, filename_ext);	if (filesystem_fileid != 0){		//file exists !!!		//->delete file & replace it with the new data:		#if FILESYSTEM_DEBUG		softuart_puts_progmem("FS  : open file for reading!");		softuart_putnewline();		#endif	}else{		//new file -> get id:		#if FILESYSTEM_DEBUG		softuart_puts_progmem("FS  : file not found!");		softuart_putnewline();		#endif		return 0;	}	//pos inside page is ?	pos = 24*((filesystem_fileid-1) % 11);	#if FILESYSTEM_DEBUG	softuart_puts_progmem("FS  : requested fid ");	softuart_put_uint16(filesystem_fileid);	softuart_puts_progmem(", loc on block ");	softuart_put_uint16((filesystem_fileid-1)/11);	softuart_puts_progmem(", pos ");	softuart_put_uint8(pos);	softuart_putnewline();	#endif	//get first block:	filesystem_current_block  = dataflash_read_buffer(pos + FILESYSTEM_FTABLE_BLOCK_START_HI, 0)<<8;	filesystem_current_block |= dataflash_read_buffer(pos + FILESYSTEM_FTABLE_BLOCK_START_LO, 0);	filesystem_first_block = filesystem_current_block;		for (unsigned char i=0; i<8; i++)		filesystem_filename[i] = filename[i];	for (unsigned char i=0; i<3; i++)		filesystem_filename_ext[i] = filename_ext[i];	filesystem_file_pos = 0;	//return OK	return 1;}*///store len bytes of data in current open file. if last==1 close file after write:void filesystem_write_data(unsigned char* data, unsigned int len, unsigned char last){	//unsigned int next_block;	//rprintf("FS: writing %d chars.\r\n",len);	//check for invalid fileid:	if (filesystem_fileid == 0)		return;	for( ; len>0; len--){		//save data:		///dataflash_write_to_page_buffer((unsigned int)filesystem_file_pos, 0, (*data));		filesystem_buffer[filesystem_file_pos] = (*data);		//rprintf("FS: saving %c\r\n",(*data));		//block full or end of file ?!		if ((filesystem_file_pos == 255) || (len==1 && last==1)){			//step0: copy buffer to flash:			dataflash_write_n_to_page_buffer(0,0,filesystem_buffer, 256);			//store buffer:			filesystem_store_current_block((filesystem_file_pos == 255) && !(len==1 && last==1));			#if FILESYSTEM_DEBUG			softuart_puts_progmem("FS  : storing block ");			softuart_put_uint16(filesystem_current_block);			softuart_puts_progmem(" on flash");			softuart_putnewline();			#endif			//filesystem_file_pos = 0xFF; //next is filepos 0		}		*data++;		filesystem_file_pos++;			}}void filesystem_store_current_block(unsigned char not_last){	unsigned int next_block;	//step1: mark current block as used:	dataflash_write_to_page_buffer(FILESYSTEM_FILE_FREETAG, 0, 0xE0);		//step2: set filsystem rev	dataflash_write_to_page_buffer(FILESYSTEM_FILE_REV, 0, FILESYSTEM_REVISION);	//step3: save file id	dataflash_write_to_page_buffer(FILESYSTEM_FILE_ID_LO, 0, filesystem_fileid&0xFF);	dataflash_write_to_page_buffer(FILESYSTEM_FILE_ID_HI, 0, (filesystem_fileid>>8)&0xFF);	//step4: save block info (first/last block)	if (filesystem_first_block == filesystem_current_block)		dataflash_write_to_page_buffer(FILESYSTEM_FILE_BLOCKTAG, 0, 0x00);	else if (!not_last)		dataflash_write_to_page_buffer(FILESYSTEM_FILE_BLOCKTAG, 0, 0xEE);	else		dataflash_write_to_page_buffer(FILESYSTEM_FILE_BLOCKTAG, 0, 0xFF);	//step 5: save block usage	dataflash_write_to_page_buffer(FILESYSTEM_FILE_BLOCK_USAGE, 0, filesystem_file_pos);	//check if it is the last block	if (not_last){		//not the last -> get next free block 		//need to allocate next block!		next_block = filesystem_allocate_block();		filesystem_block_count++;		//step6: save next block pointer:		dataflash_write_to_page_buffer(FILESYSTEM_FILE_NEXT_LO, 0, next_block&0xFF);		dataflash_write_to_page_buffer(FILESYSTEM_FILE_NEXT_HI, 0, (next_block>>8)&0xFF);		//store in flash:		dataflash_copy_buffer_to_page(filesystem_current_block,0);		//no more free blocks -> exit  ///FIXME///		if (next_block == 0){			#if FILESYSTEM_DEBUG			softuart_puts_progmem("FS  : no more blocks ?!");			softuart_putnewline();			#endif			return;		}		//update block info		filesystem_current_block = next_block;	}else{		//last block -> we do not need a next pointer		//step6: save next block pointer:		dataflash_write_to_page_buffer(FILESYSTEM_FILE_NEXT_LO, 0, 0x00);		dataflash_write_to_page_buffer(FILESYSTEM_FILE_NEXT_HI, 0, 0x00);				dataflash_write_to_page_buffer(FILESYSTEM_FILE_BLOCK_USAGE, 0, filesystem_file_pos-1);		//store in flash:		dataflash_copy_buffer_to_page(filesystem_current_block,0);	}}/// DO NOT USE THIS !!!!!!!!//seek to given byte position!//file must be open!/*unsigned char filesystem_seek_to(unsigned int pos){	unsigned int tmp_block;	unsigned int tmp_count=0;	unsigned int fs_first_byte_of_current_block;	unsigned char last=0;		#if FILESYSTEM_DEBUG	softuart_puts_progmem("FS  : DO NOT USE SEEK ! NOT YET FINISHED (buggy!!)\r\n");	#endif	//check for invalid fileid:	if (filesystem_fileid == 0)		return 0;	fs_first_byte_of_current_block = filesystem_current_block*256;	if (pos < fs_first_byte_of_current_block+filesystem_file_pos){		//requested pos is smaller than current pos !		if (pos > fs_first_byte_of_current_block){			//but position is inside current active block!			//-> set fileposition:			filesystem_file_pos = pos & 0xFF; //= pos%256		}else{			//position is inside a smaller block!			//store current data:			dataflash_write_n_to_page_buffer(0,0,filesystem_buffer, 256);			if (filesystem_block_count > filesystem_current_block)				filesystem_store_current_block(1); //not the last block!			else				filesystem_store_current_block(0); //this is the last block!			//seek from first block:			tmp_block = filesystem_first_block;						while ((tmp_count < (pos/256)) && (tmp_block != 0x0000)){				//copy block to buffer0:				dataflash_copy_page_to_buffer(tmp_block, 0);				//calc next block:				tmp_block  = dataflash_read_buffer(FILESYSTEM_FILE_NEXT_HI, 0)<<8;				tmp_block |= dataflash_read_buffer(FILESYSTEM_FILE_NEXT_LO, 0);						//tmp should be zero if this is the last block. but we have a flag for last block, too:				if (dataflash_read_buffer(FILESYSTEM_FILE_BLOCKTAG,0) == 0xEE)					tmp_block = 0x0000; //last block!				tmp_count++;			}			if (tmp_block == 0x0000)				return 0; //FS ERROR !! there should be no next block pointer with zero next pointer inside file !			if (tmp_count > filesystem_block_count)				filesystem_block_count = tmp_count;			filesystem_current_block = tmp_block;			filesystem_file_pos      = pos & 0xFF; //= pos%256			return 1;		}	}else{		//file position is bigger than current filepos		//search to filepos & append empty blocks if necessary (= when req pos > file size) 		tmp_block = filesystem_current_block;		tmp_count = filesystem_block_count;		//store current data:		dataflash_write_n_to_page_buffer(0,0,filesystem_buffer, 256);		filesystem_store_current_block(1); //not the last block!		while ((tmp_count < (pos/256)) && (tmp_block != 0x0000)){			//is this block the last ?!			if (tmp_count + 256 > (pos/256)){				//this is the last block:				last = 1;				//setup file pos:				filesystem_file_pos = pos&0xFF;			}else{				//not the last -> more data follows				last = 0;				filesystem_file_pos = 255;			}			//copy block to buffer0:			dataflash_copy_page_to_buffer(tmp_block, 0);			//calc next block:			tmp_block  = dataflash_read_buffer(FILESYSTEM_FILE_NEXT_HI, 0)<<8;			tmp_block |= dataflash_read_buffer(FILESYSTEM_FILE_NEXT_LO, 0);					//tmp should be zero if this is the last block. but we have a flag for last block, too:			if ((dataflash_read_buffer(FILESYSTEM_FILE_BLOCKTAG,0) == 0xEE) ||	(tmp_block = 0x0000)){				//this is the last block of the file !					//we need to append empty blocks now !				filesystem_store_current_block(!(last==1));			}			tmp_count++;		}		//now we are in the right block		//setup file pos:		filesystem_file_pos = pos & 0xFF;		return 1;	}	return 0;}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -