hello everyone
I port the lastest version yaffs-direct to ATMEL9G35 as a ram-file-system, but I encount a problem:
yaffs_mount and yaffs_open work, but yaffs_write not work at all.
+ yaffs_write (fd, "hello world!", strlen ("hello world!"));
 + yaffsfs_do_write(fd, buf, nbyte, 0, 0);
  + yaffs_wr_file(obj, buf, pos, nToWrite, write_trhrough);
    + yaffs_do_file_wr(in, buffer, offset, n_bytes, write_through);
in yaffs_do_file_wr function, there is user-data in cache, but not in block. It seemed jump over "yaffs_wr_data_obj " :
 
if (write_through) {  // <----- here the write_though == 0...
chunk_written = yaffs_wr_data_obj (cache->object, cache->chunk_id, cache->data, cache->n_bytes, 1);
cache->dirty = 0;
}
 
My Code list as below, hope someone kind's help.
 
main.c:
yaffs_start_up ();
 
yaffs_mount ("/ram");
 
 
fd = yaffs_open ("/ram/abc", O_RDWR|O_CREAT, 0);
 
memset (bufTmp, 0, sizeof (bufTmp));
 
if (fd < 0) {
printf ("open file error");
} else {
yaffs_write (fd, "hello world!", strlen ("hello world!"));
yaffs_close (fd);
 
fd = yaffs_open ("/ram/abc", O_RDONLY, 0);
yaffs_read (fd, bufTmp, 32);
printf ("world in files : %s\n", bufTmp);
}
 
yaffsnewcfg.c:
 
int yaffs_start_up(void)
{
yramsim_CreateRamSim("ram",0,10,1,10);
return 0;
}
 
yramsim.c :
#define DATA_SIZE 2048
#define SPARE_SIZE 64
#define PAGE_SIZE (DATA_SIZE + SPARE_SIZE)
#define PAGES_PER_BLOCK 64
 
 
typedef struct {
unsigned char page[PAGES_PER_BLOCK][PAGE_SIZE];
unsigned blockOk;
} Block;
 
typedef struct {
Block **blockList;
int nBlocks;
} SimData;
 
struct ynandif_Geometry {
  unsigned char* privateData;
  unsigned start_block;
  unsigned end_block;
  unsigned dataSize;
  unsigned spareSize;
  unsigned pagesPerBlock;
  unsigned hasECC;
  unsigned inband_tags;
  unsigned useYaffs2;
  int (*initialise) (struct yaffs_dev *);
  int (*deinitialise) (struct yaffs_dev *);
  int (*readChunk) (struct yaffs_dev*, unsigned int, unsigned char*, unsigned int,
    unsigned char *, unsigned int, int *);
  int (*writeChunk) (struct yaffs_dev*, unsigned int, const unsigned char *, unsigned int,
    const unsigned char*, unsigned int);
  int (*eraseBlock) (struct yaffs_dev*, unsigned int);
  int (*checkBlockOk) (struct yaffs_dev*, unsigned int);
  int (*markBlockBad) (struct yaffs_dev*, unsigned int);
};
 
SimData *simDevs[N_RAM_SIM_DEVS];
 
struct yaffs_dev * yaffs_add_dev_from_geometry (const YCHAR *name, struct ynandif_Geometry *g);
 
static SimData *DevToSim(struct yaffs_dev *dev)
{
struct ynandif_Geometry *geom =
(struct ynandif_Geometry *)(dev->driver_context);
SimData * sim = (SimData*)(geom->privateData);
return sim;
}
 
 
static void CheckInitialised(void)
{
 
}
 
static int yramsim_erase_internal(SimData *sim, unsigned blockId,int force)
{
if((int)blockId < 0 || blockId >= sim->nBlocks){
return 0;
}
 
if(!sim->blockList[blockId]){
return 0;
}
 
if(!force && !sim->blockList[blockId]->blockOk){
return 0;
}
 
memset(sim->blockList[blockId],0xff,sizeof(Block));
sim->blockList[blockId]->blockOk = 1;
 
return 1;
}
 
 
 
 
static int yramsim_initialise(struct yaffs_dev *dev)
{
SimData *sim = DevToSim(dev);
Block **blockList = sim->blockList;
return blockList != NULL;
}
 
 
static int yramsim_deinitialise(struct yaffs_dev *dev)
{
return 1;
}
 
static int yramsim_rd_chunk (struct yaffs_dev *dev, unsigned pageId,
  unsigned char *data, unsigned dataLength,
  unsigned char *spare, unsigned spareLength,
  int *eccStatus)
{
SimData *sim = DevToSim(dev);
Block **blockList = sim->blockList;
 
unsigned blockId = pageId / PAGES_PER_BLOCK;
unsigned pageOffset = pageId % PAGES_PER_BLOCK;
unsigned char * d;
unsigned char *s;
if(blockId >= sim->nBlocks ||
   pageOffset >= PAGES_PER_BLOCK ||
   dataLength >DATA_SIZE ||
   spareLength > SPARE_SIZE ||
   !eccStatus ||
   !blockList[blockId]->blockOk){
   return 0;
}
 
d = blockList[blockId]->page[pageOffset];
s = d + DATA_SIZE;
 
if(data)
memcpy(data,d,dataLength);
 
if(spare)
memcpy(spare,s,spareLength);
 
*eccStatus = 0; // 0 = no error, -1 = unfixable error, 1 = fixable
 
return 1;
}
 
