📄 ex1.c
字号:
/**************************************************************************
* *
* PROJECT : ARM port for UCOS-II *
* *
* MODULE : EX1.c *
* *
* AUTHOR : Michael Anburaj *
* URL : http://geocities.com/michaelanburaj/ *
* EMAIL: michaelanburaj@hotmail.com *
* *
* PROCESSOR : Any processor *
* *
* IDE : Any IDE *
* *
* DESCRIPTION : *
* This is a sample code to test UCOS-II. *
* *
**************************************************************************/
#include "includes.h"
#include "consol.h"
#include "fs_api.h"
/* ********************************************************************* */
/* Global definitions */
/* ********************************************************************* */
/* File local definitions */
#define TASK_STK_SIZE 256 /* Size of each task's stacks (# of WORDs) */
#define NO_TASKS 10 /* Number of identical tasks */
OS_STK TaskStk[NO_TASKS][TASK_STK_SIZE]; /* Tasks stacks */
OS_STK TaskStartStk[TASK_STK_SIZE];
char TaskData[NO_TASKS]; /* Parameters to pass to each task */
OS_EVENT *RandomSem;
FS_FILE *myfile;
char mybuffer[0x100];
const char dev_default_msg[]="This text was written on your default device.\n";
const char dev_ram_msg[]="This text was written on your RAM disk.\n";
static void _write_file(const char *name, const char *txt);
static void _dump_file(const char *name);
static void _show_directory(const char *name);
static void _show_free(const char *device);
/*********************************************************************
*
* debug output routines
*/
void _error(const char *msg) {
CONSOL_Printf("%s",msg);
}
void _log(const char *msg) {
CONSOL_Printf("%s",msg);
}
/* ********************************************************************* */
/* Local functions */
void Task (void *data)
{
U8 err;
while(1)
{
OSSemPend(RandomSem, 0, &err); /* Acquire semaphore to perform random numbers */
OSSemPost(RandomSem); /* Release semaphore */
CONSOL_SendChar(*(char *)data); /* Display the task number on the screen */
OSTimeDly(5); /* Delay 5 clock tick */
}
}
void TaskStart (void *data)
{
#if 0
U8 i;
char key;
data = data; /* Prevent compiler warning */
CONSOL_Printf("uC/OS-II, The Real-Time Kernel ARM Ported version\n");
CONSOL_Printf("Jean J. Labrosse/ (Ported by) Michael Anburaj\n");
CONSOL_Printf("EXAMPLE #1\n");
CONSOL_Printf("Determining CPU's capacity ...\n");
// FRMWRK_vStartTicker(OS_TICKS_PER_SEC); /* The Tick timer is started much earlier */
OSStatInit(); /* Initialize uC/OS-II's statistics */
for (i = 0; i < NO_TASKS; i++)
{ /* Create NO_TASKS identical tasks */
TaskData[i] = '0' + i; /* Each task will display its own letter */
CONSOL_Printf("#%d",i);
OSTaskCreate(Task, (void *)&TaskData[i], (void *)&TaskStk[i][TASK_STK_SIZE - 1], i + 1);
}
CONSOL_Printf("\n# Parameter1: No. Tasks\n");
CONSOL_Printf("# Parameter2: CPU Usage in %%\n");
CONSOL_Printf("# Parameter3: No. Task switches/sec\n");
CONSOL_Printf("<-PRESS 'ESC' TO QUIT->\n");
while(1)
{
CONSOL_Printf(" %d", OSTaskCtr); /* Display #tasks running */
CONSOL_Printf(" %d", OSCPUUsage); /* Display CPU usage in % */
CONSOL_Printf(" %d\n", OSCtxSwCtr); /* Display #context switches per second */
OSCtxSwCtr = 0;
if(CONSOL_GetChar(&key) == True)
{ /* See if key has been pressed */
if(key == 0x1B) /* Yes, see if it's the ESCAPE key */
while(1); /* Stay here for ever */
}
OSTimeDlyHMSM(0, 0, 1, 0); /* Wait one second */
}
#endif
int x;
/* format your RAM disk */
x = FS_IoCtl("ram:",FS_CMD_FORMAT_MEDIA,FS_MEDIA_RAM_16KB,0);
if (x!=0) {
_error("Cannot format RAM disk.\n");
}
/* create file 'default.txt' on your default device */
_write_file("default.txt",dev_default_msg);
/* create file 'ram.txt' on your RAM disk */
_write_file("ram:\\ram.txt",dev_ram_msg);
/* dump file 'default.txt' on you default device */
_dump_file("default.txt");
/* dump file 'ram.txt' on you default RAM disk */
_dump_file("ram:\\ram.txt");
#if FS_POSIX_DIR_SUPPORT
/* show root directory of default device */
_show_directory("");
/* show root directory of RAM disk */
_show_directory("ram:");
#endif /* FS_POSIX_DIR_SUPPORT */
/* display disk space information of the default device */
_show_free("");
/* display disk space information of the RAM disk */
_show_free("ram:");
/* embOS task must not exit */
while (1) {
OSTimeDly(50);
}
}
/* ********************************************************************* */
/* Global functions */
void APP_vMain (void)
{
OSInit(); /* Initialize uC/OS-II */
RandomSem = OSSemCreate(1); /* Random number semaphore */
OSTaskCreate(TaskStart, (void *)0, (void *)&TaskStartStk[TASK_STK_SIZE - 1], 0);
FRMWRK_vStartTicker(OS_TICKS_PER_SEC); /* os_cfg.h */
OSStart(); /* Start multitasking */
}
/* ********************************************************************* */
/*********************************************************************
*
* _dump_file
*
This routine demonstrates, how to open and read from a file using
the file system.
*/
static void _dump_file(const char *name) {
int x;
/* open file */
myfile = FS_FOpen(name,"r");
if (myfile) {
/* read until EOF has been reached */
do {
x = FS_FRead(mybuffer,1,sizeof(mybuffer)-1,myfile);
mybuffer[x]=0;
if (x) {
_log(mybuffer);
}
} while (x);
/* check, if there is no more data, because of EOF */
x = FS_FError(myfile);
if (x!=FS_ERR_EOF) {
/* there was a problem during read operation */
/*sprintf(mybuffer,"Error %d during read operation.\n",x);*/
_error("Error during read operation.\n");
}
/* close file */
FS_FClose(myfile);
}
else {
/*sprintf(mybuffer,"Unable to open file %s.\n",name);*/
_error("Unable to open file \n");
}
}
/*********************************************************************
*
* _show_directory
*
This routine demonstrates, how to read a directory.
*/
#if FS_POSIX_DIR_SUPPORT
static void _show_directory(const char *name) {
FS_DIR *dirp;
struct FS_DIRENT *direntp;
_log("Directory of ");
_log(name);
_log("\n");
dirp = FS_OpenDir(name);
if (dirp) {
do {
direntp = FS_ReadDir(dirp);
if (direntp) {
/*sprintf(mybuffer,"%s\n",direntp->d_name);*/
//_log("FileName\n");
CONSOL_Printf("%s\n",direntp->d_name);
}
} while (direntp);
FS_CloseDir(dirp);
}
else {
_error("Unable to open directory\n");
}
}
#endif /* FS_POSIX_DIR_SUPPORT */
/*********************************************************************
*
* _show_free
*
This routine demonstrates, how to read disk space information.
*/
static void _show_free(const char *device) {
FS_DISKFREE_T disk_data;
int x;
_log("Disk information of ");
_log(device);
_log("\n");
x = FS_IoCtl(device,FS_CMD_GET_DISKFREE,0,(void*) &disk_data);
if (x==0) {
/*sprintf(mybuffer,"total clusters : %lu\navailable clusters : %lu\nsectors/cluster : %u\nbytes per sector : %u\n",
disk_data.total_clusters, disk_data.avail_clusters, disk_data.sectors_per_cluster, disk_data.bytes_per_sector);*/
//_log("free space\n");
CONSOL_Printf("total clusters : %lu\navailable clusters : %lu\nsectors/cluster : %u\nbytes per sector : %u\n",
disk_data.total_clusters, disk_data.avail_clusters, disk_data.sectors_per_cluster, disk_data.bytes_per_sector);
}
else {
_error("Invalid drive specified\n");
}
}
/*********************************************************************
*
* _write_file
*
This routine demonstrates, how to create and write to a file
using the file system.
*/
static void _write_file(const char *name, const char *txt) {
int x;
/* create file */
myfile = FS_FOpen(name,"w");
if (myfile) {
/* write to file */
x = FS_FWrite(txt,1,strlen(txt),myfile);
/* all data written ? */
if (x!=(int)strlen(txt)) {
/* check, why not all data was written */
x = FS_FError(myfile);
/* sprintf(mybuffer,"Not all bytes written because of error %d.\n",x);*/
_error("Not all bytes written because of error .\n");
}
/* close file */
FS_FClose(myfile);
}
else {
/*sprintf(mybuffer,"Unable to create file %s\n",name);*/
_error("Unable to create file \n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -