Files
Motor_v2/Robot_V2/sys/initial.cpp
T
cbb 9f84c85a28 feat(can): CAN休眠唤醒移动到BOOT以进行以支持40k硬件看门狗
- 更新深度睡眠实现,在跳转到bootloader前记录睡眠状态到Flash
- 添加gd32c10x_fmc.h头文件以支持Flash内存控制功能
- 将prepare_deep_sleep重命名为app_prepare_deep_sleep以提高命名清晰度
- 在CAN通信检测中添加应用层休眠准备功能
- 在接收OTA启动ID时解锁FMC、设置APP为非激活状态并锁定FMC
- 移除main.cpp中的看门狗复位休眠逻辑
- 在config.h中添加睡眠配置相关的常量定义
- 改进OTA_SET函数的Flash写入逻辑,优化数据更新流程
- 在OTA初始化和应用移动过程中添加FMC解锁锁定操作
2026-01-12 01:03:43 +08:00

130 lines
3.3 KiB
C++

#include "initial.h"
#include "Can.h"
#include "config.h"
#include "flash.h"
#include "gd32c10x.h"
#include "gd32c10x_fmc.h"
#include "gd32c10x_gpio.h"
#include "gd32c10x_rcu.h"
#include <cstdio>
#include <stdint.h>
#include <stdio.h>
extern CAN_transmiter CAN0_transmiter; // CAN初始化
/* retarget the C library printf function to the USART */
u8 USART0_init() {
gd_eval_com_init(EVAL_COM0);
return 1;
}
/*!
\brief configure the watchdog peripheral
\param[in] none
\param[out] none
\retval none
*/
u8 watchdog_init(u32 time) {
u8 status = 0;
/* configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
/* time = reload_value / clock_frequency */
status = fwdgt_config(time * 0.625, FWDGT_PSC_DIV64);
fwdgt_enable();
return status;
}
int fputc(int ch, FILE *f) throw() {
usart_data_transmit(EVAL_COM0, (uint8_t)ch);
while (RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE))
;
return ch;
}
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);
}
/*!
\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;
}
void OTA_initial(void) {
// 确认当前APP可用
fmc_unlock();
OTA_SET(n_APP_LATEST_CHECK_ADDR, OTA_AVAL);
OTA_SET(n_APP_LATEST_EN_ADDR, OTA_AVAL);
fmc_lock();
u8 transmit_data[2] = {0x0A, 0xAC};
// 确认APP可用
CAN0_transmiter.CANFD_AsyncSend(CANFD_OTA_ACK_ID, 2, transmit_data);
volatile u32 *flash_ptr = (volatile u32 *)n_APP_BACKUP_AVAL_ADDR;
u32 back_aval = *flash_ptr;
flash_ptr = (volatile u32 *)n_APP_BACKUP_CHECK_ADDR;
u32 back_check = *flash_ptr;
// 如果BACK不可用
if (back_aval == OTA_NOT_AVAL) {
#if OTA_DEBUG
std::printf("notaval\r\n");
#endif
move_app_data();
} else if (back_aval == OTA_AVAL &&
back_check == OTA_NOT_AVAL) { // BACK已刷写,但未检查
#if OTA_DEBUG
std::printf("aval\r\n");
#endif
move_app_data();
} else {
printf("check\r\n");
}
}