当前位置:网站首页>[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

边栏推荐
- 图之深度优先搜索
- [combinatorics] recursive equation (the relationship theorem between the solution of the recursive equation and the characteristic root | the linear property theorem of the solution of the recursive e
- LeetCode13.罗马数字转整数(三种解法)
- The largest matrix (H) in a brush 143 monotone stack 84 histogram
- 新库上线 | CnOpenData中国观鸟记录数据
- Necessary ability of data analysis
- 深入理解 SQL 中的 Grouping Sets 语句
- mysql用户管理
- 聊聊接口优化的几个方法
- University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
猜你喜欢

One brush 149 force deduction hot question-10 regular expression matching (H)

Atom QT 16_ audiorecorder

kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)

Network security web penetration technology

kubernetes资源对象介绍及常用命令(四)

Unity notes unityxr simple to use

Static program analysis (I) -- Outline mind map and content introduction

C language modifies files by line

Redis:关于列表List类型数据的操作命令

手把手带你入门 API 开发
随机推荐
Financial management (Higher Vocational College) financial management online Assignment 1 in autumn 20
Assembly instance analysis -- screen display in real mode
Kotlin learning quick start (7) -- wonderful use of expansion
kubernetes资源对象介绍及常用命令(四)
跨境电商:外贸企业做海外社媒营销的优势
線程池:業務代碼最常用也最容易犯錯的組件
mysql用户管理
Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
Kotlin学习快速入门(7)——扩展的妙用
[combinatorics] recursive equation (example of solving recursive equation without multiple roots | complete process of solving recursive equation without multiple roots)
An example of HP array card troubleshooting
IL Runtime
线程池:业务代码最常用也最容易犯错的组件
CC2530 common registers
One brush 146 force buckle hot question-3 longest substring without repeated characters (m)
[combinatorics] recursive equation (constant coefficient linear homogeneous recursive equation | constant coefficient, linear, homogeneous concept description | constant coefficient linear homogeneous
The way of wisdom (unity of knowledge and action)
Redis:关于列表List类型数据的操作命令
Talk about several methods of interface optimization
kubernetes资源对象介绍及常用命令(三)