一、STM32串口硬件接收与发送框图
0.补充
- 串口是以ascii码字符(8位二进制数据)来通讯的
- 串口的物理线 数据是一位一位传输的
1.串口发送
- 数据写入发送数据寄存器TDR
- 数据从发送数据寄存器TDR逐步转入发送移位寄存器 ()
-------前:TXE=0-----------数据全部转入发送移位寄存器 --------后:TXE=1-------------- - 发送移位寄存器将每一帧数据逐步发送到引脚上
-------前:TC=0-----------最后一帧数据发送完 --------后:TC=1-------------- - 串口发送完成中断按照设置是否执行
2.串口接收
- 数据从引脚到接收移位寄存器
- 数据从接收移位寄存器到接收数据寄存器RDR
-------前:RXNE=0-----------数据全部转入接收数据寄存器RDR --------后:RXNE=1-------------- - 数据可以被读取,也可以产生接收中断
二、printf串口重定义(串口发送)
1.将syscall.c
文件在工程编译中排除
(因为此文件和我们即将要添加的文件相冲突)
2.添加两个文件到工程中
- 添加
retarget.h
文件到Core >> Inc中 - 添加
retarget.c
文件到Core >> Src中
/*
* retarget.c
*
* Created on: 2021年10月20日
* Author: Administrator
*/
#include <_ansi.h>
#include <_syslist.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/times.h>
#include <limits.h>
#include <signal.h>
#include <../Inc/retarget.h>
#include <stdint.h>
#include <stdio.h>
#if !defined(OS_USE_SEMIHOSTING)
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
UART_HandleTypeDef *gHuart;
void RetargetInit(UART_HandleTypeDef *huart) {
gHuart = huart;
/* Disable I/O buffering for STDOUT stream, so that
* chars are sent out as soon as they are printed. */
setvbuf(stdout, NULL, _IONBF, 0);
}
int _isatty(int fd) {
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
return 1;
errno = EBADF;
return 0;
}
int _write(int fd, char* ptr,