feat(can): 增加CAN睡眠与唤醒功能并优化通信检查逻辑
- 在CAN通信类中添加了CAN_Sleep()和CAN_Wakeup()方法,支持设备进入低功耗模式 - 优化CAN通信检查逻辑,通过解析接收数据的首字节和末字节实现多种控制命令 - 支持远程复位电机、保存角度、清零角度、进入睡眠等操作 - 解决CAN发送邮箱满时可能导致的通信阻塞问题,增加重初始化机制 - 调整mailbox不可用计数逻辑,防止持续打印错误信息 refactor(flash): 移除软件CRC计算模块,改用硬件CRC外设 - 删除自定义CRC.cpp和CRC.h文件及其相关类实现 - 使用GD32硬件CRC模块替代软件算法,提高效率和可靠性 - 更新FlashEEPROM类中的CRC校验方式,调用crc_block_data_calculate进行计算 - 修正Flash地址遍历范围以适配新CRC逻辑及页面管理策略 - 修改save_angle接口签名,不再手动传入角度值,改为内部读取FOC实时角度 feat(timer): 新增RTC闹钟中断处理函数声明 - 在Timer.h中声明rtc_set_wakeup_interval和RTC_ALARM_IRQHandler - 为后续实现基于RTC的定时唤醒功能做准备 chore(config): 同步csolution工具版本更新并调整组件配置 - 更新RTE_Components.h生成工具版本至2.12.0 - 启用BKP、CRC、RTC等标准外设组件宏定义 - 从项目配置文件中移除旧CRC源文件引用 - 添加SystemCoreClockUpdate调用确保系统时钟同步 refactor(main): 主循环任务调度重构并增强初始化流程 - 将主循环中的任务处理提取为独立do_task函数,提升代码可读性 - 提前执行电机初始化,并根据看门狗复位标志决定是否立即进入深度睡眠 - 调整EEPROM初始化顺序,保证依赖关系正确 - 添加sys/initial.h头文件包含以支持新增系统初始化逻辑
This commit is contained in:
+43
-28
@@ -1,11 +1,11 @@
|
||||
#include "flash.h"
|
||||
#include "DengFOC.h"
|
||||
#include "ang_sensor.h"
|
||||
#include "core_cmFunc.h"
|
||||
#include "gd32c10x_crc.h"
|
||||
#include "gd32c10x_fmc.h"
|
||||
#include "math.h"
|
||||
#include <CRC.h>
|
||||
#include <Can.h>
|
||||
#include <Filter.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
@@ -15,7 +15,6 @@ FlashEEPROM flashEEP = FlashEEPROM();
|
||||
extern angle_sensor KTH7823;
|
||||
extern CAN_transmiter CAN0_transmiter;
|
||||
extern FOC_Controlroller FOC; // FOC控制
|
||||
extern CRC_Calculator CRC_Calculator;
|
||||
constexpr u8 SIZE_U32 = sizeof(uint32_t);
|
||||
constexpr u8 DEFAULT_VALUE = 0;
|
||||
constexpr u8 NUM_SAVED = 5;
|
||||
@@ -31,12 +30,15 @@ FlashEEPROM::FlashEEPROM() {
|
||||
* @retval None
|
||||
*/
|
||||
u8 FlashEEPROM::init() {
|
||||
rcu_periph_clock_enable(RCU_CRC);
|
||||
// 使用硬件CRC计算校验值
|
||||
crc_deinit(); // 初始化CRC模块
|
||||
|
||||
// 读取保存的参数
|
||||
u32 CRC_NUM = 0x0000;
|
||||
FlashEepromStatus status_read[NUM_SAVED + 1] = {FLASH_EEPROM_OK};
|
||||
FlashEepromStatus status_read[NUM_SAVED] = {FLASH_EEPROM_OK};
|
||||
float angle = 0.0f;
|
||||
float data[NUM_SAVED] = {FOC.target_angle, FOC.target_velocity,
|
||||
FOC.limit_voltage, (float)FOC.control_mode};
|
||||
float data[NUM_SAVED - 1] = {0.0f, 0.0f, 0.0f, 0.0f};
|
||||
FlashEepromStatus status = FLASH_EEPROM_OK;
|
||||
if (readFloat(ADDR_ANGLE, &angle) != FLASH_EEPROM_OK) {
|
||||
angle = DEFAULT_VALUE; // 默认值
|
||||
@@ -49,7 +51,7 @@ u8 FlashEEPROM::init() {
|
||||
status_read[4] = readFloat(ADDR_CONTROL_MODE, &data[3]);
|
||||
|
||||
// 读取失败则使用默认值
|
||||
for (u8 i = 1; i < NUM_SAVED + 1; i++) {
|
||||
for (u8 i = 1; i < NUM_SAVED; i++) {
|
||||
if (status_read[i] ==
|
||||
FLASH_EEPROM_DATA_NOT_FOUND) { // 首次读取,不保存报错
|
||||
data[i - 1] = DEFAULT_VALUE;
|
||||
@@ -62,8 +64,8 @@ u8 FlashEEPROM::init() {
|
||||
if (status_read[0] == FLASH_EEPROM_OK) {
|
||||
u32 next_address = EEPROM_END_ADDRESS;
|
||||
// 遍历Flash页面寻找下一个可写入位置
|
||||
for (u32 address = EEPROM_END_ADDRESS - SIZE_U32;
|
||||
address > EEPROM_START_ADDRESS; address -= SIZE_U32) {
|
||||
for (u32 address = EEPROM_START_ADDRESS - SIZE_U32;
|
||||
address > FLASH_START_ADDRESS; address -= SIZE_U32) {
|
||||
|
||||
// 安全读取Flash数据
|
||||
volatile u32 *flash_ptr = (volatile u32 *)address;
|
||||
@@ -76,8 +78,13 @@ u8 FlashEEPROM::init() {
|
||||
// 更新下一个可能的写入位置(如果需要格式化)
|
||||
next_address = address - SIZE_U32;
|
||||
}
|
||||
CRC_NUM = CRC_Calculator::calculate(data, NUM_SAVED);
|
||||
|
||||
crc_data_register_reset(); // 确保CRC计算从初始状态开始
|
||||
CRC_NUM = crc_block_data_calculate((uint32_t *)data, NUM_SAVED);
|
||||
// for (float &p : data) {
|
||||
// printf("%f\r\n", p);
|
||||
// }
|
||||
// printf("CRC cmp1,0x%X_0x%x \r\n", CRC_NUM, *((u32 *)next_address));
|
||||
if (CRC_NUM != *((u32 *)next_address)) { // CRC错误
|
||||
for (u8 i = 0; i < NUM_SAVED; i++) {
|
||||
data[i] = DEFAULT_VALUE;
|
||||
@@ -91,7 +98,7 @@ u8 FlashEEPROM::init() {
|
||||
EEPROMStatus = status_read[0];
|
||||
}
|
||||
|
||||
KTH7823.angle_set(angle);
|
||||
KTH7823.angle_set(angle - KTH7823.update());
|
||||
FOC.target_angle = data[0];
|
||||
FOC.target_velocity = data[1];
|
||||
FOC.limit_voltage = data[2];
|
||||
@@ -149,10 +156,12 @@ FlashEepromStatus FlashEEPROM::readFloat(u8 virtualAddr, float *data) const {
|
||||
u8 addr = (u8)(data_word & 0xFF);
|
||||
// 找到匹配的虚拟地址
|
||||
if (addr == virtualAddr) {
|
||||
u32 data_word_c = *((u32 *)address);
|
||||
// 清除最低的8位地址信息,保留高24位数据
|
||||
u32 float_data = data_word & 0xFFFFFF00;
|
||||
// 使用内存拷贝将处理后的数据解释为float
|
||||
memcpy(data, &float_data, sizeof(float));
|
||||
// printf("check:0x%x=%f\r\n", address, *data);
|
||||
return FLASH_EEPROM_OK;
|
||||
}
|
||||
}
|
||||
@@ -255,8 +264,6 @@ FlashEepromStatus FlashEEPROM::writeFloat(u8 virtualAddr, float data) {
|
||||
|
||||
// 如果有足够空间,直接写入
|
||||
if (next_address < EEPROM_END_ADDRESS) {
|
||||
u32 float_bits;
|
||||
|
||||
u32 data_word;
|
||||
// 将完整的float数据复制到32位变量中
|
||||
memcpy(&data_word, &data, sizeof(float));
|
||||
@@ -271,6 +278,11 @@ FlashEepromStatus FlashEEPROM::writeFloat(u8 virtualAddr, float data) {
|
||||
fmc_state_enum fmc_status = fmc_word_program(next_address, data_word);
|
||||
|
||||
fmc_lock();
|
||||
|
||||
// u32 data_word_c = *((u32 *)next_address);
|
||||
// float float_data = *((float *)&data_word_c);
|
||||
// printf("check:0x%x=%f\r\n", next_address, float_data);
|
||||
|
||||
if (fmc_status != FMC_READY) {
|
||||
return FLASH_EEPROM_WRITE_ERROR;
|
||||
}
|
||||
@@ -283,8 +295,6 @@ FlashEepromStatus FlashEEPROM::writeFloat(u8 virtualAddr, float data) {
|
||||
}
|
||||
|
||||
// 格式化后在页面开头写入数据
|
||||
u32 float_bits;
|
||||
|
||||
u32 data_word;
|
||||
// 将完整的float数据复制到32位变量中
|
||||
memcpy(&data_word, &data, sizeof(float));
|
||||
@@ -345,13 +355,12 @@ FlashEepromStatus FlashEEPROM::format() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmc_unlock();
|
||||
// 擦除整个页面
|
||||
fmc_status = fmc_page_erase(EEPROM_START_ADDRESS);
|
||||
if (fmc_status != FMC_READY) {
|
||||
return FLASH_EEPROM_ERASE_ERROR;
|
||||
}
|
||||
fmc_unlock();
|
||||
// 重新写入有效的数据项
|
||||
for (u16 i = 0; i < valid_count; i++) {
|
||||
fmc_status = fmc_word_program(EEPROM_START_ADDRESS + i * SIZE_U32,
|
||||
@@ -371,10 +380,10 @@ FlashEepromStatus FlashEEPROM::format() {
|
||||
*/
|
||||
FlashEepromStatus FlashEEPROM::save_system_config(bool saved) {
|
||||
u32 CRC_NUM = 0x0000;
|
||||
u32 next_address;
|
||||
FlashEepromStatus status_save[NUM_SAVED] = {FLASH_EEPROM_OK};
|
||||
float data[NUM_SAVED] = {FOC.target_angle, FOC.target_velocity,
|
||||
FOC.limit_voltage, (float)FOC.control_mode};
|
||||
static u32 next_address;
|
||||
FlashEepromStatus status_save[NUM_SAVED - 1] = {FLASH_EEPROM_OK};
|
||||
float data[NUM_SAVED - 1] = {FOC.target_angle, FOC.target_velocity,
|
||||
FOC.limit_voltage, (float)FOC.control_mode};
|
||||
|
||||
// 读取保存的参数
|
||||
if (!system_config_saved) {
|
||||
@@ -390,11 +399,12 @@ FlashEepromStatus FlashEEPROM::save_system_config(bool saved) {
|
||||
}
|
||||
}
|
||||
|
||||
CRC_NUM = CRC_Calculator.calculate(data, NUM_SAVED);
|
||||
// 遍历Flash页面寻找下一个可写入位置
|
||||
for (u32 address = EEPROM_END_ADDRESS - SIZE_U32;
|
||||
address > EEPROM_START_ADDRESS; address -= SIZE_U32) {
|
||||
crc_data_register_reset(); // 确保CRC计算从初始状态开始
|
||||
CRC_NUM = crc_block_data_calculate((uint32_t *)data, NUM_SAVED);
|
||||
|
||||
// 遍历Flash页面寻找下一个可写入位置
|
||||
for (u32 address = EEPROM_START_ADDRESS - SIZE_U32;
|
||||
address > FLASH_START_ADDRESS; address -= SIZE_U32) {
|
||||
// 安全读取Flash数据
|
||||
volatile u32 *flash_ptr = (volatile u32 *)address;
|
||||
u32 data_word = *flash_ptr;
|
||||
@@ -407,16 +417,20 @@ FlashEepromStatus FlashEEPROM::save_system_config(bool saved) {
|
||||
next_address = address - SIZE_U32;
|
||||
}
|
||||
// 如果有足够空间,直接写入
|
||||
if (next_address > EEPROM_START_ADDRESS) {
|
||||
if (next_address > FLASH_START_ADDRESS) {
|
||||
fmc_unlock();
|
||||
fmc_flag_clear(FMC_FLAG_PGERR | FMC_FLAG_PGAERR | FMC_FLAG_WPERR);
|
||||
fmc_state_enum fmc_status = fmc_word_program(next_address, CRC_NUM);
|
||||
// printf("write address:0x%X,status:0x%x \r\n", next_address,
|
||||
// fmc_status);
|
||||
fmc_lock();
|
||||
// for (float &p : data) {
|
||||
// printf("%f\r\n", p);
|
||||
// }
|
||||
// printf("CRC cmp1,0x%X_0x%x \r\n", CRC_NUM, *((u32
|
||||
// *)next_address));
|
||||
fmc_lock();
|
||||
} else {
|
||||
printf("no enough space\r\n");
|
||||
// 空间不足,需要格式化页面
|
||||
FlashEepromStatus status = format();
|
||||
if (status != FLASH_EEPROM_OK) {
|
||||
@@ -452,8 +466,9 @@ FlashEepromStatus FlashEEPROM::save_system_config(bool saved) {
|
||||
* @param angle 角度数据
|
||||
* @return FlashEepromStatus: 操作状态
|
||||
*/
|
||||
FlashEepromStatus FlashEEPROM::save_angle(float angle, bool saved) {
|
||||
FlashEepromStatus FlashEEPROM::save_angle(bool saved) {
|
||||
float angle_read = DEFAULT_VALUE;
|
||||
float angle = FOC.rt_angle;
|
||||
FlashEepromStatus status = FLASH_EEPROM_OK;
|
||||
|
||||
// 读取保存的参数
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#ifndef __FLASH_H
|
||||
#define __FLASH_H
|
||||
|
||||
#include "gd32c10x.h"
|
||||
#include <cstdint>
|
||||
#include <stdint.h>
|
||||
// 修改后 - 强制4字节对齐
|
||||
#define ALIGN_4BYTE(addr) ((addr) & 0xFFFFFFFC) // 4字节对齐
|
||||
@@ -24,9 +22,10 @@ class FlashEEPROM {
|
||||
public:
|
||||
// EEPROM emulation constants
|
||||
static constexpr u32 EEPROM_START_ADDRESS =
|
||||
0x0801F800; // Flash最后一页(2KB)
|
||||
static constexpr u32 EEPROM_END_ADDRESS = 0x08020000; // Flash结束地址
|
||||
static constexpr u32 EEPROM_MAX_SIZE = 0x800; // EEPROM最大容量
|
||||
0x0801F800; // Flash最后一页(2KB)
|
||||
static constexpr u32 EEPROM_END_ADDRESS = 0x08020000; // Flash结束地址
|
||||
static constexpr u32 EEPROM_MAX_SIZE = 0x800; // EEPROM最大容量
|
||||
static constexpr u32 FLASH_START_ADDRESS = 0x08010000; // Flash起始地址
|
||||
|
||||
// 定义用户自定义的虚拟地址
|
||||
static constexpr u8 ADDR_CONTROL_MODE = 0x0; // 控制模式
|
||||
@@ -56,7 +55,7 @@ class FlashEEPROM {
|
||||
// 格式化Flash EEPROM页面
|
||||
FlashEepromStatus format();
|
||||
|
||||
FlashEepromStatus save_angle(float angle, bool saved);
|
||||
FlashEepromStatus save_angle(bool saved);
|
||||
FlashEepromStatus save_system_config(bool saved);
|
||||
float read_angle();
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ void power_manage(void) {
|
||||
float vbus = sampler.get_vbus_voltage();
|
||||
if ((vbus < 22.0f) and (last_vbus > vbus) and vbus > 8.0f) {
|
||||
FOC.shut_down();
|
||||
flashEEP.save_angle(FOC.rt_angle, true);
|
||||
flashEEP.save_angle(true);
|
||||
flashEEP.save_system_config(true);
|
||||
} else {
|
||||
FOC.resume();
|
||||
|
||||
+56
-11
@@ -1,9 +1,10 @@
|
||||
#include "initial.h"
|
||||
#include "gd32c10x.h"
|
||||
#include "gd32c10x_gpio.h"
|
||||
#include "gd32c10x_rcu.h"
|
||||
#include <cstdio>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/* retarget the C library printf function to the USART */
|
||||
u8 USART0_init() {
|
||||
@@ -34,15 +35,59 @@ int fputc(int ch, FILE *f) throw() {
|
||||
return ch;
|
||||
}
|
||||
|
||||
|
||||
void unused_pin_init() {
|
||||
void unused_pin_init() {
|
||||
rcu_periph_clock_enable(RCU_GPIOA); // 使能GPIOA时钟
|
||||
rcu_periph_clock_enable(RCU_GPIOB); // 使能GPIOB时钟
|
||||
rcu_periph_clock_enable(RCU_GPIOC); // 使能GPIOC时钟
|
||||
gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, GPIO_PIN_8|GPIO_PIN_11|GPIO_PIN_12);
|
||||
gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_15);
|
||||
gpio_init(GPIOC, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
|
||||
gpio_bit_reset(GPIOA, GPIO_PIN_8|GPIO_PIN_11|GPIO_PIN_12);
|
||||
gpio_bit_reset(GPIOB, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_15);
|
||||
gpio_bit_reset(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
|
||||
}
|
||||
gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ,
|
||||
GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12);
|
||||
gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ,
|
||||
GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_15);
|
||||
gpio_init(GPIOC, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ,
|
||||
GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
|
||||
gpio_bit_reset(GPIOA, GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12);
|
||||
gpio_bit_reset(GPIOB, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_15);
|
||||
gpio_bit_reset(GPIOC, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 检测并打印复位源信息
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
u16 get_reset_cause(void) {
|
||||
u16 cause = 0;
|
||||
if (RESET != rcu_flag_get(RCU_FLAG_FWDGTRST)) {
|
||||
// printf("Free Watchdog Timer Reset\r\n");
|
||||
cause = RCU_FLAG_FWDGTRST;
|
||||
}
|
||||
if (RESET != rcu_flag_get(RCU_FLAG_EPRST)) {
|
||||
// printf("External Pin Reset\r\n");
|
||||
cause = RCU_FLAG_EPRST;
|
||||
}
|
||||
|
||||
if (RESET != rcu_flag_get(RCU_FLAG_PORRST)) {
|
||||
// printf("Power Reset\r\n");
|
||||
cause = RCU_FLAG_PORRST;
|
||||
}
|
||||
|
||||
if (RESET != rcu_flag_get(RCU_FLAG_WWDGTRST)) {
|
||||
// printf("Window Watchdog Timer Reset\r\n");
|
||||
cause = RCU_FLAG_WWDGTRST;
|
||||
}
|
||||
|
||||
if (RESET != rcu_flag_get(RCU_FLAG_LPRST)) {
|
||||
// printf("Low Power Reset\r\n");
|
||||
cause = RCU_FLAG_LPRST;
|
||||
}
|
||||
|
||||
if (RESET != rcu_flag_get(RCU_FLAG_SWRST)) {
|
||||
// printf("Software Reset\r\n");
|
||||
cause = RCU_FLAG_SWRST;
|
||||
}
|
||||
// 清除所有复位标志
|
||||
rcu_all_reset_flag_clear();
|
||||
// printf("cause:%d\r\n", cause);
|
||||
return cause;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "gd32c10x_eval.h"
|
||||
#include "stdint.h"
|
||||
#include <stdio.h>
|
||||
@@ -19,6 +20,7 @@ int fputc(int ch, FILE *f); // 声明fputc函数以重载标准库实现
|
||||
|
||||
u8 USART0_init(void);
|
||||
u8 watchdog_init(u32 time);
|
||||
u16 get_reset_cause(void);
|
||||
void unused_pin_init();
|
||||
// 任务标志定义
|
||||
typedef enum {
|
||||
|
||||
+56
-14
@@ -33,11 +33,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "systick.h"
|
||||
#include "Can.h"
|
||||
#include "flash.h"
|
||||
#include "gd32c10x.h"
|
||||
#include "gd32c10x_exti.h"
|
||||
#include "gd32c10x_gpio.h"
|
||||
#include "motor.h"
|
||||
#include <cstdio>
|
||||
|
||||
volatile static uint32_t delay;
|
||||
extern CAN_transmiter CAN0_transmiter;
|
||||
extern FlashEEPROM flashEEP;
|
||||
|
||||
/*!
|
||||
\brief configure systick
|
||||
@@ -113,27 +118,64 @@ void system_soft_reset(void) {
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 进入深度睡眠前的准备
|
||||
void prepare_deep_sleep(void) {
|
||||
/*!
|
||||
\brief prepare system to enter deep sleep mode
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void prepare_deep_sleep(bool save_data) {
|
||||
if (save_data) {
|
||||
flashEEP.save_angle(true);
|
||||
flashEEP.save_system_config(true);
|
||||
}
|
||||
// 将SIT1044设置为低功耗待机模式
|
||||
gpio_bit_set(GPIOB, GPIO_PIN_8); // STB = 高电平
|
||||
// CAN0_transmiter.CAN_Sleep();
|
||||
motor_sleep();
|
||||
|
||||
// 配置EXTI中断(连接到SIT1044的RXD引脚)
|
||||
exti_init(EXTI_1, EXTI_INTERRUPT, EXTI_TRIG_FALLING); // 下降沿触发
|
||||
// 关闭不必要的外设时钟
|
||||
rcu_periph_clock_disable(RCU_TIMER0); // 关闭定时器时钟
|
||||
rcu_periph_clock_disable(RCU_TIMER1);
|
||||
rcu_periph_clock_disable(RCU_SPI0); // 关闭SPI时钟
|
||||
rcu_periph_clock_disable(RCU_I2C0); // 关闭I2C时钟
|
||||
rcu_periph_clock_disable(RCU_ADC0); // 关闭ADC时钟
|
||||
// 注意不要关闭GPIOB和AF的时钟,因为需要它们来处理唤醒中断
|
||||
|
||||
// 设置深睡眠模式下的电压以进一步降低功耗
|
||||
rcu_deepsleep_voltage_set(RCU_DEEPSLEEP_V_0_8); // 设置最低电压
|
||||
|
||||
// 配置PB8为输入模式,用于外部中断
|
||||
rcu_periph_clock_enable(RCU_GPIOB);
|
||||
rcu_periph_clock_enable(RCU_AF);
|
||||
gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
|
||||
|
||||
// 配置EXTI中断(连接到PB8引脚)
|
||||
gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB,
|
||||
GPIO_PIN_SOURCE_8); // 配置GPIOB的PIN8作为EXTI8的源
|
||||
exti_init(EXTI_8, EXTI_INTERRUPT, EXTI_TRIG_RISING); // 边沿触发
|
||||
exti_interrupt_enable(EXTI_8);
|
||||
nvic_irq_enable(EXTI5_9_IRQn, 0, 0); //
|
||||
|
||||
// 确保中断标志已被清除
|
||||
exti_interrupt_flag_clear(EXTI_8);
|
||||
|
||||
// 进入深度睡眠
|
||||
pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);
|
||||
}
|
||||
|
||||
// 2. 唤醒中断处理
|
||||
void EXTI1_IRQHandler(void) {
|
||||
if (exti_interrupt_flag_get(EXTI_1) != RESET) {
|
||||
/**
|
||||
* @brief EXTI5_9中断处理函数
|
||||
* @param 无
|
||||
* @retval 无
|
||||
*/
|
||||
void EXTI5_9_IRQHandler(void) {
|
||||
if (exti_interrupt_flag_get(EXTI_8) != RESET) {
|
||||
// 清除中断标志
|
||||
exti_interrupt_flag_clear(EXTI_1);
|
||||
// 禁用EXTI线1的中断
|
||||
exti_interrupt_disable(EXTI_1);
|
||||
exti_interrupt_flag_clear(EXTI_8);
|
||||
// 禁用EXTI线8的中断,防止重复触发
|
||||
exti_interrupt_disable(EXTI_8);
|
||||
|
||||
// 唤醒SIT1044到正常模式
|
||||
gpio_bit_reset(GPIOB, GPIO_PIN_8); // STB = 低电平
|
||||
printf("MCU wake up\r\n");
|
||||
system_soft_reset();
|
||||
}
|
||||
}
|
||||
@@ -55,9 +55,9 @@ uint8_t get_cpu_usage(void);
|
||||
/* 系统软复位 */
|
||||
void system_soft_reset(void);
|
||||
/* 准备进入低功耗模式 */
|
||||
void prepare_deep_sleep(void);
|
||||
void prepare_deep_sleep(bool save_data);
|
||||
/* 中断唤醒 */
|
||||
void EXTI1_IRQHandler(void);
|
||||
void EXTI5_9_IRQHandler(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user