static void rtc_stm32f4xx_initialize(void)
{
debug_printf_P(PSTR("rtc_stm32f4xx_initialize\n"));
#if defined(RCC_APB1ENR_RTCEN)
RCC->APB1ENR |= RCC_APB1ENR_PWREN | RCC_APB1ENR_RTCEN; // Включить тактирование
#else /* defined(RCC_APB1ENR_RTCEN) */
RCC->APB1ENR |= RCC_APB1ENR_PWREN /*| RCC_APB1ENR_RTCEN*/; // Включить тактирование
#endif /* defined(RCC_APB1ENR_RTCEN) */
//RCC->APB1ENR |= RCC_APB1ENR_PWREN | RCC_APB1ENR_BKPEN; // STM32F1xx: Включить тактирование
__DSB();
#if 0
// Sample for STM32F207 from
http://electronix.ru/forum/index.php?showt...t&p=1421489 /* PWR */
PWR->CR |= PWR_CR_DBP; // Disable Backup Domain write protection
PWR->CSR =
PWR_CSR_EWUP * 0 | // Enable WKUP pin
PWR_CSR_BRE * 1 | // Backup regulator enable
0;
/* Теперь можно включить НЧ генератор */
RCC->BDCR =
RCC_BDCR_LSEON * 1 | // External low-speed oscillator enable
// RCC_BDCR_LSERDY * 0 |
RCC_BDCR_LSEBYP * 0 | // External low-speed oscillator bypass
RCC_BDCR_RTCSEL_0 * 1 | // RTC clock source selection: LSE
RCC_BDCR_RTCEN * 1 | // RTC clock enable
RCC_BDCR_BDRST * 0 | // Backup domain software reset
0;
#endif
PWR->CR1 |= PWR_CR1_DBP; // Разрешить запись в Backup domain
while ((PWR->CR1 & PWR_CR1_DBP) == 0)
;
#if 1
/* use LSE as clock source 32.768 kHz xtal */
RCC->BDCR = (RCC->BDCR & ~ (RCC_BDCR_LSEDRV /*| RCC_BDCR_RTCSEL*/)) |
3 * RCC_BDCR_LSEDRV_0 |
1 * RCC_BDCR_LSEON |
//1 * RCC_BDCR_RTCSEL_0 |
//1 * RCC_BDCR_RTCEN |
0;
while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
;
RCC->BDCR = (RCC->BDCR & ~ (RCC_BDCR_RTCSEL)) |
1 * RCC_BDCR_RTCSEL_0 | /* 01: LSE oscillator clock used as the RTC clock */
0;
RCC->BDCR |= RCC_BDCR_RTCEN; /* RTC clock enabled */
#else
/* test: use LSI as clock source */
RCC->CSR |= RCC_CSR_LSION;
while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
;
RCC->BDCR = (RCC->BDCR & ~ (RCC_BDCR_RTCSEL)) |
2 * RCC_BDCR_RTCSEL_0 | /* 10: LSI oscillator clock used as the RTC clock */
0;
RCC->BDCR |= RCC_BDCR_RTCEN; /* RTC clock enabled */
#endif
PWR->CR1 &= ~ PWR_CR1_DBP; // Запретить запись в Backup domain
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
RTC->CR |= RTC_CR_BYPSHAD;
local_delay_ms(10);
/* Configure the RTC PRER */
RTC->PRER =
((256 - 1) & RTC_PRER_PREDIV_S) |
(((128 - 1) << 16) & RTC_PRER_PREDIV_A) |
0;
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
PWR->CR1 &= ~ PWR_CR1_DBP; // Запретить запись в Backup domain
while ((PWR->CR1 & PWR_CR1_DBP) != 0)
;
/* Установка начальных значений времени и даты */
const uint_fast8_t inits = (RTC->ISR & RTC_ISR_INITS) != 0;
debug_printf_P(PSTR("rtc_stm32f4xx_initialize: INITS=%d\n"), inits);
uint_fast16_t year;
uint_fast8_t month, day;
uint_fast8_t hour, minute, secounds;
stm32f4xx_getdatetime(& year, & month, & day, & hour, & minute, & secounds);
if (inits == 0 || month < 1 || month > 12 || year < 2015 ||
day < 1 || day > 31 ||
hour > 23 || minute > 59 || secounds > 59)
{
board_rtc_settime(23, 59, 0);
board_rtc_setdate(2015, 11, 29);
}
debug_printf_P(PSTR("rtc_stm32f4xx_initialize: done\n"));
}