原始版本

This commit is contained in:
冯佳
2025-06-19 21:56:46 +08:00
parent fe98e5f010
commit a4841450cf
4152 changed files with 1910684 additions and 0 deletions

View File

@ -0,0 +1,20 @@
from building import *
import os
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
if GetDepend('BOARD_STM32F407VE6T_V2'):
cwd = GetCurrentDir()
src = Glob('src/*.c')
CPPPATH = [cwd, os.path.join(cwd, 'include')]
group = DefineGroup('Bsp', src, depend = [''], CPPPATH = CPPPATH)
list = os.listdir(cwd)
for item in list:
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
group = group + SConscript(os.path.join(item, 'SConscript'))
Return('group')

View File

@ -0,0 +1,30 @@
#ifndef __BSP_KEY_H__
#define __BSP_KEY_H__
#include <rtthread.h>
#include <rtdevice.h>
#include <drv_gpio.h>
//脚位定义
#define KEY_UP_PIN GET_PIN(A, 0)
#define KEY1_PIN GET_PIN(E, 4)
#define KEY2_PIN GET_PIN(E, 3)
// 封装引脚初始化和读操作的宏
#define INIT_PIN(pin, mode) rt_pin_mode((pin), (mode))
#define READ_PIN(pin) rt_pin_read((pin))
#define WRITE_PIN(pin, value) rt_pin_write((pin), (value))
void key_init(void);
rt_uint8_t key_scan(rt_uint8_t mode);
rt_uint8_t key_hw_read_up(void);
rt_uint8_t key_hw_read_key1(void);
rt_uint8_t key_hw_read_key2(void);
rt_uint8_t key_get_state(int key);
rt_uint32_t display_key_count(int key);
rt_uint8_t MenuPack_GetKey(void);
#endif //__BSP_LED_H__

View File

@ -0,0 +1,45 @@
#ifndef __BSP_LED_H__
#define __BSP_LED_H__
#include <rtthread.h>
#include <rtdevice.h>
#include <drv_gpio.h>
#define LED1_PIN GET_PIN(A, 6)
#define LED2_PIN GET_PIN(A, 7)
// 引脚操作宏定义
#define LED1_ON rt_pin_write(LED1_PIN, PIN_LOW)
#define LED1_OFF rt_pin_write(LED1_PIN, PIN_HIGH)
#define LED2_ON rt_pin_write(LED2_PIN, PIN_LOW)
#define LED2_OFF rt_pin_write(LED2_PIN, PIN_HIGH)
// 红
#define LED1 do { \
LED1_ON; \
LED2_OFF;\
} while(0)
#define LED2 do { \
LED1_OFF;\
LED2_ON; \
} while(0)
#define LED_ALLON do { \
LED1_ON; \
LED2_ON; \
} while(0)
#define LED_ALLOFF do { \
LED1_OFF; \
LED2_OFF; \
} while(0)
void led_init(void);
void led_select(int color);
void led_loop(void);
rt_uint8_t led_get_status_led1(void);
rt_uint8_t led_get_status_led2(void);
#endif //__BSP_LED_H__

View File

