#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_APB1ENSETR |=(0x1 << 16); //gpio_init GPIOB->MODER &= (~(0x3 << 4));GPIOB->MODER |= (0x1 << 5);GPIOG->MODER &= (~(0x3 << 22));GPIOG->MODER |= (0x1 << 23);GPIOB->AFRL &= (~(0xf << 8));GPIOB->AFRL |= (0x1 << 11);GPIOG->AFRH &= (~(0xf << 12));GPIOG->AFRH |= (0x3 << 13);//uart_initif(USART4->CR1 & 0x1){USART4->CR1 &= (~(0x1 << 0));}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->CR2 &=(~(0x1 << 12));USART4->PRESC &= (~(0xf<<0)); 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[50];for(;i<49;i++){buff[i]=hal_get_char();hal_put_char(buff[i]);if (buff[i] == '\r'){break;}}buff[i]='\0';hal_put_char('\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