file.c

来自「好记星的控件,包括button,list,对文件操作」· C语言 代码 · 共 1,866 行 · 第 1/5 页

C
1,866 行
字号
                {   // 有未关闭文件
                    /* UINT8   aFileName[16];

                    GraphClearScreen();
                    GraphDrawText((MDC*)NULL, 0, 0, 200, 16, NULL, "有未关闭文件:");
                    memmove(aFileName, mem_file_pool[i].pobj->finode->fname, 8);
                    aFileName[8] = '.';
                    memmove(aFileName + 9, mem_file_pool[i].pobj->finode->fext, 3);
                    aFileName[12] = 0;
                    GraphDrawText((MDC*)NULL, 0, 16, 200, 16, NULL, aFileName);

                    while(1); */
                }
                else
                {   // 错误的关闭之前打开的文件
                    /*UINT8   aFileName[16];

                    GraphClearScreen();
                    GraphDrawText((MDC*)NULL, 0, 0, 200, 16, NULL, "多关闭了文件:");
                    memmove(aFileName, mem_file_pool[i].pobj->finode->fname, 8);
                    aFileName[8] = '.';
                    memmove(aFileName + 9, mem_file_pool[i].pobj->finode->fext, 3);
                    aFileName[12] = 0;
                    GraphDrawText((MDC*)NULL, 0, 16, 200, 16, NULL, aFileName);

                    while(1); */
                }
            }
        }
    }

    NU_Change_Preemption(opPreemptStatus);
}

/*************************************************************************
    Function   : FileDiskFormat
    Description: 格式化磁盘。提供一个函数,避免格式化、映射等处不统一

    Input:
    
    Output:
      
    Return:

      DATE      AUTHOR   VERSION  REMARKS
========================================================================== 
    2002-09-26  谢永良     1.0     create
**************************************************************************/

STATUS FileDiskFormat()
{
    FMTPARMS            fmt;
   	MFlashInfo	        fiFlsINfo;
    
    // 得到FLASH的相关信息
    FlashIoCtrl(NFLASH_DRIVENO, FLASH_COMMAND_GETINFO, (VOID *)(&fiFlsINfo));
    
    // 操作系统版本号
    memset(fmt.oemname, 0, 9);
    strncpy(fmt.oemname, "MJOS3.2", 8);

    fmt.totalsec    =  fiFlsINfo.uPageSum; // 总扇区数
    fmt.secpalloc   =  fmt.totalsec / 0x10000 + 1;  // 每簇扇区数,必须是2的倍数,即必须为偶数
    fmt.secreserved =  (UINT16) 1;                  // FAT表之前保留的扇区数,至少为1
    fmt.numfats     =  (UINT8) 2;                   // FAT表个数,一般为2
    fmt.numroot     =  (UINT16) MAX_ROOT_ENTRY;     // 根目录项数
    fmt.bytepsec    =  (UINT16) NORMAL_PAGE_SIZE;   // 每扇区字节数
    fmt.mediadesc   =  (UINT8)  0xFD;               // 介质标识:硬盘-F8;软盘-F0
    fmt.partdisk    =  (UINT8)  0;                  // 磁盘类型:0-Not paritioned disk
                                                    //           1-Partitioned disk
    fmt.numhead     =  (UINT16) 0x10;               // 磁头数
    fmt.numcyl      =  (UINT16) 0x10;               // 柱面数
    fmt.secptrk     =  (UINT16) (fmt.totalsec/fmt.numhead/fmt.numcyl);
                                                    // 每道扇区数
    fmt.physical_drive_no =   0;                    // 启动盘信息;80h-启动盘;00h非启动盘
    fmt.binary_volume_label = 0x12345678L;          // 磁盘序列号

    // 磁盘卷标
    memset(fmt.text_volume_label, 0, 12);
    strncpy(fmt.text_volume_label, "MEIJINUDISK", 11);
    
    // Format the flash Disk
    return FileFormat(NFLASH_DRIVENO, &fmt);
}

/*************************** File test ******************************/
#ifdef FILE_TEST

/* Function prototypes  */
void    task_1(UNSIGNED argc, VOID *argv);
void    task_2(UNSIGNED argc, VOID *argv);
void    task_3(UNSIGNED argc, VOID *argv);
void    task_4(UNSIGNED argc, VOID *argv);

/* Declare Nucleus Tasks */
NU_TASK         Task_1;
NU_TASK         Task_2;
NU_TASK         Task_3;
NU_TASK         Task_4;
NU_TASK         Display_Task;

/* Define tasks numbers for array access */
#define TASK0         0
#define TASK1         1
#define TASK2         2
#define TASK3         3
#define TASK4         4
#define NUM_TASKS     5

/* Define events for tasks */
#define TASK0_EVENT  1
#define TASK1_EVENT  2
#define TASK2_EVENT  4
#define TASK3_EVENT  8
#define TASK4_EVENT  16

