include/serial.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_uart.h"
//引脚封装
#define GPIO_PIN_0 0
#define GPIO_PIN_1 1
#define GPIO_PIN_2 2
#define GPIO_PIN_3 3
#define GPIO_PIN_4 4
#define GPIO_PIN_5 5
#define GPIO_PIN_6 6
#define GPIO_PIN_7 7
#define GPIO_PIN_8 8
#define GPIO_PIN_9 9
#define GPIO_PIN_10 10
#define GPIO_PIN_11 11
#define GPIO_PIN_12 12
#define GPIO_PIN_13 13
#define GPIO_PIN_14 14
#define GPIO_PIN_15 15
//模式寄存器 0 1 2 3
typedef enum
{
INPUT,
OUTPUT,
ALT,
ANALOG,
}gpio_moder_t;
//输出类型寄存器
typedef enum
{
PP, //推挽
OD, //开漏
}gpio_otyper_t;
//输出速度寄存器
typedef enum
{
LOW,
MED,
HIGH,
VERY_HIGH,
}gpio_ospeedr_t;
//是否需要上下拉电阻寄存器封装
typedef enum
{
NO_PUPDR, //禁止 0
PU, //上拉 1
PD, //下拉 2
}gpio_pupdr_t;
//输出高低电平寄存器封装
typedef enum
{
GPIO_RESET_T, //输出低电平 0
GPIO_SET_T, //输出高电平 1
}gpio_odr_t;
//寄存器初始化封装
typedef struct
{
gpio_moder_t moder; //模式
gpio_otyper_t otyper; //输出类型
gpio_ospeedr_t ospeedr; //输出速率
gpio_pupdr_t pupdr; //是否需要上下拉电阻
}gpio_init_t;
//结构体封装
typedef struct{
char* cmd_arr; //命令符字符串
gpio_t * gpio; //gpio组号
unsigned int pin; //对应的引脚编号
gpio_odr_t status; //LED灯状态
void (*gpio_write_p)(gpio_t * gpio,unsigned int pin, gpio_odr_t status);
}cmd_t;
//灯的初始化GPIO初始化
void my_Initi_Led(gpio_t* gpiox,unsigned int pin,gpio_init_t* init);
//灯的设置高低电平
void hal_gpio_write(gpio_t* gpiox,unsigned int pin,unsigned int gpio_status);
//比较函数
int my_stmcmp(const char* str,const char* str2);
//查找命令函数
cmd_t* find_command(const char* str,cmd_t* cmd_arr);
//初始化相关操作
void hal_uart4_init();
//发送一个字符
void hal_put_char(const char str);
//发送一个字符串
void hal_put_string(const char *string);
//接收一个字符
char hal_get_char();
//接受一个字符串
char* hal_get_string();
#endif
src/serial.c
#include "serial.h"
//LED1---->GPIOE10
//LED2--->GPIOF10
//LED3--->GPIOE8
//PB2--->UART4_RX
//PG11--->UART4_TX
extern void delay_ms(int ms);
//灯的设置高低电平
void hal_gpio_write(gpio_t* gpiox,unsigned int pin,unsigned int gpio_status)
{
//判断输入的值是否为低电平
if(gpio_status == GPIO_RESET_T)
{
gpiox->ODR &= ~(0x1 << pin);
}
else
{
gpiox->ODR |= 0x1 << pin;
}
}
//比较函数
int my_stmcmp(const char* str,const char* str2)
{
int i=0;
while(str[i]!='\0'&&str2[i]!='\0'&&str[i]==str2[i])
{
i++;
}
int ret = str[i]-str2[i]; //返回两个字符串ascii的差值}
return ret;
}
//查找命令函数
cmd_t* find_command(const char* str,cmd_t* cmd_arr)
{
// 串口输入的字符串与结构体中的每个元素中的cmd_arr变量进行比较
//1.for遍历进行判断
int i=0;
for(i=0;i<6;i++)
{
int ret = my_stmcmp(str,cmd_arr[i].cmd_arr);
if(ret == 0)
{
return &cmd_arr[i];
}
}
return 0;
//2.比较函数 strcmp,注意这个函数需要自己编写,因为裸机开发,没有函数库
//3.如果比较成功,返回结构体首地址
}
//初始化相关操作
void hal_uart4_init()
{
//~~~~~~~~~~~~~RCC章节初始化~~~~~~~~~~~~~~~~~~~~~~~~//
//使能AHB4初始化GPIOB GPIOG[1] [6] =1
RCC->MP_AHB4ENSETR |= (0x21 << 1);
//使能APB1初始化[16]=1
RCC->MP_APB1ENSETR |= (0x1 << 16);
//~~~~~~~~~~~~~GPIO章节初始化~~~~~~~~~~~~~~~~~~~~~~~//
//GPIOB寄存器初始化 [5:4]=10
GPIOB->MODER |= (0x1 << 5);
GPIOB->MODER &= (~(0x1 << 4));
//GPIOB_AFRL设置引脚的复用功能模式[11:8]=1000
GPIOB->AFRL |= (0x1 << 11);
GPIOB->AFRL &= (~(0x7 << 8));
//GPIOG寄存器初始化 [23:22]=10
GPIOG->MODER |= (0x1 << 23);
GPIOG->MODER &= (~(0x1 << 22));
//GPIOG_AFRH设置引脚的复用功能模式[15:12]=0110
GPIOG->AFRH |= (0xF << 15);
GPIOG->AFRH &= (~(0x9 << 15));
//~~~~~~~~~~~~~UART章节初始化~~~~~~~~~~~~~~~~~~~~~~//
//0.设置串口UE=0 [0]=0
if(USART4->CR1 & (0x1))
{
delay_ms(500);
USART4->CR1 &= (~(0x1));
}
//1.设置1位的起始位,8位数据位 [28]=0 [12]=0
USART4->CR1 &= (~(0x1 << 12));
USART4->CR1 &= (~(0x1 << 28));
//2.没有校验位 CR1[10]=0
USART4->CR1 &= (~(0x1 << 10));
//3.设置1位停止位 CR2[13:12] =00
USART4->CR2 &= (~(0x3 << 12));
//4.设置16倍采样率 CR1[15]=0
USART4->CR1 &= (~(0x1 << 15));
//5.设置串口不分频 0000
USART4->PRESC &= (~(0xF));
//6.设置串口波特率为0x22b 时钟源/波特率 64000000/115200
USART4->BRR = 0x22b;
//7.设置串口发送器使能
USART4->CR1 |= (0x1 << 3);
//8.设置串口接收器使能
USART4->CR1 |= (0x1 << 2);
//9.设置串口使能
USART4->CR1 |= (0x1);
}
//封装函数1功能:GPIO初始化相关操作
void my_Initi_Led(gpio_t* gpiox,unsigned int pin,gpio_init_t* init)
{
//模式
gpiox->MODER &= (~(0x3 << (2*pin)));
gpiox->MODER |= (init->moder << (2*pin));
//类型
gpiox->OTYPER &= (~(0x1 << pin));
gpiox->OTYPER |= (init->otyper << pin);
//速度
gpiox->OSPEEDR &= (~(0x3 << (2*pin)));
gpiox->OSPEEDR |= (init->ospeedr << (2*pin));
//上下拉电阻
gpiox->PUPDR &= (~(0x3 << (2*pin)));
gpiox->PUPDR |= (init->pupdr << (2*pin));
}
//封装函数2功能:GPIO引脚操作
void gpio_write_p(gpio_t* gpiox,unsigned int pin,gpio_odr_t gpio_status)
{
if(gpio_status == GPIO_RESET_T)
{
gpiox->ODR &= (~(0x1 << pin));
}
else
{
gpiox->ODR |= (0x1 << pin);
}
}
//发送一个字符
void hal_put_char(const char str)
{
//1.判断发送数据寄存器是否为空ISR[7]
//特点:为空才可以发送下一位数据,为满则需要等待
//读0:满,等待 读1:发送数据
while(!(USART4->ISR & (0x1 << 7)));
//2.将发送的内容,赋值给发送给数据寄存器
USART4->TDR &= 0; //寄存器清零
USART4->TDR = str;
//3.判断一帧数据是否发送完成 ISR[6]
while(!(USART4->ISR & (0x1 << 6)));
//读0:没有发送完成,需要等待 读1:发送完成
}
//接收一个字符
char hal_get_char()
{
//1.判断接收数据寄存器是否接收到数据 ISR[5]
while(!(USART4->ISR & (0x1 << 5)));
//2.将接收数据寄存器中的内容读出来
char ch = USART4->RDR;
return ch;
}
//发送一个字符串
void hal_put_string(const char* string)
{
//判断字符串输入完成标志'\0'
const char *p = string;
while(*p != '\0')
{
hal_put_char(*p);
p++;
}
//一个字符一个字符进行发送
hal_put_char('\r');
hal_put_char('\n');
}
//接收一个字符串
char* hal_get_string()
{
//循环进行接收
static char string[32] = {0};
int i;
for(i = 0;i<32;i++)
{
string[i] = 0;
}
i = 0;
while(1)
{
string[i] = hal_get_char();//板子接收
hal_put_char(string[i]); //发送到串口工具中
if(string[i] == '\r')
{
break;
}
i++;
}
string[i] = '\0';
hal_put_char('\n');
return string;
//当键盘的回车键按键之后,代表字符串输出完成'\r'
}
main.c
#include "serial.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++);
}
void init_Led()
{
//通过RCC章节 将GPIOE GPIOF 组控制器使能
RCC->MP_AHB4ENSETR |= (0x3 << 4);
//结构体初始化
gpio_init_t init = {OUTPUT,PP,LOW,NO_PUPDR};
//调用函数初始化
my_Initi_Led(GPIOE,GPIO_PIN_10, &init);
my_Initi_Led(GPIOF,GPIO_PIN_10, &init);
my_Initi_Led(GPIOE,GPIO_PIN_8, &init);
}
//对结构体进行初始化
cmd_t cmd_arr[6] = {
[0] = {
.cmd_arr = "led1on",
.gpio = GPIOE,
.pin = GPIO_PIN_10,
.status = GPIO_SET_T,
.gpio_write_p = hal_gpio_write,
},
[1] = {
.cmd_arr = "led2on",
.gpio = GPIOF,
.pin = GPIO_PIN_10,
.status = GPIO_SET_T,
.gpio_write_p = hal_gpio_write,
},
[2] = {
.cmd_arr = "led3on",
.gpio = GPIOE,
.pin = GPIO_PIN_8,
.status = GPIO_SET_T,
.gpio_write_p = hal_gpio_write,
},
[3] = {
.cmd_arr = "led1off",
.gpio = GPIOE,
.pin = GPIO_PIN_10,
.status = GPIO_RESET_T,
.gpio_write_p = hal_gpio_write,
},
[4] = {
.cmd_arr = "led2off",
.gpio = GPIOF,
.pin = GPIO_PIN_10,
.status = GPIO_RESET_T,
.gpio_write_p = hal_gpio_write,
},
[5] = {
.cmd_arr = "led3off",
.gpio = GPIOE,
.pin = GPIO_PIN_8,
.status = GPIO_RESET_T,
.gpio_write_p = hal_gpio_write,
},
};
int main()
{
init_Led();
hal_uart4_init();
hal_put_string("----please input--->");
while(1)
{
char *string = 0;
string = hal_get_string();
cmd_t *p = 0;
p = find_command(string,cmd_arr);
if(p == 0)
{
printf("there are not found\n");
}
else
{
p->gpio_write_p(p->gpio,p->pin,p->status);
}
}
return 0;
}
result