This repository has been archived on 2025-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
controller-hd/User/system/src/sys.c

166 lines
3.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Author:
* @Date: 2023-04-11 18:46:58
* @LastEditors: xxx
* @LastEditTime: 2023-08-25 11:31:06
* @Description:
* email:
* Copyright (c) 2023 by xxx, All Rights Reserved.
*/
#include "sys.h"
__IO uint32_t uwTick;
__IO uint32_t scheduler_start_time; // 调度器开始时间
__IO uint32_t scheduler_end_time; // 调度器结束时间
__IO uint32_t scheduler_occupancy_time = 0; // 调度器占用时间
/**
* @brief 设置中断向量表偏移地址
* @param baseaddr : 基址
* @param offset : 偏移量
* @retval 无
*/
void sys_nvic_set_vector_table(uint32_t baseaddr, uint32_t offset)
{
/* 设置NVIC的向量表偏移寄存器,VTOR低9位保留,即[8:0]保留 */
SCB->VTOR = baseaddr | (offset & (uint32_t)0xFFFFFE00);
}
/**
* @brief 执行: WFI指令(执行完该指令进入低功耗状态, 等待中断唤醒)
* @param 无
* @retval 无
*/
void sys_wfi_set(void)
{
__ASM __IO("wfi");
}
/**
* @brief 关闭所有中断(但是不包括fault和NMI中断)
* @param 无
* @retval 无
*/
void sys_intx_disable(void)
{
__ASM __IO("cpsid i");
}
/**
* @brief 开启所有中断
* @param 无
* @retval 无
*/
void sys_intx_enable(void)
{
__ASM __IO("cpsie i");
}
/**
* @brief 设置栈顶地址
* @note 左侧若有红X, 属于MDK误报, 实际是没问题的
* @param addr: 栈顶地址
* @retval 无
*/
void sys_msr_msp(uint32_t addr)
{
__set_MSP(addr); /* 设置栈顶地址 */
}
/**
* @brief 进入待机模式
* @param 无
* @retval 无
*/
void sys_standby(void)
{
// LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR); /* 使能电源时钟 */
// SET_BIT(PWR->CR, PWR_CR_PDDS); /* 进入待机模式 */
}
/**
* @brief 系统软复位
* @param 无
* @retval 无
*/
void sys_soft_reset(void)
{
NVIC_SystemReset();
}
/**
* @brief 10ms滴答定时器,需要单独放到定时器中断中执行
* @param 无
* @retval 提供给sys_millis()函数使用,之前放在SysTick_Handler中执行影响精度
*/
__weak void LL_IncTick(void)
{
uwTick += 1;
}
/**
* @brief 获取系统当前毫秒级时间戳。
* @return {uint32_t} 当前毫秒级时间戳
* @note: 请注意这个函数仅用于模拟硬件延时实际应用中可能需要使用其他时钟源如RTC或外部时钟。
*/
uint32_t sys_millis(void)
{
return uwTick * 10;
}
/**
* @brief 系统计时器重新开始
* @return {*}
* @note
*/
void sys_millis_reset(void)
{
uwTick = 0;
}
/**
* @brief 将系统时间戳转换为秒级时间戳。
* @param {uint32_t} start_time 开始时间戳
* @return {uint32_t} 秒级时间戳
* @note: 请注意这个函数仅用于模拟硬件延时实际应用中可能需要使用其他时钟源如RTC或外部时钟。
*/
uint32_t sys_to_seconds(uint32_t start_time)
{
return (sys_millis() - start_time) / 1000;
}
/**
* @brief 记录调度器开始时间
* @return {*}
* @note
*/
void scheduler_time_start(void)
{
scheduler_start_time = sys_millis();
}
/**
* @brief 返回调度器运行时间
* @return {*}
* @note
*/
uint32_t scheduler_time_stop(void)
{
uint32_t scheduler_end_time = sys_millis() - scheduler_start_time;
scheduler_occupancy_time += scheduler_end_time;
return scheduler_end_time;
}
/**
* @brief 计算任务占用时间百分比
* @param {uint32_t} run_time 运行时间
* @return {*} 任务占用时间百分比,单位为%
* @note
*/
uint32_t scheduler_time_occupancy_get(uint32_t run_time)
{
float32 percent = 0.0f;
percent = (float32)(scheduler_occupancy_time) / (float32)run_time;
scheduler_occupancy_time = 0;
return (uint32_t)(percent * 100);
}