OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [filesys.c] - Diff between revs 398 and 415

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 398 Rev 415
Line 20... Line 20...
 *        OS_fwrite()              //write file entry into directory
 *        OS_fwrite()              //write file entry into directory
 *        BlockRead()              //flush changes to directory
 *        BlockRead()              //flush changes to directory
 *--------------------------------------------------------------------*/
 *--------------------------------------------------------------------*/
#include "rtos.h"
#include "rtos.h"
 
 
#define FLASH_SIZE        1024*1024*16
#define FLASH_SIZE        (1024*1024*16)
#define FLASH_SECTOR_SIZE 1024*128
#define FLASH_SECTOR_SIZE (1024*128)
#define FLASH_BLOCK_SIZE  512
#define FLASH_BLOCK_SIZE  512
#define FLASH_LN2_SIZE    9                  //2^FLASH_LN2_SIZE == FLASH_BLOCK_SIZE
#define FLASH_LN2_SIZE    9                  //2^FLASH_LN2_SIZE == FLASH_BLOCK_SIZE
#define FLASH_OFFSET      FLASH_SECTOR_SIZE  //offset to start of flash file system
#define FLASH_OFFSET      FLASH_SECTOR_SIZE  //offset to start of flash file system
 
#define FLASH_BLOCKS      FLASH_SIZE/FLASH_BLOCK_SIZE
 
#define FLASH_START       (FLASH_OFFSET+FLASH_BLOCKS/8*2)/FLASH_BLOCK_SIZE
 
 
#define BLOCK_SIZE        512
#define BLOCK_SIZE        512
#define FILE_NAME_SIZE    40
#define FILE_NAME_SIZE    40
#define FULL_NAME_SIZE    128
#define FULL_NAME_SIZE    128
#define BLOCK_MALLOC      0x0
#define BLOCK_MALLOC      0x0
Line 87... Line 89...
int OS_fdir(OS_FILE *dir, char name[64]);
int OS_fdir(OS_FILE *dir, char name[64]);
void OS_fdelete(char *name);
void OS_fdelete(char *name);
 
 
 
 
/***************** Media Functions Start ***********************/
/***************** Media Functions Start ***********************/
#ifdef INCLUDE_FLASH
#ifndef EXCLUDE_FLASH
#define FLASH_BLOCKS FLASH_SIZE/FLASH_BLOCK_SIZE
 
#define FLASH_START  (FLASH_OFFSET+FLASH_BLOCKS/8*2)/FLASH_BLOCK_SIZE
 
