首页 > 编程语言 >Keil(C/C++混编)printf问题

Keil(C/C++混编)printf问题

时间:2023-12-05 17:44:30浏览次数:44  
标签:return Keil UART C++ char int file printf buf

C++不支持Use MicroLIB,解决Keil的printf问题,现象:Debug下,点击三次run可以运行,烧入芯片独立运行死机

#ifdef __cplusplus
extern "C" {
#endif
 
#define DEFAULT_HANDLE 0x100;
 
/*
 * UART prototype functions
 */
char UART_read(void)
{
  return 0;
}
void UART_write(char ch)
{
    uint16_t temp = usart2_buf.tx_rear;
    usart2_buf.tx_buf[temp] = ch;
    temp = ((temp)>=BASE_BUFF_SIZE-1) ? 0 : (temp)+1;
    usart2_buf.tx_rear = temp;
}
 
 
/*
 * These names are special strings which will be recognized by 
 * _sys_open and will cause it to return the standard I/O handles, instead
 * of opening a real file.
 */
const char __stdin_name[] ="STDIN";
const char __stdout_name[]="STDOUT";
const char __stderr_name[]="STDERR";
 
/*
 * Open a file. May return -1 if the file failed to open. We do not require
 * this function to do anything. Simply return a dummy handle.
 */
FILEHANDLE _sys_open(const char * name, int openmode)
{
    return DEFAULT_HANDLE;  
}
 
/*
 * Close a file. Should return 0 on success or a negative value on error.
 * Not required in this implementation. Always return success.
 */
int _sys_close(FILEHANDLE fh)
{
    return 0; //return success
}
 
/*
 * Write to a file. Returns 0 on success, negative on error, and the number
 * of characters _not_ written on partial success. This implementation sends
 * a buffer of size 'len' to the UART.
 */
int _sys_write(FILEHANDLE fh, const unsigned char * buf,
               unsigned len, int mode)
{
    int i;
    taskENTER_CRITICAL();
    for(i=0;i<len;i++) 
    {
        UART_write(buf[i]);
    
        // Fix for HyperTerminal    
        if(buf[i]=='\n') UART_write('\r');
    }
    taskEXIT_CRITICAL();
    SET_BIT(USART1->CR1,1U<<7);//使能串口的发送缓冲区空中断
    return 0;   
}
 
/*
 * Read from a file. Can return:
 *  - zero if the read was completely successful
 *  - the number of bytes _not_ read, if the read was partially successful
 *  - the number of bytes not read, plus the top bit set (0x80000000), if
 *    the read was partially successful due to end of file
 *  - -1 if some error other than EOF occurred
 * This function receives a character from the UART, processes the character
 * if required (backspace) and then echo the character to the Terminal 
 * Emulator, printing the correct sequence after successive keystrokes.  
 */
int _sys_read(FILEHANDLE fh, unsigned char * buf,
              unsigned len, int mode)
{
    int pos=0;
    
    do {
        
    
        buf[pos]=UART_read();
        
        // Advance position in buffer
        pos++;
        
        // Handle backspace
        if(buf[pos-1] == '\b') 
        {
            // More than 1 char in buffer
            if(pos>1)
            {
                // Delete character on terminal
                UART_write('\b');
                UART_write(' ');
                UART_write('\b');         
                
                // Update position in buffer
                pos-=2;   
            }
            else if (pos>0) pos--; // Backspace pressed, empty buffer
        }
        else UART_write(buf[pos-1]); // Echo normal char to terminal
        
        
    }while(buf[pos-1] != '\r');
    
    buf[pos]= '\0'; // Ensure Null termination
 
    return 0;       
}
 
/*
 * Writes a character to the output channel. This function is used
 * for last-resort error message output.
 */
void _ttywrch(int ch)
{
    // Convert correctly for endianness change
    char ench=ch;
    
    UART_write(ench);
}
 
/*
 * Return non-zero if the argument file is connected to a terminal.
 */
int _sys_istty(FILEHANDLE fh)
{
    return 1; // no interactive device present
}
 
/*
 * Move the file position to a given offset from the file start.
 * Returns >=0 on success, <0 on failure. Seeking is not supported for the 
 * UART.
 */
int _sys_seek(FILEHANDLE fh, long pos)
{
    return -1; // error
}
 
/*
 * Flush any OS buffers associated with fh, ensuring that the file
 * is up to date on disk. Result is >=0 if OK, negative for an
 * error.
 */
int _sys_ensure(FILEHANDLE fh)
{
    return 0; // success
}
 
/*
 * Return the current length of a file, or <0 if an error occurred.
 * _sys_flen is allowed to reposition the file pointer (so Unix can
 * implement it with a single lseek, for example), since it is only
 * called when processing SEEK_END relative fseeks, and therefore a
 * call to _sys_flen is always followed by a call to _sys_seek.
 */
long _sys_flen(FILEHANDLE fh)
{
    return 0;
}
 
/*
 * Return the name for temporary file number sig in the buffer
 * name. Returns 0 on failure. maxlen is the maximum name length
 * allowed.
 */
//int _sys_tmpnam(char * name, int sig, unsigned maxlen)
//{
//    return 0; // fail, not supported
//}
// 
/*
 * Terminate the program, passing a return code back to the user.
 * This function may not return.
 */
void _sys_exit(int returncode)
{
    while(1) {};
}
 
#ifdef __cplusplus
}
#endif

 

