发新帖我要提问
123
返回列表
打印
[应用相关]

HAL驱动程序概述

[复制链接]
楼主: xiaoqi000
手机看帖
扫描二维码
随时随地手机跟帖
41
xiaoqi000|  楼主 | 2024-3-31 22:27 | 只看该作者 回帖奖励 |倒序浏览
运行时间检查:
HAL通过检查所有HAL驱动程序函数的输入值来实现运行时故障检测;运行时检查是通过使用assert_parammacro实现的;该宏用于具有输入参数的所有HAL驱动程序函数;它允许验证输入值是否在参数允许值范围内;

要启用运行时检查,请使用assert_parammacro,并在stm32f0xx_hal_conf.hfile中取消注释定义USE_FULL_ASSERT;

void HAL_UART_Init(UART_HandleTypeDef *huart)
{
(..) /* Check the parameters */
assert_param(IS_UART_INSTANCE(huart->Instance));
assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
assert_param(IS_UART_PARITY(huart->Init.Parity));
assert_param(IS_UART_MODE(huart->Init.Mode));
assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
(..)
/** @defgroup UART_Word_Length *
@{
*/
#define UART_WORDLENGTH_8B ((uint32_t)0x00000000)
#define UART_WORDLENGTH_9B ((uint32_t)USART_CR1_M)
#define IS_UART_WORD_LENGTH(LENGTH) (((LENGTH) == UART_WORDLENGTH_8B) ||
\ ((LENGTH) == UART_WORDLENGTH_9B))

使用特权

评论回复
42
xiaoqi000|  楼主 | 2024-3-31 22:27 | 只看该作者
如果传递给assert_param宏的表达式为false,则调用theassert_failed函数并返回源文件的名称和失败的调用的源行号;如果表达式为true,则不返回任何值;assert_parammacro在stm32f0xx_hal_conf.h中实现:

/* Exported macro ------------------------------------------------------------*/
#ifdef USE_FULL_ASSERT
/**
* @brief The assert_param macro is used for function's parameters check.
* @param expr: If expr is false, it calls assert_failed function
* which reports the name of the source file and the source
* line number of the call that failed.
* If expr is true, it returns no value.
* @retval None */
#define assert_param(expr) ((expr)?(void)0:assert_failed((uint8_t *)__FILE__,
__LINE__))
/* Exported functions --------------------------------------*/
void assert_failed(uint8_t* file, uint32_t line);
#else
#define assert_param(expr)((void)0)
#endif /* USE_FULL_ASSERT */
assert_failed函数在main.c文件或任何其他用户C文件中实现:

#ifdef USE_FULL_ASSERT /**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None */
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
由于引入了开销运行时检查,因此建议在应用程序代码开发和调试期间使用它,并将其从最终应用程序中删除以改进代码大小和速度;

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则