Lines Matching +full:reset +full:- +full:on +full:- +full:invalid +full:- +full:access

6  * SPDX-License-Identifier: Apache-2.0
76 #define MIN_PPB -NB_PULSES_TO_PPB(MAX_CALM)
102 * After system reset, the RTC registers are protected against parasitic write access by the
104 * Hence, DBP bit must be set in order to enable RTC registers write access.
126 /* user-defined alarm mask, values from RTC_ALARM_TIME_MASK */
144 const struct rtc_stm32_config *cfg = dev->config; in rtc_stm32_configure()
155 * only if needed to avoid inducing time drift at each reset in rtc_stm32_configure()
158 (sync_prescaler != cfg->sync_prescaler) || in rtc_stm32_configure()
159 (async_prescaler != cfg->async_prescaler)) { in rtc_stm32_configure()
164 LL_RTC_SetSynchPrescaler(RTC, cfg->sync_prescaler); in rtc_stm32_configure()
165 LL_RTC_SetAsynchPrescaler(RTC, cfg->async_prescaler); in rtc_stm32_configure()
167 err = -EIO; in rtc_stm32_configure()
174 LL_RTC_CAL_SetOutputFreq(RTC, cfg->cal_out_freq); in rtc_stm32_configure()
192 ll_alarm_struct->AlarmDateWeekDaySel = RTC_STM32_ALRM_DATEWEEKDAYSEL_DATE; in rtc_stm32_init_alarm()
290 struct rtc_stm32_data *data = dev->data; in rtc_stm32_isr()
305 p_rtc_alrm = &(data->rtc_alrm_a); in rtc_stm32_isr()
307 p_rtc_alrm = &(data->rtc_alrm_b); in rtc_stm32_isr()
310 p_rtc_alrm->is_pending = true; in rtc_stm32_isr()
312 if (p_rtc_alrm->user_callback != NULL) { in rtc_stm32_isr()
313 p_rtc_alrm->user_callback(dev, (uint16_t)id, p_rtc_alrm->user_data); in rtc_stm32_isr()
337 const struct rtc_stm32_config *cfg = dev->config; in rtc_stm32_init()
338 struct rtc_stm32_data *data = dev->data; in rtc_stm32_init()
344 return -ENODEV; in rtc_stm32_init()
348 if (clock_control_on(clk, (clock_control_subsys_t)&cfg->pclken[0]) != 0) { in rtc_stm32_init()
350 return -EIO; in rtc_stm32_init()
353 k_mutex_init(&data->lock); in rtc_stm32_init()
355 /* Enable Backup access */ in rtc_stm32_init()
362 LL_RCC_SetRTC_HSEPrescaler(cfg->hse_prescaler); in rtc_stm32_init()
365 if (clock_control_configure(clk, (clock_control_subsys_t)&cfg->pclken[1], NULL) != 0) { in rtc_stm32_init()
367 return -EIO; in rtc_stm32_init()
371 * On STM32WBAX series, there is no bit in BCDR register to enable RTC. in rtc_stm32_init()
393 k_mutex_lock(&data->lock, K_FOREVER); in rtc_stm32_init()
394 memset(&(data->rtc_alrm_a), 0, sizeof(struct rtc_stm32_alrm)); in rtc_stm32_init()
395 memset(&(data->rtc_alrm_b), 0, sizeof(struct rtc_stm32_alrm)); in rtc_stm32_init()
396 k_mutex_unlock(&data->lock); in rtc_stm32_init()
404 struct rtc_stm32_data *data = dev->data; in rtc_stm32_set_time()
407 uint32_t real_year = timeptr->tm_year + TM_YEAR_REF; in rtc_stm32_set_time()
412 return -EINVAL; in rtc_stm32_set_time()
415 if (timeptr->tm_wday == -1) { in rtc_stm32_set_time()
417 return -EINVAL; in rtc_stm32_set_time()
420 err = k_mutex_lock(&data->lock, K_NO_WAIT); in rtc_stm32_set_time()
432 rtc_time.Hours = bin2bcd(timeptr->tm_hour); in rtc_stm32_set_time()
433 rtc_time.Minutes = bin2bcd(timeptr->tm_min); in rtc_stm32_set_time()
434 rtc_time.Seconds = bin2bcd(timeptr->tm_sec); in rtc_stm32_set_time()
437 /* Set Date after Time to be sure the DR is correctly updated on stm32F2 serie. */ in rtc_stm32_set_time()
438 rtc_date.Year = bin2bcd((real_year - RTC_YEAR_REF)); in rtc_stm32_set_time()
439 rtc_date.Month = bin2bcd((timeptr->tm_mon + 1)); in rtc_stm32_set_time()
440 rtc_date.Day = bin2bcd(timeptr->tm_mday); in rtc_stm32_set_time()
441 rtc_date.WeekDay = ((timeptr->tm_wday == 0) ? (LL_RTC_WEEKDAY_SUNDAY) : (timeptr->tm_wday)); in rtc_stm32_set_time()
461 k_mutex_unlock(&data->lock); in rtc_stm32_set_time()
463 LOG_DBG("Calendar set : %d/%d/%d - %dh%dm%ds", in rtc_stm32_set_time()
477 struct rtc_stm32_data *data = dev->data; in rtc_stm32_get_time()
482 const struct rtc_stm32_config *cfg = dev->config; in rtc_stm32_get_time()
488 return -EINVAL; in rtc_stm32_get_time()
491 int err = k_mutex_lock(&data->lock, K_NO_WAIT); in rtc_stm32_get_time()
499 * reset only on backup domain reset, so it can be read after a system in rtc_stm32_get_time()
500 * reset to check if the calendar has been initialized. in rtc_stm32_get_time()
502 k_mutex_unlock(&data->lock); in rtc_stm32_get_time()
503 return -ENODATA; in rtc_stm32_get_time()
522 k_mutex_unlock(&data->lock); in rtc_stm32_get_time()
525 timeptr->tm_year = bcd2bin(__LL_RTC_GET_YEAR(rtc_date)) + (RTC_YEAR_REF - TM_YEAR_REF); in rtc_stm32_get_time()
526 /* tm_mon allowed values are 0-11 */ in rtc_stm32_get_time()
527 timeptr->tm_mon = bcd2bin(__LL_RTC_GET_MONTH(rtc_date)) - 1; in rtc_stm32_get_time()
528 timeptr->tm_mday = bcd2bin(__LL_RTC_GET_DAY(rtc_date)); in rtc_stm32_get_time()
534 timeptr->tm_wday = 0; in rtc_stm32_get_time()
537 timeptr->tm_wday = hw_wday; in rtc_stm32_get_time()
540 timeptr->tm_hour = bcd2bin(__LL_RTC_GET_HOUR(rtc_time)); in rtc_stm32_get_time()
541 timeptr->tm_min = bcd2bin(__LL_RTC_GET_MINUTE(rtc_time)); in rtc_stm32_get_time()
542 timeptr->tm_sec = bcd2bin(__LL_RTC_GET_SECOND(rtc_time)); in rtc_stm32_get_time()
545 uint64_t temp = ((uint64_t)(cfg->sync_prescaler - rtc_subsecond)) * 1000000000L; in rtc_stm32_get_time()
547 timeptr->tm_nsec = temp / (cfg->sync_prescaler + 1); in rtc_stm32_get_time()
549 timeptr->tm_nsec = 0; in rtc_stm32_get_time()
552 timeptr->tm_yday = -1; in rtc_stm32_get_time()
553 timeptr->tm_isdst = -1; in rtc_stm32_get_time()
556 LOG_DBG("Calendar get : %d/%d/%d - %dh%dm%ds", in rtc_stm32_get_time()
557 timeptr->tm_mday, in rtc_stm32_get_time()
558 timeptr->tm_mon, in rtc_stm32_get_time()
560 timeptr->tm_hour, in rtc_stm32_get_time()
561 timeptr->tm_min, in rtc_stm32_get_time()
562 timeptr->tm_sec); in rtc_stm32_get_time()
571 LL_RTC_TimeTypeDef *p_rtc_alrm_time = &(p_rtc_alarm->AlarmTime); in rtc_stm32_init_ll_alrm_struct()
583 p_rtc_alrm_time->Seconds = bin2bcd(timeptr->tm_sec); in rtc_stm32_init_ll_alrm_struct()
588 p_rtc_alrm_time->Minutes = bin2bcd(timeptr->tm_min); in rtc_stm32_init_ll_alrm_struct()
593 p_rtc_alrm_time->Hours = bin2bcd(timeptr->tm_hour); in rtc_stm32_init_ll_alrm_struct()
599 p_rtc_alarm->AlarmDateWeekDaySel = RTC_STM32_ALRM_DATEWEEKDAYSEL_WEEKDAY; in rtc_stm32_init_ll_alrm_struct()
601 if (timeptr->tm_wday == 0) { in rtc_stm32_init_ll_alrm_struct()
603 p_rtc_alarm->AlarmDateWeekDay = LL_RTC_WEEKDAY_SUNDAY; in rtc_stm32_init_ll_alrm_struct()
606 p_rtc_alarm->AlarmDateWeekDay = bin2bcd(timeptr->tm_wday); in rtc_stm32_init_ll_alrm_struct()
612 p_rtc_alarm->AlarmDateWeekDaySel = RTC_STM32_ALRM_DATEWEEKDAYSEL_DATE; in rtc_stm32_init_ll_alrm_struct()
613 p_rtc_alarm->AlarmDateWeekDay = bin2bcd(timeptr->tm_mday); in rtc_stm32_init_ll_alrm_struct()
616 p_rtc_alrm_time->TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24; in rtc_stm32_init_ll_alrm_struct()
618 p_rtc_alarm->AlarmMask = ll_mask; in rtc_stm32_init_ll_alrm_struct()
624 timeptr->tm_sec = bcd2bin(LL_RTC_ALMA_GetSecond(RTC)); in rtc_stm32_get_ll_alrm_time()
625 timeptr->tm_min = bcd2bin(LL_RTC_ALMA_GetMinute(RTC)); in rtc_stm32_get_ll_alrm_time()
626 timeptr->tm_hour = bcd2bin(LL_RTC_ALMA_GetHour(RTC)); in rtc_stm32_get_ll_alrm_time()
627 timeptr->tm_wday = bcd2bin(LL_RTC_ALMA_GetWeekDay(RTC)); in rtc_stm32_get_ll_alrm_time()
628 timeptr->tm_mday = bcd2bin(LL_RTC_ALMA_GetDay(RTC)); in rtc_stm32_get_ll_alrm_time()
633 timeptr->tm_sec = bcd2bin(LL_RTC_ALMB_GetSecond(RTC)); in rtc_stm32_get_ll_alrm_time()
634 timeptr->tm_min = bcd2bin(LL_RTC_ALMB_GetMinute(RTC)); in rtc_stm32_get_ll_alrm_time()
635 timeptr->tm_hour = bcd2bin(LL_RTC_ALMB_GetHour(RTC)); in rtc_stm32_get_ll_alrm_time()
636 timeptr->tm_wday = bcd2bin(LL_RTC_ALMB_GetWeekDay(RTC)); in rtc_stm32_get_ll_alrm_time()
637 timeptr->tm_mday = bcd2bin(LL_RTC_ALMB_GetDay(RTC)); in rtc_stm32_get_ll_alrm_time()
697 return -EINVAL; in rtc_stm32_alarm_get_supported_fields()
701 LOG_ERR("invalid alarm ID %d", id); in rtc_stm32_alarm_get_supported_fields()
702 return -EINVAL; in rtc_stm32_alarm_get_supported_fields()
713 struct rtc_stm32_data *data = dev->data; in rtc_stm32_alarm_get_time()
721 return -EINVAL; in rtc_stm32_alarm_get_time()
724 k_mutex_lock(&data->lock, K_FOREVER); in rtc_stm32_alarm_get_time()
727 p_rtc_alrm = &(data->rtc_alrm_a); in rtc_stm32_alarm_get_time()
729 p_rtc_alrm = &(data->rtc_alrm_b); in rtc_stm32_alarm_get_time()
731 LOG_ERR("invalid alarm ID %d", id); in rtc_stm32_alarm_get_time()
732 err = -EINVAL; in rtc_stm32_alarm_get_time()
736 p_ll_rtc_alarm = &(p_rtc_alrm->ll_rtc_alrm); in rtc_stm32_alarm_get_time()
737 p_ll_rtc_alrm_time = &(p_ll_rtc_alarm->AlarmTime); in rtc_stm32_alarm_get_time()
739 memset(timeptr, -1, sizeof(struct rtc_time)); in rtc_stm32_alarm_get_time()
743 p_rtc_alrm->user_mask = rtc_stm32_get_ll_alrm_mask(id); in rtc_stm32_alarm_get_time()
745 *mask = p_rtc_alrm->user_mask; in rtc_stm32_alarm_get_time()
748 "mask = 0x%04x", timeptr->tm_mday, timeptr->tm_wday, timeptr->tm_hour, in rtc_stm32_alarm_get_time()
749 timeptr->tm_min, timeptr->tm_sec, *mask); in rtc_stm32_alarm_get_time()
752 k_mutex_unlock(&data->lock); in rtc_stm32_alarm_get_time()
760 struct rtc_stm32_data *data = dev->data; in rtc_stm32_alarm_set_time()
766 k_mutex_lock(&data->lock, K_FOREVER); in rtc_stm32_alarm_set_time()
769 p_rtc_alrm = &(data->rtc_alrm_a); in rtc_stm32_alarm_set_time()
771 p_rtc_alrm = &(data->rtc_alrm_b); in rtc_stm32_alarm_set_time()
773 LOG_ERR("invalid alarm ID %d", id); in rtc_stm32_alarm_set_time()
774 err = -EINVAL; in rtc_stm32_alarm_set_time()
779 memset(&(p_rtc_alrm->ll_rtc_alrm), 0, sizeof(LL_RTC_AlarmTypeDef)); in rtc_stm32_alarm_set_time()
780 p_rtc_alrm->user_callback = NULL; in rtc_stm32_alarm_set_time()
781 p_rtc_alrm->user_data = NULL; in rtc_stm32_alarm_set_time()
782 p_rtc_alrm->is_pending = false; in rtc_stm32_alarm_set_time()
798 err = -EINVAL; in rtc_stm32_alarm_set_time()
803 LOG_ERR("timeptr is invalid"); in rtc_stm32_alarm_set_time()
804 err = -EINVAL; in rtc_stm32_alarm_set_time()
809 LOG_DBG("One or multiple time values are invalid"); in rtc_stm32_alarm_set_time()
810 err = -EINVAL; in rtc_stm32_alarm_set_time()
814 p_ll_rtc_alarm = &(p_rtc_alrm->ll_rtc_alrm); in rtc_stm32_alarm_set_time()
815 p_ll_rtc_alrm_time = &(p_ll_rtc_alarm->AlarmTime); in rtc_stm32_alarm_set_time()
820 p_rtc_alrm->user_mask = mask; in rtc_stm32_alarm_set_time()
824 id, timeptr->tm_sec, timeptr->tm_min, timeptr->tm_hour, in rtc_stm32_alarm_set_time()
825 timeptr->tm_wday, timeptr->tm_mday, mask); in rtc_stm32_alarm_set_time()
860 err = -ECANCELED; in rtc_stm32_alarm_set_time()
885 k_mutex_unlock(&data->lock); in rtc_stm32_alarm_set_time()
909 struct rtc_stm32_data *data = dev->data; in rtc_stm32_alarm_set_callback()
913 k_mutex_lock(&data->lock, K_FOREVER); in rtc_stm32_alarm_set_callback()
916 p_rtc_alrm = &(data->rtc_alrm_a); in rtc_stm32_alarm_set_callback()
918 p_rtc_alrm = &(data->rtc_alrm_b); in rtc_stm32_alarm_set_callback()
920 LOG_ERR("invalid alarm ID %d", id); in rtc_stm32_alarm_set_callback()
921 err = -EINVAL; in rtc_stm32_alarm_set_callback()
926 p_rtc_alrm->user_callback = callback; in rtc_stm32_alarm_set_callback()
927 p_rtc_alrm->user_data = user_data; in rtc_stm32_alarm_set_callback()
930 k_mutex_unlock(&data->lock); in rtc_stm32_alarm_set_callback()
937 struct rtc_stm32_data *data = dev->data; in rtc_stm32_alarm_is_pending()
941 k_mutex_lock(&data->lock, K_FOREVER); in rtc_stm32_alarm_is_pending()
944 p_rtc_alrm = &(data->rtc_alrm_a); in rtc_stm32_alarm_is_pending()
946 p_rtc_alrm = &(data->rtc_alrm_b); in rtc_stm32_alarm_is_pending()
948 LOG_ERR("invalid alarm ID %d", id); in rtc_stm32_alarm_is_pending()
949 ret = -EINVAL; in rtc_stm32_alarm_is_pending()
954 ret = p_rtc_alrm->is_pending ? 1 : 0; in rtc_stm32_alarm_is_pending()
955 p_rtc_alrm->is_pending = false; in rtc_stm32_alarm_is_pending()
959 k_mutex_unlock(&data->lock); in rtc_stm32_alarm_is_pending()
972 * on clock period (not frequency) but with an opposite sign in rtc_stm32_set_calibration()
977 return -EINVAL; in rtc_stm32_set_calibration()
986 __ASSERT_NO_MSG(nb_pulses >= -MAX_CALM); in rtc_stm32_set_calibration()
992 calm = MAX_CALP - nb_pulses; in rtc_stm32_set_calibration()
995 calm = -nb_pulses; in rtc_stm32_set_calibration()
1000 return -EIO; in rtc_stm32_set_calibration()
1009 MODIFY_REG(RTC->CALR, RTC_CALR_CALP | RTC_CALR_CALM, calp | calm); in rtc_stm32_set_calibration()
1024 uint32_t calr = sys_read32((mem_addr_t) &RTC->CALR); in rtc_stm32_get_calibration()
1029 int32_t nb_pulses = -((int32_t) calm); in rtc_stm32_get_calibration()
1097 .async_prescaler = RTC_HSE_ASYNC_PRESCALER - 1,
1098 .sync_prescaler = RTC_HSE_SYNC_PRESCALER - 1,
1101 #error Invalid RTC SRC