uart.h:
cpp
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_uart.h"
//RCC/GPIO/UART4章节初始化
void hal_uart4_init();
//发送一个字符函数
void hal_put_char(const char str);
//发送一个字符串函数
void hal_put_string(const char* string);
//接收一个字符函数
char hal_get_char();
//接收一个字符串函数
char* hal_get_string();
#endif
uart.c:
cpp
#include "uart4.h"
extern void delay_ms(int ms);
//RCC/GPIO/UART4章节初始化
void hal_uart4_init()
{
//RCC章节初始化
//1.使能GPIOB组控制器 MP_AHB4ENSETR[1] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//2.使能GPIOG组控制器 MP_AHB4ENSETR[6] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 6);
//3.使能UART4组控制器 MP_APB1ENSETR[16] = 1
RCC->MP_APB1ENSETR |= (0x1 << 16);
//GPIO章节初始化
//1.设置PB2引脚为复用功能 MODER[5:4] = 10
GPIOB->MODER &= (~(0x3 << 4));
GPIOB->MODER |= (0x1 << 5);
//2.设置PB2引脚复用功能为UART4_RX AFRL[11:8] = 1000
GPIOB->AFRL &= (~(0xf << 8));
GPIOB->AFRL |= (0x1 << 11);
//1.设置PG11引脚为复用功能 MODER[23:22] = 10
GPIOG->MODER &= (~(0x3 << 22));
GPIOG->MODER |= (0x1 << 23);
//2.设置PG11引脚复用功能为UART4_TX AFRH[15:12] = 0110
GPIOG->AFRH &= (~(0xf << 12));
GPIOG->AFRH |= (0x3 << 13);
//UART4章节初始化
//8N1 115200 使能
//0.设置UE=0 判断
if(USART4->CR1 & (0x1 << 0))
{
delay_ms(500);
//禁止UE位
USART4->CR1 &= (~(0x1 << 0));
}
//1.设置UART4串口1位起始位,8位数据位 CR1[28][12] = 00
USART4->CR1 &= (~(0x1 << 12));
USART4->CR1 &= (~(0x1 << 28));
//2.设置UART4串口没有校验位 CR1[10] = 0
USART4->CR1 &= (~(0x1 << 10));
//3.设置UART4串口1位停止位 CR2[13:12] = 00
USART4->CR2 &= (~(0x3 << 12));
//4.设置UART4串口16倍采样率 CR1[15] = 0
USART4->CR1 &= (~(0x1 << 15));
//5.设置UART4串口不分频 PRESC[3:0] = 0000
USART4->PRESC &= (~(0xf << 0));
//6.设置UART4串口波特率为115200 BRR = 0x22B
USART4->BRR |= 0x22B;
//7.设置UART4串口发送器使能 CR1[3] = 1
USART4->CR1 |= (0x1 << 3);
//8.设置UART4串口接收器使能 CR1[2] = 1
USART4->CR1 |= (0x1 << 2);
//9.设置UART4串口使能 CR1[0] = 1
USART4->CR1 |= (0x1 << 0);
}
//发送一个字符函数
void hal_put_char(const char str)
{
//1.判断发送数据寄存器是否为空 ISR[7]
//读0:发送数据寄存器满,需要等待
//读1:发送数据寄存器为空,可以发送数据
while(!(USART4->ISR & (0x1 << 7)));
//2.将要发送的数据,赋值给发送数据寄存器中
USART4->TDR = str;
//4.判断一帧数据是否发送完成 ISR[6]
//读0:一帧数据没有发送完成,需要等待
//读1:一帧数据发送完成
while(!(USART4->ISR & (0x1 << 6)));
}
//发送一个字符串函数
void hal_put_string(const char* string)
{
//判断是否为'\0'
//一个一个字符进行发送就可以
while(*string)
{
hal_put_char(*string++);
}
hal_put_char('\n');
hal_put_char('\r');
}
//接收一个字符函数
char hal_get_char()
{
char ch;
//判断接收数据寄存器中,是否接收到数据 ISR[5]
//读0:没有接收到数据,需要等待
//读1:接收到数据
while(!(USART4->ISR & (0x1 << 5)));
//将接收数据寄存器中内容,读出来
ch = USART4->RDR;
return ch;
}
char buffer[50] = {0};
//接收一个字符串函数
char* hal_get_string()
{
//循环
//enter = '\r'
unsigned int i;
for(i=0;i<49;i++)
{
//接收一个字符
buffer[i] = hal_get_char();
//发送一个字符 回显串口工具
hal_put_char(buffer[i]);
//判断键盘enter是否按下 enter = '\r'
if(buffer[i] == '\r')
break;
}
buffer[i] = '\0';
hal_put_char('\n');
return buffer;
}
main.c:
cpp
#include "uart4.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
int main()
{
hal_uart4_init();
hal_put_string("this is DC23051\n");
while(1)
{
// hal_put_char(hal_get_char()+1);
hal_put_string(hal_get_string());
}
return 0;
}