Hack in support for other boards/microcontrollers, add GpioDriver

There's definitely plenty of cleanup work to be done (see:
"ifdefs").
This commit is contained in:
2020-04-11 11:03:17 -07:00
parent 385402e7aa
commit 99317eb99b
17 changed files with 1156 additions and 99 deletions

View File

@@ -58,19 +58,27 @@ void RtcDriver::enable_periodic_alarm()
// Only calculate alarms when second rolls over
SET_TO(RTC->ALRMASSR, RTC_ALRMASSR_MASKSS, RTC_ALRMASSR_MASKSS);
#if defined(STM32L0XX)
SET(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
SET(EXTI->IMR, EXTI_IMR_IM17);
SET(EXTI->EMR, EXTI_EMR_EM17);
SET(EXTI->RTSR, EXTI_RTSR_RT17);
#elif defined(STM32L4XX)
SET(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
SET(EXTI->IMR1, EXTI_IMR1_IM18);
SET(EXTI->EMR1, EXTI_EMR1_EM18);
SET(EXTI->RTSR1, EXTI_RTSR1_RT18);
#else
#error "Unsupported family"
#endif
}
ReturnCode RtcDriver::init_hw()
{
//SET(RCC->CSR, RCC_CSR_RTCRST);
#if defined(STM32L0XX)
uint32_t temp = RCC->CSR;
SET(RCC->CSR, RCC_CSR_RTCRST);
SET(RCC->APB1ENR, RCC_APB1ENR_PWREN);
SET(PWR->CR, PWR_CR_DBP);
@@ -91,6 +99,32 @@ ReturnCode RtcDriver::init_hw()
RTC->ISR = RTC_ISR_INIT;
while (!(RTC->ISR & RTC_ISR_INITF)) {}
#elif defined(STM32L4XX)
uint32_t temp = RCC->CSR;
SET(RCC->APB1ENR1, RCC_APB1ENR1_PWREN);
SET(PWR->CR1, PWR_CR1_DBP);
/*<! Set RTC input clock to the LSE (low-speed external 32.768kHz) clock */
if (!(RCC->BDCR & RCC_BDCR_LSERDY)) {
// TODO: Does this help?
SET(temp, RCC_BDCR_LSEON);
}
SET_TO(temp, RCC_BDCR_RTCSEL, RCC_BDCR_RTCSEL_0);
SET(temp, RCC_BDCR_RTCEN);
RCC->BDCR = temp;
while (!(RCC->BDCR & RCC_BDCR_LSERDY)) {}
enable_rtc_write();
RTC->ICSR = RTC_ICSR_INIT;
while (!(RTC->ICSR & RTC_ICSR_INITF)) {}
#else
#error "Unsupported device type"
#endif
/*<! Set the Clock Prescalers (32.768kHz / 1 / 32768 = 1Hz */
/*<! Set the Async prescaler to the Maximum (divide the clock by 128) */
// XXX reset to 0, this is to enable easier debugging
@@ -113,6 +147,7 @@ ReturnCode RtcDriver::init_hw()
SET_TO(time, RTC_TR_SU, 6 << RTC_TR_SU_Pos);
RTC->TR = time;
#if defined(STM32L0XX)
CLR(RTC->ISR, RTC_ISR_INIT);
SET(EXTI->IMR, EXTI_IMR_IM20);
@@ -124,6 +159,22 @@ ReturnCode RtcDriver::init_hw()
NVIC_EnableIRQ(RTC_IRQn);
NVIC_SetPriority(RTC_IRQn, 0);
#elif defined(STM32L4XX)
CLR(RTC->ICSR, RTC_ICSR_INIT);
SET(EXTI->IMR1, EXTI_IMR1_IM20);
SET(EXTI->EMR1, EXTI_EMR1_EM20);
SET(EXTI->RTSR1, EXTI_RTSR1_RT20);
// Enable Wakeup irq, we may/will use them later
SET(RTC->CR, RTC_CR_WUTIE);
NVIC_EnableIRQ(RTC_WKUP_IRQn);
NVIC_SetPriority(RTC_WKUP_IRQn, 0);
#else
#error "Unsupported device type"
#endif
enable_periodic_alarm();
disable_rtc_write();
@@ -164,8 +215,16 @@ ReturnCode RtcDriver::set_time(const BSP::WallClockTime &wall_time)
{
enable_rtc_write();
#if defined(STM32L0XX)
RTC->ISR = RTC_ISR_INIT;
while (!(RTC->ISR & RTC_ISR_INITF)) {}
#elif defined(STM32L4XX)
RTC->ICSR = RTC_ICSR_INIT;
while (!(RTC->ICSR & RTC_ICSR_INITF)) {}
#else
#error "Unsupported device type"
#endif
/*<! Load initial date and time */
@@ -182,9 +241,17 @@ ReturnCode RtcDriver::set_time(const BSP::WallClockTime &wall_time)
SET_TO(time, RTC_TR_SU, wall_time.get_seconds_ones() << RTC_TR_SU_Pos);
RTC->TR = time;
#if defined(STM32L0XX)
CLR(RTC->ISR, RTC_ISR_INIT);
while ((RTC->ISR & RTC_ISR_INITF)) {}
while (!(RTC->ISR & RTC_ISR_RSF)) {}
#elif defined(STM32L4XX)
CLR(RTC->ICSR, RTC_ICSR_INIT);
while ((RTC->ICSR & RTC_ICSR_INITF)) {}
while (!(RTC->ICSR & RTC_ICSR_RSF)) {}
#else
#error "Unsupported device type"
#endif
disable_rtc_write();
@@ -204,7 +271,15 @@ ReturnCode RtcDriver::set_wakeup_in(BSP::time_t wakeup_delay)
/*<! If there is an ongoing wakeup, disable it */
if (RTC->CR & RTC_CR_WUTE) {
CLR(RTC->CR, RTC_CR_WUTE);
#if defined(STM32L0XX)
while (!(RTC->ISR & RTC_ISR_WUTWF)) {}
#elif defined(STM32L4XX)
while (!(RTC->ICSR & RTC_ICSR_WUTWF)) {}
#else
#error "Unsupported device type"
#endif
}
uint32_t wucksel = 0;
@@ -278,7 +353,9 @@ static uint32_t wakeup_alarms = 0;
extern "C" void RTC_IRQHandler()
{
// Clear the wakeup and alarm irq in the EXTI
#if defined(STM32L0XX)
SET(EXTI->PR, EXTI_PR_PIF20 | EXTI_PR_PIF17);
if (RTC->ISR & RTC_ISR_ALRAF) {
@@ -295,6 +372,28 @@ extern "C" void RTC_IRQHandler()
CLR(RTC->CR, RTC_CR_WUTE);
}
#elif defined(STM32L4XX)
SET(EXTI->PR1, EXTI_PR1_PIF20 | EXTI_PR1_PIF18);
if (RTC->SR & RTC_SR_ALRAF) {
RtcDriver::increment_seconds();
CLR(RTC->SR, RTC_SR_ALRAF);
}
if (RTC->SR & RTC_SR_WUTF) {
wakeup_alarms++;
// Clear the interrupt in the RTC
SET(RTC->SCR, RTC_SCR_CWUTF);
// Disable the Wakeup timer (its periodic, but we use it as a
// one-shot timer
CLR(RTC->CR, RTC_CR_WUTE);
}
#else
#error "Unsupported device type"
#endif
}
}