回调函数如何在stm32 Hal库中工作? [英] How the callback functions work in stm32 Hal Library?

查看:240
本文介绍了回调函数如何在stm32 Hal库中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,Hal Lib提供了一些回调函数来管理硬件中断.但是我不知道它们如何工作?

As we all know,the Hal Lib provides some callback function to manage hardware interrupt.But i don't know how them work?

事实上,我正在使用 HAL_UART_RxCpltCallback(UART_HandleTypeDef * huart)这个功能来接收其他设备的数据并检查这些数据.因此,我使用usart中断来接收它们.

Te fact is that I am using HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) this function so as to receive other devices' data and check those data.So I use the usart interrupt to receive them.

但是我不知道何时执行回调函数,这取决于接收缓冲区的长度还是数据的缓冲区?

But I don't know when the callback function will be executed,is it depends on the receive buffer's length or the data's buffer?

我想硬件中断将在接收到字符时触发,但是回调函数将在接收缓冲区已满后执行.

I guess the hardware interrupt will be triggered while a character has been received,but the callback function will be executed after the receive buffer is full.

PS:我正在使用stm32-nucleo-f410开发板与AT推荐设备进行通信,因此我是一个新手.(对不起,我的英语不好!)非常感谢.

PS:I am using the stm32-nucleo-f410 development board to communicate with an AT commend device,and I am a novice about it. (So sorry for my poor English!) Thanks a lot.

推荐答案

在接收函数中指定的数据量( HAL_UART_Receive_IT 的第三个参数)时,将调用您所引用的回调.您是正确的,每次接收到一个字符时都会调用UART中断服务程序(ISR),但是使用库内部发生的HAL时,则不需要您进行管理.每次调用ISR时,接收到的字符都会通过 HAL_UART_Receive_IT 的第二个参数移入您提供的数组中,并且当调用指定的字符数达到时,将在其中调用回调.ISR(因此请确保不要做任何会花费太多时间的事情-ISR应该简短,并且HAL库中的ISR已经足够冗长以处理每种可能的用例).

The callback you are referring to is called when the amount of data specified in the receive functions (the third argument to HAL_UART_Receive_IT). You are correct that the UART interrupt service routine (ISR) is called every time a character is received, but when using the HAL that happens internally to the library and doesn't need to be managed by you. Every time the ISR is called, the received character is moved into the array you provide via the second argument of HAL_UART_Receive_IT, and when the number of characters specified by the call is reached, the callback will be called in that ISR (so make sure not to do anything that will take too much time to complete - ISRs should be short, and the ISRs in the HAL library are already pretty lengthy to handle every possible use case).

此外,如果您发现即使向外围设备发送了足够的数据也未触发回调,请确保中断实际上已启用- HAL_UART_Receive_IT 函数实际上并未启用中断,这必须在外围设备初始化期间完成.

Further, if you find that the callback is not being triggered even if you are sending enough data to the peripheral, make sure the interrupt is actually enabled - the HAL_UART_Receive_IT function doesn't actually enable the interrupt, that has to be done during initialization of the peripheral.

这篇关于回调函数如何在stm32 Hal库中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