| 30 | } |
| 31 | |
| 32 | static rt_err_t rtc_control(rt_device_t dev, int cmd, void *args) |
| 33 | { |
| 34 | rt_err_t result = RT_EOK; |
| 35 | struct tm time_temp; |
| 36 | struct tm *time_now; |
| 37 | struct rtc_time hal_rtc_time; |
| 38 | switch (cmd) |
| 39 | { |
| 40 | case RT_DEVICE_CTRL_RTC_GET_TIME: |
| 41 | |
| 42 | if (hal_rtc_gettime(&hal_rtc_time) != 0) |
| 43 | { |
| 44 | LOG_E("rtc gettime failed!\n"); |
| 45 | return -RT_ERROR; |
| 46 | } |
| 47 | |
| 48 | time_temp.tm_sec = hal_rtc_time.tm_sec; |
| 49 | time_temp.tm_min = hal_rtc_time.tm_min; |
| 50 | time_temp.tm_hour = hal_rtc_time.tm_hour; |
| 51 | time_temp.tm_mday = hal_rtc_time.tm_mday; |
| 52 | time_temp.tm_mon = hal_rtc_time.tm_mon; |
| 53 | time_temp.tm_year = hal_rtc_time.tm_year; |
| 54 | |
| 55 | *((time_t *)args) = mktime(&time_temp); |
| 56 | break; |
| 57 | case RT_DEVICE_CTRL_RTC_SET_TIME: |
| 58 | |
| 59 | rt_enter_critical(); |
| 60 | /* converts calendar time time into local time. */ |
| 61 | time_now = localtime((const time_t *)args); |
| 62 | /* copy the statically located variable */ |
| 63 | memcpy(&time_temp, time_now, sizeof(struct tm)); |
| 64 | /* unlock scheduler. */ |
| 65 | rt_exit_critical(); |
| 66 | |
| 67 | hal_rtc_time.tm_sec = time_temp.tm_sec; |
| 68 | hal_rtc_time.tm_min = time_temp.tm_min; |
| 69 | hal_rtc_time.tm_hour = time_temp.tm_hour; |
| 70 | hal_rtc_time.tm_mday = time_temp.tm_mday; |
| 71 | hal_rtc_time.tm_mon = time_temp.tm_mon; |
| 72 | hal_rtc_time.tm_year = time_temp.tm_year; |
| 73 | if (hal_rtc_settime(&hal_rtc_time) != 0) |
| 74 | { |
| 75 | LOG_E("rtc settime failed!\n"); |
| 76 | return -RT_ERROR; |
| 77 | } |
| 78 | break; |
| 79 | default: |
| 80 | return -RT_EINVAL; |
| 81 | } |
| 82 | |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | #ifdef RT_USING_DEVICE_OPS |
| 87 | const static struct rt_device_ops rt_hw_rtc_ops = |
nothing calls this directly
no test coverage detected