66 * Change Logs:
77 * Date Author Notes
88 * 2022-07-25 Rbb666 first version
9+ * 2024-11-06 kurisaw add alarm function
910 */
1011
1112#include <rtthread.h>
2122
2223cyhal_rtc_t rtc_obj ;
2324
24- static rt_rtc_dev_t ifx32_rtc_dev ;
25+ struct rtc_device_object
26+ {
27+ rt_rtc_dev_t rtc_dev ;
28+ #ifdef RT_USING_ALARM
29+ struct rt_rtc_wkalarm wkalarm ;
30+ #endif
31+ };
32+
33+ static struct rtc_device_object ifx32_rtc_dev ;
2534
2635static int get_day_of_week (int day , int month , int year )
2736{
@@ -105,6 +114,10 @@ static rt_err_t _rtc_init(void)
105114 return - RT_ERROR ;
106115 }
107116
117+ #ifdef RT_USING_ALARM
118+ cyhal_rtc_register_callback (& rtc_obj , rtc_alarm_callback , NULL );
119+ cyhal_rtc_enable_event (& rtc_obj , CYHAL_RTC_ALARM , 3u , true);
120+ #endif
108121 return RT_EOK ;
109122}
110123
@@ -133,13 +146,62 @@ static rt_err_t _rtc_set_secs(time_t *sec)
133146 return result ;
134147}
135148
149+ #if defined(RT_USING_ALARM )
150+
151+ static rt_err_t _rtc_get_alarm (struct rt_rtc_wkalarm * alarm )
152+ {
153+ #ifdef RT_USING_ALARM
154+ * alarm = ifx32_rtc_dev .wkalarm ;
155+ LOG_D ("GET_ALARM %d:%d:%d" ,ifx32_rtc_dev .wkalarm .tm_hour ,
156+ ifx32_rtc_dev .wkalarm .tm_min ,ifx32_rtc_dev .wkalarm .tm_sec );
157+ return RT_EOK ;
158+ #else
159+ return - RT_ERROR ;
160+ #endif
161+ }
162+
163+ static rt_err_t _rtc_set_alarm (struct rt_rtc_wkalarm * alarm )
164+ {
165+ #ifdef RT_USING_ALARM
166+ LOG_D ("RT_DEVICE_CTRL_RTC_SET_ALARM" );
167+ if (alarm != RT_NULL )
168+ {
169+ ifx32_rtc_dev .wkalarm .enable = alarm -> enable ;
170+ ifx32_rtc_dev .wkalarm .tm_hour = alarm -> tm_hour ;
171+ ifx32_rtc_dev .wkalarm .tm_min = alarm -> tm_min ;
172+ ifx32_rtc_dev .wkalarm .tm_sec = alarm -> tm_sec ;
173+
174+ cyhal_rtc_set_alarm_by_seconds (& rtc_obj , 1 );
175+ }
176+ else
177+ {
178+ LOG_E ("RT_DEVICE_CTRL_RTC_SET_ALARM error!!" );
179+ return - RT_ERROR ;
180+ }
181+ LOG_D ("SET_ALARM %d:%d:%d" ,alarm -> tm_hour ,
182+ alarm -> tm_min , alarm -> tm_sec );
183+ return RT_EOK ;
184+ #else
185+ return - RT_ERROR ;
186+ #endif
187+ }
188+
189+ #ifdef RT_USING_ALARM
190+ void rtc_alarm_callback (void )
191+ {
192+ rt_interrupt_enter ();
193+ rt_alarm_update (0 , 0 );
194+ rt_interrupt_leave ();
195+ }
196+ #endif
197+
136198static const struct rt_rtc_ops _rtc_ops =
137199{
138200 _rtc_init ,
139201 _rtc_get_secs ,
140202 _rtc_set_secs ,
141- RT_NULL ,
142- RT_NULL ,
203+ _rtc_get_alarm ,
204+ _rtc_set_alarm ,
143205 ifx_rtc_get_timeval ,
144206 RT_NULL ,
145207};
@@ -153,9 +215,9 @@ static int rt_hw_rtc_init(void)
153215{
154216 rt_err_t result = RT_EOK ;
155217
156- ifx32_rtc_dev .ops = & _rtc_ops ;
218+ ifx32_rtc_dev .rtc_dev . ops = & _rtc_ops ;
157219
158- if (rt_hw_rtc_register (& ifx32_rtc_dev , "rtc" , RT_DEVICE_FLAG_RDWR , RT_NULL ) != RT_EOK )
220+ if (rt_hw_rtc_register (& ( ifx32_rtc_dev . rtc_dev ) , "rtc" , RT_DEVICE_FLAG_RDWR , RT_NULL ) != RT_EOK )
159221 {
160222 LOG_E ("rtc init failed" );
161223 result = - RT_ERROR ;
0 commit comments