标签:return,Keil,UART,C++,char,int,file,printf,buf
From: https://www.cnblogs.com/boring-luobo/p/17877795.html

相关文章

  • C++运行期多态和编译期多态(以不同的模板参数调用不同的函数)
    在面向对象C++编程中,多态是OO三大特性之一,这种多态称为运行期多态,也称为动态多态;在泛型编程中,多态基于template(模板)的具现化与函数的重载解析,这种多态在编译期进行,因此称为编译期多态或静态多态。<h1"="">1运行期多态运行期多态的设计思想要归结到类继承体系的设计上去。对......
  • C++11、C++14、C++17、C++20新特性总结(5万字详解)(转载)
    文章目录C++11是什么,C++11标准的由来C++auto类型推导完全攻略auto类型推导的语法和规则auto的高级用法auto的限制auto的应用使用auto定义迭代器auto用于泛型编程C++decltype类型推导完全攻略exp注意事项decltype推导规则decltype的实际应用汇总auto和......
  • c++ json的解析和QT中json的操作
    1.下载jsoncpp源码2.首先建议jsoncpp源码编译成动态库https://www.bilibili.com/video/BV1pb4y1W7ZZhttps://www.bilibili.com/video/BV1Ra4y1e7gL (1)用QT的Cmake工具 (2)用Visualstudio a.工具打开jsoncpp源码,在CMakeLists.txt右键->jsoncpp的CMak......
  • C/C++ 实现枚举网上邻居信息
    在Windows系统中,通过网络邻居可以方便地查看本地网络中的共享资源和计算机。通过使用WindowsAPI中的一些网络相关函数,我们可以实现枚举网络邻居信息的功能,获取连接到本地网络的其他计算机的相关信息。本文将介绍一个简单的C++程序,使用WindowsAPI枚举网络邻居信息,并获取对端名称......
  • c++回调函数
    最近用到了回调函数,距离上次使用至少隔了5年了,又重新熟悉了一下。  转自:https://blog.csdn.net/hua12134/article/details/88264250什么是函数指针函数指针就是指向函数的指针,指向某种特定的类型。函数的类型由它的返回类型和形参类型共同决定,与函数名无关,例如:boollength......
  • c++传参时 值传递和引用传递的区别
    值传递需要开辟存储空间并拷贝这个对象,引用传递只是给这个对象起了一个别名,不涉及开辟空间和拷贝操作,引用传递更高效值传递在调用函数中修改的不是原来的对象,而引用传递在调用函数中就能直接修改原来的对象举个例子体会一下:435.无重叠区间贪心算法求解,涉及到排序操作,排序函数......
  • C/C++ 原生套接字抓取FTP数据包
    网络通信在今天的信息时代中扮演着至关重要的角色,而对网络数据包进行捕获与分析则是网络管理、网络安全等领域中不可或缺的一项技术。本文将深入介绍基于原始套接字的网络数据包捕获与分析工具,通过实时监控网络流量,实现抓取流量包内的FTP通信数据,并深入了解数据传输的细节,捕捉潜在......
  • C++:如何将 LLVM 嵌套到你的项目中去
    IDE:ClionLLVMcmake_minimum_required(VERSION3.9)project(clang_demo)find_package(LLVMREQUIREDCONFIG)message(STATUS"FoundLLVM${LLVM_PACKAGE_VERSION}")message(STATUS"UsingLLVMConfig.cmakein:${LLVM_DIR}")......
  • C++_线程池代码看C++类-模板-标准库
    C++线程池线程池的组成部分:线程池管理器(ThreadPoolManager):用于创建并管理线程池工作线程(WorkThread):线程池中线程任务接口(Task):每个任务必须实现的接口,以供工作线程调度任务的执行。任务队列:用于存放没有处理的任务。提供一种缓冲机制。 通过新......
  • 一. C++基础
    文章参考:《C++面向对象程序设计》✍千处细节、万字总结(建议收藏)_白鳯的博客-CSDN博客1.一个简单的案例#include<iostream>//编译预处理命令usingnamespacestd;//使用命名空间intadd(inta,intb);//函数原型说明intmain()//主函数{ intx,y;......