main.c
#include "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++)
;
}
cmd_t cmd_arr[6] = {
[0] = {.cmd_arr = "led1on", .gpio = GPIOE, .pin = GPIO_PIN_10, .status = GPIO_STATUS_SET, .gpio_write_p = hal_gpio_write},
[1] = {.cmd_arr = "led1off", .gpio = GPIOE, .pin = GPIO_PIN_10, .status = GPIO_STATUS_RESET, .gpio_write_p = hal_gpio_write},
[2] = {.cmd_arr = "led2on", .gpio = GPIOF, .pin = GPIO_PIN_10, .status = GPIO_STATUS_SET, .gpio_write_p = hal_gpio_write},
[3] = {.cmd_arr = "led2off", .gpio = GPIOF, .pin = GPIO_PIN_10, .status = GPIO_STATUS_RESET, .gpio_write_p = hal_gpio_write},
[4] = {.cmd_arr = "led3on", .gpio = GPIOE, .pin = GPIO_PIN_8, .status = GPIO_STATUS_SET, .gpio_write_p = hal_gpio_write},
[5] = {.cmd_arr = "led3off", .gpio = GPIOE, .pin = GPIO_PIN_8, .status = GPIO_STATUS_RESET, .gpio_write_p = hal_gpio_write},
};
cmd_t *find_command(const char *str)
{
int i;
for (i = 0; i < 6; i++)
{
if (!(strcmp(cmd_arr[i].cmd_arr, str)))
{
return &cmd_arr[i];
}
}
return 0;
}
int main()
{
GPIO_UART_Init();
while (1)
{
char *str = Get_String();
cmd_t *cmd_string = find_command(str);
if (0 == cmd_string)
{
printf("error,reenter please.\n");
}
else
{
cmd_string->gpio_write_p(cmd_string->gpio, cmd_string->pin, cmd_string->status);
printf("copy that\n");
}
}
return 0;
}
led.h
#ifndef __LED_H__
#define __LED_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
typedef enum
{
GPIO_STATUS_RESET,
GPIO_STATUS_SET,
} gpio_state_t;
typedef struct
{
char *cmd_arr;
gpio_t *gpio;
unsigned int pin;
gpio_state_t status;
void (*gpio_write_p)(gpio_t *gpiox, unsigned int pin, gpio_state_t status);
} cmd_t;
void hal_gpio_write(gpio_t *gpiox, unsigned int pin, gpio_state_t status);
int strcmp(const char *s1, const char *s2);
void GPIO_UART_Init();
cmd_t *find_command(const char *str);
void Put_Char(const char ch);
char Get_Char(void);
char *Get_String();
#endif
led.c
#include "led.h"
int strcmp(const char *s1, const char *s2)
{
while ((*s1 != '\0') && (*s2 != '\0') && (*s1 == *s2))
{
s1++;
s2++;
}
return (*s1 - *s2);
}
void GPIO_UART_Init()
{
RCC->MP_AHB4ENSETR |= (0x1 << 6);
RCC->MP_AHB4ENSETR |= (0x1 << 5);
RCC->MP_AHB4ENSETR |= (0x1 << 4);
RCC->MP_AHB4ENSETR |= (0x1 << 1);
RCC->MP_APB1ENSETR |= (0x1 << 16);
GPIOE->MODER &= (~(0x3 << 20));
GPIOE->MODER &= (~(0x3 << 16));
GPIOE->MODER |= (0x1 << 20);
GPIOE->MODER |= (0x1 << 16);
GPIOF->MODER &= (~(0x3 << 20));
GPIOF->MODER |= (0x1 << 20);
GPIOG->MODER &= (~(0x3 << 22));
GPIOG->MODER |= (0x2 << 22);
GPIOG->AFRH |= (0x6 << 12);
GPIOB->MODER &= (~(0x3 << 4));
GPIOB->MODER |= (0x2 << 4);
GPIOB->AFRL |= (0x8 << 8);
USART4->CR1 &= (~(0x1 << 28));
USART4->CR1 &= (~(0x1 << 12));
USART4->PRESC &= (~(0xF << 0));
USART4->BRR = 0x22b;
USART4->CR1 &= (~(0x1 << 15));
USART4->CR1 &= (~(0x1 << 10));
USART4->CR2 &= (~(0x3 << 12));
USART4->CR1 |= (0x3 << 2);
USART4->CR1 |= (0x1 << 0);
}
void hal_gpio_write(gpio_t *gpiox, unsigned int pin, gpio_state_t status)
{
if (GPIO_STATUS_RESET == status)
{
gpiox->ODR &= (~(0x1 << pin));
}
else if (GPIO_STATUS_SET == status)
{
gpiox->ODR |= (0x1 << pin);
}
}
void Put_Char(const char ch)
{
while (!(USART4->ISR & (0x1 << 7)))
;
USART4->TDR = ch;
while (!(USART4->ISR & (0x1 << 6)))
;
if ('\n' == ch)
{
Put_Char('\r');
}
}
char Get_Char(void)
{
while (!(USART4->ISR & (0x1 << 5)))
;
return (char)USART4->RDR;
}
char buf[50] = "";
char *Get_String()
{
int i = 0;
for (i = 0; i < 50; i++)
{
buf[i] = Get_Char();
Put_Char(buf[i]);
if (buf[i] == '\r')
{
break;
}
}
buf[i] = '\0';
Put_Char('\n');
return buf;
}