| 本帖最后由 liam_lee 于 2013-1-4 09:29 编辑 
 
 CoIDE 1.6.2下 Newlib-nano printf 重定向到UART 
                                                                 ---------CooCox xinyun 
 下载安装CoIDE1.6.2: http://www.coocox.org/Tools/CoIDE-1.6.2.exe
 
 并用附件中的文件,替换CoIDE安装目录下/plugins/org.coocox.builder.ui_1.0.0.201212141638.jar
 
 
 下载 GNU Tools for ARM Embedded Processors  Version: 4.7
 下载地址是:
 https://launchpad.net/gcc-arm-embedded/+milestone/4.7-2012-q4-major
 
 CoIDE中设置工具链,这里不详细说明
 
 这里以Cookie板子为例,说明CoIDE下 Newlib-nano printf 重定向到UART过程,一共分为三步
 1.新建工程,并添加printf语句
 2.选择Newlib-nano,支持打印浮点数
 3.重定向printf
 
 1.新建工程,并添加printf语句
 
 
 
 新建一个M051的工程,如何新建工程这里不详细说明
 勾选如下组件
 
  
  
 在UART组件中添加cookie_printf例子到工程
 
  
 打开cookie_printf.c文件,添加 #include <stdio.h>这个头文件
 并在while(1)循环中加上
 
 printf("Nano Clib test:int=%d   float=%f\r\n",255,123.456);
 
 
 2.选择Newlib-nano,支持打印浮点数
 
 
 
 
 点击配置按钮进入配置:
 
  
 进入link标签页
 在Linked Library中添加
 D:\Program Files\GNU Tools ARM Embedded\4.7 2012q4\arm-none-eabi\lib\armv6-m\libc_s.a
 D:\Program Files\GNU Tools ARM Embedded\4.7 2012q4\arm-none-eabi\lib\armv6-m\libg_s.a
 
  
 注意:用于Cookie是M0的芯片所以选armv6-m下面的库,如果使用M3则选armv7-m,M4则选armv7e-m
 
 | ARM Core 
 | multilib 
 |  | Cortex-M0+ 
 | armv6-m 
 |  | Cortex-M0 
 | armv6-m 
 |  | Cortex-M1 
 | armv6-m 
 |  | Cortex-M3 
 | armv7-m 
 |  | Cortex-M4 
 | armv7e-m 
 | 
 
 
 
 
 如果需要支持浮点数打印,则在Misc Consle中添加 -u;_printf_float;(注意,标准命令是 -u _printf_float,CoIDE的原因,不得已而为之)
 
  
 
 3.重定向printf
 
 
 打开syscalls.c,编写如下代码
 
 
 /**************************************************************************//*****
 * @file     stdio.c
 * @brief    Implementation of newlib syscall
 ********************************************************************************/
 
 #include <stdio.h>
 #include <stdarg.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdarg.h>
 
 #include "xhw_types.h"
 #include "xhw_ints.h"
 #include "xhw_memmap.h"
 #include "xuart.h"
 
 #undef errno
 extern int errno;
 extern int  _end;
 
 caddr_t _sbrk ( int incr )
 {
 static unsigned char *heap = NULL;
 unsigned char *prev_heap;
 
 if (heap == NULL) {
 heap = (unsigned char *)&_end;
 }
 prev_heap = heap;
 
 heap += incr;
 
 return (caddr_t) prev_heap;
 }
 
 int link(char *old, char *new) {
 return -1;
 }
 
 int _close(int file)
 {
 return -1;
 }
 
 int _fstat(int file, struct stat *st)
 {
 st->st_mode = S_IFCHR;
 return 0;
 }
 
 int _isatty(int file)
 {
 return 1;
 }
 
 int _lseek(int file, int ptr, int dir)
 {
 return 0;
 }
 
 int _read(int file, char *ptr, int len)
 {
 return 0;
 }
 
 int _write(int file, char *ptr, int len) {
 int txCount;
 
 (void) file;
 
 for (txCount = 0; txCount < len; txCount++)
 {
 xUARTCharPut(xUART0_BASE,*ptr++);
 }
 
 return len;
 }
 
 void abort(void)
 {
 /* Abort called */
 while(1);
 }
 
 /* --------------------------------- End Of File ------------------------------ */
 
 
 用ColinkEx下载就可以看到串口打印了。
 
 
  
 Newlib-nano 的好处,看看size就知道
 不使用浮点数:
 
  
 使用浮点数
 
  
 |