📄 store_file.c
字号:
//---------------------------------------------------------------------------
// Store_File.c
//---------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
// Date: 06/26/06 10:05:33
// Target:
// Version: 1.0
//
// Description:
//
// This example allows the user to store a desired number of files into the
// CP2200 flash and read them of the flash using a memory table stored in
// the flash itself. The number files is specified by <num_entries>. The
// Store_File() function stores the files while the Load_File() function
// loads them from the CP2200 flash.
#include "mn_stackconst.h" // TCP/IP Library Constants
#include "mn_errs.h" // Library Error Codes
#include "mn_defs.h" // Library Type definitions
#include "mn_funcs.h" // Library Function Prototypes
#include <string.h>
//--------------------------------------------------------------------------
// To Store a File into the flash
//--------------------------------------------------------------------------
void Store_File(int num_entries)
{
VF *vf_entry_ptr; // Pointer to the vf_structure
int i = 0,j; // counters
int filename_length; // Used to store the length of file name
int file_number=0; // Keeps track of the number of files
int currentfile_address;
int file_size = 0; // Stores the size of a file
int table_size; // stores table size
int start_add; // Starting address of the first file
int table_address; // Used to store values in the table
// Used to temporarily hold address of current file
int temp_file_address;
// Used to temporarily hold address of current table entry
int temp_table_address;
// Used to clear pages in flash memory
int page_address;
// Keeps track of the offset from the starting address
int offset = 0; // Calculates offset for table entries
unsigned char *dataptr; // Points to the data stored in the file
char file_content; // Stores the file content
//-----------------------------------------------------------------------
// Erase CP220x flash
//-----------------------------------------------------------------------
for(page_address = 0; page_address < 0x1E00; page_address += 512)
{
CPFLASH_PageErase(page_address);
// erases one page of flash
while(flash_busy);
// wait for the flash_busy flag
}
//-----------------------------------------------------------------------
// Fill the CP220x Flash
//-----------------------------------------------------------------------
// Calculate table size
table_size = VF_NAME_LEN * num_entries;
// Table structure:
// | filename_length | filename | currentfile_address | file_size |
// | (2 bytes) | (2 bytes) | (2 bytes) | (2 bytes) |
// Same format followed for all files.
// The Table stores the name, size and the address of the files
// respectively.
// The table is terminated with xFF
//Store the starting address of the first file
start_add = table_size;
// Storing the starting address of first file in temp_file_address
temp_file_address = table_size;
//-----------------------------------------------------------------------
//Loop through each file to add to the CP220x
//-----------------------------------------------------------------------
while(file_number < num_entries)
{
// Specify starting address for write to memory
currentfile_address = temp_file_address;
// Make vf_entry_ptr a pointer to the vf structures of the HTML files
vf_entry_ptr = &vf_dir[i];
// read the file from the virtual system entry
file_size = vf_entry_ptr->page_size;
// obtain a pointer to the file in the MCU flash
dataptr = vf_entry_ptr->page_ptr;
// Write the file to the CP220x Flash
for(j = 0; j < file_size; j++)
{
// if address is greater than 0x1FFA than an error is reported
// infinite while if data written to an invalid or mac address
if (currentfile_address >= 0x1FFA) while(1){};
// get a byte of a file
file_content = *dataptr;
// write the character to CP220x flash
CPFLASH_ByteWrite(currentfile_address,file_content);
while(flash_busy);
// update the file address variable
currentfile_address++;
dataptr++;
}
// Store the ending address of a file in temp_file_address
temp_file_address = currentfile_address;
//-------------------------------------------------------------------
// Enter values into the memory table
//-------------------------------------------------------------------
// If first file then starting address is zero
if (file_number == 0) temp_table_address = 0;
// load a with the ending address of the previous table entry
table_address = temp_table_address;
if (i > 0)
{ // calculate offset for the next table entry
offset = 0;
for(j = 0; j < i; j++)
{
vf_entry_ptr = &vf_dir[j];
// calculate the offset from the starting address
offset += vf_entry_ptr->page_size;
}
}
//-------------------------------------------------------------------
// Store File name length in the CP220x Flash file table
//-------------------------------------------------------------------
// Make vf_entry_ptr point to the file structure
vf_entry_ptr = &vf_dir[i];
// length of a file name
filename_length = strlen(vf_entry_ptr->filename);
// Write file length into the table
CPFLASH_ByteWrite(table_address,filename_length);
while(flash_busy);
table_address++;
//-------------------------------------------------------------------
// Store File name in the CP220x Flash file table
//-------------------------------------------------------------------
for(j = 0; j < filename_length; j++)
{
// Write the file name to the CP220x Flash
CPFLASH_ByteWrite(table_address, vf_entry_ptr->filename[j]);
while(flash_busy);
table_address++; // update the address
}
//-------------------------------------------------------------------
// Store File starting address in the CP220x Flash file table
//-------------------------------------------------------------------
// add size of the file to the offset
offset = start_add + offset;
// write the high byte address of the file
CPFLASH_ByteWrite(table_address, offset>>8);
while(flash_busy);
table_address++;
// write the low byte address of the file
CPFLASH_ByteWrite(table_address, offset);
while(flash_busy);
table_address++;
//-------------------------------------------------------------------
// Store File size in the CP220x Flash file table
//-------------------------------------------------------------------
// write the high byte of file size
CPFLASH_ByteWrite(table_address, (vf_entry_ptr->page_size)>>8);
while(flash_busy);
table_address++;
// write the low byte of the file size
CPFLASH_ByteWrite(table_address, vf_entry_ptr->page_size);
while(flash_busy);
table_address++;
// update counter
i++;
temp_table_address = table_address;
// update file number
file_number++;
}
// delimiter to indicate End Of File
CPFLASH_ByteWrite(table_address, 255);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -