A7核
//command.h
#ifndef __COMMAND_H__
#define __COMMAND_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_uart.h"
#include "stm32mp1xx_rcc.h"
typedef enum
{
GPIO_RESET_T,
GPIO_SET_T
}gpio_odr_t;
//引脚封装
#define GPIO_PIN_8 8
#define GPIO_PIN_10 10
// 命令的结构体
typedef struct
{
char *cmd_str; // 命令的字符串
gpio_t *gpiox; //gpio组号
unsigned int pin; //对应引脚编号
gpio_odr_t status; //对应引脚状态
void (*gpio_write)(gpio_t *gpiox, unsigned int pin, gpio_odr_t status);
} cmd_t;
void delay_ms(int ms);
void hal_uart4_init();
void hal_led_init();
void hal_gpio_write(gpio_t *gpiox, unsigned int pin, gpio_odr_t status);
//发送一个字符
void hal_put_char(const char str);
//发送一个字符串
void hal_put_string(const char* string);
//接收一个字符
char hal_get_char();
//接收一个字符串
char* hal_get_string();
int strcmp(const char *s1, const char *s2);
cmd_t *find_command(const char *buffer);
#endif
//command.c
#include "command.h"
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_str = "led1on",
.gpiox = GPIOE,
.pin = GPIO_PIN_10,
.status = GPIO_SET_T,
.gpio_write = hal_gpio_write,
},
[1] = {
.cmd_str = "led1off",
.gpiox = GPIOE,
.pin = GPIO_PIN_10,
.status = GPIO_RESET_T,
.gpio_write = hal_gpio_write,
},
[2] = {
.cmd_str = "led2on",
.gpiox = GPIOF,
.pin = GPIO_PIN_10,
.status = GPIO_SET_T,
.gpio_write = hal_gpio_write,
},
[3] = {
.cmd_str = "led2off",
.gpiox = GPIOF,
.pin = GPIO_PIN_10,
.status = GPIO_RESET_T,
.gpio_write = hal_gpio_write,
},
[4] = {
.cmd_str = "led3on",
.gpiox = GPIOE,
.pin = GPIO_PIN_8,
.status = GPIO_SET_T,
.gpio_write = hal_gpio_write,
},
[5] = {
.cmd_str = "led3off",
.gpiox = GPIOE,
.pin = GPIO_PIN_8,
.status = GPIO_RESET_T,
.gpio_write = hal_gpio_write,
},
};
void hal_uart4_init()
{
//RCC章节
//1.使能GPIOB组控制器MP_AHB4ENSETR[1] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//2.使能GPIOG组控制器MP_AHB4ENSETR[6] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 6);
//3.使能UART4组控制器MP_APB1ENSETR[16] = 1
RCC->MP_APB1ENSETR |= (0x1 << 16);
//GPIO章节 复用功能模式 复用功能为串口接收/发送
//PB2--->UART4_RX
//1.设置PB2引脚为复用功能模式 MODER[5:4] = 10
GPIOB->MODER &= (~(0x3 << 4));
GPIOB->MODER |= (0x1 << 5);
//2.设置PB2引脚复用功能为UART4_RX AFRL[11:8] = 1000
GPIOB->AFRL &= (~(0xf << 8));
GPIOB->AFRL |= (0x1 << 11);
//1.设置PG11引脚为复用功能模式 MODER[23:22] = 10
GPIOG->MODER &= (~(0x3 << 22));
GPIOG->MODER |= (0x1 << 23);
//2.设置PG11引脚复用功能为UART4_TX AFRH[15:12] = 0110
GPIOG->AFRH &= (~(0xf << 12));
GPIOG->AFRH |= (0x3 << 13);
//UART4章节 8N1 115200 对应位使能
//1.判断UE是否使能,如果等于1,禁止
if(USART4->CR1 & 0x1)
{
delay_ms(500);
USART4->CR1 &= (~(0x1 << 0));
}
//1.设置UART4串口 1位起始位 8位数据位 1位停止位 无奇偶校验位
//CR1[28][12] = 00 CR2[13:12] = 00 CR1[10] = 0
USART4->CR1 &= (~(0x1 << 28));
USART4->CR1 &= (~(0x1 << 12));
USART4->CR2 &= (~(0x3 << 12));
USART4->CR1 &= (~(0x1 << 10));
//2.设置16倍采样率 CR1[15] = 0
USART4->CR1 &= (~(0x1 << 15));
//3.设置串口不分频 PRESC[3:0] = 0000
USART4->PRESC &= (~(0xf << 0));
//4.设置串口波特率为115200 BRR = 0x22b
USART4->BRR |= 0x22b;
//4.设置串口发送位使能 CR1[3] = 1
USART4->CR1 |= (0x1 << 3);
//5.设置串口接收位使能 CR1[2] = 1
USART4->CR1 |= (0x1 << 2);
//6.设置串口使能 CR1[0] = 1
USART4->CR1 |= (0x1 << 0);
}
void hal_led_init(){
RCC->MP_AHB4ENSETR|=(0x1<<4);
RCC->MP_AHB4ENSETR|=(0x1<<5);
GPIOE->MODER&=(~(0x3<<20));
GPIOE->MODER|=(0x1<<20);
GPIOF->MODER&=(~(0x3<<20));
GPIOF->MODER|=(0x1<<20);
GPIOE->MODER&=(~(0x3<<16));
GPIOE->MODER|=(0x1<<16);
GPIOE->OTYPER&=(~(0x1<<10));
GPIOF->OTYPER&=(~(0x1<<10));
GPIOE->OTYPER&=(~(0x1<<8));
GPIOE->OSPEEDR&=(~(0x1<<20));
GPIOF->OSPEEDR&=(~(0x1<<20));
GPIOE->OSPEEDR&=(~(0x1<<16));
GPIOE->PUPDR&=(~(0x3<<20));
GPIOF->PUPDR&=(~(0x3<<20));
GPIOE->PUPDR&=(~(0x3<<16));
}
void hal_gpio_write(gpio_t* gpiox,unsigned int pin,gpio_odr_t status){
if(status==GPIO_SET_T){
gpiox->ODR|=(0x1<ODR&=(~(0x1<ISR & (0x1 << 7)));
//发送数据寄存器 = str
USART4->TDR = str;
//判断发送数据是否完成 ISR[6]
while(!(USART4->ISR & (0x1 << 6)));
}
//发送一个字符串
void hal_put_string(const char* string)
{
//判断是否为\0
//一个一个字符发送
while(*string)
{
hal_put_char(*string++);
}
hal_put_char('\n');
hal_put_char('\r');
}
//接收一个字符
char hal_get_char()
{
//定一个变量,用来接收的数据
char ch;
//判断接收数据寄存器中是否有数据可以读 ISR[5]
while(!(USART4->ISR & (0x1 << 5)));
// ch = 接收数据寄存器
ch = USART4->RDR;
return ch;
}
char buffer[50]={};
//接收一个字符串
char* hal_get_string()
{
//for循环
//判断键盘回车是否按下 '\r'
unsigned int i = 0;
for(i=0;i<49-1;i++)
{
//接收一个字符
buffer[i] = hal_get_char();
//回显到串口工具 发送一个字符
hal_put_char(buffer[i]);
if(buffer[i] == '\r')
break;
}
buffer[i] = '\0';
hal_put_char('\n');
return buffer;
}
int strcmp(const char *s1, const char *s2)
{
while (*s1 != '\0')
{
if (*s1 != *s2)
{
return (*s1 - *s2);
}
s1++;
s2++;
}
return (*s1 - *s2);
}
cmd_t *find_command(const char *buffer)
{
unsigned int i;
for (i = 0; i < 6; i++)
{
if (!strcmp(buffer, cmd_arr[i].cmd_str))
{
hal_put_string("find command\r");
return &cmd_arr[i];
}
}
hal_put_string("Dont find command\r");
return 0;
}
//mian.c
#include "command.h"
extern void printf(const char *fmt, ...);
int main()
{
hal_led_init();
hal_uart4_init();
while (1)
{
char *buffer=hal_get_string();
cmd_t *p = find_command(buffer);
if(p==0){
hal_put_string("cmp failed\n");
}else{
hal_put_string("cmp success\n");
p->gpio_write(p->gpiox, p->pin, p->status);
}
}
return 0;
}
M4核
//gpio.c
/* USER CODE BEGIN 2 */
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
switch(GPIO_Pin)
{
case GPIO_PIN_15:
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_8);
printf("guang dian gan ying...\n");
break;
default:
break;
}
}
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
switch(GPIO_Pin){
case GPIO_PIN_9: // KEY1
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_10);
printf("key1 down\n");
break;
case GPIO_PIN_7: // KEY2
HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_10);
printf("key2 down\n");
break;
default :
break;
}
}
/* USER CODE END 2 */
//uart.c
/* USER CODE BEGIN 1 */
int fputc(int ch, FILE *stream)
{
while(!(huart4.Instance->ISR & (0x1 << 7)));
huart4.Instance->TDR = ch;
if(ch == '\n')
{
while(!(huart4.Instance->ISR & (0x1 << 7)));
huart4.Instance->TDR = '\r';
}
return ch;
}
/* USER CODE END 1 */