assert_failed

时间: 2023-08-07 11:04:33 浏览: 118
assert_failed是一个函数,用于在参数检测失败时报告源文件名和行号。[1]它是通过宏assert_param来调用的,当定义了USE_FULL_ASSERT宏时,参数检测机制才会被打开。[2]assert_failed函数的实现可以根据需要进行自定义,用于报告文件名和行号等信息。[3]在spi.c文件中,可以看到assert_param被用来检测SPI_InitStruct->SPI_Direction参数的合法性。
相关问题

void assert_failed(uint8_t* file, uint32_t line);

void assert_failed(uint8_t* file, uint32_t line)是一个用于在C语言中进行调试的函数。它通常用于在程序中检查某些条件是否满足,如果不满足,则会调用该函数来输出错误信息并停止程序的执行。在该函数中,file参数表示出现错误的文件名,line参数表示出现错误的行号。这个函数的作用是帮助程序员在开发过程中快速定位错误并进行调试。 演示代码如下: ```c #include <stdio.h> #include <stdlib.h> void assert_failed(uint8_t* file, uint32_t line) { printf("Wrong parameters value: file %s on line %d\r\n", file, line); exit(1); } int main() { int a = 10, b = 20; // 检查a是否等于b,如果不等于则调用assert_failed函数 if (a == b) { printf("a equals b.\n"); } else { assert_failed(__FILE__, __LINE__); } return 0; } ```

#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))

这是一个宏定义,通常在使用ST公司的芯片时会用到。它的作用是在调试时根据表达式判断程序是否运行正常,如果表达式不成立(结果为false),则会调用assert_failed函数进行错误处理。 具体来说,这个宏定义中包含了一个三目运算符,判断表达式是否成立。如果成立,则什么也不做,直接返回void;如果不成立,则调用assert_failed函数,该函数会将错误信息输出到调试终端,以便于调试人员进行错误分析和定位。 在使用这个宏定义时,我们需要在程序中显式地调用它,传入需要判断的表达式作为参数。如果表达式不成立,程序会在这里中断并输出错误信息,以提醒我们进行错误处理。

相关推荐

int main(void) { /*HW semaphore Clock enable*/ __HAL_RCC_HSEM_CLK_ENABLE(); /* Activate HSEM notification for Cortex-M4*/ HAL_HSEM_ActivateNotification(__HAL_HSEM_SEMID_TO_MASK(HSEM_ID_0)); /* Domain D2 goes to STOP mode (Cortex-M4 in deep-sleep) waiting for Cortex-M7 to perform system initialization (system clock config, external memory configuration.. ) */ HAL_PWREx_ClearPendingEvent(); HAL_PWREx_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFE, PWR_D2_DOMAIN); /* Clear HSEM flag */ __HAL_HSEM_CLEAR_FLAG(__HAL_HSEM_SEMID_TO_MASK(HSEM_ID_0)); /* STM32H7xx HAL library initialization: - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Infinite loop */ while (1) { } } #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) { } } #endif /** * @} */ /** * @} */

