1.7.9.uart stdio的移植1
朱老师笔记
1.7.9.1、什么是stdio
(1)#include <stdio.h>(2)stdio:standard input output,标准输入输出
(3)标准输入输出就是操作系统定义的默认的输入和输出通道。一般在PC机的情况下,标准输入指的是键盘,标准输出指的是屏幕。
(4)printf函数和scanf函数可以和底层输入/输出函数绑定,然后这两个函数就可以和stdio绑定起来。也就是说我们直接调用printf函数输出,内容就会被从标准输出输出出去。
(5)在我们这里,标准输出当然不是屏幕了,而是串口。标准输出也不是键盘,而是串口。
1.7.9.2、printf函数的工作原理
(1)printf函数工作时内部实际调用了2个关键函数:一个是vsprintf函数(主要功能是格式化打印信息,最终得到纯字符串格式的打印信息等待输出),另一个就是真正的输出函数putc(操控标准输出的硬件,将信息发送出去)
1.7.9.3、移植printf函数的三种思路
(1)我们希望在我们的开发板上使用printf函数进行(串口)输出,使用scanf函数进行(串口)输入,就像在PC机上用键盘和屏幕进行输入输出一样。因此需要移植printf函数/scanf函数
(2)我们说的移植而不是编写,我们不希望自己完全从新编写而是想尽量借用也有的代码(叫移植)
(3)一般移植printf函数可以有3个途径获取printf的实现源码:最原始最原本的来源就是linux内核中的printk。难度较大、关键是麻烦;稍微简单些的方法是从uboot中移植printf;更简单的方法就是直接使用别人移植好的。
(3)我们课程中使用第三种方法,别人移植好的printf函数来自于友善之臂的Tiny210的裸机教程中提供的。
1.7.9.4、移植好的printf介绍
参考视频中讲解。
《朱老师物联网大讲堂》学习笔记
学习地址:www.zhulaoshi.org
uart stdio的移植
什么是stdio?
就是标准输入输出,#include <stdio.h>
像我们PC,平时可能有好几个输入和输出,
不过一般默认输入是键盘,输出是屏幕。
printf函数和scanf函数可以和底层输入/输出函数绑定,
然后这两个函数就可以和stdio绑定起来,
在我们这里标准输入和输出都是串口。
printf函数工作时候实际调用了2个关键函数,
vsprintf,主要功能是格式化打印信息,最终得到纯字符串格式的打印信息等待输出。
putc,这个才是真正的输出函数,操控标准输出的硬件,将信息发送出去,
我们需要将putc函数与我们的串口输出函数绑定在一起。
移植方法
1.从linux源代码中去获取材料
2.从uboot中移植这些函数
3.使用别人已经移植好的
在这里我们使用的是第三种方式
这一节还没实际去做一些移植的工作,但是给了我们很多思考。
va_start(args, fmt);
len = vsprintf(g_pcOutBuf,fmt,args);
va_end(args);
for (i = 0; i < strlen(g_pcOutBuf); i++)
{
putc(g_pcOutBuf[i]);
}
比如下面引用的这段代码,是移植课程里面的,这个基本运算,和我自己想的理解差别好大,
并且我还要强调的一点是,我们移植过来的库函数,那些底层代码的实现,不是那么简单就写出来的,比如一些使用汇编实现的,
- /* More subroutines needed by GCC output code on some machines. */
- /* Compile this one with gcc. */
- /* Copyright (C) 1989, 92-98, 1999 Free Software Foundation, Inc.
- This file is part of GNU CC.
- GNU CC is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
- GNU CC is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with GNU CC; see the file COPYING. If not, write to
- the Free Software Foundation, 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA. */
- /* As a special exception, if you link this library with other files,
- some of which are compiled with GCC, to produce an executable,
- this library does not by itself cause the resulting executable
- to be covered by the GNU General Public License.
- This exception does not however invalidate any other reasons why
- the executable file might be covered by the GNU General Public License.
- */
- /* support functions required by the kernel. based on code from gcc-2.95.3 */
- /* I Molton 29/07/01 */
- #include "gcclib.h"
- #define umul_ppmm(xh, xl, a, b) \
- {register USItype __t0, __t1, __t2; \
- __asm__ ("%@ Inlined umul_ppmm \n\
- mov %2, %5, lsr #16 \n\
- mov %0, %6, lsr #16 \n\
- bic %3, %5, %2, lsl #16 \n\
- bic %4, %6, %0, lsl #16 \n\
- mul %1, %3, %4 \n\
- mul %4, %2, %4 \n\
- mul %3, %0, %3 \n\
- mul %0, %2, %0 \n\
- adds %3, %4, %3 \n\
- addcs %0, %0, #65536 \n\
- adds %1, %1, %3, lsl #16 \n\
- adc %0, %0, %3, lsr #16" \
- : "=&r" ((USItype) (xh)), \
- "=r" ((USItype) (xl)), \
- "=&r" (__t0), "=&r" (__t1), "=r" (__t2) \
- : "r" ((USItype) (a)), \
- "r" ((USItype) (b)));}
- #define __umulsidi3(u, v) \
- ({DIunion __w; \
- umul_ppmm (__w.s.high, __w.s.low, u, v); \
- __w.ll; })
- DItype
- __muldi3 (DItype u, DItype v)
- {
- DIunion w;
- DIunion uu, vv;
- uu.ll = u,
- vv.ll = v;
- w.ll = __umulsidi3 (uu.s.low, vv.s.low);
- w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
- + (USItype) uu.s.high * (USItype) vv.s.low);
- return w.ll;
- }