当前位置:网站首页>[RT thread] NXP rt10xx device driver framework -- RTC construction and use
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
2022-07-03 17:11:00 【L_ seventeen】
RTC Full name Real-Time Clock,RTC The module is often used for the function of clock calendar . The detailed function can search by itself , There is no extension .
Preparation before development
- Hardware platform :nxp rt10xx Single chip microcomputer
- IDE: Keil
1.Kconfig Modifications and menuconfig To configure
stay Env Environmental Science menuconfig in RT-Thread Components->Device Drivers The device driver defaults to n, So it needs to be turned on .alarm and software simulation rtc Select according to the application , There is no introduction in this chapter
First in Kconfig Add the following statement to , And then in Env Environmental Science menuconfig in Hardware Drivers Config->On-Chip Peripheral Drivers Can make RTC
2. Engineering additions RTC Drive framework and BSP Driver interface
Device drive frame :rtc.c BSP Interface :drv_rtc.c fsl_snvs_hp.c
3. Add or modify drv_rtc.c
The author consulted the document , Find out drv_rtc.c Compilation failed , Don't panic , Anyway, the main work is also modification drv_xx.c bsp Driven , As long as the official device driver framework is ok .rtc The driver directly references the parent class device Built with standard framework , as follows :
static struct rt_device device =
{
.type = RT_Device_Class_RTC,
.init = imxrt_hp_rtc_init,
.open = imxrt_hp_rtc_open,
.close = imxrt_hp_rtc_close,
.read = imxrt_hp_rtc_read,
.write = imxrt_hp_rtc_write,
.control = imxrt_hp_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)
{
LOG_E("rt device register failed %d\n", ret);
return ret;
}
rt_device_open(&device, RT_DEVICE_OFLAG_RDWR);
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
The content is very simple, directly paste the code , basic git Coming down , Modify a little rtc The frame is set up
static time_t imxrt_get_timestamp(void)
{
struct tm tm_new = {
0};
snvs_hp_rtc_datetime_t rtcDate = {
0};
SNVS_HP_RTC_GetDatetime(SNVS, &rtcDate);
tm_new.tm_sec = rtcDate.second;
tm_new.tm_min = rtcDate.minute;
tm_new.tm_hour = rtcDate.hour;
tm_new.tm_mday = rtcDate.day;
tm_new.tm_mon = rtcDate.month - 1;
tm_new.tm_year = rtcDate.year - 1900;
return timegm(&tm_new);
}
static int imxrt_set_timestamp(time_t timestamp)
{
struct tm *p_tm;
snvs_hp_rtc_datetime_t rtcDate = {
0};
p_tm = gmtime(×tamp);
rtcDate.second = p_tm->tm_sec ;
rtcDate.minute = p_tm->tm_min ;
rtcDate.hour = p_tm->tm_hour;
rtcDate.day = p_tm->tm_mday;
rtcDate.month = p_tm->tm_mon + 1;
rtcDate.year = p_tm->tm_year + 1900;
if (SNVS_HP_RTC_SetDatetime(SNVS, &rtcDate) != kStatus_Success)
{
LOG_E("set rtc date time failed\n");
return -RT_ERROR;
}
return RT_EOK;
}
static rt_err_t imxrt_hp_rtc_init(rt_device_t dev)
{
snvs_hp_rtc_config_t snvsRtcConfig;
SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig);
SNVS_HP_RTC_Init(SNVS, &snvsRtcConfig);
return RT_EOK;
}
static rt_err_t imxrt_hp_rtc_open(rt_device_t dev, rt_uint16_t oflag)
{
SNVS_HP_RTC_StartTimer(SNVS);
return RT_EOK;
}
static rt_err_t imxrt_hp_rtc_close(rt_device_t dev)
{
SNVS_HP_RTC_StopTimer(SNVS);
return RT_EOK;
}
static rt_size_t imxrt_hp_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
return RT_EOK;
}
static rt_size_t imxrt_hp_rtc_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
return RT_EOK;
}
static rt_err_t imxrt_hp_rtc_control(rt_device_t dev, int cmd, void *args)
{
RT_ASSERT(dev != RT_NULL);
switch(cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
{
*(uint32_t *)args = imxrt_get_timestamp();
}
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
{
//set_timestamp(*(time_t *)args);
imxrt_set_timestamp(*(time_t *)args);
}
break;
default:
return RT_EINVAL;
}
return RT_EOK;
}
4. Build application layer demo
Turn on timer , Check whether the time accuracy ok
/**************************************************START OF FILE*****************************************************/
/*------------------------------------------------------------------------------------------------------------------ Includes */
#include <rtthread.h>
#include <rtdevice.h>
/*------------------------------------------------------------------------------------------------------------------ Macros */
/*------------------------------------------------------------------------------------------------------------------ Variables */
/*------------------------------------------------------------------------------------------------------------------ Functions */
static int rtcsample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
/* Setting date */
ret = set_date(2022, 7, 2);
if (ret != RT_EOK)
{
rt_kprintf("set RTC date failed\n");
return ret;
}
/* Setup time */
ret = set_time(12, 00, 00);
if (ret != RT_EOK)
{
rt_kprintf("set RTC time failed\n");
return ret;
}
rt_kprintf("RTC time start\n");
return ret;
}
static void rtcprint(void)
{
time_t now;
/* Acquisition time */
now = time(RT_NULL);
rt_kprintf("%s\n", ctime(&now));
}
/* Export to msh In the command list */
MSH_CMD_EXPORT(rtcsample, rtc sample);
MSH_CMD_EXPORT(rtcprint, rtc print);
Enter the command rtcsample Run the application , Input rtcprint Print current rtc The clock
边栏推荐
- RF Analyze Demo搭建 Step by Step
- One brush 144 force deduction hot question-1 sum of two numbers (E)
- PHP online confusion encryption tutorial sharing + basically no solution
- Kotlin学习快速入门(7)——扩展的妙用
- 深入理解 SQL 中的 Grouping Sets 语句
- 【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
- Deep understanding of grouping sets statements in SQL
- How to allow remote connection to MySQL server on Linux system?
- C language string practice
- 29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
猜你喜欢
Mysql database DDL and DML
【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
Kotlin学习快速入门(7)——扩展的妙用
Analysis of variance summary
Leetcode: lucky number in matrix
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
kubernetes资源对象介绍及常用命令(四)
ANOVA example
Network security web penetration technology
CC2530 common registers for watchdog
随机推荐
Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
Open vsftpd port under iptables firewall
Why is WPA3 security of enterprise business so important?
Mysql database -dql
Kindeditor editor upload image ultra wide automatic compression -php code
图之深度优先搜索
RedHat 6.2 配置 Zabbix
汇编实例解析--实模式下屏幕显示
How do large consumer enterprises make digital transformation?
远程办公之如何推进跨部门项目协作 | 社区征文
Vs code plug-in korofileheader
[combinatorics] recursive equation (example of solving recursive equation without multiple roots | complete process of solving recursive equation without multiple roots)
建立自己的网站(23)
UCORE overview
How SVN views modified file records
Kotlin learning quick start (7) -- wonderful use of expansion
CC2530 common registers for watchdog
Deep understanding of grouping sets statements in SQL
The largest matrix (H) in a brush 143 monotone stack 84 histogram
Kotlin learning quick start (7) -- wonderful use of expansion