/* Declarations for global counters.  */
int     Files_Created[NUM_TASKS];
int     Files_Renamed[NUM_TASKS];
int     Files_Read[NUM_TASKS];
int     Files_Deleted[NUM_TASKS];
int     Directories_Created[NUM_TASKS];
int     Directories_Deleted[NUM_TASKS];
int     Errors[NUM_TASKS];
CHAR    stack1[0x2000];
CHAR    stack2[0x2000];
CHAR    stack3[0x2000];
CHAR    stack4[0x2000];
CHAR    stack_display[0x2000];
CHAR    displayBuf[50];

/* Declarations for other globals */
CHAR outbuf[512];      /* Buffer for writing file data */ 
CHAR inbuf[512];       /* Buffer for reading in file data */  

/* Declare event group */
NU_EVENT_GROUP  Event_Group_0;

/* These #defines are used in the demo tasks */
#define         DEMO_DRIVE    0       /* 1   Used for SD disk demo */
#define         DEMO_DISK     "A:"    /* B:  Used for SD disk demo */
#define         DEMO_ROOT     "A:/"  /* B:\ Used for SD disk demo */

void    bail_out(void)
{
    NU_Suspend_Task(NU_Current_Task_Pointer());
}

/************************************************************************/
/*                                                                      */
/*  FUNCTION                           "display_task"                   */
/*                                                                      */
/*                                                                      */
/*  DESCRIPTION                                                         */
/*                                                                      */
/*      This task sends global data out Serial port.                    */
/*      Then it checks to see if a key has been pressed.  If            */
/*      it has, it reads the key and sends it back out the              */
/*      serial port.  After that it sleeps for awhile and               */
/*      then repeats the entire process.                                */
/*                                                                      */
/*  AUTHOR                                                              */
/*                      Accelerated Technology                          */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      kernel                              Started by task_0.          */
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*      Calls multiple uart routines                                    */
/*                                                                      */
/************************************************************************/
void   display_task(UNSIGNED argc, VOID *argv)
{
STATUS status;
int ii;    /* Local loop counter */

    /* Access argc and argv just to avoid compilation warnings.  */
    status = (STATUS)argc + (STATUS)argv;

    while(1)
    {
        GraphClearScreen();

        sprintf(displayBuf, "Files Created -> ");
        GraphDrawText(0, 0, 0, 0, (UINT8 *)(&displayBuf[0]));


        for (ii=0; ii < NUM_TASKS; ii++)
		{
        sprintf(displayBuf, "%5d",Files_Created[ii]);
        GraphDrawText(8 + (ii%3)*40, 0x10 + (ii/3)*16, 0, 0, (UINT8 *)(&displayBuf[0]));
		}

        sprintf(displayBuf, "Files Deleted -> ");
        //GraphDrawText(0, 0x30, 0, 0, (UINT8 *)(&displayBuf[0]));

        for (ii=0; ii < NUM_TASKS; ii++)
		{
            sprintf(displayBuf, "%5d", Files_Deleted[ii]);
            //GraphDrawText(8 + (ii%3)*40, 0x40 + (ii/3)*16, 0, 0, (UINT8 *)(&displayBuf[0]));
		}

        sprintf(displayBuf, "Files Renamed -> ");
        //GraphDrawText(0, 0x60, 0, 0, (UINT8 *)(&displayBuf[0]));

        for (ii=0; ii < NUM_TASKS; ii++)
		{
			sprintf(displayBuf, "%5d", Files_Renamed[ii]);
            //GraphDrawText(8 + (ii%3)*40, 0x70 + (ii/3)*16, 0, 0, (UINT8 *)(&displayBuf[0]));
		}

        sprintf(displayBuf, "Files Read    -> ");
        //GraphDrawText(0, 0x90, 0, 0, (UINT8 *)(&displayBuf[0]));

        for (ii=0; ii < NUM_TASKS; ii++)
		{
            sprintf(displayBuf, "%5d", Files_Read[ii]);
            //GraphDrawText(8 + (ii%3)*40, 0xa0 + (ii/3)*16, 0, 0, (UINT8 *)(&displayBuf[0]));
		}

        sprintf(displayBuf, "Dirs Created  -> ");
        GraphDrawText(0, 0x30, 0, 0, (UINT8 *)(&displayBuf[0]));

        for (ii=0; ii < NUM_TASKS; ii++)
		{
            sprintf(displayBuf, "%5d", Directories_Created[ii]);
            GraphDrawText(8 + (ii%3)*40, 0x40 + (ii/3)*16, 0, 0, (UINT8 *)(&displayBuf[0]));
		}

        sprintf(displayBuf, "Dirs Deleted  -> ");
        //GraphDrawText(0, 0xf0, 0, 0, (UINT8 *)(&displayBuf[0]));

        for (ii=0; ii < NUM_TASKS; ii++)
		{
            sprintf(displayBuf, "%5d", Directories_Deleted[ii]);
            //GraphDrawText(8 + (ii%3)*40, 0x100 + (ii/3)*16, 0, 0, (UINT8 *)(&displayBuf[0]));
		}

        sprintf(displayBuf, "Errors        -> ");
        GraphDrawText(0, 0x60, 0, 0, (UINT8 *)(&displayBuf[0]));

        for (ii=0; ii < NUM_TASKS; ii++)
		{
            sprintf(displayBuf, "%5d", Errors[ii]);
            GraphDrawText(8 + (ii%3)*40, 0x70 + (ii/3)*16, 0, 0, (UINT8 *)(&displayBuf[0]));
		}

		/* Sleep 2 seconds */
		NU_Sleep(500);
    }
}

