实验一
键盘输入一个字符'a',串口工具显示'b'
uart4.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"
//初始化UART4
void hal_uart4_init();
//发送一个字符
void hal_put_char(const char str);
//发送一个字符串
void hal_put_string(const char * str);
//接收一个字符
char hal_get_char();
//接收一个字符串
char* hal_get_string();
#endif
uart4.c
#include "uart4.h"
//初始化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.设置 FB2 引脚为复用模式 MODER[5:4] = 10
GPIOB->MODER &= (~(0X3 << 4));
GPIOB->MODER |= (0x2 << 4);
//2.设置 FB2 引脚复用为 UART4_RX AFRL[11:8] = 1000
GPIOB->AFRL &= (~(0xf << 8));
GPIOB->AFRL |= (0x8 << 8);
//1.设置 FG11 引脚为复用模式 MODER[23:22] = 10
GPIOG->MODER &= (~(0X3 << 22));
GPIOG->MODER |= (0X2 << 22);
//2.设置 FG11 引脚复用为 UART4_TX AFRL[15:12] = 1000
GPIOG->AFRL &= (~(0XF << 12));
GPIOG->AFRL |= (0X6 << 12);
/***********UART4*************/
//0.设置串口UE = 0
USART4->CR1 &= (~0x1);
//1.设置1位起始位,8位数据位
USART4->CR1 &= (~(0x1 << 28));
USART4->CR1 &= (~(0x1 << 12));
//2.没有奇偶校验
USART4->CR1 &= (~(0x1 << 10));
//3.设置1位停止位
USART4->CR2 &= (~(0x3 << 12));
//4.设置16倍采样位
USART4->CR1 &= (~(0x1 << 15));
//5.设置串口不分频
USART4->PRESC &= (~0xF);
//6.设置串口波特率为115200
USART4->BRR &= (~0xFFFF);
USART4->BRR |= (0x22B);
//7.设置串口发送器使能
USART4->CR1 |= (0x1 << 3);
//8.设置串口接收器使能
USART4->CR1 |= (0x1 << 2);
//9.设置串口使能
USART4->CR1 |= 0x1;
}
//发送一个字符
void hal_put_char(const char str)
{
//1.判断是否为空 ISR[7]
//0为满(堵塞),1为空(发送)
while(!(USART4->ISR & (0x1 << 7)));
//2.将要发送内容赋值寄存器
USART4->TDR = str;
//3.判断一帧数据是否发送完成ISR[6]
//0未完成,1已完成
while(!(USART4->ISR & (0x1 << 6)));
}
//接收一个字符
char hal_get_char()
{
//1.判断接收是否有数据ISR[5]
while(!(USART4->ISR & (0x1 << 5)));
//2.将数据读取
return USART4->RDR;
}
main.c
#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();
while(1)
{
char arr = hal_get_char();
hal_put_char(arr+1);
}
return 0;
}
实现二
键盘输入一个字符串,串口工具回显输入的字符串
uart4.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"
//初始化UART4
void hal_uart4_init();
//发送一个字符
void hal_put_char(const char str);
//发送一个字符串
void hal_put_string(const char * str);
//接收一个字符
char hal_get_char();
//接收一个字符串
char* hal_get_string();
#endif
uart4.c
#include "uart4.h"
//初始化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.设置 FB2 引脚为复用模式 MODER[5:4] = 10
GPIOB->MODER &= (~(0X3 << 4));
GPIOB->MODER |= (0x2 << 4);
//2.设置 FB2 引脚复用为 UART4_RX AFRL[11:8] = 1000
GPIOB->AFRL &= (~(0xf << 8));
GPIOB->AFRL |= (0x8 << 8);
//1.设置 FG11 引脚为复用模式 MODER[23:22] = 10
GPIOG->MODER &= (~(0X3 << 22));
GPIOG->MODER |= (0X2 << 22);
//2.设置 FG11 引脚复用为 UART4_TX AFRL[15:12] = 1000
GPIOG->AFRL &= (~(0XF << 12));
GPIOG->AFRL |= (0X6 << 12);
/***********UART4*************/
//0.设置串口UE = 0
USART4->CR1 &= (~0x1);
//1.设置1位起始位,8位数据位
USART4->CR1 &= (~(0x1 << 28));
USART4->CR1 &= (~(0x1 << 12));
//2.没有奇偶校验
USART4->CR1 &= (~(0x1 << 10));
//3.设置1位停止位
USART4->CR2 &= (~(0x3 << 12));
//4.设置16倍采样位
USART4->CR1 &= (~(0x1 << 15));
//5.设置串口不分频
USART4->PRESC &= (~0xF);
//6.设置串口波特率为115200
USART4->BRR &= (~0xFFFF);
USART4->BRR |= (0x22B);
//7.设置串口发送器使能
USART4->CR1 |= (0x1 << 3);
//8.设置串口接收器使能
USART4->CR1 |= (0x1 << 2);
//9.设置串口使能
USART4->CR1 |= 0x1;
}
//发送一个字符
void hal_put_char(const char str)
{
//1.判断是否为空 ISR[7]
//0为满(堵塞),1为空(发送)
while(!(USART4->ISR & (0x1 << 7)));
//2.将要发送内容赋值寄存器
USART4->TDR = str;
//3.判断一帧数据是否发送完成ISR[6]
//0未完成,1已完成
while(!(USART4->ISR & (0x1 << 6)));
}
//发送一个字符串
void hal_put_string(const char* str)
{
//判断是否为‘\0'
//一个一个字符发送
int i=0;
while(*(str+i) != '\0')
{
hal_put_char(*(str+i));
i++;
}
}
//接收一个字符
char hal_get_char()
{
//1.判断接收是否有数据ISR[5]
while(!(USART4->ISR & (0x1 << 5)));
//2.将数据读取
return USART4->RDR;
}
//接收一个字符串
char* hal_get_string()
{
//循环接收
char* arr='\0';
int i=0;
while(1)
{
//'\n'回车表示接收完成
*(arr+i) = hal_get_char();
hal_put_char(*(arr+i));
if(*(arr+i) != '\n') break;
i++;
}
return arr;
}
main.c
#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();
while(1)
{
char* arr = hal_get_string();
hal_put_string(arr);
}
return 0;
}