/*
 * 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 <new>

#include "Bsp/Drivers/RtcDriver.h"
#include "Bsp/macros.h"

namespace BSP {

using BSP::ReturnCode;
using BSP::time_t;

RtcDriver::RtcSystemTimer RtcDriver::m_sys_timer;

ReturnCode RtcDriver::init()
{
    init_hw();

    return ReturnCode::OK;
}

void RtcDriver::enable_rtc_write()
{
    /*<! Disable write protection */
    RTC->WPR = 0xCA;
    RTC->WPR = 0x53;
}

void RtcDriver::disable_rtc_write()
{
    /*<! Enable write protection */
    RTC->WPR = 0x00;
}

void RtcDriver::enable_periodic_alarm()
{

    SET(RTC->ALRMAR, RTC_ALRMAR_MSK4 | RTC_ALRMAR_MSK3 | RTC_ALRMAR_MSK2 | RTC_ALRMAR_MSK1);

    // Only calculate alarms when second rolls over
    SET_TO(RTC->ALRMASSR, RTC_ALRMASSR_MASKSS, RTC_ALRMASSR_MASKSS);

    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);

}

ReturnCode RtcDriver::init_hw()
{
    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 LSE (low-speed external 32.768kHz) clock */
    if (!(RCC->CSR & RCC_CSR_LSERDY)) {
        // TODO: Does this help?
        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;
    while (!(RTC->ISR & RTC_ISR_INITF)) {}

    /*<! 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
    SET_TO(RTC->PRER, RTC_PRER_PREDIV_A,  0);
    /*<! Set the Syncronous scaler so the RTC updates at 1Hz */
    SET_TO(RTC->PRER, RTC_PRER_PREDIV_S, (LSE_CLOCK_FREQ - 1));

    /*<! Load initial date and time */

    // 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, 0 << RTC_TR_HU_Pos);
    SET_TO(time, RTC_TR_MNT, 0 << RTC_TR_MNT_Pos);
    SET_TO(time, RTC_TR_MNU, 9 << RTC_TR_MNU_Pos);
    SET_TO(time, RTC_TR_ST, 3 << RTC_TR_ST_Pos);
    SET_TO(time, RTC_TR_SU, 6 << RTC_TR_SU_Pos);
    RTC->TR = time;

    CLR(RTC->ISR, RTC_ISR_INIT);

    SET(EXTI->IMR, EXTI_IMR_IM20);
    SET(EXTI->EMR, EXTI_EMR_EM20);
    SET(EXTI->RTSR, EXTI_RTSR_RT20);

    // Enable Wakeup irq, we may/will use them later
    SET(RTC->CR, RTC_CR_WUTIE);
    NVIC_EnableIRQ(RTC_IRQn);
    NVIC_SetPriority(RTC_IRQn, 0);

    enable_periodic_alarm();

    disable_rtc_write();

    return ReturnCode::OK;
}

ReturnCode RtcDriver::get_time(BSP::WallClockTime &wall_time)
{
    /*<! The value of TR in the shadow register is locked when SSR is
    read (by the system timer), until the date register is read. We're
    not using the date here, but we do need to clear the stale value. */
    (void) RTC->DR;

    // TODO: reread TR + PM for consistency
    uint32_t time = RTC->TR;
    unsigned int hours = 0, minutes = 0, seconds = 0;

    hours += 10 * STM32_GET_FIELD(time, RTC_TR_HT);
    hours += STM32_GET_FIELD(time, RTC_TR_HU);

    minutes += 10 * STM32_GET_FIELD(time, RTC_TR_MNT);
    minutes += STM32_GET_FIELD(time, RTC_TR_MNU);

    seconds += 10 * STM32_GET_FIELD(time, RTC_TR_ST);
    seconds += STM32_GET_FIELD(time, RTC_TR_SU);

    if (STM32_GET_FIELD(time, RTC_TR_PM)) {
        hours += 12;
    }

    new (&wall_time) BSP::WallClockTime(hours, minutes, seconds);

    return ReturnCode::OK;
}

ReturnCode RtcDriver::set_time(const BSP::WallClockTime &wall_time)
{
    enable_rtc_write();

    RTC->ISR = RTC_ISR_INIT;
    while (!(RTC->ISR & RTC_ISR_INITF)) {}

    /*<! Load initial date and time */

    // 12-Hour format
    SET(RTC->CR, RTC_CR_FMT);

    uint32_t time = 0;
    SET_TO(time, RTC_TR_PM,  wall_time.get_is_pm()         << RTC_TR_PM_Pos);
    SET_TO(time, RTC_TR_HT,  wall_time.get_hours_12_tens() << RTC_TR_HT_Pos);
    SET_TO(time, RTC_TR_HU,  wall_time.get_hours_12_ones() << RTC_TR_HU_Pos);
    SET_TO(time, RTC_TR_MNT, wall_time.get_minutes_tens()  << RTC_TR_MNT_Pos);
    SET_TO(time, RTC_TR_MNU, wall_time.get_minutes_ones()  << RTC_TR_MNU_Pos);
    SET_TO(time, RTC_TR_ST,  wall_time.get_seconds_tens()  << RTC_TR_ST_Pos);
    SET_TO(time, RTC_TR_SU,  wall_time.get_seconds_ones()  << RTC_TR_SU_Pos);
    RTC->TR = time;

    CLR(RTC->ISR, RTC_ISR_INIT);
    while ((RTC->ISR & RTC_ISR_INITF)) {}
    while (!(RTC->ISR & RTC_ISR_RSF)) {}

    disable_rtc_write();

    return ReturnCode::OK;
}

ReturnCode RtcDriver::set_wakeup_in(BSP::time_t wakeup_delay)
{
    /*<! 2^64 / (1000000 * 32768) / 60 / 60 / 24 / 365 = ~17.85 This
    value will only overflow for wakeup_delays > 17.85 years, so
    this is fine. */
    uint64_t delay_cycles = BSP::Time::to_micros(wakeup_delay) * LSE_CLOCK_FREQ /
        BSP::Time::MICROS_PER_SEC;

    enable_rtc_write();

    /*<! If there is an ongoing wakeup, disable it */
    if (RTC->CR & RTC_CR_WUTE) {
        CLR(RTC->CR, RTC_CR_WUTE);
        while (!(RTC->ISR & RTC_ISR_WUTWF)) {}
    }

    uint32_t wucksel = 0;

    if (delay_cycles == 0) {
        return ReturnCode::FAIL;
    } else if (delay_cycles < 0x10000) {
        delay_cycles /= 2;
        wucksel = 3;
    } else if (delay_cycles <= 0x20000) {
        delay_cycles /= 4;
        wucksel = 2;
    } else if (delay_cycles <= 0x40000) {
        delay_cycles /= 8;
        wucksel = 1;
    } else if (delay_cycles <= 0x80000) {
        delay_cycles /= 16;
        wucksel = 0;
    } else {
#if 0
        // TODO: implement longer delays using ck_spre as clock source
        // TODO: the datasheet text and block diagram disagree- is it using clock_spre or clock_apre?
        wucksel = 4;
        delay_cycles >>= async_prediv; //
#else
        return ReturnCode::FAIL;
#endif
    }

    SET_TO(RTC->WUTR, RTC_WUTR_WUT, delay_cycles - 1);
    SET_TO(RTC->CR, RTC_CR_WUCKSEL, wucksel << RTC_CR_WUCKSEL_Pos);

    SET(RTC->CR, RTC_CR_WUTE);

    disable_rtc_write();

    return ReturnCode::OK;
}

 BSP::time_t RtcDriver::RtcSystemTimer::get_time()
 {
          uint32_t new_secs, old_secs, ssr;
     uint64_t new_timer_ticks, new_millis;
     do {
         __disable_irq();
         old_secs = m_seconds;
         ssr = RTC->SSR & 0xFFFF;
         new_secs = m_seconds;
         __enable_irq();
     } while (new_secs != old_secs);

     new_timer_ticks = (uint64_t) new_secs * LSE_CLOCK_FREQ;
     /** SSR is a countdown register */
     new_millis = (new_timer_ticks + LSE_CLOCK_FREQ - 1 - ssr) * BSP::Time::MILLIS_PER_SEC / LSE_CLOCK_FREQ;
     return BSP::Time::millis(new_millis);
}

void RtcDriver::RtcSystemTimer::increment_seconds()
{
    __disable_irq();
    m_seconds++;
    __enable_irq();
}

void RtcDriver::increment_seconds()
{
    m_sys_timer.increment_seconds();
}

static uint32_t wakeup_alarms = 0;

extern "C" void RTC_IRQHandler()
{
    // Clear the wakeup and alarm irq in the EXTI
    SET(EXTI->PR, EXTI_PR_PIF20 | EXTI_PR_PIF17);

    if (RTC->ISR & RTC_ISR_ALRAF) {
        RtcDriver::increment_seconds();
        CLR(RTC->ISR, RTC_ISR_ALRAF);
    }

    if (RTC->ISR & RTC_ISR_WUTF) {
        wakeup_alarms++;
        // Clear the interrupt in the RTC
        CLR(RTC->ISR, RTC_ISR_WUTF);
        // Disable the Wakeup timer (its periodic, but we use it as a
        // one-shot timer
        CLR(RTC->CR, RTC_CR_WUTE);
    }

}

}