@ -0,0 +1,167 @@
#include "bsp_key.h"
// 定义按键状态标志变量
rt_uint8_t key_status_flag = 0;
void key_hw_init(void)
{
// 初始化引脚
INIT_PIN(KEY_UP_PIN, PIN_MODE_INPUT_PULLDOWN);
INIT_PIN(KEY1_PIN, PIN_MODE_INPUT_PULLUP);
INIT_PIN(KEY2_PIN, PIN_MODE_INPUT_PULLUP);
}
// 按键初始化函数
void key_init(void)
{
key_hw_init();
}
rt_uint8_t key_hw_read_up(void)
{
return READ_PIN(KEY_UP_PIN);
}
rt_uint8_t key_hw_read_key1(void)
{
return READ_PIN(KEY1_PIN);
}
rt_uint8_t key_hw_read_key2(void)
{
return READ_PIN(KEY2_PIN);
}
// 读取按键状态的函数
rt_uint8_t key_scan(rt_uint8_t mode)
{
static rt_uint8_t key_up = 1;
if (mode)
{
key_up = 1;
}
if (key_up && ( key_hw_read_up() == PIN_HIGH ||
key_hw_read_key1() == PIN_LOW ||
key_hw_read_key2() == PIN_LOW )){
rt_thread_mdelay(10);
key_up = 0;
if (key_hw_read_up() == PIN_HIGH)
{
key_status_flag = 1;
return 1;
}
else if (key_hw_read_key1() == PIN_LOW)
{
key_status_flag = 2;
return 2;
}
else if (key_hw_read_key2() == PIN_LOW)
{
key_status_flag = 3;
return 3;
}
} else if (key_hw_read_up() == PIN_LOW &&
key_hw_read_key1() == PIN_HIGH &&
key_hw_read_key2() == PIN_HIGH) {
key_up = 1;
key_status_flag = 0;
}
return 0;
}
static rt_uint32_t key_count[4] = {0};
// 测试按键功能的线程入口函数
static void key_test_thread_entry(void *parameter)
{
rt_uint8_t key_val;
// 初始化按键
key_init();
while (1)
{
key_val = key_scan(0);
if (key_val)
{
if(key_val == 1) // KEY_UP 复位按键计数
{
key_count[1]++;
key_count[2] = 0;
key_count[3] = 0;
rt_kprintf("KEY_UP pressed, key counts reset!\n");
}
else
{
key_count[key_val]++;
rt_kprintf("Key %d is pressed, count: %d\n", key_val, key_count[key_val]);
}
}
rt_thread_mdelay(10);
}
}
rt_uint32_t key_get_count(int key)
{
if (key < 1 || key > 3)
return 0;
return key_count[key];
}
rt_uint8_t key_get_state(int key)
{
switch(key)
{
case 1:
return key_hw_read_up(); // KEY_UP
case 2:
return !key_hw_read_key1(); // KEY1
case 3:
return !key_hw_read_key2(); // KEY2
default:
return 0;
}
}
rt_uint32_t display_key_count(int key)
{
return key_get_count(key);
}
// 实现获取按键状态标志的函数
rt_uint8_t get_key_status_flag(void)
{
return key_status_flag;
}
/**
* @brief 获取当前被按下的按键编号
* @return 0:无按键1:KEY_UP2:KEY13:KEY2
*/
rt_uint8_t MenuPack_GetKey(void)
{
return key_scan(0);
}
// 创建按键测试线程
int key_test(void)
{
rt_thread_t tid;
tid = rt_thread_create("key_test",
key_test_thread_entry,
RT_NULL,
512,
RT_THREAD_PRIORITY_MAX / 2,
20);
if (tid != RT_NULL)
rt_thread_startup(tid);
return 0;
}
// 自动初始化按键测试线程
INIT_APP_EXPORT(key_test);

View File

@ -0,0 +1,59 @@
#include "bsp_led.h"
static rt_uint8_t led1_status = 0;
static rt_uint8_t led2_status = 0;
void led_init(void) {
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
LED_ALLOFF;
}
int led_init_wrapper(void) {
led_init();
return 0; // 返回 0 表示初始化成功
}
INIT_APP_EXPORT(led_init_wrapper);
void led_select(int color) {
switch (color) {
case 1:
LED1;
led1_status = 1;
led2_status = 0;
break;
case 2:
LED2;
led1_status = 0;
led2_status = 1;
break;
case 3:
LED_ALLON;
led1_status = 1;
led2_status = 1;
break;
case 0:
default:
LED_ALLOFF;
led1_status = 0;
led2_status = 0;
break;
}
}
rt_uint8_t led_get_status_led1(void) {
return led1_status;
}
rt_uint8_t led_get_status_led2(void) {
return led2_status;
}
void led_loop(void) {
while (1) {
for (int i = 0; i <= 3; i++) {
led_select(i);
rt_thread_mdelay(5000);
}
}
}