添加rtthread相关代码
This commit is contained in:
		
							
								
								
									
										65
									
								
								riscv/rtthread/components/drivers/rtc/README.md
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										65
									
								
								riscv/rtthread/components/drivers/rtc/README.md
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,65 @@ | ||||
| # RT-Thread RTC 设备 | ||||
|  | ||||
| ## 1、介绍 | ||||
|  | ||||
| RT-Thread 的 RTC (实时时钟)设备为操作系统的时间系统提供了基础服务。面对越来越多的 IoT 场景,RTC 已经成为产品的标配,甚至在诸如 SSL 的安全传输过程中,RTC 已经成为不可或缺的部分。 | ||||
|  | ||||
| ## 2、使用 | ||||
|  | ||||
| 应用层对于 RTC 设备一般不存在直接调用的 API ,如果使用到 C 标准库中的时间 API (目前主要是获取当前时间的  `time_t time(time_t *t)`),则会间接通过设备的 control 接口完成交互。 | ||||
|  | ||||
| > 注意:目前系统内只允许存在一个 RTC 设备,且名称为 `"rtc"` 。 | ||||
|  | ||||
| ### 2.1  设置日期 | ||||
|  | ||||
| ```C | ||||
| rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day) | ||||
| ``` | ||||
|  | ||||
| |参数                                    |描述| | ||||
| |:-----                                  |:----| | ||||
| |year                                    |待设置生效的年份| | ||||
| |month                                   |待设置生效的月份| | ||||
| |day                                     |待设置生效的日| | ||||
|  | ||||
| ### 2.2 设置时间 | ||||
|  | ||||
| ```C | ||||
| rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second) | ||||
| ``` | ||||
|  | ||||
| |参数                                    |描述| | ||||
| |:-----                                  |:----| | ||||
| |hour                                    |待设置生效的时| | ||||
| |minute                                  |待设置生效的分| | ||||
| |second                                  |待设置生效的秒| | ||||
|  | ||||
| ### 2.3 使用 Finsh/MSH 命令 查看/设置 日期和时间 | ||||
|  | ||||
| #### 2.3.1 查看日期和时间 | ||||
|  | ||||
| 输入 `date` 即可,大致效果如下: | ||||
|  | ||||
| ``` | ||||
| msh />date | ||||
| Fri Feb 16 01:11:56 2018 | ||||
| msh /> | ||||
| ``` | ||||
|  | ||||
| #### 2.3.2 设置日期和时间 | ||||
|  | ||||
| 同样使用 `date` 命令,在命令后面再依次输入 `年` `月` `日` `时` `分` `秒` (中间空格隔开, 24H 制),大致效果如下: | ||||
|  | ||||
| ``` | ||||
| msh />date 2018 02 16 01 15 30    # 设置当前时间为 2018-02-16 01:15:30 | ||||
| msh /> | ||||
| ``` | ||||
|  | ||||
| ### 2.4 启用 Soft RTC (软件模拟 RTC) | ||||
|  | ||||
| 这个模式非常适用于对时间精度要求不高,没有硬件 RTC 的产品。 | ||||
|  | ||||
| #### 2.4.1 使用方法 | ||||
|  | ||||
| 在 menuconfig 中启用 `RT_USING_SOFT_RTC` 配置。 | ||||
|  | ||||
							
								
								
									
										18
									
								
								riscv/rtthread/components/drivers/rtc/SConscript
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										18
									
								
								riscv/rtthread/components/drivers/rtc/SConscript
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| from building import * | ||||
|  | ||||
| cwd = GetCurrentDir() | ||||
| src = [] | ||||
|  | ||||
| CPPPATH = [cwd + '/../include'] | ||||
| group = [] | ||||
|  | ||||
| if GetDepend(['RT_USING_RTC']): | ||||
|     src = src + ['rtc.c'] | ||||
|     if GetDepend(['RT_USING_ALARM']): | ||||
|         src = src + ['alarm.c'] | ||||
|     if GetDepend(['RT_USING_SOFT_RTC']): | ||||
|         src = src + ['soft_rtc.c'] | ||||
|  | ||||
| group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_RTC'], CPPPATH = CPPPATH) | ||||
|  | ||||
| Return('group') | ||||
							
								
								
									
										800
									
								
								riscv/rtthread/components/drivers/rtc/alarm.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										800
									
								
								riscv/rtthread/components/drivers/rtc/alarm.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,800 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author            Notes | ||||
