当前位置:网站首页>【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
2022-07-03 17:07:00 【L_17】
RTC全称Real-Time Clock,RTC模块常用于时钟日历的功能。细节化的功能可以自行搜索,这里不做扩展。
开发前准备
- 硬件平台:nxp rt10xx单片机
- IDE: Keil
1.Kconfig 修改和menuconfig配置
在Env环境menuconfig中 RT-Thread Components->Device Drivers 设备驱动默认为n,所以需要开启。alarm 和 software simulation rtc根据应用情况选择,本章不做介绍
先在Kconfig中添加如下语句,然后在Env环境menuconfig中 Hardware Drivers Config->On-Chip Peripheral Drivers 使能RTC
2.工程添加RTC驱动框架和BSP驱动接口
设备驱动框架:rtc.c BSP接口:drv_rtc.c fsl_snvs_hp.c
3.添加或修改drv_rtc.c
笔者查阅了文件,发现 drv_rtc.c 编译无法通过,也不用慌,反正主要工作也是修改drv_xx.c bsp驱动的,只要官方设备驱动框架没问题就行。rtc 驱动直接参考父类device标准框架搭建的,如下:
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);
内容很简单直接贴代码,基本git下来的,修改一点点rtc框架就搭建好了
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.搭建应用层demo
开启定时器,查阅时间精度是否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;
/* 设置日期 */
ret = set_date(2022, 7, 2);
if (ret != RT_EOK)
{
rt_kprintf("set RTC date failed\n");
return ret;
}
/* 设置时间 */
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;
/* 获取时间 */
now = time(RT_NULL);
rt_kprintf("%s\n", ctime(&now));
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(rtcsample, rtc sample);
MSH_CMD_EXPORT(rtcprint, rtc print);
输入命令rtcsample运行应用,输入rtcprint打印当前rtc时钟
边栏推荐
- Host based intrusion system IDS
- 29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da
- PHP production website active push (website)
- 29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
- 13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
- 人生还在迷茫?也许这些订阅号里有你需要的答案!
- 数据分析必备的能力
- 線程池:業務代碼最常用也最容易犯錯的組件
- Deep understanding of grouping sets statements in SQL
- Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
猜你喜欢
29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da
What material is sa537cl1? Sa537cl1 corresponds to the national standard material
CC2530 common registers for timer 1
The way of wisdom (unity of knowledge and action)
Simple use of unity pen XR grab
Fast Ethernet and Gigabit Ethernet: what's the difference?
Mysql database DDL and DML
PHP online confusion encryption tutorial sharing + basically no solution
Mysql database -dql
大变局!全国房价,跌破万元大关
随机推荐
What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR
Simple configuration of postfix server
How to promote cross department project collaboration | community essay solicitation
执行脚本不认\r
C语言字符串反转
基于主机的入侵系统IDS
[combinatorics] polynomial theorem (polynomial coefficients | full arrangement of multiple sets | number of schemes corresponding to the ball sub model | polynomial coefficient correlation identity)
Squid service startup script
An example of HP array card troubleshooting
深入理解 SQL 中的 Grouping Sets 语句
Depth first search of graph
Thread pool: the most common and error prone component of business code
How do large consumer enterprises make digital transformation?
The way of wisdom (unity of knowledge and action)
静态程序分析(一)—— 大纲思维导图与内容介绍
跨境电商:外贸企业做海外社媒营销的优势
PHP production website active push (website)
RF Analyze Demo搭建 Step by Step
Squid 服务启动脚本
What is your income level in the country?