#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_spi.h"
#include "ltc1867.h"
#include "debug.h"

__attribute__((section(".bss_ccm"))) uint32_t ltc1867_samp_cnt = 0;
__attribute__((section(".data_ccm"))) uint16_t ltc1867_spi_data[LTC1867_CH_CNT] = {0x9400, 0x8400}; /* order: ch2, ch0 */

void ltc1867_init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    SPI_InitTypeDef  SPI_InitStructure;

    /* TIM2 32bit for sample */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = TIMER_PERIOD_SAMPLE;
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    TIM_PrescalerConfig(TIM2, 0, TIM_PSCReloadMode_Immediate);
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM2, ENABLE);

    /* TIM3 16bit for ADC conversion */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    TIM_TimeBaseStructure.TIM_Period = TIMER_PERIOD_DELAY;
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    TIM_PrescalerConfig(TIM3, 0, TIM_PSCReloadMode_Immediate);
    TIM_Cmd(TIM3, DISABLE);

    /* LEDIR pin configure */
    GPIO_InitStructure.GPIO_Pin = PIN_LEDIR;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    GPIO_Init(PORT_LEDIR, &GPIO_InitStructure);
    ltc1867_ledir_off();

    /* LTC1867 conv pin configure */
    GPIO_InitStructure.GPIO_Pin = PIN_CONV;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_Init(PORT_LTC1867, &GPIO_InitStructure);
    /* config SPI for LTC1867 communication */
    GPIO_PinAFConfig(PORT_SPI, GPIO_PinSource5, GPIO_AF_SPI1);
    GPIO_PinAFConfig(PORT_SPI, GPIO_PinSource6, GPIO_AF_SPI1);
    GPIO_PinAFConfig(PORT_SPI, GPIO_PinSource7, GPIO_AF_SPI1);
    GPIO_InitStructure.GPIO_Pin = PIN_SPI_SCK | PIN_SPI_SDI | PIN_SPI_SDO;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_Init(PORT_SPI, &GPIO_InitStructure);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
    SPI_I2S_DeInit(SPI1);
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI1, &SPI_InitStructure);
    SPI_Cmd(SPI1, ENABLE);
    ltc1867_spi_select();
    ltc1867_transfer(ltc1867_spi_data[LTC1867_CH_CNT - 1]);
}

void ltc1867_ledir_on(void)
{
    GPIO_SetBits(PORT_LEDIR, PIN_LEDIR);
}

void ltc1867_ledir_off(void)
{
    GPIO_ResetBits(PORT_LEDIR, PIN_LEDIR);
}

void ltc1867_ledir_toggle(void)
{
    GPIO_ToggleBits(PORT_LEDIR, PIN_LEDIR);
}

void ltc1867_delay(void)
{
    FlagStatus sts;

    TIM_SetCounter(TIM3, 0);
    TIM_ClearFlag(TIM3, TIM_FLAG_Update);
    TIM_Cmd(TIM3, ENABLE);
    while (1) {
        sts = TIM_GetFlagStatus(TIM3, TIM_FLAG_Update);
        if (sts != RESET) {
            break;
        }
    }
    TIM_Cmd(TIM3, DISABLE);
}

void ltc1867_conv(void)
{
    GPIO_SetBits(PORT_LTC1867, PIN_CONV);
}

void ltc1867_spi_select(void)
{
    GPIO_ResetBits(PORT_LTC1867, PIN_SPI_CS);
}

uint16_t ltc1867_transfer(uint16_t data_send)
{
    FlagStatus flag;

    while (1) {
        flag = SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE);
        if (flag != RESET) {
            break;
        }
    }
    SPI_I2S_SendData(SPI1, data_send);
    while (1) {
        flag = SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE);
        if (flag != RESET) {
            break;
        }
    }
    return SPI_I2S_ReceiveData(SPI1);
}

void TIM2_IRQHandler(void)
{
    uint16_t val[LTC1867_CH_CNT];

    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

    /* for control led IR */
    ltc1867_samp_cnt++;
    if (ltc1867_samp_cnt == (SAMPLES_PER_PERIOD / 2)) {
        ltc1867_ledir_on();
    } else if (ltc1867_samp_cnt >= SAMPLES_PER_PERIOD) {
        ltc1867_ledir_off();
        ltc1867_samp_cnt = 0;
    }

    /* sample multi channel adc data */
    for (uint8_t i = 0; i < LTC1867_CH_CNT; i++) {
        ltc1867_conv(); /* this point start convert */
        ltc1867_delay(); /* delay 3.5us for convert time, reference for LTC1867 datasheet */
        ltc1867_spi_select(); /* convert complete, make convert pin low for reading sample data by SPI */
        val[i] = ltc1867_transfer(ltc1867_spi_data[i]); /* read convert data and send next channel information */
    }

    debug_write_data(DEBUG_CH_REF, (float)val[0]);
    debug_write_data(DEBUG_CH_SIG, (float)val[1]);
    // debug_write_data(DEBUG_CH_CNT, (float)ltc1867_samp_cnt * (65536 / SAMPLES_PER_PERIOD));
    if (ltc1867_samp_cnt < SAMPLES_PER_PERIOD / 2) {
        debug_write_data(DEBUG_CH_CNT, (float)0);
    } else {
        debug_write_data(DEBUG_CH_CNT, (float)65535);
    }
    debug_send_frame();
}
