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

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

问题描述

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

事实是我正在使用HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)这个函数来接收其他设备的数据并检查这些数据.所以我使用usart中断来接收它们.>

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

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

PS:我用的是stm32-nucleo-f410开发板与AT命令设备通信,我是新手.(对不起,我的英语不好!)非常感谢.

解决方案

当接收函数中指定的数据量(HAL_UART_Receive_IT 的第三个参数)时,将调用您所指的回调.每次接收到字符时都会调用 UART 中断服务例程 (ISR),这是正确的,但是当使用库内部发生的 HAL 时,不需要由您管理.每次调用 ISR 时,接收到的字符都会通过 HAL_UART_Receive_IT 的第二个参数移动到你提供的数组中,当达到调用指定的字符数时,回调将被调用ISR(因此请确保不要做任何需要花费太多时间才能完成的事情——ISR 应该很短,而且 HAL 库中的 ISR 已经很长,无法处理所有可能的用例).

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

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

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: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.

解决方案

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).

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天全站免登陆