c - MCU 上的 RTC - 函数指针和回调

标签 c pointers callback function-pointers microcontroller

下面的代码是如何在单片机上使用实时时钟的示例。

我的问题与回调和函数指针有关。

我在下面包含了 rtc_config_t 的结构声明。

我的问题是,在线 cfg.callback = rtc_example_callback

  1. 为什么在rtc_example_callback之前没有使用&符号。

  2. 为什么不需要将参数传递给 rtc_example_callback

  3. 最后一个struct memeber void *callback_data;设置为NULL,我不明白这是干什么的? 你想什么时候或什么东西回来?

许多坦克供您输入

#include "rtc.h"
#include "interrupt.h"
#include "isr.h"

#define ALARM (QM_RTC_ALARM_MINUTE / 6)
#define MAX_RTC_FIRINGS (5)

void rtc_example_callback(void *);

static volatile uint32_t rtc_fired = 0;

/*  RTC app example */
int main(void)
{
    /*  Variables */
    rtc_config_t cfg; //create a struct variable to configure the RTC

    PRINTF("Starting: RTC\n");

    /*  Initialise RTC configuration */
    cfg.init_val = 0;
    cfg.alarm_en = true;
    cfg.alarm_val = ALARM;
    cfg.callback = rtc_example_callback;
    cfg.callback_data = NULL;

    irq_request(IRQ_RTC_0, rtc_isr_0); //submit the RTC to the interrupt service routine

    clk_periph_enable(CLK_PERIPH_RTC_REGISTER | CLK_PERIPH_CLK); //switch on RTC and Periphal clock

    rtc_set_config(RTC_0, &cfg); //Set the RTC configuration

    /* Wait for RTC to fire 5 times and then finish. */
    while (rtc_fired < MAX_RTC_FIRINGS) {
    }

    PRINTF("Finished: RTC\n");
    clk_periph_disable(CLK_PERIPH_RTC_REGISTER | CLK_PERIPH_CLK); //turn off the clocks
    return 0;
}

void rtc_example_callback(void *data)
{
    PUTS("Alarm!!\n");
    qm_rtc_set_alarm(RTC_0, (RTC[RTC_0].rtc_ccvr + ALARM));
    rtc_fired++;
}
-----------------------------------------------------------------------

/**
 * RTC configuration type.
 */
typedef struct {
    uint32_t init_val;  /**< Initial value in RTC clocks. */
    bool alarm_en;      /**< Alarm enable. */
    uint32_t alarm_val; /**< Alarm value in RTC clocks. */

    /**
     * User callback.
     *
     * @param[in] data User defined data.
     */
    void (*callback)(void *data);
    void *callback_data; /**< Callback user data. */
} rtc_config_t;

最佳答案

  1. 函数名是指向函数的指针
  2. 函数将使用 rtc 库中的参数调用,您没有调用它(您不能在此处传递参数)。
  3. 我猜分配给 custom_callback 的 NULL 不会调用库中的自定义方法(默认函数或不调用任何函数),如果您不想使用自定义回调,只需分配 NULL。

通常库代码如下所示:

if(custom_callback)
{
    custom_callback(some_parameters);
}
else
{
    default_callback(some_parameters);
}

关于c - MCU 上的 RTC - 函数指针和回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40180389/

相关文章:

javascript - $http 错误回调不能与 httpProvider responseInterceptor 结合使用

c - 在 MATLAB 中使用 MEX 文件访问存储在元胞数组中的矩阵

C++将 self 传递给 self 神秘

c - 存储 malloc 的返回值(空指针)

c - 函数调用时指针更改大小(使用 gdb 检查)

actionscript-3 - AS3 - 我可以访问调用我的对象(或函数)吗?

c - 在 C 中将 NULL 字符写入文件

C 程序卡在 strcmp 附近

C 错误 - wait()

javascript - 在循环中使用回调的正确方法?