/*
 * 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 "Bsp/Drivers/SpiDriver.h"
#include "Bsp/macros.h"

namespace BSP {

using RC = BSP::ReturnCode;
using BSP::Schedule::TaskScheduler;
using BSP::Schedule::NextTime;
using BSP::Time;

SpiDriver::SpiDriver(TaskScheduler &scheduler)
    : m_scheduler(scheduler)
    , m_spi(SPI1)
{}

void SpiDriver::init()
{
    SET(RCC->IOPENR, RCC_IOPENR_IOPAEN);
    SET(RCC->IOPENR, RCC_IOPENR_IOPBEN);
    RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;

    /* Assign SPI_MOSI to PB1, since PA7 is taken by LPTIM_OUT */
    GPIOB->AFR[0] &= ~GPIO_AFRH_AFRH1;
    GPIOB->AFR[0] |= 1u << GPIO_AFRH_AFRH1_Pos;

    SET_TO(GPIOB->MODER, GPIO_MODER_MODE1, 2u << GPIO_MODER_MODE1_Pos);

    GPIOB->OTYPER &= ~GPIO_OTYPER_OT_1;
    GPIOB->PUPDR &= ~GPIO_PUPDR_PUPD12;

    // SPI1 NSS (PA4)

    SET_TO(GPIOA->MODER, GPIO_MODER_MODE4, 1u << GPIO_MODER_MODE4_Pos);

    GPIOA->OTYPER &= ~GPIO_OTYPER_OT_4;
    GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD4;

    // SPI1 SCK (PA5)
    GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL5;

    SET_TO(GPIOA->MODER, GPIO_MODER_MODE5, 2u << GPIO_MODER_MODE5_Pos);

    GPIOA->OTYPER &= ~GPIO_OTYPER_OT_5;
    GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD5;

    // SPI1 MISO (PA6)
    GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL6;

    SET_TO(GPIOA->MODER, GPIO_MODER_MODE6, 2u << GPIO_MODER_MODE6_Pos);

    GPIOA->OTYPER &= ~GPIO_OTYPER_OT_6;
    GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD6;

    // Enable Master mode and half the baud rate, so it's set to ~1MHz
    m_spi->CR1 |= SPI_CR1_MSTR | SPI_CR1_LSBFIRST | SPI_CR1_SSM;
    //m_spi->CR1 |= 1u << SPI_CR1_BR_Pos;
    m_spi->CR2 |= SPI_CR2_SSOE;
}

NextTime SpiDriver::execute()
{
    return NextTime::never();
}

RC SpiDriver::tx_blocking(const uint8_t *data, size_t len)
{
    if (len <= 0) {
      return RC::FAIL;
    }

    m_spi->CR1 |= SPI_CR1_SPE;

    //FLIP(GPIOB->ODR, GPIO_ODR_OD3);
    CLR(m_spi->CR1, SPI_CR1_SSI);
    SET(GPIOA->ODR, GPIO_ODR_OD4);

    for (size_t i = 0; i < len; i++) {
        while (!(m_spi->SR & SPI_SR_TXE)) {}
        m_spi->DR = data[i];
    }

   //FLIP(GPIOB->ODR, GPIO_ODR_OD3);

    while (!(m_spi->SR & SPI_SR_TXE)) {}

    // Ensure that NSS is held for long enough to meet the display's thSCS
    for (int i = 0; i < 4; i++);

    m_spi->CR1 &= ~SPI_CR1_SPE;
    SET(m_spi->CR1, SPI_CR1_SSI);
    CLR(GPIOA->ODR, GPIO_ODR_OD4);

    return RC::OK;
}

}
