arm_uart4实验

cs 复制代码
#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_init
        if(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;
                                   
}                                        
cs 复制代码
#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());
    }
}
cs 复制代码
#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
相关推荐
陌上花开缓缓归以9 小时前
LiteOS和RTOS 系统选型分析
arm开发
深圳市九鼎创展科技11 小时前
瑞芯微 RK3399 开发板 X3399 评测:高性能 ARM 平台的多面手
linux·arm开发·人工智能·单片机·嵌入式硬件·边缘计算
森焱森14 小时前
嵌入式硬件工程师应知 白银快速分析报告
linux·c语言·arm开发·嵌入式硬件·去中心化
森G1 天前
七、04ledc-sdk--------makefile有变化
linux·c语言·arm开发·c++·ubuntu
VekiSon1 天前
Linux内核驱动——杂项设备驱动与内核模块编译
linux·c语言·arm开发·嵌入式硬件
AI+程序员在路上1 天前
Nand Flash与EMMC区别及ARM开发板中的应用对比
arm开发
17(无规则自律)2 天前
深入浅出 Linux 内核模块,写一个内核版的 Hello World
linux·arm开发·嵌入式硬件
梁洪飞2 天前
内核的schedule和SMP多核处理器启动协议
linux·arm开发·嵌入式硬件·arm
代码游侠3 天前
学习笔记——Linux字符设备驱动
linux·运维·arm开发·嵌入式硬件·学习·架构
syseptember3 天前
Linux网络基础
linux·网络·arm开发