feat(bootloader): 添加了完整的系统休眠和唤醒机制,包括:
- 实现深睡眠模式下的功耗优化 - 添加PB8引脚的外部中断唤醒功能 - 新增Flash区域用于睡眠状态管理 - 实现睡眠配置的自动检查和恢复逻辑 同时重构了OTA_SET函数,改进了Flash配置参数的写入机制, 解决了GD32 Flash特性导致的0到1写入问题,并添加了详细的 调试信息输出和错误处理。
This commit is contained in:
+165
-2
@@ -1,11 +1,17 @@
|
||||
#include "initial.h"
|
||||
#include "OTA.h"
|
||||
#include "config.h"
|
||||
#include "core_cmFunc.h"
|
||||
#include "gd32c10x.h"
|
||||
#include "gd32c10x_gpio.h"
|
||||
#include "gd32c10x_rcu.h"
|
||||
#include "main.h"
|
||||
#include "systick.h"
|
||||
#include <cstdio>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include "config.h"
|
||||
|
||||
extern BootloaderSystem sys;
|
||||
|
||||
/* retarget the C library printf function to the USART */
|
||||
u8 USART0_init() {
|
||||
@@ -36,7 +42,6 @@ void unused_pin_init() {
|
||||
gpio_bit_reset(GPIOC, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 扫描APP地址范围内的Flash内容
|
||||
* @param print_content 是否打印Flash内容,默认为true
|
||||
@@ -134,6 +139,164 @@ void scanAppFlashContent(bool print_content = true, u32 page_count = 0) {
|
||||
#endif
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief prepare system to enter deep sleep mode
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void enter_deep_sleep() {
|
||||
// 关闭不必要的外设时钟
|
||||
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);
|
||||
__enable_irq();
|
||||
delay_1ms(10);
|
||||
// 进入深度睡眠
|
||||
pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 写地址
|
||||
* @return fmc_state_enum
|
||||
*/
|
||||
fmc_state_enum write_ADDRESS(u32 address, u32 data) {
|
||||
fmc_unlock();
|
||||
fmc_flag_clear(FMC_FLAG_PGERR | FMC_FLAG_PGAERR | FMC_FLAG_WPERR);
|
||||
|
||||
// 确保使用对齐地址
|
||||
fmc_state_enum fmc_status = fmc_word_program(address, data);
|
||||
|
||||
fmc_lock();
|
||||
return fmc_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief EXTI5_9中断处理函数
|
||||
* @param 无
|
||||
* @retval 无
|
||||
*/
|
||||
void EXTI5_9_IRQHandler(void) {
|
||||
if (exti_interrupt_flag_get(EXTI_8) != RESET) {
|
||||
// 清除中断标志
|
||||
exti_interrupt_flag_clear(EXTI_8);
|
||||
// 禁用EXTI线8的中断,防止重复触发
|
||||
exti_interrupt_disable(EXTI_8);
|
||||
#if SLEEP_DEBUG
|
||||
printf("MCU wake up\r\n");
|
||||
#endif
|
||||
OTA_SET(n_APP_LATEST_EN_ADDR, OTA_AVAL); // 设置APP为不激活
|
||||
// 遍历睡眠存储区
|
||||
for (u32 address = SLEEP_CONFIG_END - SIZE_U32;
|
||||
address >= SLEEP_CONFIG_START; address -= SIZE_U32) {
|
||||
volatile u32 *flash_ptr = (volatile u32 *)address;
|
||||
u32 sleep_check = *flash_ptr;
|
||||
if (sleep_check == FLASH_EMPTY and
|
||||
address > SLEEP_CONFIG_START) { // 空则跳过该次循环
|
||||
continue;
|
||||
} else if (
|
||||
sleep_check == SLEEP_ENABLE and
|
||||
address >
|
||||
SLEEP_CONFIG_START) { // 如果检查到睡眠启用,写入非启用
|
||||
#if SLEEP_DEBUG
|
||||
printf("boot sleep found %08x at %08x\r\n", sleep_check,
|
||||
address);
|
||||
#endif
|
||||
if (address == SLEEP_CONFIG_END - SIZE_U32) {
|
||||
fmc_unlock();
|
||||
fmc_state_enum fmc_status =
|
||||
fmc_page_erase(SLEEP_CONFIG_START); // 擦除已满的页
|
||||
if (fmc_status != FMC_READY) { // 擦除失败报错
|
||||
fmc_lock();
|
||||
return;
|
||||
}
|
||||
address = SLEEP_CONFIG_START - SIZE_U32; // 重新开始写入
|
||||
}
|
||||
fmc_state_enum fmc_status =
|
||||
write_ADDRESS(address + SIZE_U32, SLEEP_DISABLE);
|
||||
if (fmc_status != FMC_READY) { // 写入失败报错
|
||||
;
|
||||
}
|
||||
break; // 完成,跳出遍历
|
||||
} else if (sleep_check == SLEEP_DISABLE) { // 已经是非休眠则跳出遍历
|
||||
break;
|
||||
} else {
|
||||
fmc_state_enum fmc_status =
|
||||
write_ADDRESS(address, SLEEP_DISABLE);
|
||||
if (fmc_status != FMC_READY) { // 写入失败报错
|
||||
;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 唤醒后软重启,确保正常初始化
|
||||
system_soft_reset();
|
||||
}
|
||||
}
|
||||
|
||||
int check_sleep() {
|
||||
#if SLEEP_DEBUG
|
||||
printf("sleep check\r\n");
|
||||
#endif
|
||||
for (u32 address = SLEEP_CONFIG_END - SIZE_U32;
|
||||
address >= SLEEP_CONFIG_START; address -= SIZE_U32) { // 遍历睡眠存储区
|
||||
volatile u32 *flash_ptr = (volatile u32 *)address;
|
||||
u32 sleep_check = *flash_ptr;
|
||||
#if SLEEP_DEBUG
|
||||
printf("sleep check %8x-----%x\r\n", address, sleep_check);
|
||||
#endif
|
||||
if (sleep_check == FLASH_EMPTY) { // 空则跳过
|
||||
continue;
|
||||
} else if (sleep_check == SLEEP_ENABLE) { // 睡眠使能则继续休眠
|
||||
#if SLEEP_DEBUG
|
||||
printf("sleep enable\r\n");
|
||||
#endif
|
||||
enter_deep_sleep();
|
||||
return 0;
|
||||
} else if (sleep_check == SLEEP_DISABLE) { // 禁用睡眠则跳出
|
||||
#if SLEEP_DEBUG
|
||||
printf("sleep disable\r\n");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// 遍历到头都未找到,写入第一次的数据
|
||||
#if SLEEP_DEBUG
|
||||
printf("boot sleep not found\r\n");
|
||||
#endif
|
||||
fmc_state_enum fmc_status =
|
||||
write_ADDRESS(SLEEP_CONFIG_START, SLEEP_DISABLE);
|
||||
if (fmc_status != FMC_READY) { // 写入失败报错
|
||||
#if SLEEP_DEBUG
|
||||
printf("boot sleep write error\r\n");
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
return 2; // 第一次启动
|
||||
}
|
||||
|
||||
// // 发送单个字节到串口 (用于调试)
|
||||
// void uart_send_byte(uint8_t data) {
|
||||
// // 实际的串口发送代码
|
||||
|
||||
Reference in New Issue
Block a user