解释代码void LedOn(GPIO_Module* GPIOx, uint16_t Pin) { GPIOx->PBSC = Pin; } /** * @brief Turns selected Led Off. * @param GPIOx x can be A to G to select the GPIO port. * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15. */ void LedOff(GPIO_Module* GPIOx, uint16_t Pin) { GPIOx->PBC = Pin; } /** * @brief Turns selected Led on or off. * @param GPIOx x can be A to G to select the GPIO port. * @param Pin This parameter can be one of the following values: * @arg GPIO_PIN_0~GPIO_PIN_15: set related pin on * @arg (GPIO_PIN_0<<16)~(GPIO_PIN_15<<16): clear related pin off */ void LedOnOff(GPIO_Module* GPIOx, uint32_t Pin) { GPIOx->PBSC = Pin; } /** * @brief Toggles the selected Led. * @param GPIOx x can be A to G to select the GPIO port. * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15. */ void LedBlink(GPIO_Module* GPIOx, uint16_t Pin) { GPIOx->POD ^= Pin; } /** * @brief Assert failed function by user. * @param file The name of the call that failed. * @param line The source line number of the call that failed. */ #ifdef USE_FULL_ASSERT void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line) { while (1) { } } #endif // USE_FULL_ASSERT /** * @brief Main program. */ int main(void) { /*SystemInit() function has been called by startup file startup_n32g45x.s*/ /* Initialize Led1~Led5 as output pushpull mode*/ LedInit(PORT_GROUP1, LED1_PIN | LED2_PIN); LedInit(PORT_GROUP2, LED3_PIN | LED4_PIN | LED5_PIN); /*Turn on Led1*/ LedOn(PORT_GROUP1, LED1_PIN); while (1) { /*LED1_PORT and LED2_PORT are the same port group.Enable Led2 blink and not effect Led1 by Exclusive-OR * operation.*/ LedBlink(PORT_GROUP1, LED2_PIN); /*LED3_PORT, LED4_PORT and LED5_PORT are the same port group.*/ /*Turn Led4 and Led5 off and not effect other ports by PBC register,correspond to * PORT_GROUP2->POD&=~(LED4_PIN|LED5_PIN);*/ LedOff(PORT_GROUP2, LED4_PIN | LED5_PIN); /* Insert delay */ Delay(0x28FFFF); /*Turn Led4 and Led5 on,turn Led3 off and not effect other ports by PBSC register,correspond to * PORT_GROUP2->POD&=~(LED3_PIN),then PORT_GROUP2->POD|=(LED4_PIN|LED5_PIN);*/ LedOnOff(PORT_GROUP2, (LED3_PIN << 16) | LED4_PIN | LED5_PIN); /* Insert delay */ Delay(0x28FFFF); /*Turn on Led3*/ LedOn(PORT_GROUP2, LED3_PIN); /* Insert delay */ Delay(0x28FFFF); } }

修改以下程序,使其接收指令后发送一个hello,world字符串。#include "main.h"#include "usart.h"#include "gpio.h"#include "stdio.h"#include "string.h"uint8_t aRxBuffer;void SystemClock_Config(void);int fputc(int ch, FILE *f){ uint8_t temp[1] = {ch}; HAL_UART_Transmit(&huart1, temp, 1, 0xffff);return ch;}int fgetc(FILE * f){ uint8_t ch = 0; HAL_UART_Receive(&huart1,&ch, 1, 0xffff); return ch;}int main(void){ HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1); user_main_printf(""); /* USER CODE END 2 */ while (1) { }}void SystemClock_Config(void){ RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); }}void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){ char *pCmd = NULL; uint8_t len; switch(aRxBuffer){ case '1': pCmd = "command 1\r\n"; len = strlen(pCmd); break; case '2': pCmd = "command 2\r\n"; len = strlen(pCmd); break; case '3': pCmd = "command 3\r\n"; len = strlen(pCmd); break; case '4': pCmd = "command 4\r\n"; len = strlen(pCmd); break; default: pCmd = "command cmd\r\n"; len = strlen(pCmd); break; } HAL_UART_Transmit(&huart1, (uint8_t *)pCmd, len,0xFFFF); HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1); }void Error_Handler(void){}#ifdef USE_FULL_ASSERTvoid assert_failed(uint8_t *file, uint32_t line){ }#endif

最新推荐

recommend-type

grpcio-1.63.0-cp38-cp38-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

SQLyog-13.1.3-0.x86Community.exe

SQLyog-13.1.3-0.x86Community
recommend-type

VB自动出题题库系统设计(源代码+系统).rar

计算机专业毕业设计VB精品论文资源
recommend-type

debugpy-1.0.0b2-cp35-cp35m-manylinux1_i686.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

实战自学python如何成为大佬(目录):https://blog.csdn.net/weixin-67859959/artic

实战自学python如何成为大佬(目录):https://blog.csdn.net/weixin-67859959/artic
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。