为什么 MSP 被定义为回调函数?

发布于 2025-01-12 15:30:55 字数 1207 浏览 1 评论 0原文

在 STM32CubeMX 中,MSP 代表 MCU 支持包,其基本含义如下:

MSP 是执行系统级别的用户回调函数 初始化,例如(时钟、GPIO、DMA、中断)。

现在我正在查看这样一个函数,用作:

HAL_TIM_MspPostInit(&htim2);

当我打开声明时,它在 stm32f3xx_hal_msp.c 下找到:

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(htim->Instance==TIM2)
  {
  /* USER CODE BEGIN TIM2_MspPostInit 0 */

  /* USER CODE END TIM2_MspPostInit 0 */

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**TIM2 GPIO Configuration
    PA0     ------> TIM2_CH1
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN TIM2_MspPostInit 1 */

  /* USER CODE END TIM2_MspPostInit 1 */
  }

现在在 C 回调函数中,它的指针被传递给另一个函数。 此处所选答案是一个示例。

我的问题是:什么使 MSP 具有回调函数?他们获取作为参数而不是函数传递的结构。 MSP 中的回调在哪里?我在那里看不到回调函数的足迹。举个例子会有帮助。

In STM32CubeMX MSP stands for MCU Support Package and of all here is what it basically about:

MSPs are user callback functions to perform system level
initializations such as (Clock, GPIOs, DMA, interrupts).

Now I'm looking at such a function used as:

HAL_TIM_MspPostInit(&htim2);

And when I open declaration it is found under stm32f3xx_hal_msp.c as:

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(htim->Instance==TIM2)
  {
  /* USER CODE BEGIN TIM2_MspPostInit 0 */

  /* USER CODE END TIM2_MspPostInit 0 */

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**TIM2 GPIO Configuration
    PA0     ------> TIM2_CH1
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN TIM2_MspPostInit 1 */

  /* USER CODE END TIM2_MspPostInit 1 */
  }

Now in C callback function is a function which its pointer is passed to another function. Here selected answer is an example.

My question is: What makes MSPs callback functions? They get structs passed as arguments not functions. And where are the callbacks in MSPs? I could not see the footprint of a callback function there. An example would help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝颜夕 2025-01-19 15:30:55

这些函数由 HAL 库调用。 (就像您的情况下的HAL_TIM_Init)这些是将通用库从您的实现/mp中分离出来所必需的。毕竟它是一个硬件抽象层库。

大多数 HAL 回调函数的实现都很弱,因此即使您没有定义它们,库也会提供一个空的实现来确保您的代码可以编译。

These function are called by HAL library. (Like HAL_TIM_Init in your case) These are necessary to split the generic library from your implementation/mp. Its an hardware abstraction layer library after all.

Most of these HAL callback function have weak implementation so even if you don't define them there always an empty implementation provided by the library to make sure your code compiles.

栖迟 2025-01-19 15:30:55

回调意味着应用程序代码中由库代码调用的函数

在这种情况下,您的应用程序代码告诉库代码“我想初始化定时器 TIM2”。库代码完成部分工作,然后它必须配置引脚分配(定时器库不知道),因此它回调应用程序代码来执行以下操作引脚设置。当 MSP 函数返回时,定时器库代码完成定时器设置并返回到应用程序。

为了简单起见,回调函数的名称被硬编码到库函数中,因此它最终以静态方式链接。这避免了对函数指针的需要。在库函数返回之前,控制流返回到应用程序代码,这使得它成为回调,而不是所使用的语言语法。

A callback means a function in the application code which is called by library code.

In this case your application code tells the library code "I want to initialize timer TIM2". The library code does part of the job, then it gets to the bit where it has to configure the pinout (which the timer library has no knowledge of) so it calls back to the application code to do the pin setup. When the MSP function returns the timer library code completes setting up the timer and returns to the application.

For the sake of simplicity the name of the callback function is hard coded into the library function, and so it ends up linked statically. This avoids the need for a function pointer. It is the flow of control back to application code before the library function has returned which makes this a callback, not the language syntax used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文