Stm32串口搭配DMA实现自定义printf、scanf

前言:本文仅供学习参考使用,主要目的是让大家快速使用串口调试,文章所提及的GCC适用于Clion,Vscode等第三方编辑器的用户。作者有时间会继续更新^_^

一、GCC环境

1、标准库

(1)、使用方法

在主函数while(1)初始化中,添加Serial_Init();

int main(void) {
   
    Serial_Init();

    while (1) {
   

    }
}

在代码目录下创建USART文件夹,新建syscalls.c,sysmem.c,usart.c,usart.h四个文件,工程目录结构如下:
在这里插入图片描述
在CMakeLists中添加如下代码:

# include_directories(Core/USART) 根据USART文件夹实际路径进行修改
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-u_printf_float")
add_link_options( -specs=nosys.specs -specs=nano.specs)

使用u_scanf,u_printf来替代scanf,printf函数,用法一致。

(2)代码部分

syscalls.c
/**
 ******************************************************************************
 * @file      syscalls.c
 * @author    Auto-generated by STM32CubeIDE
 * @brief     STM32CubeIDE Minimal System calls file
 *
 *            For more information about which c-functions
 *            need which of these lowlevel functions
 *            please consult the Newlib libc-manual
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020-2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes */
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>


/* Variables */
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));


char *__env[1] = {
    0 };
char **environ = __env;


/* Functions */
void initialise_monitor_handles()
{
   
}

int _getpid(void)
{
   
  return 1;
}

int _kill(int pid, int sig)
{
   
  (void)pid;
  (void)sig;
  errno = EINVAL;
  return -1;
}

void _exit (int status)
{
   
  _kill(status, -1);
  while (1) {
   }    /* Make sure we hang here */
}

__attribute__((weak)) int _read(int file, char *ptr, int len)
{
   
  (void)file;
  int DataIdx;

  for (DataIdx = 0; DataIdx < len; DataIdx++)
  {
   
    *ptr++ = __io_getchar();
  }

  return len;
}

__attribute__((weak)) int _write(int file, char *ptr, int len)
{
   
  (void)file;
  int DataIdx;

  for (DataIdx = 0; DataIdx < len; DataIdx++)
  {
   
    __io_putchar(*ptr++);
  }
  return len;
}

int _close(int file)
{
   
  (void)file;
  return -1;
}


int _fstat(int file, struct stat *st)
{
   
  (void)file;
  st->st_mode = S_IFCHR;
  return 0;
}

int _isatty(int file)
{
   
  (void)file;
  return 1;
}

int _lseek(int file, int ptr, int dir)
{
   
  (void)file;
  (void)ptr;
  (void)dir;
  return 0;
}

int _open(char *path, int flags, ...)
{
   
  (void)path;
  (void)flags;
  /* Pretend like we always fail */
  return -1;
}

int _wait(int *status)
{
   
  (void)status;
  errno = ECHILD;
  return -1;
}

int _unlink(char *name)
{
   
  (void)name;
  errno = ENOENT;
  return -1;
}

int _times(struct tms *buf)
{
   
  (void)buf;
  return -1;
}

int _stat(char *file, struct stat *st)
{
   
  (void
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kk_阿白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值