From 606f1d91311b1d198267af101a942686acf235f9 Mon Sep 17 00:00:00 2001 From: Robbe Date: Wed, 30 Oct 2019 18:30:00 -0700 Subject: [PATCH] Fixed RTC on non-uno boards, second try. Cannot work when there is no xtal. --- VERSION | 2 +- board/board_declarations.h | 8 +++ board/boards/uno.h | 3 + board/drivers/rtc.h | 116 +++++++++++++++++++------------------ board/main.c | 3 - 5 files changed, 73 insertions(+), 59 deletions(-) diff --git a/VERSION b/VERSION index 0c76f1991327b5..4279ff22f4fa77 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v1.5.8 \ No newline at end of file +v1.5.9 \ No newline at end of file diff --git a/board/board_declarations.h b/board/board_declarations.h index 99b8c8cc97c624..2fd3976a0dac68 100644 --- a/board/board_declarations.h +++ b/board/board_declarations.h @@ -62,3 +62,11 @@ struct board { // ********************* Globals ********************** uint8_t usb_power_mode = USB_POWER_NONE; + +// ************ Board function prototypes ************* +bool board_has_gps(void); +bool board_has_gmlan(void); +bool board_has_obd(void); +bool board_has_lin(void); +bool board_has_rtc(void); +bool board_has_relay(void); \ No newline at end of file diff --git a/board/boards/uno.h b/board/boards/uno.h index e57038f58d2ea3..f5765c226f8a6f 100644 --- a/board/boards/uno.h +++ b/board/boards/uno.h @@ -182,6 +182,9 @@ void uno_init(void) { // Initialize harness harness_init(); + // Initialize RTC + rtc_init(); + // Enable CAN transcievers uno_enable_can_transcievers(true); diff --git a/board/drivers/rtc.h b/board/drivers/rtc.h index 84670ffe0f69d1..25190ea76b032d 100644 --- a/board/drivers/rtc.h +++ b/board/drivers/rtc.h @@ -22,72 +22,78 @@ uint16_t from_bcd(uint8_t value){ } void rtc_init(void){ - // Initialize RTC module and clock if not done already. - if((RCC->BDCR & RCC_BDCR_MASK) != RCC_BDCR_OPTIONS){ - puts("Initializing RTC\n"); - // Reset backup domain - RCC->BDCR |= RCC_BDCR_BDRST; + if(board_has_rtc()){ + // Initialize RTC module and clock if not done already. + if((RCC->BDCR & RCC_BDCR_MASK) != RCC_BDCR_OPTIONS){ + puts("Initializing RTC\n"); + // Reset backup domain + RCC->BDCR |= RCC_BDCR_BDRST; - // Disable write protection - PWR->CR |= PWR_CR_DBP; + // Disable write protection + PWR->CR |= PWR_CR_DBP; - // Clear backup domain reset - RCC->BDCR &= ~(RCC_BDCR_BDRST); + // Clear backup domain reset + RCC->BDCR &= ~(RCC_BDCR_BDRST); - // Set RTC options - RCC->BDCR = RCC_BDCR_OPTIONS | (RCC->BDCR & (~RCC_BDCR_MASK)); + // Set RTC options + RCC->BDCR = RCC_BDCR_OPTIONS | (RCC->BDCR & (~RCC_BDCR_MASK)); - // Enable write protection - PWR->CR &= ~(PWR_CR_DBP); + // Enable write protection + PWR->CR &= ~(PWR_CR_DBP); + } } } void rtc_set_time(timestamp_t time){ - puts("Setting RTC time\n"); - - // Disable write protection - PWR->CR |= PWR_CR_DBP; - RTC->WPR = 0xCA; - RTC->WPR = 0x53; - - // Enable initialization mode - RTC->ISR |= RTC_ISR_INIT; - while((RTC->ISR & RTC_ISR_INITF) == 0){} - - // Set time - RTC->TR = (to_bcd(time.hour) << RTC_TR_HU_Pos) | (to_bcd(time.minute) << RTC_TR_MNU_Pos) | (to_bcd(time.second) << RTC_TR_SU_Pos); - RTC->DR = (to_bcd(time.year - YEAR_OFFSET) << RTC_DR_YU_Pos) | (time.weekday << RTC_DR_WDU_Pos) | (to_bcd(time.month) << RTC_DR_MU_Pos) | (to_bcd(time.day) << RTC_DR_DU_Pos); - - // Set options - RTC->CR = 0U; - - // Disable initalization mode - RTC->ISR &= ~(RTC_ISR_INIT); - - // Wait for synchronization - while((RTC->ISR & RTC_ISR_RSF) == 0){} - - // Re-enable write protection - RTC->WPR = 0x00; - PWR->CR &= ~(PWR_CR_DBP); + if(board_has_rtc()){ + puts("Setting RTC time\n"); + + // Disable write protection + PWR->CR |= PWR_CR_DBP; + RTC->WPR = 0xCA; + RTC->WPR = 0x53; + + // Enable initialization mode + RTC->ISR |= RTC_ISR_INIT; + while((RTC->ISR & RTC_ISR_INITF) == 0){} + + // Set time + RTC->TR = (to_bcd(time.hour) << RTC_TR_HU_Pos) | (to_bcd(time.minute) << RTC_TR_MNU_Pos) | (to_bcd(time.second) << RTC_TR_SU_Pos); + RTC->DR = (to_bcd(time.year - YEAR_OFFSET) << RTC_DR_YU_Pos) | (time.weekday << RTC_DR_WDU_Pos) | (to_bcd(time.month) << RTC_DR_MU_Pos) | (to_bcd(time.day) << RTC_DR_DU_Pos); + + // Set options + RTC->CR = 0U; + + // Disable initalization mode + RTC->ISR &= ~(RTC_ISR_INIT); + + // Wait for synchronization + while((RTC->ISR & RTC_ISR_RSF) == 0){} + + // Re-enable write protection + RTC->WPR = 0x00; + PWR->CR &= ~(PWR_CR_DBP); + } } timestamp_t rtc_get_time(void){ - // Wait until the register sync flag is set - while((RTC->ISR & RTC_ISR_RSF) == 0){} - - // Read time and date registers. Since our HSE > 7*LSE, this should be fine. - uint32_t time = RTC->TR; - uint32_t date = RTC->DR; - - // Parse values timestamp_t result; - result.year = from_bcd((date & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos) + YEAR_OFFSET; - result.month = from_bcd((date & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos); - result.day = from_bcd((date & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos); - result.weekday = ((date & RTC_DR_WDU) >> RTC_DR_WDU_Pos); - result.hour = from_bcd((time & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos); - result.minute = from_bcd((time & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos); - result.second = from_bcd((time & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos); + if(board_has_rtc()){ + // Wait until the register sync flag is set + while((RTC->ISR & RTC_ISR_RSF) == 0){} + + // Read time and date registers. Since our HSE > 7*LSE, this should be fine. + uint32_t time = RTC->TR; + uint32_t date = RTC->DR; + + // Parse values + result.year = from_bcd((date & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos) + YEAR_OFFSET; + result.month = from_bcd((date & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos); + result.day = from_bcd((date & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos); + result.weekday = ((date & RTC_DR_WDU) >> RTC_DR_WDU_Pos); + result.hour = from_bcd((time & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos); + result.minute = from_bcd((time & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos); + result.second = from_bcd((time & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos); + } return result; } \ No newline at end of file diff --git a/board/main.c b/board/main.c index 7dd87c344b28cd..3bcae876e203d0 100644 --- a/board/main.c +++ b/board/main.c @@ -727,9 +727,6 @@ int main(void) { // init board current_board->init(); - // Initialize RTC - rtc_init(); - // panda has an FPU, let's use it! enable_fpu();