static unsigned char FlashBlockEmpty[FLASH_BLOCKS/8];
static unsigned char FlashBlockEmpty[FLASH_BLOCKS/8];
static unsigned char FlashBlockUsed[FLASH_BLOCKS/8];
static unsigned char FlashBlockUsed[FLASH_BLOCKS/8];
static int FlashBlock;
static int FlashBlock;
 
 
//Free unused flash blocks
//Free unused flash blocks
Line 104... Line 104...
 
 
   printf("FlashCleanup\n");
   printf("FlashCleanup\n");
   buf = (unsigned char*)malloc(FLASH_SECTOR_SIZE);
   buf = (unsigned char*)malloc(FLASH_SECTOR_SIZE);
   if(buf == NULL)
   if(buf == NULL)
      return 0;
      return 0;
 
   FlashLock();
   for(sector = FLASH_OFFSET / FLASH_SECTOR_SIZE; sector < FLASH_SIZE / FLASH_SECTOR_SIZE; ++sector)
   for(sector = FLASH_OFFSET / FLASH_SECTOR_SIZE; sector < FLASH_SIZE / FLASH_SECTOR_SIZE; ++sector)
   {
   {
 
      printf(".");
      FlashRead((uint16*)buf, FLASH_SECTOR_SIZE*sector, FLASH_SECTOR_SIZE);
      FlashRead((uint16*)buf, FLASH_SECTOR_SIZE*sector, FLASH_SECTOR_SIZE);
      if(sector == FLASH_OFFSET / FLASH_SECTOR_SIZE)
      if(sector == FLASH_OFFSET / FLASH_SECTOR_SIZE)
      {
      {
         for(i = 0; i < FLASH_BLOCKS/8; ++i)
         for(i = FLASH_START/8; i < FLASH_BLOCKS/8; ++i)
            FlashBlockEmpty[i] |= ~FlashBlockUsed[i];
            FlashBlockEmpty[i] |= ~FlashBlockUsed[i];
 
         memset(FlashBlockEmpty, 0, FLASH_START/8);
         memcpy(buf, FlashBlockEmpty, sizeof(FlashBlockEmpty));
         memcpy(buf, FlashBlockEmpty, sizeof(FlashBlockEmpty));
         memset(FlashBlockUsed, 0xff, sizeof(FlashBlockUsed));
         memset(FlashBlockUsed, 0xff, sizeof(FlashBlockUsed));
         memset(buf+sizeof(FlashBlockEmpty), 0xff, sizeof(FlashBlockUsed));
         memset(buf+sizeof(FlashBlockEmpty), 0xff, sizeof(FlashBlockUsed));
      }
      }
      //Erase empty blocks
      //Erase empty blocks
      for(block = 0; block < FLASH_SECTOR_SIZE / FLASH_BLOCK_SIZE; ++block)
      for(block = 0; block < FLASH_SECTOR_SIZE / FLASH_BLOCK_SIZE; ++block)
      {
      {
         i = sector * FLASH_SECTOR_SIZE / FLASH_BLOCK_SIZE + block;
         i = sector * FLASH_SECTOR_SIZE / FLASH_BLOCK_SIZE + block;
         if(i < FLASH_BLOCKS/8 && (FlashBlockEmpty[i >> 3] & (1 << (i & 7))))
         if(FlashBlockEmpty[i >> 3] & (1 << (i & 7)))
         {
         {
            memset(buf + FLASH_BLOCK_SIZE*block, 0xff, FLASH_BLOCK_SIZE);
            memset(buf + FLASH_BLOCK_SIZE*block, 0xff, FLASH_BLOCK_SIZE);
            ++count;
            ++count;
         }
         }
      }
      }
      FlashErase(FLASH_SECTOR_SIZE * sector);
      FlashErase(FLASH_SECTOR_SIZE * sector);
      FlashWrite((uint16*)buf, FLASH_SECTOR_SIZE * sector, FLASH_SECTOR_SIZE);
      FlashWrite((uint16*)buf, FLASH_SECTOR_SIZE * sector, FLASH_SECTOR_SIZE);
   }
   }
 
   FlashBlock = FLASH_START;
 
   FlashUnlock();
   free(buf);
   free(buf);
   return count;
   return count;
}
}
 
 
 
 
Line 139... Line 144...
{
{
   FlashRead((uint16*)FlashBlockEmpty, FLASH_OFFSET, sizeof(FlashBlockEmpty));
   FlashRead((uint16*)FlashBlockEmpty, FLASH_OFFSET, sizeof(FlashBlockEmpty));
   FlashRead((uint16*)FlashBlockUsed, FLASH_OFFSET+sizeof(FlashBlockEmpty),
   FlashRead((uint16*)FlashBlockUsed, FLASH_OFFSET+sizeof(FlashBlockEmpty),
             sizeof(FlashBlockUsed));
             sizeof(FlashBlockUsed));
   FlashBlock = FLASH_START;
   FlashBlock = FLASH_START;
 
   memset(FlashBlockEmpty, 0, FLASH_START/8);  //space for FlashBlockEmpty/Used
   return FlashBlockEmpty[FlashBlock >> 3] & (1 << (FlashBlock & 7));
   return FlashBlockEmpty[FlashBlock >> 3] & (1 << (FlashBlock & 7));
}
}
#endif
#endif  //!EXCLUDE_FLASH
 
 
 
 
static uint32 MediaBlockMalloc(OS_FILE *file)
static uint32 MediaBlockMalloc(OS_FILE *file)
{
{
   int i, j;
   int i, j;
   (void)i; (void)j;
   (void)i; (void)j;
 
 
   if(file->fileEntry.mediaType == FILE_MEDIA_RAM)
   if(file->fileEntry.mediaType == FILE_MEDIA_RAM)
      return (uint32)malloc(file->fileEntry.blockSize);
      return (uint32)malloc(file->fileEntry.blockSize);
#ifdef INCLUDE_FLASH
#ifndef EXCLUDE_FLASH
   //Find empty flash block
   //Find empty flash block
   for(i = FlashBlock; i < FLASH_BLOCKS; ++i)
   for(i = FlashBlock; i < FLASH_BLOCKS; ++i)
   {
   {
      if(FlashBlockEmpty[i >> 3] & (1 << (i & 7)))
      if(FlashBlockEmpty[i >> 3] & (1 << (i & 7)))
      {
      {
Line 181... Line 187...
 
 
static void MediaBlockFree(OS_FILE *file, uint32 blockIndex)
static void MediaBlockFree(OS_FILE *file, uint32 blockIndex)
{
{
   if(file->fileEntry.mediaType == FILE_MEDIA_RAM)
   if(file->fileEntry.mediaType == FILE_MEDIA_RAM)
      free((void*)blockIndex);
      free((void*)blockIndex);
#ifdef INCLUDE_FLASH
#ifndef EXCLUDE_FLASH
   else
   else
   {
   {
      int i=blockIndex, j;
      int i=blockIndex, j;
      FlashBlockUsed[i >> 3] &= ~(1 << (i & 7));
      FlashBlockUsed[i >> 3] &= ~(1 << (i & 7));
      j = i >> 3;
      j = i >> 3;
Line 198... Line 204...
 
 
static void MediaBlockRead(OS_FILE *file, uint32 blockIndex)
static void MediaBlockRead(OS_FILE *file, uint32 blockIndex)
{
{
   if(file->fileEntry.mediaType == FILE_MEDIA_RAM)
   if(file->fileEntry.mediaType == FILE_MEDIA_RAM)
      file->block = (OS_Block_t*)blockIndex;
      file->block = (OS_Block_t*)blockIndex;
#ifdef INCLUDE_FLASH
#ifndef EXCLUDE_FLASH
   else
   else
   {
   {
      file->block = &file->blockLocal;
      file->block = &file->blockLocal;
      FlashRead((uint16*)file->block, blockIndex << FLASH_LN2_SIZE, FLASH_BLOCK_SIZE);
      FlashRead((uint16*)file->block, blockIndex << FLASH_LN2_SIZE, FLASH_BLOCK_SIZE);
   }
   }
Line 212... Line 218...
 
 
static void MediaBlockWrite(OS_FILE *file, uint32 blockIndex)
static void MediaBlockWrite(OS_FILE *file, uint32 blockIndex)
{
{
   (void)file;
   (void)file;
   (void)blockIndex;
   (void)blockIndex;
#ifdef INCLUDE_FLASH
#ifndef EXCLUDE_FLASH
   if(file->fileEntry.mediaType != FILE_MEDIA_RAM)
   if(file->fileEntry.mediaType != FILE_MEDIA_RAM)
      FlashWrite((uint16*)file->block, blockIndex << FLASH_LN2_SIZE, FLASH_BLOCK_SIZE);
      FlashWrite((uint16*)file->block, blockIndex << FLASH_LN2_SIZE, FLASH_BLOCK_SIZE);
#endif
#endif
}
}
 
 
Line 485... Line 491...
      rootFileEntry.mediaType = dir.fileEntry.mediaType;
      rootFileEntry.mediaType = dir.fileEntry.mediaType;
      rootFileEntry.blockIndex = dir.blockIndex;
      rootFileEntry.blockIndex = dir.blockIndex;
      rootFileEntry.blockSize = dir.fileEntry.blockSize;
      rootFileEntry.blockSize = dir.fileEntry.blockSize;
      rootFileEntry.isDirectory = 1;
      rootFileEntry.isDirectory = 1;
      BlockRead(&dir, BLOCK_EOF);    //Flush data
      BlockRead(&dir, BLOCK_EOF);    //Flush data
#ifdef INCLUDE_FLASH
#ifndef EXCLUDE_FLASH
      file = OS_fopen("flash", "w+");
      file = OS_fopen("flash", "w+");
      if(file == NULL)
      if(file == NULL)
         return NULL;
         return NULL;
      file->fileEntry.isDirectory = 1;
      file->fileEntry.isDirectory = 1;
      file->fileEntry.mediaType = FILE_MEDIA_FLASH;
      file->fileEntry.mediaType = FILE_MEDIA_FLASH;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.