这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 行业应用 » 汽车电子 » 【S32K146】S32K146片内 RTC Alarm 功能使用

共4条 1/1 1 跳转至

【S32K146】S32K146片内 RTC Alarm 功能使用

高工
2025-06-27 21:51:19     打赏

【简介】 

在之前的贴子中我们已经使用了RTC的计时功能(【S32K146】S32K146片内RTC使用),S32K146的RTC还支持alarm 功能,我们在此基础上继续配置验证RTC的alarm 功能。

【S32DS配置】

使用S32DS 配置RTC 初始时间为2025 1/1 0:0:0

image.png

配置alarm 时间为2025 1/1  0:1:0 并开启alarm 中断 并设置中断callback 函数

image.png

生成初始化配置代码如下:

/*
 * RTC Initialization configurations
 */

/*! rtc_1InitCfg0 instance initialization configuration */
const rtc_init_config_t rtc_1InitCfg0 = {
  .updateEnable = false,
  .nonSupervisorAccessEnable = false,
  .compensationInterval = 0U,
  .compensation = 0,
  .clockSelect = RTC_CLK_SRC_LPO_1KHZ,
  .clockOutConfig = RTC_CLKOUT_DISABLED
};

/*! rtc_1InitCfg0_StartTime Initial Time and Date */
const rtc_timedate_t rtc_1InitCfg0_StartTime  = {
  .year = 2025U,
  .month = 1U,
  .day = 1U,
  .hour = 0U,
  .minutes = 0U,
  .seconds = 0U
};

/*
 * RTC Register Lock configurations
 */

/*! rtc_1LockCfg0 register lock configuration */
const rtc_register_lock_config_t rtc_1LockCfg0 = {
  .lockRegisterLock = false,
  .statusRegisterLock = false,
  .controlRegisterLock = false,
  .timeCompensationRegisterLock = false
};

/*
 * RTC Alarm configurations
 */

/*! rtc_1AlarmCfg0 alarm configuration */
rtc_alarm_config_t rtc_1AlarmCfg0 = {
  .alarmTime = {
    .year = 2025U,
    .month = 1U,
    .day = 1U,
    .hour = 0U,
    .minutes = 1U,
    .seconds = 0U
  },
  .repeatForever = false,
  .repetitionInterval = 0UL,
  .numberOfRepeats = 0UL,
  .alarmIntEnable = true,
  .callbackParams = NULL,
  .alarmCallback = rtc_alarm_isr_callback
};

/*
 * RTC Time Seconds Interrupt configurations
 */

/*! rtc_1SecIntCfg0 time seconds configuration */
rtc_seconds_int_config_t rtc_1SecIntCfg0 = {
  .secondIntConfig = RTC_INT_1HZ,
  .secondIntEnable = false,
  .secondsCallbackParams = NULL,
  .rtcSecondsCallback = NULL
};

/*
 * RTC Fault interrupt configurations
 */

/*! rtc_1FaultCfg0 fault configuration */
rtc_interrupt_config_t rtc_1FaultCfg0 = {
  .overflowIntEnable = false,
  .timeInvalidIntEnable = false,
  .callbackParams = NULL,
  .rtcCallback = NULL
};


   【测试验证】

添加如下初始化RTC 并在设置alarm,在alarm 回调中添加打印并打印当前RTC时间

void rtc_alarm_isr_callback(void * callbackParam)
{
    LOG_I("Alarm call back.");
    rtc_timedate_t  currentTime;
    status_t ret;

    ret = RTC_DRV_GetCurrentTimeDate ( RTC_1, &currentTime );
    if(ret != STATUS_SUCCESS)
    {
        LOG_E("RTC read failed %x.",ret);
    }
    else
    {
        LOG_I("%d:%d:%d %d:%d:%d",currentTime.year,currentTime.month,currentTime.day,
                                  currentTime.hour,currentTime.minutes,currentTime.seconds);
    }
}

static int rtc_init(void)
{
    status_t ret;
    /* Call the init function */
    ret = RTC_DRV_Init(RTC_1, &rtc_1InitCfg0);
    if(ret != STATUS_SUCCESS)
    {
        LOG_E("RTC init failed %x.",ret);
    }
    else
    {
        LOG_I("RTC init OK.");
    }

    /* Set the time and date */
    ret = RTC_DRV_SetTimeDate(RTC_1, &rtc_1InitCfg0_StartTime);
    if(ret != STATUS_SUCCESS)
    {
        LOG_E("RTC set time failed %x.",ret);
    }
    else
    {
        LOG_I("RTC set time OK.");
    }

    /* Start RTC counter */
    ret = RTC_DRV_StartCounter(RTC_1);
    if(ret != STATUS_SUCCESS)
    {
        LOG_E("RTC start failed %x.",ret);
    }
    else
    {
        LOG_I("RTC start OK.");
    }


    /* Start RTC counter */
    ret = RTC_DRV_ConfigureAlarm(RTC_1, &rtc_1AlarmCfg0);
    if(ret != STATUS_SUCCESS)
    {
        LOG_E("RTC start failed %x.",ret);
    }
    else
    {
        LOG_I("RTC start OK.");
    }
    return 1;
}

INIT_BOARD_EXPORT_LEVEL1(rtc_init);

下载运行结果如下,跟预期的一致出发了alarm 中断callback 

image.png



专家
2025-06-27 22:36:18     打赏
2楼

感谢分享


专家
2025-06-27 22:37:32     打赏
3楼

感谢分享


专家
2025-06-27 22:38:55     打赏
4楼

感谢分享


共4条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]