diff --git a/.DS_Store b/.DS_Store index b3388df..c294515 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/User/bsp/servo_pwm.c b/User/bsp/servo_pwm.c new file mode 100644 index 0000000..4648e7a --- /dev/null +++ b/User/bsp/servo_pwm.c @@ -0,0 +1,48 @@ +/* Includes ----------------------------------------------------------------- */ +#include "servo_pwm.h" + +#include "main.h" + + +/* Private define ----------------------------------------------------------- */ +/* Private macro ------------------------------------------------------------ */ +/* Private typedef ---------------------------------------------------------- */ +/* Private variables -------------------------------------------------------- */ +/* Private function -------------------------------------------------------- */ +/* Exported functions ------------------------------------------------------- */ + +int8_t BSP_PWM_Start(BSP_PWM_Channel_t ch) { + + TIM_HandleTypeDef* htim = pwm_channel_config[ch].htim; + uint32_t channel = pwm_channel_config[ch].channel; + + if(HAL_TIM_PWM_Start(htim, channel)!=HAL_OK){ + return -1; + }else return 0; +} + +int8_t BSP_PWM_Set(BSP_PWM_Channel_t ch, float duty_cycle) { + if (duty_cycle > 1.0f) return -1; + + uint16_t pulse = duty_cycle/CYCLE * PWM_RESOLUTION; + + if(__HAL_TIM_SET_COMPARE(pwm_channel_config[ch].htim, pwm_channel_config[ch].channel, pulse)!=HAL_OK){ + return -1; + }else return 0; +} + +int8_t BSP_PWM_Stop(BSP_PWM_Channel_t ch){ + + TIM_HandleTypeDef* htim = pwm_channel_config[ch].htim; + uint32_t channel = pwm_channel_config[ch].channel; + + if(HAL_TIM_PWM_Stop(htim, channel)!=HAL_OK){ + return -1; + }else return 0; + + +}; + + + + diff --git a/User/bsp/servo_pwm.h b/User/bsp/servo_pwm.h new file mode 100644 index 0000000..aefb7f1 --- /dev/null +++ b/User/bsp/servo_pwm.h @@ -0,0 +1,45 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ----------------------------------------------------------------- */ +#include +#include "tim.h" +#include "bsp/bsp.h" + +/* Exported constants ------------------------------------------------------- */ +/* Exported macro ----------------------------------------------------------- */ +/* Exported types ----------------------------------------------------------- */ +typedef struct { + TIM_HandleTypeDef* htim; // 定时器句柄 + uint32_t channel; // 定时器通道 +} PWM_Channel_Config_t; + +#define PWM_RESOLUTION 1000 // ARR change begin +#define CYCLE 20 //ms + +typedef enum { + BSP_PWM_SERVO = 0, + BSP_PWM_IMU_HEAT = 1, +} BSP_PWM_Channel_t; + +const PWM_Channel_Config_t pwm_channel_config[] = { + [BSP_PWM_SERVO] = { &htim1, TIM_CHANNEL_1 }, // xxx 对应 TIMx 通道x + [BSP_PWM_IMU_HEAT] = { &htim1, TIM_CHANNEL_2 } +}; //change end + +/* Exported functions prototypes -------------------------------------------- */ +int8_t BSP_PWM_Start(BSP_PWM_Channel_t ch); +int8_t BSP_PWM_Set(BSP_PWM_Channel_t ch, float duty_cycle); +int8_t BSP_PWM_Stop(BSP_PWM_Channel_t ch); + +#ifdef __cplusplus +} +#endif + + + + + diff --git a/User/device/.DS_Store b/User/device/.DS_Store new file mode 100644 index 0000000..f792790 Binary files /dev/null and b/User/device/.DS_Store differ diff --git a/User/device/key_gpio.c b/User/device/key_gpio.c new file mode 100644 index 0000000..d15e240 --- /dev/null +++ b/User/device/key_gpio.c @@ -0,0 +1,86 @@ +/* Includes ----------------------------------------------------------------- */ +#include "key_gpio.h" +#include "device.h" +#include + +/* Private define ----------------------------------------------------------- */ +/* Private macro ------------------------------------------------------------ */ +/* Private typedef ---------------------------------------------------------- */ +/* Private variables -------------------------------------------------------- */ + +static uint32_t key_stats; // ʹλ¼ÿͨ״̬֧32LED + +/* ⲿ־λ־ʣ */ +extern volatile uint8_t key_flag; // 1=£0=ɿ + +/* ״̬ṹ */ +typedef struct { + uint16_t state; // ǰ״̬ + uint32_t press_timestamp; // ʱ + uint8_t last_flag; // һεı־λ +} Key_Status_t; + +static Key_Status_t key = { + .state = DEVICE_KEY_RELEASED, + .press_timestamp = 0, + .last_flag = 0 +}; + +/* жϴ */ +void Key_Process(void) { + uint32_t now = HAL_GetTick(); + uint8_t current_flag = key_flag; // ȡǰ־λ + + switch(key.state) { + case DEVICE_KEY_RELEASED: + if(current_flag == 1) { + // ״μ⵽£¼ʱ + if(key.last_flag == 0) { + key.press_timestamp = now; + } + // ȷϣ20msΪ£ + if((now - key.press_timestamp) > 20) { + key.state = DEVICE_KEY_PRESSED; + } + } + break; + + case DEVICE_KEY_PRESSED: + case DEVICE_KEY_LONG_PRESS: + if(current_flag == 1) { + // ס20msHOLD״̬ + if((now - key.press_timestamp) > 20) { + key.state = DEVICE_KEY_LONG_PRESS; + } + } else { + key.state = DEVICE_KEY_RELEASED; // ɿλ + } + break; + } + + key.last_flag = current_flag; // ʷ״̬ +} + +//ȡǰ״̬ +uint16_t Get_Key_State(void) { + return key.state; +} +//ҲǺ߼(ǰĴѭ +void KeyTask(void) +{ + + //¾ִ߼ +// if( key.state == DEVICE_KEY_PRESSED) +// { +// //ѭִ߼£ +// } + //סִ߼ɿͣ + if (key.state == DEVICE_KEY_LONG_PRESS) + { + //ִ߼ + } + else if (key.state == DEVICE_KEY_RELEASED) + { + //ֹͣ߼ + } +} \ No newline at end of file diff --git a/User/device/key_gpio.h b/User/device/key_gpio.h new file mode 100644 index 0000000..861279c --- /dev/null +++ b/User/device/key_gpio.h @@ -0,0 +1,38 @@ +#pragma once + +/* Includes ----------------------------------------------------------------- */ +#include +#include "main.h" +//#include "key_gpio.h" +/* Exported constants ------------------------------------------------------- */ +/* Exported macro ----------------------------------------------------------- */ +/* Exported types ----------------------------------------------------------- */ + +///* KEY״̬ */ +typedef enum +{ + DEVICE_KEY_RELEASED, //ͷ + DEVICE_KEY_PRESSED, // + DEVICE_KEY_LONG_PRESS, // +} DEVICE_KEY_Status_t; + + +/* ͨ */ +typedef enum { + DEVICE_KEY_1, + DEVICE_KEY_2, + /* ɸҪչ */ + DEVICE_KEY_COUNT +} DEVICE_Key_Channel_t; + +/* Ӳýṹ */ +typedef struct { + GPIO_TypeDef *port; // GPIO˿ + uint16_t pin; // ű + uint8_t active_level; // ЧƽGPIO_PIN_SET/RESET + uint32_t trigger_edge; // жϴأRISING/FALLING +} DEVICE_Key_Config_t; + +void Key_Process(void); +uint16_t Get_Key_State(void); +void KeyTask(void); \ No newline at end of file diff --git a/User/device/servo.c b/User/device/servo.c new file mode 100644 index 0000000..12841fe --- /dev/null +++ b/User/device/servo.c @@ -0,0 +1,36 @@ +/* Includes ----------------------------------------------------------------- */ +#include "main.h" +#include "servo.h" + +#include "bsp/servo_pwm.h" + + +/* Private define ----------------------------------------------------------- */ +#define MIN_CYCLE 0.5f //change begin +#define MAX_CYCLE 2.5f +#define ANGLE_LIMIT 180 //change end +/* Private macro ------------------------------------------------------------ */ +/* Private typedef ---------------------------------------------------------- */ +/* Private variables -------------------------------------------------------- */ +/* Private function -------------------------------------------------------- */ +/* Exported functions ------------------------------------------------------- */ +int serve_Init(BSP_PWM_Channel_t ch) +{ + if(BSP_PWM_Start(ch)!=0){ + return -1; + }else return 0; +} + + +int set_servo_angle(BSP_PWM_Channel_t ch,float angle) +{ + if (angle < 0.0f || angle > ANGLE_LIMIT) { + return -1; // ЧĽǶ + } + + float duty_cycle=MIN_CYCLE+(MAX_CYCLE-MIN_CYCLE)*(angle/ANGLE_LIMIT); + if(BSP_PWM_Set(ch,duty_cycle)!=0){ + return -1; + }else return 0; +} + diff --git a/User/device/servo.h b/User/device/servo.h new file mode 100644 index 0000000..97337df --- /dev/null +++ b/User/device/servo.h @@ -0,0 +1,41 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ----------------------------------------------------------------- */ +#include +#include "tim.h" +#include "bsp/bsp.h" +#include "bsp/servo_pwm.h" +/* Exported constants ------------------------------------------------------- */ +/* Exported macro ----------------------------------------------------------- */ +/* Exported types ----------------------------------------------------------- */ + + + +extern int serve_Init(BSP_PWM_Channel_t ch); +extern int set_servo_angle(BSP_PWM_Channel_t ch,float angle); + + + + + + + + + + + + + + +#ifdef __cplusplus +} +#endif + + + + +