Retargetting a C Library Function

摘自:http://www.doulos.com/knowhow/arm/Retargetting_a_C_library_function/

Retargetting a C Library Function


Overview

Following on from (Getting started with Cortex-M3 CMSIS programming »). In this tutorial we will look at printing messages on a text console.

Background Information

In a typical C application messages are printed to onto the computer screen via printf(). However, in the case of an embedded device, the printf() functionality would have to be altered to redirect the characters from a computer screen to a console. Since there is no true console attached, STDOUT will be emulated using Cortex-M3 ITM (Instrumentation Trace Microcell). ITM is output-only, so STDIN will be emulated by a special protocol defined by CMSIS and hardwired into µVision. The main advantage of STDIN/STDOUT emulation is that it works in exactly the same way in the simulator as on every Cortex-M3 device. 

To get the best out of this tutorial you may decide to download the following support files. In exchange, we will ask you to enter some personal details. On the registration form, you will be asked whether you want us to send you further information concerning other Doulos products and services in the subject area concerned.

Download the files for this tutorial »
Download the required software »

Actions

  • Invoke μVision and navigate to the \intro\session1 directory: File > Open.

  • Open the provided project file called session1.uproj.

  • Ensure that project session1 is active (highlighted). Otherwise right-click the project name and select: Set as Active Project.

Starting from the files provided for our previous tutorial, we have now added a printf() statement inside the loop to print the value of the variable <span class="snippet" i<="" span="" style="margin: 0px; padding: 0px; display: inline; color: rgb(255, 51, 0); font-family: 'Courier New', Monaco, monospace; background-repeat: no-repeat no-repeat; ">.

int main(void) {
	int j = 0;
	unsigned char i =0;

	while (1) {
		j++;
		if (j==1000000) {
			j = 0;
			if (i==0xFFFF) i = 0;
			printf("Value of i: %d\n", i);
			i++;
		}
	}
}

The provided file \intro\common\retarget.c has been added inside the User group. This file redefines functions used byprintf() for outputting characters. The printf() function ultimately relies on the fputc() function to operate. The fputc()has been implemented using the CMSIS standard function ITM_SendChar().

int fputc(int ch, FILE *f) {
    return ITM_SendChar(ch);
}

In contrast the fgetc() function relies on the CMSIS standard function ITM_ReceiveChar().

int fgetc(FILE *f)
{
    int r;
    
    /* if we just backspaced, then return the backspaced character */
    /* otherwise output the next character in the stream */
    if (backspace_called == 1)
    {
      backspace_called = 0;
    }
    else {
        do {
            r = ITM_ReceiveChar();
        } while (r == -1);
        last_char_read = (unsigned char)r;
#ifdef ECHO_FGETC
        ITM_SendChar(r);
#endif
    }
    return last_char_read;
}

The declaration of the ITM_SendChar() can be found inside the core_cm3.h file and is as follows

static __INLINE uint32_t ITM_SendChar (uint32_t ch)

To complete the separation from the standard I/O library we also have had to redefine __stdout and __stdin. These can be found inside the retarget.c file below the declaration of the __FILE structure.

FILE __stdin;
FILE __stdout;
  • You will have to rebuild the project and re-invoke the debugger (see previous tutorial)..

  • In order to watch the program output we will need a text console. If the Debug (printf) Viewer is not visible, it can be activated via the menu: View > Serial Windows > Debug (printf) Viewer

  • Execute the program and observe the message printed on the terminal

This concludes this second part of the series. In the final tutorial » we will look at proving that our program is working adequately by executing it on a hardware evaluation board.


http://www.codeforge.cn/read/37914/Retarget.c__html

/*----------------------------------------------------------------------------
 * Name:    Retarget.c
 * Purpose: 'Retarget' layer for target-dependent low level functions
 * Version: V1.0
 *----------------------------------------------------------------------------
 * This file is part of the uVision/ARM development tools.
 * This software may only be used under the terms of a valid, current,
 * end user licence from KEIL for a compatible version of KEIL software
 * development tools. Nothing else gives you the right to use this software.
 *
 * Copyright (c) 2005-2007 Keil Software. All rights reserved.
 *----------------------------------------------------------------------------*/

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

#pragma import(__use_no_semihosting_swi)


/*----------------------------------------------------------------------------
  external functions
 *----------------------------------------------------------------------------*/
extern int  SendChar(int ch);
extern int  GetKey(void);


struct __FILE {
  int handle;                 // Add whatever you need here 
};
FILE __stdout;
FILE __stdin;


/*----------------------------------------------------------------------------
  fputc
 *----------------------------------------------------------------------------*/
int fputc(int ch, FILE *f) {
  return (SendChar(ch));
}

/*----------------------------------------------------------------------------
  fgetc
 *----------------------------------------------------------------------------*/
int fgetc(FILE *f) {
  return (SendChar(GetKey()));
}

/*----------------------------------------------------------------------------
  _ttywrch
 *----------------------------------------------------------------------------*/
void _ttywrch(int ch) {
 SendChar (ch);
}

/*----------------------------------------------------------------------------
  ferror
 *----------------------------------------------------------------------------*/
int ferror(FILE *f) {
                              // Your implementation of ferror
  return EOF;
}

/*----------------------------------------------------------------------------
  _sys_exit
 *----------------------------------------------------------------------------*/
void _sys_exit(int return_code) {
label:  goto label;           // endless loop
}
 ...

http://www.cnblogs.com/liu_xf/archive/2011/04/14/2015726.html

#include <stdio.h>
#include
<stdarg.h>

/*********************************************************/
//向串口usart0发送一个字节函数
void Uart0_putchar( unsigned char sdbyte)
{
UDR0
=sdbyte;
while(!(UCSR0A&0x40));
UCSR0A
|=0x40;
}


////
void Uart0_printf(char *str,...)
{
char buf[128];
unsigned
char i = 0;
va_list ptr;
va_start(ptr,str);
vsprintf(buf,str,ptr);
while(buf[i])
{
Uart0_putchar(buf[i]);
i
++;
}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值