#include "at32f403a_407.h"
#include "dac_transport.h"
#include "FreeRTOS.h"
#include "task.h"
#include "misc.h"
#include <stdio.h>
#include <stdbool.h>


#define DAC_SPI  SPI4


// PE12 - CS_DAC1
// PD1 - CS_DAC2
// PD0 - CS_DAC3
// PD3 - CS_DAC4
void dac_init(void)
{
    gpio_init_type gpio_initstructure;
    spi_init_type spi_init_struct;
  
    crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
    crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
    crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
    
    gpio_pin_remap_config(SPI4_GMUX_0001, TRUE);
  
    // SCK
    gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
    gpio_initstructure.gpio_pull           = GPIO_PULL_DOWN;  
    gpio_initstructure.gpio_mode           = GPIO_MODE_MUX;  
    gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
    gpio_initstructure.gpio_pins           = GPIO_PINS_11;
    gpio_init(GPIOE, &gpio_initstructure);
    
    // MOSI
    gpio_initstructure.gpio_pull           = GPIO_PULL_UP;  
    gpio_initstructure.gpio_mode           = GPIO_MODE_MUX; 
    gpio_initstructure.gpio_pins           = GPIO_PINS_14;
    gpio_init(GPIOE, &gpio_initstructure);
     
    // CS 
    gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
    gpio_initstructure.gpio_pull           = GPIO_PULL_UP;  
    gpio_initstructure.gpio_mode           = GPIO_MODE_OUTPUT;  
    gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
    gpio_initstructure.gpio_pins           = GPIO_PINS_12;
    gpio_init(GPIOE, &gpio_initstructure);
    
    gpio_initstructure.gpio_pins           = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_3;
    gpio_init(GPIOD, &gpio_initstructure);
    
    dac_cs(CH_DAC_1, false);
    dac_cs(CH_DAC_2, false);
    dac_cs(CH_DAC_3, false);
    dac_cs(CH_DAC_4, false);
    

    crm_periph_clock_enable(CRM_SPI4_PERIPH_CLOCK, TRUE);
    
    spi_default_para_init(&spi_init_struct);
    spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
    spi_init_struct.master_slave_mode = SPI_MODE_MASTER;
    spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_2; //SPI_MCLK_DIV_2;
    spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB; //SPI_FIRST_BIT_LSB; 
    spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
    spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_LOW; // SPI_CLOCK_POLARITY_HIGH;
    spi_init_struct.clock_phase = SPI_CLOCK_PHASE_2EDGE;
    spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
    
    spi_init(DAC_SPI, &spi_init_struct);
    
    spi_enable(DAC_SPI, TRUE);
}

//
void dac_test(DAC_CHANNEL ch, uint16_t val)
{
    uint8_t ret;
    
    dac_cs(ch, true);

    while (spi_i2s_flag_get(DAC_SPI, SPI_I2S_TDBE_FLAG) == RESET);
    DAC_SPI->dt = 0;
    
    while (spi_i2s_flag_get(DAC_SPI, SPI_I2S_TDBE_FLAG) == RESET);
    DAC_SPI->dt = val >> 8;
    
    while (spi_i2s_flag_get(DAC_SPI, SPI_I2S_TDBE_FLAG) == RESET);
    DAC_SPI->dt = val;
 
    
    dac_cs(ch, false);
}

//
void dac_cs(DAC_CHANNEL ch, bool state)
{
    switch (ch)
    {
        case CH_DAC_1:
            state == true ? gpio_bits_reset(GPIOE, GPIO_PINS_12) : gpio_bits_set(GPIOE, GPIO_PINS_12);
        break;
        
        case CH_DAC_2:
            state == true ? gpio_bits_reset(GPIOD, GPIO_PINS_1) : gpio_bits_set(GPIOD, GPIO_PINS_1);
        break;
        
        case CH_DAC_3:
            state == true ? gpio_bits_reset(GPIOD, GPIO_PINS_0) : gpio_bits_set(GPIOD, GPIO_PINS_0);
        break;
        
        case CH_DAC_4:
            state == true ? gpio_bits_reset(GPIOD, GPIO_PINS_3) : gpio_bits_set(GPIOD, GPIO_PINS_3);
        break;
        
        default: break;
    }
}


//
#if 0
void dac_task(void *params)
{
    for (;;)
    {
        vTaskDelay(3000);
        dac_test(CH_DAC_1, 0);  
        dac_test(CH_DAC_2, 0);  
        dac_test(CH_DAC_3, 0);  
        dac_test(CH_DAC_4, 0);  
        vTaskDelay(3000);
        dac_test(CH_DAC_1, 0x7EFF);  
        dac_test(CH_DAC_2, 0x7EFF);  
        dac_test(CH_DAC_3, 0x7EFF);  
        dac_test(CH_DAC_4, 0x7EFF);  
        vTaskDelay(3000);
        dac_test(CH_DAC_1, 0xFFFF);  
        dac_test(CH_DAC_2, 0xFFFF);  
        dac_test(CH_DAC_3, 0xFFFF);  
        dac_test(CH_DAC_4, 0xFFFF); 
    }
}
#endif