#ifndef __UART_H__
#define __UART_H__
#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_rcc.h"
#include"stm32mp1xx_uart.h"
void hal_uart_init();
void hal_put_char(const char str);
void hal_put_string(const char *string);
char hal_get_char();
char* hal_get_string();
#endif
#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_uart_init();
while(1)
{
hal_put_string(hal_get_string());
}
return 0;
}
#include"uart4.h"
void hal_uart_init()
{
RCC->MP_AHB4ENSETR|=(0x1<<1);
RCC->MP_AHB4ENSETR|=(0x1<<6);
RCC->MP_APB1ENSETR|=(0x1<<16);
GPIOB->MODER&=(~(0x3<<4));
GPIOB->MODER|=(0x2<<4);
GPIOB->AFRL&=(~(0xF<<8));
GPIOB->AFRL|=(0x8<<8);
GPIOG->MODER&=(~(0x3<<22));
GPIOG->MODER|=(0x2<<22);
GPIOG->AFRH&=(~(0xF<<12));
GPIOG->AFRH|=(0x6<<12);
USART4->CR1|=(0x1);
USART4->CR1|=(0x1<<2);
USART4->CR1|=(0x1<<3);
USART4->CR1&=(~(0x1<<10));
USART4->CR1&=(~(0x1<<15));
USART4->CR1&=(~(0x1<<12));
USART4->CR1|=(~(0x1<<28));
USART4->CR2&=(~(0x3<<12));
USART4->BRR&=0;
USART4->BRR|=(0x22B);
USART4->PRESC&=0;
}
void hal_put_char(const char str)
{
while(!(USART4->ISR&(0x1<<7)));
USART4->TDR&=0;
USART4->TDR=str;
while(!(USART4->ISR&(0x1<<6)));
}
void hal_put_string(const char *string)
{
const char *p=string;
while(*p!='\0'){
hal_put_char(*p);
p++;
}
hal_put_char('\n');
}
char hal_get_char()
{
char ch;
while(!(USART4->ISR&(0x1<<5)));
ch=USART4->RDR;
return ch;
}
char* hal_get_string()
{
static char buf[128];
for(int i=0;i<128;i++){
buf[i]=0;
}
int i=0;
while(1){
buf[i]=hal_get_char();
hal_put_char(buf[i]);
if(buf[i]=='\r'){
break;
}
i++;
}
hal_put_char('\0');
hal_put_char('\n');
return buf;
}