|  * 2012-10-27     heyuanjie87       first version. | ||||
|  * 2013-05-17     aozima            initial alarm event & mutex in system init. | ||||
|  * 2020-10-15     zhangsz           add alarm flags hour minute second. | ||||
|  * 2020-11-09     zhangsz           fix alarm set when modify rtc time. | ||||
|  */ | ||||
|  | ||||
| #include <rtthread.h> | ||||
| #include <rtdevice.h> | ||||
| #include <sys/time.h> | ||||
|  | ||||
| #define RT_RTC_YEARS_MAX         137 | ||||
| #ifdef RT_USING_SOFT_RTC | ||||
| #define RT_ALARM_DELAY             0 | ||||
| #else | ||||
| #define RT_ALARM_DELAY             2 | ||||
| #endif | ||||
|  | ||||
| #if (defined(RT_USING_RTC) && defined(RT_USING_ALARM)) | ||||
| static struct rt_alarm_container _container; | ||||
|  | ||||
| rt_inline rt_uint32_t alarm_mkdaysec(struct tm *time) | ||||
| { | ||||
|     rt_uint32_t sec; | ||||
|  | ||||
|     sec = time->tm_sec; | ||||
|     sec += time->tm_min * 60; | ||||
|     sec += time->tm_hour * 3600; | ||||
|  | ||||
|     return (sec); | ||||
| } | ||||
|  | ||||
| static rt_err_t alarm_set(struct rt_alarm *alarm) | ||||
| { | ||||
|     rt_device_t device; | ||||
|     struct rt_rtc_wkalarm wkalarm; | ||||
|     rt_err_t ret; | ||||
|  | ||||
|     device = rt_device_find("rtc"); | ||||
|  | ||||
|     if (device == RT_NULL) | ||||
|     { | ||||
|         return (RT_ERROR); | ||||
|     } | ||||
|  | ||||
|     if (alarm->flag & RT_ALARM_STATE_START) | ||||
|         wkalarm.enable = RT_TRUE; | ||||
|     else | ||||
|         wkalarm.enable = RT_FALSE; | ||||
|  | ||||
|     wkalarm.tm_sec = alarm->wktime.tm_sec; | ||||
|     wkalarm.tm_min = alarm->wktime.tm_min; | ||||
|     wkalarm.tm_hour = alarm->wktime.tm_hour; | ||||
|     wkalarm.tm_mday = alarm->wktime.tm_mday; | ||||
|     wkalarm.tm_mon = alarm->wktime.tm_mon; | ||||
|     wkalarm.tm_year = alarm->wktime.tm_year; | ||||
|  | ||||
|     ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_ALARM, &wkalarm); | ||||
|     if ((ret == RT_EOK) && wkalarm.enable) | ||||
|     { | ||||
|         ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_ALARM, &wkalarm); | ||||
|         if (ret == RT_EOK) | ||||
|         { | ||||
|             /* | ||||
|               some RTC device like RX8025,it's alarms precision is 1 minute. | ||||
|               in this case,low level RTC driver should set wkalarm->tm_sec to 0. | ||||
|             */ | ||||
|             alarm->wktime.tm_sec = wkalarm.tm_sec; | ||||
|             alarm->wktime.tm_min = wkalarm.tm_min; | ||||
|             alarm->wktime.tm_hour = wkalarm.tm_hour; | ||||
|             alarm->wktime.tm_mday = wkalarm.tm_mday; | ||||
|             alarm->wktime.tm_mon = wkalarm.tm_mon; | ||||
|             alarm->wktime.tm_year = wkalarm.tm_year; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     return (ret); | ||||
| } | ||||
|  | ||||
| static void alarm_wakeup(struct rt_alarm *alarm, struct tm *now) | ||||
| { | ||||
|     rt_uint32_t sec_alarm, sec_now; | ||||
|     rt_bool_t wakeup = RT_FALSE; | ||||
|     time_t timestamp; | ||||
|  | ||||
|     sec_alarm = alarm_mkdaysec(&alarm->wktime); | ||||
|     sec_now = alarm_mkdaysec(now); | ||||
|  | ||||
|     if (alarm->flag & RT_ALARM_STATE_START) | ||||
|     { | ||||
|         switch (alarm->flag & 0xFF00) | ||||
|         { | ||||
|         case RT_ALARM_ONESHOT: | ||||
|         { | ||||
|             sec_alarm = timegm(&alarm->wktime); | ||||
|             sec_now = timegm(now); | ||||
|             if (((sec_now - sec_alarm) <= RT_ALARM_DELAY) && (sec_now >= sec_alarm)) | ||||
|             { | ||||
|                 /* stop alarm */ | ||||
|                 alarm->flag &= ~RT_ALARM_STATE_START; | ||||
|                 alarm_set(alarm); | ||||
|                 wakeup = RT_TRUE; | ||||
|             } | ||||
|         } | ||||
|         break; | ||||
|         case RT_ALARM_SECOND: | ||||
|         { | ||||
|             alarm->wktime.tm_hour = now->tm_hour; | ||||
|             alarm->wktime.tm_min = now->tm_min; | ||||
|             alarm->wktime.tm_sec = now->tm_sec + 1; | ||||
|             if (alarm->wktime.tm_sec > 59) | ||||
|             { | ||||
|                 alarm->wktime.tm_sec = 0; | ||||
|                 alarm->wktime.tm_min = alarm->wktime.tm_min + 1; | ||||
|                 if (alarm->wktime.tm_min > 59) | ||||
|                 { | ||||
|                     alarm->wktime.tm_min = 0; | ||||
|                     alarm->wktime.tm_hour = alarm->wktime.tm_hour + 1; | ||||
|                     if (alarm->wktime.tm_hour > 23) | ||||
|                     { | ||||
|                         alarm->wktime.tm_hour = 0; | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|             wakeup = RT_TRUE; | ||||
|         } | ||||
|         break; | ||||
|         case RT_ALARM_MINUTE: | ||||
|         { | ||||
|             alarm->wktime.tm_hour = now->tm_hour; | ||||
|             if (alarm->wktime.tm_sec == now->tm_sec) | ||||
|             { | ||||
|                 alarm->wktime.tm_min = now->tm_min + 1; | ||||
|                 if (alarm->wktime.tm_min > 59) | ||||
|                 { | ||||
|                     alarm->wktime.tm_min = 0; | ||||
|                     alarm->wktime.tm_hour = alarm->wktime.tm_hour + 1; | ||||
|                     if (alarm->wktime.tm_hour > 23) | ||||
|                     { | ||||
|                         alarm->wktime.tm_hour = 0; | ||||
|                     } | ||||
|                 } | ||||
|                 wakeup = RT_TRUE; | ||||
|             } | ||||
|         } | ||||
|         break; | ||||
|         case RT_ALARM_HOUR: | ||||
|         { | ||||
|             if ((alarm->wktime.tm_min == now->tm_min) && | ||||
|                 (alarm->wktime.tm_sec == now->tm_sec)) | ||||
|             { | ||||
|                 alarm->wktime.tm_hour = now->tm_hour + 1; | ||||
|                 if (alarm->wktime.tm_hour > 23) | ||||
|                 { | ||||
|                     alarm->wktime.tm_hour = 0; | ||||
|                 } | ||||
|                 wakeup = RT_TRUE; | ||||
|             } | ||||
|         } | ||||
|         break; | ||||
|         case RT_ALARM_DAILY: | ||||
|         { | ||||
|             if (((sec_now - sec_alarm) <= RT_ALARM_DELAY) && (sec_now >= sec_alarm)) | ||||
|                 wakeup = RT_TRUE; | ||||
|         } | ||||
|         break; | ||||
|         case RT_ALARM_WEEKLY: | ||||
|         { | ||||
|             /* alarm at wday */ | ||||
|             if (alarm->wktime.tm_wday == now->tm_wday) | ||||
|             { | ||||
|                 sec_alarm += alarm->wktime.tm_wday * 24 * 3600; | ||||
|                 sec_now += now->tm_wday * 24 * 3600; | ||||
|  | ||||
|                 if (sec_now == sec_alarm) | ||||
|                     wakeup = RT_TRUE; | ||||
|             } | ||||
|         } | ||||
|         break; | ||||
|         case RT_ALARM_MONTHLY: | ||||
|         { | ||||
|             /* monthly someday generate alarm signals */ | ||||
|             if (alarm->wktime.tm_mday == now->tm_mday) | ||||
|             { | ||||
|                 if ((sec_now - sec_alarm) <= RT_ALARM_DELAY) | ||||
|                     wakeup = RT_TRUE; | ||||
|             } | ||||
|         } | ||||
|         break; | ||||
|         case RT_ALARM_YAERLY: | ||||
|         { | ||||
|             if ((alarm->wktime.tm_mday == now->tm_mday) && \ | ||||
|                     (alarm->wktime.tm_mon == now->tm_mon)) | ||||
|             { | ||||
|                 if ((sec_now - sec_alarm) <= RT_ALARM_DELAY) | ||||
|                     wakeup = RT_TRUE; | ||||
|             } | ||||
|         } | ||||
|         break; | ||||
|         } | ||||
|  | ||||
|         if ((wakeup == RT_TRUE) && (alarm->callback != RT_NULL)) | ||||
|         { | ||||
|             timestamp = (time_t)0; | ||||
|             get_timestamp(×tamp); | ||||
|             alarm->callback(alarm, timestamp); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| static void alarm_update(rt_uint32_t event) | ||||
| { | ||||
|     struct rt_alarm *alm_prev = RT_NULL, *alm_next = RT_NULL; | ||||
|     struct rt_alarm *alarm; | ||||
|     rt_int32_t sec_now, sec_alarm, sec_tmp; | ||||
|     rt_int32_t sec_next = 24 * 3600, sec_prev = 0; | ||||
|     time_t timestamp = (time_t)0; | ||||
|     struct tm now; | ||||
|     rt_list_t *next; | ||||
|  | ||||
|     rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER); | ||||
|     if (!rt_list_isempty(&_container.head)) | ||||
|     { | ||||
|         /* get time of now */ | ||||
|         get_timestamp(×tamp); | ||||
|         gmtime_r(×tamp, &now); | ||||
|  | ||||
|         for (next = _container.head.next; next != &_container.head; next = next->next) | ||||
|         { | ||||
|             alarm = rt_list_entry(next, struct rt_alarm, list); | ||||
|             /* check the overtime alarm */ | ||||
|             alarm_wakeup(alarm, &now); | ||||
|         } | ||||
|  | ||||
|         /* get time of now */ | ||||
|         get_timestamp(×tamp); | ||||
|         gmtime_r(×tamp, &now); | ||||
|         sec_now = alarm_mkdaysec(&now); | ||||
|  | ||||
|         for (next = _container.head.next; next != &_container.head; next = next->next) | ||||
|         { | ||||
|             alarm = rt_list_entry(next, struct rt_alarm, list); | ||||
|             /* calculate seconds from 00:00:00 */ | ||||
|             sec_alarm = alarm_mkdaysec(&alarm->wktime); | ||||
|             if (alarm->flag & RT_ALARM_STATE_START) | ||||
|             { | ||||
|                 sec_tmp = sec_alarm - sec_now; | ||||
|                 if (sec_tmp > 0) | ||||
|                 { | ||||
|                     /* find alarm after now(now to 23:59:59) and the most recent */ | ||||
|                     if (sec_tmp < sec_next) | ||||
|                     { | ||||
|                         sec_next = sec_tmp; | ||||
|                         alm_next = alarm; | ||||
|                     } | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     /* find alarm before now(00:00:00 to now) and furthest from now */ | ||||
|                     if (sec_tmp < sec_prev) | ||||
|                     { | ||||
|                         sec_prev = sec_tmp; | ||||
|                         alm_prev = alarm; | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /* enable the alarm after now first */ | ||||
|         if (sec_next < 24 * 3600) | ||||
|         { | ||||
|             if (alarm_set(alm_next) == RT_EOK) | ||||
|                 _container.current = alm_next; | ||||
|         } | ||||
|         else if (sec_prev < 0) | ||||
|         { | ||||
|             /* enable the alarm before now */ | ||||
|             if (alarm_set(alm_prev) == RT_EOK) | ||||
|                 _container.current = alm_prev; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             if (_container.current != RT_NULL) | ||||
|             { | ||||
|                 alarm_set(_container.current); | ||||
|                 if (!(_container.current->flag & RT_ALARM_STATE_START)) | ||||
|                     _container.current = RT_NULL; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|     rt_mutex_release(&_container.mutex); | ||||
| } | ||||
|  | ||||
| static int days_of_year_month(int tm_year, int tm_mon) | ||||
| { | ||||
|     int ret, year; | ||||
|  | ||||
|     year = tm_year + 1900; | ||||
|     if (tm_mon == 1) | ||||
|     { | ||||
|         ret = 28 + ((!(year % 4) && (year % 100)) || !(year % 400)); | ||||
|     } | ||||
|     else if (((tm_mon <= 6) && (tm_mon % 2 == 0)) || ((tm_mon > 6) && (tm_mon % 2 == 1))) | ||||
|     { | ||||
|         ret = 31; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         ret = 30; | ||||
|     } | ||||
|  | ||||
|     return (ret); | ||||
| } | ||||
|  | ||||
| static rt_bool_t is_valid_date(struct tm *date) | ||||
| { | ||||
|     if ((date->tm_year < 0) || (date->tm_year > RT_RTC_YEARS_MAX)) | ||||
|     { | ||||
|         return (RT_FALSE); | ||||
|     } | ||||
|  | ||||
|     if ((date->tm_mon < 0) || (date->tm_mon > 11)) | ||||
|     { | ||||
|         return (RT_FALSE); | ||||
|     } | ||||
|  | ||||
|     if ((date->tm_mday < 1) || \ | ||||
|             (date->tm_mday > days_of_year_month(date->tm_year, date->tm_mon))) | ||||
|     { | ||||
|         return (RT_FALSE); | ||||
|     } | ||||
|  | ||||
|     return (RT_TRUE); | ||||
| } | ||||
|  | ||||
| static rt_err_t alarm_setup(rt_alarm_t alarm, struct tm *wktime) | ||||
| { | ||||
|     rt_err_t ret = -RT_ERROR; | ||||
|     time_t timestamp = (time_t)0; | ||||
|     struct tm *setup, now; | ||||
|  | ||||
|     setup = &alarm->wktime; | ||||
|     *setup = *wktime; | ||||
|     /* get time of now */ | ||||
|     get_timestamp(×tamp); | ||||
|     gmtime_r(×tamp, &now); | ||||
|  | ||||
|     /* if these are a "don't care" value,we set them to now*/ | ||||
|     if ((setup->tm_sec > 59) || (setup->tm_sec < 0)) | ||||
|         setup->tm_sec = now.tm_sec; | ||||
|     if ((setup->tm_min > 59) || (setup->tm_min < 0)) | ||||
|         setup->tm_min = now.tm_min; | ||||
|     if ((setup->tm_hour > 23) || (setup->tm_hour < 0)) | ||||
|         setup->tm_hour = now.tm_hour; | ||||
|  | ||||
|     switch (alarm->flag & 0xFF00) | ||||
|     { | ||||
|     case RT_ALARM_SECOND: | ||||
|     { | ||||
|         alarm->wktime.tm_hour = now.tm_hour; | ||||
|         alarm->wktime.tm_min = now.tm_min; | ||||
|         alarm->wktime.tm_sec = now.tm_sec + 1; | ||||
|         if (alarm->wktime.tm_sec > 59) | ||||
|         { | ||||
|             alarm->wktime.tm_sec = 0; | ||||
|             alarm->wktime.tm_min = alarm->wktime.tm_min + 1; | ||||
|             if (alarm->wktime.tm_min > 59) | ||||
|             { | ||||
|                 alarm->wktime.tm_min = 0; | ||||
|                 alarm->wktime.tm_hour = alarm->wktime.tm_hour + 1; | ||||
|                 if (alarm->wktime.tm_hour > 23) | ||||
|                 { | ||||
|                     alarm->wktime.tm_hour = 0; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|     break; | ||||
|     case RT_ALARM_MINUTE: | ||||
|     { | ||||
|         alarm->wktime.tm_hour = now.tm_hour; | ||||
|         alarm->wktime.tm_min = now.tm_min + 1; | ||||
|         if (alarm->wktime.tm_min > 59) | ||||
|         { | ||||
|             alarm->wktime.tm_min = 0; | ||||
|             alarm->wktime.tm_hour = alarm->wktime.tm_hour + 1; | ||||
|             if (alarm->wktime.tm_hour > 23) | ||||
|             { | ||||
|                 alarm->wktime.tm_hour = 0; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|     break; | ||||
|     case RT_ALARM_HOUR: | ||||
|     { | ||||
|         alarm->wktime.tm_hour = now.tm_hour + 1; | ||||
|         if (alarm->wktime.tm_hour > 23) | ||||
|         { | ||||
|             alarm->wktime.tm_hour = 0; | ||||
|         } | ||||
|     } | ||||
|     break; | ||||
|     case RT_ALARM_DAILY: | ||||
|     { | ||||
|         /* do nothing but needed */ | ||||
|     } | ||||
|     break; | ||||
|     case RT_ALARM_ONESHOT: | ||||
|     { | ||||
|         /* if these are "don't care" value we set them to now */ | ||||
|         if (setup->tm_year == RT_ALARM_TM_NOW) | ||||
|             setup->tm_year = now.tm_year; | ||||
|         if (setup->tm_mon == RT_ALARM_TM_NOW) | ||||
|             setup->tm_mon = now.tm_mon; | ||||
|         if (setup->tm_mday == RT_ALARM_TM_NOW) | ||||
|             setup->tm_mday = now.tm_mday; | ||||
|         /* make sure the setup is valid */ | ||||
|         if (!is_valid_date(setup)) | ||||
|             goto _exit; | ||||
|     } | ||||
|     break; | ||||
|     case RT_ALARM_WEEKLY: | ||||
|     { | ||||
|         /* if tm_wday is a "don't care" value we set it to now */ | ||||
|         if ((setup->tm_wday < 0) || (setup->tm_wday > 6)) | ||||
|             setup->tm_wday = now.tm_wday; | ||||
|     } | ||||
|     break; | ||||
|     case RT_ALARM_MONTHLY: | ||||
|     { | ||||
|         /* if tm_mday is a "don't care" value we set it to now */ | ||||
|         if ((setup->tm_mday < 1) || (setup->tm_mday > 31)) | ||||
|             setup->tm_mday = now.tm_mday; | ||||
|     } | ||||
|     break; | ||||
|     case RT_ALARM_YAERLY: | ||||
|     { | ||||
|         /* if tm_mon is a "don't care" value we set it to now */ | ||||
|         if ((setup->tm_mon < 0) || (setup->tm_mon > 11)) | ||||
|             setup->tm_mon = now.tm_mon; | ||||
|  | ||||
|         if (setup->tm_mon == 1) | ||||
|         { | ||||
|             /* tm_mon is February */ | ||||
|  | ||||
|             /* tm_mday should be 1~29.otherwise,it's a "don't care" value */ | ||||
|             if ((setup->tm_mday < 1) || (setup->tm_mday > 29)) | ||||
|                 setup->tm_mday = now.tm_mday; | ||||
|         } | ||||
|         else if (((setup->tm_mon <= 6) && (setup->tm_mon % 2 == 0)) || \ | ||||
|                  ((setup->tm_mon > 6) && (setup->tm_mon % 2 == 1))) | ||||
|         { | ||||
|             /* Jan,Mar,May,Jul,Aug,Oct,Dec */ | ||||
|  | ||||
|             /* tm_mday should be 1~31.otherwise,it's a "don't care" value */ | ||||
|             if ((setup->tm_mday < 1) || (setup->tm_mday > 31)) | ||||
|                 setup->tm_mday = now.tm_mday; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             /* tm_mday should be 1~30.otherwise,it's a "don't care" value */ | ||||
|             if ((setup->tm_mday < 1) || (setup->tm_mday > 30)) | ||||
|                 setup->tm_mday = now.tm_mday; | ||||
|         } | ||||
|     } | ||||
|     break; | ||||
|     default: | ||||
|     { | ||||
|         goto _exit; | ||||
|     } | ||||
|     } | ||||
|  | ||||
|     if ((setup->tm_hour == 23) && (setup->tm_min == 59) && (setup->tm_sec == 59)) | ||||
|     { | ||||
|         /* | ||||
|            for insurance purposes, we will generate an alarm | ||||
|            signal two seconds ahead of. | ||||
|         */ | ||||
|         setup->tm_sec = 60 - RT_ALARM_DELAY; | ||||
|     } | ||||
|     /* set initialized state */ | ||||
|     alarm->flag |= RT_ALARM_STATE_INITED; | ||||
|     ret = RT_EOK; | ||||
|  | ||||
| _exit: | ||||
|  | ||||
|     return (ret); | ||||
| } | ||||
|  | ||||
| /** \brief send a rtc alarm event | ||||
|  * | ||||
|  * \param dev pointer to RTC device(currently unused,you can ignore it) | ||||
|  * \param event RTC event(currently unused) | ||||
|  * \return none | ||||
|  */ | ||||
| void rt_alarm_update(rt_device_t dev, rt_uint32_t event) | ||||
| { | ||||
|     rt_event_send(&_container.event, 1); | ||||
| } | ||||
|  | ||||
| /** \brief modify the alarm setup | ||||
|  * | ||||
|  * \param alarm pointer to alarm | ||||
|  * \param cmd control command | ||||
|  * \param arg argument | ||||
|  */ | ||||
| rt_err_t rt_alarm_control(rt_alarm_t alarm, int cmd, void *arg) | ||||
| { | ||||
|     rt_err_t ret = -RT_ERROR; | ||||
|  | ||||
|     RT_ASSERT(alarm != RT_NULL); | ||||
|  | ||||
|     rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER); | ||||
|     switch (cmd) | ||||
|     { | ||||
|     case RT_ALARM_CTRL_MODIFY: | ||||
|     { | ||||
|         struct rt_alarm_setup *setup; | ||||
|  | ||||
|         RT_ASSERT(arg != RT_NULL); | ||||
|         setup = arg; | ||||
|         rt_alarm_stop(alarm); | ||||
|         alarm->flag = setup->flag & 0xFF00; | ||||
|         alarm->wktime = setup->wktime; | ||||
|         ret = alarm_setup(alarm, &alarm->wktime); | ||||
|     } | ||||
|     break; | ||||
|     } | ||||
|  | ||||
|     rt_mutex_release(&_container.mutex); | ||||
|  | ||||
|     return (ret); | ||||
| } | ||||
|  | ||||
| /** \brief start an alarm | ||||
|  * | ||||
|  * \param alarm pointer to alarm | ||||
|  * \return RT_EOK | ||||
|  */ | ||||
| rt_err_t rt_alarm_start(rt_alarm_t alarm) | ||||
| { | ||||
|     rt_int32_t sec_now, sec_old, sec_new; | ||||
|     rt_err_t ret = RT_EOK; | ||||
|     time_t timestamp = (time_t)0; | ||||
|     struct tm now; | ||||
|  | ||||
|     if (alarm == RT_NULL) | ||||
|         return (RT_ERROR); | ||||
|     rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER); | ||||
|  | ||||
|     if (!(alarm->flag & RT_ALARM_STATE_START)) | ||||
|     { | ||||
|         if (alarm_setup(alarm, &alarm->wktime) != RT_EOK) | ||||
|         { | ||||
|             ret = -RT_ERROR; | ||||
|             goto _exit; | ||||
|         } | ||||
|  | ||||
|         /* get time of now */ | ||||
|         get_timestamp(×tamp); | ||||
|         gmtime_r(×tamp, &now); | ||||
|  | ||||
|         alarm->flag |= RT_ALARM_STATE_START; | ||||
|  | ||||
|         /* set alarm */ | ||||
|         if (_container.current == RT_NULL) | ||||
|         { | ||||
|             ret = alarm_set(alarm); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             sec_now = alarm_mkdaysec(&now); | ||||
|             sec_old = alarm_mkdaysec(&_container.current->wktime); | ||||
|             sec_new = alarm_mkdaysec(&alarm->wktime); | ||||
|  | ||||
|             if ((sec_new < sec_old) && (sec_new > sec_now)) | ||||
|             { | ||||
|                 ret = alarm_set(alarm); | ||||
|             } | ||||
|             else if ((sec_new > sec_now) && (sec_old < sec_now)) | ||||
|             { | ||||
|                 ret = alarm_set(alarm); | ||||
|             } | ||||
|             else if ((sec_new < sec_old) && (sec_old < sec_now)) | ||||
|             { | ||||
|                 ret = alarm_set(alarm); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 ret = RT_EOK; | ||||
|                 goto _exit; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if (ret == RT_EOK) | ||||
|         { | ||||
|             _container.current = alarm; | ||||
|         } | ||||
|     } | ||||
|  | ||||
| _exit: | ||||
|     rt_mutex_release(&_container.mutex); | ||||
|  | ||||
|     return (ret); | ||||
| } | ||||
|  | ||||
| /** \brief stop an alarm | ||||
|  * | ||||
|  * \param alarm pointer to alarm | ||||
|  * \return RT_EOK | ||||
|  */ | ||||
| rt_err_t rt_alarm_stop(rt_alarm_t alarm) | ||||
| { | ||||
|     rt_err_t ret = RT_EOK; | ||||
|  | ||||
|     if (alarm == RT_NULL) | ||||
|         return (RT_ERROR); | ||||
|     rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER); | ||||
|     if (!(alarm->flag & RT_ALARM_STATE_START)) | ||||
|         goto _exit; | ||||
|     /* stop alarm */ | ||||
|     alarm->flag &= ~RT_ALARM_STATE_START; | ||||
|  | ||||
|     if (_container.current == alarm) | ||||
|     { | ||||
|         ret = alarm_set(alarm); | ||||
|         _container.current = RT_NULL; | ||||
|     } | ||||
|  | ||||
|     if (ret == RT_EOK) | ||||
|         alarm_update(0); | ||||
|  | ||||
| _exit: | ||||
|     rt_mutex_release(&_container.mutex); | ||||
|  | ||||
|     return (ret); | ||||
| } | ||||
|  | ||||
| /** \brief delete an alarm | ||||
|  * | ||||
|  * \param alarm pointer to alarm | ||||
|  * \return RT_EOK | ||||
|  */ | ||||
| rt_err_t rt_alarm_delete(rt_alarm_t alarm) | ||||
| { | ||||
|     rt_err_t ret = RT_EOK; | ||||
|  | ||||
|     if (alarm == RT_NULL) | ||||
|         return -RT_ERROR; | ||||
|     rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER); | ||||
|     /* stop the alarm */ | ||||
|     alarm->flag &= ~RT_ALARM_STATE_START; | ||||
|     if (_container.current == alarm) | ||||
|     { | ||||
|         ret = alarm_set(alarm); | ||||
|         _container.current = RT_NULL; | ||||
|         /* set new alarm if necessary */ | ||||
|         alarm_update(0); | ||||
|     } | ||||
|     rt_list_remove(&alarm->list); | ||||
|     rt_free(alarm); | ||||
|  | ||||
|     rt_mutex_release(&_container.mutex); | ||||
|  | ||||
|     return (ret); | ||||
| } | ||||
|  | ||||
| /** \brief create an alarm | ||||
|  * | ||||
|  * \param flag set alarm mode e.g: RT_ALARM_DAILY | ||||
|  * \param setup pointer to setup infomation | ||||
|  */ | ||||
| rt_alarm_t rt_alarm_create(rt_alarm_callback_t callback, struct rt_alarm_setup *setup) | ||||
| { | ||||
|     struct rt_alarm *alarm; | ||||
|  | ||||
|     if (setup == RT_NULL) | ||||
|         return (RT_NULL); | ||||
|  | ||||
|     alarm = rt_malloc(sizeof(struct rt_alarm)); | ||||
|     if (alarm == RT_NULL) | ||||
|         return (RT_NULL); | ||||
|  | ||||
|     rt_list_init(&alarm->list); | ||||
|  | ||||
|     alarm->wktime = setup->wktime; | ||||
|     alarm->flag = setup->flag & 0xFF00; | ||||
|     alarm->callback = callback; | ||||
|     rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER); | ||||
|     rt_list_insert_after(&_container.head, &alarm->list); | ||||
|     rt_mutex_release(&_container.mutex); | ||||
|  | ||||
|     return (alarm); | ||||
| } | ||||
|  | ||||
| /** \brief rtc alarm service thread entry | ||||
|  * | ||||
|  */ | ||||
| static void rt_alarmsvc_thread_init(void *param) | ||||
| { | ||||
|     rt_uint32_t recv; | ||||
|  | ||||
|     _container.current = RT_NULL; | ||||
|  | ||||
|     while (1) | ||||
|     { | ||||
|         if (rt_event_recv(&_container.event, 0xFFFF, | ||||
|                           RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, | ||||
|                           RT_WAITING_FOREVER, &recv) == RT_EOK) | ||||
|         { | ||||
|             alarm_update(recv); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| struct _alarm_flag | ||||
| { | ||||
|     const char* name; | ||||
|     rt_uint32_t flag; | ||||
| }; | ||||
|  | ||||
| static const struct _alarm_flag _alarm_flag_tbl[] = | ||||
| { | ||||
|     {"N",        0xffff}, /* none */ | ||||
|     {"O",       RT_ALARM_ONESHOT}, /* only alarm once */ | ||||
|     {"D",       RT_ALARM_DAILY}, /* alarm everyday */ | ||||
|     {"W",       RT_ALARM_WEEKLY}, /* alarm weekly at Monday or Friday etc. */ | ||||
|     {"Mo",      RT_ALARM_MONTHLY}, /* alarm monthly at someday */ | ||||
|     {"Y",       RT_ALARM_YAERLY}, /* alarm yearly at a certain date */ | ||||
|     {"H",       RT_ALARM_HOUR}, /* alarm each hour at a certain min:second */ | ||||
|     {"M",       RT_ALARM_MINUTE}, /* alarm each minute at a certain second */ | ||||
|     {"S",       RT_ALARM_SECOND}, /* alarm each second */ | ||||
| }; | ||||
|  | ||||
| static rt_uint8_t _alarm_flag_tbl_size = sizeof(_alarm_flag_tbl) / sizeof(_alarm_flag_tbl[0]); | ||||
|  | ||||
| static rt_uint8_t get_alarm_flag_index(rt_uint32_t alarm_flag) | ||||
| { | ||||
|     for (rt_uint8_t index = 0; index < _alarm_flag_tbl_size; index++) | ||||
|     { | ||||
|         alarm_flag &= 0xff00; | ||||
|         if (alarm_flag == _alarm_flag_tbl[index].flag) | ||||
|         { | ||||
|             return index; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| void rt_alarm_dump(void) | ||||
| { | ||||
|     rt_list_t *next; | ||||
|     rt_alarm_t alarm; | ||||
|  | ||||
|     rt_kprintf("| hh:mm:ss | week | flag | en |\n"); | ||||
|     rt_kprintf("+----------+------+------+----+\n"); | ||||
|     for (next = _container.head.next; next != &_container.head; next = next->next) | ||||
|     { | ||||
|         alarm = rt_list_entry(next, struct rt_alarm, list); | ||||
|         rt_uint8_t flag_index = get_alarm_flag_index(alarm->flag); | ||||
|         rt_kprintf("| %02d:%02d:%02d |  %2d  |  %2s  | %2d |\n", | ||||
|             alarm->wktime.tm_hour, alarm->wktime.tm_min, alarm->wktime.tm_sec, | ||||
|             alarm->wktime.tm_wday, _alarm_flag_tbl[flag_index].name, alarm->flag & RT_ALARM_STATE_START); | ||||
|     } | ||||
|     rt_kprintf("+----------+------+------+----+\n"); | ||||
| } | ||||
|  | ||||
| MSH_CMD_EXPORT_ALIAS(rt_alarm_dump, list_alarm, list alarm info); | ||||
|  | ||||
| /** \brief initialize alarm service system | ||||
|  * | ||||
|  * \param none | ||||
|  * \return none | ||||
|  */ | ||||
| int rt_alarm_system_init(void) | ||||
| { | ||||
|     rt_thread_t tid; | ||||
|  | ||||
|     rt_list_init(&_container.head); | ||||
|     rt_event_init(&_container.event, "alarmsvc", RT_IPC_FLAG_FIFO); | ||||
|     rt_mutex_init(&_container.mutex, "alarmsvc", RT_IPC_FLAG_PRIO); | ||||
|  | ||||
|     tid = rt_thread_create("alarmsvc", | ||||
|                            rt_alarmsvc_thread_init, RT_NULL, | ||||
|                            2048, 10, 5); | ||||
|     if (tid != RT_NULL) | ||||
|         rt_thread_startup(tid); | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| INIT_PREV_EXPORT(rt_alarm_system_init); | ||||
| #endif | ||||
							
								
								
									
										375
									
								
								riscv/rtthread/components/drivers/rtc/rtc.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										375
									
								
								riscv/rtthread/components/drivers/rtc/rtc.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,375 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2012-01-29     aozima       first version. | ||||
|  * 2012-04-12     aozima       optimization: find rtc device only first. | ||||
|  * 2012-04-16     aozima       add scheduler lock for set_date and set_time. | ||||
|  * 2018-02-16     armink       add auto sync time by NTP | ||||
|  * 2021-05-09     Meco Man     remove NTP | ||||
|  * 2021-06-11     iysheng      implement RTC framework V2.0 | ||||
|  * 2021-07-30     Meco Man     move rtc_core.c to rtc.c | ||||
|  */ | ||||
|  | ||||
| #include <string.h> | ||||
| #include <stdlib.h> | ||||
| #include <rtthread.h> | ||||
| #include <drivers/rtc.h> | ||||
|  | ||||
| #ifdef RT_USING_RTC | ||||
|  | ||||
| static rt_device_t _rtc_device; | ||||
| /* | ||||
|  * This function initializes rtc_core | ||||
|  */ | ||||
| static rt_err_t rt_rtc_init(struct rt_device *dev) | ||||
| { | ||||
|     rt_rtc_dev_t *rtc_core; | ||||
|  | ||||
|     RT_ASSERT(dev != RT_NULL); | ||||
|     rtc_core = (rt_rtc_dev_t *)dev; | ||||
|     if (rtc_core->ops->init) | ||||
|     { | ||||
|         return (rtc_core->ops->init()); | ||||
|     } | ||||
|  | ||||
|     return -RT_ENOSYS; | ||||
| } | ||||
|  | ||||
| static rt_err_t rt_rtc_open(struct rt_device *dev, rt_uint16_t oflag) | ||||
| { | ||||
|     return RT_EOK; | ||||
| } | ||||
|  | ||||
| static rt_err_t rt_rtc_close(struct rt_device *dev) | ||||
| { | ||||
|     /* Add close member function in rt_rtc_ops when need, | ||||
|      * then call that function here. | ||||
|      * */ | ||||
|     return RT_EOK; | ||||
| } | ||||
|  | ||||
| static rt_err_t rt_rtc_control(struct rt_device *dev, int cmd, void *args) | ||||
| { | ||||
| #define TRY_DO_RTC_FUNC(rt_rtc_dev, func_name, args) \ | ||||
|     rt_rtc_dev->ops->func_name ?  rt_rtc_dev->ops->func_name(args) : -RT_EINVAL; | ||||
|  | ||||
|     rt_rtc_dev_t *rtc_device; | ||||
|     rt_err_t ret = -RT_EINVAL; | ||||
|  | ||||
|     RT_ASSERT(dev != RT_NULL); | ||||
|     rtc_device = (rt_rtc_dev_t *)dev; | ||||
|  | ||||
|     switch (cmd) | ||||
|     { | ||||
|         case RT_DEVICE_CTRL_RTC_GET_TIME: | ||||
|             ret = TRY_DO_RTC_FUNC(rtc_device, get_secs, args); | ||||
|             break; | ||||
|         case RT_DEVICE_CTRL_RTC_SET_TIME: | ||||
|             ret = TRY_DO_RTC_FUNC(rtc_device, set_secs, args); | ||||
|             break; | ||||
|         case RT_DEVICE_CTRL_RTC_GET_TIMEVAL: | ||||
|             ret = TRY_DO_RTC_FUNC(rtc_device, get_timeval, args); | ||||
|             break; | ||||
|         case RT_DEVICE_CTRL_RTC_SET_TIMEVAL: | ||||
|             ret = TRY_DO_RTC_FUNC(rtc_device, set_timeval, args); | ||||
|             break; | ||||
|         case RT_DEVICE_CTRL_RTC_GET_ALARM: | ||||
|             ret = TRY_DO_RTC_FUNC(rtc_device, get_alarm, args); | ||||
|             break; | ||||
|         case RT_DEVICE_CTRL_RTC_SET_ALARM: | ||||
|             ret = TRY_DO_RTC_FUNC(rtc_device, set_alarm, args); | ||||
|             break; | ||||
|         default: | ||||
|             break; | ||||
|     } | ||||
|  | ||||
|     return ret; | ||||
|  | ||||
| #undef TRY_DO_RTC_FUNC | ||||
| } | ||||
|  | ||||
| #ifdef RT_USING_DEVICE_OPS | ||||
| const static struct rt_device_ops rtc_core_ops = | ||||
| { | ||||
|     rt_rtc_init, | ||||
|     rt_rtc_open, | ||||
|     rt_rtc_close, | ||||
|     RT_NULL, | ||||
|     RT_NULL, | ||||
|     rt_rtc_control, | ||||
| }; | ||||
| #endif /* RT_USING_DEVICE_OPS */ | ||||
|  | ||||
| rt_err_t rt_hw_rtc_register(rt_rtc_dev_t  *rtc, | ||||
|                             const char    *name, | ||||
|                             rt_uint32_t    flag, | ||||
|                             void          *data) | ||||
| { | ||||
|     struct rt_device *device; | ||||
|     RT_ASSERT(rtc != RT_NULL); | ||||
|  | ||||
|     device = &(rtc->parent); | ||||
|  | ||||
|     device->type        = RT_Device_Class_RTC; | ||||
|     device->rx_indicate = RT_NULL; | ||||
|     device->tx_complete = RT_NULL; | ||||
|  | ||||
| #ifdef RT_USING_DEVICE_OPS | ||||
|     device->ops         = &rtc_core_ops; | ||||
| #else | ||||
|     device->init        = rt_rtc_init; | ||||
|     device->open        = rt_rtc_open; | ||||
|     device->close       = rt_rtc_close; | ||||
|     device->read        = RT_NULL; | ||||
|     device->write       = RT_NULL; | ||||
|     device->control     = rt_rtc_control; | ||||
| #endif /* RT_USING_DEVICE_OPS */ | ||||
|     device->user_data   = data; | ||||
|  | ||||
|     /* register a character device */ | ||||
|     return rt_device_register(device, name, flag); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * Set system date(time not modify, local timezone). | ||||
|  * | ||||
|  * @param rt_uint32_t year  e.g: 2012. | ||||
|  * @param rt_uint32_t month e.g: 12 (1~12). | ||||
|  * @param rt_uint32_t day   e.g: 31. | ||||
|  * | ||||
|  * @return rt_err_t if set success, return RT_EOK. | ||||
|  */ | ||||
| rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day) | ||||
| { | ||||
|     time_t now, old_timestamp = 0; | ||||
|     struct tm tm_new = {0}; | ||||
|     rt_err_t ret = -RT_ERROR; | ||||
|  | ||||
|     if (_rtc_device == RT_NULL) | ||||
|     { | ||||
|         _rtc_device = rt_device_find("rtc"); | ||||
|         if (_rtc_device == RT_NULL) | ||||
|         { | ||||
|             return -RT_ERROR; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /* get current time */ | ||||
|     ret = rt_device_control(_rtc_device, RT_DEVICE_CTRL_RTC_GET_TIME, &old_timestamp); | ||||
|     if (ret != RT_EOK) | ||||
|     { | ||||
|         return ret; | ||||
|     } | ||||
|  | ||||
|     /* converts calendar time into local time. */ | ||||
|     localtime_r(&old_timestamp, &tm_new); | ||||
|  | ||||
|     /* update date. */ | ||||
|     tm_new.tm_year = year - 1900; | ||||
|     tm_new.tm_mon  = month - 1; /* tm_mon: 0~11 */ | ||||
|     tm_new.tm_mday = day; | ||||
|  | ||||
|     /* converts the local time into the calendar time. */ | ||||
|     now = mktime(&tm_new); | ||||
|  | ||||
|     /* update to RTC device. */ | ||||
|     ret = rt_device_control(_rtc_device, RT_DEVICE_CTRL_RTC_SET_TIME, &now); | ||||
|     return ret; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * Set system time(date not modify, local timezone). | ||||
|  * | ||||
|  * @param rt_uint32_t hour   e.g: 0~23. | ||||
|  * @param rt_uint32_t minute e.g: 0~59. | ||||
|  * @param rt_uint32_t second e.g: 0~59. | ||||
|  * | ||||
|  * @return rt_err_t if set success, return RT_EOK. | ||||
|  */ | ||||
| rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second) | ||||
| { | ||||
|     time_t now, old_timestamp = 0; | ||||
|     struct tm tm_new = {0}; | ||||
|     rt_err_t ret = -RT_ERROR; | ||||
|  | ||||
|     if (_rtc_device == RT_NULL) | ||||
|     { | ||||
|         _rtc_device = rt_device_find("rtc"); | ||||
|         if (_rtc_device == RT_NULL) | ||||
|         { | ||||
|             return -RT_ERROR; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /* get current time */ | ||||
|     ret = rt_device_control(_rtc_device, RT_DEVICE_CTRL_RTC_GET_TIME, &old_timestamp); | ||||
|     if (ret != RT_EOK) | ||||
|     { | ||||
|         return ret; | ||||
|     } | ||||
|  | ||||
|     /* converts calendar time into local time. */ | ||||
|     localtime_r(&old_timestamp, &tm_new); | ||||
|  | ||||
|     /* update time. */ | ||||
|     tm_new.tm_hour = hour; | ||||
|     tm_new.tm_min  = minute; | ||||
|     tm_new.tm_sec  = second; | ||||
|  | ||||
|     /* converts the local time into the calendar time. */ | ||||
|     now = mktime(&tm_new); | ||||
|  | ||||
|     /* update to RTC device. */ | ||||
|     ret = rt_device_control(_rtc_device, RT_DEVICE_CTRL_RTC_SET_TIME, &now); | ||||
|     return ret; | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * Set timestamp(UTC). | ||||
|  * | ||||
|  * @param time_t timestamp | ||||
|  * | ||||
|  * @return rt_err_t if set success, return RT_EOK. | ||||
|  */ | ||||
| rt_err_t set_timestamp(time_t timestamp) | ||||
| { | ||||
|     if (_rtc_device == RT_NULL) | ||||
|     { | ||||
|         _rtc_device = rt_device_find("rtc"); | ||||
|         if (_rtc_device == RT_NULL) | ||||
|         { | ||||
|             return -RT_ERROR; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /* update to RTC device. */ | ||||
|     return rt_device_control(_rtc_device, RT_DEVICE_CTRL_RTC_SET_TIME, ×tamp); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * Get timestamp(UTC). | ||||
|  * | ||||
|  * @param time_t* timestamp | ||||
|  * | ||||
|  * @return rt_err_t if set success, return RT_EOK. | ||||
|  */ | ||||
| rt_err_t get_timestamp(time_t *timestamp) | ||||
| { | ||||
|     if (_rtc_device == RT_NULL) | ||||
|     { | ||||
|         _rtc_device = rt_device_find("rtc"); | ||||
|         if (_rtc_device == RT_NULL) | ||||
|         { | ||||
|             return -RT_ERROR; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /* Get timestamp from RTC device. */ | ||||
|     return rt_device_control(_rtc_device, RT_DEVICE_CTRL_RTC_GET_TIME, timestamp); | ||||
| } | ||||
|  | ||||
| #ifdef RT_USING_FINSH | ||||
| #include <finsh.h> | ||||
| /** | ||||
|  * get date and time or set (local timezone) [year month day hour min sec] | ||||
|  */ | ||||
| static void date(int argc, char **argv) | ||||
| { | ||||
|     time_t now = (time_t)0; | ||||
|  | ||||
|     if (argc == 1) | ||||
|     { | ||||
|         struct timeval tv = { 0 }; | ||||
|         int32_t tz_offset_sec = 0; | ||||
|         uint32_t abs_tz_offset_sec = 0U; | ||||
|  | ||||
| #if defined(RT_LIBC_USING_LIGHT_TZ_DST) | ||||
|         tz_offset_sec = rt_tz_get(); | ||||
| #endif /* RT_LIBC_USING_LIGHT_TZ_DST */ | ||||
|  | ||||
|         gettimeofday(&tv, RT_NULL); | ||||
|         now = tv.tv_sec; | ||||
|  | ||||
|         abs_tz_offset_sec = tz_offset_sec > 0 ? tz_offset_sec : -tz_offset_sec; | ||||
|         /* output current time */ | ||||
|         rt_kprintf("local time: %.*s", 25U, ctime(&now)); | ||||
|         rt_kprintf("timestamps: %ld\n", (long)tv.tv_sec); | ||||
|         rt_kprintf("timezone: UTC%c%02d:%02d:%02d\n", | ||||
|             tz_offset_sec > 0 ? '+' : '-', abs_tz_offset_sec / 3600U, abs_tz_offset_sec % 3600U / 60U, abs_tz_offset_sec % 3600U % 60U); | ||||
|     } | ||||
|     else if (argc >= 7) | ||||
|     { | ||||
|         /* set time and date */ | ||||
|         struct tm tm_new = {0}; | ||||
|         time_t old = (time_t)0; | ||||
|         rt_err_t err; | ||||
|  | ||||
|         tm_new.tm_year = atoi(argv[1]) - 1900; | ||||
|         tm_new.tm_mon = atoi(argv[2]) - 1; /* .tm_min's range is [0-11] */ | ||||
|         tm_new.tm_mday = atoi(argv[3]); | ||||
|         tm_new.tm_hour = atoi(argv[4]); | ||||
|         tm_new.tm_min = atoi(argv[5]); | ||||
|         tm_new.tm_sec = atoi(argv[6]); | ||||
|         if (tm_new.tm_year <= 0) | ||||
|         { | ||||
|             rt_kprintf("year is out of range [1900-]\n"); | ||||
|             return; | ||||
|         } | ||||
|         if (tm_new.tm_mon > 11) /* .tm_min's range is [0-11] */ | ||||
|         { | ||||
|             rt_kprintf("month is out of range [1-12]\n"); | ||||
|             return; | ||||
|         } | ||||
|         if (tm_new.tm_mday == 0 || tm_new.tm_mday > 31) | ||||
|         { | ||||
|             rt_kprintf("day is out of range [1-31]\n"); | ||||
|             return; | ||||
|         } | ||||
|         if (tm_new.tm_hour > 23) | ||||
|         { | ||||
|             rt_kprintf("hour is out of range [0-23]\n"); | ||||
|             return; | ||||
|         } | ||||
|         if (tm_new.tm_min > 59) | ||||
|         { | ||||
|             rt_kprintf("minute is out of range [0-59]\n"); | ||||
|             return; | ||||
|         } | ||||
|         if (tm_new.tm_sec > 60) | ||||
|         { | ||||
|             rt_kprintf("second is out of range [0-60]\n"); | ||||
|             return; | ||||
|         } | ||||
|         /* save old timestamp */ | ||||
|         err = get_timestamp(&old); | ||||
|         if (err != RT_EOK) | ||||
|         { | ||||
|             rt_kprintf("Get current timestamp failed. %d\n", err); | ||||
|             return; | ||||
|         } | ||||
|         /* converts the local time into the calendar time. */ | ||||
|         now = mktime(&tm_new); | ||||
|         err = set_timestamp(now); | ||||
|         if (err != RT_EOK) | ||||
|         { | ||||
|             rt_kprintf("set date failed. %d\n", err); | ||||
|             return; | ||||
|         } | ||||
|         get_timestamp(&now); /* get new timestamp */ | ||||
|         rt_kprintf("old: %.*s", 25, ctime(&old)); | ||||
|         rt_kprintf("now: %.*s", 25, ctime(&now)); | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         rt_kprintf("please input: date [year month day hour min sec] or date\n"); | ||||
|         rt_kprintf("e.g: date 2018 01 01 23 59 59 or date\n"); | ||||
|     } | ||||
| } | ||||
| MSH_CMD_EXPORT(date, get date and time or set (local timezone) [year month day hour min sec]) | ||||
| #endif /* RT_USING_FINSH */ | ||||
|  | ||||
| #endif /* RT_USING_RTC */ | ||||
							
								
								
									
										325
									
								
								riscv/rtthread/components/drivers/rtc/soft_rtc.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										325
									
								
								riscv/rtthread/components/drivers/rtc/soft_rtc.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,325 @@ | ||||
| /* | ||||
|  * Copyright (c) 2006-2023, RT-Thread Development Team | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  * | ||||
|  * Change Logs: | ||||
|  * Date           Author       Notes | ||||
|  * 2018-01-30     armink       the first version | ||||
|  */ | ||||
|  | ||||
| #include <sys/time.h> | ||||
| #include <string.h> | ||||
| #include <rtthread.h> | ||||
| #include <rtdevice.h> | ||||
|  | ||||
| #ifdef RT_USING_KTIME | ||||
| #include <ktime.h> | ||||
| #endif | ||||
|  | ||||
| #ifdef RT_USING_SOFT_RTC | ||||
|  | ||||
| /* 2018-01-30 14:44:50 = RTC_TIME_INIT(2018, 1, 30, 14, 44, 50)  */ | ||||
| #define RTC_TIME_INIT(year, month, day, hour, minute, second)        \ | ||||
|     {.tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second} | ||||
|  | ||||
| #ifndef SOFT_RTC_TIME_DEFAULT | ||||
| #define SOFT_RTC_TIME_DEFAULT                    RTC_TIME_INIT(2018, 1, 1, 0, 0 ,0) | ||||
| #endif | ||||
|  | ||||
| #ifndef RTC_AUTO_SYNC_FIRST_DELAY | ||||
| #define RTC_AUTO_SYNC_FIRST_DELAY 25 | ||||
| #endif | ||||
| #ifndef RTC_AUTO_SYNC_PERIOD | ||||
| #define RTC_AUTO_SYNC_PERIOD 3600 | ||||
| #endif | ||||
|  | ||||
| static struct rt_work rtc_sync_work; | ||||
| static rt_device_t    source_device = RT_NULL; | ||||
|  | ||||
| static struct rt_device soft_rtc_dev; | ||||
| static rt_tick_t init_tick; | ||||
| static time_t init_time; | ||||
| static struct timeval init_tv = {0}; | ||||
|  | ||||
| #ifdef RT_USING_KTIME | ||||
| static struct timespec init_ts = {0}; | ||||
| #endif | ||||
|  | ||||
| #ifdef RT_USING_ALARM | ||||
|  | ||||
| static struct rt_rtc_wkalarm wkalarm; | ||||
| static struct rt_timer alarm_time; | ||||
|  | ||||
| static void alarm_timeout(void *param) | ||||
| { | ||||
|     rt_alarm_update(param, 1); | ||||
| } | ||||
|  | ||||
| static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm) | ||||
| { | ||||
|     rt_tick_t next_tick; | ||||
|  | ||||
|     if (palarm->enable) | ||||
|     { | ||||
|         next_tick = RT_TICK_PER_SECOND; | ||||
|         rt_timer_control(&alarm_time, RT_TIMER_CTRL_SET_TIME, &next_tick); | ||||
|         rt_timer_start(&alarm_time); | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         rt_timer_stop(&alarm_time); | ||||
|     } | ||||
| } | ||||
|  | ||||
| #endif | ||||
|  | ||||
| static void set_rtc_time(time_t t) | ||||
| { | ||||
|     init_time = t - (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND; | ||||
| #ifdef RT_USING_ALARM | ||||
|     soft_rtc_alarm_update(&wkalarm); | ||||
| #endif | ||||
| } | ||||
|  | ||||
| static void _source_device_control(int cmd, void *args) | ||||
| { | ||||
|     if (source_device == RT_NULL) | ||||
|         return; | ||||
|  | ||||
|     if (rt_device_open(source_device, 0) == RT_EOK) | ||||
|     { | ||||
|         rt_device_control(source_device, cmd, args); | ||||
|         rt_device_close(source_device); | ||||
|     } | ||||
| } | ||||
|  | ||||
| static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) | ||||
| { | ||||
|     time_t *t; | ||||
|     struct tm time_temp; | ||||
|  | ||||
|     RT_ASSERT(dev != RT_NULL); | ||||
|     rt_memset(&time_temp, 0, sizeof(struct tm)); | ||||
|  | ||||
|     switch (cmd) | ||||
|     { | ||||
|     case RT_DEVICE_CTRL_RTC_GET_TIME: | ||||
|     { | ||||
|         t = (time_t *) args; | ||||
|         *t = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND; | ||||
|         break; | ||||
|     } | ||||
|     case RT_DEVICE_CTRL_RTC_SET_TIME: | ||||
|     { | ||||
|         t = (time_t *) args; | ||||
|         set_rtc_time(*t); | ||||
|         _source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, t); | ||||
|         break; | ||||
|     } | ||||
| #ifdef RT_USING_ALARM | ||||
|     case RT_DEVICE_CTRL_RTC_GET_ALARM: | ||||
|         *((struct rt_rtc_wkalarm *)args) = wkalarm; | ||||
|         break; | ||||
|     case RT_DEVICE_CTRL_RTC_SET_ALARM: | ||||
|         wkalarm = *((struct rt_rtc_wkalarm *)args); | ||||
|         soft_rtc_alarm_update(&wkalarm); | ||||
|         break; | ||||
| #endif | ||||
| #ifdef RT_USING_KTIME | ||||
|     case RT_DEVICE_CTRL_RTC_GET_TIMEVAL: | ||||
|     { | ||||
|         struct timeval _tv; | ||||
|         struct timeval *tv = (struct timeval *)args; | ||||
|         rt_ktime_boottime_get_us(&_tv); | ||||
|         tv->tv_sec  = init_time + _tv.tv_sec; | ||||
|         tv->tv_usec = init_tv.tv_usec + _tv.tv_usec; | ||||
|         break; | ||||
|     } | ||||
|     case RT_DEVICE_CTRL_RTC_SET_TIMEVAL: | ||||
|     { | ||||
|         struct timeval _tv; | ||||
|         struct timeval *tv = (struct timeval *)args; | ||||
|         rt_ktime_boottime_get_us(&_tv); | ||||
|         set_rtc_time(tv->tv_sec); | ||||
|         init_tv.tv_usec = tv->tv_usec - _tv.tv_usec; | ||||
|         _source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, &(tv->tv_sec)); | ||||
|         break; | ||||
|     } | ||||
|     case RT_DEVICE_CTRL_RTC_GET_TIMESPEC: | ||||
|     { | ||||
|         struct timespec _ts; | ||||
|         struct timespec *ts = (struct timespec *)args; | ||||
|         rt_ktime_boottime_get_ns(&_ts); | ||||
|         ts->tv_sec  = init_time + _ts.tv_sec; | ||||
|         ts->tv_nsec = init_ts.tv_nsec + _ts.tv_nsec; | ||||
|         break; | ||||
|     } | ||||
|     case RT_DEVICE_CTRL_RTC_SET_TIMESPEC: | ||||
|     { | ||||
|         struct timespec _ts; | ||||
|         struct timespec *ts = (struct timespec *)args; | ||||
|         rt_ktime_boottime_get_ns(&_ts); | ||||
|         set_rtc_time(ts->tv_sec); | ||||
|         init_ts.tv_nsec = ts->tv_nsec - _ts.tv_nsec; | ||||
|         _source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, &(ts->tv_sec)); | ||||
|         break; | ||||
|     } | ||||
|     case RT_DEVICE_CTRL_RTC_GET_TIMERES: | ||||
|     { | ||||
|         struct timespec *ts = (struct timespec *)args; | ||||
|         ts->tv_sec  = 0; | ||||
|         ts->tv_nsec = (rt_ktime_cputimer_getres() / RT_KTIME_RESMUL); | ||||
|         break; | ||||
|     } | ||||
| #else | ||||
|     case RT_DEVICE_CTRL_RTC_GET_TIMEVAL: | ||||
|     { | ||||
|         struct timeval *tv = (struct timeval *)args; | ||||
|         rt_tick_t tick = rt_tick_get() - init_tick; | ||||
|         tv->tv_sec = init_time + tick / RT_TICK_PER_SECOND; | ||||
|         tv->tv_usec = init_tv.tv_usec + ((tick % RT_TICK_PER_SECOND) * (1000000 / RT_TICK_PER_SECOND)); | ||||
|         break; | ||||
|     } | ||||
|     case RT_DEVICE_CTRL_RTC_SET_TIMEVAL: | ||||
|     { | ||||
|         struct timeval *tv = (struct timeval *)args; | ||||
|         rt_tick_t tick = rt_tick_get() - init_tick; | ||||
|         set_rtc_time(tv->tv_sec); | ||||
|         init_tv.tv_usec = tv->tv_usec - ((tick % RT_TICK_PER_SECOND) * (1000000 / RT_TICK_PER_SECOND)); | ||||
|         _source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, &(tv->tv_sec)); | ||||
|         break; | ||||
|     } | ||||
|     case RT_DEVICE_CTRL_RTC_GET_TIMERES: | ||||
|     { | ||||
|         struct timespec *ts = (struct timespec *)args; | ||||
|         ts->tv_sec  = 0; | ||||
|         ts->tv_nsec = (1000UL * 1000 * 1000) / RT_TICK_PER_SECOND; | ||||
|         break; | ||||
|     } | ||||
| #endif /* RT_USING_KTIME */ | ||||
|     default: | ||||
|         return -RT_EINVAL; | ||||
|     } | ||||
|  | ||||
|     return RT_EOK; | ||||
| } | ||||
|  | ||||
| #ifdef RT_USING_DEVICE_OPS | ||||
| const static struct rt_device_ops soft_rtc_ops = | ||||
| { | ||||
|     RT_NULL, | ||||
|     RT_NULL, | ||||
|     RT_NULL, | ||||
|     RT_NULL, | ||||
|     RT_NULL, | ||||
|     soft_rtc_control | ||||
| }; | ||||
| #endif | ||||
|  | ||||
| static int rt_soft_rtc_init(void) | ||||
| { | ||||
|     static rt_bool_t init_ok = RT_FALSE; | ||||
|     struct tm time_new = SOFT_RTC_TIME_DEFAULT; | ||||
|  | ||||
|     if (init_ok) | ||||
|     { | ||||
|         return 0; | ||||
|     } | ||||
|     /* make sure only one 'rtc' device */ | ||||
|     RT_ASSERT(!rt_device_find("rtc")); | ||||
|  | ||||
| #ifdef RT_USING_ALARM | ||||
|     rt_timer_init(&alarm_time, | ||||
|                   "alarm", | ||||
|                   alarm_timeout, | ||||
|                   &soft_rtc_dev, | ||||
|                   0, | ||||
|                   RT_TIMER_FLAG_SOFT_TIMER|RT_TIMER_FLAG_ONE_SHOT); | ||||
| #endif | ||||
|  | ||||
|     init_tick = rt_tick_get(); | ||||
|     init_time = timegm(&time_new); | ||||
|  | ||||
|     soft_rtc_dev.type    = RT_Device_Class_RTC; | ||||
|  | ||||
|     /* register rtc device */ | ||||
| #ifdef RT_USING_DEVICE_OPS | ||||
|     soft_rtc_dev.ops     = &soft_rtc_ops; | ||||
| #else | ||||
|     soft_rtc_dev.init    = RT_NULL; | ||||
|     soft_rtc_dev.open    = RT_NULL; | ||||
|     soft_rtc_dev.close   = RT_NULL; | ||||
|     soft_rtc_dev.read    = RT_NULL; | ||||
|     soft_rtc_dev.write   = RT_NULL; | ||||
|     soft_rtc_dev.control = soft_rtc_control; | ||||
| #endif | ||||
|  | ||||
|     /* no private */ | ||||
|     soft_rtc_dev.user_data = RT_NULL; | ||||
|  | ||||
|     rt_device_register(&soft_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR); | ||||
|  | ||||
|     init_ok = RT_TRUE; | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
| INIT_DEVICE_EXPORT(rt_soft_rtc_init); | ||||
|  | ||||
| #ifdef RT_USING_SYSTEM_WORKQUEUE | ||||
|  | ||||
| rt_err_t rt_soft_rtc_sync(void) | ||||
| { | ||||
|     time_t time = 0; | ||||
|  | ||||
|     if (source_device == RT_NULL) | ||||
|     { | ||||
|         rt_kprintf("error: rtc source not found, please set it!!!\n"); | ||||
|         return RT_ENOSYS; | ||||
|     } | ||||
|  | ||||
|     _source_device_control(RT_DEVICE_CTRL_RTC_GET_TIME, &time); | ||||
|     set_rtc_time(time); | ||||
|     return RT_EOK; | ||||
| } | ||||
|  | ||||
| static void rtc_sync_work_func(struct rt_work *work, void *work_data) | ||||
| { | ||||
|     rt_soft_rtc_sync(); | ||||
|     rt_work_submit(work, rt_tick_from_millisecond(RTC_AUTO_SYNC_PERIOD * 1000)); | ||||
| } | ||||
|  | ||||
| rt_err_t rt_soft_rtc_set_source(const char *name) | ||||
| { | ||||
|     RT_ASSERT(name != RT_NULL); | ||||
|     RT_ASSERT(rt_device_find(name));  // make sure source is exist | ||||
|  | ||||
|     source_device = rt_device_find(name); | ||||
|     rt_work_init(&rtc_sync_work, rtc_sync_work_func, RT_NULL); | ||||
|     rt_work_submit(&rtc_sync_work, rt_tick_from_millisecond(RTC_AUTO_SYNC_FIRST_DELAY * 1000)); | ||||
|  | ||||
|     return RT_EOK; | ||||
| } | ||||
|  | ||||
| #ifdef FINSH_USING_MSH | ||||
| #include <finsh.h> | ||||
| static void cmd_rtc_sync(int argc, char **argv) | ||||
| { | ||||
|     struct timeval  tv  = {0}; | ||||
|     struct timezone tz  = {0}; | ||||
|     time_t          now = (time_t)0; | ||||
|  | ||||
|     rt_soft_rtc_sync(); | ||||
|  | ||||
|     gettimeofday(&tv, &tz); | ||||
|     now = tv.tv_sec; | ||||
|     /* output current time */ | ||||
|     rt_kprintf("local time: %.*s", 25, ctime(&now)); | ||||
|     rt_kprintf("timestamps: %ld\n", (long)tv.tv_sec); | ||||
| } | ||||
| MSH_CMD_EXPORT_ALIAS(cmd_rtc_sync, rtc_sync, Update time by real rtc); | ||||
| #endif | ||||
|  | ||||
| #endif /* RT_USING_SYSTEM_WORKQUEUE */ | ||||
|  | ||||
| #endif /* RT_USING_SOFT_RTC */ | ||||
		Reference in New Issue
	
	Block a user