#include "uart4.h"
//UART
//初始化
void hal_uart4_init()
{
//rcc_init
//使能GPIOB组控制器[1]=1
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//使能GPIOG组控制器[6]=1
RCC->MP_AHB4ENSETR |= (0x1 << 6);
//使能uart组控制器[16]=1
RCC->MP_AHB4ENSETR |=(0x1 << 16);
//gpio_init
GPIOB->MODER &= (~(0x2 << 5));
GPIOB->MODER |= (0x1 << 5);
GPIOG->MODER &= (~(0x2 << 23));
GPIOG->MODER |= (0x1 << 23);
GPIOB->AFRL &= (~(0x8 << 11));
GPIOB->AFRL |= (0x1 << 11);
GPIOB->AFRH &= (~(0x6 << 15));
GPIOB->AFRH |= (0x6 << 14);
//uart_init
USART4->CR1 &=(~(0x1 << 28));
USART4->CR1 &=(~(0x1 << 12));
USART4->CR1 &=(~(0x1 << 15));
USART4->CR1 &=(~(0x1 << 10));
USART4->CR1 |=(0x1 << 3);
USART4->CR1 |=(0x1 << 2) ;
USART4->CR1 |= 0x1;
USART4->CR1 &=(~(0x1 << 13));
USART4->CR1 &=(~(0x1 << 12));
USART4->PRESC &= ~(0x7);
USART4->BRR=0x22b;
}
//发送一个字符
void hal_put_char(const char str)
{
while (!(USART4->ISR &(1<<7)));
USART4->TDR=str;
if(str=='\n'){
hal_put_char('\r');
}
}
//接受一个字符
char hal_get_char()
{
char data;
while(!(USART4->ISR &(1<<5)));
data=(char)USART4->RDR;
return data;
}
// //发送一个字符串
// void hal_put_string(const char* string)
// {
// while(*string !='\0'){
// hal_put_string(*string);
// string++;
// }
// }
// //接一个字符串
// char* hal_get_string()
// {
// int i=0;
// char buff[128];
// for(;i<49;i++){
// buff[i]=hal_recvchar();
// hal_put_string(buff[i]);
// if (buff[i] == '\r')//代表串口调试助手的enter,串口调试助手没有\0
// {
// break;
// }
// }
// buff[i]='\0';
// hal_put_string('\n');
// return buff;
// }
#include "uart4.h"
int main()
{
hal_uart4_init();
//hal_put_string("uart4 test");
while(1)
{
hal_put_char(hal_get_char()+1);
//hal_put_string(hal_get_string());
}
}
#ifndef __UART_H__
#define __UART_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"
void hal_uart4_init();
void hal_put_char(const char str);
char hal_get_char();
void hal_put_string(const char* string);
char* hal_get_string();
#endif