#include "ltc1867.h"
#include "filter.h"
#include "device.h"
#include "cali.h"
#include "cali_flash.h"
#include "debug.h"
#include "cli.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_spi.h"

#define LTC1867_RAW_BUFF_SIZE (SAMPLES_PER_PERIOD / 4 * 3)
float data_ltc1867_raw[2][4][LTC1867_RAW_BUFF_SIZE] __attribute__((section(".bss_ccm")));
struct data_ltc1867_s data_ltc1867 __attribute__((section(".bss_ccm")));
__attribute__((section(".data_ccm"))) uint16_t ltc1867_spi_data[LTC1867_CH_CNT] = {0xC400, 0x9400, 0xD400, 0xA400, 0x8400}; /* order: ch1, ch2, ch3, ch4, 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);
    // NVIC_SetPriority(PendSV_IRQn, 1); /* priority is just lower than sample, for write cali data to flash */
    /* 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);
    /* Prescaler configuration */
    TIM_PrescalerConfig(TIM2, 0, TIM_PSCReloadMode_Immediate);
    /* TIM2 Interrupts enable */
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    /* TIM2 enable counter */
    TIM_Cmd(TIM2, ENABLE);

    /* TIM3 16bit for ADC conversion */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    /* Time base configuration */
    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);
    /* Prescaler configuration */
    TIM_PrescalerConfig(TIM3, 0, TIM_PSCReloadMode_Immediate);
    /* TIM3 disable counter, start when ltc1867 need delay */
    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]);

    /* data_ltc1867 init */
    data_ltc1867.calc_cnt = 0;
    data_ltc1867.calc_flag = 0; /* 2 period complete, need calc */
    data_ltc1867.ledir_cnt = 0; /* index of led count, for togglr IR led */
    data_ltc1867.sample_cnt = 0; /* index for search point of 32768 */
    for (uint8_t i = 0; i < 4; i++) {
        data_ltc1867.p_samp[i] = data_ltc1867_raw[0][i];
        data_ltc1867.p_calc[i] = data_ltc1867_raw[1][i];
    }
}

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)
{
    static uint16_t pre_ref = 65535;
    uint16_t val[LTC1867_CH_CNT];
    float *p;

    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

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

    /* sample 5 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 */
    }

    /* ltc1867 original sample data */
    for (uint8_t i = 0; i < 4; i++) {
        float val_f = (float)val[i]; /* convert to float */
        data_ltc1867.p_samp[i][data_ltc1867.sample_cnt] = val_f;
        debug_write_data(DEBUG_CH_RAW + i, val_f);
    }
    data_ltc1867.sample_cnt++;
    if (data_ltc1867.sample_cnt >= LTC1867_RAW_BUFF_SIZE) {
        data_ltc1867.sample_cnt = 0;
    }

    /* search point of 32768 */
    if (((pre_ref < 32768) && (val[0] >= 32768)) || ((pre_ref >= 32768) && (val[0] < 32768))) {
        if ((data_ltc1867.sample_cnt > SAMPLES_PER_PERIOD / 32 * 15) && \
            (data_ltc1867.sample_cnt < SAMPLES_PER_PERIOD / 32 * 17)) {
            /* exchange pointor of calc/samp data */
            for (uint8_t i = 0; i < 4; i++) {
                p = data_ltc1867.p_calc[i];
                data_ltc1867.p_calc[i] = data_ltc1867.p_samp[i];
                data_ltc1867.p_samp[i] = p;
            }
            data_ltc1867.calc_flag++; /* trig calc */
        }
        data_ltc1867.calc_cnt++;
        data_ltc1867.sample_total = data_ltc1867.sample_cnt;
        data_ltc1867.sample_cnt = 0;
    }
    pre_ref = val[0];

    /* channel 4 for ltc1867 detector temperature sample data */
    adc_det_temp_from_ltc1867 = val[4];

    cali_flash_write();
    debug_send_frame();
}
