uart_led.h
#ifndef __UART_LED_H__
#define __UART_LED_H__
#include "stm32mp1xx_uart.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
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_PUPD,
PU,
PD
}gpio_pupdr_t;
typedef enum
{
GPIO_RESET,
GPIO_SET
}gpio_status_t;
typedef struct
{
gpio_moder_t moder;
gpio_otyper_t otyper;
gpio_pupdr_t pupdr;
gpio_ospeedr_t speed;
}gpio_init_t;
typedef struct
{
char *arr;
gpio_t* gpio;
unsigned int pin;
gpio_status_t status;
void (*gpio_write_t)(gpio_t* gpio,unsigned int pin, gpio_status_t status);
}cmd_t;
void hal_gpio_write(gpio_t* gpiox,unsigned int pin,gpio_status_t status);
void hal_gpio_init(gpio_t*gpiox,gpio_init_t* init,unsigned int pin);
char get_char();
char* get_string();
void put_char(const char c);
cmd_t* find_command(const char* str);
int strcmp(const char* s1,const char* s2);
void hal_uart_init();
void put_string(const char* string);
#endif
main.c
#include "uart_led.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 led_init()
{
RCC->MP_AHB4ENSETR |= (0x1<<4);
RCC->MP_AHB4ENSETR |= (0x1<<5);
gpio_init_t init ={OUTPUT,PP,LOW,NO_PUPD};
hal_gpio_init(GPIOE,&init,10);
hal_gpio_init(GPIOF,&init,10);
hal_gpio_init(GPIOE,&init,8);
}
cmd_t cmd_arr[6]={
[0]={
.arr="led1on",
.gpio=GPIOE,
.pin=10,
.status=GPIO_SET,
.gpio_write_t=hal_gpio_write
},
[1]={
.arr="led1off",
.gpio=GPIOE,
.pin=10,
.status=GPIO_RESET,
.gpio_write_t=hal_gpio_write
},
[2]={
.arr="led2on",
.gpio=GPIOF,
.pin=10,
.status=GPIO_SET,
.gpio_write_t=hal_gpio_write
},
[3]={
.arr="led2off",
.gpio=GPIOF,
.pin=10,
.status=GPIO_RESET,
.gpio_write_t=hal_gpio_write
},
[4]={
.arr="led3on",
.gpio=GPIOE,
.pin=8,
.status=GPIO_SET,
.gpio_write_t=hal_gpio_write
},
[5]={
.arr="led3off",
.gpio=GPIOE,
.pin=8,
.status=GPIO_RESET,
.gpio_write_t=hal_gpio_write
},
};
//查找字符串函数
cmd_t* find_command(const char* str)
{
int i;
for(i=0;i<6;i++)
{
if(!(strcmp(str,cmd_arr[i].arr)))
{
put_string("找到了");
printf("%d",i);
return &cmd_arr[i];
}
}
return 0;
}
int main()
{
led_init();
hal_uart_init();
while(1)
{
put_string("please input>>");
char* str=get_string();
cmd_t* p;
p=find_command(str);
if(p==0)
{
put_string("error");
}
else
{
put_string(p->arr);
p->gpio_write_t(p->gpio,p->pin,p->status);
}
}
return 0;
}
uart_led.c
#include "uart_led.h"
extern void delay_ms(int ms);
//uart串口代码
void hal_uart_init()
{
//RCC章节
RCC->MP_AHB4ENSETR |= (0x1<<1);//GPIOB
RCC->MP_AHB4ENSETR |= (0x1<<6);//GPIOG
RCC->MP_APB1ENSETR |= (0x1<<16);//UART4
//GPIO章节
GPIOB->MODER &= ~(0x3<<4);
GPIOB->MODER |= (0x2<<4);
GPIOG->MODER &= ~(0x3<<22);
GPIOG->MODER |= (0x2<<22);
GPIOB->AFRL &= (0xf<<8);
GPIOB->AFRL |= (0x8<<8);
GPIOG->AFRH &= (0xf<<12);
GPIOG->AFRH |= (0x6<<12);
//USART4章节
if(USART4->CR1 & 1)
{
delay_ms(10);
USART4->CR1 &= ~(0x1<<0);
}
USART4->PRESC &= ~(0xf<<0);
USART4->BRR=0x22b;//115200
USART4->CR1 &= ~(0x1<<28);
USART4->CR1 &= ~(0x1<<12);//8
USART4->CR1 &= ~(0x1<<10);//N
USART4->CR2 &= ~(0x3<<12);//1
USART4->CR1 &= ~(0x1<<15);//16倍串口采样率
USART4->CR1 |= (0x1<<0);
USART4->CR1 |= (0x1<<2);//串口接收发送寄存器使能
}
char get_char()
{
char c;
while(!(USART4->ISR & (0x1<<5)));
c=USART4->RDR;
return c;
}
char buffer[50]={0};
char* get_string()
{
int i=0;
while(1)
{
buffer[i]=get_char();
put_char(buffer[i]);
if(buffer[i]=='\r')
{
break;
}
i++;
}
buffer[i]='\0';
put_char('\n');
return buffer;
}
void put_char(const char c)
{
while(!(USART4->ISR & (0x1<<7)));
USART4->TDR=c;
while(!(USART4->ISR & (0x1<<6)));
}
void put_string(const char* string)
{
while(*string)
{
put_char(*string++);
}
put_char('\n');
put_char('\r');
}
//led灯代码
void hal_gpio_init(gpio_t*gpiox,gpio_init_t* init,unsigned int pin)
{
gpiox->MODER &= ~(0x3<<pin*2);
gpiox->MODER |= (init->moder<<pin*2);
gpiox->OTYPER &= ~(0x1<<pin);
gpiox->OTYPER |= (init->otyper<<pin);
gpiox->OSPEEDR &= ~((0x3<<(pin*2)));
gpiox->OSPEEDR |= (init->speed<<(2*pin));
gpiox->PUPDR &= ~(0x3<<(pin*2));
gpiox->PUPDR &= ~(init->pupdr<<(pin*2));
}
void hal_gpio_write(gpio_t* gpiox,unsigned int pin,gpio_status_t status)
{
if(status==GPIO_RESET)
{
gpiox->ODR &= ~(0x1<<pin);
}
else if(status==GPIO_SET)
{
gpiox->ODR |= (0x1<<pin);
}
}
}
int strcmp(const char* src, const char* dst)
{
int ret = 0;
while( !(ret = *(unsigned char*)src - *(unsigned char*)dst) && *dst)
{
src ++;
dst ++;
}
if( ret < 0) ret = -1;
else if(ret > 0) ret = 1;
return ret;
}