串口通讯驱动–基于STM32F10x
嵌入式开发中为了方便调试, 我们在开发过程中都会使用串口通讯, 本文是基于STM32平台写的串口通讯方法, 如果你有程序开发的经验, 那么你一定熟悉通过将信息打印到控制台来测试自己的程序运行结果, 或者通过从命令行输入来为函数传递参数, 串口通讯提供了类似的功能,通过配置串口通信来实现通过PC端的串口助手来打印设备运行信息。
本文提供一个回显程序, MCU将接收到的数据同步打印到串口助手上, 这是一个非常简单的程序, 但是足够测试串口通信的完整功能。包括基本的格式输入scanf, 和格式化输出print.
本文不是一篇介绍串口硬件设备的文章, 而是注重于在上位机上实现串口通讯本身, 也就是说注重于编程, 如果你是希望寻找一篇介绍串口协议的文章, 那么可以另寻高就。
这里默认读者熟悉C语言开发, 所以不会对语法知识有过多的说明, 函数的功能和实现方法均以注释的方式给出。
文章未经本人允许, 禁止转载。
文件组织
嵌入式开发受平台限制, 编写文件的时候需要考虑到它的可移植性, 出于这个考虑, 我将于硬件设备相关的信息全部放在一个头文件下, 命名为usart_std_periph_hardware_config.h
std_periph 后缀表面这是STM32产品的标志外设, hardware_config表面这里面存放硬件配置信息。另外提供两个文件usart_std_periph.h存放函数原型, usart_std_periph.c存放函数定义。
usart_std_periph_hardware_config.h
usart_std_periph.h
usart_std_periph.c
usart_std_periph_hardware_config.h
/********************************************************************************************
* File Name : usart_std_periph_hardware_config.h *
* *
* Programmer : Yang Long *
* *
* Last update : 25/02/2021 *
*------------------------------------------------------------------------------------------*
*Description: *
* This file provides some definitions for configuring the serial port *
* *
*------------------------------------------------------------------------------------------*
*Definition includs : *
* usart tx/rx ports and pins alias *
* usart gpio clk alias *
* usart device clock command function alias *
* usart gpio clock command function alias *
* usart interrupt handle alias *
*==========================================================================================*/
#ifndef __USART_HARDWARE_CONFIG_H__
#define __USART_HARDWARE_CONFIG_H__
#include "stm32f10x.h"
/********************************************************************************************
* Hardware configuration Reference *
*------------------------------------------------------------------------------------------*
* Pin Assignment *
* USART1 USART2 USART3 UART4 UART5 *
* TX PA9 PA2 PB10 PC10 PC12 *
* RX PA10 PA3 PB11 PC11 PD2 *
* *
*------------------------------------------------------------------------------------------*
* Periheral Architecture *
* USART1 USART2 USART3 UART4 UART5 *
* APBx APB2 APB1 APB1 APB1 APB1 *
* *
*------------------------------------------------------------------------------------------*
* Interrupt handle *
* USART1 USART2 USART3 UART4 UART5 *
* IRQn USART1_IRQn USART2_IRQn USART3_IRQn UART4_IRQn UART5_IRQn *
* Handle USART1_IRQ- USART1_IRQ- USART1_IRQ- USART1_IRQ- USART1_IRQ- *
* Handler Handler Handler Handler Handler *
*========================================================================================= */
/********************************************************************************************
* DEBUG_USART macro *
* In order to debug the program, a serial port for debugging is generally reserved, *
* Here called debug usart. You can modify the usart it uses by modifying the macro *
* definitnion here *
*==========================================================================================*/
#define DEBUG_USART_USE_USART1
//#define DEBUG_USART_USE_USART2
//#define DEBUG_USART_USE_USART3
//#define DEBUG_USART_USE_USART4
//#define DEBUG_USART_USE_USART5
/*========================================================================================= */
#ifdef DEBUG_USART_USE_USART1
#define DEBUG_USART USART1
#define DEBUG_USART_CLK USART1_CLK
#define DEBUG_USART_APBxPeriphClockCmd USART1_APBxPeriphClockCmd
#define DEBUG_USART_GPIO_CLK USART1_GPIO_CLK
#define DEBUG_USART_GPIO_APBxPeriphClockCmd USART1_GPIO_APBxPeriphClockCmd
#define DEBUG_USART_TX_GPIO_PORT USART1_TX_GPIO_PORT
#define DEBUG_USART_TX_GPIO_PIN USART1_TX_GPIO_PIN
#define DEBUG_USART_RX_GPIO_PORT USART1_RX_GPIO_PORT
#define DEBUG_USART_RX_GPIO_PIN USART1_RX_GPIO_PIN
#define DEBUG_USART_IRQ USART1_IRQn
#define DEBUG_USART_IRQHandler USART1_IRQHandler
#endif
#ifdef DEBUG_USART_USE_USART2
#define DEBUG_USART USART2
#define DEBUG_USART_CLK USART2_CLK
#define DEBUG_USART_APBxPeriphClockCmd USART2_APBxPeriphClockCmd
#define DEBUG_USART_GPIO_CLK USART2_GPIO_CLK
#define DEBUG_USART_GPIO_APBxPeriphClockCmd USART2_GPIO_APBxPeriphClockCmd
#define DEBUG_USART_TX_GPIO_PORT USART2_TX_GPIO_PORT
#define DEBUG_USART_TX_GPIO_PIN USART2_TX_GPIO_PIN
#define DEBUG_USART_RX_GPIO_PORT USART2_RX_GPIO_PORT
#define DEBUG_USART_RX_GPIO_PIN USART2_RX_GPIO_PIN
#define DEBUG_USART_IRQ USART2_IRQn
#define<