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]);
}

比如下面引用的这段代码,是移植课程里面的,这个基本运算,和我自己想的理解差别好大,

并且我还要强调的一点是,我们移植过来的库函数,那些底层代码的实现,不是那么简单就写出来的,比如一些使用汇编实现的,


[cpp]  view plain  copy
  1. /* More subroutines needed by GCC output code on some machines.  */  
  2. /* Compile this one with gcc.  */  
  3. /* Copyright (C) 1989, 92-98, 1999 Free Software Foundation, Inc. 
  4.  
  5. This file is part of GNU CC. 
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify 
  8. it under the terms of the GNU General Public License as published by 
  9. the Free Software Foundation; either version 2, or (at your option) 
  10. any later version. 
  11.  
  12. GNU CC is distributed in the hope that it will be useful, 
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  15. GNU General Public License for more details. 
  16.  
  17. You should have received a copy of the GNU General Public License 
  18. along with GNU CC; see the file COPYING.  If not, write to 
  19. the Free Software Foundation, 59 Temple Place - Suite 330, 
  20. Boston, MA 02111-1307, USA.  */  
  21.   
  22. /* As a special exception, if you link this library with other files, 
  23.    some of which are compiled with GCC, to produce an executable, 
  24.    this library does not by itself cause the resulting executable 
  25.    to be covered by the GNU General Public License. 
  26.    This exception does not however invalidate any other reasons why 
  27.    the executable file might be covered by the GNU General Public License. 
  28.  */  
  29. /* support functions required by the kernel. based on code from gcc-2.95.3 */  
  30. /* I Molton     29/07/01 */  
  31.   
  32. #include "gcclib.h"  
  33.   
  34. #define umul_ppmm(xh, xl, a, b) \  
  35. {register USItype __t0, __t1, __t2;                                     \  
  36.   __asm__ ("%@ Inlined umul_ppmm                    \n\  
  37.         mov     %2, %5, lsr #16                     \n\  
  38.         mov     %0, %6, lsr #16                     \n\  
  39.         bic     %3, %5, %2, lsl #16                 \n\  
  40.         bic     %4, %6, %0, lsl #16                 \n\  
  41.         mul     %1, %3, %4                      \n\  
  42.         mul     %4, %2, %4                      \n\  
  43.         mul     %3, %0, %3                      \n\  
  44.         mul     %0, %2, %0                      \n\  
  45.         adds    %3, %4, %3                      \n\  
  46.         addcs   %0, %0, #65536                      \n\  
  47.         adds    %1, %1, %3, lsl #16                 \n\  
  48.         adc     %0, %0, %3, lsr #16"                                    \  
  49.            : "=&r" ((USItype) (xh)),                                    \  
  50.              "=r" ((USItype) (xl)),                                     \  
  51.              "=&r" (__t0), "=&r" (__t1), "=r" (__t2)                    \  
  52.            : "r" ((USItype) (a)),                                       \  
  53.              "r" ((USItype) (b)));}  
  54.   
  55.   
  56. #define __umulsidi3(u, v) \  
  57.   ({DIunion __w;                                                        \  
  58.     umul_ppmm (__w.s.high, __w.s.low, u, v);                            \  
  59.     __w.ll; })  
  60.   
  61.   
  62. DItype  
  63. __muldi3 (DItype u, DItype v)  
  64. {  
  65.   DIunion w;  
  66.   DIunion uu, vv;  
  67.   
  68.   uu.ll = u,  
  69.   vv.ll = v;  
  70.   
  71.   w.ll = __umulsidi3 (uu.s.low, vv.s.low);  
  72.   w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high  
  73.                + (USItype) uu.s.high * (USItype) vv.s.low);  
  74.   
  75.   return w.ll;  
  76. }  












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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值