[bsp][nxp/mcxn10] Initial support for RTC driver.

Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
Yilin Sun 2024-02-19 19:18:10 +08:00 committed by Meco Man
parent 66cc1f5da1
commit 9538f569e7
3 changed files with 147 additions and 2 deletions

View File

@ -0,0 +1,126 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-02-06 Yilin Sun Initial version.
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#include "drv_rtc.h"
#include "fsl_common.h"
#include "fsl_irtc.h"
#ifdef RT_USING_RTC
static rt_err_t mcx_rtc_init(rt_device_t dev)
{
irtc_config_t rtc_cfg;
IRTC_GetDefaultConfig(&rtc_cfg);
//rtc_cfg.clockSelect = kIRTC_Clk32K;
if (IRTC_Init(RTC0, &rtc_cfg) != kStatus_Success)
{
return -RT_EIO;
}
return RT_EOK;
}
static rt_err_t mcx_rtc_get_time(time_t *ts)
{
struct tm tm_new = {0};
irtc_datetime_t rtc_date;
IRTC_GetDatetime(RTC0, &rtc_date);
tm_new.tm_sec = rtc_date.second;
tm_new.tm_min = rtc_date.minute;
tm_new.tm_hour = rtc_date.hour;
tm_new.tm_mday = rtc_date.day;
tm_new.tm_mon = rtc_date.month - 1;
tm_new.tm_year = rtc_date.year - 1900;
*ts = timegm(&tm_new);
return RT_EOK;
}
static rt_err_t mcx_rtc_set_time(time_t *ts)
{
struct tm now;
irtc_datetime_t rtc_date;
gmtime_r(ts, &now);
rtc_date.second = now.tm_sec ;
rtc_date.minute = now.tm_min ;
rtc_date.hour = now.tm_hour;
rtc_date.weekDay = now.tm_wday;
rtc_date.day = now.tm_mday;
rtc_date.month = now.tm_mon + 1;
rtc_date.year = now.tm_year + 1900;
IRTC_SetWriteProtection(RTC0, false);
IRTC_SetDatetime(RTC0, &rtc_date);
return RT_EOK;
}
static rt_err_t mcx_rtc_control(rt_device_t dev, int cmd, void *args)
{
switch (cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
mcx_rtc_get_time((time_t *)args);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
mcx_rtc_set_time((time_t *)args);
break;
case RT_DEVICE_CTRL_RTC_SET_ALARM:
/* TODO: Implement alarm features */
default:
return -RT_EINVAL;
}
return RT_EOK;
}
static struct rt_device device =
{
.type = RT_Device_Class_RTC,
.init = mcx_rtc_init,
.open = RT_NULL,
.close = RT_NULL,
.read = RT_NULL,
.write = RT_NULL,
.control = mcx_rtc_control,
};
int rt_hw_rtc_init(void)
{
rt_err_t ret = RT_EOK;
ret = rt_device_register(&device, "rtc", RT_DEVICE_FLAG_RDWR);
if (ret != RT_EOK)
{
return ret;
}
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
#endif /*RT_USING_RTC */

View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-02-06 Yilin Sun Initial version.
*/
#ifndef __DRV_RTC_H__
#define __DRV_RTC_H__
#include <rtthread.h>
#include <rtdevice.h>
int rt_hw_rtc_init(void);
#endif

View File

@ -66,7 +66,6 @@ void rt_hw_board_init()
/* enable VREF */ /* enable VREF */
SPC0->ACTIVE_CFG1 |= 0xFFFFFFFF; SPC0->ACTIVE_CFG1 |= 0xFFFFFFFF;
CLOCK_EnableClock(kCLOCK_Dma0); CLOCK_EnableClock(kCLOCK_Dma0);
CLOCK_EnableClock(kCLOCK_Dma1); CLOCK_EnableClock(kCLOCK_Dma1);
@ -75,10 +74,11 @@ void rt_hw_board_init()
EDMA_Init(DMA0, &userConfig); EDMA_Init(DMA0, &userConfig);
EDMA_Init(DMA1, &userConfig); EDMA_Init(DMA1, &userConfig);
/* This init has finished in secure side of TF-M */ /* This init has finished in secure side of TF-M */
BOARD_InitBootClocks(); BOARD_InitBootClocks();
CLOCK_SetupClk16KClocking(kCLOCK_Clk16KToAll);
SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND); SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
/* set pend exception priority */ /* set pend exception priority */
NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1); NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);