C++ refactoring, plus low-power support, plus software-based SPI CS

I'm backlogged.
This commit is contained in:
2019-04-13 15:08:02 -07:00
parent d5bfecedb2
commit 6747d6c831
54 changed files with 13193 additions and 7264 deletions

47
rtc.c
View File

@@ -36,18 +36,34 @@ static void disable_rtc_write()
RTC->WPR = 0x00;
}
uint32_t bin_to_bcd(uint32_t bin)
{
uint32_t bcd = 0;
while (bin > 0) {
bcd <<= 4;
bcd |= bin % 10;
bin /= 10;
}
return bcd;
}
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_LSEON);
SET_TO(temp, RCC_CSR_RTCSEL, RCC_CSR_RTCSEL_0);
SET(temp, RCC_CSR_RTCEN);
RCC->CSR = temp;
while (!(RCC->CSR & RCC_CSR_LSERDY)) {}
enable_rtc_write();
RTC->ISR = RTC_ISR_INIT;
@@ -59,6 +75,7 @@ void rtc_init()
/*<! 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; */
@@ -66,6 +83,20 @@ void rtc_init()
/*<! Set the date and time format */
// TODO: currently defaults to 24hr
// 12-Hour format
SET(RTC->CR, RTC_CR_FMT);
uint32_t time = 0;
SET(time, RTC_TR_PM);
SET_TO(time, RTC_TR_HT, 1 << RTC_TR_HT_Pos);
SET_TO(time, RTC_TR_HU, 2 << RTC_TR_HU_Pos);
SET_TO(time, RTC_TR_MNT, 5 << RTC_TR_MNT_Pos);
SET_TO(time, RTC_TR_MNU, 9 << RTC_TR_MNU_Pos);
SET_TO(time, RTC_TR_ST, 0 << RTC_TR_ST_Pos);
SET_TO(time, RTC_TR_SU, 0 << RTC_TR_SU_Pos);
RTC->TR = time;
CLR(RTC->ISR, RTC_ISR_INIT);
disable_rtc_write();
@@ -76,14 +107,14 @@ 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->hour_tens = STM32_GET_FIELD(time, RTC_TR_HT);
tm_bcd->hour_ones = STM32_GET_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->minute_tens = STM32_GET_FIELD(time, RTC_TR_MNT);
tm_bcd->minute_ones = STM32_GET_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->second_tens = STM32_GET_FIELD(time, RTC_TR_ST);
tm_bcd->second_ones = STM32_GET_FIELD(time, RTC_TR_SU);
tm_bcd->pm = STM32_FIELD(time, RTC_TR_PM);
tm_bcd->pm = STM32_GET_FIELD(time, RTC_TR_PM);
}