Use the builtin RTC and display it
Its roughly %15 fast, and initializes to zero, but the display currently shows HH:MM:SS AM/PM.
This commit is contained in:
89
rtc.c
Normal file
89
rtc.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Max Regan
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "rtc.h"
|
||||
#include "stm32l0xx.h"
|
||||
#include "macros.h"
|
||||
|
||||
static void enable_rtc_write()
|
||||
{
|
||||
/*<! Disable write protection */
|
||||
RTC->WPR = 0xCA;
|
||||
RTC->WPR = 0x53;
|
||||
}
|
||||
|
||||
static void disable_rtc_write()
|
||||
{
|
||||
/*<! Disable write protection */
|
||||
RTC->WPR = 0x00;
|
||||
}
|
||||
|
||||
void rtc_init()
|
||||
{
|
||||
uint32_t temp = RCC->CSR;
|
||||
SET(RCC->CSR, RCC_CSR_RTCRST);
|
||||
SET(RCC->APB1ENR, RCC_APB1ENR_PWREN);
|
||||
SET(PWR->CR, PWR_CR_DBP);
|
||||
|
||||
/*<! Set RTC input clock to the LSI (low-speed internal 32.768kHz) clock */
|
||||
SET_TO(temp, RCC_CSR_RTCSEL, RCC_CSR_RTCSEL_1);
|
||||
SET(temp, RCC_CSR_RTCEN);
|
||||
RCC->CSR = temp;
|
||||
|
||||
enable_rtc_write();
|
||||
|
||||
RTC->ISR = RTC_ISR_INIT;
|
||||
while (!(RTC->ISR & RTC_ISR_INITF)) {}
|
||||
|
||||
/*<! Set the Clock Prescalers (32.768kHz / 128 / 256 = 1Hz */
|
||||
/*<! Set the Async prescaler to the Maximum (divide the clock by 128) */
|
||||
SET_TO(RTC->PRER, RTC_PRER_PREDIV_A, RTC_PRER_PREDIV_A);
|
||||
/*<! Set the Syncronous scaler (divide the clock by 255 + 1) */
|
||||
SET_TO(RTC->PRER, RTC_PRER_PREDIV_S, 255);
|
||||
|
||||
/*<! Load initial date and time */
|
||||
// TODO
|
||||
/* uint32_t time = 0; */
|
||||
/* uint32_t date = 0; */
|
||||
/*<! Set the date and time format */
|
||||
// TODO: currently defaults to 24hr
|
||||
|
||||
CLR(RTC->ISR, RTC_ISR_INIT);
|
||||
|
||||
disable_rtc_write();
|
||||
}
|
||||
|
||||
|
||||
void rtc_get_time_bcd(struct time_bcd *tm_bcd)
|
||||
{
|
||||
uint32_t time = RTC->TR;
|
||||
|
||||
tm_bcd->hour_tens = STM32_FIELD(time, RTC_TR_HT);
|
||||
tm_bcd->hour_ones = STM32_FIELD(time, RTC_TR_HU);
|
||||
|
||||
tm_bcd->minute_tens = STM32_FIELD(time, RTC_TR_MNT);
|
||||
tm_bcd->minute_ones = STM32_FIELD(time, RTC_TR_MNU);
|
||||
|
||||
tm_bcd->second_tens = STM32_FIELD(time, RTC_TR_ST);
|
||||
tm_bcd->second_ones = STM32_FIELD(time, RTC_TR_SU);
|
||||
|
||||
tm_bcd->pm = STM32_FIELD(time, RTC_TR_PM);
|
||||
}
|
||||
Reference in New Issue
Block a user