static int yramsim_wr_chunk (struct yaffs_dev *dev,unsigned pageId,
   const unsigned char *data, unsigned dataLength,
   const unsigned char *spare, unsigned spareLength)
{
SimData *sim = DevToSim(dev);
Block **blockList = sim->blockList;
 
unsigned blockId = pageId / PAGES_PER_BLOCK;
unsigned pageOffset = pageId % PAGES_PER_BLOCK;
unsigned char * d;
unsigned char *s;
if(blockId >= sim->nBlocks ||
   pageOffset >= PAGES_PER_BLOCK ||
   dataLength >DATA_SIZE ||
   spareLength > SPARE_SIZE ||
   !blockList[blockId]->blockOk){
   return 0;
}
 
d = blockList[blockId]->page[pageOffset];
s = d + DATA_SIZE;
 
if(data)
memcpy(d,data,dataLength);
 
if(spare)
memcpy(s,spare,spareLength);
 
return 1;
}
 
 
static int yramsim_erase(struct yaffs_dev *dev,unsigned blockId)
{
SimData *sim = DevToSim(dev);
 
CheckInitialised();
return yramsim_erase_internal(sim,blockId,0);
}
 
static int yramsim_check_block_ok(struct yaffs_dev *dev,unsigned blockId)
{
SimData *sim = DevToSim(dev);
Block **blockList = sim->blockList;
if(blockId >= sim->nBlocks){
return 0;
}
 
return blockList[blockId]->blockOk ? 1 : 0;
}
 
static int yramsim_mark_block_bad(struct yaffs_dev *dev,unsigned blockId)
{
SimData *sim = DevToSim(dev);
Block **blockList = sim->blockList;
if(blockId >= sim->nBlocks){
return 0;
}
 
blockList[blockId]->blockOk = 0;
 
return 1;
}
 
 
static SimData *yramsim_alloc_sim_data(u32 devId, u32 nBlocks)
{
int ok = 1;
 
Block **blockList;
SimData *sim;
Block *b;
u32 i;
 
if(devId >= N_RAM_SIM_DEVS)
return NULL;
 
sim = simDevs[devId];
 
if(sim)
return sim;
 
sim = malloc(sizeof (SimData));
if(!sim)
return NULL;
 
simDevs[devId] = sim;
 
blockList = malloc(nBlocks * sizeof(Block *));
 
sim->blockList = blockList;
sim->nBlocks = nBlocks;
if(!blockList){
free(sim);
return NULL;
}
 
for(i = 0; i < nBlocks; i++)
blockList[i] = NULL;
 
for(i = 0; i < nBlocks && ok; i++){
b=  malloc(sizeof(Block));
if(b){
blockList[i] = b;
yramsim_erase_internal(sim,i,1);
}
else
ok = 0;
}
 
if(!ok){
for(i = 0; i < nBlocks; i++)
if(blockList[i]){
free(blockList[i]);
blockList[i] = NULL;
}
free(blockList);
blockList = NULL;
free(sim);
sim = NULL;
}
 
return sim;
}
 
 
struct yaffs_dev *yramsim_CreateRamSim(const YCHAR *name,
u32 devId, u32 nBlocks,
u32 start_block, u32 end_block)
{
SimData *sim;
struct ynandif_Geometry *g;
 
sim = yramsim_alloc_sim_data(devId, nBlocks);
 
g = malloc(sizeof(*g));
 
if(!sim || !g){
if(g)
free(g);
return NULL;
}
 
if(start_block >= sim->nBlocks)
start_block = 0;
if(end_block == 0 || end_block >= sim->nBlocks)
end_block = sim->nBlocks - 1;
 
memset(g,0,sizeof(*g));
g->start_block = start_block;
g->end_block = end_block;
g->dataSize = DATA_SIZE;
g->spareSize= SPARE_SIZE;
g->pagesPerBlock = PAGES_PER_BLOCK;
g->hasECC = 1;
g->inband_tags = 0;
g->useYaffs2 = 1;
g->initialise = yramsim_initialise;
g->deinitialise = yramsim_deinitialise;
g->readChunk = yramsim_rd_chunk,
g->writeChunk = yramsim_wr_chunk,
g->eraseBlock = yramsim_erase,
g->checkBlockOk = yramsim_check_block_ok,
g->markBlockBad = yramsim_mark_block_bad,
g->privateData = (void *)sim;
 
return yaffs_add_dev_from_geometry(name,g);
}
 
struct yaffs_dev * yaffs_add_dev_from_geometry (const YCHAR *name, struct ynandif_Geometry *g)
{
struct yaffs_dev *dev;
struct yaffs_param *param;
struct yaffs_driver *drv;
 
dev = malloc(sizeof(struct yaffs_dev));
 
if(!dev)
goto fail;
 
memset(dev, 0, sizeof(*dev));
 
param = &dev->param;
 
param->name = name;
 
param->total_bytes_per_chunk = g->dataSize;
param->chunks_per_block = g->pagesPerBlock;
param->spare_bytes_per_chunk = g->spareSize;
param->n_reserved_blocks = 5;
param->start_block = g->start_block; // First block
param->end_block = g->end_block; // Last block
param->is_yaffs2 = 1;
param->use_nand_ecc = 1;
param->n_caches = 10;
 
drv = &dev->drv;
drv->drv_write_chunk_fn = g->writeChunk;
drv->drv_read_chunk_fn = g->readChunk;
drv->drv_erase_fn = g->eraseBlock;
drv->drv_mark_bad_fn = g->markBlockBad;
drv->drv_check_bad_fn = g->checkBlockOk;
drv->drv_initialise_fn = g->initialise;
drv->drv_deinitialise_fn = g->deinitialise;
 
dev->driver_context = (void *) g;
 
/* The yaffs device has been configured, install it into yaffs */
yaffs_add_device(dev);
 
return dev;
 
fail:
free(dev);
return NULL;
}

With kind regards

-------------------------------

ÌïÖÇÖÞ

TEL : 15501024303

E-mail : 284847706@qq.com
Address : Haidian,Beijing,100091,China