⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 retarget.c

📁 MiniGUI到ucOS-II的位图操作。通过该例子
💻 C
字号:
/***************************************************************************\
	Copyright (c) 2004-2007 threewater@up-tech.com, All rights reserved.
	by threewter	2004.4.26
\***************************************************************************/
	

/***************************************************************************\
    #说明: 系统标准C输入输出重定向函数
	----------------------------------  Bug  --------------------------------------

	----------------------------------  TODO list  ----------------------------------

	----------------------------------修正--------------------------------------
	2004-8-12	创建,测试通过

	----------------------------------使用说明--------------------------------

\***************************************************************************/

#include <stdio.h>
#include <rt_misc.h>
#include <time.h>
#include <string.h>

#include "../inc/sys/lib.h"

#ifdef __thumb 
/* Thumb Semihosting SWI */  
#define SemiSWI 0xAB  
#else  
/* ARM Semihosting SWI */  
#define SemiSWI 0x123456 
#endif 

//#define DEBUG
#undef DEBUG

#ifdef DEBUG
#define DPRINTF		printf
#else
#define DPRINTF
#endif

struct __FILE {
	U8 Buffer[Block_Size];	//文件缓冲区32*1024
	U32 fileCluster;		//文件当前的簇的位置
	U32 filemode;			//打开文件的模式
	U32 filebufnum;	//文件缓冲区中已经读取/写入的字节数
	U32 fileCurpos;	//读写的当前位置
	U32 filesize;	//文件的大小
	int rootpos;	//文件系统中目录的位置 by threewater
};
FILE __stdin, __stdout, __stderr;


extern unsigned int bottom_of_heap;     /* defined in heap.s */

int fputc(int ch, FILE *f)
{
    /* Place your implementation of fputc here     */
    /* e.g. write a character to a UART, or to the */
    /* debugger console with SWI WriteC            */
	if(f == &__stdout)	//for console out put e.g. printf
		CONSOLE_PUTC(ch);

	if(f == &__stderr)	//for console out put e.g. printf
		CONSOLE_PUTC(ch);

	return ch;
}


int ferror(FILE *f)
{   /* Your implementation of ferror */
	if(f!=&__stdout)
		DPRINTF("ferror:\n");
    return EOF;
}


void _sys_exit(int return_code)
{
	for(;;);
}


int __raise(int signal, int argument)//void _ttywrch(int ch)
{
	return 0;
}


__value_in_regs struct __initial_stackheap __user_initial_stackheap(
        unsigned R0, unsigned SP, unsigned R2, unsigned SL)
{
    struct __initial_stackheap config;
    
    config.heap_base = (unsigned int)&bottom_of_heap; // defined in heap.s
                                                      // placed by scatterfile   
    config.stack_base = SP;   // inherit SP from the execution environment

    return config;
}

/*
Below is an equivalent example assembler version of __user_initial_stackheap.

It will be entered with the value of the stackpointer in r1 (as set in init.s), 
this does not need to be changed and so can be passed unmodified out of the 
function. 

    IMPORT bottom_of_heap
    EXPORT __user_initial_stackheap

__user_initial_stackheap    
    LDR   r0,=bottom_of_heap
    MOV   pc,lr
*/

int fclose(FILE * stream)
{
	if(stream)
		CloseOSFile((OSFILE*)stream);

	return 0;
}

FILE *fopen(const char * filename, const char * mode)
{
#ifdef DEBUG
	FILE *ret;
#endif

	//we only support read only mode.
	if(strcmp(mode, "rb")!=0 || mode[0]!='r')
		return NULL;

	DPRINTF("fopen: %s\n", filename);

#ifdef DEBUG
	ret=OpenOSFile(filename , FILEMODE_READ);
	if(ret==NULL)
		DPRINTF("fopen: %s failed\n", filename);
	return ret;
#else
	return (FILE*)OpenOSFile((char*)filename , FILEMODE_READ);
#endif
}

FILE *freopen(const char * filename, const char * mode,
		FILE * stream)
{
	DPRINTF("freopen: %s\n", filename);
	fclose(stream);
	return fopen(filename, mode);
}

int fflush(FILE * stream)
{
	DPRINTF("fflush:\n");
	return 0;
}

int fseek(FILE * stream, long int offset, int whence)
{
	if(whence==SEEK_CUR)
		offset+=stream->fileCurpos;
	else if(whence==SEEK_END)
		offset+=stream->filesize;

	DPRINTF("seek: 0x%x\n", offset);

	if(SeekOSFile((OSFILE*)stream, offset)!=offset){
		DPRINTF("seek: failed");
		return -1;
	}

	return 0;
}

FILE *tmpfile(void)
{
	DPRINTF("tmpfile:\n");
	return NULL;
}

void rewind(FILE * stream)
{
	SeekOSFile((OSFILE*)stream, 0);
}

int fgetc(FILE * stream)
{
	unsigned char c;

	DPRINTF("fgetc:\n");
	if(ReadOSFile((OSFILE*)stream, &c, 1)!=1)
		return 0;

	return c;
}

size_t fread(void * ptr,size_t size, size_t nmemb, FILE * stream)
{
	DPRINTF("fread: %d byte", size*nmemb);
	return ReadOSFile((OSFILE*)stream, ptr, size*nmemb);
}

size_t fwrite(const void * ptr,size_t size, size_t nmemb, FILE * stream)
{
	return 0;
}

long int ftell(FILE * stream)
{
	DPRINTF("ftell:\n");
	return 0;
}

int ungetc(int c, FILE * stream)
{
	DPRINTF("ungetc:\n");
	return 0;
}

time_t time(time_t * timer)
{
	DPRINTF("time:\n");
	return NULL;
}

⌨️ 快捷键说明

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