/************************************************************************/
/*                                                                      */
/*  FUNCTION                           "task_0"                         */
/*                                                                      */
/*                                                                      */
/*  DESCRIPTION                                                         */
/*                                                                      */
/*      This task is responsible for initializing the file system       */
/*      and then starting the other tasks in the demonstration.         */
/*      It then begins creating files in the root directory and         */
/*      writing 512 bytes to each file.  Then it deletes all files      */
/*      it created in the root directory using a wildcard.  The         */
/*      creating, writing, and deleting are repeated in an endless      */
/*      loop.                                                           */
/*                                                                      */
/*  AUTHOR                                                              */
/*                      Accelerated Technology                          */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      kernel                              Started upon creation.      */
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*      bail_out                            Error reporting routine.    */
/*      NU_Become_File_User                 Registers this task as a    */
/*                                          user of the file system.    */
/*      NU_Close                            Close a file.               */
/*      NU_Delete                           Delete a file(s).           */
/*      NU_Open                             Open a file for access.     */
/*      NU_Resume_Task                      Resume suspended task.      */
/*      NU_Set_Default_Drive                Select default drive.       */
/*      NU_Write                            Write data to a file.       */
/*                                          on the disk.                */
/*                                                                      */
/*  INPUTS                                                              */
/*       NONE.                                                          */
/*                                                                      */
/*  OUTPUTS                                                             */
/*       NONE                                                           */
/*                                                                      */
/************************************************************************/
#ifdef FILE_TASK_TEST

void   task_0(UNSIGNED argc, VOID *argv)
{
int fd;                         /* File descriptor for file access */ 
int ii;                         /* Local loop variable */
STATUS  status;                 /* Return value from funtion calls */
int task_id = TASK0;            /* Task number for this task */ 
CHAR fname[] = "T0F0FILE.TXT";  /* Base file name */


    /* Access argc and argv just to avoid compilation warnings.  */
    status = (STATUS)argc + (STATUS)argv;

    
    /* Create task 1 */
    status = NU_Create_Task(&Task_1, "TASK 1", task_1, 0, NU_NULL, stack1, 0x2000, 3, 10,
													NU_PREEMPT, NU_NO_START);

    /* Create task 2 */
    status = NU_Create_Task(&Task_2, "TASK 2", task_2, 0, NU_NULL, stack2, 0x2000, 3, 10,
													NU_PREEMPT, NU_NO_START);

    /* Create task 3 */
    status = NU_Create_Task(&Task_3, "TASK 3", task_3, 0, NU_NULL, stack3, 0x2000, 3, 10,
													NU_PREEMPT, NU_NO_START);

    /* Create task 4 */
    status = NU_Create_Task(&Task_4, "TASK 4", task_4, 0, NU_NULL, stack4, 0x2000, 3, 10,
													NU_PREEMPT, NU_NO_START);

    status = NU_Create_Task(&Display_Task, "DISPLAY TASK", display_task, 0, NU_NULL, stack_display, 0x2000, 3, 10,
													NU_PREEMPT, NU_NO_START);

    /* Create event flag group */
    NU_Create_Event_Group(&Event_Group_0, "EVGROUP0"); 

	/* Initialize global write buffer */
    memset(&outbuf[0], 'A', 512);

	/* Activate the other tasks now that the file system is initialized */
	NU_Resume_Task(&Task_1);
 	NU_Resume_Task(&Task_2);
	NU_Resume_Task(&Task_3);
	NU_Resume_Task(&Task_4);
	NU_Resume_Task(&Display_Task);

    /* Set the default drive to B: */
    status = NU_Set_Default_Drive(DEMO_DRIVE);

    /* Set current directory to root */
    status = NU_Set_Current_Dir (DEMO_ROOT);

    /* do forever loop */
    while (1)
	{
		/* Create 10 files in the root directory */
		for ( ii = 0; ii < 10; ii++)
		{
			/* Make unique file name for each */
            fname[3] = 0x30 + ii;  /* ascii numbers 0-4 */

			/* Create File */
			fd = NU_Open(fname,(PO_TEXT|PO_RDWR|PO_CREAT|PO_TRUNC),
				               (PS_IWRITE | PS_IREAD));

            /* Write 512 bytes to the new file */
            status = NU_Write(fd, outbuf, 512);

			/* Check status for number of bytes written */
			if (status == 512)
			{
                Files_Created[task_id]++;  /* Increment Task0 Files Created */
			}
			else
			{
			    Errors[task_id]++;  /* Log error */
			}

			/* Close file */

⌨️ 快捷键说明

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